using the mouse with ncurses

This is a discussion on using the mouse with ncurses within the Linux Programming forums, part of the Platform Specific Boards category; Hello. I am having a problem using the mouse with ncurses. I am 100% sure that my code is correct ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    4

    Question using the mouse with ncurses

    Hello. I am having a problem using the mouse with ncurses. I am 100% sure that my code is correct and it compiles fine. When I run my program it is not picking up the mouse events. I first tried running the program in Konsole, then I tried xterm, and finally I tried aterm but it's still not working. Does anyone have any idea why the mouse isn't working? Or where I might find the answer?
    Note: I compiled with gcc v3.2.2 and I'm using ncurses v5.2

    Thank you for your time.

  2. #2
    Me -=SoKrA=-'s Avatar
    Join Date
    Oct 2002
    Location
    Europe
    Posts
    448
    Post your code for picking up the mouse events. It might be a logic problem. Have you tried with a `real' console? ie. Have you tried it outside X? Mind you, the mouse may not be intialized, not sure...
    SoKrA-BTS "Judge not the program I made, but the one I've yet to code"
    I say what I say, I mean what I mean.
    IDE: emacs + make + gcc and proud of it.

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    4
    The following code is from the ncurses programming how-to and it doesn't even work.
    Code:
    #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(void) {
    	int c, choice = 0;
    	WINDOW *menu_win;
    	MEVENT *event;
    	
    	initscr();
    	clear();
    	noecho();
    	cbreak();
    	
    	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);
    	
    	menu_win = newwin(HEIGHT, WIDTH, starty, startx);
    	print_menu(menu_win, 1);
    	
    	mousemask(ALL_MOUSE_EVENTS, NULL);
    	
    	while(1) {
    		c = wgetch(menu_win);
    		switch(c) {
    			case KEY_MOUSE:
    				if(getmouse(event) == OK) {
    					if(event->bstate & BUTTON1_PRESSED) {
    						report_choice(event->x + 1, event->y + 1, &choice);
    						if(choice == -1)
    							goto end;
    						mvprintw(22, 1, "Choice made is: %d String chosen is \"%10s\"", choice,
    							choices[choice - 1]);
    						refresh();
    					}
    				}
    				print_menu(menu_win, choice);
    				break;
    		}
    	}
    end:
    	endwin();
    	return 0;
    }
    
    void print_menu(WINDOW *menu_win, int highlight) {
    	int x, y, i;
    	y = 2;
    	x = 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);
    }
    
    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;
    		}
    	}
    }
    That's not my code above but it should work, but it doesn't. And I have tried it from a virtual console. Maybe there is something wrong with my ncurses installation. Anyways, thanks for the reply.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need Help in Mouse Pointer
    By obaid in forum C++ Programming
    Replies: 3
    Last Post: 12-07-2006, 03:33 AM
  2. Problem in mouse position
    By Arangol in forum Game Programming
    Replies: 6
    Last Post: 08-08-2006, 08:07 AM
  3. Mouse scroll stopped working
    By Waldo2k2 in forum Tech Board
    Replies: 2
    Last Post: 10-12-2004, 12:25 PM
  4. Making a mouse hover button, API style
    By hanhao in forum C++ Programming
    Replies: 1
    Last Post: 05-27-2004, 07:17 AM
  5. Game Design Topic #2 - Keyboard or Mouse?
    By TechWins in forum Game Programming
    Replies: 4
    Last Post: 10-08-2002, 04:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21