2 Copyright (C) 1994 Miguel de Icaza.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 #include <sys/types.h>
32 #include "key.h" /* For mi_getch() */
34 int menubar_visible
= 1; /* This is the new default */
37 menu_scan_hotkey (Menu
*menu
)
39 char *cp
= strchr (menu
->name
, '&');
41 if (cp
!= NULL
&& cp
[1] != '\0') {
42 g_strlcpy (cp
, cp
+ 1, strlen (cp
));
43 menu
->hotkey
= tolower (*cp
);
49 create_menu (char *name
, menu_entry
*entries
, int count
, char *help_node
)
54 menu
= (Menu
*) g_malloc (sizeof (*menu
));
56 menu
->max_entry_len
= 20;
57 menu
->entries
= entries
;
59 if (entries
!= (menu_entry
*) NULL
) {
60 register menu_entry
* mp
;
61 for (mp
= entries
; count
--; mp
++) {
62 if (mp
->text
[0] != '\0') {
64 mp
->text
= _(mp
->text
);
65 #endif /* ENABLE_NLS */
66 cp
= strchr (mp
->text
,'&');
68 if (cp
!= NULL
&& *(cp
+1) != '\0') {
69 mp
->hot_key
= tolower (*(cp
+1));
70 menu
->max_entry_len
= max (strlen (mp
->text
) - 1,
73 menu
->max_entry_len
= max (strlen (mp
->text
),
80 menu
->name
= g_strdup (name
);
81 menu_scan_hotkey(menu
);
83 menu
->help_node
= g_strdup (help_node
);
87 static void menubar_drop_compute (WMenu
*menubar
)
89 menubar
->max_entry_len
= menubar
->menu
[menubar
->selected
]->max_entry_len
;
92 static void menubar_paint_idx (WMenu
*menubar
, int idx
, int color
)
94 const Menu
*menu
= menubar
->menu
[menubar
->selected
];
95 const int y
= 2 + idx
;
96 int x
= menubar
-> menu
[menubar
->selected
]->start_x
;
98 if (x
+ menubar
->max_entry_len
+ 3 > menubar
->widget
.cols
)
99 x
= menubar
->widget
.cols
- menubar
->max_entry_len
- 3;
101 widget_move (&menubar
->widget
, y
, x
);
103 hline (' ', menubar
->max_entry_len
+2);
104 if (!*menu
->entries
[idx
].text
) {
105 attrset (SELECTED_COLOR
);
106 widget_move (&menubar
->widget
, y
, x
+ 1);
107 hline (slow_terminal
? ' ' : ACS_HLINE
, menubar
->max_entry_len
);
111 addch((unsigned char)menu
->entries
[idx
].first_letter
);
112 for (text
= menu
->entries
[idx
].text
; *text
; text
++)
117 attrset (color
== MENU_SELECTED_COLOR
?
118 MENU_HOTSEL_COLOR
: MENU_HOT_COLOR
);
124 widget_move (&menubar
->widget
, y
, x
+ 1);
127 static inline void menubar_draw_drop (WMenu
*menubar
)
129 const int count
= (menubar
->menu
[menubar
->selected
])->count
;
131 int sel
= menubar
->subsel
;
132 int column
= menubar
-> menu
[menubar
->selected
]->start_x
- 1;
134 if (column
+ menubar
->max_entry_len
+ 4 > menubar
->widget
.cols
)
135 column
= menubar
->widget
.cols
- menubar
->max_entry_len
- 4;
137 attrset (SELECTED_COLOR
);
138 draw_box (menubar
->widget
.parent
,
139 menubar
->widget
.y
+1, menubar
->widget
.x
+ column
,
140 count
+2, menubar
->max_entry_len
+ 4);
143 for (i
= 0; i
< count
; i
++){
146 menubar_paint_idx (menubar
, i
, MENU_ENTRY_COLOR
);
148 menubar_paint_idx (menubar
, sel
, MENU_SELECTED_COLOR
);
151 static void menubar_draw (WMenu
*menubar
)
153 const int items
= menubar
->items
;
156 /* First draw the complete menubar */
157 attrset (SELECTED_COLOR
);
158 widget_move (&menubar
->widget
, 0, 0);
160 /* ncurses bug: it should work with hline but it does not */
161 for (i
= menubar
->widget
.cols
; i
; i
--)
164 attrset (SELECTED_COLOR
);
165 /* Now each one of the entries */
166 for (i
= 0; i
< items
; i
++){
168 attrset(i
== menubar
->selected
?MENU_SELECTED_COLOR
:SELECTED_COLOR
);
169 widget_move (&menubar
->widget
, 0, menubar
->menu
[i
]->start_x
);
170 printw ("%s", menubar
->menu
[i
]->name
);
173 if (menubar
->dropped
)
174 menubar_draw_drop (menubar
);
176 widget_move (&menubar
->widget
, 0,
177 menubar
-> menu
[menubar
->selected
]->start_x
);
180 static inline void menubar_remove (WMenu
*menubar
)
183 if (menubar
->dropped
){
184 menubar
->dropped
= 0;
186 menubar
->dropped
= 1;
190 static void menubar_left (WMenu
*menu
)
192 menubar_remove (menu
);
193 menu
->selected
= (menu
->selected
- 1) % menu
->items
;
194 if (menu
->selected
< 0)
195 menu
->selected
= menu
->items
-1;
196 menubar_drop_compute (menu
);
200 static void menubar_right (WMenu
*menu
)
202 menubar_remove (menu
);
203 menu
->selected
= (menu
->selected
+ 1) % menu
->items
;
204 menubar_drop_compute (menu
);
209 menubar_finish (WMenu
*menubar
)
211 menubar
->dropped
= 0;
213 menubar
->widget
.lines
= 1;
214 widget_want_hotkey (menubar
->widget
, 0);
216 dlg_select_by_id (menubar
->widget
.parent
, menubar
->previous_widget
);
220 static void menubar_drop (WMenu
*menubar
, int selected
)
222 menubar
->dropped
= 1;
223 menubar
->selected
= selected
;
225 menubar_drop_compute (menubar
);
226 menubar_draw (menubar
);
229 static void menubar_execute (WMenu
*menubar
, int entry
)
231 const Menu
*menu
= menubar
->menu
[menubar
->selected
];
232 const callfn call_back
= menu
->entries
[entry
].call_back
;
234 is_right
= menubar
->selected
!= 0;
236 /* This used to be the other way round, i.e. first callback and
237 then menubar_finish. The new order (hack?) is needed to make
238 change_panel () work which is used in quick_view_cmd () -- Norbert
240 menubar_finish (menubar
);
245 static void menubar_move (WMenu
*menubar
, int step
)
247 const Menu
*menu
= menubar
->menu
[menubar
->selected
];
249 menubar_paint_idx (menubar
, menubar
->subsel
, MENU_ENTRY_COLOR
);
251 menubar
->subsel
+= step
;
252 if (menubar
->subsel
< 0)
253 menubar
->subsel
= menu
->count
- 1;
255 menubar
->subsel
%= menu
->count
;
256 } while (!menu
->entries
[menubar
->subsel
].call_back
);
257 menubar_paint_idx (menubar
, menubar
->subsel
, MENU_SELECTED_COLOR
);
260 static int menubar_handle_key (WMenu
*menubar
, int key
)
265 if (key
< 256 && isalpha (key
)) /* Linux libc.so.5.x.x bug fix */
268 if (is_abort_char (key
)){
269 menubar_finish (menubar
);
273 if (key
== KEY_F(1)) {
274 if (menubar
->dropped
) {
275 interactive_display (NULL
,
276 (menubar
->menu
[menubar
->selected
])->help_node
);
278 interactive_display (NULL
, "[Menu Bar]");
280 menubar_draw (menubar
);
284 if (key
== KEY_LEFT
|| key
== XCTRL('b')){
285 menubar_left (menubar
);
287 } else if (key
== KEY_RIGHT
|| key
== XCTRL ('f')){
288 menubar_right (menubar
);
292 if (!menubar
->dropped
){
293 const int items
= menubar
->items
;
294 for (i
= 0; i
< items
; i
++){
295 const Menu
*menu
= menubar
->menu
[i
];
297 if (menu
->hotkey
== key
){
298 menubar_drop (menubar
, i
);
302 if (key
== KEY_ENTER
|| key
== XCTRL ('n') || key
== KEY_DOWN
304 menubar_drop (menubar
, menubar
->selected
);
309 const int selected
= menubar
->selected
;
310 const Menu
*menu
= menubar
->menu
[selected
];
311 const int items
= menu
->count
;
313 for (i
= 0; i
< items
; i
++){
314 if (!menu
->entries
[i
].call_back
)
317 if (key
!= menu
->entries
[i
].hot_key
)
320 menubar_execute (menubar
, i
);
324 if (key
== KEY_ENTER
|| key
== '\n'){
325 menubar_execute (menubar
, menubar
->subsel
);
330 if (key
== KEY_DOWN
|| key
== XCTRL ('n'))
331 menubar_move (menubar
, 1);
333 if (key
== KEY_UP
|| key
== XCTRL ('p'))
334 menubar_move (menubar
, -1);
340 menubar_callback (WMenu
*menubar
, widget_msg_t msg
, int parm
)
343 /* We do not want the focus unless we have been activated */
345 if (!menubar
->active
)
346 return MSG_NOT_HANDLED
;
348 widget_want_cursor (menubar
->widget
, 1);
350 /* Trick to get all the mouse events */
351 menubar
->widget
.lines
= LINES
;
353 /* Trick to get all of the hotkeys */
354 widget_want_hotkey (menubar
->widget
, 1);
356 menubar_drop_compute (menubar
);
357 menubar_draw (menubar
);
360 /* We don't want the buttonbar to activate while using the menubar */
363 if (menubar
->active
) {
364 menubar_handle_key (menubar
, parm
);
367 return MSG_NOT_HANDLED
;
370 /* Put the cursor in a suitable place */
371 return MSG_NOT_HANDLED
;
375 return MSG_NOT_HANDLED
;
377 widget_want_cursor (menubar
->widget
, 0);
383 menubar_draw (menubar
);
387 return default_proc (msg
, parm
);
392 menubar_event (Gpm_Event
*event
, WMenu
*menubar
)
396 int left_x
, right_x
, bottom_y
;
398 if (!(event
->type
& (GPM_UP
|GPM_DOWN
|GPM_DRAG
)))
401 if (!menubar
->dropped
){
402 menubar
->previous_widget
= menubar
->widget
.parent
->current
->dlg_id
;
404 menubar
->dropped
= 1;
409 /* Mouse operations on the menubar */
410 if (event
->y
== 1 || !was_active
){
411 if (event
->type
& GPM_UP
)
415 while (new_selection
< menubar
->items
416 && event
->x
> menubar
->menu
[new_selection
]->start_x
420 if (new_selection
) /* Don't set the invalid value -1 */
424 menubar
->selected
= new_selection
;
425 dlg_select_widget (menubar
->widget
.parent
, menubar
);
426 menubar_drop_compute (menubar
);
427 menubar_draw (menubar
);
431 menubar_remove (menubar
);
433 menubar
->selected
= new_selection
;
435 menubar_drop_compute (menubar
);
436 menubar_draw (menubar
);
440 if (!menubar
->dropped
)
443 /* Ignore the events on anything below the third line */
447 /* Else, the mouse operation is on the menus or it is not */
448 left_x
= menubar
->menu
[menubar
->selected
]->start_x
;
449 right_x
= left_x
+ menubar
->max_entry_len
+ 4;
450 if (right_x
> menubar
->widget
.cols
)
452 left_x
= menubar
->widget
.cols
- menubar
->max_entry_len
- 3;
453 right_x
= menubar
->widget
.cols
- 1;
456 bottom_y
= (menubar
->menu
[menubar
->selected
])->count
+ 3;
458 if ((event
->x
> left_x
) && (event
->x
< right_x
) && (event
->y
< bottom_y
)){
459 int pos
= event
->y
- 3;
461 if (!menubar
->menu
[menubar
->selected
]->entries
[pos
].call_back
)
464 menubar_paint_idx (menubar
, menubar
->subsel
, MENU_ENTRY_COLOR
);
465 menubar
->subsel
= pos
;
466 menubar_paint_idx (menubar
, menubar
->subsel
, MENU_SELECTED_COLOR
);
468 if (event
->type
& GPM_UP
)
469 menubar_execute (menubar
, pos
);
471 if (event
->type
& GPM_DOWN
)
472 menubar_finish (menubar
);
478 * Properly space menubar items. Should be called when menubar is created
479 * and also when widget width is changed (i.e. upon xterm resize).
482 menubar_arrange(WMenu
* menubar
)
484 register int i
, start_x
= 1;
485 int items
= menubar
->items
;
487 #ifndef RESIZABLE_MENUBAR
490 for (i
= 0; i
< items
; i
++)
492 int len
= strlen(menubar
->menu
[i
]->name
);
493 menubar
->menu
[i
]->start_x
= start_x
;
494 start_x
+= len
+ gap
;
497 #else /* RESIZABLE_MENUBAR */
499 int gap
= menubar
->widget
.cols
- 2;
501 /* First, calculate gap between items... */
502 for (i
= 0; i
< items
; i
++)
504 /* preserve length here, to be used below */
505 gap
-= (menubar
->menu
[i
]->start_x
= strlen(menubar
->menu
[i
]->name
));
512 /* We are out of luck - window is too narrow... */
516 /* ...and now fix start positions of menubar items */
517 for (i
= 0; i
< items
; i
++)
519 int len
= menubar
->menu
[i
]->start_x
;
520 menubar
->menu
[i
]->start_x
= start_x
;
521 start_x
+= len
+ gap
;
523 #endif /* RESIZABLE_MENUBAR */
527 destroy_menu (Menu
*menu
)
530 g_free (menu
->help_node
);
535 menubar_new (int y
, int x
, int cols
, Menu
*menu
[], int items
)
537 WMenu
*menubar
= g_new0 (WMenu
, 1); /* FIXME: subsel used w/o being set */
539 init_widget (&menubar
->widget
, y
, x
, 1, cols
,
540 (callback_fn
) menubar_callback
,
541 (mouse_h
) menubar_event
);
542 menubar
->menu
= menu
;
544 menubar
->dropped
= 0;
545 menubar
->items
= items
;
546 menubar
->selected
= 0;
547 widget_want_cursor (menubar
->widget
, 0);
548 menubar_arrange (menubar
);