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.
6 * \author Lars Gullik Bjønnes
8 * Full author contact details are available in file CREDITS
14 #include "Menubar_pimpl.h"
15 #include "MenuBackend.h"
16 #include "LyXAction.h"
19 #include "XFormsView.h"
21 #include "support/lstrings.h"
22 #include "support/LAssert.h"
25 #include FORMS_H_LOCATION
27 #include <boost/scoped_ptr.hpp>
37 typedef vector<int>::size_type size_type;
39 extern boost::scoped_ptr<kb_keymap> toplevel_keymap;
44 int const MENU_LABEL_SIZE = FL_NORMAL_SIZE;
45 int const MENU_LABEL_STYLE = FL_NORMAL_STYLE;
46 int const mheight = 30;
47 int const mbheight= 22;
48 // where to place the menubar?
49 int const yloc = (mheight - mbheight)/2; //air + bw;
50 int const mbadd = 20; // menu button add (to width)
51 // Some space between buttons on the menubar
53 char const * menu_tabstop = "aa";
54 char const * default_tabstop = "aaaaaaaa";
55 // We do not want to mix position values in a menu (like the index of
56 // a submenu) with the action numbers which convey actual information.
57 // Therefore we offset all the action values by an arbitrary large
59 int const action_offset = 1000;
61 // This is used a few times below.
63 int string_width(string const & str)
65 return fl_get_string_widthTAB(MENU_LABEL_STYLE, MENU_LABEL_SIZE,
67 static_cast<int>(str.length()));
75 //Defined later, used in makeMenubar().
77 void C_Menubar_Pimpl_MenuCallback(FL_OBJECT * ob, long button)
79 Menubar::Pimpl::MenuCallback(ob, button);
85 Menubar::Pimpl::Pimpl(LyXView * view, MenuBackend const & mb)
86 : owner_(static_cast<XFormsView*>(view)), menubackend_(&mb)
88 makeMenubar(menubackend_->getMenubar());
92 Menubar::Pimpl::~Pimpl()
96 void Menubar::Pimpl::makeMenubar(Menu const & menu)
98 FL_FORM * form = owner_->getForm();
101 // Create menu frame if there is non yet.
102 FL_OBJECT * frame = fl_add_frame(FL_UP_FRAME, 0, 0,
103 form->w, mheight, "");
104 fl_set_object_resize(frame, FL_RESIZE_ALL);
105 fl_set_object_gravity(frame, NorthWestGravity,
108 Menu::const_iterator i = menu.begin();
109 Menu::const_iterator end = menu.end();
110 for (; i != end; ++i) {
112 if (i->kind() != MenuItem::Submenu) {
113 lyxerr << "ERROR: Menubar::Pimpl::createMenubar:"
114 " only submenus can appear in a menubar"
118 string const label = i->label();
119 string const shortcut = '#' + i->shortcut();
120 int const width = string_width(label);
121 obj = fl_add_button(FL_MENU_BUTTON,
126 fl_set_object_boxtype(obj, FL_FLAT_BOX);
127 fl_set_object_color(obj, FL_MCOL, FL_MCOL);
128 fl_set_object_lsize(obj, MENU_LABEL_SIZE);
129 fl_set_object_lstyle(obj, MENU_LABEL_STYLE);
130 fl_set_object_resize(obj, FL_RESIZE_ALL);
131 fl_set_object_gravity(obj, NorthWestGravity,
133 moffset += obj->w + air;
134 fl_set_object_shortcut(obj, shortcut.c_str(), 1);
135 fl_set_object_callback(obj, C_Menubar_Pimpl_MenuCallback, 1);
137 boost::shared_ptr<ItemInfo>
138 iteminfo(new ItemInfo(this, new MenuItem(*i), obj));
139 buttonlist_.push_back(iteminfo);
140 obj->u_vdata = iteminfo.get();
146 void Menubar::Pimpl::update()
152 void Menubar::Pimpl::openByName(string const & name)
154 for (ButtonList::const_iterator cit = buttonlist_.begin();
155 cit != buttonlist_.end(); ++cit) {
156 if ((*cit)->item_->submenuname() == name) {
157 MenuCallback((*cit)->obj_, 1);
162 lyxerr << "Menubar::Pimpl::openByName: menu "
163 << name << " not found" << endl;
169 Menu::size_type const max_number_of_items = 25;
171 int get_new_submenu(vector<int> & smn, Window win)
173 static size_type max_number_of_menus = 32;
174 if (smn.size() >= max_number_of_menus)
175 max_number_of_menus =
176 fl_setpup_maxpup(static_cast<int>(2*smn.size()));
177 int menu = fl_newpup(win);
178 fl_setpup_softedge(menu, true);
179 fl_setpup_bw(menu, -1);
180 lyxerr[Debug::GUI] << "Adding menu " << menu
181 << " in deletion list" << endl;
187 string const fixlabel(string const & str)
189 return subst(str, "%", "%%");
198 int Menubar::Pimpl::create_submenu(Window win, XFormsView * view,
199 Menu const & menu, vector<int> & smn,
202 const int menuid = get_new_submenu(smn, win);
203 lyxerr[Debug::GUI] << "Menubar::Pimpl::create_submenu: creating "
204 << menu.name() << " as menuid=" << menuid << endl;
206 // Compute the size of the largest label (because xforms is
207 // not able to support shortcuts correctly...)
210 Menu::const_iterator end = menu.end();
211 for (Menu::const_iterator i = menu.begin(); i != end; ++i) {
212 MenuItem const & item = (*i);
213 if (item.kind() == MenuItem::Command) {
214 string const label = item.label() + '\t';
215 int const width = string_width(label);
216 if (width > max_width) {
218 widest_label = label;
222 lyxerr[Debug::GUI] << "max_width=" << max_width
223 << ", widest_label=`" << widest_label
226 // Compute where to put separators
227 vector<string> extra_labels(menu.size());
228 vector<string>::iterator it = extra_labels.begin();
229 vector<string>::iterator last = it;
230 for (Menu::const_iterator i = menu.begin(); i != end; ++i, ++it) {
231 if (i->kind() == MenuItem::Separator)
233 else if (i->kind() == MenuItem::Command) {
235 = view->getLyXFunc().getStatus(i->action());
236 if (!(i->optional() && flag.disabled())
242 it = extra_labels.begin();
245 int curmenuid = menuid;
246 for (Menu::const_iterator i = menu.begin(); i != end; ++i, ++it) {
247 MenuItem const & item = (*i);
248 string & extra_label = *it;
251 // add a More... submenu if the menu is too long (but
252 // not just for one extra entry!)
253 if (count > max_number_of_items && (i+1) != end) {
254 int tmpmenuid = get_new_submenu(smn, win);
255 lyxerr[Debug::GUI] << "Too many items, creating "
256 << "new menu " << tmpmenuid << endl;
257 string label = _("More");
259 fl_addtopup(curmenuid, label.c_str(), tmpmenuid);
261 curmenuid = tmpmenuid;
264 switch (item.kind()) {
265 case MenuItem::Command: {
266 FuncStatus const flag =
267 view->getLyXFunc().getStatus(item.action());
268 // handle optional or unknown entries.
270 || (item.optional() && flag.disabled())) {
273 << item.label() << endl;
277 // Get the keys bound to this action, but keep only the
280 toplevel_keymap->findbinding(item.action());
281 // Build the menu label from all the info
282 string label = fixlabel(item.label());
284 if (!accel.empty()) {
285 // Try to be clever and add just enough
286 // tabs to align shortcuts.
289 while (string_width(label) < max_width + 5);
290 label += accel.substr(1,accel.find(']') - 1);
292 label += "%x" + tostr(item.action() + action_offset)
295 // Modify the entry using the function status
297 if (flag.onoff(true))
299 if (flag.onoff(false))
304 all_disabled = false;
307 // Finally the menu shortcut
308 string shortcut = item.shortcut();
310 if (!shortcut.empty()) {
311 shortcut += lowercase(shortcut[0]);
313 fl_addtopup(curmenuid, label.c_str(),
316 fl_addtopup(curmenuid, label.c_str());
318 lyxerr[Debug::GUI] << "Command: \""
319 << lyxaction.getActionName(item.action())
320 << "\", binding \"" << accel
321 << "\", shortcut \"" << shortcut
322 << "\" (added to menu"
323 << curmenuid << ')' << endl;
327 case MenuItem::Submenu: {
328 bool sub_all_disabled;
329 int submenuid = create_submenu(win, view,
330 *item.submenu(), smn,
332 all_disabled &= sub_all_disabled;
335 string label = fixlabel(item.label());
336 label += extra_label + "%m";
337 if (sub_all_disabled)
339 string shortcut = item.shortcut();
340 if (!shortcut.empty()) {
341 shortcut += lowercase(shortcut[0]);
343 fl_addtopup(curmenuid, label.c_str(),
344 submenuid, shortcut.c_str());
346 fl_addtopup(curmenuid, label.c_str(),
352 case MenuItem::Separator:
353 // already done, and if it was the first one,
354 // we just ignore it.
359 lyxerr << "Menubar::Pimpl::create_submenu: "
360 "this should not happen" << endl;
368 void Menubar::Pimpl::MenuCallback(FL_OBJECT * ob, long button)
370 ItemInfo * iteminfo = static_cast<ItemInfo *>(ob->u_vdata);
371 XFormsView * view = iteminfo->pimpl_->owner_;
372 MenuItem const * item = iteminfo->item_.get();
375 // set the pseudo menu-button
376 fl_set_object_boxtype(ob, FL_DOWN_BOX);
377 fl_set_button(ob, 0);
378 fl_redraw_object(ob);
382 lyx::Assert(item->kind() == MenuItem::Submenu);
384 // set tabstop length
385 fl_set_tabstop(menu_tabstop);
387 MenuBackend const * menubackend_ = iteminfo->pimpl_->menubackend_;
389 Menu const frommenu = menubackend_->getMenu(item->submenuname());
390 menubackend_->expand(frommenu, tomenu, view->buffer());
391 vector<int> submenus;
392 bool all_disabled = true;
393 int menu = iteminfo->pimpl_->
394 create_submenu(FL_ObjWin(ob), view, tomenu,
395 submenus, all_disabled);
398 fl_setpup_position(view->getForm()->x + ob->x,
399 view->getForm()->y + ob->y + ob->h + 10);
400 int choice = fl_dopup(menu);
403 // set the pseudo menu-button back
404 fl_set_object_boxtype(ob, FL_FLAT_BOX);
405 fl_redraw_object(ob);
408 // If the action value is too low, then it is not a
409 // valid action, but something else.
410 if (choice >= action_offset + 1) {
411 view->getLyXFunc().dispatch(choice - action_offset, true);
414 << "MenuCallback: ignoring bogus action "
418 lyxerr << "Error in MenuCallback" << endl;
421 for_each(submenus.begin(), submenus.end(), fl_freepup);
422 // restore tabstop length
423 fl_set_tabstop(default_tabstop);
428 Menubar::Pimpl::ItemInfo::ItemInfo
429 (Menubar::Pimpl * p, MenuItem const * i, FL_OBJECT * o)
436 Menubar::Pimpl::ItemInfo::~ItemInfo()