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