This source file includes following definitions.
- menu_arrange
- menubar_paint_idx
- menubar_draw_drop
- menubar_set_color
- menubar_draw
- menubar_remove
- menubar_left
- menubar_right
- menubar_finish
- menubar_drop
- menubar_execute
- menubar_down
- menubar_up
- menubar_first
- menubar_last
- menubar_try_drop_menu
- menubar_try_exec_menu
- menubar_help
- menubar_execute_cmd
- menubar_handle_key
- menubar_refresh
- menubar_free_menu
- menubar_callback
- menubar_get_menu_by_x_coord
- menubar_mouse_on_menu
- menubar_change_selected_item
- menubar_mouse_callback
- menu_entry_new
- menu_entry_free
- menu_new
- menu_set_name
- menu_free
- menubar_new
- menubar_set_menu
- menubar_add_menu
- menubar_arrange
- menubar_find
- menubar_activate
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 #include <config.h>
31
32 #include <ctype.h>
33 #include <stdarg.h>
34 #include <string.h>
35 #include <sys/types.h>
36
37 #include "lib/global.h"
38
39 #include "lib/tty/tty.h"
40 #include "lib/skin.h"
41 #include "lib/tty/key.h"
42 #include "lib/strutil.h"
43 #include "lib/widget.h"
44 #include "lib/event.h"
45
46
47
48 const global_keymap_t *menu_map = NULL;
49
50
51
52 #define MENUENTRY(x) ((menu_entry_t *)(x))
53 #define MENU(x) ((menu_t *)(x))
54
55
56
57 struct menu_entry_t
58 {
59 unsigned char first_letter;
60 hotkey_t text;
61 long command;
62 char *shortcut;
63 };
64
65 struct menu_t
66 {
67 int start_x;
68 hotkey_t text;
69 GList *entries;
70 size_t max_entry_len;
71 size_t max_hotkey_len;
72 unsigned int current;
73 char *help_node;
74 };
75
76
77
78
79
80
81
82
83
84 static void
85 menu_arrange (menu_t *menu, dlg_shortcut_str get_shortcut)
86 {
87 if (menu != NULL)
88 {
89 GList *i;
90 size_t max_shortcut_len = 0;
91
92 menu->max_entry_len = 1;
93 menu->max_hotkey_len = 1;
94
95 for (i = menu->entries; i != NULL; i = g_list_next (i))
96 {
97 menu_entry_t *entry = MENUENTRY (i->data);
98
99 if (entry != NULL)
100 {
101 size_t len;
102
103 len = (size_t) hotkey_width (entry->text);
104 menu->max_hotkey_len = MAX (menu->max_hotkey_len, len);
105
106 if (get_shortcut != NULL)
107 entry->shortcut = get_shortcut (entry->command);
108
109 if (entry->shortcut != NULL)
110 {
111 len = (size_t) str_term_width1 (entry->shortcut);
112 max_shortcut_len = MAX (max_shortcut_len, len);
113 }
114 }
115 }
116
117 menu->max_entry_len = menu->max_hotkey_len + max_shortcut_len;
118 }
119 }
120
121
122
123 static void
124 menubar_paint_idx (const WMenuBar *menubar, unsigned int idx, int color)
125 {
126 const WRect *w = &CONST_WIDGET (menubar)->rect;
127 const menu_t *menu = MENU (g_list_nth_data (menubar->menu, menubar->current));
128 const menu_entry_t *entry = MENUENTRY (g_list_nth_data (menu->entries, idx));
129 const int y = 2 + idx;
130 int x = menu->start_x;
131
132 if (x + menu->max_entry_len + 4 > (gsize) w->cols)
133 x = w->cols - menu->max_entry_len - 4;
134
135 if (entry == NULL)
136 {
137
138 tty_setcolor (MENU_ENTRY_COLOR);
139
140 widget_gotoyx (menubar, y, x - 1);
141 tty_print_alt_char (ACS_LTEE, FALSE);
142 tty_draw_hline (w->y + y, w->x + x, ACS_HLINE, menu->max_entry_len + 3);
143 widget_gotoyx (menubar, y, x + menu->max_entry_len + 3);
144 tty_print_alt_char (ACS_RTEE, FALSE);
145 }
146 else
147 {
148 int yt, xt;
149
150
151 tty_setcolor (color);
152 widget_gotoyx (menubar, y, x);
153 tty_print_char ((unsigned char) entry->first_letter);
154 tty_getyx (&yt, &xt);
155 tty_draw_hline (yt, xt, ' ', menu->max_entry_len + 2);
156 tty_print_string (entry->text.start);
157
158 if (entry->text.hotkey != NULL)
159 {
160 tty_setcolor (color == MENU_SELECTED_COLOR ? MENU_HOTSEL_COLOR : MENU_HOT_COLOR);
161 tty_print_string (entry->text.hotkey);
162 tty_setcolor (color);
163 }
164
165 if (entry->text.end != NULL)
166 tty_print_string (entry->text.end);
167
168 if (entry->shortcut != NULL)
169 {
170 widget_gotoyx (menubar, y, x + menu->max_hotkey_len + 3);
171 tty_print_string (entry->shortcut);
172 }
173
174
175 widget_gotoyx (menubar, y, x + 1);
176 }
177 }
178
179
180
181 static void
182 menubar_draw_drop (const WMenuBar *menubar)
183 {
184 const WRect *w = &CONST_WIDGET (menubar)->rect;
185 const menu_t *menu = MENU (g_list_nth_data (menubar->menu, menubar->current));
186 const unsigned int count = g_list_length (menu->entries);
187 int column = menu->start_x - 1;
188 unsigned int i;
189
190 if (column + menu->max_entry_len + 5 > (gsize) w->cols)
191 column = w->cols - menu->max_entry_len - 5;
192
193 if (mc_global.tty.shadows)
194 tty_draw_box_shadow (w->y + 1, w->x + column, count + 2, menu->max_entry_len + 5,
195 SHADOW_COLOR);
196
197 tty_setcolor (MENU_ENTRY_COLOR);
198 tty_draw_box (w->y + 1, w->x + column, count + 2, menu->max_entry_len + 5, FALSE);
199
200 for (i = 0; i < count; i++)
201 menubar_paint_idx (menubar, i, i == menu->current ? MENU_SELECTED_COLOR : MENU_ENTRY_COLOR);
202 }
203
204
205
206 static void
207 menubar_set_color (const WMenuBar *menubar, gboolean current, gboolean hotkey)
208 {
209 if (!widget_get_state (CONST_WIDGET (menubar), WST_FOCUSED))
210 tty_setcolor (MENU_INACTIVE_COLOR);
211 else if (current)
212 tty_setcolor (hotkey ? MENU_HOTSEL_COLOR : MENU_SELECTED_COLOR);
213 else
214 tty_setcolor (hotkey ? MENU_HOT_COLOR : MENU_ENTRY_COLOR);
215 }
216
217
218
219 static void
220 menubar_draw (const WMenuBar *menubar)
221 {
222 const WRect *w = &CONST_WIDGET (menubar)->rect;
223 GList *i;
224
225
226 tty_setcolor (widget_get_state (WIDGET (menubar), WST_FOCUSED) ? MENU_ENTRY_COLOR :
227 MENU_INACTIVE_COLOR);
228 tty_draw_hline (w->y, w->x, ' ', w->cols);
229
230
231 for (i = menubar->menu; i != NULL; i = g_list_next (i))
232 {
233 menu_t *menu = MENU (i->data);
234 gboolean is_selected = (menubar->current == (gsize) g_list_position (menubar->menu, i));
235
236 menubar_set_color (menubar, is_selected, FALSE);
237 widget_gotoyx (menubar, 0, menu->start_x);
238
239 tty_print_char (' ');
240 tty_print_string (menu->text.start);
241
242 if (menu->text.hotkey != NULL)
243 {
244 menubar_set_color (menubar, is_selected, TRUE);
245 tty_print_string (menu->text.hotkey);
246 menubar_set_color (menubar, is_selected, FALSE);
247 }
248
249 if (menu->text.end != NULL)
250 tty_print_string (menu->text.end);
251
252 tty_print_char (' ');
253 }
254
255 if (menubar->is_dropped)
256 menubar_draw_drop (menubar);
257 else
258 widget_gotoyx (menubar, 0,
259 MENU (g_list_nth_data (menubar->menu, menubar->current))->start_x);
260 }
261
262
263
264 static void
265 menubar_remove (WMenuBar *menubar)
266 {
267 Widget *g;
268
269 if (!menubar->is_dropped)
270 return;
271
272
273
274
275
276 g = WIDGET (WIDGET (menubar)->owner);
277 GROUP (g)->current = widget_find (g, widget_find_by_id (g, menubar->previous_widget));
278
279 menubar->is_dropped = FALSE;
280 do_refresh ();
281 menubar->is_dropped = TRUE;
282
283
284 GROUP (g)->current = widget_find (g, WIDGET (menubar));
285 }
286
287
288
289 static void
290 menubar_left (WMenuBar *menubar)
291 {
292 menubar_remove (menubar);
293 if (menubar->current == 0)
294 menubar->current = g_list_length (menubar->menu) - 1;
295 else
296 menubar->current--;
297 menubar_draw (menubar);
298 }
299
300
301
302 static void
303 menubar_right (WMenuBar *menubar)
304 {
305 menubar_remove (menubar);
306 menubar->current = (menubar->current + 1) % g_list_length (menubar->menu);
307 menubar_draw (menubar);
308 }
309
310
311
312 static void
313 menubar_finish (WMenuBar *menubar)
314 {
315 Widget *w = WIDGET (menubar);
316
317 menubar->is_dropped = FALSE;
318 w->rect.lines = 1;
319 widget_want_hotkey (w, FALSE);
320 widget_set_options (w, WOP_SELECTABLE, FALSE);
321
322 if (!mc_global.keybar_visible)
323 widget_hide (w);
324 else
325 {
326
327
328 widget_set_bottom (w);
329 }
330
331
332 if (DIALOG (w->owner)->bg != NULL)
333 widget_set_bottom (WIDGET (DIALOG (w->owner)->bg));
334
335 group_select_widget_by_id (w->owner, menubar->previous_widget);
336 do_refresh ();
337 }
338
339
340
341 static void
342 menubar_drop (WMenuBar *menubar, unsigned int selected)
343 {
344 menubar->is_dropped = TRUE;
345 menubar->current = selected;
346 menubar_draw (menubar);
347 }
348
349
350
351 static void
352 menubar_execute (WMenuBar *menubar)
353 {
354 const menu_t *menu = MENU (g_list_nth_data (menubar->menu, menubar->current));
355 const menu_entry_t *entry = MENUENTRY (g_list_nth_data (menu->entries, menu->current));
356
357 if ((entry != NULL) && (entry->command != CK_IgnoreKey))
358 {
359 Widget *w = WIDGET (menubar);
360
361 mc_global.widget.is_right = (menubar->current != 0);
362 menubar_finish (menubar);
363 send_message (w->owner, w, MSG_ACTION, entry->command, NULL);
364 do_refresh ();
365 }
366 }
367
368
369
370 static void
371 menubar_down (WMenuBar *menubar)
372 {
373 menu_t *menu = MENU (g_list_nth_data (menubar->menu, menubar->current));
374 const unsigned int len = g_list_length (menu->entries);
375 menu_entry_t *entry;
376
377 menubar_paint_idx (menubar, menu->current, MENU_ENTRY_COLOR);
378
379 do
380 {
381 menu->current = (menu->current + 1) % len;
382 entry = MENUENTRY (g_list_nth_data (menu->entries, menu->current));
383 }
384 while ((entry == NULL) || (entry->command == CK_IgnoreKey));
385
386 menubar_paint_idx (menubar, menu->current, MENU_SELECTED_COLOR);
387 }
388
389
390
391 static void
392 menubar_up (WMenuBar *menubar)
393 {
394 menu_t *menu = MENU (g_list_nth_data (menubar->menu, menubar->current));
395 const unsigned int len = g_list_length (menu->entries);
396 menu_entry_t *entry;
397
398 menubar_paint_idx (menubar, menu->current, MENU_ENTRY_COLOR);
399
400 do
401 {
402 if (menu->current == 0)
403 menu->current = len - 1;
404 else
405 menu->current--;
406 entry = MENUENTRY (g_list_nth_data (menu->entries, menu->current));
407 }
408 while ((entry == NULL) || (entry->command == CK_IgnoreKey));
409
410 menubar_paint_idx (menubar, menu->current, MENU_SELECTED_COLOR);
411 }
412
413
414
415 static void
416 menubar_first (WMenuBar *menubar)
417 {
418 if (menubar->is_dropped)
419 {
420 menu_t *menu = MENU (g_list_nth_data (menubar->menu, menubar->current));
421
422 if (menu->current == 0)
423 return;
424
425 menubar_paint_idx (menubar, menu->current, MENU_ENTRY_COLOR);
426
427 menu->current = 0;
428
429 while (TRUE)
430 {
431 menu_entry_t *entry;
432
433 entry = MENUENTRY (g_list_nth_data (menu->entries, menu->current));
434
435 if ((entry == NULL) || (entry->command == CK_IgnoreKey))
436 menu->current++;
437 else
438 break;
439 }
440
441 menubar_paint_idx (menubar, menu->current, MENU_SELECTED_COLOR);
442 }
443 else
444 {
445 menubar->current = 0;
446 menubar_draw (menubar);
447 }
448 }
449
450
451
452 static void
453 menubar_last (WMenuBar *menubar)
454 {
455 if (menubar->is_dropped)
456 {
457 menu_t *menu = MENU (g_list_nth_data (menubar->menu, menubar->current));
458 const unsigned int len = g_list_length (menu->entries);
459 menu_entry_t *entry;
460
461 if (menu->current == len - 1)
462 return;
463
464 menubar_paint_idx (menubar, menu->current, MENU_ENTRY_COLOR);
465
466 menu->current = len;
467
468 do
469 {
470 menu->current--;
471 entry = MENUENTRY (g_list_nth_data (menu->entries, menu->current));
472 }
473 while ((entry == NULL) || (entry->command == CK_IgnoreKey));
474
475 menubar_paint_idx (menubar, menu->current, MENU_SELECTED_COLOR);
476 }
477 else
478 {
479 menubar->current = g_list_length (menubar->menu) - 1;
480 menubar_draw (menubar);
481 }
482 }
483
484
485
486 static cb_ret_t
487 menubar_try_drop_menu (WMenuBar *menubar, int hotkey)
488 {
489 GList *i;
490
491 for (i = menubar->menu; i != NULL; i = g_list_next (i))
492 {
493 menu_t *menu = MENU (i->data);
494
495 if (menu->text.hotkey != NULL && hotkey == g_ascii_tolower (menu->text.hotkey[0]))
496 {
497 menubar_drop (menubar, g_list_position (menubar->menu, i));
498 return MSG_HANDLED;
499 }
500 }
501
502 return MSG_NOT_HANDLED;
503 }
504
505
506
507 static cb_ret_t
508 menubar_try_exec_menu (WMenuBar *menubar, int hotkey)
509 {
510 menu_t *menu;
511 GList *i;
512
513 menu = g_list_nth_data (menubar->menu, menubar->current);
514
515 for (i = menu->entries; i != NULL; i = g_list_next (i))
516 {
517 const menu_entry_t *entry = MENUENTRY (i->data);
518
519 if (entry != NULL && entry->text.hotkey != NULL
520 && hotkey == g_ascii_tolower (entry->text.hotkey[0]))
521 {
522 menu->current = g_list_position (menu->entries, i);
523 menubar_execute (menubar);
524 return MSG_HANDLED;
525 }
526 }
527
528 return MSG_NOT_HANDLED;
529 }
530
531
532
533 static void
534 menubar_help (const WMenuBar *menubar)
535 {
536 ev_help_t event_data;
537
538 event_data.filename = NULL;
539
540 if (menubar->is_dropped)
541 event_data.node = MENU (g_list_nth_data (menubar->menu, menubar->current))->help_node;
542 else
543 event_data.node = "[Menu Bar]";
544
545 mc_event_raise (MCEVENT_GROUP_CORE, "help", &event_data);
546 menubar_draw (menubar);
547 }
548
549
550
551 static cb_ret_t
552 menubar_execute_cmd (WMenuBar *menubar, long command)
553 {
554 cb_ret_t ret = MSG_HANDLED;
555
556 switch (command)
557 {
558 case CK_Help:
559 menubar_help (menubar);
560 break;
561
562 case CK_Left:
563 menubar_left (menubar);
564 break;
565 case CK_Right:
566 menubar_right (menubar);
567 break;
568 case CK_Up:
569 if (menubar->is_dropped)
570 menubar_up (menubar);
571 break;
572 case CK_Down:
573 if (menubar->is_dropped)
574 menubar_down (menubar);
575 else
576 menubar_drop (menubar, menubar->current);
577 break;
578 case CK_Home:
579 menubar_first (menubar);
580 break;
581 case CK_End:
582 menubar_last (menubar);
583 break;
584
585 case CK_Enter:
586 if (menubar->is_dropped)
587 menubar_execute (menubar);
588 else
589 menubar_drop (menubar, menubar->current);
590 break;
591 case CK_Quit:
592 menubar_finish (menubar);
593 break;
594
595 default:
596 ret = MSG_NOT_HANDLED;
597 break;
598 }
599
600 return ret;
601 }
602
603
604
605 static int
606 menubar_handle_key (WMenuBar *menubar, int key)
607 {
608 long cmd;
609 cb_ret_t ret = MSG_NOT_HANDLED;
610
611 cmd = widget_lookup_key (WIDGET (menubar), key);
612
613 if (cmd != CK_IgnoreKey)
614 ret = menubar_execute_cmd (menubar, cmd);
615
616 if (ret != MSG_HANDLED)
617 {
618 if (menubar->is_dropped)
619 ret = menubar_try_exec_menu (menubar, key);
620 else
621 ret = menubar_try_drop_menu (menubar, key);
622 }
623
624 return ret;
625 }
626
627
628
629 static gboolean
630 menubar_refresh (WMenuBar *menubar)
631 {
632 Widget *w = WIDGET (menubar);
633
634 if (!widget_get_state (w, WST_FOCUSED))
635 return FALSE;
636
637
638 w->rect.lines = LINES;
639
640
641 widget_want_hotkey (w, TRUE);
642 return TRUE;
643 }
644
645
646
647 static inline void
648 menubar_free_menu (WMenuBar *menubar)
649 {
650 g_clear_list (&menubar->menu, (GDestroyNotify) menu_free);
651 }
652
653
654
655 static cb_ret_t
656 menubar_callback (Widget *w, Widget *sender, widget_msg_t msg, int parm, void *data)
657 {
658 WMenuBar *menubar = MENUBAR (w);
659
660 switch (msg)
661 {
662
663 case MSG_FOCUS:
664 if (menubar_refresh (menubar))
665 {
666 menubar_draw (menubar);
667 return MSG_HANDLED;
668 }
669 return MSG_NOT_HANDLED;
670
671 case MSG_UNFOCUS:
672 return widget_get_state (w, WST_FOCUSED) ? MSG_NOT_HANDLED : MSG_HANDLED;
673
674
675 case MSG_HOTKEY:
676 case MSG_KEY:
677 if (widget_get_state (w, WST_FOCUSED))
678 {
679 menubar_handle_key (menubar, parm);
680 return MSG_HANDLED;
681 }
682 return MSG_NOT_HANDLED;
683
684 case MSG_CURSOR:
685
686 return MSG_NOT_HANDLED;
687
688 case MSG_DRAW:
689 if (widget_get_state (w, WST_VISIBLE) || menubar_refresh (menubar))
690 menubar_draw (menubar);
691 return MSG_HANDLED;
692
693 case MSG_RESIZE:
694
695 widget_default_callback (w, NULL, MSG_RESIZE, 0, data);
696 menubar_refresh (menubar);
697 return MSG_HANDLED;
698
699 case MSG_DESTROY:
700 menubar_free_menu (menubar);
701 return MSG_HANDLED;
702
703 default:
704 return widget_default_callback (w, sender, msg, parm, data);
705 }
706 }
707
708
709
710 static unsigned int
711 menubar_get_menu_by_x_coord (const WMenuBar *menubar, int x)
712 {
713 unsigned int i;
714 GList *menu;
715
716 for (i = 0, menu = menubar->menu;
717 menu != NULL && x >= MENU (menu->data)->start_x; i++, menu = g_list_next (menu))
718 ;
719
720
721 if (i != 0)
722 i--;
723
724 return i;
725 }
726
727
728
729 static gboolean
730 menubar_mouse_on_menu (const WMenuBar *menubar, int y, int x)
731 {
732 const WRect *w = &CONST_WIDGET (menubar)->rect;
733 menu_t *menu;
734 int left_x, right_x, bottom_y;
735
736 if (!menubar->is_dropped)
737 return FALSE;
738
739 menu = MENU (g_list_nth_data (menubar->menu, menubar->current));
740 left_x = menu->start_x;
741 right_x = left_x + menu->max_entry_len + 3;
742 if (right_x > w->cols)
743 {
744 left_x = w->cols - (menu->max_entry_len + 3);
745 right_x = w->cols;
746 }
747
748 bottom_y = g_list_length (menu->entries) + 2;
749
750 return (x >= left_x && x < right_x && y > 1 && y < bottom_y);
751 }
752
753
754
755 static void
756 menubar_change_selected_item (WMenuBar *menubar, int y)
757 {
758 menu_t *menu;
759 menu_entry_t *entry;
760
761 y -= 2;
762 menu = MENU (g_list_nth_data (menubar->menu, menubar->current));
763 entry = MENUENTRY (g_list_nth_data (menu->entries, y));
764
765 if (entry != NULL && entry->command != CK_IgnoreKey)
766 {
767 menubar_paint_idx (menubar, menu->current, MENU_ENTRY_COLOR);
768 menu->current = y;
769 menubar_paint_idx (menubar, menu->current, MENU_SELECTED_COLOR);
770 }
771 }
772
773
774
775 static void
776 menubar_mouse_callback (Widget *w, mouse_msg_t msg, mouse_event_t *event)
777 {
778 static gboolean was_drag = FALSE;
779
780 WMenuBar *menubar = MENUBAR (w);
781 gboolean mouse_on_drop;
782
783 mouse_on_drop = menubar_mouse_on_menu (menubar, event->y, event->x);
784
785 switch (msg)
786 {
787 case MSG_MOUSE_DOWN:
788 was_drag = FALSE;
789
790 if (event->y == 0)
791 {
792
793 unsigned int selected;
794
795 selected = menubar_get_menu_by_x_coord (menubar, event->x);
796 menubar_activate (menubar, TRUE, selected);
797 menubar_remove (menubar);
798 menubar_drop (menubar, selected);
799 }
800 else if (mouse_on_drop)
801 menubar_change_selected_item (menubar, event->y);
802 else
803 {
804
805 menubar_finish (menubar);
806
807
808
809
810
811
812
813
814
815
816
817
818
819 w->mouse.capture = FALSE;
820 }
821 break;
822
823 case MSG_MOUSE_UP:
824 if (was_drag && mouse_on_drop)
825 menubar_execute (menubar);
826 was_drag = FALSE;
827 break;
828
829 case MSG_MOUSE_CLICK:
830 was_drag = FALSE;
831
832 if ((event->buttons & GPM_B_MIDDLE) != 0 && event->y > 0 && menubar->is_dropped)
833 {
834
835 menubar_execute (menubar);
836 }
837 else if (mouse_on_drop)
838 menubar_execute (menubar);
839 else if (event->y > 0)
840
841 menubar_finish (menubar);
842 break;
843
844 case MSG_MOUSE_DRAG:
845 if (event->y == 0)
846 {
847 menubar_remove (menubar);
848 menubar_drop (menubar, menubar_get_menu_by_x_coord (menubar, event->x));
849 }
850 else if (mouse_on_drop)
851 menubar_change_selected_item (menubar, event->y);
852
853 was_drag = TRUE;
854 break;
855
856 case MSG_MOUSE_SCROLL_UP:
857 case MSG_MOUSE_SCROLL_DOWN:
858 was_drag = FALSE;
859
860 if (widget_get_state (w, WST_FOCUSED))
861 {
862 if (event->y == 0)
863 {
864
865 if (msg == MSG_MOUSE_SCROLL_UP)
866 menubar_left (menubar);
867 else
868 menubar_right (menubar);
869 }
870 else if (mouse_on_drop)
871 {
872
873 if (msg == MSG_MOUSE_SCROLL_UP)
874 menubar_up (menubar);
875 else
876 menubar_down (menubar);
877 }
878 }
879 break;
880
881 default:
882 was_drag = FALSE;
883 break;
884 }
885 }
886
887
888
889
890
891 menu_entry_t *
892 menu_entry_new (const char *name, long command)
893 {
894 menu_entry_t *entry;
895
896 entry = g_new (menu_entry_t, 1);
897 entry->first_letter = ' ';
898 entry->text = hotkey_new (name);
899 entry->command = command;
900 entry->shortcut = NULL;
901
902 return entry;
903 }
904
905
906
907 void
908 menu_entry_free (menu_entry_t *entry)
909 {
910 if (entry != NULL)
911 {
912 hotkey_free (entry->text);
913 g_free (entry->shortcut);
914 g_free (entry);
915 }
916 }
917
918
919
920 menu_t *
921 menu_new (const char *name, GList *entries, const char *help_node)
922 {
923 menu_t *menu;
924
925 menu = g_new (menu_t, 1);
926 menu->start_x = 0;
927 menu->text = hotkey_new (name);
928 menu->entries = entries;
929 menu->max_entry_len = 1;
930 menu->max_hotkey_len = 0;
931 menu->current = 0;
932 menu->help_node = g_strdup (help_node);
933
934 return menu;
935 }
936
937
938
939 void
940 menu_set_name (menu_t *menu, const char *name)
941 {
942 hotkey_free (menu->text);
943 menu->text = hotkey_new (name);
944 }
945
946
947
948 void
949 menu_free (menu_t *menu)
950 {
951 hotkey_free (menu->text);
952 g_list_free_full (menu->entries, (GDestroyNotify) menu_entry_free);
953 g_free (menu->help_node);
954 g_free (menu);
955 }
956
957
958
959 WMenuBar *
960 menubar_new (GList *menu)
961 {
962 WRect r = { 0, 0, 1, COLS };
963 WMenuBar *menubar;
964 Widget *w;
965
966 menubar = g_new0 (WMenuBar, 1);
967 w = WIDGET (menubar);
968 widget_init (w, &r, menubar_callback, menubar_mouse_callback);
969 w->pos_flags = WPOS_KEEP_HORZ | WPOS_KEEP_TOP;
970 w->options |= WOP_TOP_SELECT;
971 w->keymap = menu_map;
972 menubar_set_menu (menubar, menu);
973
974 return menubar;
975 }
976
977
978
979 void
980 menubar_set_menu (WMenuBar *menubar, GList *menu)
981 {
982
983 menubar_free_menu (menubar);
984
985 menubar->is_dropped = FALSE;
986 menubar->menu = menu;
987 menubar->current = 0;
988 menubar_arrange (menubar);
989 widget_set_state (WIDGET (menubar), WST_FOCUSED, FALSE);
990 }
991
992
993
994 void
995 menubar_add_menu (WMenuBar *menubar, menu_t *menu)
996 {
997 if (menu != NULL)
998 {
999 menu_arrange (menu, DIALOG (WIDGET (menubar)->owner)->get_shortcut);
1000 menubar->menu = g_list_append (menubar->menu, menu);
1001 }
1002
1003 menubar_arrange (menubar);
1004 }
1005
1006
1007
1008
1009
1010
1011
1012 void
1013 menubar_arrange (WMenuBar *menubar)
1014 {
1015 int start_x = 1;
1016 GList *i;
1017 int gap;
1018
1019 if (menubar->menu == NULL)
1020 return;
1021
1022 gap = WIDGET (menubar)->rect.cols - 2;
1023
1024
1025 for (i = menubar->menu; i != NULL; i = g_list_next (i))
1026 {
1027 menu_t *menu = MENU (i->data);
1028
1029
1030 menu->start_x = hotkey_width (menu->text) + 2;
1031 gap -= menu->start_x;
1032 }
1033
1034 if (g_list_next (menubar->menu) == NULL)
1035 gap = 1;
1036 else
1037 gap /= (g_list_length (menubar->menu) - 1);
1038
1039 if (gap <= 0)
1040 {
1041
1042 gap = 1;
1043 }
1044 else if (gap >= 3)
1045 gap = 3;
1046
1047
1048 for (i = menubar->menu; i != NULL; i = g_list_next (i))
1049 {
1050 menu_t *menu = MENU (i->data);
1051 int len = menu->start_x;
1052
1053 menu->start_x = start_x;
1054 start_x += len + gap;
1055 }
1056 }
1057
1058
1059
1060
1061 WMenuBar *
1062 menubar_find (const WDialog *h)
1063 {
1064 return MENUBAR (widget_find_by_type (CONST_WIDGET (h), menubar_callback));
1065 }
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076 void
1077 menubar_activate (WMenuBar *menubar, gboolean dropped, int which)
1078 {
1079 Widget *w = WIDGET (menubar);
1080
1081 widget_show (w);
1082
1083 if (!widget_get_state (w, WST_FOCUSED))
1084 {
1085 widget_set_options (w, WOP_SELECTABLE, TRUE);
1086
1087 menubar->is_dropped = dropped;
1088 if (which >= 0)
1089 menubar->current = (guint) which;
1090
1091 menubar->previous_widget = group_get_current_widget_id (w->owner);
1092
1093
1094
1095 widget_select (w);
1096 }
1097 }
1098
1099