2 * \file XFormsMenubar.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
7 * \author Jean-Marc Lasgouttes
9 * Full author contact details are available in file CREDITS.
14 #include "XFormsMenubar.h"
16 #include "XFormsView.h"
21 #include "MenuBackend.h"
23 #include "support/lstrings.h"
24 #include "support/tostr.h"
26 #include <boost/bind.hpp>
28 #include "lyx_forms.h"
38 using support::lowercase;
43 typedef vector<int>::size_type size_type;
48 int const MENU_LABEL_SIZE = FL_NORMAL_SIZE;
49 int const MENU_LABEL_STYLE = FL_NORMAL_STYLE;
50 int const mheight = 30;
51 int const mbheight= 22;
52 // where to place the menubar?
53 int const yloc = (mheight - mbheight)/2; //air + bw;
54 int const mbadd = 20; // menu button add (to width)
55 // Some space between buttons on the menubar
57 char const * menu_tabstop = "aa";
58 char const * default_tabstop = "aaaaaaaa";
59 // We do not want to mix position values in a menu (like the index of
60 // a submenu) with the action numbers which convey actual information.
61 // Therefore we offset all the action values by an arbitrary large
63 int const action_offset = 1000;
65 // This is used a few times below.
67 int string_width(string const & str)
69 return fl_get_string_widthTAB(MENU_LABEL_STYLE, MENU_LABEL_SIZE,
71 static_cast<int>(str.length()));
79 //Defined later, used in makeMenubar().
81 void C_XFormsMenubar_MenuCallback(FL_OBJECT * ob, long button)
83 XFormsMenubar::MenuCallback(ob, button);
89 XFormsMenubar::XFormsMenubar(LyXView * view, MenuBackend const & mb)
90 : owner_(static_cast<XFormsView*>(view)),
94 owner_->metricsUpdated.connect(boost::bind(&WidgetMap::updateMetrics,
97 makeMenubar(menubackend_->getMenubar());
101 XFormsMenubar::~XFormsMenubar()
105 void XFormsMenubar::makeMenubar(Menu const & menu)
107 // Draw a frame around the whole.
108 BoxList & boxlist = owner_->getBox(XFormsView::Top).children();
110 FL_OBJECT * frame = fl_add_frame(FL_UP_FRAME, 0, 0, 0, 0, "");
111 fl_set_object_resize(frame, FL_RESIZE_ALL);
112 fl_set_object_gravity(frame, NorthWestGravity, NorthEastGravity);
114 menubar_ = &widgets_.add(frame, boxlist, 0, mheight);
116 // The menubar contains three vertically-aligned Boxes,
117 // the center one of which is to contain the buttons,
118 // aligned horizontally.
119 // The other two provide some visual padding.
120 menubar_->children().push_back(Box(0, yloc));
121 Box & menubar_center = menubar_->children().push_back(Box(0,0));
122 menubar_center.set(Box::Horizontal);
123 menubar_->children().push_back(Box(0, yloc));
125 BoxList & menubar_buttons = menubar_center.children();
128 Menu::const_iterator i = menu.begin();
129 Menu::const_iterator end = menu.end();
130 for (; i != end; ++i) {
132 if (i->kind() != MenuItem::Submenu) {
133 lyxerr << "ERROR: XFormsMenubar::createMenubar:"
134 " only submenus can appear in a menubar"
138 string const label = i->label();
139 string const shortcut = '#' + i->shortcut();
140 int const width = string_width(label);
142 obj = fl_add_button(FL_MENU_BUTTON, 0, 0, 0, 0, label.c_str());
144 menubar_buttons.push_back(Box(air, 0));
145 widgets_.add(obj, menubar_buttons, width + mbadd, mbheight);
147 fl_set_object_boxtype(obj, FL_FLAT_BOX);
148 fl_set_object_color(obj, FL_MCOL, FL_MCOL);
149 fl_set_object_lsize(obj, MENU_LABEL_SIZE);
150 fl_set_object_lstyle(obj, MENU_LABEL_STYLE);
151 fl_set_object_resize(obj, FL_RESIZE_ALL);
152 fl_set_object_gravity(obj, NorthWestGravity,
154 fl_set_object_shortcut(obj, shortcut.c_str(), 1);
155 fl_set_object_callback(obj, C_XFormsMenubar_MenuCallback, 1);
157 boost::shared_ptr<ItemInfo>
158 iteminfo(new ItemInfo(this, new MenuItem(*i), obj));
159 buttonlist_.push_back(iteminfo);
160 obj->u_vdata = iteminfo.get();
165 void XFormsMenubar::update()
171 void XFormsMenubar::openByName(string const & name)
173 for (ButtonList::const_iterator cit = buttonlist_.begin();
174 cit != buttonlist_.end(); ++cit) {
175 if ((*cit)->item_->submenuname() == name) {
176 MenuCallback((*cit)->obj_, 1);
181 lyxerr << "XFormsMenubar::openByName: menu "
182 << name << " not found" << endl;
188 Menu::size_type const max_number_of_items = 25;
190 int get_new_submenu(vector<int> & smn, Window win)
192 static size_type max_number_of_menus = 32;
193 if (smn.size() >= max_number_of_menus)
194 max_number_of_menus =
195 fl_setpup_maxpup(static_cast<int>(2*smn.size()));
196 int menu = fl_newpup(win);
197 fl_setpup_softedge(menu, true);
198 fl_setpup_bw(menu, -1);
204 string const fixlabel(string const & str)
206 return subst(str, "%", "%%");
215 int XFormsMenubar::create_submenu(Window win, XFormsView * view,
217 vector<int> & smn, Funcs & funcs)
219 int const menuid = get_new_submenu(smn, win);
220 lyxerr[Debug::GUI] << "XFormsMenubar::create_submenu: creating "
221 << menu.name() << " as menuid=" << menuid << endl;
223 // Compute the size of the largest label (because xforms is
224 // not able to support shortcuts correctly...)
227 Menu::const_iterator end = menu.end();
228 for (Menu::const_iterator i = menu.begin(); i != end; ++i) {
229 MenuItem const & item = (*i);
230 if (item.kind() == MenuItem::Command) {
231 string const label = item.label() + '\t';
232 int const width = string_width(label);
233 if (width > max_width) {
235 widest_label = label;
239 lyxerr[Debug::GUI] << "max_width=" << max_width
240 << ", widest_label=\"" << widest_label
244 int curmenuid = menuid;
245 for (Menu::const_iterator i = menu.begin(); i != end; ++i) {
246 MenuItem const & item = (*i);
249 // add a More... submenu if the menu is too long (but
250 // not just for one extra entry!)
251 if (count > max_number_of_items && (i + 1) != end) {
252 int tmpmenuid = get_new_submenu(smn, win);
253 lyxerr[Debug::GUI] << "Too many items, creating "
254 << "new menu " << tmpmenuid << endl;
255 string label = _("More");
257 fl_addtopup(curmenuid, label.c_str(), tmpmenuid);
259 curmenuid = tmpmenuid;
262 switch (item.kind()) {
263 case MenuItem::Command:
264 case MenuItem::Submenu:
266 // Build the menu label from all the info
267 string label = fixlabel(item.label());
268 FuncStatus const flag = item.status();
271 // Is there a key binding?
272 string const binding = item.binding();
273 if (!binding.empty()) {
274 // Try to be clever and add just enough
275 // tabs to align shortcuts.
278 while (string_width(label) < max_width + 5);
282 // Is there a separator after the item?
284 && (i + 1)->kind() == MenuItem::Separator)
287 // Modify the entry using the function status
288 if (flag.onoff(true))
290 if (flag.onoff(false))
296 string shortcut = item.shortcut();
297 if (!shortcut.empty()) {
298 shortcut += lowercase(shortcut[0]);
302 // Finally add the action/submenu
303 if (item.kind() == MenuItem::Submenu) {
304 // create the submenu
306 create_submenu(win, view,
307 *item.submenu(), smn, funcs);
310 label += "%x" + tostr(smn.size());
312 << "Menu: " << submenuid
313 << " (at index " << smn.size()
317 Funcs::iterator fit =
318 funcs.insert(funcs.end(), item.func());
319 int const action_count =
320 distance(funcs.begin(), fit);
322 label += "%x" + tostr(action_count + action_offset);
323 lyxerr[Debug::GUI] << "Action: \""
324 << item.func().action
325 << "(" << item.func().argument
329 // Add everything to the menu
330 fl_addtopup(curmenuid, label.c_str(),
332 if (item.kind() == MenuItem::Submenu)
333 fl_setpup_submenu(curmenuid, smn.size(),
336 lyxerr[Debug::GUI] << "label \"" << label
337 << "\", binding \"" << binding
338 << "\", shortcut \"" << shortcut
339 << "\" (added to menu "
340 << curmenuid << ')' << endl;
344 case MenuItem::Separator:
345 // already done, and if it was the first one,
346 // we just ignore it.
352 lyxerr << "XFormsMenubar::create_submenu: "
353 "this should not happen" << endl;
361 void XFormsMenubar::MenuCallback(FL_OBJECT * ob, long button)
363 ItemInfo * iteminfo = static_cast<ItemInfo *>(ob->u_vdata);
364 XFormsView * view = iteminfo->menubar_->owner_;
365 MenuItem const * item = iteminfo->item_.get();
368 // set the pseudo menu-button
369 fl_set_object_boxtype(ob, FL_DOWN_BOX);
370 fl_set_button(ob, 0);
371 fl_redraw_object(ob);
375 BOOST_ASSERT(item->kind() == MenuItem::Submenu);
377 // set tabstop length
378 fl_set_tabstop(menu_tabstop);
380 MenuBackend const * menubackend_ = iteminfo->menubar_->menubackend_;
382 Menu const frommenu = menubackend_->getMenu(item->submenuname());
383 menubackend_->expand(frommenu, tomenu, view);
385 vector<int> submenus;
386 int menu = iteminfo->menubar_->create_submenu(FL_ObjWin(ob), view,
387 tomenu, submenus, funcs);
390 fl_setpup_position(view->getForm()->x + ob->x,
391 view->getForm()->y + ob->y + ob->h + 10);
392 int choice = fl_dopup(menu);
395 // set the pseudo menu-button back
396 fl_set_object_boxtype(ob, FL_FLAT_BOX);
397 fl_redraw_object(ob);
400 // If the action value is too low, then it is not a
401 // valid action, but something else.
402 if (choice >= action_offset) {
403 view->getLyXFunc().dispatch(funcs[choice - action_offset]);
406 << "MenuCallback: ignoring bogus action "
410 lyxerr << "Error in MenuCallback" << endl;
413 for_each(submenus.begin(), submenus.end(), fl_freepup);
414 // restore tabstop length
415 fl_set_tabstop(default_tabstop);
420 XFormsMenubar::ItemInfo::ItemInfo
421 (XFormsMenubar * p, MenuItem const * i, FL_OBJECT * o)
422 : menubar_(p), obj_(o)
428 XFormsMenubar::ItemInfo::~ItemInfo()
431 } // namespace frontend