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 "FloatList.h"
23 #include "support/lstrings.h"
24 #include "support/LAssert.h"
28 #include FORMS_H_LOCATION
30 #include <boost/scoped_ptr.hpp>
40 typedef vector<int>::size_type size_type;
42 extern boost::scoped_ptr<kb_keymap> toplevel_keymap;
43 extern LyXAction lyxaction;
48 int const MENU_LABEL_SIZE = FL_NORMAL_SIZE;
49 int const MENU_LABEL_STYLE = FL_NORMAL_STYLE;
50 int const mheight = 30;
51 int const mbheight= 22;
52 // where to place the menubar?
53 int const yloc = (mheight - mbheight)/2; //air + bw;
54 int const mbadd = 20; // menu button add (to width)
55 // Some space between buttons on the menubar
57 char const * menu_tabstop = "aa";
58 char const * default_tabstop = "aaaaaaaa";
59 // We do not want to mix position values in a menu (like the index of
60 // a submenu) with the action numbers which convey actual information.
61 // Therefore we offset all the action values by an arbitrary large
63 int const action_offset = 1000;
65 // This is used a few times below.
67 int string_width(string const & str)
69 return fl_get_string_widthTAB(MENU_LABEL_STYLE, MENU_LABEL_SIZE,
71 static_cast<int>(str.length()));
79 //Defined later, used in makeMenubar().
81 void C_Menubar_Pimpl_MenuCallback(FL_OBJECT * ob, long button)
83 Menubar::Pimpl::MenuCallback(ob, button);
89 Menubar::Pimpl::Pimpl(LyXView * view, MenuBackend const & mb)
90 : owner_(static_cast<XFormsView*>(view)), menubackend_(&mb)
92 makeMenubar(menubackend_->getMenubar());
96 void Menubar::Pimpl::makeMenubar(Menu const & menu)
98 FL_FORM * form = owner_->getForm();
101 // Create menu frame if there is non yet.
102 FL_OBJECT * frame = fl_add_frame(FL_UP_FRAME, 0, 0,
103 form->w, mheight, "");
104 fl_set_object_resize(frame, FL_RESIZE_ALL);
105 fl_set_object_gravity(frame, NorthWestGravity,
108 Menu::const_iterator i = menu.begin();
109 Menu::const_iterator end = menu.end();
110 for (; i != end; ++i) {
112 if (i->kind() != MenuItem::Submenu) {
113 lyxerr << "ERROR: Menubar::Pimpl::createMenubar:"
114 " only submenus can appear in a menubar"
118 string const label = i->label();
119 string const shortcut = "#" + i->shortcut();
120 int const width = string_width(label);
121 obj = fl_add_button(FL_MENU_BUTTON,
126 fl_set_object_boxtype(obj, FL_FLAT_BOX);
127 fl_set_object_color(obj, FL_MCOL, FL_MCOL);
128 fl_set_object_lsize(obj, MENU_LABEL_SIZE);
129 fl_set_object_lstyle(obj, MENU_LABEL_STYLE);
130 fl_set_object_resize(obj, FL_RESIZE_ALL);
131 fl_set_object_gravity(obj, NorthWestGravity,
133 moffset += obj->w + air;
134 fl_set_object_shortcut(obj, shortcut.c_str(), 1);
135 fl_set_object_callback(obj, C_Menubar_Pimpl_MenuCallback, 1);
137 boost::shared_ptr<ItemInfo>
138 iteminfo(new ItemInfo(this, new MenuItem(*i), obj));
139 buttonlist_.push_back(iteminfo);
140 obj->u_vdata = iteminfo.get();
146 void Menubar::Pimpl::update()
152 void Menubar::Pimpl::openByName(string const & name)
154 for (ButtonList::const_iterator cit = buttonlist_.begin();
155 cit != buttonlist_.end(); ++cit) {
156 if ((*cit)->item_->submenuname() == name) {
157 MenuCallback((*cit)->obj_, 1);
162 lyxerr << "Menubar::Pimpl::openByName: menu "
163 << name << " not found" << endl;
169 Menu::size_type const max_number_of_items = 25;
171 int get_new_submenu(vector<int> & smn, Window win)
173 static size_type max_number_of_menus = 32;
174 if (smn.size() >= max_number_of_menus)
175 max_number_of_menus =
176 fl_setpup_maxpup(static_cast<int>(2*smn.size()));
177 int menu = fl_newpup(win);
178 fl_setpup_softedge(menu, true);
179 fl_setpup_bw(menu, -1);
180 lyxerr[Debug::GUI] << "Adding menu " << menu
181 << " in deletion list" << endl;
187 string const fixlabel(string const & str)
189 #if FL_VERSION < 1 && FL_REVISION < 89
190 return subst(str, '%', '?');
192 return subst(str, "%", "%%");
202 int Menubar::Pimpl::create_submenu(Window win, XFormsView * view,
203 Menu const & menu, vector<int> & smn)
205 const int menuid = get_new_submenu(smn, win);
206 lyxerr[Debug::GUI] << "Menubar::Pimpl::create_submenu: creating "
207 << menu.name() << " as menuid=" << menuid << endl;
209 // Compute the size of the largest label (because xforms is
210 // not able to support shortcuts correctly...)
213 Menu::const_iterator end = menu.end();
214 for (Menu::const_iterator i = menu.begin(); i != end; ++i) {
215 MenuItem const & item = (*i);
216 if (item.kind() == MenuItem::Command) {
217 string const label = item.label() + '\t';
218 int const width = string_width(label);
219 if (width > max_width) {
221 widest_label = label;
225 lyxerr[Debug::GUI] << "max_width=" << max_width
226 << ", widest_label=`" << widest_label
229 // Compute where to put separators
230 vector<string> extra_labels(menu.size());
231 vector<string>::iterator it = extra_labels.begin();
232 vector<string>::iterator last = it;
233 for (Menu::const_iterator i = menu.begin(); i != end; ++i, ++it)
234 if (i->kind() == MenuItem::Separator)
236 else if (!i->optional() ||
237 !(view->getLyXFunc()->getStatus(i->action()).disabled()))
240 it = extra_labels.begin();
242 int curmenuid = menuid;
243 for (Menu::const_iterator i = menu.begin(); i != end; ++i, ++it) {
244 MenuItem const & item = (*i);
245 string & extra_label = *it;
248 if (count > max_number_of_items) {
249 int tmpmenuid = get_new_submenu(smn, win);
250 lyxerr[Debug::GUI] << "Too many items, creating "
251 << "new menu " << tmpmenuid << endl;
252 string label = _("More");
254 fl_addtopup(curmenuid, label.c_str(), tmpmenuid);
256 curmenuid = tmpmenuid;
259 switch (item.kind()) {
260 case MenuItem::Command: {
261 FuncStatus const flag =
262 view->getLyXFunc()->getStatus(item.action());
263 // handle optional entries.
265 && (flag.disabled())) {
267 << "Skipping optional item "
268 << item.label() << endl;
272 // Get the keys bound to this action, but keep only the
275 toplevel_keymap->findbinding(item.action());
276 // Build the menu label from all the info
277 string label = fixlabel(item.label());
279 if (!accel.empty()) {
280 // Try to be clever and add just enough
281 // tabs to align shortcuts.
284 while (string_width(label) < max_width + 5);
285 label += accel.substr(1,accel.find(']') - 1);
287 label += "%x" + tostr(item.action() + action_offset)
290 // Modify the entry using the function status
292 if (flag.onoff(true))
294 if (flag.onoff(false))
296 if (flag.disabled() || flag.unknown())
300 // Finally the menu shortcut
301 string shortcut = item.shortcut();
303 if (!shortcut.empty()) {
304 shortcut += lowercase(shortcut[0]);
306 fl_addtopup(curmenuid, label.c_str(),
309 fl_addtopup(curmenuid, label.c_str());
311 lyxerr[Debug::GUI] << "Command: \""
312 << lyxaction.getActionName(item.action())
313 << "\", binding \"" << accel
314 << "\", shortcut \"" << shortcut
315 << "\" (added to menu"
316 << curmenuid << ")" << endl;
320 case MenuItem::Submenu: {
321 int submenuid = create_submenu(win, view,
322 *item.submenu(), smn);
325 string label = fixlabel(item.label());
326 label += extra_label + "%m";
327 string shortcut = item.shortcut();
328 if (!shortcut.empty()) {
329 shortcut += lowercase(shortcut[0]);
331 fl_addtopup(curmenuid, label.c_str(),
332 submenuid, shortcut.c_str());
334 fl_addtopup(curmenuid, label.c_str(),
340 case MenuItem::Separator:
341 // already done, and if it was the first one,
342 // we just ignore it.
347 lyxerr << "Menubar::Pimpl::create_submenu: "
348 "this should not happen" << endl;
356 void Menubar::Pimpl::MenuCallback(FL_OBJECT * ob, long button)
358 ItemInfo * iteminfo = static_cast<ItemInfo *>(ob->u_vdata);
359 XFormsView * view = iteminfo->pimpl_->owner_;
360 MenuItem const * item = iteminfo->item_.get();
363 // set the pseudo menu-button
364 fl_set_object_boxtype(ob, FL_DOWN_BOX);
365 fl_set_button(ob, 0);
366 fl_redraw_object(ob);
370 lyx::Assert(item->kind() == MenuItem::Submenu);
372 // set tabstop length
373 fl_set_tabstop(menu_tabstop);
375 MenuBackend const * menubackend_ = iteminfo->pimpl_->menubackend_;
377 Menu const frommenu = menubackend_->getMenu(item->submenuname());
378 menubackend_->expand(frommenu, tomenu, view->buffer());
379 vector<int> submenus;
380 int menu = iteminfo->pimpl_->
381 create_submenu(FL_ObjWin(ob), view, tomenu, submenus);
384 fl_setpup_position(view->getForm()->x + ob->x,
385 view->getForm()->y + ob->y + ob->h + 10);
386 int choice = fl_dopup(menu);
389 // set the pseudo menu-button back
390 fl_set_object_boxtype(ob, FL_FLAT_BOX);
391 fl_redraw_object(ob);
394 // If the action value is too low, then it is not a
395 // valid action, but something else.
396 if (choice >= action_offset + 1) {
397 view->getLyXFunc()->dispatch(choice - action_offset, true);
400 << "MenuCallback: ignoring bogus action "
404 lyxerr << "Error in MenuCallback" << endl;
407 for_each(submenus.begin(), submenus.end(), fl_freepup);
408 // restore tabstop length
409 fl_set_tabstop(default_tabstop);