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