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 #pragma implementation
17 #include "Menubar_pimpl.h"
18 #include "MenuBackend.h"
19 #include "LyXAction.h"
22 #include "XFormsView.h"
24 #include "support/lstrings.h"
25 #include "support/LAssert.h"
28 #include FORMS_H_LOCATION
30 #include <boost/scoped_ptr.hpp>
40 typedef vector<int>::size_type size_type;
42 extern boost::scoped_ptr<kb_keymap> toplevel_keymap;
47 int const MENU_LABEL_SIZE = FL_NORMAL_SIZE;
48 int const MENU_LABEL_STYLE = FL_NORMAL_STYLE;
49 int const mheight = 30;
50 int const mbheight= 22;
51 // where to place the menubar?
52 int const yloc = (mheight - mbheight)/2; //air + bw;
53 int const mbadd = 20; // menu button add (to width)
54 // Some space between buttons on the menubar
56 char const * menu_tabstop = "aa";
57 char const * default_tabstop = "aaaaaaaa";
58 // We do not want to mix position values in a menu (like the index of
59 // a submenu) with the action numbers which convey actual information.
60 // Therefore we offset all the action values by an arbitrary large
62 int const action_offset = 1000;
64 // This is used a few times below.
66 int string_width(string const & str)
68 return fl_get_string_widthTAB(MENU_LABEL_STYLE, MENU_LABEL_SIZE,
70 static_cast<int>(str.length()));
78 //Defined later, used in makeMenubar().
80 void C_Menubar_Pimpl_MenuCallback(FL_OBJECT * ob, long button)
82 Menubar::Pimpl::MenuCallback(ob, button);
88 Menubar::Pimpl::Pimpl(LyXView * view, MenuBackend const & mb)
89 : owner_(static_cast<XFormsView*>(view)), menubackend_(&mb)
91 makeMenubar(menubackend_->getMenubar());
95 Menubar::Pimpl::~Pimpl()
99 void Menubar::Pimpl::makeMenubar(Menu const & menu)
101 FL_FORM * form = owner_->getForm();
104 // Create menu frame if there is non yet.
105 FL_OBJECT * frame = fl_add_frame(FL_UP_FRAME, 0, 0,
106 form->w, mheight, "");
107 fl_set_object_resize(frame, FL_RESIZE_ALL);
108 fl_set_object_gravity(frame, NorthWestGravity,
111 Menu::const_iterator i = menu.begin();
112 Menu::const_iterator end = menu.end();
113 for (; i != end; ++i) {
115 if (i->kind() != MenuItem::Submenu) {
116 lyxerr << "ERROR: Menubar::Pimpl::createMenubar:"
117 " only submenus can appear in a menubar"
121 string const label = i->label();
122 string const shortcut = "#" + i->shortcut();
123 int const width = string_width(label);
124 obj = fl_add_button(FL_MENU_BUTTON,
129 fl_set_object_boxtype(obj, FL_FLAT_BOX);
130 fl_set_object_color(obj, FL_MCOL, FL_MCOL);
131 fl_set_object_lsize(obj, MENU_LABEL_SIZE);
132 fl_set_object_lstyle(obj, MENU_LABEL_STYLE);
133 fl_set_object_resize(obj, FL_RESIZE_ALL);
134 fl_set_object_gravity(obj, NorthWestGravity,
136 moffset += obj->w + air;
137 fl_set_object_shortcut(obj, shortcut.c_str(), 1);
138 fl_set_object_callback(obj, C_Menubar_Pimpl_MenuCallback, 1);
140 boost::shared_ptr<ItemInfo>
141 iteminfo(new ItemInfo(this, new MenuItem(*i), obj));
142 buttonlist_.push_back(iteminfo);
143 obj->u_vdata = iteminfo.get();
149 void Menubar::Pimpl::update()
155 void Menubar::Pimpl::openByName(string const & name)
157 for (ButtonList::const_iterator cit = buttonlist_.begin();
158 cit != buttonlist_.end(); ++cit) {
159 if ((*cit)->item_->submenuname() == name) {
160 MenuCallback((*cit)->obj_, 1);
165 lyxerr << "Menubar::Pimpl::openByName: menu "
166 << name << " not found" << endl;
172 Menu::size_type const max_number_of_items = 25;
174 int get_new_submenu(vector<int> & smn, Window win)
176 static size_type max_number_of_menus = 32;
177 if (smn.size() >= max_number_of_menus)
178 max_number_of_menus =
179 fl_setpup_maxpup(static_cast<int>(2*smn.size()));
180 int menu = fl_newpup(win);
181 fl_setpup_softedge(menu, true);
182 fl_setpup_bw(menu, -1);
183 lyxerr[Debug::GUI] << "Adding menu " << menu
184 << " in deletion list" << endl;
190 string const fixlabel(string const & str)
192 #if FL_VERSION < 1 && FL_REVISION < 89
193 return subst(str, '%', '?');
195 return subst(str, "%", "%%");
205 int Menubar::Pimpl::create_submenu(Window win, XFormsView * view,
206 Menu const & menu, vector<int> & smn,
209 const int menuid = get_new_submenu(smn, win);
210 lyxerr[Debug::GUI] << "Menubar::Pimpl::create_submenu: creating "
211 << menu.name() << " as menuid=" << menuid << endl;
213 // Compute the size of the largest label (because xforms is
214 // not able to support shortcuts correctly...)
217 Menu::const_iterator end = menu.end();
218 for (Menu::const_iterator i = menu.begin(); i != end; ++i) {
219 MenuItem const & item = (*i);
220 if (item.kind() == MenuItem::Command) {
221 string const label = item.label() + '\t';
222 int const width = string_width(label);
223 if (width > max_width) {
225 widest_label = label;
229 lyxerr[Debug::GUI] << "max_width=" << max_width
230 << ", widest_label=`" << widest_label
233 // Compute where to put separators
234 vector<string> extra_labels(menu.size());
235 vector<string>::iterator it = extra_labels.begin();
236 vector<string>::iterator last = it;
237 for (Menu::const_iterator i = menu.begin(); i != end; ++i, ++it)
238 if (i->kind() == MenuItem::Separator)
240 else if (!i->optional() ||
241 !(view->getLyXFunc().getStatus(i->action()).disabled()))
244 it = extra_labels.begin();
247 int curmenuid = menuid;
248 for (Menu::const_iterator i = menu.begin(); i != end; ++i, ++it) {
249 MenuItem const & item = (*i);
250 string & extra_label = *it;
253 // add a More... submenu if the menu is too long (but
254 // not just for one extra entry!)
255 if (count > max_number_of_items && (i+1) != end) {
256 int tmpmenuid = get_new_submenu(smn, win);
257 lyxerr[Debug::GUI] << "Too many items, creating "
258 << "new menu " << tmpmenuid << endl;
259 string label = _("More");
261 fl_addtopup(curmenuid, label.c_str(), tmpmenuid);
263 curmenuid = tmpmenuid;
266 switch (item.kind()) {
267 case MenuItem::Command: {
268 FuncStatus const flag =
269 view->getLyXFunc().getStatus(item.action());
270 // handle optional entries.
272 && (flag.disabled())) {
274 << "Skipping optional item "
275 << item.label() << endl;
279 // Get the keys bound to this action, but keep only the
282 toplevel_keymap->findbinding(item.action());
283 // Build the menu label from all the info
284 string label = fixlabel(item.label());
286 if (!accel.empty()) {
287 // Try to be clever and add just enough
288 // tabs to align shortcuts.
291 while (string_width(label) < max_width + 5);
292 label += accel.substr(1,accel.find(']') - 1);
294 label += "%x" + tostr(item.action() + action_offset)
297 // Modify the entry using the function status
299 if (flag.onoff(true))
301 if (flag.onoff(false))
303 if (flag.disabled() || flag.unknown())
306 all_disabled = false;
309 // Finally the menu shortcut
310 string shortcut = item.shortcut();
312 if (!shortcut.empty()) {
313 shortcut += lowercase(shortcut[0]);
315 fl_addtopup(curmenuid, label.c_str(),
318 fl_addtopup(curmenuid, label.c_str());
320 lyxerr[Debug::GUI] << "Command: \""
321 << lyxaction.getActionName(item.action())
322 << "\", binding \"" << accel
323 << "\", shortcut \"" << shortcut
324 << "\" (added to menu"
325 << curmenuid << ")" << endl;
329 case MenuItem::Submenu: {
330 bool sub_all_disabled;
331 int submenuid = create_submenu(win, view,
332 *item.submenu(), smn,
334 all_disabled &= sub_all_disabled;
337 string label = fixlabel(item.label());
338 label += extra_label + "%m";
339 if (sub_all_disabled)
341 string shortcut = item.shortcut();
342 if (!shortcut.empty()) {
343 shortcut += lowercase(shortcut[0]);
345 fl_addtopup(curmenuid, label.c_str(),
346 submenuid, shortcut.c_str());
348 fl_addtopup(curmenuid, label.c_str(),
354 case MenuItem::Separator:
355 // already done, and if it was the first one,
356 // we just ignore it.
361 lyxerr << "Menubar::Pimpl::create_submenu: "
362 "this should not happen" << endl;
370 void Menubar::Pimpl::MenuCallback(FL_OBJECT * ob, long button)
372 ItemInfo * iteminfo = static_cast<ItemInfo *>(ob->u_vdata);
373 XFormsView * view = iteminfo->pimpl_->owner_;
374 MenuItem const * item = iteminfo->item_.get();
377 // set the pseudo menu-button
378 fl_set_object_boxtype(ob, FL_DOWN_BOX);
379 fl_set_button(ob, 0);
380 fl_redraw_object(ob);
384 lyx::Assert(item->kind() == MenuItem::Submenu);
386 // set tabstop length
387 fl_set_tabstop(menu_tabstop);
389 MenuBackend const * menubackend_ = iteminfo->pimpl_->menubackend_;
391 Menu const frommenu = menubackend_->getMenu(item->submenuname());
392 menubackend_->expand(frommenu, tomenu, view->buffer());
393 vector<int> submenus;
394 bool all_disabled = true;
395 int menu = iteminfo->pimpl_->
396 create_submenu(FL_ObjWin(ob), view, tomenu,
397 submenus, all_disabled);
400 fl_setpup_position(view->getForm()->x + ob->x,
401 view->getForm()->y + ob->y + ob->h + 10);
402 int choice = fl_dopup(menu);
405 // set the pseudo menu-button back
406 fl_set_object_boxtype(ob, FL_FLAT_BOX);
407 fl_redraw_object(ob);
410 // If the action value is too low, then it is not a
411 // valid action, but something else.
412 if (choice >= action_offset + 1) {
413 view->getLyXFunc().dispatch(choice - action_offset, true);
416 << "MenuCallback: ignoring bogus action "
420 lyxerr << "Error in MenuCallback" << endl;
423 for_each(submenus.begin(), submenus.end(), fl_freepup);
424 // restore tabstop length
425 fl_set_tabstop(default_tabstop);
430 Menubar::Pimpl::ItemInfo::ItemInfo
431 (Menubar::Pimpl * p, MenuItem const * i, FL_OBJECT * o)
438 Menubar::Pimpl::ItemInfo::~ItemInfo()