2 * \file xforms/Menubar_pimpl.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 "Menubar_pimpl.h"
15 #include "MenuBackend.h"
16 #include "XFormsView.h"
18 #include "support/lstrings.h"
19 #include "support/tostr.h"
20 #include "support/LAssert.h"
23 #include "lyx_forms.h"
25 //#include <boost/scoped_ptr.hpp>
35 typedef vector<int>::size_type size_type;
40 int const MENU_LABEL_SIZE = FL_NORMAL_SIZE;
41 int const MENU_LABEL_STYLE = FL_NORMAL_STYLE;
42 int const mheight = 30;
43 int const mbheight= 22;
44 // where to place the menubar?
45 int const yloc = (mheight - mbheight)/2; //air + bw;
46 int const mbadd = 20; // menu button add (to width)
47 // Some space between buttons on the menubar
49 char const * menu_tabstop = "aa";
50 char const * default_tabstop = "aaaaaaaa";
51 // We do not want to mix position values in a menu (like the index of
52 // a submenu) with the action numbers which convey actual information.
53 // Therefore we offset all the action values by an arbitrary large
55 int const action_offset = 1000;
57 // This is used a few times below.
59 int string_width(string const & str)
61 return fl_get_string_widthTAB(MENU_LABEL_STYLE, MENU_LABEL_SIZE,
63 static_cast<int>(str.length()));
71 //Defined later, used in makeMenubar().
73 void C_Menubar_Pimpl_MenuCallback(FL_OBJECT * ob, long button)
75 Menubar::Pimpl::MenuCallback(ob, button);
81 Menubar::Pimpl::Pimpl(LyXView * view, MenuBackend const & mb)
82 : owner_(static_cast<XFormsView*>(view)), menubackend_(&mb)
84 makeMenubar(menubackend_->getMenubar());
88 Menubar::Pimpl::~Pimpl()
92 void Menubar::Pimpl::makeMenubar(Menu const & menu)
94 FL_FORM * form = owner_->getForm();
97 // Create menu frame if there is non yet.
98 FL_OBJECT * frame = fl_add_frame(FL_UP_FRAME, 0, 0,
99 form->w, mheight, "");
100 fl_set_object_resize(frame, FL_RESIZE_ALL);
101 fl_set_object_gravity(frame, NorthWestGravity,
104 Menu::const_iterator i = menu.begin();
105 Menu::const_iterator end = menu.end();
106 for (; i != end; ++i) {
108 if (i->kind() != MenuItem::Submenu) {
109 lyxerr << "ERROR: Menubar::Pimpl::createMenubar:"
110 " only submenus can appear in a menubar"
114 string const label = i->label();
115 string const shortcut = '#' + i->shortcut();
116 int const width = string_width(label);
117 obj = fl_add_button(FL_MENU_BUTTON,
122 fl_set_object_boxtype(obj, FL_FLAT_BOX);
123 fl_set_object_color(obj, FL_MCOL, FL_MCOL);
124 fl_set_object_lsize(obj, MENU_LABEL_SIZE);
125 fl_set_object_lstyle(obj, MENU_LABEL_STYLE);
126 fl_set_object_resize(obj, FL_RESIZE_ALL);
127 fl_set_object_gravity(obj, NorthWestGravity,
129 moffset += obj->w + air;
130 fl_set_object_shortcut(obj, shortcut.c_str(), 1);
131 fl_set_object_callback(obj, C_Menubar_Pimpl_MenuCallback, 1);
133 boost::shared_ptr<ItemInfo>
134 iteminfo(new ItemInfo(this, new MenuItem(*i), obj));
135 buttonlist_.push_back(iteminfo);
136 obj->u_vdata = iteminfo.get();
142 void Menubar::Pimpl::update()
148 void Menubar::Pimpl::openByName(string const & name)
150 for (ButtonList::const_iterator cit = buttonlist_.begin();
151 cit != buttonlist_.end(); ++cit) {
152 if ((*cit)->item_->submenuname() == name) {
153 MenuCallback((*cit)->obj_, 1);
158 lyxerr << "Menubar::Pimpl::openByName: menu "
159 << name << " not found" << endl;
165 Menu::size_type const max_number_of_items = 25;
167 int get_new_submenu(vector<int> & smn, Window win)
169 static size_type max_number_of_menus = 32;
170 if (smn.size() >= max_number_of_menus)
171 max_number_of_menus =
172 fl_setpup_maxpup(static_cast<int>(2*smn.size()));
173 int menu = fl_newpup(win);
174 fl_setpup_softedge(menu, true);
175 fl_setpup_bw(menu, -1);
181 string const fixlabel(string const & str)
183 return subst(str, "%", "%%");
192 int Menubar::Pimpl::create_submenu(Window win, XFormsView * view,
193 Menu const & menu, vector<int> & smn)
195 const int menuid = get_new_submenu(smn, win);
196 lyxerr[Debug::GUI] << "Menubar::Pimpl::create_submenu: creating "
197 << menu.name() << " as menuid=" << menuid << endl;
199 // Compute the size of the largest label (because xforms is
200 // not able to support shortcuts correctly...)
203 Menu::const_iterator end = menu.end();
204 for (Menu::const_iterator i = menu.begin(); i != end; ++i) {
205 MenuItem const & item = (*i);
206 if (item.kind() == MenuItem::Command) {
207 string const label = item.label() + '\t';
208 int const width = string_width(label);
209 if (width > max_width) {
211 widest_label = label;
215 lyxerr[Debug::GUI] << "max_width=" << max_width
216 << ", widest_label=\"" << widest_label
220 int curmenuid = menuid;
221 for (Menu::const_iterator i = menu.begin(); i != end; ++i) {
222 MenuItem const & item = (*i);
225 // add a More... submenu if the menu is too long (but
226 // not just for one extra entry!)
227 if (count > max_number_of_items && (i+1) != end) {
228 int tmpmenuid = get_new_submenu(smn, win);
229 lyxerr[Debug::GUI] << "Too many items, creating "
230 << "new menu " << tmpmenuid << endl;
231 string label = _("More");
233 fl_addtopup(curmenuid, label.c_str(), tmpmenuid);
235 curmenuid = tmpmenuid;
238 switch (item.kind()) {
239 case MenuItem::Command:
240 case MenuItem::Submenu:
242 // Build the menu label from all the info
243 string label = fixlabel(item.label());
244 FuncStatus const flag = item.status();
247 // Is there a key binding?
248 string const binding = item.binding();
249 if (!binding.empty()) {
250 // Try to be clever and add just enough
251 // tabs to align shortcuts.
254 while (string_width(label) < max_width + 5);
258 // Is there a separator after the item?
260 && (i + 1)->kind() == MenuItem::Separator)
263 // Modify the entry using the function status
264 if (flag.onoff(true))
266 if (flag.onoff(false))
272 string shortcut = item.shortcut();
273 if (!shortcut.empty()) {
274 shortcut += lowercase(shortcut[0]);
278 // Finally add the action/submenu
279 if (item.kind() == MenuItem::Submenu) {
280 // create the submenu
282 create_submenu(win, view,
283 *item.submenu(), smn);
286 label += "%x" + tostr(smn.size());
288 << "Menu: " << submenuid
289 << " (at index " << smn.size()
293 label += "%x" + tostr(item.action()
295 lyxerr[Debug::GUI] << "Action: \""
296 << item.action() << "\", ";
299 // Add everything to the menu
300 fl_addtopup(curmenuid, label.c_str(),
302 if (item.kind() == MenuItem::Submenu)
303 fl_setpup_submenu(curmenuid, smn.size(),
306 lyxerr[Debug::GUI] << "label \"" << label
307 << "\", binding \"" << binding
308 << "\", shortcut \"" << shortcut
309 << "\" (added to menu "
310 << curmenuid << ')' << endl;
314 case MenuItem::Separator:
315 // already done, and if it was the first one,
316 // we just ignore it.
322 lyxerr << "Menubar::Pimpl::create_submenu: "
323 "this should not happen" << endl;
331 void Menubar::Pimpl::MenuCallback(FL_OBJECT * ob, long button)
333 ItemInfo * iteminfo = static_cast<ItemInfo *>(ob->u_vdata);
334 XFormsView * view = iteminfo->pimpl_->owner_;
335 MenuItem const * item = iteminfo->item_.get();
338 // set the pseudo menu-button
339 fl_set_object_boxtype(ob, FL_DOWN_BOX);
340 fl_set_button(ob, 0);
341 fl_redraw_object(ob);
345 lyx::Assert(item->kind() == MenuItem::Submenu);
347 // set tabstop length
348 fl_set_tabstop(menu_tabstop);
350 MenuBackend const * menubackend_ = iteminfo->pimpl_->menubackend_;
352 Menu const frommenu = menubackend_->getMenu(item->submenuname());
353 menubackend_->expand(frommenu, tomenu, view);
354 vector<int> submenus;
355 int menu = iteminfo->pimpl_->create_submenu(FL_ObjWin(ob), view,
359 fl_setpup_position(view->getForm()->x + ob->x,
360 view->getForm()->y + ob->y + ob->h + 10);
361 int choice = fl_dopup(menu);
364 // set the pseudo menu-button back
365 fl_set_object_boxtype(ob, FL_FLAT_BOX);
366 fl_redraw_object(ob);
369 // If the action value is too low, then it is not a
370 // valid action, but something else.
371 if (choice >= action_offset + 1) {
372 view->getLyXFunc().dispatch(choice - action_offset, true);
375 << "MenuCallback: ignoring bogus action "
379 lyxerr << "Error in MenuCallback" << endl;
382 for_each(submenus.begin(), submenus.end(), fl_freepup);
383 // restore tabstop length
384 fl_set_tabstop(default_tabstop);
389 Menubar::Pimpl::ItemInfo::ItemInfo
390 (Menubar::Pimpl * p, MenuItem const * i, FL_OBJECT * o)
397 Menubar::Pimpl::ItemInfo::~ItemInfo()