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