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_->submenu() == name) {
157 MenuCallback((*cit)->obj_, 1);
162 lyxerr << "Menubar::Pimpl::openByName: menu "
163 << name << " not found" << endl;
170 string const limit_string_length(string const & str)
172 string::size_type const max_item_length = 45;
174 if (str.size() > max_item_length)
175 return str.substr(0, max_item_length - 3) + "...";
181 int get_new_submenu(vector<int> & smn, Window win)
183 static size_type max_number_of_menus = 32;
184 if (smn.size() >= max_number_of_menus)
185 max_number_of_menus =
186 fl_setpup_maxpup(static_cast<int>(2*smn.size()));
187 int menu = fl_newpup(win);
193 size_type const max_number_of_items = 25;
196 string const fixlabel(string const & str)
198 #if FL_VERSION < 1 && FL_REVISION < 89
199 return subst(str, '%', '?');
201 return subst(str, "%", "%%");
207 void add_toc2(int menu, string const & extra_label,
208 vector<int> & smn, Window win,
209 toc::Toc const & toc_list,
210 size_type from, size_type to, int depth)
212 int shortcut_count = 0;
213 if (to - from <= max_number_of_items) {
214 for (size_type i = from; i < to; ++i) {
215 int const action = toc_list[i].action();
216 string label(4 * max(0, toc_list[i].depth - depth),' ');
217 label += fixlabel(toc_list[i].str);
218 label = limit_string_length(label);
219 label += "%x" + tostr(action + action_offset);
220 if (i == to - 1 && depth == 0)
221 label += extra_label;
222 if (toc_list[i].depth == depth
223 && ++shortcut_count <= 9) {
225 fl_addtopup(menu, label.c_str(),
226 tostr(shortcut_count).c_str());
228 fl_addtopup(menu, label.c_str());
231 size_type pos = from;
235 if (count > max_number_of_items) {
236 int menu2 = get_new_submenu(smn, win);
237 add_toc2(menu2, extra_label, smn, win,
238 toc_list, pos, to, depth);
239 string label = _("More");
242 label += extra_label;
243 fl_addtopup(menu, label.c_str(), menu2);
246 size_type new_pos = pos+1;
247 while (new_pos < to &&
248 toc_list[new_pos].depth > depth)
251 int const action = toc_list[pos].action();
252 string label(4 * max(0, toc_list[pos].depth - depth), ' ');
253 label += fixlabel(toc_list[pos].str);
254 label = limit_string_length(label);
255 if (new_pos == to && depth == 0)
256 label += extra_label;
258 if (toc_list[pos].depth == depth &&
259 ++shortcut_count <= 9)
260 shortcut = tostr(shortcut_count);
262 if (new_pos == pos + 1) {
263 label += "%x" + tostr(action + action_offset);
264 if (!shortcut.empty()) {
266 fl_addtopup(menu, label.c_str(),
269 fl_addtopup(menu, label.c_str());
271 int menu2 = get_new_submenu(smn, win);
272 add_toc2(menu2, extra_label, smn, win,
273 toc_list, pos, new_pos, depth+1);
275 if (!shortcut.empty()) {
277 fl_addtopup(menu, label.c_str(), menu2,
280 fl_addtopup(menu, label.c_str(), menu2);
290 void Menubar::Pimpl::add_toc(int menu, string const & extra_label,
291 vector<int> & smn, Window win)
293 if (!owner_->buffer())
295 toc::TocList toc_list = toc::getTocList(owner_->buffer());
296 toc::TocList::const_iterator cit = toc_list.begin();
297 toc::TocList::const_iterator end = toc_list.end();
298 for (; cit != end; ++cit) {
299 // Handle this elsewhere
300 if (cit->first == "TOC") continue;
302 // All the rest is for floats
303 int menu_first_sub = get_new_submenu(smn, win);
304 int menu_current = menu_first_sub;
305 toc::Toc::const_iterator ccit = cit->second.begin();
306 toc::Toc::const_iterator eend = cit->second.end();
308 for (; ccit != eend; ++ccit) {
310 if (count > max_number_of_items) {
311 int menu_tmp = get_new_submenu(smn, win);
312 string label = _("More");
314 fl_addtopup(menu_current, label.c_str(), menu_tmp);
316 menu_current = menu_tmp;
318 int const action = ccit->action();
319 string label = fixlabel(ccit->str);
320 label = limit_string_length(label);
321 label += "%x" + tostr(action + action_offset);
322 fl_addtopup(menu_current, label.c_str());
324 string const m = floatList[cit->first]->second.name() + "%m";
325 fl_addtopup(menu, m.c_str(), menu_first_sub);
330 cit = toc_list.find("TOC");
332 string const tmp = _("No Table of contents%i") + extra_label;
333 fl_addtopup(menu, tmp.c_str());
336 add_toc2(menu, extra_label, smn, win,
337 cit->second, 0, cit->second.size(), 0);
342 int Menubar::Pimpl::create_submenu(Window win, XFormsView * view,
343 string const & menu_name,
346 if (!menubackend_->hasMenu(menu_name)) {
347 lyxerr << "ERROR:create_submenu: Unknown menu `"
348 << menu_name << "'" << endl;
352 menubackend_->getMenu(menu_name).expand(md, owner_->buffer());
354 int const menu = get_new_submenu(smn, win);
355 fl_setpup_softedge(menu, true);
356 fl_setpup_bw(menu, -1);
357 lyxerr[Debug::GUI] << "Adding menu " << menu
358 << " in deletion list" << endl;
360 // Compute the size of the largest label (because xforms is
361 // not able to support shortcuts correctly...)
364 Menu::const_iterator end = md.end();
365 for (Menu::const_iterator i = md.begin(); i != end; ++i) {
366 MenuItem const & item = (*i);
367 if (item.kind() == MenuItem::Command) {
368 string const label = item.label() + '\t';
369 int const width = string_width(label);
370 if (width > max_width) {
372 widest_label = label;
376 lyxerr[Debug::GUI] << "max_width=" << max_width
377 << ", widest_label=`" << widest_label
380 // Compute where to put separators
381 vector<string> extra_labels(md.size());
382 vector<string>::iterator it = extra_labels.begin();
383 vector<string>::iterator last = it;
384 for (Menu::const_iterator i = md.begin(); i != end; ++i, ++it)
385 if (i->kind() == MenuItem::Separator)
387 else if (!i->optional() ||
388 !(view->getLyXFunc()->getStatus(i->action()).disabled()))
391 it = extra_labels.begin();
392 for (Menu::const_iterator i = md.begin(); i != end; ++i, ++it) {
393 MenuItem const & item = (*i);
394 string & extra_label = *it;
396 switch (item.kind()) {
397 case MenuItem::Command: {
398 FuncStatus const flag =
399 view->getLyXFunc()->getStatus(item.action());
400 // handle optional entries.
402 && (flag.disabled())) {
404 << "Skipping optional item "
405 << item.label() << endl;
409 // Get the keys bound to this action, but keep only the
411 string const accel = toplevel_keymap->findbinding(kb_action(item.action()));
412 // Build the menu label from all the info
413 string label = item.label();
415 if (!accel.empty()) {
416 // Try to be clever and add just enough
417 // tabs to align shortcuts.
420 while (string_width(label) < max_width + 5);
421 label += accel.substr(1,accel.find(']') - 1);
423 label += "%x" + tostr(item.action() + action_offset)
426 // Modify the entry using the function status
428 if (flag.onoff(true))
430 if (flag.onoff(false))
432 if (flag.disabled() || flag.unknown())
436 // Finally the menu shortcut
437 string shortcut = item.shortcut();
439 if (!shortcut.empty()) {
440 shortcut += lowercase(shortcut[0]);
442 fl_addtopup(menu, label.c_str(),
445 fl_addtopup(menu, label.c_str());
447 lyxerr[Debug::GUI] << "Command: \""
448 << lyxaction.getActionName(item.action())
449 << "\", binding \"" << accel
450 << "\", shortcut \"" << shortcut
455 case MenuItem::Submenu: {
456 int submenu = create_submenu(win, view,
457 item.submenu(), smn);
460 string label = item.label();
461 label += extra_label + "%m";
462 string shortcut = item.shortcut();
463 if (!shortcut.empty()) {
464 shortcut += lowercase(shortcut[0]);
466 fl_addtopup(menu, label.c_str(),
467 submenu, shortcut.c_str());
469 fl_addtopup(menu, label.c_str(), submenu);
474 case MenuItem::Separator:
475 // already done, and if it was the first one,
476 // we just ignore it.
480 add_toc(menu, extra_label, smn, win);
483 case MenuItem::Documents:
484 case MenuItem::Lastfiles:
485 case MenuItem::ViewFormats:
486 case MenuItem::UpdateFormats:
487 case MenuItem::ExportFormats:
488 case MenuItem::ImportFormats:
489 case MenuItem::FloatListInsert:
490 case MenuItem::FloatInsert:
491 lyxerr << "Menubar::Pimpl::create_submenu: "
492 "this should not happen" << endl;
501 void Menubar::Pimpl::MenuCallback(FL_OBJECT * ob, long button)
503 ItemInfo * iteminfo = static_cast<ItemInfo *>(ob->u_vdata);
504 // lyxerr << "MenuCallback: ItemInfo address=" << iteminfo
505 // << "Val=(pimpl_=" << iteminfo->pimpl_
506 // << ", item_=" << iteminfo->item_
507 // << ", obj_=" << iteminfo->obj_ << ")" <<endl;
509 XFormsView * view = iteminfo->pimpl_->owner_;
510 MenuItem const * item = iteminfo->item_.get();
513 // set the pseudo menu-button
514 fl_set_object_boxtype(ob, FL_DOWN_BOX);
515 fl_set_button(ob, 0);
516 fl_redraw_object(ob);
520 lyx::Assert(item->kind() == MenuItem::Submenu);
522 // set tabstop length
523 fl_set_tabstop(menu_tabstop);
524 vector<int> submenus;
525 int menu = iteminfo->pimpl_->
526 create_submenu(FL_ObjWin(ob), view,
527 item->submenu(), submenus);
530 fl_setpup_position(view->getForm()->x + ob->x,
531 view->getForm()->y + ob->y + ob->h + 10);
532 int choice = fl_dopup(menu);
535 // set the pseudo menu-button back
536 fl_set_object_boxtype(ob, FL_FLAT_BOX);
537 fl_redraw_object(ob);
540 // If the action value is too low, then it is not a
541 // valid action, but something else.
542 if (choice >= action_offset + 1) {
543 view->getLyXFunc()->dispatch(choice - action_offset, true);
546 << "MenuCallback: ignoring bogus action "
550 lyxerr << "Error in MenuCallback" << endl;
553 for_each(submenus.begin(), submenus.end(), fl_freepup);
554 // restore tabstop length
555 fl_set_tabstop(default_tabstop);