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"
37 typedef vector<int>::size_type size_type;
39 extern boost::scoped_ptr<kb_keymap> toplevel_keymap;
40 extern LyXAction lyxaction;
45 int const MENU_LABEL_SIZE = FL_NORMAL_SIZE;
46 int const mheight = 30;
47 int const mbheight= 22;
48 // where to place the menubar?
49 int const yloc = (mheight - mbheight)/2; //air + bw;
50 int const mbadd = 20; // menu button add (to width)
51 // Some space between buttons on the menubar
53 char const * menu_tabstop = "aa";
54 char const * default_tabstop = "aaaaaaaa";
55 // We do not want to mix position values in a menu (like the index of
56 // a submenu) with the action numbers which convey actual information.
57 // Therefore we offset all the action values by an arbitrary large
59 int const action_offset = 1000;
61 // This is used a few times below.
63 int string_width(string const & str)
65 return fl_get_string_widthTAB(FL_NORMAL_STYLE, MENU_LABEL_SIZE,
67 static_cast<int>(str.length()));
73 //Defined later, used in makeMenubar().
75 void C_Menubar_Pimpl_MenuCallback(FL_OBJECT * ob, long button);
78 Menubar::Pimpl::Pimpl(LyXView * view, MenuBackend const & mb)
79 : owner_(view), menubackend_(&mb), current_group_(0)
81 for (MenuBackend::const_iterator menu = menubackend_->begin();
82 menu != menubackend_->end() ; ++menu) {
83 if (menu->menubar()) {
84 FL_OBJECT * group = fl_bgn_group();
87 fl_hide_object(group);
89 << "Menubar::Pimpl::Pimpl: "
90 << "creating and hiding group " << group
91 << " for menubar " << menu->name() << endl;
92 menubarmap_[menu->name()] = group;
98 void Menubar::Pimpl::makeMenubar(Menu const & menu)
100 FL_FORM * form = static_cast<XFormsView *>(owner_)->getForm();
103 // Create menu frame if there is non yet.
104 FL_OBJECT * frame = fl_add_frame(FL_UP_FRAME, 0, 0,
105 form->w, mheight, "");
106 fl_set_object_resize(frame, FL_RESIZE_ALL);
107 fl_set_object_gravity(frame, NorthWestGravity,
110 Menu::const_iterator i = menu.begin();
111 Menu::const_iterator end = menu.end();
112 for (; i != end; ++i) {
114 if (i->kind() != MenuItem::Submenu) {
115 lyxerr << "ERROR: Menubar::Pimpl::createMenubar:"
116 " 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, FL_NORMAL_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::set(string const & menu_name)
149 lyxerr[Debug::GUI] << "Entering Menubar::Pimpl::set "
150 << "for menu `" << menu_name << "'" << endl;
152 if (menu_name != current_menu_name_) {
153 MenubarMap::iterator mbit = menubarmap_.find(menu_name);
155 if (mbit == menubarmap_.end()) {
156 lyxerr << "ERROR:set: Unknown menu `" << menu_name
161 if (current_group_) {
162 lyxerr[Debug::GUI] << " hiding group "
163 << current_group_ << endl;
164 fl_hide_object(current_group_);
167 lyxerr[Debug::GUI] << " showing group "
168 << mbit->second << endl;
169 fl_show_object(mbit->second);
170 current_menu_name_ = menu_name;
171 current_group_ = mbit->second;
172 lyxerr[Debug::GUI] << "Menubar::Pimpl::set: Menubar set."
175 lyxerr [Debug::GUI] << "Menubar::Pimpl::set: Nothing to do."
180 void Menubar::Pimpl::openByName(string const & name)
182 if (menubackend_->getMenu(current_menu_name_).hasSubmenu(name)) {
183 for (ButtonList::const_iterator cit = buttonlist_.begin();
184 cit != buttonlist_.end(); ++cit) {
185 if ((*cit)->item_->submenu() == name) {
186 MenuCallback((*cit)->obj_, 1);
191 lyxerr << "Menubar::Pimpl::openByName: menu "
192 << name << " not found" << endl;
199 string const limit_string_length(string const & str)
201 string::size_type const max_item_length = 45;
203 if (str.size() > max_item_length)
204 return str.substr(0, max_item_length - 3) + "...";
210 int get_new_submenu(vector<int> & smn, Window win)
212 static size_type max_number_of_menus = 32;
213 if (smn.size() >= max_number_of_menus)
214 max_number_of_menus =
215 fl_setpup_maxpup(static_cast<int>(2*smn.size()));
216 int menu = fl_newpup(win);
222 size_type const max_number_of_items = 25;
225 string const fixlabel(string const & str)
228 return subst(str, '%', '?');
230 return subst(str, "%", "%%");
236 void add_toc2(int menu, string const & extra_label,
237 vector<int> & smn, Window win,
238 vector<Buffer::TocItem> const & toc_list,
239 size_type from, size_type to, int depth)
241 int shortcut_count = 0;
242 if (to - from <= max_number_of_items) {
243 for (size_type i = from; i < to; ++i) {
244 int const action = lyxaction.
245 getPseudoAction(LFUN_GOTO_PARAGRAPH,
246 tostr(toc_list[i].par->id()));
247 string label(4 * max(0, toc_list[i].depth - depth),' ');
248 label += fixlabel(toc_list[i].str);
249 label = limit_string_length(label);
250 label += "%x" + tostr(action + action_offset);
251 if (i == to - 1 && depth == 0)
252 label += extra_label;
253 if (toc_list[i].depth == depth
254 && ++shortcut_count <= 9) {
256 fl_addtopup(menu, label.c_str(),
257 tostr(shortcut_count).c_str());
259 fl_addtopup(menu, label.c_str());
262 size_type pos = from;
266 if (count > max_number_of_items) {
267 int menu2 = get_new_submenu(smn, win);
268 add_toc2(menu2, extra_label, smn, win,
269 toc_list, pos, to, depth);
270 string label = _("More");
273 label += extra_label;
274 fl_addtopup(menu, label.c_str(), menu2);
277 size_type new_pos = pos+1;
278 while (new_pos < to &&
279 toc_list[new_pos].depth > depth)
282 int const action = lyxaction.
283 getPseudoAction(LFUN_GOTO_PARAGRAPH,
284 tostr(toc_list[pos].par->id()));
285 string label(4 * max(0, toc_list[pos].depth - depth), ' ');
286 label += fixlabel(toc_list[pos].str);
287 label = limit_string_length(label);
288 if (new_pos == to && depth == 0)
289 label += extra_label;
291 if (toc_list[pos].depth == depth &&
292 ++shortcut_count <= 9)
293 shortcut = tostr(shortcut_count);
295 if (new_pos == pos + 1) {
296 label += "%x" + tostr(action + action_offset);
297 if (!shortcut.empty()) {
299 fl_addtopup(menu, label.c_str(),
302 fl_addtopup(menu, label.c_str());
304 int menu2 = get_new_submenu(smn, win);
305 add_toc2(menu2, extra_label, smn, win,
306 toc_list, pos, new_pos, depth+1);
308 if (!shortcut.empty()) {
310 fl_addtopup(menu, label.c_str(), menu2,
313 fl_addtopup(menu, label.c_str(), menu2);
322 void Menubar::Pimpl::add_toc(int menu, string const & extra_label,
323 vector<int> & smn, Window win)
325 Buffer::Lists toc_list = owner_->buffer()->getLists();
326 Buffer::Lists::const_iterator cit = toc_list.begin();
327 Buffer::Lists::const_iterator end = toc_list.end();
328 for (; cit != end; ++cit) {
329 // Handle this elsewhere
330 if (cit->first == "TOC") continue;
332 // All the rest is for floats
333 int menu2 = get_new_submenu(smn, win);
334 Buffer::SingleList::const_iterator ccit = cit->second.begin();
335 Buffer::SingleList::const_iterator eend = cit->second.end();
336 for (; ccit != eend; ++ccit) {
339 .getPseudoAction(LFUN_GOTO_PARAGRAPH,
340 tostr(ccit->par->id()));
341 string label = fixlabel(ccit->str);
342 label = limit_string_length(label);
343 label += "%x" + tostr(action + action_offset);
344 fl_addtopup(menu2, label.c_str());
346 string const m = floatList[cit->first]->second.name() + "%m";
347 fl_addtopup(menu, m.c_str(), menu2);
352 cit = toc_list.find("TOC");
354 string const tmp = _("No Table of contents%i") + extra_label;
355 fl_addtopup(menu, tmp.c_str());
358 add_toc2(menu, extra_label, smn, win,
359 cit->second, 0, cit->second.size(), 0);
364 int Menubar::Pimpl::create_submenu(Window win, LyXView * view,
365 string const & menu_name,
368 if (!menubackend_->hasMenu(menu_name)){
369 lyxerr << "ERROR:create_submenu: Unknown menu `"
370 << menu_name << "'" << endl;
374 menubackend_->getMenu(menu_name).expand(md, owner_->buffer());
376 int const menu = get_new_submenu(smn, win);
377 fl_setpup_softedge(menu, true);
378 fl_setpup_bw(menu, -1);
379 lyxerr[Debug::GUI] << "Adding menu " << menu
380 << " in deletion list" << endl;
382 // Compute the size of the largest label (because xforms is
383 // not able to support shortcuts correctly...)
386 Menu::const_iterator end = md.end();
387 for (Menu::const_iterator i = md.begin(); i != end; ++i) {
388 MenuItem const & item = (*i);
389 if (item.kind() == MenuItem::Command) {
390 string const label = item.label() + '\t';
391 int const width = string_width(label);
392 if (width > max_width) {
394 widest_label = label;
398 lyxerr[Debug::GUI] << "max_width=" << max_width
399 << ", widest_label=`" << widest_label
402 // Compute where to put separators
403 vector<string> extra_labels(md.size());
404 vector<string>::iterator it = extra_labels.begin();
405 vector<string>::iterator last = it;
406 for (Menu::const_iterator i = md.begin(); i != end; ++i, ++it)
407 if (i->kind() == MenuItem::Separator)
409 else if (!i->optional() ||
410 !(view->getLyXFunc()->getStatus(i->action())
411 & func_status::Disabled))
414 it = extra_labels.begin();
415 for (Menu::const_iterator i = md.begin(); i != end; ++i, ++it) {
416 MenuItem const & item = (*i);
417 string & extra_label = *it;
419 switch (item.kind()) {
420 case MenuItem::Command: {
421 func_status::value_type flag =
422 view->getLyXFunc()->getStatus(item.action());
424 // handle optional entries.
426 && (flag & func_status::Disabled)) {
428 << "Skipping optional item "
429 << item.label() << endl;
433 // Get the keys bound to this action, but keep only the
435 string const accel = toplevel_keymap->findbinding(item.action());
436 // Build the menu label from all the info
437 string label = item.label();
439 if (!accel.empty()) {
440 // Try to be clever and add just enough
441 // tabs to align shortcuts.
444 while (string_width(label) < max_width + 5);
445 label += accel.substr(1,accel.find(']') - 1);
447 label += "%x" + tostr(item.action() + action_offset)
450 // Modify the entry using the function status
452 if (flag & (func_status::Disabled
453 | func_status::Unknown))
455 if (flag & func_status::ToggleOn)
457 if (flag & func_status::ToggleOff)
461 // Finally the menu shortcut
462 string shortcut = item.shortcut();
464 if (!shortcut.empty()) {
465 shortcut += lowercase(shortcut[0]);
467 fl_addtopup(menu, label.c_str(),
470 fl_addtopup(menu, label.c_str());
472 lyxerr[Debug::GUI] << "Command: \""
473 << lyxaction.getActionName(item.action())
474 << "\", binding \"" << accel
475 << "\", shortcut \"" << shortcut
480 case MenuItem::Submenu: {
481 int submenu = create_submenu(win, view,
482 item.submenu(), smn);
485 string label = item.label();
486 label += extra_label + "%m";
487 string shortcut = item.shortcut();
488 if (!shortcut.empty()) {
489 shortcut += lowercase(shortcut[0]);
491 fl_addtopup(menu, label.c_str(),
492 submenu, shortcut.c_str());
494 fl_addtopup(menu, label.c_str(), submenu);
499 case MenuItem::Separator:
500 // already done, and if it was the first one,
501 // we just ignore it.
505 add_toc(menu, extra_label, smn, win);
508 case MenuItem::Documents:
509 case MenuItem::Lastfiles:
510 case MenuItem::ViewFormats:
511 case MenuItem::UpdateFormats:
512 case MenuItem::ExportFormats:
513 case MenuItem::ImportFormats:
514 case MenuItem::FloatListInsert:
515 case MenuItem::FloatInsert:
516 lyxerr << "Menubar::Pimpl::create_submenu: "
517 "this should not happen" << endl;
527 void C_Menubar_Pimpl_MenuCallback(FL_OBJECT * ob, long button)
529 Menubar::Pimpl::MenuCallback(ob, button);
533 void Menubar::Pimpl::MenuCallback(FL_OBJECT * ob, long button)
535 ItemInfo * iteminfo = static_cast<ItemInfo *>(ob->u_vdata);
536 // lyxerr << "MenuCallback: ItemInfo address=" << iteminfo
537 // << "Val=(pimpl_=" << iteminfo->pimpl_
538 // << ", item_=" << iteminfo->item_
539 // << ", obj_=" << iteminfo->obj_ << ")" <<endl;
541 LyXView * view = iteminfo->pimpl_->owner_;
542 MenuItem const * item = iteminfo->item_.get();
544 /* get the splash out of the way. It would be nicer
545 * to only have this code at the start, but xforms
546 * makes it too ugly to do
548 view->getDialogs()->destroySplash();
551 // set the pseudo menu-button
552 fl_set_object_boxtype(ob, FL_DOWN_BOX);
553 fl_set_button(ob, 0);
554 fl_redraw_object(ob);
558 lyx::Assert(item->kind() == MenuItem::Submenu);
560 // set tabstop length
561 fl_set_tabstop(menu_tabstop);
562 vector<int> submenus;
563 int menu = iteminfo->pimpl_->
564 create_submenu(FL_ObjWin(ob), view,
565 item->submenu(), submenus);
568 fl_setpup_position(static_cast<XFormsView *>(view)->getForm()->x + ob->x,
569 static_cast<XFormsView *>(view)->getForm()->y + ob->y + ob->h + 10);
570 int choice = fl_dopup(menu);
573 // set the pseudo menu-button back
574 fl_set_object_boxtype(ob, FL_FLAT_BOX);
575 fl_redraw_object(ob);
578 // If the action value is too low, then it is not a
579 // valid action, but something else.
580 if (choice >= action_offset + 1) {
581 view->getLyXFunc()->dispatch(choice - action_offset);
584 << "MenuCallback: ignoring bogus action "
588 lyxerr << "Error in MenuCallback" << endl;
591 std::for_each(submenus.begin(), submenus.end(), fl_freepup);
592 // restore tabstop length
593 fl_set_tabstop(default_tabstop);