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