]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/Menubar_pimpl.C
some minor lyxaction cleanup
[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 "support/lstrings.h"
23 #include "support/LAssert.h"
24 #include "gettext.h"
25 #include "debug.h"
26 #include FORMS_H_LOCATION
27
28 #include <boost/scoped_ptr.hpp>
29
30 #include <algorithm>
31
32 using std::endl;
33 using std::vector;
34 using std::max;
35 using std::min;
36 using std::for_each;
37
38 typedef vector<int>::size_type size_type;
39
40 extern boost::scoped_ptr<kb_keymap> toplevel_keymap;
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_(static_cast<XFormsView*>(view)), menubackend_(&mb)
88 {
89         makeMenubar(menubackend_->getMenubar());
90 }
91
92
93 void Menubar::Pimpl::makeMenubar(Menu const & menu)
94 {
95         FL_FORM * form = owner_->getForm();
96         int moffset = 0;
97
98         // Create menu frame if there is non yet.
99         FL_OBJECT * frame = fl_add_frame(FL_UP_FRAME, 0, 0,
100                                          form->w, mheight, "");
101         fl_set_object_resize(frame, FL_RESIZE_ALL);
102         fl_set_object_gravity(frame, NorthWestGravity,
103                               NorthEastGravity);
104
105         Menu::const_iterator i = menu.begin();
106         Menu::const_iterator end = menu.end();
107         for (; i != end; ++i) {
108                 FL_OBJECT * obj;
109                 if (i->kind() != MenuItem::Submenu) {
110                         lyxerr << "ERROR: Menubar::Pimpl::createMenubar:"
111                                 " only submenus can appear in a menubar"
112                                << endl;
113                         continue;
114                 }
115                 string const label = i->label();
116                 string const shortcut = "#" + i->shortcut();
117                 int const width = string_width(label);
118                 obj = fl_add_button(FL_MENU_BUTTON,
119                                     air + moffset, yloc,
120                                     width + mbadd,
121                                     mbheight,
122                                     label.c_str());
123                 fl_set_object_boxtype(obj, FL_FLAT_BOX);
124                 fl_set_object_color(obj, FL_MCOL, FL_MCOL);
125                 fl_set_object_lsize(obj, MENU_LABEL_SIZE);
126                 fl_set_object_lstyle(obj, MENU_LABEL_STYLE);
127                 fl_set_object_resize(obj, FL_RESIZE_ALL);
128                 fl_set_object_gravity(obj, NorthWestGravity,
129                                       NorthWestGravity);
130                 moffset += obj->w + air;
131                 fl_set_object_shortcut(obj, shortcut.c_str(), 1);
132                 fl_set_object_callback(obj, C_Menubar_Pimpl_MenuCallback, 1);
133
134                 boost::shared_ptr<ItemInfo>
135                         iteminfo(new ItemInfo(this, new MenuItem(*i), obj));
136                 buttonlist_.push_back(iteminfo);
137                 obj->u_vdata = iteminfo.get();
138         }
139
140 }
141
142
143 void Menubar::Pimpl::update()
144 {
145         // nothing yet
146 }
147
148
149 void Menubar::Pimpl::openByName(string const & name)
150 {
151         for (ButtonList::const_iterator cit = buttonlist_.begin();
152              cit != buttonlist_.end(); ++cit) {
153                 if ((*cit)->item_->submenuname() == name) {
154                         MenuCallback((*cit)->obj_, 1);
155                         return;
156                 }
157         }
158
159         lyxerr << "Menubar::Pimpl::openByName: menu "
160                << name << " not found" << endl;
161 }
162
163
164 namespace {
165
166 Menu::size_type const max_number_of_items = 25;
167
168 int get_new_submenu(vector<int> & smn, Window win)
169 {
170         static size_type max_number_of_menus = 32;
171         if (smn.size() >= max_number_of_menus)
172                 max_number_of_menus =
173                     fl_setpup_maxpup(static_cast<int>(2*smn.size()));
174         int menu = fl_newpup(win);
175         fl_setpup_softedge(menu, true);
176         fl_setpup_bw(menu, -1);
177         lyxerr[Debug::GUI] << "Adding menu " << menu
178                            << " in deletion list" << endl;
179         smn.push_back(menu);
180         return menu;
181 }
182
183
184 string const fixlabel(string const & str)
185 {
186 #if FL_VERSION < 1 && FL_REVISION < 89
187         return subst(str, '%', '?');
188 #else
189         return subst(str, "%", "%%");
190 #endif
191 }
192
193
194
195 } // namespace anon
196
197
198
199 int Menubar::Pimpl::create_submenu(Window win, XFormsView * view,
200                                    Menu const & menu, vector<int> & smn,
201                                    bool & all_disabled)
202 {
203         const int menuid = get_new_submenu(smn, win);
204         lyxerr[Debug::GUI] << "Menubar::Pimpl::create_submenu: creating "
205                            << menu.name() << " as menuid=" << menuid << endl;
206
207         // Compute the size of the largest label (because xforms is
208         // not able to support shortcuts correctly...)
209         int max_width = 0;
210         string widest_label;
211         Menu::const_iterator end = menu.end();
212         for (Menu::const_iterator i = menu.begin(); i != end; ++i) {
213                 MenuItem const & item = (*i);
214                 if (item.kind() == MenuItem::Command) {
215                         string const label = item.label() + '\t';
216                         int const width = string_width(label);
217                         if (width > max_width) {
218                                 max_width = width;
219                                 widest_label = label;
220                         }
221                 }
222         }
223         lyxerr[Debug::GUI] << "max_width=" << max_width
224                            << ", widest_label=`" << widest_label
225                            << "'" << endl;
226
227         // Compute where to put separators
228         vector<string> extra_labels(menu.size());
229         vector<string>::iterator it = extra_labels.begin();
230         vector<string>::iterator last = it;
231         for (Menu::const_iterator i = menu.begin(); i != end; ++i, ++it)
232                 if (i->kind() == MenuItem::Separator)
233                         *last = "%l";
234                 else if (!i->optional() ||
235                          !(view->getLyXFunc()->getStatus(i->action()).disabled()))
236                         last = it;
237
238         it = extra_labels.begin();
239         size_type count = 0;
240         int curmenuid = menuid;
241         for (Menu::const_iterator i = menu.begin(); i != end; ++i, ++it) {
242                 MenuItem const & item = (*i);
243                 string & extra_label = *it;
244
245                 ++count;
246                 if (count > max_number_of_items) {
247                         int tmpmenuid = get_new_submenu(smn, win);
248                         lyxerr[Debug::GUI] << "Too many items, creating "
249                                            << "new menu " << tmpmenuid << endl;
250                         string label = _("More");
251                         label += "...%m";
252                         fl_addtopup(curmenuid, label.c_str(), tmpmenuid);
253                         count = 1;
254                         curmenuid = tmpmenuid;
255                 }
256
257                 switch (item.kind()) {
258                 case MenuItem::Command: {
259                         FuncStatus const flag =
260                                 view->getLyXFunc()->getStatus(item.action());
261                         // handle optional entries.
262                         if (item.optional()
263                             && (flag.disabled())) {
264                                 lyxerr[Debug::GUI]
265                                         << "Skipping optional item "
266                                         << item.label() << endl;
267                                 break;
268                         }
269
270                         // Get the keys bound to this action, but keep only the
271                         // first one later
272                         string const accel =
273                                 toplevel_keymap->findbinding(item.action());
274                         // Build the menu label from all the info
275                         string label = fixlabel(item.label());
276
277                         if (!accel.empty()) {
278                                 // Try to be clever and add  just enough
279                                 // tabs to align shortcuts.
280                                 do
281                                         label += '\t';
282                                 while (string_width(label) < max_width + 5);
283                                 label += accel.substr(1,accel.find(']') - 1);
284                         }
285                         label += "%x" + tostr(item.action() + action_offset)
286                                 + extra_label;
287
288                         // Modify the entry using the function status
289                         string pupmode;
290                         if (flag.onoff(true))
291                                 pupmode += "%B";
292                         if (flag.onoff(false))
293                                 pupmode += "%b";
294                         if (flag.disabled() || flag.unknown())
295                                 pupmode += "%i";
296                         else
297                                 all_disabled = false;
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                                                        all_disabled);
324                         if (submenuid == -1)
325                                 return -1;
326                         string label = fixlabel(item.label());
327                         label += extra_label + "%m";
328                         if (all_disabled)
329                                 label += "%i";
330                         string shortcut = item.shortcut();
331                         if (!shortcut.empty()) {
332                                 shortcut += lowercase(shortcut[0]);
333                                 label += "%h";
334                                 fl_addtopup(curmenuid, label.c_str(),
335                                             submenuid, shortcut.c_str());
336                         } else {
337                                 fl_addtopup(curmenuid, label.c_str(),
338                                             submenuid);
339                         }
340                         break;
341                 }
342
343                 case MenuItem::Separator:
344                         // already done, and if it was the first one,
345                         // we just ignore it.
346                         break;
347
348
349                 default:
350                         lyxerr << "Menubar::Pimpl::create_submenu: "
351                                 "this should not happen" << endl;
352                         break;
353                 }
354         }
355         return menuid;
356 }
357
358
359 void Menubar::Pimpl::MenuCallback(FL_OBJECT * ob, long button)
360 {
361         ItemInfo * iteminfo = static_cast<ItemInfo *>(ob->u_vdata);
362         XFormsView * view = iteminfo->pimpl_->owner_;
363         MenuItem const * item = iteminfo->item_.get();
364
365         if (button == 1) {
366                 // set the pseudo menu-button
367                 fl_set_object_boxtype(ob, FL_DOWN_BOX);
368                 fl_set_button(ob, 0);
369                 fl_redraw_object(ob);
370         }
371
372         // Paranoia check
373         lyx::Assert(item->kind() == MenuItem::Submenu);
374
375         // set tabstop length
376         fl_set_tabstop(menu_tabstop);
377
378         MenuBackend const * menubackend_ = iteminfo->pimpl_->menubackend_;
379         Menu tomenu;
380         Menu const frommenu = menubackend_->getMenu(item->submenuname());
381         menubackend_->expand(frommenu, tomenu, view->buffer());
382         vector<int> submenus;
383         bool all_disabled = true;
384         int menu = iteminfo->pimpl_->
385                 create_submenu(FL_ObjWin(ob), view, tomenu,
386                                submenus, all_disabled);
387         if (menu != -1) {
388                 // place popup
389                 fl_setpup_position(view->getForm()->x + ob->x,
390                                    view->getForm()->y + ob->y + ob->h + 10);
391                 int choice = fl_dopup(menu);
392
393                 if (button == 1) {
394                                 // set the pseudo menu-button back
395                         fl_set_object_boxtype(ob, FL_FLAT_BOX);
396                         fl_redraw_object(ob);
397                 }
398
399                 // If the action value is too low, then it is not a
400                 // valid action, but something else.
401                 if (choice >= action_offset + 1) {
402                         view->getLyXFunc()->dispatch(choice - action_offset, true);
403                 } else {
404                         lyxerr[Debug::GUI]
405                                 << "MenuCallback: ignoring bogus action "
406                                 << choice << endl;
407                 }
408         } else {
409                 lyxerr << "Error in MenuCallback" << endl;
410         }
411
412         for_each(submenus.begin(), submenus.end(), fl_freepup);
413         // restore tabstop length
414         fl_set_tabstop(default_tabstop);
415
416 }