]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/XFormsMenubar.C
6d420f14869ad4f0795e902ed5ac4908558e1e96
[lyx.git] / src / frontends / xforms / XFormsMenubar.C
1 /**
2  * \file XFormsMenubar.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author  Lars Gullik Bjønnes
7  * \author Jean-Marc Lasgouttes
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "XFormsMenubar.h"
15
16 #include "XFormsView.h"
17
18 #include "debug.h"
19 #include "gettext.h"
20 #include "lyxfunc.h"
21 #include "MenuBackend.h"
22
23 #include "support/LAssert.h"
24 #include "support/lstrings.h"
25 #include "support/tostr.h"
26
27 #include "lyx_forms.h"
28
29 using namespace lyx::support;
30
31 using std::endl;
32 using std::for_each;
33 using std::vector;
34
35
36 typedef vector<int>::size_type size_type;
37
38 namespace {
39
40 // Some constants
41 int const MENU_LABEL_SIZE = FL_NORMAL_SIZE;
42 int const MENU_LABEL_STYLE = FL_NORMAL_STYLE;
43 int const mheight = 30;
44 int const mbheight= 22;
45 // where to place the menubar?
46 int const yloc = (mheight - mbheight)/2; //air + bw;
47 int const mbadd = 20; // menu button add (to width)
48 // Some space between buttons on the menubar
49 int const air = 2;
50 char const * menu_tabstop = "aa";
51 char const * default_tabstop = "aaaaaaaa";
52 // We do not want to mix position values in a menu (like the index of
53 // a submenu) with the action numbers which convey actual information.
54 // Therefore we offset all the action values by an arbitrary large
55 // constant.
56 int const action_offset = 1000;
57
58 // This is used a few times below.
59 inline
60 int string_width(string const & str)
61 {
62         return fl_get_string_widthTAB(MENU_LABEL_STYLE, MENU_LABEL_SIZE,
63                                       str.c_str(),
64                                       static_cast<int>(str.length()));
65 }
66
67 } // namespace anon
68
69
70 extern "C" {
71
72         //Defined later, used in makeMenubar().
73         static
74         void C_XFormsMenubar_MenuCallback(FL_OBJECT * ob, long button)
75         {
76                 XFormsMenubar::MenuCallback(ob, button);
77         }
78
79 }
80
81
82 XFormsMenubar::XFormsMenubar(LyXView * view, MenuBackend const & mb)
83         : owner_(static_cast<XFormsView*>(view)), menubackend_(&mb)
84 {
85         makeMenubar(menubackend_->getMenubar());
86 }
87
88
89 XFormsMenubar::~XFormsMenubar()
90 {}
91
92
93 void XFormsMenubar::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: XFormsMenubar::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_XFormsMenubar_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 XFormsMenubar::update()
144 {
145         // nothing yet
146 }
147
148
149 void XFormsMenubar::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 << "XFormsMenubar::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         smn.push_back(menu);
178         return menu;
179 }
180
181
182 string const fixlabel(string const & str)
183 {
184         return subst(str, "%", "%%");
185 }
186
187
188
189 } // namespace anon
190
191
192
193 int XFormsMenubar::create_submenu(Window win, XFormsView * view,
194                                    Menu const & menu, vector<int> & smn)
195 {
196         const int menuid = get_new_submenu(smn, win);
197         lyxerr[Debug::GUI] << "XFormsMenubar::create_submenu: creating "
198                            << menu.name() << " as menuid=" << menuid << endl;
199
200         // Compute the size of the largest label (because xforms is
201         // not able to support shortcuts correctly...)
202         int max_width = 0;
203         string widest_label;
204         Menu::const_iterator end = menu.end();
205         for (Menu::const_iterator i = menu.begin(); i != end; ++i) {
206                 MenuItem const & item = (*i);
207                 if (item.kind() == MenuItem::Command) {
208                         string const label = item.label() + '\t';
209                         int const width = string_width(label);
210                         if (width > max_width) {
211                                 max_width = width;
212                                 widest_label = label;
213                         }
214                 }
215         }
216         lyxerr[Debug::GUI] << "max_width=" << max_width
217                            << ", widest_label=\"" << widest_label
218                            << '"' << endl;
219
220         size_type count = 0;
221         int curmenuid = menuid;
222         for (Menu::const_iterator i = menu.begin(); i != end; ++i) {
223                 MenuItem const & item = (*i);
224
225                 ++count;
226                 // add a More... submenu if the menu is too long (but
227                 // not just for one extra entry!)
228                 if (count > max_number_of_items && (i+1) != end) {
229                         int tmpmenuid = get_new_submenu(smn, win);
230                         lyxerr[Debug::GUI] << "Too many items, creating "
231                                            << "new menu " << tmpmenuid << endl;
232                         string label = _("More");
233                         label += "...%m";
234                         fl_addtopup(curmenuid, label.c_str(), tmpmenuid);
235                         count = 0;
236                         curmenuid = tmpmenuid;
237                 }
238
239                 switch (item.kind()) {
240                 case MenuItem::Command:
241                 case MenuItem::Submenu:
242                 {
243                         // Build the menu label from all the info
244                         string label = fixlabel(item.label());
245                         FuncStatus const flag = item.status();
246                         int submenuid = 0;
247
248                         // Is there a key binding?
249                         string const binding = item.binding();
250                         if (!binding.empty()) {
251                                 // Try to be clever and add  just enough
252                                 // tabs to align shortcuts.
253                                 do
254                                         label += '\t';
255                                 while (string_width(label) < max_width + 5);
256                                 label += binding;
257                         }
258
259                         // Is there a separator after the item?
260                         if ((i+1) != end
261                             && (i + 1)->kind() == MenuItem::Separator)
262                                 label += "%l";
263
264                         // Modify the entry using the function status
265                         if (flag.onoff(true))
266                                 label += "%B";
267                         if (flag.onoff(false))
268                                 label += "%b";
269                         if (flag.disabled())
270                                 label += "%i";
271
272                         // Add the shortcut
273                         string shortcut = item.shortcut();
274                         if (!shortcut.empty()) {
275                                 shortcut += lowercase(shortcut[0]);
276                                 label += "%h";
277                         }
278
279                         // Finally add the action/submenu
280                         if (item.kind() == MenuItem::Submenu) {
281                                 // create the submenu
282                                 submenuid =
283                                         create_submenu(win, view,
284                                                        *item.submenu(), smn);
285                                 if (submenuid == -1)
286                                         return -1;
287                                 label += "%x" + tostr(smn.size());
288                                 lyxerr[Debug::GUI]
289                                         << "Menu: " << submenuid
290                                         << " (at index " << smn.size()
291                                         << "), ";
292                         } else {
293                                 // Add the action
294                                 label += "%x" + tostr(item.action()
295                                                       + action_offset);
296                                 lyxerr[Debug::GUI] << "Action: \""
297                                                    << item.action() << "\", ";
298                         }
299
300                         // Add everything to the menu
301                         fl_addtopup(curmenuid, label.c_str(),
302                                     shortcut.c_str());
303                         if (item.kind() == MenuItem::Submenu)
304                                 fl_setpup_submenu(curmenuid, smn.size(),
305                                                   submenuid);
306
307                         lyxerr[Debug::GUI] << "label \"" << label
308                                            << "\", binding \"" << binding
309                                            << "\", shortcut \"" << shortcut
310                                            << "\" (added to menu "
311                                            << curmenuid << ')' << endl;
312                         break;
313                 }
314
315                 case MenuItem::Separator:
316                         // already done, and if it was the first one,
317                         // we just ignore it.
318                         --count;
319                         break;
320
321
322                 default:
323                         lyxerr << "XFormsMenubar::create_submenu: "
324                                 "this should not happen" << endl;
325                         break;
326                 }
327         }
328         return menuid;
329 }
330
331
332 void XFormsMenubar::MenuCallback(FL_OBJECT * ob, long button)
333 {
334         ItemInfo * iteminfo = static_cast<ItemInfo *>(ob->u_vdata);
335         XFormsView * view = iteminfo->menubar_->owner_;
336         MenuItem const * item = iteminfo->item_.get();
337
338         if (button == 1) {
339                 // set the pseudo menu-button
340                 fl_set_object_boxtype(ob, FL_DOWN_BOX);
341                 fl_set_button(ob, 0);
342                 fl_redraw_object(ob);
343         }
344
345         // Paranoia check
346         Assert(item->kind() == MenuItem::Submenu);
347
348         // set tabstop length
349         fl_set_tabstop(menu_tabstop);
350
351         MenuBackend const * menubackend_ = iteminfo->menubar_->menubackend_;
352         Menu tomenu;
353         Menu const frommenu = menubackend_->getMenu(item->submenuname());
354         menubackend_->expand(frommenu, tomenu, view);
355         vector<int> submenus;
356         int menu = iteminfo->menubar_->create_submenu(FL_ObjWin(ob), view,
357                                                     tomenu, submenus);
358         if (menu != -1) {
359                 // place popup
360                 fl_setpup_position(view->getForm()->x + ob->x,
361                                    view->getForm()->y + ob->y + ob->h + 10);
362                 int choice = fl_dopup(menu);
363
364                 if (button == 1) {
365                                 // set the pseudo menu-button back
366                         fl_set_object_boxtype(ob, FL_FLAT_BOX);
367                         fl_redraw_object(ob);
368                 }
369
370                 // If the action value is too low, then it is not a
371                 // valid action, but something else.
372                 if (choice >= action_offset + 1) {
373                         view->getLyXFunc().dispatch(choice - action_offset, true);
374                 } else {
375                         lyxerr[Debug::GUI]
376                                 << "MenuCallback: ignoring bogus action "
377                                 << choice << endl;
378                 }
379         } else {
380                 lyxerr << "Error in MenuCallback" << endl;
381         }
382
383         for_each(submenus.begin(), submenus.end(), fl_freepup);
384         // restore tabstop length
385         fl_set_tabstop(default_tabstop);
386
387 }
388
389
390 XFormsMenubar::ItemInfo::ItemInfo
391         (XFormsMenubar * p, MenuItem const * i, FL_OBJECT * o)
392         : menubar_(p), obj_(o)
393 {
394         item_.reset(i);
395 }
396
397
398 XFormsMenubar::ItemInfo::~ItemInfo()
399 {}