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