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