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;
37 typedef vector<int>::size_type size_type;
42 int const MENU_LABEL_SIZE = FL_NORMAL_SIZE;
43 int const MENU_LABEL_STYLE = FL_NORMAL_STYLE;
44 int const mheight = 30;
45 int const mbheight= 22;
46 // where to place the menubar?
47 int const yloc = (mheight - mbheight)/2; //air + bw;
48 int const mbadd = 20; // menu button add (to width)
49 // Some space between buttons on the menubar
51 char const * menu_tabstop = "aa";
52 char const * default_tabstop = "aaaaaaaa";
53 // We do not want to mix position values in a menu (like the index of
54 // a submenu) with the action numbers which convey actual information.
55 // Therefore we offset all the action values by an arbitrary large
57 int const action_offset = 1000;
59 // This is used a few times below.
61 int string_width(string const & str)
63 return fl_get_string_widthTAB(MENU_LABEL_STYLE, MENU_LABEL_SIZE,
65 static_cast<int>(str.length()));
73 //Defined later, used in makeMenubar().
75 void C_XFormsMenubar_MenuCallback(FL_OBJECT * ob, long button)
77 XFormsMenubar::MenuCallback(ob, button);
83 XFormsMenubar::XFormsMenubar(LyXView * view, MenuBackend const & mb)
84 : owner_(static_cast<XFormsView*>(view)), menubackend_(&mb)
86 makeMenubar(menubackend_->getMenubar());
90 XFormsMenubar::~XFormsMenubar()
94 void XFormsMenubar::makeMenubar(Menu const & menu)
96 FL_FORM * form = owner_->getForm();
99 // Create menu frame if there is non yet.
100 FL_OBJECT * frame = fl_add_frame(FL_UP_FRAME, 0, 0,
101 form->w, mheight, "");
102 fl_set_object_resize(frame, FL_RESIZE_ALL);
103 fl_set_object_gravity(frame, NorthWestGravity,
106 Menu::const_iterator i = menu.begin();
107 Menu::const_iterator end = menu.end();
108 for (; i != end; ++i) {
110 if (i->kind() != MenuItem::Submenu) {
111 lyxerr << "ERROR: XFormsMenubar::createMenubar:"
112 " only submenus can appear in a menubar"
116 string const label = i->label();
117 string const shortcut = '#' + i->shortcut();
118 int const width = string_width(label);
119 obj = fl_add_button(FL_MENU_BUTTON,
124 fl_set_object_boxtype(obj, FL_FLAT_BOX);
125 fl_set_object_color(obj, FL_MCOL, FL_MCOL);
126 fl_set_object_lsize(obj, MENU_LABEL_SIZE);
127 fl_set_object_lstyle(obj, MENU_LABEL_STYLE);
128 fl_set_object_resize(obj, FL_RESIZE_ALL);
129 fl_set_object_gravity(obj, NorthWestGravity,
131 moffset += obj->w + air;
132 fl_set_object_shortcut(obj, shortcut.c_str(), 1);
133 fl_set_object_callback(obj, C_XFormsMenubar_MenuCallback, 1);
135 boost::shared_ptr<ItemInfo>
136 iteminfo(new ItemInfo(this, new MenuItem(*i), obj));
137 buttonlist_.push_back(iteminfo);
138 obj->u_vdata = iteminfo.get();
144 void XFormsMenubar::update()
150 void XFormsMenubar::openByName(string const & name)
152 for (ButtonList::const_iterator cit = buttonlist_.begin();
153 cit != buttonlist_.end(); ++cit) {
154 if ((*cit)->item_->submenuname() == name) {
155 MenuCallback((*cit)->obj_, 1);
160 lyxerr << "XFormsMenubar::openByName: menu "
161 << name << " not found" << endl;
167 Menu::size_type const max_number_of_items = 25;
169 int get_new_submenu(vector<int> & smn, Window win)
171 static size_type max_number_of_menus = 32;
172 if (smn.size() >= max_number_of_menus)
173 max_number_of_menus =
174 fl_setpup_maxpup(static_cast<int>(2*smn.size()));
175 int menu = fl_newpup(win);
176 fl_setpup_softedge(menu, true);
177 fl_setpup_bw(menu, -1);
183 string const fixlabel(string const & str)
185 return subst(str, "%", "%%");
194 int XFormsMenubar::create_submenu(Window win, XFormsView * view,
196 vector<int> & smn, Funcs & funcs)
198 int const menuid = get_new_submenu(smn, win);
199 lyxerr[Debug::GUI] << "XFormsMenubar::create_submenu: creating "
200 << menu.name() << " as menuid=" << menuid << endl;
202 // Compute the size of the largest label (because xforms is
203 // not able to support shortcuts correctly...)
206 Menu::const_iterator end = menu.end();
207 for (Menu::const_iterator i = menu.begin(); i != end; ++i) {
208 MenuItem const & item = (*i);
209 if (item.kind() == MenuItem::Command) {
210 string const label = item.label() + '\t';
211 int const width = string_width(label);
212 if (width > max_width) {
214 widest_label = label;
218 lyxerr[Debug::GUI] << "max_width=" << max_width
219 << ", widest_label=\"" << widest_label
223 int curmenuid = menuid;
224 for (Menu::const_iterator i = menu.begin(); i != end; ++i) {
225 MenuItem const & item = (*i);
228 // add a More... submenu if the menu is too long (but
229 // not just for one extra entry!)
230 if (count > max_number_of_items && (i + 1) != end) {
231 int tmpmenuid = get_new_submenu(smn, win);
232 lyxerr[Debug::GUI] << "Too many items, creating "
233 << "new menu " << tmpmenuid << endl;
234 string label = _("More");
236 fl_addtopup(curmenuid, label.c_str(), tmpmenuid);
238 curmenuid = tmpmenuid;
241 switch (item.kind()) {
242 case MenuItem::Command:
243 case MenuItem::Submenu:
245 // Build the menu label from all the info
246 string label = fixlabel(item.label());
247 FuncStatus const flag = item.status();
250 // Is there a key binding?
251 string const binding = item.binding();
252 if (!binding.empty()) {
253 // Try to be clever and add just enough
254 // tabs to align shortcuts.
257 while (string_width(label) < max_width + 5);
261 // Is there a separator after the item?
263 && (i + 1)->kind() == MenuItem::Separator)
266 // Modify the entry using the function status
267 if (flag.onoff(true))
269 if (flag.onoff(false))
275 string shortcut = item.shortcut();
276 if (!shortcut.empty()) {
277 shortcut += lowercase(shortcut[0]);
281 // Finally add the action/submenu
282 if (item.kind() == MenuItem::Submenu) {
283 // create the submenu
285 create_submenu(win, view,
286 *item.submenu(), smn, funcs);
289 label += "%x" + tostr(smn.size());
291 << "Menu: " << submenuid
292 << " (at index " << smn.size()
296 Funcs::iterator fit =
297 funcs.insert(funcs.end(), item.func());
298 int const action_count =
299 std::distance(funcs.begin(), fit);
301 label += "%x" + tostr(action_count + action_offset);
302 lyxerr[Debug::GUI] << "Action: \""
303 << item.func().action
304 << "(" << item.func().argument
308 // Add everything to the menu
309 fl_addtopup(curmenuid, label.c_str(),
311 if (item.kind() == MenuItem::Submenu)
312 fl_setpup_submenu(curmenuid, smn.size(),
315 lyxerr[Debug::GUI] << "label \"" << label
316 << "\", binding \"" << binding
317 << "\", shortcut \"" << shortcut
318 << "\" (added to menu "
319 << curmenuid << ')' << endl;
323 case MenuItem::Separator:
324 // already done, and if it was the first one,
325 // we just ignore it.
331 lyxerr << "XFormsMenubar::create_submenu: "
332 "this should not happen" << endl;
340 void XFormsMenubar::MenuCallback(FL_OBJECT * ob, long button)
342 ItemInfo * iteminfo = static_cast<ItemInfo *>(ob->u_vdata);
343 XFormsView * view = iteminfo->menubar_->owner_;
344 MenuItem const * item = iteminfo->item_.get();
347 // set the pseudo menu-button
348 fl_set_object_boxtype(ob, FL_DOWN_BOX);
349 fl_set_button(ob, 0);
350 fl_redraw_object(ob);
354 BOOST_ASSERT(item->kind() == MenuItem::Submenu);
356 // set tabstop length
357 fl_set_tabstop(menu_tabstop);
359 MenuBackend const * menubackend_ = iteminfo->menubar_->menubackend_;
361 Menu const frommenu = menubackend_->getMenu(item->submenuname());
362 menubackend_->expand(frommenu, tomenu, view);
364 vector<int> submenus;
365 int menu = iteminfo->menubar_->create_submenu(FL_ObjWin(ob), view,
366 tomenu, submenus, funcs);
369 fl_setpup_position(view->getForm()->x + ob->x,
370 view->getForm()->y + ob->y + ob->h + 10);
371 int choice = fl_dopup(menu);
374 // set the pseudo menu-button back
375 fl_set_object_boxtype(ob, FL_FLAT_BOX);
376 fl_redraw_object(ob);
379 // If the action value is too low, then it is not a
380 // valid action, but something else.
381 if (choice >= action_offset) {
382 view->getLyXFunc().dispatch(funcs[choice - action_offset], true);
385 << "MenuCallback: ignoring bogus action "
389 lyxerr << "Error in MenuCallback" << endl;
392 for_each(submenus.begin(), submenus.end(), fl_freepup);
393 // restore tabstop length
394 fl_set_tabstop(default_tabstop);
399 XFormsMenubar::ItemInfo::ItemInfo
400 (XFormsMenubar * p, MenuItem const * i, FL_OBJECT * o)
401 : menubar_(p), obj_(o)
407 XFormsMenubar::ItemInfo::~ItemInfo()