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