]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/Menubar_pimpl.C
toc support reorganization; changes to xform_helpers; aspect ratio patch from herbert
[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 "Dialogs.h"
20 #include "XFormsView.h"
21 #include "lyxfunc.h"
22 #include "FloatList.h"
23 #include "support/lstrings.h"
24 #include "support/LAssert.h"
25 #include "gettext.h"
26 #include "debug.h"
27 #include "toc.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               toc::Toc 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 = toc_list[i].action();
216                         string label(4 * max(0, toc_list[i].depth - depth),' ');
217                         label += fixlabel(toc_list[i].str);
218                         label = limit_string_length(label);
219                         label += "%x" + tostr(action + action_offset);
220                         if (i == to - 1 && depth == 0)
221                                 label += extra_label;
222                         if (toc_list[i].depth == depth
223                             && ++shortcut_count <= 9) {
224                                 label += "%h";
225                                 fl_addtopup(menu, label.c_str(),
226                                             tostr(shortcut_count).c_str());
227                         } else
228                                 fl_addtopup(menu, label.c_str());
229                 }
230         } else {
231                 size_type pos = from;
232                 size_type count = 0;
233                 while (pos < to) {
234                         ++count;
235                         if (count > max_number_of_items) {
236                                 int menu2 = get_new_submenu(smn, win);
237                                 add_toc2(menu2, extra_label, smn, win,
238                                          toc_list, pos, to, depth);
239                                 string label = _("More");
240                                 label += "...%m";
241                                 if (depth == 0)
242                                         label += extra_label;
243                                 fl_addtopup(menu, label.c_str(), menu2);
244                                 break;
245                         }
246                         size_type new_pos = pos+1;
247                         while (new_pos < to &&
248                                toc_list[new_pos].depth > depth)
249                                 ++new_pos;
250
251                         int const action = toc_list[pos].action();
252                         string label(4 * max(0, toc_list[pos].depth - depth), ' ');
253                         label += fixlabel(toc_list[pos].str);
254                         label = limit_string_length(label);
255                         if (new_pos == to && depth == 0)
256                                 label += extra_label;
257                         string shortcut;
258                         if (toc_list[pos].depth == depth &&
259                             ++shortcut_count <= 9)
260                                 shortcut = tostr(shortcut_count);
261
262                         if (new_pos == pos + 1) {
263                                 label += "%x" + tostr(action + action_offset);
264                                 if (!shortcut.empty()) {
265                                         label += "%h";
266                                         fl_addtopup(menu, label.c_str(),
267                                                     shortcut.c_str());
268                                 } else
269                                         fl_addtopup(menu, label.c_str());
270                         } else {
271                                 int menu2 = get_new_submenu(smn, win);
272                                 add_toc2(menu2, extra_label, smn, win,
273                                          toc_list, pos, new_pos, depth+1);
274                                 label += "%m";
275                                 if (!shortcut.empty()) {
276                                         label += "%h";
277                                         fl_addtopup(menu, label.c_str(), menu2,
278                                                     shortcut.c_str());
279                                 } else
280                                         fl_addtopup(menu, label.c_str(), menu2);
281                         }
282                         pos = new_pos;
283                 }
284         }
285 }
286
287 } // namespace anon
288
289
290 void Menubar::Pimpl::add_toc(int menu, string const & extra_label,
291                              vector<int> & smn, Window win)
292 {
293         if (!owner_->buffer())
294                 return;
295         toc::TocList toc_list = toc::getTocList(owner_->buffer());
296         toc::TocList::const_iterator cit = toc_list.begin();
297         toc::TocList::const_iterator end = toc_list.end();
298         for (; cit != end; ++cit) {
299                 // Handle this elsewhere
300                 if (cit->first == "TOC") continue;
301
302                 // All the rest is for floats
303                 int menu_first_sub = get_new_submenu(smn, win);
304                 int menu_current = menu_first_sub;
305                 toc::Toc::const_iterator ccit = cit->second.begin();
306                 toc::Toc::const_iterator eend = cit->second.end();
307                 size_type count = 0;
308                 for (; ccit != eend; ++ccit) {
309                         ++count;
310                         if (count > max_number_of_items) {
311                                 int menu_tmp = get_new_submenu(smn, win);
312                                 string label = _("More");
313                                 label += "...%m";
314                                 fl_addtopup(menu_current, label.c_str(), menu_tmp);
315                                 count = 1;
316                                 menu_current = menu_tmp;
317                         }
318                         int const action = ccit->action();
319                         string label = fixlabel(ccit->str);
320                         label = limit_string_length(label);
321                         label += "%x" + tostr(action + action_offset);
322                         fl_addtopup(menu_current, label.c_str());
323                 }
324                 string const m = floatList[cit->first]->second.name() + "%m";
325                 fl_addtopup(menu, m.c_str(), menu_first_sub);
326         }
327
328
329         // Handle normal TOC
330         cit = toc_list.find("TOC");
331         if (cit == end) {
332                 string const tmp = _("No Table of contents%i") + extra_label;
333                 fl_addtopup(menu, tmp.c_str());
334                 return;
335         } else {
336                 add_toc2(menu, extra_label, smn, win,
337                          cit->second, 0, cit->second.size(), 0);
338         }
339 }
340
341
342 int Menubar::Pimpl::create_submenu(Window win, XFormsView * view,
343                                    string const & menu_name,
344                                    vector<int> & smn)
345 {
346         if (!menubackend_->hasMenu(menu_name)) {
347                 lyxerr << "ERROR:create_submenu: Unknown menu `"
348                        << menu_name << "'" << endl;
349                 return -1;
350         }
351         Menu md;
352         menubackend_->getMenu(menu_name).expand(md, owner_->buffer());
353
354         int const menu = get_new_submenu(smn, win);
355         fl_setpup_softedge(menu, true);
356         fl_setpup_bw(menu, -1);
357         lyxerr[Debug::GUI] << "Adding menu " << menu
358                            << " in deletion list" << endl;
359
360         // Compute the size of the largest label (because xforms is
361         // not able to support shortcuts correctly...)
362         int max_width = 0;
363         string widest_label;
364         Menu::const_iterator end = md.end();
365         for (Menu::const_iterator i = md.begin(); i != end; ++i) {
366                 MenuItem const & item = (*i);
367                 if (item.kind() == MenuItem::Command) {
368                         string const label = item.label() + '\t';
369                         int const width = string_width(label);
370                         if (width > max_width) {
371                                 max_width = width;
372                                 widest_label = label;
373                         }
374                 }
375         }
376         lyxerr[Debug::GUI] << "max_width=" << max_width
377                            << ", widest_label=`" << widest_label
378                            << "'" << endl;
379
380         // Compute where to put separators
381         vector<string> extra_labels(md.size());
382         vector<string>::iterator it = extra_labels.begin();
383         vector<string>::iterator last = it;
384         for (Menu::const_iterator i = md.begin(); i != end; ++i, ++it)
385                 if (i->kind() == MenuItem::Separator)
386                         *last = "%l";
387                 else if (!i->optional() ||
388                          !(view->getLyXFunc()->getStatus(i->action()).disabled()))
389                         last = it;
390
391         it = extra_labels.begin();
392         for (Menu::const_iterator i = md.begin(); i != end; ++i, ++it) {
393                 MenuItem const & item = (*i);
394                 string & extra_label = *it;
395
396                 switch (item.kind()) {
397                 case MenuItem::Command: {
398                         FuncStatus const flag =
399                                 view->getLyXFunc()->getStatus(item.action());
400                         // handle optional entries.
401                         if (item.optional()
402                             && (flag.disabled())) {
403                                 lyxerr[Debug::GUI]
404                                         << "Skipping optional item "
405                                         << item.label() << endl;
406                                 break;
407                         }
408
409                         // Get the keys bound to this action, but keep only the
410                         // first one later
411                         string const accel = toplevel_keymap->findbinding(kb_action(item.action()));
412                         // Build the menu label from all the info
413                         string label = item.label();
414
415                         if (!accel.empty()) {
416                                 // Try to be clever and add  just enough
417                                 // tabs to align shortcuts.
418                                 do
419                                         label += '\t';
420                                 while (string_width(label) < max_width + 5);
421                                 label += accel.substr(1,accel.find(']') - 1);
422                         }
423                         label += "%x" + tostr(item.action() + action_offset)
424                                 + extra_label;
425
426                         // Modify the entry using the function status
427                         string pupmode;
428                         if (flag.onoff(true))
429                                 pupmode += "%B";
430                         if (flag.onoff(false))
431                                 pupmode += "%b";
432                         if (flag.disabled() || flag.unknown())
433                                 pupmode += "%i";
434                         label += pupmode;
435
436                         // Finally the menu shortcut
437                         string shortcut = item.shortcut();
438
439                         if (!shortcut.empty()) {
440                                 shortcut += lowercase(shortcut[0]);
441                                 label += "%h";
442                                 fl_addtopup(menu, label.c_str(),
443                                             shortcut.c_str());
444                         } else
445                                 fl_addtopup(menu, label.c_str());
446
447                         lyxerr[Debug::GUI] << "Command: \""
448                                            << lyxaction.getActionName(item.action())
449                                            << "\", binding \"" << accel
450                                            << "\", shortcut \"" << shortcut
451                                            << "\"" << endl;
452                         break;
453                 }
454
455                 case MenuItem::Submenu: {
456                         int submenu = create_submenu(win, view,
457                                                      item.submenu(), smn);
458                         if (submenu == -1)
459                                 return -1;
460                         string label = item.label();
461                         label += extra_label + "%m";
462                         string shortcut = item.shortcut();
463                         if (!shortcut.empty()) {
464                                 shortcut += lowercase(shortcut[0]);
465                                 label += "%h";
466                                 fl_addtopup(menu, label.c_str(),
467                                             submenu, shortcut.c_str());
468                         } else {
469                                 fl_addtopup(menu, label.c_str(), submenu);
470                         }
471                         break;
472                 }
473
474                 case MenuItem::Separator:
475                         // already done, and if it was the first one,
476                         // we just ignore it.
477                         break;
478
479                 case MenuItem::Toc:
480                         add_toc(menu, extra_label, smn, win);
481                         break;
482
483                 case MenuItem::Documents:
484                 case MenuItem::Lastfiles:
485                 case MenuItem::ViewFormats:
486                 case MenuItem::UpdateFormats:
487                 case MenuItem::ExportFormats:
488                 case MenuItem::ImportFormats:
489                 case MenuItem::FloatListInsert:
490                 case MenuItem::FloatInsert:
491                         lyxerr << "Menubar::Pimpl::create_submenu: "
492                                 "this should not happen" << endl;
493                         break;
494
495                 }
496         }
497         return menu;
498 }
499
500
501 void Menubar::Pimpl::MenuCallback(FL_OBJECT * ob, long button)
502 {
503         ItemInfo * iteminfo = static_cast<ItemInfo *>(ob->u_vdata);
504 //      lyxerr << "MenuCallback: ItemInfo address=" << iteminfo
505 //             << "Val=(pimpl_=" << iteminfo->pimpl_
506 //             << ", item_=" << iteminfo->item_
507 //             << ", obj_=" << iteminfo->obj_ << ")" <<endl;
508
509         XFormsView * view = iteminfo->pimpl_->owner_;
510         MenuItem const * item = iteminfo->item_.get();
511
512         if (button == 1) {
513                 // set the pseudo menu-button
514                 fl_set_object_boxtype(ob, FL_DOWN_BOX);
515                 fl_set_button(ob, 0);
516                 fl_redraw_object(ob);
517         }
518
519         // Paranoia check
520         lyx::Assert(item->kind() == MenuItem::Submenu);
521
522         // set tabstop length
523         fl_set_tabstop(menu_tabstop);
524         vector<int> submenus;
525         int menu = iteminfo->pimpl_->
526                 create_submenu(FL_ObjWin(ob), view,
527                                item->submenu(), submenus);
528         if (menu != -1) {
529                 // place popup
530                 fl_setpup_position(view->getForm()->x + ob->x,
531                                    view->getForm()->y + ob->y + ob->h + 10);
532                 int choice = fl_dopup(menu);
533
534                 if (button == 1) {
535                                 // set the pseudo menu-button back
536                         fl_set_object_boxtype(ob, FL_FLAT_BOX);
537                         fl_redraw_object(ob);
538                 }
539
540                 // If the action value is too low, then it is not a
541                 // valid action, but something else.
542                 if (choice >= action_offset + 1) {
543                         view->getLyXFunc()->dispatch(choice - action_offset, true);
544                 } else {
545                         lyxerr[Debug::GUI]
546                                 << "MenuCallback: ignoring bogus action "
547                                 << choice << endl;
548                 }
549         } else {
550                 lyxerr << "Error in MenuCallback" << endl;
551         }
552
553         for_each(submenus.begin(), submenus.end(), fl_freepup);
554         // restore tabstop length
555         fl_set_tabstop(default_tabstop);
556
557 }