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