2 * ========================================================================
3 * Copyright 2006-2007 University of Washington
4 * Copyright 2013-2022 Eduardo Chappa
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * ========================================================================
18 #include "../headers.h"
33 /* useful definitions */
34 #define XTERM_MOUSE_ON "\033[?1000h" /* DECSET with parm 1000 */
35 #define XTERM_MOUSE_OFF "\033[?1000l" /* DECRST with parm 1000 */
38 /* useful declarations */
39 static int mexist
= 0; /* is the mouse driver installed? */
40 static unsigned mnoop
;
44 /* internal prototypes */
51 * init_mouse - check for xterm and initialize mouse tracking if present...
59 if(getenv("DISPLAY")){
61 kpinsert("\033[M", KEY_XTERM_MOUSE
, 1);
62 return(mexist
= TRUE
);
70 * end_mouse - clear xterm mouse tracking if present...
76 mexist
= 0; /* just see if it exists here. */
83 * mouseexist - function to let outsiders know if mouse is turned on
94 * mouseon - call made available for programs calling pico to turn ON the
100 fputs(XTERM_MOUSE_ON
, stdout
);
105 * mouseon - call made available for programs calling pico to turn OFF the
111 fputs(XTERM_MOUSE_OFF
, stdout
);
116 * checkmouse - look for mouse events in key menu and return
120 checkmouse(unsigned long *ch
, int down
, int mcol
, int mrow
)
126 if(!mexist
|| mcol
< 0 || mrow
< 0)
129 if(down
) /* button down */
132 for(mp
= mfunc
; mp
; mp
= mp
->next
)
133 if(mp
->action
&& M_ACTIVE(mrow
, mcol
, mp
))
139 r
= (*mp
->action
)(down
? M_EVENT_DOWN
: M_EVENT_UP
,
140 mrow
, mcol
, M_BUTTON_LEFT
, 0);
147 while(1){ /* see if we understand event */
153 if(M_ACTIVE(mrow
, mcol
, &menuitems
[i
]))
159 if(down
){ /* button down */
160 oindex
= i
; /* remember where */
162 && menuitems
[i
].label_hiliter
!= NULL
163 && menuitems
[i
].val
!= mnoop
) /* invert label */
164 (*menuitems
[i
].label_hiliter
)(1, &menuitems
[i
]);
166 else{ /* button up */
169 *ch
= menuitems
[i
].val
;
179 && menuitems
[oindex
].label_hiliter
!= NULL
180 && menuitems
[oindex
].val
!= mnoop
)
181 (*menuitems
[oindex
].label_hiliter
)(0, &menuitems
[oindex
]);
188 * invert_label - highlight the label of the given menu item.
191 invert_label(int state
, MENUITEM
*m
)
194 int col_offset
, savettrow
, savettcol
;
197 get_cursor(&savettrow
, &savettcol
);
200 * Leave the command name bold
202 col_offset
= (state
|| !(lp
=strchr(m
->label
, ' '))) ? 0 : (lp
- m
->label
);
203 movecursor((int)m
->tl
.r
, (int)m
->tl
.c
+ col_offset
);
206 for(i
= m
->tl
.r
; i
<= m
->br
.r
; i
++)
207 for(j
= m
->tl
.c
+ col_offset
; j
<= m
->br
.c
; j
++)
208 if(i
== m
->lbl
.r
&& j
== m
->lbl
.c
+ col_offset
&& m
->label
){
209 lp
= m
->label
+ col_offset
; /* show label?? */
210 while(*lp
&& j
++ < m
->br
.c
)
221 movecursor(savettrow
, savettcol
);
226 #define MOUSE_BUTTONS 3
228 static int mexist
= 0; /* is the mouse driver installed? */
229 static int nbuttons
; /* number of buttons on the mouse */
230 static unsigned mnoop
;
233 * init_mouse - check for and initialize mouse driver...
239 nbuttons
= MOUSE_BUTTONS
;
240 return (mexist
= TRUE
); /* Mouse always exists under windows */
244 * end_mouse - a no-op on Windows
252 * mouseexist - function to let outsiders know if mouse is turned on
262 * checkmouse - Check mouse and return mapped command.
265 * NOTE: "down", "xxx", and "yyy" aren't used under windows.
268 checkmouse (unsigned long *ch
, int ddd
, int xxx
, int yyy
)
270 static int oindex
; /* Index of previous mouse down. */
271 int mcol
; /* current mouse column */
272 int mrow
; /* current mouse row */
274 int rv
= 0; /* TRUE when we have something to return. */
282 /* Mouse installed? */
286 if (!mswin_getmouseevent (&mouse
))
290 /* Location of mouse event. */
291 mcol
= mouse
.nColumn
;
297 * If there is a tracking function it gets all the mouse events
298 * regardless of where they occur.
300 if (mtrack
!= NULL
) {
301 r
= mtrack (mouse
.event
, mrow
, mcol
, mouse
.button
, mouse
.keys
);
312 /* Mouse down or up? */
313 if (mouse
.event
== M_EVENT_DOWN
) { /* button down */
314 oindex
= -1; /* No Previous mouse down. */
318 /* In special screen region? */
319 for(mp
= mfunc
; mp
; mp
= mp
->next
)
320 if(mp
->action
&& M_ACTIVE(mrow
, mcol
, mp
))
325 r
= (*mp
->action
)(mouse
.event
, mrow
, mcol
, mouse
.button
, mouse
.keys
);
331 else if(mouse
.button
== M_BUTTON_LEFT
){
333 /* In any of the menuitems? */
334 while(1){ /* see if we understand event */
336 i
= -1; /* Not Found. */
340 if(M_ACTIVE(mrow
, mcol
, &menuitems
[i
]))
346 /* Now, was that a mouse down or mouse up? */
347 if (mouse
.event
== M_EVENT_DOWN
) { /* button down */
348 oindex
= i
; /* remember where */
349 if(i
!= -1){ /* invert label */
350 if(menuitems
[i
].label_hiliter
!= NULL
)
351 (*menuitems
[i
].label_hiliter
)(1, &menuitems
[i
]);
353 invert_label(1, &menuitems
[i
]);
356 else if (mouse
.event
== M_EVENT_UP
) {/* button up */
357 if (oindex
!= -1) { /* If up in menu item. */
358 if (i
== oindex
){ /* And same item down in. */
359 *ch
= menuitems
[i
].val
; /* Return menu character. */
365 else if(mouse
.button
== M_BUTTON_RIGHT
){
366 if (mouse
.event
== M_EVENT_UP
) {
367 mswin_keymenu_popup();
371 /* If this is mouse up AND there was a mouse down in a menu item
372 * then uninvert that menu item */
373 if(mouse
.event
== M_EVENT_UP
&& oindex
!= -1){
374 if(menuitems
[oindex
].label_hiliter
!= NULL
)
375 (*menuitems
[oindex
].label_hiliter
)(0, &menuitems
[oindex
]);
377 invert_label(0, &menuitems
[oindex
]);
384 * invert_label - highlight the label of the given menu item.
387 invert_label(int state
, MENUITEM
*m
)
395 COLOR_PAIR
*lastc
= NULL
;
400 mswin_getpos (&r
, &c
); /* get cursor position */
401 wasShown
= mswin_showcaret (0);
402 old_state
= mswin_getrevstate ();
404 * Leave the command name bold
406 col_offset
= (state
|| !(lp
=strchr(m
->label
, ' '))) ? 0 : (lp
- m
->label
);
407 (*term
.t_move
)(m
->tl
.r
, m
->tl
.c
+ col_offset
);
409 lastc
= pico_set_colorp(m
->kncp
, PSC_REV
|PSC_RET
);
410 else if(!state
&& m
->klcp
)
411 lastc
= pico_set_colorp(m
->klcp
, PSC_NORM
|PSC_RET
);
413 (*term
.t_rev
)(state
);
415 for(i
= m
->tl
.r
; i
<= m
->br
.r
; i
++) {
416 for(j
= m
->tl
.c
+ col_offset
; j
<= m
->br
.c
; j
++) {
417 if(i
== m
->lbl
.r
&& j
== m
->lbl
.c
+ col_offset
){ /* show label?? */
418 lp
= m
->label
+ col_offset
;
419 while(*lp
&& j
++ < m
->br
.c
)
420 (*term
.t_putchar
)(*lp
++);
425 (*term
.t_putchar
)(' ');
430 (void)pico_set_colorp(lastc
, PSC_NONE
);
431 free_color_pair(&lastc
);
434 (*term
.t_rev
)(old_state
);
436 mswin_showcaret (wasShown
);
440 #endif /* _WINDOWS */