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"
21 #include "XFormsView.h"
23 #include "FloatList.h"
24 #include "support/lstrings.h"
25 #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 vector<Buffer::TocItem> 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 = lyxaction.
216 getPseudoAction(LFUN_GOTO_PARAGRAPH,
217 tostr(toc_list[i].par->id()));
218 string label(4 * max(0, toc_list[i].depth - depth),' ');
219 label += fixlabel(toc_list[i].str);
220 label = limit_string_length(label);
221 label += "%x" + tostr(action + action_offset);
222 if (i == to - 1 && depth == 0)
223 label += extra_label;
224 if (toc_list[i].depth == depth
225 && ++shortcut_count <= 9) {
227 fl_addtopup(menu, label.c_str(),
228 tostr(shortcut_count).c_str());
230 fl_addtopup(menu, label.c_str());
233 size_type pos = from;
237 if (count > max_number_of_items) {
238 int menu2 = get_new_submenu(smn, win);
239 add_toc2(menu2, extra_label, smn, win,
240 toc_list, pos, to, depth);
241 string label = _("More");
244 label += extra_label;
245 fl_addtopup(menu, label.c_str(), menu2);
248 size_type new_pos = pos+1;
249 while (new_pos < to &&
250 toc_list[new_pos].depth > depth)
253 int const action = lyxaction.
254 getPseudoAction(LFUN_GOTO_PARAGRAPH,
255 tostr(toc_list[pos].par->id()));
256 string label(4 * max(0, toc_list[pos].depth - depth), ' ');
257 label += fixlabel(toc_list[pos].str);
258 label = limit_string_length(label);
259 if (new_pos == to && depth == 0)
260 label += extra_label;
262 if (toc_list[pos].depth == depth &&
263 ++shortcut_count <= 9)
264 shortcut = tostr(shortcut_count);
266 if (new_pos == pos + 1) {
267 label += "%x" + tostr(action + action_offset);
268 if (!shortcut.empty()) {
270 fl_addtopup(menu, label.c_str(),
273 fl_addtopup(menu, label.c_str());
275 int menu2 = get_new_submenu(smn, win);
276 add_toc2(menu2, extra_label, smn, win,
277 toc_list, pos, new_pos, depth+1);
279 if (!shortcut.empty()) {
281 fl_addtopup(menu, label.c_str(), menu2,
284 fl_addtopup(menu, label.c_str(), menu2);
294 void Menubar::Pimpl::add_toc(int menu, string const & extra_label,
295 vector<int> & smn, Window win)
297 if (!owner_->buffer())
299 Buffer::Lists toc_list = owner_->buffer()->getLists();
300 Buffer::Lists::const_iterator cit = toc_list.begin();
301 Buffer::Lists::const_iterator end = toc_list.end();
302 for (; cit != end; ++cit) {
303 // Handle this elsewhere
304 if (cit->first == "TOC") continue;
306 // All the rest is for floats
307 int menu_first_sub = get_new_submenu(smn, win);
308 int menu_current = menu_first_sub;
309 Buffer::SingleList::const_iterator ccit = cit->second.begin();
310 Buffer::SingleList::const_iterator eend = cit->second.end();
312 for (; ccit != eend; ++ccit) {
314 if (count > max_number_of_items) {
315 int menu_tmp = get_new_submenu(smn, win);
316 string label = _("More");
318 fl_addtopup(menu_current, label.c_str(), menu_tmp);
320 menu_current = menu_tmp;
324 .getPseudoAction(LFUN_GOTO_PARAGRAPH,
325 tostr(ccit->par->id()));
326 string label = fixlabel(ccit->str);
327 label = limit_string_length(label);
328 label += "%x" + tostr(action + action_offset);
329 fl_addtopup(menu_current, label.c_str());
331 string const m = floatList[cit->first]->second.name() + "%m";
332 fl_addtopup(menu, m.c_str(), menu_first_sub);
337 cit = toc_list.find("TOC");
339 string const tmp = _("No Table of contents%i") + extra_label;
340 fl_addtopup(menu, tmp.c_str());
343 add_toc2(menu, extra_label, smn, win,
344 cit->second, 0, cit->second.size(), 0);
349 int Menubar::Pimpl::create_submenu(Window win, XFormsView * view,
350 string const & menu_name,
353 if (!menubackend_->hasMenu(menu_name)) {
354 lyxerr << "ERROR:create_submenu: Unknown menu `"
355 << menu_name << "'" << endl;
359 menubackend_->getMenu(menu_name).expand(md, owner_->buffer());
361 int const menu = get_new_submenu(smn, win);
362 fl_setpup_softedge(menu, true);
363 fl_setpup_bw(menu, -1);
364 lyxerr[Debug::GUI] << "Adding menu " << menu
365 << " in deletion list" << endl;
367 // Compute the size of the largest label (because xforms is
368 // not able to support shortcuts correctly...)
371 Menu::const_iterator end = md.end();
372 for (Menu::const_iterator i = md.begin(); i != end; ++i) {
373 MenuItem const & item = (*i);
374 if (item.kind() == MenuItem::Command) {
375 string const label = item.label() + '\t';
376 int const width = string_width(label);
377 if (width > max_width) {
379 widest_label = label;
383 lyxerr[Debug::GUI] << "max_width=" << max_width
384 << ", widest_label=`" << widest_label
387 // Compute where to put separators
388 vector<string> extra_labels(md.size());
389 vector<string>::iterator it = extra_labels.begin();
390 vector<string>::iterator last = it;
391 for (Menu::const_iterator i = md.begin(); i != end; ++i, ++it)
392 if (i->kind() == MenuItem::Separator)
394 else if (!i->optional() ||
395 !(view->getLyXFunc()->getStatus(i->action()).disabled()))
398 it = extra_labels.begin();
399 for (Menu::const_iterator i = md.begin(); i != end; ++i, ++it) {
400 MenuItem const & item = (*i);
401 string & extra_label = *it;
403 switch (item.kind()) {
404 case MenuItem::Command: {
405 FuncStatus const flag =
406 view->getLyXFunc()->getStatus(item.action());
407 // handle optional entries.
409 && (flag.disabled())) {
411 << "Skipping optional item "
412 << item.label() << endl;
416 // Get the keys bound to this action, but keep only the
418 string const accel = toplevel_keymap->findbinding(kb_action(item.action()));
419 // Build the menu label from all the info
420 string label = item.label();
422 if (!accel.empty()) {
423 // Try to be clever and add just enough
424 // tabs to align shortcuts.
427 while (string_width(label) < max_width + 5);
428 label += accel.substr(1,accel.find(']') - 1);
430 label += "%x" + tostr(item.action() + action_offset)
433 // Modify the entry using the function status
435 if (flag.onoff(true))
437 if (flag.onoff(false))
439 if (flag.disabled() || flag.unknown())
443 // Finally the menu shortcut
444 string shortcut = item.shortcut();
446 if (!shortcut.empty()) {
447 shortcut += lowercase(shortcut[0]);
449 fl_addtopup(menu, label.c_str(),
452 fl_addtopup(menu, label.c_str());
454 lyxerr[Debug::GUI] << "Command: \""
455 << lyxaction.getActionName(item.action())
456 << "\", binding \"" << accel
457 << "\", shortcut \"" << shortcut
462 case MenuItem::Submenu: {
463 int submenu = create_submenu(win, view,
464 item.submenu(), smn);
467 string label = item.label();
468 label += extra_label + "%m";
469 string shortcut = item.shortcut();
470 if (!shortcut.empty()) {
471 shortcut += lowercase(shortcut[0]);
473 fl_addtopup(menu, label.c_str(),
474 submenu, shortcut.c_str());
476 fl_addtopup(menu, label.c_str(), submenu);
481 case MenuItem::Separator:
482 // already done, and if it was the first one,
483 // we just ignore it.
487 add_toc(menu, extra_label, smn, win);
490 case MenuItem::Documents:
491 case MenuItem::Lastfiles:
492 case MenuItem::ViewFormats:
493 case MenuItem::UpdateFormats:
494 case MenuItem::ExportFormats:
495 case MenuItem::ImportFormats:
496 case MenuItem::FloatListInsert:
497 case MenuItem::FloatInsert:
498 lyxerr << "Menubar::Pimpl::create_submenu: "
499 "this should not happen" << endl;
508 void Menubar::Pimpl::MenuCallback(FL_OBJECT * ob, long button)
510 ItemInfo * iteminfo = static_cast<ItemInfo *>(ob->u_vdata);
511 // lyxerr << "MenuCallback: ItemInfo address=" << iteminfo
512 // << "Val=(pimpl_=" << iteminfo->pimpl_
513 // << ", item_=" << iteminfo->item_
514 // << ", obj_=" << iteminfo->obj_ << ")" <<endl;
516 XFormsView * view = iteminfo->pimpl_->owner_;
517 MenuItem const * item = iteminfo->item_.get();
520 // set the pseudo menu-button
521 fl_set_object_boxtype(ob, FL_DOWN_BOX);
522 fl_set_button(ob, 0);
523 fl_redraw_object(ob);
527 lyx::Assert(item->kind() == MenuItem::Submenu);
529 // set tabstop length
530 fl_set_tabstop(menu_tabstop);
531 vector<int> submenus;
532 int menu = iteminfo->pimpl_->
533 create_submenu(FL_ObjWin(ob), view,
534 item->submenu(), submenus);
537 fl_setpup_position(view->getForm()->x + ob->x,
538 view->getForm()->y + ob->y + ob->h + 10);
539 int choice = fl_dopup(menu);
542 // set the pseudo menu-button back
543 fl_set_object_boxtype(ob, FL_FLAT_BOX);
544 fl_redraw_object(ob);
547 // If the action value is too low, then it is not a
548 // valid action, but something else.
549 if (choice >= action_offset + 1) {
550 view->getLyXFunc()->dispatch(choice - action_offset, true);
553 << "MenuCallback: ignoring bogus action "
557 lyxerr << "Error in MenuCallback" << endl;
560 for_each(submenus.begin(), submenus.end(), fl_freepup);
561 // restore tabstop length
562 fl_set_tabstop(default_tabstop);