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