2 * \file Menubar_pimpl.C
3 * Copyright 1999-2001 The LyX Team.
4 * See the file COPYING.
6 * \author Lars Gullik Bjønnes, larsbj@lyx.org
12 #pragma implementation
15 #include "Menubar_pimpl.h"
16 #include "MenuBackend.h"
17 #include "LyXAction.h"
20 #include "XFormsView.h"
22 #include "support/lstrings.h"
23 #include "support/LAssert.h"
26 #include FORMS_H_LOCATION
28 #include <boost/scoped_ptr.hpp>
38 typedef vector<int>::size_type size_type;
40 extern boost::scoped_ptr<kb_keymap> toplevel_keymap;
45 int const MENU_LABEL_SIZE = FL_NORMAL_SIZE;
46 int const MENU_LABEL_STYLE = FL_NORMAL_STYLE;
47 int const mheight = 30;
48 int const mbheight= 22;
49 // where to place the menubar?
50 int const yloc = (mheight - mbheight)/2; //air + bw;
51 int const mbadd = 20; // menu button add (to width)
52 // Some space between buttons on the menubar
54 char const * menu_tabstop = "aa";
55 char const * default_tabstop = "aaaaaaaa";
56 // We do not want to mix position values in a menu (like the index of
57 // a submenu) with the action numbers which convey actual information.
58 // Therefore we offset all the action values by an arbitrary large
60 int const action_offset = 1000;
62 // This is used a few times below.
64 int string_width(string const & str)
66 return fl_get_string_widthTAB(MENU_LABEL_STYLE, MENU_LABEL_SIZE,
68 static_cast<int>(str.length()));
76 //Defined later, used in makeMenubar().
78 void C_Menubar_Pimpl_MenuCallback(FL_OBJECT * ob, long button)
80 Menubar::Pimpl::MenuCallback(ob, button);
86 Menubar::Pimpl::Pimpl(LyXView * view, MenuBackend const & mb)
87 : owner_(static_cast<XFormsView*>(view)), menubackend_(&mb)
89 makeMenubar(menubackend_->getMenubar());
93 Menubar::Pimpl::~Pimpl()
97 void Menubar::Pimpl::makeMenubar(Menu const & menu)
99 FL_FORM * form = owner_->getForm();
102 // Create menu frame if there is non yet.
103 FL_OBJECT * frame = fl_add_frame(FL_UP_FRAME, 0, 0,
104 form->w, mheight, "");
105 fl_set_object_resize(frame, FL_RESIZE_ALL);
106 fl_set_object_gravity(frame, NorthWestGravity,
109 Menu::const_iterator i = menu.begin();
110 Menu::const_iterator end = menu.end();
111 for (; i != end; ++i) {
113 if (i->kind() != MenuItem::Submenu) {
114 lyxerr << "ERROR: Menubar::Pimpl::createMenubar:"
115 " only submenus can appear in a menubar"
119 string const label = i->label();
120 string const shortcut = "#" + i->shortcut();
121 int const width = string_width(label);
122 obj = fl_add_button(FL_MENU_BUTTON,
127 fl_set_object_boxtype(obj, FL_FLAT_BOX);
128 fl_set_object_color(obj, FL_MCOL, FL_MCOL);
129 fl_set_object_lsize(obj, MENU_LABEL_SIZE);
130 fl_set_object_lstyle(obj, MENU_LABEL_STYLE);
131 fl_set_object_resize(obj, FL_RESIZE_ALL);
132 fl_set_object_gravity(obj, NorthWestGravity,
134 moffset += obj->w + air;
135 fl_set_object_shortcut(obj, shortcut.c_str(), 1);
136 fl_set_object_callback(obj, C_Menubar_Pimpl_MenuCallback, 1);
138 boost::shared_ptr<ItemInfo>
139 iteminfo(new ItemInfo(this, new MenuItem(*i), obj));
140 buttonlist_.push_back(iteminfo);
141 obj->u_vdata = iteminfo.get();
147 void Menubar::Pimpl::update()
153 void Menubar::Pimpl::openByName(string const & name)
155 for (ButtonList::const_iterator cit = buttonlist_.begin();
156 cit != buttonlist_.end(); ++cit) {
157 if ((*cit)->item_->submenuname() == name) {
158 MenuCallback((*cit)->obj_, 1);
163 lyxerr << "Menubar::Pimpl::openByName: menu "
164 << name << " not found" << endl;
170 Menu::size_type const max_number_of_items = 25;
172 int get_new_submenu(vector<int> & smn, Window win)
174 static size_type max_number_of_menus = 32;
175 if (smn.size() >= max_number_of_menus)
176 max_number_of_menus =
177 fl_setpup_maxpup(static_cast<int>(2*smn.size()));
178 int menu = fl_newpup(win);
179 fl_setpup_softedge(menu, true);
180 fl_setpup_bw(menu, -1);
181 lyxerr[Debug::GUI] << "Adding menu " << menu
182 << " in deletion list" << endl;
188 string const fixlabel(string const & str)
190 #if FL_VERSION < 1 && FL_REVISION < 89
191 return subst(str, '%', '?');
193 return subst(str, "%", "%%");
203 int Menubar::Pimpl::create_submenu(Window win, XFormsView * view,
204 Menu const & menu, vector<int> & smn,
207 const int menuid = get_new_submenu(smn, win);
208 lyxerr[Debug::GUI] << "Menubar::Pimpl::create_submenu: creating "
209 << menu.name() << " as menuid=" << menuid << endl;
211 // Compute the size of the largest label (because xforms is
212 // not able to support shortcuts correctly...)
215 Menu::const_iterator end = menu.end();
216 for (Menu::const_iterator i = menu.begin(); i != end; ++i) {
217 MenuItem const & item = (*i);
218 if (item.kind() == MenuItem::Command) {
219 string const label = item.label() + '\t';
220 int const width = string_width(label);
221 if (width > max_width) {
223 widest_label = label;
227 lyxerr[Debug::GUI] << "max_width=" << max_width
228 << ", widest_label=`" << widest_label
231 // Compute where to put separators
232 vector<string> extra_labels(menu.size());
233 vector<string>::iterator it = extra_labels.begin();
234 vector<string>::iterator last = it;
235 for (Menu::const_iterator i = menu.begin(); i != end; ++i, ++it)
236 if (i->kind() == MenuItem::Separator)
238 else if (!i->optional() ||
239 !(view->getLyXFunc().getStatus(i->action()).disabled()))
242 it = extra_labels.begin();
245 int curmenuid = menuid;
246 for (Menu::const_iterator i = menu.begin(); i != end; ++i, ++it) {
247 MenuItem const & item = (*i);
248 string & extra_label = *it;
251 if (count > max_number_of_items) {
252 int tmpmenuid = get_new_submenu(smn, win);
253 lyxerr[Debug::GUI] << "Too many items, creating "
254 << "new menu " << tmpmenuid << endl;
255 string label = _("More");
257 fl_addtopup(curmenuid, label.c_str(), tmpmenuid);
259 curmenuid = tmpmenuid;
262 switch (item.kind()) {
263 case MenuItem::Command: {
264 FuncStatus const flag =
265 view->getLyXFunc().getStatus(item.action());
266 // handle optional entries.
268 && (flag.disabled())) {
270 << "Skipping optional item "
271 << item.label() << endl;
275 // Get the keys bound to this action, but keep only the
278 toplevel_keymap->findbinding(item.action());
279 // Build the menu label from all the info
280 string label = fixlabel(item.label());
282 if (!accel.empty()) {
283 // Try to be clever and add just enough
284 // tabs to align shortcuts.
287 while (string_width(label) < max_width + 5);
288 label += accel.substr(1,accel.find(']') - 1);
290 label += "%x" + tostr(item.action() + action_offset)
293 // Modify the entry using the function status
295 if (flag.onoff(true))
297 if (flag.onoff(false))
299 if (flag.disabled() || flag.unknown())
302 all_disabled = false;
305 // Finally the menu shortcut
306 string shortcut = item.shortcut();
308 if (!shortcut.empty()) {
309 shortcut += lowercase(shortcut[0]);
311 fl_addtopup(curmenuid, label.c_str(),
314 fl_addtopup(curmenuid, label.c_str());
316 lyxerr[Debug::GUI] << "Command: \""
317 << lyxaction.getActionName(item.action())
318 << "\", binding \"" << accel
319 << "\", shortcut \"" << shortcut
320 << "\" (added to menu"
321 << curmenuid << ")" << endl;
325 case MenuItem::Submenu: {
326 bool sub_all_disabled;
327 int submenuid = create_submenu(win, view,
328 *item.submenu(), smn,
330 all_disabled &= sub_all_disabled;
333 string label = fixlabel(item.label());
334 label += extra_label + "%m";
335 if (sub_all_disabled)
337 string shortcut = item.shortcut();
338 if (!shortcut.empty()) {
339 shortcut += lowercase(shortcut[0]);
341 fl_addtopup(curmenuid, label.c_str(),
342 submenuid, shortcut.c_str());
344 fl_addtopup(curmenuid, label.c_str(),
350 case MenuItem::Separator:
351 // already done, and if it was the first one,
352 // we just ignore it.
357 lyxerr << "Menubar::Pimpl::create_submenu: "
358 "this should not happen" << endl;
366 void Menubar::Pimpl::MenuCallback(FL_OBJECT * ob, long button)
368 ItemInfo * iteminfo = static_cast<ItemInfo *>(ob->u_vdata);
369 XFormsView * view = iteminfo->pimpl_->owner_;
370 MenuItem const * item = iteminfo->item_.get();
373 // set the pseudo menu-button
374 fl_set_object_boxtype(ob, FL_DOWN_BOX);
375 fl_set_button(ob, 0);
376 fl_redraw_object(ob);
380 lyx::Assert(item->kind() == MenuItem::Submenu);
382 // set tabstop length
383 fl_set_tabstop(menu_tabstop);
385 MenuBackend const * menubackend_ = iteminfo->pimpl_->menubackend_;
387 Menu const frommenu = menubackend_->getMenu(item->submenuname());
388 menubackend_->expand(frommenu, tomenu, view->buffer());
389 vector<int> submenus;
390 bool all_disabled = true;
391 int menu = iteminfo->pimpl_->
392 create_submenu(FL_ObjWin(ob), view, tomenu,
393 submenus, all_disabled);
396 fl_setpup_position(view->getForm()->x + ob->x,
397 view->getForm()->y + ob->y + ob->h + 10);
398 int choice = fl_dopup(menu);
401 // set the pseudo menu-button back
402 fl_set_object_boxtype(ob, FL_FLAT_BOX);
403 fl_redraw_object(ob);
406 // If the action value is too low, then it is not a
407 // valid action, but something else.
408 if (choice >= action_offset + 1) {
409 view->getLyXFunc().dispatch(choice - action_offset, true);
412 << "MenuCallback: ignoring bogus action "
416 lyxerr << "Error in MenuCallback" << endl;
419 for_each(submenus.begin(), submenus.end(), fl_freepup);
420 // restore tabstop length
421 fl_set_tabstop(default_tabstop);
426 Menubar::Pimpl::ItemInfo::ItemInfo
427 (Menubar::Pimpl * p, MenuItem const * i, FL_OBJECT * o)
434 Menubar::Pimpl::ItemInfo::~ItemInfo()