root/lib/widget/menu.c

/* [previous][next][first][last][top][bottom][index][help]  */

DEFINITIONS

This source file includes following definitions.
  1. menu_arrange
  2. menubar_paint_idx
  3. menubar_draw_drop
  4. menubar_set_color
  5. menubar_draw
  6. menubar_remove
  7. menubar_left
  8. menubar_right
  9. menubar_finish
  10. menubar_drop
  11. menubar_execute
  12. menubar_down
  13. menubar_up
  14. menubar_first
  15. menubar_last
  16. menubar_try_drop_menu
  17. menubar_try_exec_menu
  18. menubar_execute_cmd
  19. menubar_handle_key
  20. menubar_refresh
  21. menubar_free_menu
  22. menubar_callback
  23. menubar_get_menu_by_x_coord
  24. menubar_mouse_on_menu
  25. menubar_change_selected_item
  26. menubar_mouse_callback
  27. menu_entry_create
  28. menu_entry_free
  29. create_menu
  30. menu_set_name
  31. destroy_menu
  32. menubar_new
  33. menubar_set_menu
  34. menubar_add_menu
  35. menubar_arrange
  36. find_menubar
  37. menubar_activate

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

/* [previous][next][first][last][top][bottom][index][help]  */