1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1999-2001 The LyX Team.
8 * ======================================================*/
15 #pragma implementation
18 #include "Menubar_pimpl.h"
19 #include "MenuBackend.h"
20 #include "LyXAction.h"
24 #include "XFormsView.h"
26 #include "FloatList.h"
27 #include "support/lstrings.h"
28 #include "support/LAssert.h"
38 typedef vector<int>::size_type size_type;
40 extern boost::scoped_ptr<kb_keymap> toplevel_keymap;
41 extern LyXAction lyxaction;
46 int const MENU_LABEL_SIZE = FL_NORMAL_SIZE;
47 int const MENU_LABEL_STYLE = FL_NORMAL_STYLE;
48 int const mheight = 30;
49 int const mbheight= 22;
50 // where to place the menubar?
51 int const yloc = (mheight - mbheight)/2; //air + bw;
52 int const mbadd = 20; // menu button add (to width)
53 // Some space between buttons on the menubar
55 char const * menu_tabstop = "aa";
56 char const * default_tabstop = "aaaaaaaa";
57 // We do not want to mix position values in a menu (like the index of
58 // a submenu) with the action numbers which convey actual information.
59 // Therefore we offset all the action values by an arbitrary large
61 int const action_offset = 1000;
63 // This is used a few times below.
65 int string_width(string const & str)
67 return fl_get_string_widthTAB(MENU_LABEL_STYLE, MENU_LABEL_SIZE,
69 static_cast<int>(str.length()));
77 //Defined later, used in makeMenubar().
79 void C_Menubar_Pimpl_MenuCallback(FL_OBJECT * ob, long button)
81 Menubar::Pimpl::MenuCallback(ob, button);
87 Menubar::Pimpl::Pimpl(LyXView * view, MenuBackend const & mb)
88 : owner_(view), menubackend_(&mb), current_group_(0)
90 for (MenuBackend::const_iterator menu = menubackend_->begin();
91 menu != menubackend_->end() ; ++menu) {
92 if (menu->menubar()) {
93 FL_OBJECT * group = fl_bgn_group();
96 fl_hide_object(group);
98 << "Menubar::Pimpl::Pimpl: "
99 << "creating and hiding group " << group
100 << " for menubar " << menu->name() << endl;
101 menubarmap_[menu->name()] = group;
107 void Menubar::Pimpl::makeMenubar(Menu const & menu)
109 FL_FORM * form = static_cast<XFormsView *>(owner_)->getForm();
112 // Create menu frame if there is non yet.
113 FL_OBJECT * frame = fl_add_frame(FL_UP_FRAME, 0, 0,
114 form->w, mheight, "");
115 fl_set_object_resize(frame, FL_RESIZE_ALL);
116 fl_set_object_gravity(frame, NorthWestGravity,
119 Menu::const_iterator i = menu.begin();
120 Menu::const_iterator end = menu.end();
121 for (; i != end; ++i) {
123 if (i->kind() != MenuItem::Submenu) {
124 lyxerr << "ERROR: Menubar::Pimpl::createMenubar:"
125 " only submenus can appear in a menubar"
129 string const label = i->label();
130 string const shortcut = "#" + i->shortcut();
131 int const width = string_width(label);
132 obj = fl_add_button(FL_MENU_BUTTON,
137 fl_set_object_boxtype(obj, FL_FLAT_BOX);
138 fl_set_object_color(obj, FL_MCOL, FL_MCOL);
139 fl_set_object_lsize(obj, MENU_LABEL_SIZE);
140 fl_set_object_lstyle(obj, MENU_LABEL_STYLE);
141 fl_set_object_resize(obj, FL_RESIZE_ALL);
142 fl_set_object_gravity(obj, NorthWestGravity,
144 moffset += obj->w + air;
145 fl_set_object_shortcut(obj, shortcut.c_str(), 1);
146 fl_set_object_callback(obj, C_Menubar_Pimpl_MenuCallback, 1);
148 boost::shared_ptr<ItemInfo>
149 iteminfo(new ItemInfo(this, new MenuItem(*i), obj));
150 buttonlist_.push_back(iteminfo);
151 obj->u_vdata = iteminfo.get();
157 void Menubar::Pimpl::set(string const & menu_name)
159 lyxerr[Debug::GUI] << "Entering Menubar::Pimpl::set "
160 << "for menu `" << menu_name << "'" << endl;
162 if (menu_name != current_menu_name_) {
163 MenubarMap::iterator mbit = menubarmap_.find(menu_name);
165 if (mbit == menubarmap_.end()) {
166 lyxerr << "ERROR:set: Unknown menu `" << menu_name
171 if (current_group_) {
172 lyxerr[Debug::GUI] << " hiding group "
173 << current_group_ << endl;
174 fl_hide_object(current_group_);
177 lyxerr[Debug::GUI] << " showing group "
178 << mbit->second << endl;
179 fl_show_object(mbit->second);
180 current_menu_name_ = menu_name;
181 current_group_ = mbit->second;
182 lyxerr[Debug::GUI] << "Menubar::Pimpl::set: Menubar set."
185 lyxerr [Debug::GUI] << "Menubar::Pimpl::set: Nothing to do."
190 void Menubar::Pimpl::openByName(string const & name)
192 if (menubackend_->getMenu(current_menu_name_).hasSubmenu(name)) {
193 for (ButtonList::const_iterator cit = buttonlist_.begin();
194 cit != buttonlist_.end(); ++cit) {
195 if ((*cit)->item_->submenu() == name) {
196 MenuCallback((*cit)->obj_, 1);
201 lyxerr << "Menubar::Pimpl::openByName: menu "
202 << name << " not found" << endl;
209 string const limit_string_length(string const & str)
211 string::size_type const max_item_length = 45;
213 if (str.size() > max_item_length)
214 return str.substr(0, max_item_length - 3) + "...";
220 int get_new_submenu(vector<int> & smn, Window win)
222 static size_type max_number_of_menus = 32;
223 if (smn.size() >= max_number_of_menus)
224 max_number_of_menus =
225 fl_setpup_maxpup(static_cast<int>(2*smn.size()));
226 int menu = fl_newpup(win);
232 size_type const max_number_of_items = 25;
235 string const fixlabel(string const & str)
238 return subst(str, '%', '?');
240 return subst(str, "%", "%%");
246 void add_toc2(int menu, string const & extra_label,
247 vector<int> & smn, Window win,
248 vector<Buffer::TocItem> const & toc_list,
249 size_type from, size_type to, int depth)
251 int shortcut_count = 0;
252 if (to - from <= max_number_of_items) {
253 for (size_type i = from; i < to; ++i) {
254 int const action = lyxaction.
255 getPseudoAction(LFUN_GOTO_PARAGRAPH,
256 tostr(toc_list[i].par->id()));
257 string label(4 * max(0, toc_list[i].depth - depth),' ');
258 label += fixlabel(toc_list[i].str);
259 label = limit_string_length(label);
260 label += "%x" + tostr(action + action_offset);
261 if (i == to - 1 && depth == 0)
262 label += extra_label;
263 if (toc_list[i].depth == depth
264 && ++shortcut_count <= 9) {
266 fl_addtopup(menu, label.c_str(),
267 tostr(shortcut_count).c_str());
269 fl_addtopup(menu, label.c_str());
272 size_type pos = from;
276 if (count > max_number_of_items) {
277 int menu2 = get_new_submenu(smn, win);
278 add_toc2(menu2, extra_label, smn, win,
279 toc_list, pos, to, depth);
280 string label = _("More");
283 label += extra_label;
284 fl_addtopup(menu, label.c_str(), menu2);
287 size_type new_pos = pos+1;
288 while (new_pos < to &&
289 toc_list[new_pos].depth > depth)
292 int const action = lyxaction.
293 getPseudoAction(LFUN_GOTO_PARAGRAPH,
294 tostr(toc_list[pos].par->id()));
295 string label(4 * max(0, toc_list[pos].depth - depth), ' ');
296 label += fixlabel(toc_list[pos].str);
297 label = limit_string_length(label);
298 if (new_pos == to && depth == 0)
299 label += extra_label;
301 if (toc_list[pos].depth == depth &&
302 ++shortcut_count <= 9)
303 shortcut = tostr(shortcut_count);
305 if (new_pos == pos + 1) {
306 label += "%x" + tostr(action + action_offset);
307 if (!shortcut.empty()) {
309 fl_addtopup(menu, label.c_str(),
312 fl_addtopup(menu, label.c_str());
314 int menu2 = get_new_submenu(smn, win);
315 add_toc2(menu2, extra_label, smn, win,
316 toc_list, pos, new_pos, depth+1);
318 if (!shortcut.empty()) {
320 fl_addtopup(menu, label.c_str(), menu2,
323 fl_addtopup(menu, label.c_str(), menu2);
332 void Menubar::Pimpl::add_toc(int menu, string const & extra_label,
333 vector<int> & smn, Window win)
335 Buffer::Lists toc_list = owner_->buffer()->getLists();
336 Buffer::Lists::const_iterator cit = toc_list.begin();
337 Buffer::Lists::const_iterator end = toc_list.end();
338 for (; cit != end; ++cit) {
339 // Handle this elsewhere
340 if (cit->first == "TOC") continue;
342 // All the rest is for floats
343 int menu_first_sub = get_new_submenu(smn, win);
344 int menu_current = menu_first_sub;
345 Buffer::SingleList::const_iterator ccit = cit->second.begin();
346 Buffer::SingleList::const_iterator eend = cit->second.end();
348 for (; ccit != eend; ++ccit) {
350 if (count > max_number_of_items) {
351 int menu_tmp = get_new_submenu(smn, win);
352 string label = _("More");
354 fl_addtopup(menu_current, label.c_str(), menu_tmp);
356 menu_current = menu_tmp;
360 .getPseudoAction(LFUN_GOTO_PARAGRAPH,
361 tostr(ccit->par->id()));
362 string label = fixlabel(ccit->str);
363 label = limit_string_length(label);
364 label += "%x" + tostr(action + action_offset);
365 fl_addtopup(menu_current, label.c_str());
367 string const m = floatList[cit->first]->second.name() + "%m";
368 fl_addtopup(menu, m.c_str(), menu_first_sub);
373 cit = toc_list.find("TOC");
375 string const tmp = _("No Table of contents%i") + extra_label;
376 fl_addtopup(menu, tmp.c_str());
379 add_toc2(menu, extra_label, smn, win,
380 cit->second, 0, cit->second.size(), 0);
385 int Menubar::Pimpl::create_submenu(Window win, LyXView * view,
386 string const & menu_name,
389 if (!menubackend_->hasMenu(menu_name)) {
390 lyxerr << "ERROR:create_submenu: Unknown menu `"
391 << menu_name << "'" << endl;
395 menubackend_->getMenu(menu_name).expand(md, owner_->buffer());
397 int const menu = get_new_submenu(smn, win);
398 fl_setpup_softedge(menu, true);
399 fl_setpup_bw(menu, -1);
400 lyxerr[Debug::GUI] << "Adding menu " << menu
401 << " in deletion list" << endl;
403 // Compute the size of the largest label (because xforms is
404 // not able to support shortcuts correctly...)
407 Menu::const_iterator end = md.end();
408 for (Menu::const_iterator i = md.begin(); i != end; ++i) {
409 MenuItem const & item = (*i);
410 if (item.kind() == MenuItem::Command) {
411 string const label = item.label() + '\t';
412 int const width = string_width(label);
413 if (width > max_width) {
415 widest_label = label;
419 lyxerr[Debug::GUI] << "max_width=" << max_width
420 << ", widest_label=`" << widest_label
423 // Compute where to put separators
424 vector<string> extra_labels(md.size());
425 vector<string>::iterator it = extra_labels.begin();
426 vector<string>::iterator last = it;
427 for (Menu::const_iterator i = md.begin(); i != end; ++i, ++it)
428 if (i->kind() == MenuItem::Separator)
430 else if (!i->optional() ||
431 !(view->getLyXFunc()->getStatus(i->action()).disabled()))
434 it = extra_labels.begin();
435 for (Menu::const_iterator i = md.begin(); i != end; ++i, ++it) {
436 MenuItem const & item = (*i);
437 string & extra_label = *it;
439 switch (item.kind()) {
440 case MenuItem::Command: {
441 FuncStatus const flag =
442 view->getLyXFunc()->getStatus(item.action());
443 // handle optional entries.
445 && (flag.disabled())) {
447 << "Skipping optional item "
448 << item.label() << endl;
452 // Get the keys bound to this action, but keep only the
454 string const accel = toplevel_keymap->findbinding(kb_action(item.action()));
455 // Build the menu label from all the info
456 string label = item.label();
458 if (!accel.empty()) {
459 // Try to be clever and add just enough
460 // tabs to align shortcuts.
463 while (string_width(label) < max_width + 5);
464 label += accel.substr(1,accel.find(']') - 1);
466 label += "%x" + tostr(item.action() + action_offset)
469 // Modify the entry using the function status
471 if (flag.onoff(true))
473 if (flag.onoff(false))
475 if (flag.disabled() || flag.unknown())
479 // Finally the menu shortcut
480 string shortcut = item.shortcut();
482 if (!shortcut.empty()) {
483 shortcut += lowercase(shortcut[0]);
485 fl_addtopup(menu, label.c_str(),
488 fl_addtopup(menu, label.c_str());
490 lyxerr[Debug::GUI] << "Command: \""
491 << lyxaction.getActionName(item.action())
492 << "\", binding \"" << accel
493 << "\", shortcut \"" << shortcut
498 case MenuItem::Submenu: {
499 int submenu = create_submenu(win, view,
500 item.submenu(), smn);
503 string label = item.label();
504 label += extra_label + "%m";
505 string shortcut = item.shortcut();
506 if (!shortcut.empty()) {
507 shortcut += lowercase(shortcut[0]);
509 fl_addtopup(menu, label.c_str(),
510 submenu, shortcut.c_str());
512 fl_addtopup(menu, label.c_str(), submenu);
517 case MenuItem::Separator:
518 // already done, and if it was the first one,
519 // we just ignore it.
523 add_toc(menu, extra_label, smn, win);
526 case MenuItem::Documents:
527 case MenuItem::Lastfiles:
528 case MenuItem::ViewFormats:
529 case MenuItem::UpdateFormats:
530 case MenuItem::ExportFormats:
531 case MenuItem::ImportFormats:
532 case MenuItem::FloatListInsert:
533 case MenuItem::FloatInsert:
534 lyxerr << "Menubar::Pimpl::create_submenu: "
535 "this should not happen" << endl;
544 void Menubar::Pimpl::MenuCallback(FL_OBJECT * ob, long button)
546 ItemInfo * iteminfo = static_cast<ItemInfo *>(ob->u_vdata);
547 // lyxerr << "MenuCallback: ItemInfo address=" << iteminfo
548 // << "Val=(pimpl_=" << iteminfo->pimpl_
549 // << ", item_=" << iteminfo->item_
550 // << ", obj_=" << iteminfo->obj_ << ")" <<endl;
552 LyXView * view = iteminfo->pimpl_->owner_;
553 MenuItem const * item = iteminfo->item_.get();
556 // set the pseudo menu-button
557 fl_set_object_boxtype(ob, FL_DOWN_BOX);
558 fl_set_button(ob, 0);
559 fl_redraw_object(ob);
563 lyx::Assert(item->kind() == MenuItem::Submenu);
565 // set tabstop length
566 fl_set_tabstop(menu_tabstop);
567 vector<int> submenus;
568 int menu = iteminfo->pimpl_->
569 create_submenu(FL_ObjWin(ob), view,
570 item->submenu(), submenus);
573 fl_setpup_position(static_cast<XFormsView *>(view)->getForm()->x + ob->x,
574 static_cast<XFormsView *>(view)->getForm()->y + ob->y + ob->h + 10);
575 int choice = fl_dopup(menu);
578 // set the pseudo menu-button back
579 fl_set_object_boxtype(ob, FL_FLAT_BOX);
580 fl_redraw_object(ob);
583 // If the action value is too low, then it is not a
584 // valid action, but something else.
585 if (choice >= action_offset + 1) {
586 view->getLyXFunc()->verboseDispatch(choice - action_offset, true);
589 << "MenuCallback: ignoring bogus action "
593 lyxerr << "Error in MenuCallback" << endl;
596 for_each(submenus.begin(), submenus.end(), fl_freepup);
597 // restore tabstop length
598 fl_set_tabstop(default_tabstop);