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"
30 using boost::shared_ptr;
40 using support::lowercase;
45 typedef vector<int>::size_type size_type;
50 int const MENU_LABEL_SIZE = FL_NORMAL_SIZE;
51 int const MENU_LABEL_STYLE = FL_NORMAL_STYLE;
52 int const mheight = 30;
53 int const mbheight= 22;
54 // where to place the menubar?
55 int const yloc = (mheight - mbheight)/2; //air + bw;
56 int const mbadd = 20; // menu button add (to width)
57 // Some space between buttons on the menubar
59 char const * menu_tabstop = "aa";
60 char const * default_tabstop = "aaaaaaaa";
61 // We do not want to mix position values in a menu (like the index of
62 // a submenu) with the action numbers which convey actual information.
63 // Therefore we offset all the action values by an arbitrary large
65 int const action_offset = 1000;
67 // This is used a few times below.
69 int string_width(string const & str)
71 return fl_get_string_widthTAB(MENU_LABEL_STYLE, MENU_LABEL_SIZE,
73 static_cast<int>(str.length()));
81 //Defined later, used in makeMenubar().
83 void C_XFormsMenubar_MenuCallback(FL_OBJECT * ob, long button)
85 XFormsMenubar::MenuCallback(ob, button);
91 XFormsMenubar::XFormsMenubar(LyXView * view, MenuBackend const & mb)
92 : owner_(static_cast<XFormsView*>(view)),
95 owner_->metricsUpdated.connect(boost::bind(&WidgetMap::updateMetrics,
98 makeMenubar(menubackend_->getMenubar());
102 XFormsMenubar::~XFormsMenubar()
106 void XFormsMenubar::makeMenubar(Menu const & menu)
108 // Draw a frame around the whole.
109 BoxList & boxlist = owner_->getBox(XFormsView::Top)->children();
111 FL_OBJECT * frame = fl_add_frame(FL_UP_FRAME, 0, 0, 0, 0, "");
112 fl_set_object_resize(frame, FL_RESIZE_ALL);
113 fl_set_object_gravity(frame, NorthWestGravity, NorthEastGravity);
115 menubar_ = widgets_.add(frame, boxlist, 0, mheight);
117 // The menubar contains three vertically-aligned Boxes,
118 // the center one of which is to contain the buttons,
119 // aligned horizontally.
120 // The other two provide some visual padding.
121 menubar_->children().push_back(Box(0, yloc));
122 shared_ptr<Box> menubar_center = menubar_->children().push_back(Box(0,0));
123 menubar_center->set(Box::Horizontal);
124 menubar_->children().push_back(Box(0, yloc));
126 BoxList & menubar_buttons = menubar_center->children();
129 Menu::const_iterator i = menu.begin();
130 Menu::const_iterator end = menu.end();
131 for (; i != end; ++i) {
133 if (i->kind() != MenuItem::Submenu) {
134 lyxerr << "ERROR: XFormsMenubar::createMenubar:"
135 " only submenus can appear in a menubar"
139 string const label = i->label();
140 string const shortcut = '#' + i->shortcut();
141 int const width = string_width(label);
143 obj = fl_add_button(FL_MENU_BUTTON, 0, 0, 0, 0, label.c_str());
145 menubar_buttons.push_back(Box(air, 0));
146 widgets_.add(obj, menubar_buttons, width + mbadd, mbheight);
148 fl_set_object_boxtype(obj, FL_FLAT_BOX);
149 fl_set_object_color(obj, FL_MCOL, FL_MCOL);
150 fl_set_object_lsize(obj, MENU_LABEL_SIZE);
151 fl_set_object_lstyle(obj, MENU_LABEL_STYLE);
152 fl_set_object_resize(obj, FL_RESIZE_ALL);
153 fl_set_object_gravity(obj, NorthWestGravity,
155 fl_set_object_shortcut(obj, shortcut.c_str(), 1);
156 fl_set_object_callback(obj, C_XFormsMenubar_MenuCallback, 1);
158 boost::shared_ptr<ItemInfo>
159 iteminfo(new ItemInfo(this, new MenuItem(*i), obj));
160 buttonlist_.push_back(iteminfo);
161 obj->u_vdata = iteminfo.get();
166 void XFormsMenubar::update()
172 void XFormsMenubar::openByName(string const & name)
174 for (ButtonList::const_iterator cit = buttonlist_.begin();
175 cit != buttonlist_.end(); ++cit) {
176 if ((*cit)->item_->submenuname() == name) {
177 MenuCallback((*cit)->obj_, 1);
182 lyxerr << "XFormsMenubar::openByName: menu "
183 << name << " not found" << endl;
189 Menu::size_type const max_number_of_items = 25;
191 int get_new_submenu(vector<int> & smn, Window win)
193 static size_type max_number_of_menus = 32;
194 if (smn.size() >= max_number_of_menus)
195 max_number_of_menus =
196 fl_setpup_maxpup(static_cast<int>(2*smn.size()));
197 int menu = fl_newpup(win);
198 fl_setpup_softedge(menu, true);
199 fl_setpup_bw(menu, -1);
205 string const fixlabel(string const & str)
207 return subst(str, "%", "%%");
216 int XFormsMenubar::create_submenu(Window win, XFormsView * view,
218 vector<int> & smn, Funcs & funcs)
220 int const menuid = get_new_submenu(smn, win);
221 lyxerr[Debug::GUI] << "XFormsMenubar::create_submenu: creating "
222 << menu.name() << " as menuid=" << menuid << endl;
224 // Compute the size of the largest label (because xforms is
225 // not able to support shortcuts correctly...)
228 Menu::const_iterator end = menu.end();
229 for (Menu::const_iterator i = menu.begin(); i != end; ++i) {
230 MenuItem const & item = (*i);
231 if (item.kind() == MenuItem::Command) {
232 string const label = item.label() + '\t';
233 int const width = string_width(label);
234 if (width > max_width) {
236 widest_label = label;
240 lyxerr[Debug::GUI] << "max_width=" << max_width
241 << ", widest_label=\"" << widest_label
245 int curmenuid = menuid;
246 for (Menu::const_iterator i = menu.begin(); i != end; ++i) {
247 MenuItem const & item = (*i);
250 // add a More... submenu if the menu is too long (but
251 // not just for one extra entry!)
252 if (count > max_number_of_items && (i + 1) != end) {
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 case MenuItem::Submenu:
267 // Build the menu label from all the info
268 string label = fixlabel(item.label());
269 FuncStatus const flag = item.status();
272 // Is there a key binding?
273 string const binding = item.binding();
274 if (!binding.empty()) {
275 // Try to be clever and add just enough
276 // tabs to align shortcuts.
279 while (string_width(label) < max_width + 5);
283 // Is there a separator after the item?
285 && (i + 1)->kind() == MenuItem::Separator)
288 // Modify the entry using the function status
289 if (flag.onoff(true))
291 if (flag.onoff(false))
297 string shortcut = item.shortcut();
298 if (!shortcut.empty()) {
299 shortcut += lowercase(shortcut[0]);
303 // Finally add the action/submenu
304 if (item.kind() == MenuItem::Submenu) {
305 // create the submenu
307 create_submenu(win, view,
308 *item.submenu(), smn, funcs);
311 label += "%x" + tostr(smn.size());
313 << "Menu: " << submenuid
314 << " (at index " << smn.size()
318 Funcs::iterator fit =
319 funcs.insert(funcs.end(), item.func());
320 int const action_count =
321 distance(funcs.begin(), fit);
323 label += "%x" + tostr(action_count + action_offset);
324 lyxerr[Debug::GUI] << "Action: \""
325 << item.func().action
326 << "(" << item.func().argument
330 // Add everything to the menu
331 fl_addtopup(curmenuid, label.c_str(),
333 if (item.kind() == MenuItem::Submenu)
334 fl_setpup_submenu(curmenuid, smn.size(),
337 lyxerr[Debug::GUI] << "label \"" << label
338 << "\", binding \"" << binding
339 << "\", shortcut \"" << shortcut
340 << "\" (added to menu "
341 << curmenuid << ')' << endl;
345 case MenuItem::Separator:
346 // already done, and if it was the first one,
347 // we just ignore it.
353 lyxerr << "XFormsMenubar::create_submenu: "
354 "this should not happen" << endl;
362 void XFormsMenubar::MenuCallback(FL_OBJECT * ob, long button)
364 ItemInfo * iteminfo = static_cast<ItemInfo *>(ob->u_vdata);
365 XFormsView * view = iteminfo->menubar_->owner_;
366 MenuItem const * item = iteminfo->item_.get();
369 // set the pseudo menu-button
370 fl_set_object_boxtype(ob, FL_DOWN_BOX);
371 fl_set_button(ob, 0);
372 fl_redraw_object(ob);
376 BOOST_ASSERT(item->kind() == MenuItem::Submenu);
378 // set tabstop length
379 fl_set_tabstop(menu_tabstop);
381 MenuBackend const * menubackend_ = iteminfo->menubar_->menubackend_;
383 Menu const frommenu = menubackend_->getMenu(item->submenuname());
384 menubackend_->expand(frommenu, tomenu, view);
386 vector<int> submenus;
387 int menu = iteminfo->menubar_->create_submenu(FL_ObjWin(ob), view,
388 tomenu, submenus, funcs);
391 fl_setpup_position(view->getForm()->x + ob->x,
392 view->getForm()->y + ob->y + ob->h + 10);
393 int choice = fl_dopup(menu);
396 // set the pseudo menu-button back
397 fl_set_object_boxtype(ob, FL_FLAT_BOX);
398 fl_redraw_object(ob);
401 // If the action value is too low, then it is not a
402 // valid action, but something else.
403 if (choice >= action_offset) {
404 view->getLyXFunc().dispatch(funcs[choice - action_offset]);
407 << "MenuCallback: ignoring bogus action "
411 lyxerr << "Error in MenuCallback" << endl;
414 for_each(submenus.begin(), submenus.end(), fl_freepup);
415 // restore tabstop length
416 fl_set_tabstop(default_tabstop);
421 XFormsMenubar::ItemInfo::ItemInfo
422 (XFormsMenubar * p, MenuItem const * i, FL_OBJECT * o)
423 : menubar_(p), obj_(o)
429 XFormsMenubar::ItemInfo::~ItemInfo()
432 } // namespace frontend