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/LAssert.h"
22 #include FORMS_H_LOCATION
24 //#include <boost/scoped_ptr.hpp>
34 typedef vector<int>::size_type size_type;
39 int const MENU_LABEL_SIZE = FL_NORMAL_SIZE;
40 int const MENU_LABEL_STYLE = FL_NORMAL_STYLE;
41 int const mheight = 30;
42 int const mbheight= 22;
43 // where to place the menubar?
44 int const yloc = (mheight - mbheight)/2; //air + bw;
45 int const mbadd = 20; // menu button add (to width)
46 // Some space between buttons on the menubar
48 char const * menu_tabstop = "aa";
49 char const * default_tabstop = "aaaaaaaa";
50 // We do not want to mix position values in a menu (like the index of
51 // a submenu) with the action numbers which convey actual information.
52 // Therefore we offset all the action values by an arbitrary large
54 int const action_offset = 1000;
56 // This is used a few times below.
58 int string_width(string const & str)
60 return fl_get_string_widthTAB(MENU_LABEL_STYLE, MENU_LABEL_SIZE,
62 static_cast<int>(str.length()));
70 //Defined later, used in makeMenubar().
72 void C_Menubar_Pimpl_MenuCallback(FL_OBJECT * ob, long button)
74 Menubar::Pimpl::MenuCallback(ob, button);
80 Menubar::Pimpl::Pimpl(LyXView * view, MenuBackend const & mb)
81 : owner_(static_cast<XFormsView*>(view)), menubackend_(&mb)
83 makeMenubar(menubackend_->getMenubar());
87 Menubar::Pimpl::~Pimpl()
91 void Menubar::Pimpl::makeMenubar(Menu const & menu)
93 FL_FORM * form = owner_->getForm();
96 // Create menu frame if there is non yet.
97 FL_OBJECT * frame = fl_add_frame(FL_UP_FRAME, 0, 0,
98 form->w, mheight, "");
99 fl_set_object_resize(frame, FL_RESIZE_ALL);
100 fl_set_object_gravity(frame, NorthWestGravity,
103 Menu::const_iterator i = menu.begin();
104 Menu::const_iterator end = menu.end();
105 for (; i != end; ++i) {
107 if (i->kind() != MenuItem::Submenu) {
108 lyxerr << "ERROR: Menubar::Pimpl::createMenubar:"
109 " only submenus can appear in a menubar"
113 string const label = i->label();
114 string const shortcut = '#' + i->shortcut();
115 int const width = string_width(label);
116 obj = fl_add_button(FL_MENU_BUTTON,
121 fl_set_object_boxtype(obj, FL_FLAT_BOX);
122 fl_set_object_color(obj, FL_MCOL, FL_MCOL);
123 fl_set_object_lsize(obj, MENU_LABEL_SIZE);
124 fl_set_object_lstyle(obj, MENU_LABEL_STYLE);
125 fl_set_object_resize(obj, FL_RESIZE_ALL);
126 fl_set_object_gravity(obj, NorthWestGravity,
128 moffset += obj->w + air;
129 fl_set_object_shortcut(obj, shortcut.c_str(), 1);
130 fl_set_object_callback(obj, C_Menubar_Pimpl_MenuCallback, 1);
132 boost::shared_ptr<ItemInfo>
133 iteminfo(new ItemInfo(this, new MenuItem(*i), obj));
134 buttonlist_.push_back(iteminfo);
135 obj->u_vdata = iteminfo.get();
141 void Menubar::Pimpl::update()
147 void Menubar::Pimpl::openByName(string const & name)
149 for (ButtonList::const_iterator cit = buttonlist_.begin();
150 cit != buttonlist_.end(); ++cit) {
151 if ((*cit)->item_->submenuname() == name) {
152 MenuCallback((*cit)->obj_, 1);
157 lyxerr << "Menubar::Pimpl::openByName: menu "
158 << name << " not found" << endl;
164 Menu::size_type const max_number_of_items = 25;
166 int get_new_submenu(vector<int> & smn, Window win)
168 static size_type max_number_of_menus = 32;
169 if (smn.size() >= max_number_of_menus)
170 max_number_of_menus =
171 fl_setpup_maxpup(static_cast<int>(2*smn.size()));
172 int menu = fl_newpup(win);
173 fl_setpup_softedge(menu, true);
174 fl_setpup_bw(menu, -1);
180 string const fixlabel(string const & str)
182 return subst(str, "%", "%%");
191 int Menubar::Pimpl::create_submenu(Window win, XFormsView * view,
192 Menu const & menu, vector<int> & smn)
194 const int menuid = get_new_submenu(smn, win);
195 lyxerr[Debug::GUI] << "Menubar::Pimpl::create_submenu: creating "
196 << menu.name() << " as menuid=" << menuid << endl;
198 // Compute the size of the largest label (because xforms is
199 // not able to support shortcuts correctly...)
202 Menu::const_iterator end = menu.end();
203 for (Menu::const_iterator i = menu.begin(); i != end; ++i) {
204 MenuItem const & item = (*i);
205 if (item.kind() == MenuItem::Command) {
206 string const label = item.label() + '\t';
207 int const width = string_width(label);
208 if (width > max_width) {
210 widest_label = label;
214 lyxerr[Debug::GUI] << "max_width=" << max_width
215 << ", widest_label=\"" << widest_label
219 int curmenuid = menuid;
220 for (Menu::const_iterator i = menu.begin(); i != end; ++i) {
221 MenuItem const & item = (*i);
224 // add a More... submenu if the menu is too long (but
225 // not just for one extra entry!)
226 if (count > max_number_of_items && (i+1) != end) {
227 int tmpmenuid = get_new_submenu(smn, win);
228 lyxerr[Debug::GUI] << "Too many items, creating "
229 << "new menu " << tmpmenuid << endl;
230 string label = _("More");
232 fl_addtopup(curmenuid, label.c_str(), tmpmenuid);
234 curmenuid = tmpmenuid;
237 switch (item.kind()) {
238 case MenuItem::Command:
239 case MenuItem::Submenu:
241 // Build the menu label from all the info
242 string label = fixlabel(item.label());
243 FuncStatus const flag = item.status();
246 // Is there a key binding?
247 string const binding = item.binding();
248 if (!binding.empty()) {
249 // Try to be clever and add just enough
250 // tabs to align shortcuts.
253 while (string_width(label) < max_width + 5);
257 // Is there a separator after the item?
259 && (i + 1)->kind() == MenuItem::Separator)
262 // Modify the entry using the function status
263 if (flag.onoff(true))
265 if (flag.onoff(false))
271 string shortcut = item.shortcut();
272 if (!shortcut.empty()) {
273 shortcut += lowercase(shortcut[0]);
277 // Finally add the action/submenu
278 if (item.kind() == MenuItem::Submenu) {
279 // create the submenu
281 create_submenu(win, view,
282 *item.submenu(), smn);
285 label += "%x" + tostr(smn.size());
287 << "Menu: " << submenuid
288 << " (at index " << smn.size()
292 label += "%x" + tostr(item.action()
294 lyxerr[Debug::GUI] << "Action: \""
295 << item.action() << "\", ";
298 // Add everything to the menu
299 fl_addtopup(curmenuid, label.c_str(),
301 if (item.kind() == MenuItem::Submenu)
302 fl_setpup_submenu(curmenuid, smn.size(),
305 lyxerr[Debug::GUI] << "label \"" << label
306 << "\", binding \"" << binding
307 << "\", shortcut \"" << shortcut
308 << "\" (added to menu "
309 << curmenuid << ')' << endl;
313 case MenuItem::Separator:
314 // already done, and if it was the first one,
315 // we just ignore it.
321 lyxerr << "Menubar::Pimpl::create_submenu: "
322 "this should not happen" << endl;
330 void Menubar::Pimpl::MenuCallback(FL_OBJECT * ob, long button)
332 ItemInfo * iteminfo = static_cast<ItemInfo *>(ob->u_vdata);
333 XFormsView * view = iteminfo->pimpl_->owner_;
334 MenuItem const * item = iteminfo->item_.get();
337 // set the pseudo menu-button
338 fl_set_object_boxtype(ob, FL_DOWN_BOX);
339 fl_set_button(ob, 0);
340 fl_redraw_object(ob);
344 lyx::Assert(item->kind() == MenuItem::Submenu);
346 // set tabstop length
347 fl_set_tabstop(menu_tabstop);
349 MenuBackend const * menubackend_ = iteminfo->pimpl_->menubackend_;
351 Menu const frommenu = menubackend_->getMenu(item->submenuname());
352 menubackend_->expand(frommenu, tomenu, view);
353 vector<int> submenus;
354 int menu = iteminfo->pimpl_->create_submenu(FL_ObjWin(ob), view,
358 fl_setpup_position(view->getForm()->x + ob->x,
359 view->getForm()->y + ob->y + ob->h + 10);
360 int choice = fl_dopup(menu);
363 // set the pseudo menu-button back
364 fl_set_object_boxtype(ob, FL_FLAT_BOX);
365 fl_redraw_object(ob);
368 // If the action value is too low, then it is not a
369 // valid action, but something else.
370 if (choice >= action_offset + 1) {
371 view->getLyXFunc().dispatch(choice - action_offset, true);
374 << "MenuCallback: ignoring bogus action "
378 lyxerr << "Error in MenuCallback" << endl;
381 for_each(submenus.begin(), submenus.end(), fl_freepup);
382 // restore tabstop length
383 fl_set_tabstop(default_tabstop);
388 Menubar::Pimpl::ItemInfo::ItemInfo
389 (Menubar::Pimpl * p, MenuItem const * i, FL_OBJECT * o)
396 Menubar::Pimpl::ItemInfo::~ItemInfo()