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