]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/XFormsMenubar.C
Change Assert to BOOST_ASSERT.
[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/lstrings.h"
24 #include "support/tostr.h"
25
26 #include "lyx_forms.h"
27
28 using namespace lyx::support;
29
30 using std::endl;
31 using std::for_each;
32 using std::vector;
33
34
35 typedef vector<int>::size_type size_type;
36
37 namespace {
38
39 // Some constants
40 int const MENU_LABEL_SIZE = FL_NORMAL_SIZE;
41 int const MENU_LABEL_STYLE = FL_NORMAL_STYLE;
42 int const mheight = 30;
43 int const mbheight= 22;
44 // where to place the menubar?
45 int const yloc = (mheight - mbheight)/2; //air + bw;
46 int const mbadd = 20; // menu button add (to width)
47 // Some space between buttons on the menubar
48 int const air = 2;
49 char const * menu_tabstop = "aa";
50 char const * default_tabstop = "aaaaaaaa";
51 // We do not want to mix position values in a menu (like the index of
52 // a submenu) with the action numbers which convey actual information.
53 // Therefore we offset all the action values by an arbitrary large
54 // constant.
55 int const action_offset = 1000;
56
57 // This is used a few times below.
58 inline
59 int string_width(string const & str)
60 {
61         return fl_get_string_widthTAB(MENU_LABEL_STYLE, MENU_LABEL_SIZE,
62                                       str.c_str(),
63                                       static_cast<int>(str.length()));
64 }
65
66 } // namespace anon
67
68
69 extern "C" {
70
71         //Defined later, used in makeMenubar().
72         static
73         void C_XFormsMenubar_MenuCallback(FL_OBJECT * ob, long button)
74         {
75                 XFormsMenubar::MenuCallback(ob, button);
76         }
77
78 }
79
80
81 XFormsMenubar::XFormsMenubar(LyXView * view, MenuBackend const & mb)
82         : owner_(static_cast<XFormsView*>(view)), menubackend_(&mb)
83 {
84         makeMenubar(menubackend_->getMenubar());
85 }
86
87
88 XFormsMenubar::~XFormsMenubar()
89 {}
90
91
92 void XFormsMenubar::makeMenubar(Menu const & menu)
93 {
94         FL_FORM * form = owner_->getForm();
95         int moffset = 0;
96
97         // Create menu frame if there is non yet.
98         FL_OBJECT * frame = fl_add_frame(FL_UP_FRAME, 0, 0,
99                                          form->w, mheight, "");
100         fl_set_object_resize(frame, FL_RESIZE_ALL);
101         fl_set_object_gravity(frame, NorthWestGravity,
102                               NorthEastGravity);
103
104         Menu::const_iterator i = menu.begin();
105         Menu::const_iterator end = menu.end();
106         for (; i != end; ++i) {
107                 FL_OBJECT * obj;
108                 if (i->kind() != MenuItem::Submenu) {
109                         lyxerr << "ERROR: XFormsMenubar::createMenubar:"
110                                 " only submenus can appear in a menubar"
111                                << endl;
112                         continue;
113                 }
114                 string const label = i->label();
115                 string const shortcut = '#' + i->shortcut();
116                 int const width = string_width(label);
117                 obj = fl_add_button(FL_MENU_BUTTON,
118                                     air + moffset, yloc,
119                                     width + mbadd,
120                                     mbheight,
121                                     label.c_str());
122                 fl_set_object_boxtype(obj, FL_FLAT_BOX);
123                 fl_set_object_color(obj, FL_MCOL, FL_MCOL);
124                 fl_set_object_lsize(obj, MENU_LABEL_SIZE);
125                 fl_set_object_lstyle(obj, MENU_LABEL_STYLE);
126                 fl_set_object_resize(obj, FL_RESIZE_ALL);
127                 fl_set_object_gravity(obj, NorthWestGravity,
128                                       NorthWestGravity);
129                 moffset += obj->w + air;
130                 fl_set_object_shortcut(obj, shortcut.c_str(), 1);
131                 fl_set_object_callback(obj, C_XFormsMenubar_MenuCallback, 1);
132
133                 boost::shared_ptr<ItemInfo>
134                         iteminfo(new ItemInfo(this, new MenuItem(*i), obj));
135                 buttonlist_.push_back(iteminfo);
136                 obj->u_vdata = iteminfo.get();
137         }
138
139 }
140
141
142 void XFormsMenubar::update()
143 {
144         // nothing yet
145 }
146
147
148 void XFormsMenubar::openByName(string const & name)
149 {
150         for (ButtonList::const_iterator cit = buttonlist_.begin();
151              cit != buttonlist_.end(); ++cit) {
152                 if ((*cit)->item_->submenuname() == name) {
153                         MenuCallback((*cit)->obj_, 1);
154                         return;
155                 }
156         }
157
158         lyxerr << "XFormsMenubar::openByName: menu "
159                << name << " not found" << endl;
160 }
161
162
163 namespace {
164
165 Menu::size_type const max_number_of_items = 25;
166
167 int get_new_submenu(vector<int> & smn, Window win)
168 {
169         static size_type max_number_of_menus = 32;
170         if (smn.size() >= max_number_of_menus)
171                 max_number_of_menus =
172                     fl_setpup_maxpup(static_cast<int>(2*smn.size()));
173         int menu = fl_newpup(win);
174         fl_setpup_softedge(menu, true);
175         fl_setpup_bw(menu, -1);
176         smn.push_back(menu);
177         return menu;
178 }
179
180
181 string const fixlabel(string const & str)
182 {
183         return subst(str, "%", "%%");
184 }
185
186
187
188 } // namespace anon
189
190
191
192 int XFormsMenubar::create_submenu(Window win, XFormsView * view,
193                                    Menu const & menu, vector<int> & smn)
194 {
195         const int menuid = get_new_submenu(smn, win);
196         lyxerr[Debug::GUI] << "XFormsMenubar::create_submenu: creating "
197                            << menu.name() << " as menuid=" << menuid << endl;
198
199         // Compute the size of the largest label (because xforms is
200         // not able to support shortcuts correctly...)
201         int max_width = 0;
202         string widest_label;
203         Menu::const_iterator end = menu.end();
204         for (Menu::const_iterator i = menu.begin(); i != end; ++i) {
205                 MenuItem const & item = (*i);
206                 if (item.kind() == MenuItem::Command) {
207                         string const label = item.label() + '\t';
208                         int const width = string_width(label);
209                         if (width > max_width) {
210                                 max_width = width;
211                                 widest_label = label;
212                         }
213                 }
214         }
215         lyxerr[Debug::GUI] << "max_width=" << max_width
216                            << ", widest_label=\"" << widest_label
217                            << '"' << endl;
218
219         size_type count = 0;
220         int curmenuid = menuid;
221         for (Menu::const_iterator i = menu.begin(); i != end; ++i) {
222                 MenuItem const & item = (*i);
223
224                 ++count;
225                 // add a More... submenu if the menu is too long (but
226                 // not just for one extra entry!)
227                 if (count > max_number_of_items && (i+1) != end) {
228                         int tmpmenuid = get_new_submenu(smn, win);
229                         lyxerr[Debug::GUI] << "Too many items, creating "
230                                            << "new menu " << tmpmenuid << endl;
231                         string label = _("More");
232                         label += "...%m";
233                         fl_addtopup(curmenuid, label.c_str(), tmpmenuid);
234                         count = 0;
235                         curmenuid = tmpmenuid;
236                 }
237
238                 switch (item.kind()) {
239                 case MenuItem::Command:
240                 case MenuItem::Submenu:
241                 {
242                         // Build the menu label from all the info
243                         string label = fixlabel(item.label());
244                         FuncStatus const flag = item.status();
245                         int submenuid = 0;
246
247                         // Is there a key binding?
248                         string const binding = item.binding();
249                         if (!binding.empty()) {
250                                 // Try to be clever and add  just enough
251                                 // tabs to align shortcuts.
252                                 do
253                                         label += '\t';
254                                 while (string_width(label) < max_width + 5);
255                                 label += binding;
256                         }
257
258                         // Is there a separator after the item?
259                         if ((i+1) != end
260                             && (i + 1)->kind() == MenuItem::Separator)
261                                 label += "%l";
262
263                         // Modify the entry using the function status
264                         if (flag.onoff(true))
265                                 label += "%B";
266                         if (flag.onoff(false))
267                                 label += "%b";
268                         if (flag.disabled())
269                                 label += "%i";
270
271                         // Add the shortcut
272                         string shortcut = item.shortcut();
273                         if (!shortcut.empty()) {
274                                 shortcut += lowercase(shortcut[0]);
275                                 label += "%h";
276                         }
277
278                         // Finally add the action/submenu
279                         if (item.kind() == MenuItem::Submenu) {
280                                 // create the submenu
281                                 submenuid =
282                                         create_submenu(win, view,
283                                                        *item.submenu(), smn);
284                                 if (submenuid == -1)
285                                         return -1;
286                                 label += "%x" + tostr(smn.size());
287                                 lyxerr[Debug::GUI]
288                                         << "Menu: " << submenuid
289                                         << " (at index " << smn.size()
290                                         << "), ";
291                         } else {
292                                 // Add the action
293                                 label += "%x" + tostr(item.action()
294                                                       + action_offset);
295                                 lyxerr[Debug::GUI] << "Action: \""
296                                                    << item.action() << "\", ";
297                         }
298
299                         // Add everything to the menu
300                         fl_addtopup(curmenuid, label.c_str(),
301                                     shortcut.c_str());
302                         if (item.kind() == MenuItem::Submenu)
303                                 fl_setpup_submenu(curmenuid, smn.size(),
304                                                   submenuid);
305
306                         lyxerr[Debug::GUI] << "label \"" << label
307                                            << "\", binding \"" << binding
308                                            << "\", shortcut \"" << shortcut
309                                            << "\" (added to menu "
310                                            << curmenuid << ')' << endl;
311                         break;
312                 }
313
314                 case MenuItem::Separator:
315                         // already done, and if it was the first one,
316                         // we just ignore it.
317                         --count;
318                         break;
319
320
321                 default:
322                         lyxerr << "XFormsMenubar::create_submenu: "
323                                 "this should not happen" << endl;
324                         break;
325                 }
326         }
327         return menuid;
328 }
329
330
331 void XFormsMenubar::MenuCallback(FL_OBJECT * ob, long button)
332 {
333         ItemInfo * iteminfo = static_cast<ItemInfo *>(ob->u_vdata);
334         XFormsView * view = iteminfo->menubar_->owner_;
335         MenuItem const * item = iteminfo->item_.get();
336
337         if (button == 1) {
338                 // set the pseudo menu-button
339                 fl_set_object_boxtype(ob, FL_DOWN_BOX);
340                 fl_set_button(ob, 0);
341                 fl_redraw_object(ob);
342         }
343
344         // Paranoia check
345         BOOST_ASSERT(item->kind() == MenuItem::Submenu);
346
347         // set tabstop length
348         fl_set_tabstop(menu_tabstop);
349
350         MenuBackend const * menubackend_ = iteminfo->menubar_->menubackend_;
351         Menu tomenu;
352         Menu const frommenu = menubackend_->getMenu(item->submenuname());
353         menubackend_->expand(frommenu, tomenu, view);
354         vector<int> submenus;
355         int menu = iteminfo->menubar_->create_submenu(FL_ObjWin(ob), view,
356                                                     tomenu, submenus);
357         if (menu != -1) {
358                 // place popup
359                 fl_setpup_position(view->getForm()->x + ob->x,
360                                    view->getForm()->y + ob->y + ob->h + 10);
361                 int choice = fl_dopup(menu);
362
363                 if (button == 1) {
364                                 // set the pseudo menu-button back
365                         fl_set_object_boxtype(ob, FL_FLAT_BOX);
366                         fl_redraw_object(ob);
367                 }
368
369                 // If the action value is too low, then it is not a
370                 // valid action, but something else.
371                 if (choice >= action_offset + 1) {
372                         view->getLyXFunc().dispatch(choice - action_offset, true);
373                 } else {
374                         lyxerr[Debug::GUI]
375                                 << "MenuCallback: ignoring bogus action "
376                                 << choice << endl;
377                 }
378         } else {
379                 lyxerr << "Error in MenuCallback" << endl;
380         }
381
382         for_each(submenus.begin(), submenus.end(), fl_freepup);
383         // restore tabstop length
384         fl_set_tabstop(default_tabstop);
385
386 }
387
388
389 XFormsMenubar::ItemInfo::ItemInfo
390         (XFormsMenubar * p, MenuItem const * i, FL_OBJECT * o)
391         : menubar_(p), obj_(o)
392 {
393         item_.reset(i);
394 }
395
396
397 XFormsMenubar::ItemInfo::~ItemInfo()
398 {}