LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-15-2004, 08:03 PM   #1
muhazam
LQ Newbie
 
Registered: Oct 2003
Location: Malaysia, KL.
Posts: 6

Rep: Reputation: 0
Mouse programming in curses


Hi, is there any linux curses expert around here ?

I have myself a supposed-to-work curses code (sample from a website) that uses mouse but it does not seem to be able to capture mouse events. The code is as attached.

Pls try to compile it for me [gcc -o sample sample.c -lcurses] and see. What could be the problem. Is it machine dependent ? My machine is a normal IBM clone with PS/2 mouse. I don't think it causes the problem. Or, can anyone suggest other alternative to control mouse from text based program in linux, without extra installation.



thx. Muhazam

=========================================

#include <ncurses.h>
#define WIDTH 30
#define HEIGHT 10

int startx = 0;
int starty = 0;

char *choices[] = { "Choice 1",
"Choice 2",
"Choice 3",
"Choice 4",
"Exit", };

int n_choices = sizeof(choices) / sizeof(char *);

void print_menu(WINDOW *menu_win, int highlight);
void report_choice(int mouse_x, int mouse_y, int *p_choice);

int main()
{
int c, choice = 0;
WINDOW *menu_win;
MEVENT event;

/* Initialize curses */
initscr();
clear();
noecho();
cbreak(); //Line buffering disabled. pass on everything

/* Try to put the window in the middle of screen */
startx = (80 - WIDTH) / 2;
starty = (24 - HEIGHT) / 2;
attron(A_REVERSE);
mvprintw(23, 1, "Click on Exit to quit (Works best in a virtual console)");
refresh();
attroff(A_REVERSE);

/* Print the menu for the first time */
menu_win = newwin(HEIGHT, WIDTH, starty, startx);
print_menu(menu_win, 1);

/* Get all the mouse events */
mousemask(ALL_MOUSE_EVENTS, NULL);
while (1) {
c = wgetch(menu_win);
switch (c) {
case KEY_MOUSE:
if (getmouse(&event) == OK) { /* When the user clicks left mouse button */
if (event.bstate & BUTTON1_PRESSED) {
report_choice(event.x + 1, event.y + 1, &choice);
if (choice == -1) //Exit chosen
goto end;
mvprintw(22, 1, "Choice made");
refresh(); } }
print_menu(menu_win, choice);
break; } }

end:

endwin();

return 0;
}

void print_menu(WINDOW *menu_win, int highlight)
{
int x, y, i;
x = 2;
y = 2;

box(menu_win, 0, 0);

for (i = 0; i < n_choices; ++i) {
if (highlight == i + 1) {
wattron(menu_win, A_REVERSE);
mvwprintw(menu_win, y, x, "%s", choices[i]);
wattroff(menu_win, A_REVERSE); }
else
mvwprintw(menu_win, y, x, "%s", choices[i]);
++y; }

wrefresh(menu_win);
}

/* Report the choice according to mouse position */
void report_choice(int mouse_x, int mouse_y, int *p_choice)
{
int i,j, choice;

i = startx + 2;
j = starty + 3;

for (choice = 0; choice < n_choices; ++choice)
if (mouse_y == j + choice && mouse_x >= i && mouse_x <= i + strlen(choices[choice])) {
if (choice == n_choices - 1)
*p_choice = -1;
else
*p_choice = choice + 1;
break; }
}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Programming extra mouse buttons? dive Slackware 1 07-23-2005 12:00 AM
how to use mouse in C graphics programming... bastin_gh Programming 5 07-13-2005 11:47 AM
Mouse Programming hugoveiga Programming 2 01-22-2005 11:00 AM
programming the mouse in a VESA graphic mode in Linux Zither Programming 5 03-18-2004 05:35 AM
mouse device programming, differences between busmouse/mousedev? infamous41md Programming 0 03-06-2004 11:08 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:32 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration