various mouse support-related simplifications, improvements, and fixes

git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@4107 35c25a1d-7b9e-4130-9fde-d3aeb78583b8
This commit is contained in:
David Lawrence Ramsey 2007-05-20 23:41:56 +00:00
parent 7f8bfca081
commit 3a5eaeb401
6 changed files with 90 additions and 66 deletions

View File

@ -1,3 +1,14 @@
2007-05-20 David Lawrence Ramsey <pooka109@gmail.com>
* nano.c (do_mouse), prompt.c (do_statusbar_mouse,
do_yesno_prompt), winio.c (do_mouseinput): Fix processing of
mouse events so that those we don't handle are ignored instead
of being erroneously passed through.
* winio.c (do_mouseinput): Simplify handling of mouse events
involving the first mouse button.
* winio.c (do_mouseinput): Improve mouse wheel support to only
move the cursor if we're in the edit window or on the statusbar.
2007-05-15 David Lawrence Ramsey <pooka109@gmail.com> 2007-05-15 David Lawrence Ramsey <pooka109@gmail.com>
* winio.c (do_mouseinput): Add mouse wheel support, per Helmut * winio.c (do_mouseinput): Add mouse wheel support, per Helmut

View File

@ -131,7 +131,7 @@ char *do_browser(char *path, DIR *dir)
{ {
int mouse_x, mouse_y; int mouse_x, mouse_y;
if (!get_mouseinput(&mouse_x, &mouse_y, TRUE)) { if (get_mouseinput(&mouse_x, &mouse_y, TRUE) == 0) {
/* We can click in the edit window to select a /* We can click in the edit window to select a
* filename. */ * filename. */
if (wenclose(edit, mouse_y, mouse_x)) { if (wenclose(edit, mouse_y, mouse_x)) {

View File

@ -1333,7 +1333,7 @@ int do_input(bool *meta_key, bool *func_key, bool *s_or_t, bool
/* If we got a mouse click and it was on a shortcut, read in the /* If we got a mouse click and it was on a shortcut, read in the
* shortcut character. */ * shortcut character. */
if (*func_key && input == KEY_MOUSE) { if (*func_key && input == KEY_MOUSE) {
if (do_mouse()) if (do_mouse() == 1)
input = get_kbinput(edit, meta_key, func_key); input = get_kbinput(edit, meta_key, func_key);
else { else {
*meta_key = FALSE; *meta_key = FALSE;
@ -1491,12 +1491,12 @@ int do_input(bool *meta_key, bool *func_key, bool *s_or_t, bool
#ifndef DISABLE_MOUSE #ifndef DISABLE_MOUSE
/* Handle a mouse click on the edit window or the shortcut list. */ /* Handle a mouse click on the edit window or the shortcut list. */
bool do_mouse(void) int do_mouse(void)
{ {
int mouse_x, mouse_y; int mouse_x, mouse_y;
bool retval = get_mouseinput(&mouse_x, &mouse_y, TRUE); int retval = get_mouseinput(&mouse_x, &mouse_y, TRUE);
if (!retval) { if (retval == 0) {
/* We can click in the edit window to move the cursor. */ /* We can click in the edit window to move the cursor. */
if (wenclose(edit, mouse_y, mouse_x)) { if (wenclose(edit, mouse_y, mouse_x)) {
bool sameline; bool sameline;

View File

@ -76,7 +76,7 @@ int do_statusbar_input(bool *meta_key, bool *func_key, bool *s_or_t,
/* If we got a mouse click and it was on a shortcut, read in the /* If we got a mouse click and it was on a shortcut, read in the
* shortcut character. */ * shortcut character. */
if (*func_key && input == KEY_MOUSE) { if (*func_key && input == KEY_MOUSE) {
if (do_statusbar_mouse()) if (do_statusbar_mouse() == 1)
input = get_kbinput(bottomwin, meta_key, func_key); input = get_kbinput(bottomwin, meta_key, func_key);
else { else {
*meta_key = FALSE; *meta_key = FALSE;
@ -273,12 +273,12 @@ int do_statusbar_input(bool *meta_key, bool *func_key, bool *s_or_t,
#ifndef DISABLE_MOUSE #ifndef DISABLE_MOUSE
/* Handle a mouse click on the statusbar prompt or the shortcut list. */ /* Handle a mouse click on the statusbar prompt or the shortcut list. */
bool do_statusbar_mouse(void) int do_statusbar_mouse(void)
{ {
int mouse_x, mouse_y; int mouse_x, mouse_y;
bool retval = get_mouseinput(&mouse_x, &mouse_y, TRUE); int retval = get_mouseinput(&mouse_x, &mouse_y, TRUE);
if (!retval) { if (retval == 0) {
/* We can click in the statusbar window text to move the /* We can click in the statusbar window text to move the
* cursor. */ * cursor. */
if (wenclose(bottomwin, mouse_y, mouse_x)) { if (wenclose(bottomwin, mouse_y, mouse_x)) {
@ -1337,9 +1337,8 @@ int do_yesno_prompt(bool all, const char *msg)
break; break;
#ifndef DISABLE_MOUSE #ifndef DISABLE_MOUSE
case KEY_MOUSE: case KEY_MOUSE:
get_mouseinput(&mouse_x, &mouse_y, FALSE); if (get_mouseinput(&mouse_x, &mouse_y, FALSE) == 0 &&
wenclose(bottomwin, mouse_y, mouse_x) &&
if (wenclose(bottomwin, mouse_y, mouse_x) &&
!ISSET(NO_HELP) && mouse_x < (width * 2) && !ISSET(NO_HELP) && mouse_x < (width * 2) &&
mouse_y - (2 - no_more_space()) - mouse_y - (2 - no_more_space()) -
editwinrows - 1 >= 0) { editwinrows - 1 >= 0) {

View File

@ -485,7 +485,7 @@ void terminal_init(void);
int do_input(bool *meta_key, bool *func_key, bool *s_or_t, bool int do_input(bool *meta_key, bool *func_key, bool *s_or_t, bool
*ran_func, bool *finished, bool allow_funcs); *ran_func, bool *finished, bool allow_funcs);
#ifndef DISABLE_MOUSE #ifndef DISABLE_MOUSE
bool do_mouse(void); int do_mouse(void);
#endif #endif
void do_output(char *output, size_t output_len, bool allow_cntrls); void do_output(char *output, size_t output_len, bool allow_cntrls);
@ -494,7 +494,7 @@ int do_statusbar_input(bool *meta_key, bool *func_key, bool *s_or_t,
bool *ran_func, bool *finished, bool allow_funcs, void bool *ran_func, bool *finished, bool allow_funcs, void
(*refresh_func)(void)); (*refresh_func)(void));
#ifndef DISABLE_MOUSE #ifndef DISABLE_MOUSE
bool do_statusbar_mouse(void); int do_statusbar_mouse(void);
#endif #endif
void do_statusbar_output(char *output, size_t output_len, bool void do_statusbar_output(char *output, size_t output_len, bool
*got_enter, bool allow_cntrls); *got_enter, bool allow_cntrls);
@ -749,7 +749,7 @@ void unparse_kbinput(char *output, size_t output_len);
int *get_verbatim_kbinput(WINDOW *win, size_t *kbinput_len); int *get_verbatim_kbinput(WINDOW *win, size_t *kbinput_len);
int *parse_verbatim_kbinput(WINDOW *win, size_t *kbinput_len); int *parse_verbatim_kbinput(WINDOW *win, size_t *kbinput_len);
#ifndef DISABLE_MOUSE #ifndef DISABLE_MOUSE
bool get_mouseinput(int *mouse_x, int *mouse_y, bool allow_shortcuts); int get_mouseinput(int *mouse_x, int *mouse_y, bool allow_shortcuts);
#endif #endif
const shortcut *get_shortcut(const shortcut *s_list, int *kbinput, bool const shortcut *get_shortcut(const shortcut *s_list, int *kbinput, bool
*meta_key, bool *func_key); *meta_key, bool *func_key);

View File

@ -1625,17 +1625,18 @@ int *parse_verbatim_kbinput(WINDOW *win, size_t *kbinput_len)
#ifndef DISABLE_MOUSE #ifndef DISABLE_MOUSE
/* Handle any mouse events that may have occurred. We currently handle /* Handle any mouse events that may have occurred. We currently handle
* releases or clicks of the first mouse button. If allow_shortcuts is * releases of the first mouse button. If allow_shortcuts is TRUE,
* TRUE, releasing or clicking on a visible shortcut will put back the * releasing on a visible shortcut will put back the keystroke
* keystroke associated with that shortcut. If NCURSES_MOUSE_VERSION is * associated with that shortcut. If NCURSES_MOUSE_VERSION is at least
* at least 2, we also currently handle presses of the fourth mouse * 2, we also currently handle presses of the fourth mouse button
* button (upward rolls of the mouse wheel) by putting back the * (upward rolls of the mouse wheel) by putting back the keystrokes to
* keystrokes to move up, and presses of the fifth mouse button * move up, and presses of the fifth mouse button (downward rolls of the
* (downward rolls of the mouse wheel) by putting back the keystrokes to * mouse wheel) by putting back the keystrokes to move down. Return -1
* move down. Return FALSE if we don't put back any keystrokes in the * on error, 0 if the mouse event needs to be handled, 1 if it's been
* course of handling mouse events, or TRUE if we do. Assume that * handled by putting back keystrokes that need to be handled. or 2 if
* KEY_MOUSE has already been read in. */ * the mouse event is ignored. Assume that KEY_MOUSE has already been
bool get_mouseinput(int *mouse_x, int *mouse_y, bool allow_shortcuts) * read in. */
int get_mouseinput(int *mouse_x, int *mouse_y, bool allow_shortcuts)
{ {
MEVENT mevent; MEVENT mevent;
@ -1644,20 +1645,19 @@ bool get_mouseinput(int *mouse_x, int *mouse_y, bool allow_shortcuts)
/* First, get the actual mouse event. */ /* First, get the actual mouse event. */
if (getmouse(&mevent) == ERR) if (getmouse(&mevent) == ERR)
return FALSE; return -1;
/* Handle releases or clicks of the first mouse button. */ /* Save the screen coordinates where the mouse event took place. */
if (mevent.bstate & (BUTTON1_RELEASED | BUTTON1_CLICKED)) { *mouse_x = mevent.x;
/* Save the screen coordinates where the mouse event took *mouse_y = mevent.y;
* place. */
*mouse_x = mevent.x;
*mouse_y = mevent.y;
/* Handle releases of the first mouse button. */
if (mevent.bstate & BUTTON1_RELEASED) {
/* If we're allowing shortcuts, the current shortcut list is /* If we're allowing shortcuts, the current shortcut list is
* being displayed on the last two lines of the screen, and the * being displayed on the last two lines of the screen, and the
* mouse event took place inside it, we need to figure out which * first mouse button was pressed inside it, we need to figure
* shortcut was clicked and put back the equivalent keystroke(s) * out which shortcut was clicked and put back the equivalent
* for it. */ * keystroke(s) for it. */
if (allow_shortcuts && !ISSET(NO_HELP) && wenclose(bottomwin, if (allow_shortcuts && !ISSET(NO_HELP) && wenclose(bottomwin,
*mouse_y, *mouse_x)) { *mouse_y, *mouse_x)) {
int i, j; int i, j;
@ -1665,7 +1665,7 @@ bool get_mouseinput(int *mouse_x, int *mouse_y, bool allow_shortcuts)
/* The number of shortcuts in the current shortcut /* The number of shortcuts in the current shortcut
* list. */ * list. */
const shortcut *s = currshortcut; const shortcut *s = currshortcut;
/* The actual shortcut we clicked on, starting at the /* The actual shortcut we released on, starting at the
* first one in the current shortcut list. */ * first one in the current shortcut list. */
/* Get the shortcut lists' length. */ /* Get the shortcut lists' length. */
@ -1694,26 +1694,28 @@ bool get_mouseinput(int *mouse_x, int *mouse_y, bool allow_shortcuts)
* out, and set j to it. */ * out, and set j to it. */
j = *mouse_y - (2 - no_more_space()) - editwinrows - 1; j = *mouse_y - (2 - no_more_space()) - editwinrows - 1;
/* If we're on the statusbar, don't do anything. */ /* Ignore releases of the first mouse button on the
* statusbar. */
if (j < 0) if (j < 0)
return FALSE; return 2;
/* Calculate the x-coordinate relative to the beginning of /* Calculate the x-coordinate relative to the beginning of
* the shortcut list in bottomwin, and add it to j. j * the shortcut list in bottomwin, and add it to j. j
* should now be the index in the shortcut list of the * should now be the index in the shortcut list of the
* shortcut we clicked. */ * shortcut we released on. */
j = (*mouse_x / i) * 2 + j; j = (*mouse_x / i) * 2 + j;
/* Adjust j if we clicked in the last two shortcuts. */ /* Adjust j if we released on the last two shortcuts. */
if ((j >= currslen) && (*mouse_x % i < COLS % i)) if ((j >= currslen) && (*mouse_x % i < COLS % i))
j -= 2; j -= 2;
/* If we're beyond the last shortcut, don't do anything. */ /* Ignore releases of the first mouse button beyond the last
* shortcut. */
if (j >= currslen) if (j >= currslen)
return FALSE; return 2;
/* Go through the shortcut list to determine which shortcut /* Go through the shortcut list to determine which shortcut
* was clicked. */ * we released on. */
for (; j > 0; j--) for (; j > 0; j--)
s = s->next; s = s->next;
@ -1722,41 +1724,53 @@ bool get_mouseinput(int *mouse_x, int *mouse_y, bool allow_shortcuts)
* key, an equivalent primary meta key sequence, or both. */ * key, an equivalent primary meta key sequence, or both. */
if (s->ctrlval != NANO_NO_KEY) { if (s->ctrlval != NANO_NO_KEY) {
unget_kbinput(s->ctrlval, FALSE, FALSE); unget_kbinput(s->ctrlval, FALSE, FALSE);
return TRUE; return 1;
} else if (s->metaval != NANO_NO_KEY) { } else if (s->metaval != NANO_NO_KEY) {
unget_kbinput(s->metaval, TRUE, FALSE); unget_kbinput(s->metaval, TRUE, FALSE);
return TRUE; return 1;
} }
} else } else
return FALSE; /* Handle releases of the first mouse button that aren't on
* the current shortcut list elsewhere. */
return 0;
} }
#if NCURSES_MOUSE_VERSION >= 2 #if NCURSES_MOUSE_VERSION >= 2
/* Handle presses of the fourth mouse button (upward rolls of the /* Handle presses of the fourth mouse button (upward rolls of the
* mouse wheel). */ * mouse wheel) and presses of the fifth mouse button (downward
else if (mevent.bstate & BUTTON4_PRESSED) { * rolls of the mouse wheel) . */
int i = 0; else if (mevent.bstate & (BUTTON4_PRESSED | BUTTON5_PRESSED)) {
if (wenclose(edit, *mouse_y, *mouse_x) || wenclose(bottomwin,
*mouse_y, *mouse_x)) {
/* Calculate the y-coordinate relative to the beginning of
* the shortcut list in bottomwin, i.e. with the sizes of
* topwin, edit, and the first line of bottomwin subtracted
* out, and set i to it. */
int i = *mouse_y - (2 - no_more_space()) - editwinrows - 1;
/* One upward roll of the mouse wheel is equivalent to moving up /* Ignore presses of the fourth mouse button and presses of
* three lines. */ * the fifth mouse button below the statusbar. */
for (; i < 3; i++) if (i >= 0)
unget_kbinput(NANO_PREVLINE_KEY, FALSE, FALSE); return 2;
return TRUE; /* One upward roll of the mouse wheel is equivalent to
/* Handle presses of the fifth mouse button (downward rolls of the * moving up three lines, and one downward roll of the mouse
* mouse wheel). */ * wheel is equivalent to moving down three lines. */
} else if (mevent.bstate & BUTTON5_PRESSED) { for (i = 0; i < 3; i++)
int i = 0; unget_kbinput((mevent.bstate & BUTTON4_PRESSED) ?
NANO_PREVLINE_KEY : NANO_NEXTLINE_KEY, FALSE,
FALSE);
/* One downward roll of the mouse wheel is equivalent to moving return 1;
* down three lines. */ } else
for (; i < 3; i++) /* Ignore presses of the fourth mouse button and presses of
unget_kbinput(NANO_NEXTLINE_KEY, FALSE, FALSE); * the fifth mouse buttons that aren't on the edit window or
* the statusbar. */
return TRUE; return 2;
} }
#endif #endif
else else
return FALSE; /* Ignore all other mouse events. */
return 2;
} }
#endif /* !DISABLE_MOUSE */ #endif /* !DISABLE_MOUSE */