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