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 "lyx_forms.h"
28 using lyx::support::lowercase;
29 using lyx::support::subst;
38 typedef vector<int>::size_type size_type;
43 int const MENU_LABEL_SIZE = FL_NORMAL_SIZE;
44 int const MENU_LABEL_STYLE = FL_NORMAL_STYLE;
45 int const mheight = 30;
46 int const mbheight= 22;
47 // where to place the menubar?
48 int const yloc = (mheight - mbheight)/2; //air + bw;
49 int const mbadd = 20; // menu button add (to width)
50 // Some space between buttons on the menubar
52 char const * menu_tabstop = "aa";
53 char const * default_tabstop = "aaaaaaaa";
54 // We do not want to mix position values in a menu (like the index of
55 // a submenu) with the action numbers which convey actual information.
56 // Therefore we offset all the action values by an arbitrary large
58 int const action_offset = 1000;
60 // This is used a few times below.
62 int string_width(string const & str)
64 return fl_get_string_widthTAB(MENU_LABEL_STYLE, MENU_LABEL_SIZE,
66 static_cast<int>(str.length()));
74 //Defined later, used in makeMenubar().
76 void C_XFormsMenubar_MenuCallback(FL_OBJECT * ob, long button)
78 XFormsMenubar::MenuCallback(ob, button);
84 XFormsMenubar::XFormsMenubar(LyXView * view, MenuBackend const & mb)
85 : owner_(static_cast<XFormsView*>(view)), menubackend_(&mb)
87 makeMenubar(menubackend_->getMenubar());
91 XFormsMenubar::~XFormsMenubar()
95 void XFormsMenubar::makeMenubar(Menu const & menu)
97 FL_FORM * form = owner_->getForm();
100 // Create menu frame if there is non yet.
101 FL_OBJECT * frame = fl_add_frame(FL_UP_FRAME, 0, 0,
102 form->w, mheight, "");
103 fl_set_object_resize(frame, FL_RESIZE_ALL);
104 fl_set_object_gravity(frame, NorthWestGravity,
107 Menu::const_iterator i = menu.begin();
108 Menu::const_iterator end = menu.end();
109 for (; i != end; ++i) {
111 if (i->kind() != MenuItem::Submenu) {
112 lyxerr << "ERROR: XFormsMenubar::createMenubar:"
113 " only submenus can appear in a menubar"
117 string const label = i->label();
118 string const shortcut = '#' + i->shortcut();
119 int const width = string_width(label);
120 obj = fl_add_button(FL_MENU_BUTTON,
125 fl_set_object_boxtype(obj, FL_FLAT_BOX);
126 fl_set_object_color(obj, FL_MCOL, FL_MCOL);
127 fl_set_object_lsize(obj, MENU_LABEL_SIZE);
128 fl_set_object_lstyle(obj, MENU_LABEL_STYLE);
129 fl_set_object_resize(obj, FL_RESIZE_ALL);
130 fl_set_object_gravity(obj, NorthWestGravity,
132 moffset += obj->w + air;
133 fl_set_object_shortcut(obj, shortcut.c_str(), 1);
134 fl_set_object_callback(obj, C_XFormsMenubar_MenuCallback, 1);
136 boost::shared_ptr<ItemInfo>
137 iteminfo(new ItemInfo(this, new MenuItem(*i), obj));
138 buttonlist_.push_back(iteminfo);
139 obj->u_vdata = iteminfo.get();
145 void XFormsMenubar::update()
151 void XFormsMenubar::openByName(string const & name)
153 for (ButtonList::const_iterator cit = buttonlist_.begin();
154 cit != buttonlist_.end(); ++cit) {
155 if ((*cit)->item_->submenuname() == name) {
156 MenuCallback((*cit)->obj_, 1);
161 lyxerr << "XFormsMenubar::openByName: menu "
162 << name << " not found" << endl;
168 Menu::size_type const max_number_of_items = 25;
170 int get_new_submenu(vector<int> & smn, Window win)
172 static size_type max_number_of_menus = 32;
173 if (smn.size() >= max_number_of_menus)
174 max_number_of_menus =
175 fl_setpup_maxpup(static_cast<int>(2*smn.size()));
176 int menu = fl_newpup(win);
177 fl_setpup_softedge(menu, true);
178 fl_setpup_bw(menu, -1);
184 string const fixlabel(string const & str)
186 return subst(str, "%", "%%");
195 int XFormsMenubar::create_submenu(Window win, XFormsView * view,
197 vector<int> & smn, Funcs & funcs)
199 int const menuid = get_new_submenu(smn, win);
200 lyxerr[Debug::GUI] << "XFormsMenubar::create_submenu: creating "
201 << menu.name() << " as menuid=" << menuid << endl;
203 // Compute the size of the largest label (because xforms is
204 // not able to support shortcuts correctly...)
207 Menu::const_iterator end = menu.end();
208 for (Menu::const_iterator i = menu.begin(); i != end; ++i) {
209 MenuItem const & item = (*i);
210 if (item.kind() == MenuItem::Command) {
211 string const label = item.label() + '\t';
212 int const width = string_width(label);
213 if (width > max_width) {
215 widest_label = label;
219 lyxerr[Debug::GUI] << "max_width=" << max_width
220 << ", widest_label=\"" << widest_label
224 int curmenuid = menuid;
225 for (Menu::const_iterator i = menu.begin(); i != end; ++i) {
226 MenuItem const & item = (*i);
229 // add a More... submenu if the menu is too long (but
230 // not just for one extra entry!)
231 if (count > max_number_of_items && (i + 1) != end) {
232 int tmpmenuid = get_new_submenu(smn, win);
233 lyxerr[Debug::GUI] << "Too many items, creating "
234 << "new menu " << tmpmenuid << endl;
235 string label = _("More");
237 fl_addtopup(curmenuid, label.c_str(), tmpmenuid);
239 curmenuid = tmpmenuid;
242 switch (item.kind()) {
243 case MenuItem::Command:
244 case MenuItem::Submenu:
246 // Build the menu label from all the info
247 string label = fixlabel(item.label());
248 FuncStatus const flag = item.status();
251 // Is there a key binding?
252 string const binding = item.binding();
253 if (!binding.empty()) {
254 // Try to be clever and add just enough
255 // tabs to align shortcuts.
258 while (string_width(label) < max_width + 5);
262 // Is there a separator after the item?
264 && (i + 1)->kind() == MenuItem::Separator)
267 // Modify the entry using the function status
268 if (flag.onoff(true))
270 if (flag.onoff(false))
276 string shortcut = item.shortcut();
277 if (!shortcut.empty()) {
278 shortcut += lowercase(shortcut[0]);
282 // Finally add the action/submenu
283 if (item.kind() == MenuItem::Submenu) {
284 // create the submenu
286 create_submenu(win, view,
287 *item.submenu(), smn, funcs);
290 label += "%x" + tostr(smn.size());
292 << "Menu: " << submenuid
293 << " (at index " << smn.size()
297 Funcs::iterator fit =
298 funcs.insert(funcs.end(), item.func());
299 int const action_count =
300 distance(funcs.begin(), fit);
302 label += "%x" + tostr(action_count + action_offset);
303 lyxerr[Debug::GUI] << "Action: \""
304 << item.func().action
305 << "(" << item.func().argument
309 // Add everything to the menu
310 fl_addtopup(curmenuid, label.c_str(),
312 if (item.kind() == MenuItem::Submenu)
313 fl_setpup_submenu(curmenuid, smn.size(),
316 lyxerr[Debug::GUI] << "label \"" << label
317 << "\", binding \"" << binding
318 << "\", shortcut \"" << shortcut
319 << "\" (added to menu "
320 << curmenuid << ')' << endl;
324 case MenuItem::Separator:
325 // already done, and if it was the first one,
326 // we just ignore it.
332 lyxerr << "XFormsMenubar::create_submenu: "
333 "this should not happen" << endl;
341 void XFormsMenubar::MenuCallback(FL_OBJECT * ob, long button)
343 ItemInfo * iteminfo = static_cast<ItemInfo *>(ob->u_vdata);
344 XFormsView * view = iteminfo->menubar_->owner_;
345 MenuItem const * item = iteminfo->item_.get();
348 // set the pseudo menu-button
349 fl_set_object_boxtype(ob, FL_DOWN_BOX);
350 fl_set_button(ob, 0);
351 fl_redraw_object(ob);
355 BOOST_ASSERT(item->kind() == MenuItem::Submenu);
357 // set tabstop length
358 fl_set_tabstop(menu_tabstop);
360 MenuBackend const * menubackend_ = iteminfo->menubar_->menubackend_;
362 Menu const frommenu = menubackend_->getMenu(item->submenuname());
363 menubackend_->expand(frommenu, tomenu, view);
365 vector<int> submenus;
366 int menu = iteminfo->menubar_->create_submenu(FL_ObjWin(ob), view,
367 tomenu, submenus, funcs);
370 fl_setpup_position(view->getForm()->x + ob->x,
371 view->getForm()->y + ob->y + ob->h + 10);
372 int choice = fl_dopup(menu);
375 // set the pseudo menu-button back
376 fl_set_object_boxtype(ob, FL_FLAT_BOX);
377 fl_redraw_object(ob);
380 // If the action value is too low, then it is not a
381 // valid action, but something else.
382 if (choice >= action_offset) {
383 view->getLyXFunc().dispatch(funcs[choice - action_offset], true);
386 << "MenuCallback: ignoring bogus action "
390 lyxerr << "Error in MenuCallback" << endl;
393 for_each(submenus.begin(), submenus.end(), fl_freepup);
394 // restore tabstop length
395 fl_set_tabstop(default_tabstop);
400 XFormsMenubar::ItemInfo::ItemInfo
401 (XFormsMenubar * p, MenuItem const * i, FL_OBJECT * o)
402 : menubar_(p), obj_(o)
408 XFormsMenubar::ItemInfo::~ItemInfo()