1 #if !defined(lint) && !defined(DOS)
2 static char rcsid
[] = "$Id: mouse.c 769 2007-10-24 00:15:40Z hubert@u.washington.edu $";
6 * ========================================================================
7 * Copyright 2006-2007 University of Washington
8 * Copyright 2013-2016 Eduardo Chappa
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * ========================================================================
22 #include "../headers.h"
37 /* useful definitions */
38 #define XTERM_MOUSE_ON "\033[?1000h" /* DECSET with parm 1000 */
39 #define XTERM_MOUSE_OFF "\033[?1000l" /* DECRST with parm 1000 */
42 /* useful declarations */
43 static int mexist
= 0; /* is the mouse driver installed? */
44 static unsigned mnoop
;
48 /* internal prototypes */
55 * init_mouse - check for xterm and initialize mouse tracking if present...
63 if(getenv("DISPLAY")){
65 kpinsert("\033[M", KEY_XTERM_MOUSE
, 1);
66 return(mexist
= TRUE
);
74 * end_mouse - clear xterm mouse tracking if present...
80 mexist
= 0; /* just see if it exists here. */
87 * mouseexist - function to let outsiders know if mouse is turned on
98 * mouseon - call made available for programs calling pico to turn ON the
104 fputs(XTERM_MOUSE_ON
, stdout
);
109 * mouseon - call made available for programs calling pico to turn OFF the
115 fputs(XTERM_MOUSE_OFF
, stdout
);
120 * checkmouse - look for mouse events in key menu and return
124 checkmouse(unsigned long *ch
, int down
, int mcol
, int mrow
)
130 if(!mexist
|| mcol
< 0 || mrow
< 0)
133 if(down
) /* button down */
136 for(mp
= mfunc
; mp
; mp
= mp
->next
)
137 if(mp
->action
&& M_ACTIVE(mrow
, mcol
, mp
))
143 r
= (*mp
->action
)(down
? M_EVENT_DOWN
: M_EVENT_UP
,
144 mrow
, mcol
, M_BUTTON_LEFT
, 0);
151 while(1){ /* see if we understand event */
157 if(M_ACTIVE(mrow
, mcol
, &menuitems
[i
]))
163 if(down
){ /* button down */
164 oindex
= i
; /* remember where */
166 && menuitems
[i
].label_hiliter
!= NULL
167 && menuitems
[i
].val
!= mnoop
) /* invert label */
168 (*menuitems
[i
].label_hiliter
)(1, &menuitems
[i
]);
170 else{ /* button up */
173 *ch
= menuitems
[i
].val
;
183 && menuitems
[oindex
].label_hiliter
!= NULL
184 && menuitems
[oindex
].val
!= mnoop
)
185 (*menuitems
[oindex
].label_hiliter
)(0, &menuitems
[oindex
]);
192 * invert_label - highlight the label of the given menu item.
195 invert_label(int state
, MENUITEM
*m
)
198 int col_offset
, savettrow
, savettcol
;
201 get_cursor(&savettrow
, &savettcol
);
204 * Leave the command name bold
206 col_offset
= (state
|| !(lp
=strchr(m
->label
, ' '))) ? 0 : (lp
- m
->label
);
207 movecursor((int)m
->tl
.r
, (int)m
->tl
.c
+ col_offset
);
210 for(i
= m
->tl
.r
; i
<= m
->br
.r
; i
++)
211 for(j
= m
->tl
.c
+ col_offset
; j
<= m
->br
.c
; j
++)
212 if(i
== m
->lbl
.r
&& j
== m
->lbl
.c
+ col_offset
&& m
->label
){
213 lp
= m
->label
+ col_offset
; /* show label?? */
214 while(*lp
&& j
++ < m
->br
.c
)
225 movecursor(savettrow
, savettcol
);
230 #define MOUSE_BUTTONS 3
232 static int mexist
= 0; /* is the mouse driver installed? */
233 static int nbuttons
; /* number of buttons on the mouse */
234 static unsigned mnoop
;
237 * init_mouse - check for and initialize mouse driver...
243 nbuttons
= MOUSE_BUTTONS
;
244 return (mexist
= TRUE
); /* Mouse always exists under windows */
248 * end_mouse - a no-op on Windows
256 * mouseexist - function to let outsiders know if mouse is turned on
266 * checkmouse - Check mouse and return maped command.
269 * NOTE: "down", "xxx", and "yyy" aren't used under windows.
272 checkmouse (unsigned long *ch
, int ddd
, int xxx
, int yyy
)
274 static int oindex
; /* Index of previous mouse down. */
275 int mcol
; /* current mouse column */
276 int mrow
; /* current mouse row */
278 int rv
= 0; /* TRUE when we have something to return. */
286 /* Mouse installed? */
290 if (!mswin_getmouseevent (&mouse
))
294 /* Location of mouse event. */
295 mcol
= mouse
.nColumn
;
301 * If there is a tracking function it gets all the mouse events
302 * reguardless of where they occur.
304 if (mtrack
!= NULL
) {
305 r
= mtrack (mouse
.event
, mrow
, mcol
, mouse
.button
, mouse
.keys
);
316 /* Mouse down or up? */
317 if (mouse
.event
== M_EVENT_DOWN
) { /* button down */
318 oindex
= -1; /* No Previous mouse down. */
322 /* In special screen region? */
323 for(mp
= mfunc
; mp
; mp
= mp
->next
)
324 if(mp
->action
&& M_ACTIVE(mrow
, mcol
, mp
))
329 r
= (*mp
->action
)(mouse
.event
, mrow
, mcol
, mouse
.button
, mouse
.keys
);
335 else if(mouse
.button
== M_BUTTON_LEFT
){
337 /* In any of the menuitems? */
338 while(1){ /* see if we understand event */
340 i
= -1; /* Not Found. */
344 if(M_ACTIVE(mrow
, mcol
, &menuitems
[i
]))
350 /* Now, was that a mouse down or mouse up? */
351 if (mouse
.event
== M_EVENT_DOWN
) { /* button down */
352 oindex
= i
; /* remember where */
353 if(i
!= -1){ /* invert label */
354 if(menuitems
[i
].label_hiliter
!= NULL
)
355 (*menuitems
[i
].label_hiliter
)(1, &menuitems
[i
]);
357 invert_label(1, &menuitems
[i
]);
360 else if (mouse
.event
== M_EVENT_UP
) {/* button up */
361 if (oindex
!= -1) { /* If up in menu item. */
362 if (i
== oindex
){ /* And same item down in. */
363 *ch
= menuitems
[i
].val
; /* Return menu character. */
369 else if(mouse
.button
== M_BUTTON_RIGHT
){
370 if (mouse
.event
== M_EVENT_UP
) {
371 mswin_keymenu_popup();
375 /* If this is mouse up AND there was a mouse down in a menu item
376 * then uninvert that menu item */
377 if(mouse
.event
== M_EVENT_UP
&& oindex
!= -1){
378 if(menuitems
[oindex
].label_hiliter
!= NULL
)
379 (*menuitems
[oindex
].label_hiliter
)(0, &menuitems
[oindex
]);
381 invert_label(0, &menuitems
[oindex
]);
388 * invert_label - highlight the label of the given menu item.
391 invert_label(int state
, MENUITEM
*m
)
399 COLOR_PAIR
*lastc
= NULL
;
404 mswin_getpos (&r
, &c
); /* get cursor position */
405 wasShown
= mswin_showcaret (0);
406 old_state
= mswin_getrevstate ();
408 * Leave the command name bold
410 col_offset
= (state
|| !(lp
=strchr(m
->label
, ' '))) ? 0 : (lp
- m
->label
);
411 (*term
.t_move
)(m
->tl
.r
, m
->tl
.c
+ col_offset
);
413 lastc
= pico_set_colorp(m
->kncp
, PSC_REV
|PSC_RET
);
414 else if(!state
&& m
->klcp
)
415 lastc
= pico_set_colorp(m
->klcp
, PSC_NORM
|PSC_RET
);
417 (*term
.t_rev
)(state
);
419 for(i
= m
->tl
.r
; i
<= m
->br
.r
; i
++) {
420 for(j
= m
->tl
.c
+ col_offset
; j
<= m
->br
.c
; j
++) {
421 if(i
== m
->lbl
.r
&& j
== m
->lbl
.c
+ col_offset
){ /* show label?? */
422 lp
= m
->label
+ col_offset
;
423 while(*lp
&& j
++ < m
->br
.c
)
424 (*term
.t_putchar
)(*lp
++);
429 (*term
.t_putchar
)(' ');
434 (void)pico_set_colorp(lastc
, PSC_NONE
);
435 free_color_pair(&lastc
);
438 (*term
.t_rev
)(old_state
);
440 mswin_showcaret (wasShown
);
444 #endif /* _WINDOWS */