]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/Menubar_pimpl.C
move toc menu code to menubackend
[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_->submenuname() == 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 Menu::size_type const max_number_of_items = 25;
170
171 int get_new_submenu(vector<int> & smn, Window win)
172 {
173         static size_type max_number_of_menus = 32;
174         if (smn.size() >= max_number_of_menus)
175                 max_number_of_menus =
176                     fl_setpup_maxpup(static_cast<int>(2*smn.size()));
177         int menu = fl_newpup(win);
178         fl_setpup_softedge(menu, true);
179         fl_setpup_bw(menu, -1);
180         lyxerr[Debug::GUI] << "Adding menu " << menu
181                            << " in deletion list" << endl;
182         smn.push_back(menu);
183         return menu;
184 }
185
186
187 string const fixlabel(string const & str)
188 {
189 #if FL_VERSION < 1 && FL_REVISION < 89
190         return subst(str, '%', '?');
191 #else
192         return subst(str, "%", "%%");
193 #endif
194 }
195
196
197
198 } // namespace anon
199
200
201
202 int Menubar::Pimpl::create_submenu(Window win, XFormsView * view,
203                                    Menu const & menu, vector<int> & smn)
204 {
205         const int menuid = get_new_submenu(smn, win);
206         lyxerr[Debug::GUI] << "Menubar::Pimpl::create_submenu: creating "
207                            << menu.name() << " as menuid=" << menuid << endl;
208
209         // Compute the size of the largest label (because xforms is
210         // not able to support shortcuts correctly...)
211         int max_width = 0;
212         string widest_label;
213         Menu::const_iterator end = menu.end();
214         for (Menu::const_iterator i = menu.begin(); i != end; ++i) {
215                 MenuItem const & item = (*i);
216                 if (item.kind() == MenuItem::Command) {
217                         string const label = item.label() + '\t';
218                         int const width = string_width(label);
219                         if (width > max_width) {
220                                 max_width = width;
221                                 widest_label = label;
222                         }
223                 }
224         }
225         lyxerr[Debug::GUI] << "max_width=" << max_width
226                            << ", widest_label=`" << widest_label
227                            << "'" << endl;
228
229         // Compute where to put separators
230         vector<string> extra_labels(menu.size());
231         vector<string>::iterator it = extra_labels.begin();
232         vector<string>::iterator last = it;
233         for (Menu::const_iterator i = menu.begin(); i != end; ++i, ++it)
234                 if (i->kind() == MenuItem::Separator)
235                         *last = "%l";
236                 else if (!i->optional() ||
237                          !(view->getLyXFunc()->getStatus(i->action()).disabled()))
238                         last = it;
239
240         it = extra_labels.begin();
241         size_type count = 0;
242         int curmenuid = menuid;
243         for (Menu::const_iterator i = menu.begin(); i != end; ++i, ++it) {
244                 MenuItem const & item = (*i);
245                 string & extra_label = *it;
246
247                 ++count;
248                 if (count > max_number_of_items) {
249                         int tmpmenuid = get_new_submenu(smn, win);
250                         lyxerr[Debug::GUI] << "Too many items, creating "
251                                            << "new menu " << tmpmenuid << endl;
252                         string label = _("More");
253                         label += "...%m";
254                         fl_addtopup(curmenuid, label.c_str(), tmpmenuid);
255                         count = 1;
256                         curmenuid = tmpmenuid;
257                 }
258
259                 switch (item.kind()) {
260                 case MenuItem::Command: {
261                         FuncStatus const flag =
262                                 view->getLyXFunc()->getStatus(item.action());
263                         // handle optional entries.
264                         if (item.optional()
265                             && (flag.disabled())) {
266                                 lyxerr[Debug::GUI]
267                                         << "Skipping optional item "
268                                         << item.label() << endl;
269                                 break;
270                         }
271
272                         // Get the keys bound to this action, but keep only the
273                         // first one later
274                         string const accel =
275                                 toplevel_keymap->findbinding(item.action());
276                         // Build the menu label from all the info
277                         string label = fixlabel(item.label());
278
279                         if (!accel.empty()) {
280                                 // Try to be clever and add  just enough
281                                 // tabs to align shortcuts.
282                                 do
283                                         label += '\t';
284                                 while (string_width(label) < max_width + 5);
285                                 label += accel.substr(1,accel.find(']') - 1);
286                         }
287                         label += "%x" + tostr(item.action() + action_offset)
288                                 + extra_label;
289
290                         // Modify the entry using the function status
291                         string pupmode;
292                         if (flag.onoff(true))
293                                 pupmode += "%B";
294                         if (flag.onoff(false))
295                                 pupmode += "%b";
296                         if (flag.disabled() || flag.unknown())
297                                 pupmode += "%i";
298                         label += pupmode;
299
300                         // Finally the menu shortcut
301                         string shortcut = item.shortcut();
302
303                         if (!shortcut.empty()) {
304                                 shortcut += lowercase(shortcut[0]);
305                                 label += "%h";
306                                 fl_addtopup(curmenuid, label.c_str(),
307                                             shortcut.c_str());
308                         } else
309                                 fl_addtopup(curmenuid, label.c_str());
310
311                         lyxerr[Debug::GUI] << "Command: \""
312                                            << lyxaction.getActionName(item.action())
313                                            << "\", binding \"" << accel
314                                            << "\", shortcut \"" << shortcut
315                                            << "\" (added to menu"
316                                            << curmenuid << ")" << endl;
317                         break;
318                 }
319
320                 case MenuItem::Submenu: {
321                         int submenuid = create_submenu(win, view,
322                                                        *item.submenu(), smn);
323                         if (submenuid == -1)
324                                 return -1;
325                         string label = fixlabel(item.label());
326                         label += extra_label + "%m";
327                         string shortcut = item.shortcut();
328                         if (!shortcut.empty()) {
329                                 shortcut += lowercase(shortcut[0]);
330                                 label += "%h";
331                                 fl_addtopup(curmenuid, label.c_str(),
332                                             submenuid, shortcut.c_str());
333                         } else {
334                                 fl_addtopup(curmenuid, label.c_str(),
335                                             submenuid);
336                         }
337                         break;
338                 }
339
340                 case MenuItem::Separator:
341                         // already done, and if it was the first one,
342                         // we just ignore it.
343                         break;
344
345
346                 default:
347                         lyxerr << "Menubar::Pimpl::create_submenu: "
348                                 "this should not happen" << endl;
349                         break;
350                 }
351         }
352         return menuid;
353 }
354
355
356 void Menubar::Pimpl::MenuCallback(FL_OBJECT * ob, long button)
357 {
358         ItemInfo * iteminfo = static_cast<ItemInfo *>(ob->u_vdata);
359         XFormsView * view = iteminfo->pimpl_->owner_;
360         MenuItem const * item = iteminfo->item_.get();
361
362         if (button == 1) {
363                 // set the pseudo menu-button
364                 fl_set_object_boxtype(ob, FL_DOWN_BOX);
365                 fl_set_button(ob, 0);
366                 fl_redraw_object(ob);
367         }
368
369         // Paranoia check
370         lyx::Assert(item->kind() == MenuItem::Submenu);
371
372         // set tabstop length
373         fl_set_tabstop(menu_tabstop);
374
375         MenuBackend const * menubackend_ = iteminfo->pimpl_->menubackend_;
376         Menu tomenu;
377         Menu const frommenu = menubackend_->getMenu(item->submenuname());
378         menubackend_->expand(frommenu, tomenu, view->buffer());
379         vector<int> submenus;
380         int menu = iteminfo->pimpl_->
381                 create_submenu(FL_ObjWin(ob), view, tomenu, submenus);
382         if (menu != -1) {
383                 // place popup
384                 fl_setpup_position(view->getForm()->x + ob->x,
385                                    view->getForm()->y + ob->y + ob->h + 10);
386                 int choice = fl_dopup(menu);
387
388                 if (button == 1) {
389                                 // set the pseudo menu-button back
390                         fl_set_object_boxtype(ob, FL_FLAT_BOX);
391                         fl_redraw_object(ob);
392                 }
393
394                 // If the action value is too low, then it is not a
395                 // valid action, but something else.
396                 if (choice >= action_offset + 1) {
397                         view->getLyXFunc()->dispatch(choice - action_offset, true);
398                 } else {
399                         lyxerr[Debug::GUI]
400                                 << "MenuCallback: ignoring bogus action "
401                                 << choice << endl;
402                 }
403         } else {
404                 lyxerr << "Error in MenuCallback" << endl;
405         }
406
407         for_each(submenus.begin(), submenus.end(), fl_freepup);
408         // restore tabstop length
409         fl_set_tabstop(default_tabstop);
410
411 }