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/LAssert.h"
24 #include "support/lstrings.h"
25 #include "support/tostr.h"
27 #include "lyx_forms.h"
29 using namespace lyx::support;
36 typedef vector<int>::size_type size_type;
41 int const MENU_LABEL_SIZE = FL_NORMAL_SIZE;
42 int const MENU_LABEL_STYLE = FL_NORMAL_STYLE;
43 int const mheight = 30;
44 int const mbheight= 22;
45 // where to place the menubar?
46 int const yloc = (mheight - mbheight)/2; //air + bw;
47 int const mbadd = 20; // menu button add (to width)
48 // Some space between buttons on the menubar
50 char const * menu_tabstop = "aa";
51 char const * default_tabstop = "aaaaaaaa";
52 // We do not want to mix position values in a menu (like the index of
53 // a submenu) with the action numbers which convey actual information.
54 // Therefore we offset all the action values by an arbitrary large
56 int const action_offset = 1000;
58 // This is used a few times below.
60 int string_width(string const & str)
62 return fl_get_string_widthTAB(MENU_LABEL_STYLE, MENU_LABEL_SIZE,
64 static_cast<int>(str.length()));
72 //Defined later, used in makeMenubar().
74 void C_XFormsMenubar_MenuCallback(FL_OBJECT * ob, long button)
76 XFormsMenubar::MenuCallback(ob, button);
82 XFormsMenubar::XFormsMenubar(LyXView * view, MenuBackend const & mb)
83 : owner_(static_cast<XFormsView*>(view)), menubackend_(&mb)
85 makeMenubar(menubackend_->getMenubar());
89 XFormsMenubar::~XFormsMenubar()
93 void XFormsMenubar::makeMenubar(Menu const & menu)
95 FL_FORM * form = owner_->getForm();
98 // Create menu frame if there is non yet.
99 FL_OBJECT * frame = fl_add_frame(FL_UP_FRAME, 0, 0,
100 form->w, mheight, "");
101 fl_set_object_resize(frame, FL_RESIZE_ALL);
102 fl_set_object_gravity(frame, NorthWestGravity,
105 Menu::const_iterator i = menu.begin();
106 Menu::const_iterator end = menu.end();
107 for (; i != end; ++i) {
109 if (i->kind() != MenuItem::Submenu) {
110 lyxerr << "ERROR: XFormsMenubar::createMenubar:"
111 " only submenus can appear in a menubar"
115 string const label = i->label();
116 string const shortcut = '#' + i->shortcut();
117 int const width = string_width(label);
118 obj = fl_add_button(FL_MENU_BUTTON,
123 fl_set_object_boxtype(obj, FL_FLAT_BOX);
124 fl_set_object_color(obj, FL_MCOL, FL_MCOL);
125 fl_set_object_lsize(obj, MENU_LABEL_SIZE);
126 fl_set_object_lstyle(obj, MENU_LABEL_STYLE);
127 fl_set_object_resize(obj, FL_RESIZE_ALL);
128 fl_set_object_gravity(obj, NorthWestGravity,
130 moffset += obj->w + air;
131 fl_set_object_shortcut(obj, shortcut.c_str(), 1);
132 fl_set_object_callback(obj, C_XFormsMenubar_MenuCallback, 1);
134 boost::shared_ptr<ItemInfo>
135 iteminfo(new ItemInfo(this, new MenuItem(*i), obj));
136 buttonlist_.push_back(iteminfo);
137 obj->u_vdata = iteminfo.get();
143 void XFormsMenubar::update()
149 void XFormsMenubar::openByName(string const & name)
151 for (ButtonList::const_iterator cit = buttonlist_.begin();
152 cit != buttonlist_.end(); ++cit) {
153 if ((*cit)->item_->submenuname() == name) {
154 MenuCallback((*cit)->obj_, 1);
159 lyxerr << "XFormsMenubar::openByName: menu "
160 << name << " not found" << endl;
166 Menu::size_type const max_number_of_items = 25;
168 int get_new_submenu(vector<int> & smn, Window win)
170 static size_type max_number_of_menus = 32;
171 if (smn.size() >= max_number_of_menus)
172 max_number_of_menus =
173 fl_setpup_maxpup(static_cast<int>(2*smn.size()));
174 int menu = fl_newpup(win);
175 fl_setpup_softedge(menu, true);
176 fl_setpup_bw(menu, -1);
182 string const fixlabel(string const & str)
184 return subst(str, "%", "%%");
193 int XFormsMenubar::create_submenu(Window win, XFormsView * view,
194 Menu const & menu, vector<int> & smn)
196 const int menuid = get_new_submenu(smn, win);
197 lyxerr[Debug::GUI] << "XFormsMenubar::create_submenu: creating "
198 << menu.name() << " as menuid=" << menuid << endl;
200 // Compute the size of the largest label (because xforms is
201 // not able to support shortcuts correctly...)
204 Menu::const_iterator end = menu.end();
205 for (Menu::const_iterator i = menu.begin(); i != end; ++i) {
206 MenuItem const & item = (*i);
207 if (item.kind() == MenuItem::Command) {
208 string const label = item.label() + '\t';
209 int const width = string_width(label);
210 if (width > max_width) {
212 widest_label = label;
216 lyxerr[Debug::GUI] << "max_width=" << max_width
217 << ", widest_label=\"" << widest_label
221 int curmenuid = menuid;
222 for (Menu::const_iterator i = menu.begin(); i != end; ++i) {
223 MenuItem const & item = (*i);
226 // add a More... submenu if the menu is too long (but
227 // not just for one extra entry!)
228 if (count > max_number_of_items && (i+1) != end) {
229 int tmpmenuid = get_new_submenu(smn, win);
230 lyxerr[Debug::GUI] << "Too many items, creating "
231 << "new menu " << tmpmenuid << endl;
232 string label = _("More");
234 fl_addtopup(curmenuid, label.c_str(), tmpmenuid);
236 curmenuid = tmpmenuid;
239 switch (item.kind()) {
240 case MenuItem::Command:
241 case MenuItem::Submenu:
243 // Build the menu label from all the info
244 string label = fixlabel(item.label());
245 FuncStatus const flag = item.status();
248 // Is there a key binding?
249 string const binding = item.binding();
250 if (!binding.empty()) {
251 // Try to be clever and add just enough
252 // tabs to align shortcuts.
255 while (string_width(label) < max_width + 5);
259 // Is there a separator after the item?
261 && (i + 1)->kind() == MenuItem::Separator)
264 // Modify the entry using the function status
265 if (flag.onoff(true))
267 if (flag.onoff(false))
273 string shortcut = item.shortcut();
274 if (!shortcut.empty()) {
275 shortcut += lowercase(shortcut[0]);
279 // Finally add the action/submenu
280 if (item.kind() == MenuItem::Submenu) {
281 // create the submenu
283 create_submenu(win, view,
284 *item.submenu(), smn);
287 label += "%x" + tostr(smn.size());
289 << "Menu: " << submenuid
290 << " (at index " << smn.size()
294 label += "%x" + tostr(item.action()
296 lyxerr[Debug::GUI] << "Action: \""
297 << item.action() << "\", ";
300 // Add everything to the menu
301 fl_addtopup(curmenuid, label.c_str(),
303 if (item.kind() == MenuItem::Submenu)
304 fl_setpup_submenu(curmenuid, smn.size(),
307 lyxerr[Debug::GUI] << "label \"" << label
308 << "\", binding \"" << binding
309 << "\", shortcut \"" << shortcut
310 << "\" (added to menu "
311 << curmenuid << ')' << endl;
315 case MenuItem::Separator:
316 // already done, and if it was the first one,
317 // we just ignore it.
323 lyxerr << "XFormsMenubar::create_submenu: "
324 "this should not happen" << endl;
332 void XFormsMenubar::MenuCallback(FL_OBJECT * ob, long button)
334 ItemInfo * iteminfo = static_cast<ItemInfo *>(ob->u_vdata);
335 XFormsView * view = iteminfo->menubar_->owner_;
336 MenuItem const * item = iteminfo->item_.get();
339 // set the pseudo menu-button
340 fl_set_object_boxtype(ob, FL_DOWN_BOX);
341 fl_set_button(ob, 0);
342 fl_redraw_object(ob);
346 Assert(item->kind() == MenuItem::Submenu);
348 // set tabstop length
349 fl_set_tabstop(menu_tabstop);
351 MenuBackend const * menubackend_ = iteminfo->menubar_->menubackend_;
353 Menu const frommenu = menubackend_->getMenu(item->submenuname());
354 menubackend_->expand(frommenu, tomenu, view);
355 vector<int> submenus;
356 int menu = iteminfo->menubar_->create_submenu(FL_ObjWin(ob), view,
360 fl_setpup_position(view->getForm()->x + ob->x,
361 view->getForm()->y + ob->y + ob->h + 10);
362 int choice = fl_dopup(menu);
365 // set the pseudo menu-button back
366 fl_set_object_boxtype(ob, FL_FLAT_BOX);
367 fl_redraw_object(ob);
370 // If the action value is too low, then it is not a
371 // valid action, but something else.
372 if (choice >= action_offset + 1) {
373 view->getLyXFunc().dispatch(choice - action_offset, true);
376 << "MenuCallback: ignoring bogus action "
380 lyxerr << "Error in MenuCallback" << endl;
383 for_each(submenus.begin(), submenus.end(), fl_freepup);
384 // restore tabstop length
385 fl_set_tabstop(default_tabstop);
390 XFormsMenubar::ItemInfo::ItemInfo
391 (XFormsMenubar * p, MenuItem const * i, FL_OBJECT * o)
392 : menubar_(p), obj_(o)
398 XFormsMenubar::ItemInfo::~ItemInfo()