2 * \file Menubar_pimpl.C
3 * See the file COPYING.
5 * \author Lars Gullik Bjønnes
7 * Full author contact details are available in file CREDITS
13 #pragma implementation
16 #include "Menubar_pimpl.h"
17 #include "MenuBackend.h"
18 #include "LyXAction.h"
21 #include "XFormsView.h"
23 #include "support/lstrings.h"
24 #include "support/LAssert.h"
27 #include FORMS_H_LOCATION
29 #include <boost/scoped_ptr.hpp>
39 typedef vector<int>::size_type size_type;
41 extern boost::scoped_ptr<kb_keymap> toplevel_keymap;
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
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
61 int const action_offset = 1000;
63 // This is used a few times below.
65 int string_width(string const & str)
67 return fl_get_string_widthTAB(MENU_LABEL_STYLE, MENU_LABEL_SIZE,
69 static_cast<int>(str.length()));
77 //Defined later, used in makeMenubar().
79 void C_Menubar_Pimpl_MenuCallback(FL_OBJECT * ob, long button)
81 Menubar::Pimpl::MenuCallback(ob, button);
87 Menubar::Pimpl::Pimpl(LyXView * view, MenuBackend const & mb)
88 : owner_(static_cast<XFormsView*>(view)), menubackend_(&mb)
90 makeMenubar(menubackend_->getMenubar());
94 Menubar::Pimpl::~Pimpl()
98 void Menubar::Pimpl::makeMenubar(Menu const & menu)
100 FL_FORM * form = owner_->getForm();
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,
110 Menu::const_iterator i = menu.begin();
111 Menu::const_iterator end = menu.end();
112 for (; i != end; ++i) {
114 if (i->kind() != MenuItem::Submenu) {
115 lyxerr << "ERROR: Menubar::Pimpl::createMenubar:"
116 " only submenus can appear in a menubar"
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,
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,
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);
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();
148 void Menubar::Pimpl::update()
154 void Menubar::Pimpl::openByName(string const & name)
156 for (ButtonList::const_iterator cit = buttonlist_.begin();
157 cit != buttonlist_.end(); ++cit) {
158 if ((*cit)->item_->submenuname() == name) {
159 MenuCallback((*cit)->obj_, 1);
164 lyxerr << "Menubar::Pimpl::openByName: menu "
165 << name << " not found" << endl;
171 Menu::size_type const max_number_of_items = 25;
173 int get_new_submenu(vector<int> & smn, Window win)
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;
189 string const fixlabel(string const & str)
191 #if FL_VERSION < 1 && FL_REVISION < 89
192 return subst(str, '%', '?');
194 return subst(str, "%", "%%");
204 int Menubar::Pimpl::create_submenu(Window win, XFormsView * view,
205 Menu const & menu, vector<int> & smn,
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;
212 // Compute the size of the largest label (because xforms is
213 // not able to support shortcuts correctly...)
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) {
224 widest_label = label;
228 lyxerr[Debug::GUI] << "max_width=" << max_width
229 << ", widest_label=`" << widest_label
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)
239 else if (!i->optional() ||
240 !(view->getLyXFunc().getStatus(i->action()).disabled()))
243 it = extra_labels.begin();
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;
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");
258 fl_addtopup(curmenuid, label.c_str(), tmpmenuid);
260 curmenuid = tmpmenuid;
263 switch (item.kind()) {
264 case MenuItem::Command: {
265 FuncStatus const flag =
266 view->getLyXFunc().getStatus(item.action());
267 // handle optional entries.
269 && (flag.disabled())) {
271 << "Skipping optional item "
272 << item.label() << endl;
276 // Get the keys bound to this action, but keep only the
279 toplevel_keymap->findbinding(item.action());
280 // Build the menu label from all the info
281 string label = fixlabel(item.label());
283 if (!accel.empty()) {
284 // Try to be clever and add just enough
285 // tabs to align shortcuts.
288 while (string_width(label) < max_width + 5);
289 label += accel.substr(1,accel.find(']') - 1);
291 label += "%x" + tostr(item.action() + action_offset)
294 // Modify the entry using the function status
296 if (flag.onoff(true))
298 if (flag.onoff(false))
300 if (flag.disabled() || flag.unknown())
303 all_disabled = false;
306 // Finally the menu shortcut
307 string shortcut = item.shortcut();
309 if (!shortcut.empty()) {
310 shortcut += lowercase(shortcut[0]);
312 fl_addtopup(curmenuid, label.c_str(),
315 fl_addtopup(curmenuid, label.c_str());
317 lyxerr[Debug::GUI] << "Command: \""
318 << lyxaction.getActionName(item.action())
319 << "\", binding \"" << accel
320 << "\", shortcut \"" << shortcut
321 << "\" (added to menu"
322 << curmenuid << ")" << endl;
326 case MenuItem::Submenu: {
327 bool sub_all_disabled;
328 int submenuid = create_submenu(win, view,
329 *item.submenu(), smn,
331 all_disabled &= sub_all_disabled;
334 string label = fixlabel(item.label());
335 label += extra_label + "%m";
336 if (sub_all_disabled)
338 string shortcut = item.shortcut();
339 if (!shortcut.empty()) {
340 shortcut += lowercase(shortcut[0]);
342 fl_addtopup(curmenuid, label.c_str(),
343 submenuid, shortcut.c_str());
345 fl_addtopup(curmenuid, label.c_str(),
351 case MenuItem::Separator:
352 // already done, and if it was the first one,
353 // we just ignore it.
358 lyxerr << "Menubar::Pimpl::create_submenu: "
359 "this should not happen" << endl;
367 void Menubar::Pimpl::MenuCallback(FL_OBJECT * ob, long button)
369 ItemInfo * iteminfo = static_cast<ItemInfo *>(ob->u_vdata);
370 XFormsView * view = iteminfo->pimpl_->owner_;
371 MenuItem const * item = iteminfo->item_.get();
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);
381 lyx::Assert(item->kind() == MenuItem::Submenu);
383 // set tabstop length
384 fl_set_tabstop(menu_tabstop);
386 MenuBackend const * menubackend_ = iteminfo->pimpl_->menubackend_;
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);
397 fl_setpup_position(view->getForm()->x + ob->x,
398 view->getForm()->y + ob->y + ob->h + 10);
399 int choice = fl_dopup(menu);
402 // set the pseudo menu-button back
403 fl_set_object_boxtype(ob, FL_FLAT_BOX);
404 fl_redraw_object(ob);
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);
413 << "MenuCallback: ignoring bogus action "
417 lyxerr << "Error in MenuCallback" << endl;
420 for_each(submenus.begin(), submenus.end(), fl_freepup);
421 // restore tabstop length
422 fl_set_tabstop(default_tabstop);
427 Menubar::Pimpl::ItemInfo::ItemInfo
428 (Menubar::Pimpl * p, MenuItem const * i, FL_OBJECT * o)
435 Menubar::Pimpl::ItemInfo::~ItemInfo()