]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/Menubar_pimpl.C
9ca79d8bdaf6b95d3f5691cb1e7e4655b53d5938
[lyx.git] / src / frontends / xforms / Menubar_pimpl.C
1 /* This file is part of
2 * ======================================================
3
4 *           LyX, The Document Processor
5 *        
6 *           Copyright (C) 1999 The LyX Team.
7 *
8 *======================================================*/
9
10 #include <config.h>
11
12 #include <algorithm>
13
14 #ifdef __GNUG__
15 #pragma implementation
16 #endif
17
18 #include "support/lstrings.h"
19 #include "support/LAssert.h"
20 #include "debug.h"
21 #include "LyXAction.h"
22 #include "lyxfunc.h"
23 #include "kbmap.h"
24 #include "buffer.h"
25 #include "Dialogs.h"
26 #include "LyXView.h"
27 #include "MenuBackend.h"
28 #include "Menubar_pimpl.h"
29
30 using std::endl;
31 using std::vector;
32 using std::max;
33 using std::min;
34
35 typedef vector<int>::size_type size_type;
36
37 extern boost::scoped_ptr<kb_keymap> toplevel_keymap;
38 extern LyXAction lyxaction;
39
40 // Some constants
41 static const int MENU_LABEL_SIZE = FL_NORMAL_SIZE;
42 static const int mheight = 30;
43 static const int mbheight= 22;
44 // where to place the menubar?
45 static const int yloc = (mheight - mbheight)/2; //air + bw;
46 static const int mbadd = 20; // menu button add (to width)
47 // Some space between buttons on the menubar 
48 static const int air = 2;
49 static char const * menu_tabstop = "aa";
50 static char const * default_tabstop = "aaaaaaaa";
51 // We do not want to mix position values in a menu (like the index of
52 // a submenu) with the action numbers which convey actual information.
53 // Therefore we offset all the action values by an arbitrary large
54 // constant. 
55 static const int action_offset = 1000;
56
57
58 //Defined later, used in makeMenubar().
59 extern "C"
60 void C_Menubar_Pimpl_MenuCallback(FL_OBJECT * ob, long button);
61
62 // This is used a few times below.
63 static inline
64 int string_width(string const & str) 
65 {
66         return fl_get_string_widthTAB(FL_NORMAL_STYLE, MENU_LABEL_SIZE,
67                                       str.c_str(),
68                                       static_cast<int>(str.length()));
69 }
70
71
72 Menubar::Pimpl::Pimpl(LyXView * view, MenuBackend const & mb) 
73         : owner_(view), menubackend_(&mb), current_group_(0)
74 {
75         for (MenuBackend::const_iterator menu = menubackend_->begin();
76             menu != menubackend_->end() ; ++menu) {
77                 if (menu->menubar()) {
78                         FL_OBJECT * group = fl_bgn_group();
79                         makeMenubar(*menu);
80                         fl_end_group();
81                         fl_hide_object(group);
82                         lyxerr[Debug::GUI]
83                                 << "Menubar::Pimpl::Pimpl: "
84                                 << "creating and hiding group " << group
85                                 << " for menubar " << menu->name() << endl;
86                         menubarmap_[menu->name()] = group;
87                 }
88         }
89 }
90
91
92 void Menubar::Pimpl::makeMenubar(Menu const &menu)
93 {
94         FL_FORM * form = owner_->getForm(); 
95         int moffset = 0;
96
97         // Create menu frame if there is non yet.
98         FL_OBJECT * frame = fl_add_frame(FL_UP_FRAME, 0, 0,
99                                          form->w, mheight, "");
100         fl_set_object_resize(frame, FL_RESIZE_ALL);
101         fl_set_object_gravity(frame, NorthWestGravity, 
102                               NorthEastGravity);
103
104         for (Menu::const_iterator i = menu.begin(); 
105              i != menu.end(); ++i) {
106                 FL_OBJECT * obj;
107                 if (i->kind() != MenuItem::Submenu) {
108                         lyxerr << "ERROR: Menubar::Pimpl::createMenubar:"
109                                 " only submenus can appear in a menubar";
110                         break;
111                 }
112                 string label = i->label();
113                 string shortcut = "#" + i->shortcut();
114                 int width = string_width(label);
115                 obj = fl_add_button(FL_MENU_BUTTON,
116                                     air + moffset, yloc,
117                                     width + mbadd,
118                                     mbheight, 
119                                     label.c_str());
120                 fl_set_object_boxtype(obj, FL_FLAT_BOX);
121                 fl_set_object_color(obj, FL_MCOL, FL_MCOL);
122                 fl_set_object_lsize(obj, MENU_LABEL_SIZE);
123                 fl_set_object_lstyle(obj, FL_NORMAL_STYLE);
124                 fl_set_object_resize(obj, FL_RESIZE_ALL);
125                 fl_set_object_gravity(obj, NorthWestGravity, 
126                                       NorthWestGravity);
127                 moffset += obj->w + air;
128                 fl_set_object_shortcut(obj, shortcut.c_str(), 1);
129                 fl_set_object_callback(obj, C_Menubar_Pimpl_MenuCallback, 1);
130
131                 boost::shared_ptr<ItemInfo> iteminfo(new ItemInfo(this, 
132                                                    new MenuItem(*i), obj));
133                 buttonlist_.push_back(iteminfo);
134                 obj->u_vdata = iteminfo.get();
135         }
136
137 }
138
139 void Menubar::Pimpl::set(string const & menu_name) 
140 {
141         lyxerr[Debug::GUI] << "Entering Menubar::Pimpl::set " 
142                            << "for menu `" << menu_name << "'" << endl;
143
144         if (menu_name != current_menu_name_) {
145                 MenubarMap::iterator mbit = menubarmap_.find(menu_name);
146
147                 if (mbit == menubarmap_.end()) {
148                         lyxerr << "ERROR:set: Unknown menu `" << menu_name
149                                << "'" << endl;
150                         return;
151                 }
152
153                 if (current_group_) {
154                         lyxerr[Debug::GUI] << "  hiding group "
155                                            << current_group_ << endl;
156                         fl_hide_object(current_group_);
157                 }
158                 
159                 lyxerr[Debug::GUI] << "  showing group "
160                                    << mbit->second << endl;
161                 fl_show_object(mbit->second);
162                 current_menu_name_ = menu_name;
163                 current_group_ = mbit->second;
164                 lyxerr[Debug::GUI] << "Menubar::Pimpl::set: Menubar set."
165                                    << endl;
166         }
167         else
168                 lyxerr [Debug::GUI] << "Menubar::Pimpl::set: Nothing to do."
169                                     << endl;
170
171
172 void Menubar::Pimpl::openByName(string const & name)
173 {
174         if (menubackend_->getMenu(current_menu_name_).hasSubmenu(name)) {
175                 for (ButtonList::const_iterator cit = buttonlist_.begin();
176                      cit != buttonlist_.end(); ++cit) {
177                         if ((*cit)->item_->submenu() == name) {
178                                 MenuCallback((*cit)->obj_, 1);
179                                 return;
180                         }
181                 }
182         }
183         lyxerr << "Menubar::Pimpl::openByName: menu "
184                << name << " not found" << endl;
185 }
186
187
188 static inline
189 string const limit_string_length(string const & str)
190 {
191         string::size_type const max_item_length = 45;
192
193         if (str.size() > max_item_length)
194                 return str.substr(0, max_item_length - 3) + "...";
195         else
196                 return str;
197 }
198
199
200 static
201 int get_new_submenu(vector<int> & smn, Window win)
202 {
203         static size_type max_number_of_menus = 32;
204         if (smn.size() >= max_number_of_menus)
205                 max_number_of_menus =
206                     fl_setpup_maxpup(static_cast<int>(2*smn.size()));
207         int menu = fl_newpup(win);
208         smn.push_back(menu);
209         return menu;
210 }
211
212
213 size_type const max_number_of_items = 25;
214
215 static inline
216 string const fixlabel(string const & str)
217 {
218 #if FL_REVISION < 89
219         return subst(str, '%', '?');
220 #else
221         return subst(str, "%", "%%");
222 #endif
223 }
224
225
226 void add_toc2(int menu, string const & extra_label,
227               vector<int> & smn, Window win,
228               vector<Buffer::TocItem> const & toc_list,
229               size_type from, size_type to, int depth)
230 {
231         int shortcut_count = 0;
232         if (to - from <= max_number_of_items) {
233                 for (size_type i = from; i < to; ++i) {
234                         int const action = lyxaction.
235                                 getPseudoAction(LFUN_GOTO_PARAGRAPH,
236                                                 tostr(toc_list[i].par->id()));
237                         string label(4 * max(0, toc_list[i].depth - depth),' ');
238                         label += fixlabel(toc_list[i].str);
239                         label = limit_string_length(label);
240                         label += "%x" + tostr(action + action_offset);
241                         if (i == to - 1 && depth == 0)
242                                 label += extra_label;
243                         if (toc_list[i].depth == depth
244                             && ++shortcut_count <= 9) {
245                                 label += "%h";
246                                 fl_addtopup(menu, label.c_str(),
247                                             tostr(shortcut_count).c_str());
248                         } else
249                                 fl_addtopup(menu, label.c_str());
250                 }
251         } else {
252                 size_type pos = from;
253                 size_type count = 0;
254                 while (pos < to) {
255                         ++count;
256                         if (count > max_number_of_items) {
257                                 int menu2 = get_new_submenu(smn, win);
258                                 add_toc2(menu2, extra_label, smn, win,
259                                          toc_list, pos, to, depth);
260                                 string label = _("More");
261                                 label += "...%m";
262                                 if (depth == 0)
263                                         label += extra_label;
264                                 fl_addtopup(menu, label.c_str(), menu2);
265                                 break;
266                         }
267                         size_type new_pos = pos+1;
268                         while (new_pos < to &&
269                                toc_list[new_pos].depth > depth)
270                                 ++new_pos;
271
272                         int const action = lyxaction.
273                                 getPseudoAction(LFUN_GOTO_PARAGRAPH,
274                                                 tostr(toc_list[pos].par->id()));
275                         string label(4 * max(0, toc_list[pos].depth - depth), ' ');
276                         label += fixlabel(toc_list[pos].str);
277                         label = limit_string_length(label);
278                         if (new_pos == to && depth == 0)
279                                 label += extra_label;
280                         string shortcut;
281                         if (toc_list[pos].depth == depth &&
282                             ++shortcut_count <= 9)
283                                 shortcut = tostr(shortcut_count);
284
285                         if (new_pos == pos + 1) {
286                                 label += "%x" + tostr(action + action_offset);
287                                 if (!shortcut.empty()) {
288                                         label += "%h";
289                                         fl_addtopup(menu, label.c_str(),
290                                                     shortcut.c_str());
291                                 } else
292                                         fl_addtopup(menu, label.c_str());
293                         } else {
294                                 int menu2 = get_new_submenu(smn, win);
295                                 add_toc2(menu2, extra_label, smn, win,
296                                          toc_list, pos, new_pos, depth+1);
297                                 label += "%m";
298                                 if (!shortcut.empty()) {
299                                         label += "%h";
300                                         fl_addtopup(menu, label.c_str(), menu2,
301                                                     shortcut.c_str());
302                                 } else
303                                         fl_addtopup(menu, label.c_str(), menu2);
304                         }
305                         pos = new_pos;
306                 }
307         }
308 }
309
310
311 void Menubar::Pimpl::add_toc(int menu, string const & extra_label,
312                              vector<int> & smn, Window win)
313 {
314 #if 0
315         //xgettext:no-c-format
316         static char const * MenuNames[3] = { N_("List of Figures%m"),
317         //xgettext:no-c-format
318                                              N_("List of Tables%m"),
319         //xgettext:no-c-format
320                                              N_("List of Algorithms%m") };
321
322         vector<vector<Buffer::TocItem> > toc_list =
323                 owner_->buffer()->getTocList();
324
325         // Handle LOF/LOT/LOA
326         int max_nonempty = 0;
327         for (int i = 1; i <= 3; ++i)
328                 if (!toc_list[i].empty())
329                         max_nonempty = i;
330
331         for (int j = 1; j <= 3; ++j)
332                 if (!toc_list[j].empty()) {
333                         int menu2 = get_new_submenu(smn, win);
334                         for (size_type i = 0; i < toc_list[j].size(); ++i) {
335                                 if (i > max_number_of_items) {
336                                         fl_addtopup(menu2, ". . .%d");
337                                         break;
338                                 }
339                                 int const action = lyxaction.
340                                         getPseudoAction(LFUN_GOTO_PARAGRAPH,
341                                                         tostr(toc_list[j][i].par->id()));
342                                 string label = fixlabel(toc_list[j][i].str);
343                                 label = limit_string_length(label);
344                                 label += "%x" + tostr(action + action_offset);
345                                 fl_addtopup(menu2, label.c_str());
346                         }
347                         if (j == max_nonempty) {
348                                 string label = _(MenuNames[j - 1]);
349                                 label += "%l";
350                                 fl_addtopup(menu, label.c_str(), menu2);
351                         } else
352                                 fl_addtopup(menu, _(MenuNames[j - 1]), menu2);
353                 }
354
355         // Handle normal TOC
356         if (max_nonempty == 0 && toc_list[0].empty()) {
357                 fl_addtopup(menu, (_("No Table of Contents%i")
358                                    + extra_label).c_str());
359                 return;
360         }
361
362         add_toc2(menu, extra_label, smn, win,
363                  toc_list[0], 0, toc_list[0].size(), 0);
364 #else
365 #warning Fix Me! (Lgb)
366         Buffer::Lists toc_list = owner_->buffer()->getLists();
367         Buffer::Lists::const_iterator cit = toc_list.begin();
368         Buffer::Lists::const_iterator end = toc_list.end();
369         for (; cit != end; ++cit) {
370                 // Handle this elsewhere
371                 if (cit->first == "TOC") continue;
372                 
373                 int menu2 = get_new_submenu(smn, win);
374                 Buffer::SingleList::const_iterator ccit = cit->second.begin();
375                 Buffer::SingleList::const_iterator eend = cit->second.end();
376                 for (; ccit != eend; ++ccit) {
377                         int const action =
378                                 lyxaction
379                                 .getPseudoAction(LFUN_GOTO_PARAGRAPH,
380                                                  tostr(ccit->par->id()));
381                         string label = fixlabel(ccit->str);
382                         label = limit_string_length(label);
383                         label += "%x" + tostr(action + action_offset);
384                         fl_addtopup(menu2, label.c_str());
385                 }
386                 string const m = cit->first + "%m";
387                 fl_addtopup(menu, m.c_str(), menu2);
388         }
389         
390         
391         // Handle normal TOC
392         cit = toc_list.find("TOC");
393         if (cit == end) {
394                 string const tmp = _("No Table of contents%i") + extra_label;
395                 fl_addtopup(menu, tmp.c_str());
396                 return;
397         } else {
398                 add_toc2(menu, extra_label, smn, win,
399                          cit->second, 0, cit->second.size(), 0);
400         }
401         
402 #endif
403 }
404
405 int Menubar::Pimpl::create_submenu(Window win, LyXView * view, 
406                                    string const & menu_name, 
407                                    vector<int> & smn) 
408 {
409         if (!menubackend_->hasMenu(menu_name)){ 
410                 lyxerr << "ERROR:create_submenu: Unknown menu `" 
411                        << menu_name << "'" << endl;
412                 return -1;
413         }
414         Menu md = Menu();
415         menubackend_->getMenu(menu_name).expand(md, owner_->buffer());
416
417         int menu = get_new_submenu(smn, win);
418         fl_setpup_softedge(menu, true);
419         fl_setpup_bw(menu, -1);
420         lyxerr[Debug::GUI] << "Adding menu " << menu 
421                            << " in deletion list" << endl;
422
423         // Compute the size of the largest label (because xforms is
424         // not able to support shortcuts correctly...)
425         int max_width = 0;
426         string widest_label;
427         Menu::const_iterator end = md.end();
428         for (Menu::const_iterator i = md.begin(); i != end; ++i) {
429                 MenuItem const & item = (*i);
430                 if (item.kind() == MenuItem::Command) {
431                         string label = item.label() + '\t';
432                         int width = string_width(label);
433                         if (width > max_width) {
434                                 max_width = width;
435                                 widest_label = label;
436                         }
437                 }
438         }
439         lyxerr[Debug::GUI] << "max_width=" << max_width 
440                            << ", widest_label=`" << widest_label 
441                            << "'" << endl;
442
443         // Compute where to put separators
444         vector<string> extra_labels(md.size());
445         vector<string>::iterator it = extra_labels.begin();
446         vector<string>::iterator last = it;
447         for (Menu::const_iterator i = md.begin(); i != end; ++i, ++it)
448                 if (i->kind() == MenuItem::Separator)
449                         *last = "%l";
450                 else if (!i->optional() ||
451                          !(view->getLyXFunc()->getStatus(i->action()) & LyXFunc::Disabled))
452                         last = it;
453
454         it = extra_labels.begin();
455         for (Menu::const_iterator i = md.begin(); i != end; ++i, ++it) {
456                 MenuItem const & item = (*i);
457                 string & extra_label = *it;
458
459                 switch (item.kind()) {
460                 case MenuItem::Command: {
461                         LyXFunc::func_status flag = 
462                                 view->getLyXFunc()->getStatus(item.action()); 
463
464                         // handle optional entries.
465                         if (item.optional() && (flag & LyXFunc::Disabled)) {
466                                 lyxerr[Debug::GUI] 
467                                         << "Skipping optional item " 
468                                         << item.label() << endl; 
469                                 break;
470                         }
471
472                         // Get the keys bound to this action, but keep only the
473                         // first one later
474                         string const accel = toplevel_keymap->findbinding(item.action());
475                         // Build the menu label from all the info
476                         string label = item.label();
477
478                         if (!accel.empty()) {
479                                 // Try to be clever and add  just enough
480                                 // tabs to align shortcuts.
481                                 do 
482                                         label += '\t';
483                                 while (string_width(label) < max_width + 5);
484                                 label += accel.substr(1,accel.find(']') - 1);
485                         }
486                         label += "%x" + tostr(item.action() + action_offset)
487                                 + extra_label;
488                         
489                         // Modify the entry using the function status
490                         string pupmode;
491                         if (flag & (LyXFunc::Disabled | LyXFunc::Unknown))
492                                 pupmode += "%i";
493                         if (flag & LyXFunc::ToggleOn)
494                                 pupmode += "%B";
495                         if (flag & LyXFunc::ToggleOff)
496                                 pupmode += "%b";
497                         label += pupmode;
498
499                         // Finally the menu shortcut
500                         string shortcut = item.shortcut();
501
502                         if (!shortcut.empty()) {
503                                 shortcut += lowercase(shortcut[0]);
504                                 label += "%h";
505                                 fl_addtopup(menu, label.c_str(), 
506                                             shortcut.c_str());
507                         } else
508                                 fl_addtopup(menu, label.c_str());
509                         
510                         lyxerr[Debug::GUI] << "Command: \""  
511                                            << lyxaction.getActionName(item.action())
512                                            << "\", binding \"" << accel
513                                            << "\", shortcut \"" << shortcut 
514                                            << "\"" << endl;
515                         break;
516                 }
517
518                 case MenuItem::Submenu: {
519                         int submenu = create_submenu(win, view, 
520                                                      item.submenu(), smn);
521                         if (submenu == -1)
522                                 return -1;
523                         string label = item.label();
524                         label += extra_label + "%m";
525                         string shortcut = item.shortcut();
526                         if (!shortcut.empty()) {
527                                 shortcut += lowercase(shortcut[0]);
528                                 label += "%h";
529                                 fl_addtopup(menu, label.c_str(),
530                                             submenu, shortcut.c_str());
531                         }
532                         else {
533                                 fl_addtopup(menu, label.c_str(), submenu);
534                         }
535                         break;
536                 }
537
538                 case MenuItem::Separator:
539                         // already done, and if it was the first one,
540                         // we just ignore it.
541                         break;
542
543                 case MenuItem::Toc:
544                         add_toc(menu, extra_label, smn, win);
545                         break;
546
547                 case MenuItem::Documents: 
548                 case MenuItem::Lastfiles: 
549                 case MenuItem::ViewFormats:
550                 case MenuItem::UpdateFormats:
551                 case MenuItem::ExportFormats:
552                 case MenuItem::ImportFormats:
553                         lyxerr << "Menubar::Pimpl::create_submenu: "
554                                 "this should not happen" << endl;
555                         break;
556
557                 }
558         }
559         return menu;
560 }
561
562
563 extern "C"
564 void C_Menubar_Pimpl_MenuCallback(FL_OBJECT * ob, long button)
565 {
566         Menubar::Pimpl::MenuCallback(ob, button);
567 }
568
569
570 void Menubar::Pimpl::MenuCallback(FL_OBJECT * ob, long button)
571 {
572         ItemInfo * iteminfo = static_cast<ItemInfo *>(ob->u_vdata);
573 //      lyxerr << "MenuCallback: ItemInfo address=" << iteminfo
574 //             << "Val=(pimpl_=" << iteminfo->pimpl_
575 //             << ", item_=" << iteminfo->item_
576 //             << ", obj_=" << iteminfo->obj_ << ")" <<endl;
577
578         LyXView * view = iteminfo->pimpl_->owner_;
579         MenuItem const * item = iteminfo->item_.get();
580
581         /* get the splash out of the way. It would be nicer
582          * to only have this code at the start, but xforms
583          * makes it too ugly to do
584          */
585         view->getDialogs()->destroySplash();
586  
587         if (button == 1) {
588                 // set the pseudo menu-button
589                 fl_set_object_boxtype(ob, FL_DOWN_BOX);
590                 fl_set_button(ob, 0);
591                 fl_redraw_object(ob);
592         }
593
594         // Paranoia check
595         Assert(item->kind() == MenuItem::Submenu);
596         
597         // set tabstop length
598         fl_set_tabstop(menu_tabstop);
599         vector<int> submenus;
600         int menu = iteminfo->pimpl_->
601                 create_submenu(FL_ObjWin(ob), view, 
602                                item->submenu(), submenus);
603         if (menu != -1) {
604                 // place popup
605                 fl_setpup_position(view->getForm()->x + ob->x,
606                                    view->getForm()->y + ob->y + ob->h + 10);   
607                 int choice = fl_dopup(menu);
608                 
609                 if (button == 1) {
610                                 // set the pseudo menu-button back
611                         fl_set_object_boxtype(ob, FL_FLAT_BOX);
612                         fl_redraw_object(ob);
613                 }
614
615                 // If the action value is too low, then it is not a
616                 // valid action, but something else.
617                 if (choice >= action_offset + 1) {
618                         view->getLyXFunc()->Dispatch(choice - action_offset);
619                 }
620                 else {
621                         lyxerr[Debug::GUI]
622                                 << "MenuCallback: ignoring bogus action "
623                                 << choice << endl;
624                 }
625         }
626         else 
627                 lyxerr << "Error in MenuCallback" << endl;
628         
629         std::for_each(submenus.begin(), submenus.end(), fl_freepup);
630         // restore tabstop length
631         fl_set_tabstop(default_tabstop);
632
633 }