]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/Menubar_pimpl.C
0a86334e27c07609bcf5348834803dd1e33799af
[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 #if 0
313         //xgettext:no-c-format
314         static char const * MenuNames[3] = { N_("List of Figures%m"),
315         //xgettext:no-c-format
316                                              N_("List of Tables%m"),
317         //xgettext:no-c-format
318                                              N_("List of Algorithms%m") };
319
320         vector<vector<Buffer::TocItem> > toc_list =
321                 owner_->buffer()->getTocList();
322
323         // Handle LOF/LOT/LOA
324         int max_nonempty = 0;
325         for (int i = 1; i <= 3; ++i)
326                 if (!toc_list[i].empty())
327                         max_nonempty = i;
328
329         for (int j = 1; j <= 3; ++j)
330                 if (!toc_list[j].empty()) {
331                         int menu2 = get_new_submenu(smn, win);
332                         for (size_type i = 0; i < toc_list[j].size(); ++i) {
333                                 if (i > max_number_of_items) {
334                                         fl_addtopup(menu2, ". . .%d");
335                                         break;
336                                 }
337                                 int const action = lyxaction.
338                                         getPseudoAction(LFUN_GOTO_PARAGRAPH,
339                                                         tostr(toc_list[j][i].par->id()));
340                                 string label = fixlabel(toc_list[j][i].str);
341                                 label = limit_string_length(label);
342                                 label += "%x" + tostr(action + action_offset);
343                                 fl_addtopup(menu2, label.c_str());
344                         }
345                         if (j == max_nonempty) {
346                                 string label = _(MenuNames[j - 1]);
347                                 label += "%l";
348                                 fl_addtopup(menu, label.c_str(), menu2);
349                         } else
350                                 fl_addtopup(menu, _(MenuNames[j - 1]), menu2);
351                 }
352
353         // Handle normal TOC
354         if (max_nonempty == 0 && toc_list[0].empty()) {
355                 fl_addtopup(menu, (_("No Table of Contents%i")
356                                    + extra_label).c_str());
357                 return;
358         }
359
360         add_toc2(menu, extra_label, smn, win,
361                  toc_list[0], 0, toc_list[0].size(), 0);
362 #else
363 #warning Fix Me! (Lgb)
364         map<string, vector<Buffer::TocItem> > toc_list =
365                 owner_->buffer()->getTocList();
366
367         map<string, vector<Buffer::TocItem> >::const_iterator cit =
368                 toc_list.begin();
369         map<string, vector<Buffer::TocItem> >::const_iterator end =
370                 toc_list.end();
371         for (; cit != end; ++cit) {
372                 // Handle this elsewhere
373                 if (cit->first == "TOC") continue;
374                 
375                 int menu2 = get_new_submenu(smn, win);
376                 vector<Buffer::TocItem>::const_iterator ccit =
377                         cit->second.begin();
378                 vector<Buffer::TocItem>::const_iterator eend =
379                         cit->second.end();
380                 for (; ccit != eend; ++ccit) {
381                         int const action = lyxaction.getPseudoAction(LFUN_GOTO_PARAGRAPH, tostr(ccit->par->id()));
382                         string label = fixlabel(ccit->str);
383                         label = limit_string_length(label);
384                         label += "%x" + tostr(action + action_offset);
385                         fl_addtopup(menu2, label.c_str());
386                         lyxerr << "[" << cit->first << "] " << label << endl;
387                 }
388                 string const m = cit->first + "%m";
389                 fl_addtopup(menu, m.c_str(), menu2);
390         }
391         
392         
393         // Handle normal TOC
394         cit = toc_list.find("TOC");
395         if (cit == end) {
396                 string const tmp = _("No Table of contents%i") + extra_label;
397                 fl_addtopup(menu, tmp.c_str());
398                 return;
399         } else {
400                 add_toc2(menu, extra_label, smn, win,
401                          cit->second, 0, cit->second.size(), 0);
402         }
403         
404 #endif
405 }
406
407 int Menubar::Pimpl::create_submenu(Window win, LyXView * view, 
408                                    string const & menu_name, 
409                                    vector<int> & smn) 
410 {
411         if (!menubackend_->hasMenu(menu_name)){ 
412                 lyxerr << "ERROR:create_submenu: Unknown menu `" 
413                        << menu_name << "'" << endl;
414                 return -1;
415         }
416         Menu md = Menu();
417         menubackend_->getMenu(menu_name).expand(md, owner_->buffer());
418
419         int menu = get_new_submenu(smn, win);
420         fl_setpup_softedge(menu, true);
421         fl_setpup_bw(menu, -1);
422         lyxerr[Debug::GUI] << "Adding menu " << menu 
423                            << " in deletion list" << endl;
424
425         // Compute the size of the largest label (because xforms is
426         // not able to support shortcuts correctly...)
427         int max_width = 0;
428         string widest_label;
429         Menu::const_iterator end = md.end();
430         for (Menu::const_iterator i = md.begin(); i != end; ++i) {
431                 MenuItem const & item = (*i);
432                 if (item.kind() == MenuItem::Command) {
433                         string label = item.label() + '\t';
434                         int width = string_width(label);
435                         if (width > max_width) {
436                                 max_width = width;
437                                 widest_label = label;
438                         }
439                 }
440         }
441         lyxerr[Debug::GUI] << "max_width=" << max_width 
442                            << ", widest_label=`" << widest_label 
443                            << "'" << endl;
444
445         // Compute where to put separators
446         vector<string> extra_labels(md.size());
447         vector<string>::iterator it = extra_labels.begin();
448         vector<string>::iterator last = it;
449         for (Menu::const_iterator i = md.begin(); i != end; ++i, ++it)
450                 if (i->kind() == MenuItem::Separator)
451                         *last = "%l";
452                 else if (!i->optional() ||
453                          !(view->getLyXFunc()->getStatus(i->action()) & LyXFunc::Disabled))
454                         last = it;
455
456         it = extra_labels.begin();
457         for (Menu::const_iterator i = md.begin(); i != end; ++i, ++it) {
458                 MenuItem const & item = (*i);
459                 string & extra_label = *it;
460
461                 switch (item.kind()) {
462                 case MenuItem::Command: {
463                         LyXFunc::func_status flag = 
464                                 view->getLyXFunc()->getStatus(item.action()); 
465
466                         // handle optional entries.
467                         if (item.optional() && (flag & LyXFunc::Disabled)) {
468                                 lyxerr[Debug::GUI] 
469                                         << "Skipping optional item " 
470                                         << item.label() << endl; 
471                                 break;
472                         }
473
474                         // Get the keys bound to this action, but keep only the
475                         // first one later
476                         string const accel = toplevel_keymap->findbinding(item.action());
477                         // Build the menu label from all the info
478                         string label = item.label();
479
480                         if (!accel.empty()) {
481                                 // Try to be clever and add  just enough
482                                 // tabs to align shortcuts.
483                                 do 
484                                         label += '\t';
485                                 while (string_width(label) < max_width + 5);
486                                 label += accel.substr(1,accel.find(']') - 1);
487                         }
488                         label += "%x" + tostr(item.action() + action_offset)
489                                 + extra_label;
490                         
491                         // Modify the entry using the function status
492                         string pupmode;
493                         if (flag & (LyXFunc::Disabled | LyXFunc::Unknown))
494                                 pupmode += "%i";
495                         if (flag & LyXFunc::ToggleOn)
496                                 pupmode += "%B";
497                         if (flag & LyXFunc::ToggleOff)
498                                 pupmode += "%b";
499                         label += pupmode;
500
501                         // Finally the menu shortcut
502                         string shortcut = item.shortcut();
503
504                         if (!shortcut.empty()) {
505                                 shortcut += lowercase(shortcut[0]);
506                                 label += "%h";
507                                 fl_addtopup(menu, label.c_str(), 
508                                             shortcut.c_str());
509                         } else
510                                 fl_addtopup(menu, label.c_str());
511                         
512                         lyxerr[Debug::GUI] << "Command: \""  
513                                            << lyxaction.getActionName(item.action())
514                                            << "\", binding \"" << accel
515                                            << "\", shortcut \"" << shortcut 
516                                            << "\"" << endl;
517                         break;
518                 }
519
520                 case MenuItem::Submenu: {
521                         int submenu = create_submenu(win, view, 
522                                                      item.submenu(), smn);
523                         if (submenu == -1)
524                                 return -1;
525                         string label = item.label();
526                         label += extra_label + "%m";
527                         string shortcut = item.shortcut();
528                         if (!shortcut.empty()) {
529                                 shortcut += lowercase(shortcut[0]);
530                                 label += "%h";
531                                 fl_addtopup(menu, label.c_str(),
532                                             submenu, shortcut.c_str());
533                         }
534                         else {
535                                 fl_addtopup(menu, label.c_str(), submenu);
536                         }
537                         break;
538                 }
539
540                 case MenuItem::Separator:
541                         // already done, and if it was the first one,
542                         // we just ignore it.
543                         break;
544
545                 case MenuItem::Toc:
546                         add_toc(menu, extra_label, smn, win);
547                         break;
548
549                 case MenuItem::Documents: 
550                 case MenuItem::Lastfiles: 
551                 case MenuItem::ViewFormats:
552                 case MenuItem::UpdateFormats:
553                 case MenuItem::ExportFormats:
554                 case MenuItem::ImportFormats:
555                         lyxerr << "Menubar::Pimpl::create_submenu: "
556                                 "this should not happen" << endl;
557                         break;
558
559                 }
560         }
561         return menu;
562 }
563
564
565 extern "C"
566 void C_Menubar_Pimpl_MenuCallback(FL_OBJECT * ob, long button)
567 {
568         Menubar::Pimpl::MenuCallback(ob, button);
569 }
570
571
572 void Menubar::Pimpl::MenuCallback(FL_OBJECT * ob, long button)
573 {
574         ItemInfo * iteminfo = static_cast<ItemInfo *>(ob->u_vdata);
575 //      lyxerr << "MenuCallback: ItemInfo address=" << iteminfo
576 //             << "Val=(pimpl_=" << iteminfo->pimpl_
577 //             << ", item_=" << iteminfo->item_
578 //             << ", obj_=" << iteminfo->obj_ << ")" <<endl;
579
580         LyXView * view = iteminfo->pimpl_->owner_;
581         MenuItem const * item = iteminfo->item_.get();
582
583         if (button == 1) {
584                 // set the pseudo menu-button
585                 fl_set_object_boxtype(ob, FL_DOWN_BOX);
586                 fl_set_button(ob, 0);
587                 fl_redraw_object(ob);
588         }
589
590         // Paranoia check
591         Assert(item->kind() == MenuItem::Submenu);
592         
593         // set tabstop length
594         fl_set_tabstop(menu_tabstop);
595         vector<int> submenus;
596         int menu = iteminfo->pimpl_->
597                 create_submenu(FL_ObjWin(ob), view, 
598                                item->submenu(), submenus);
599         if (menu != -1) {
600                 // place popup
601                 fl_setpup_position(view->getForm()->x + ob->x,
602                                    view->getForm()->y + ob->y + ob->h + 10);   
603                 int choice = fl_dopup(menu);
604                 
605                 if (button == 1) {
606                                 // set the pseudo menu-button back
607                         fl_set_object_boxtype(ob, FL_FLAT_BOX);
608                         fl_redraw_object(ob);
609                 }
610
611                 // If the action value is too low, then it is not a
612                 // valid action, but something else.
613                 if (choice >= action_offset + 1) {
614                         view->getLyXFunc()->Dispatch(choice - action_offset);
615                 }
616                 else {
617                         lyxerr[Debug::GUI]
618                                 << "MenuCallback: ignoring bogus action "
619                                 << choice << endl;
620                 }
621         }
622         else 
623                 lyxerr << "Error in MenuCallback" << endl;
624         
625         std::for_each(submenus.begin(), submenus.end(), fl_freepup);
626         // restore tabstop length
627         fl_set_tabstop(default_tabstop);
628
629 }