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