Thomas Burns
Thomas Burns

Reputation: 11

Using mouse to paint row of characters in ncurses

I'm trying to use ncurses to paint a row of characters on the screen with the mouse. I'm able to distinguish between a BUTTON1_CLICKED event and BUTTON1_RELEASED event on the screen, but haven't figured out how to paint between them.

The following code prints BUTTON1_CLICKED and BUTTON1_RELEASED at their event positions:

void obstacle_draw(WINDOW * boardWin, int array[LINES][COLS]) {

int ch;
MEVENT event;

keypad(boardWin, TRUE);
mousemask(BUTTON1_CLICKED | BUTTON1_RELEASED | REPORT_MOUSE_POSITION, NULL);
mouseinterval(0);  

while(1) {
    ch = wgetch(boardWin);   
    if (ch == KEY_MOUSE) {   
        getmouse(&event);
        if (event.bstate & BUTTON1_CLICKED) {
            wattron(boardWin, COLOR_PAIR(BLACK_ON_WHITE) | A_REVERSE);
            mvwprintw(boardWin, event.y, event.x, "CLICKED at %d,%d", event.y, event.x);
            wrefresh(boardWin);
        } else if (event.bstate & BUTTON1_RELEASED) {
                wattron(boardWin, COLOR_PAIR(BLACK_ON_WHITE) | A_REVERSE);
            mvwprintw(boardWin, event.y, event.x, "RELEASED at %d,%d", event.y, event.x);
            wrefresh(boardWin);
        }  
    }
    if (ch == '\n') {
        break;
    }
}
return;
}

Adding the following line before my while loop allows me to paint these coordinates once I've made the first mouse click, and without holding the button. But I can't seem to turn it off (I also don't understand how it works):

printf("\033[?1003h\n");

I feel like I've read every ncurses mouse post on stack overflow. What am I missing?

Upvotes: 1

Views: 155

Answers (1)

Thomas Dickey
Thomas Dickey

Reputation: 54525

The mousemask function is used for this:

To make mouse events visible, use the mousemask function. This will set the mouse events to be reported. By default, no mouse events are reported. The function will return a mask to indicate which of the specified mouse events can be reported; on complete failure it returns 0. If oldmask is non-NULL, this function fills the indicated location with the previous value of the given window's mouse event mask.

As a side effect, setting a zero mousemask may turn off the mouse pointer; setting a nonzero mask may turn it on. Whether this happens is device-dependent.

That's actually two questions. The one specific to ncurses is how to turn the mouse off/on (using printf is guaranteed to provide disappointment). The other question is how to enable "any event", and is terminal-specific, seen by ncurses as a different initialization string for the mouse. The terminal descriptions xterm-1002 and xterm-1003 provide examples of this. The XM capability (which is what ncurses looks at) is described in the user_caps manual page.

Just in case there's a third question, the pros/cons of turning the any-event mode have been mentioned a few times on the bug-ncurses mailing list.

Upvotes: 1

Related Questions