2 /* This file is part of
3 * ======================================================
5 * LyX, The Document Processor
7 * Copyright 1999-2001 The LyX Team.
9 * ======================================================*/
16 #pragma implementation
19 #include "Menubar_pimpl.h"
20 #include "MenuBackend.h"
21 #include "LyXAction.h"
27 #include "FloatList.h"
28 #include "support/lstrings.h"
29 #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 mheight = 30;
48 int const mbheight= 22;
49 // where to place the menubar?
50 int const yloc = (mheight - mbheight)/2; //air + bw;
51 int const mbadd = 20; // menu button add (to width)
52 // Some space between buttons on the menubar
54 char const * menu_tabstop = "aa";
55 char const * default_tabstop = "aaaaaaaa";
56 // We do not want to mix position values in a menu (like the index of
57 // a submenu) with the action numbers which convey actual information.
58 // Therefore we offset all the action values by an arbitrary large
60 int const action_offset = 1000;
62 // This is used a few times below.
64 int string_width(string const & str)
66 return fl_get_string_widthTAB(FL_NORMAL_STYLE, MENU_LABEL_SIZE,
68 static_cast<int>(str.length()));
74 //Defined later, used in makeMenubar().
76 void C_Menubar_Pimpl_MenuCallback(FL_OBJECT * ob, long button);
79 Menubar::Pimpl::Pimpl(LyXView * view, MenuBackend const & mb)
80 : owner_(view), menubackend_(&mb), current_group_(0)
82 for (MenuBackend::const_iterator menu = menubackend_->begin();
83 menu != menubackend_->end() ; ++menu) {
84 if (menu->menubar()) {
85 FL_OBJECT * group = fl_bgn_group();
88 fl_hide_object(group);
90 << "Menubar::Pimpl::Pimpl: "
91 << "creating and hiding group " << group
92 << " for menubar " << menu->name() << endl;
93 menubarmap_[menu->name()] = group;
99 void Menubar::Pimpl::makeMenubar(Menu const & menu)
101 FL_FORM * form = owner_->getForm();
104 // Create menu frame if there is non yet.
105 FL_OBJECT * frame = fl_add_frame(FL_UP_FRAME, 0, 0,
106 form->w, mheight, "");
107 fl_set_object_resize(frame, FL_RESIZE_ALL);
108 fl_set_object_gravity(frame, NorthWestGravity,
111 Menu::const_iterator i = menu.begin();
112 Menu::const_iterator end = menu.end();
113 for (; i != end; ++i) {
115 if (i->kind() != MenuItem::Submenu) {
116 lyxerr << "ERROR: Menubar::Pimpl::createMenubar:"
117 " only submenus can appear in a menubar";
120 string const label = i->label();
121 string const shortcut = "#" + i->shortcut();
122 int const width = string_width(label);
123 obj = fl_add_button(FL_MENU_BUTTON,
128 fl_set_object_boxtype(obj, FL_FLAT_BOX);
129 fl_set_object_color(obj, FL_MCOL, FL_MCOL);
130 fl_set_object_lsize(obj, MENU_LABEL_SIZE);
131 fl_set_object_lstyle(obj, FL_NORMAL_STYLE);
132 fl_set_object_resize(obj, FL_RESIZE_ALL);
133 fl_set_object_gravity(obj, NorthWestGravity,
135 moffset += obj->w + air;
136 fl_set_object_shortcut(obj, shortcut.c_str(), 1);
137 fl_set_object_callback(obj, C_Menubar_Pimpl_MenuCallback, 1);
139 boost::shared_ptr<ItemInfo>
140 iteminfo(new ItemInfo(this, new MenuItem(*i), obj));
141 buttonlist_.push_back(iteminfo);
142 obj->u_vdata = iteminfo.get();
148 void Menubar::Pimpl::set(string const & menu_name)
150 lyxerr[Debug::GUI] << "Entering Menubar::Pimpl::set "
151 << "for menu `" << menu_name << "'" << endl;
153 if (menu_name != current_menu_name_) {
154 MenubarMap::iterator mbit = menubarmap_.find(menu_name);
156 if (mbit == menubarmap_.end()) {
157 lyxerr << "ERROR:set: Unknown menu `" << menu_name
162 if (current_group_) {
163 lyxerr[Debug::GUI] << " hiding group "
164 << current_group_ << endl;
165 fl_hide_object(current_group_);
168 lyxerr[Debug::GUI] << " showing group "
169 << mbit->second << endl;
170 fl_show_object(mbit->second);
171 current_menu_name_ = menu_name;
172 current_group_ = mbit->second;
173 lyxerr[Debug::GUI] << "Menubar::Pimpl::set: Menubar set."
176 lyxerr [Debug::GUI] << "Menubar::Pimpl::set: Nothing to do."
181 void Menubar::Pimpl::openByName(string const & name)
183 if (menubackend_->getMenu(current_menu_name_).hasSubmenu(name)) {
184 for (ButtonList::const_iterator cit = buttonlist_.begin();
185 cit != buttonlist_.end(); ++cit) {
186 if ((*cit)->item_->submenu() == name) {
187 MenuCallback((*cit)->obj_, 1);
192 lyxerr << "Menubar::Pimpl::openByName: menu "
193 << name << " not found" << endl;
200 string const limit_string_length(string const & str)
202 string::size_type const max_item_length = 45;
204 if (str.size() > max_item_length)
205 return str.substr(0, max_item_length - 3) + "...";
211 int get_new_submenu(vector<int> & smn, Window win)
213 static size_type max_number_of_menus = 32;
214 if (smn.size() >= max_number_of_menus)
215 max_number_of_menus =
216 fl_setpup_maxpup(static_cast<int>(2*smn.size()));
217 int menu = fl_newpup(win);
223 size_type const max_number_of_items = 25;
226 string const fixlabel(string const & str)
229 return subst(str, '%', '?');
231 return subst(str, "%", "%%");
238 void add_toc2(int menu, string const & extra_label,
239 vector<int> & smn, Window win,
240 vector<Buffer::TocItem> const & toc_list,
241 size_type from, size_type to, int depth)
243 int shortcut_count = 0;
244 if (to - from <= max_number_of_items) {
245 for (size_type i = from; i < to; ++i) {
246 int const action = lyxaction.
247 getPseudoAction(LFUN_GOTO_PARAGRAPH,
248 tostr(toc_list[i].par->id()));
249 string label(4 * max(0, toc_list[i].depth - depth),' ');
250 label += fixlabel(toc_list[i].str);
251 label = limit_string_length(label);
252 label += "%x" + tostr(action + action_offset);
253 if (i == to - 1 && depth == 0)
254 label += extra_label;
255 if (toc_list[i].depth == depth
256 && ++shortcut_count <= 9) {
258 fl_addtopup(menu, label.c_str(),
259 tostr(shortcut_count).c_str());
261 fl_addtopup(menu, label.c_str());
264 size_type pos = from;
268 if (count > max_number_of_items) {
269 int menu2 = get_new_submenu(smn, win);
270 add_toc2(menu2, extra_label, smn, win,
271 toc_list, pos, to, depth);
272 string label = _("More");
275 label += extra_label;
276 fl_addtopup(menu, label.c_str(), menu2);
279 size_type new_pos = pos+1;
280 while (new_pos < to &&
281 toc_list[new_pos].depth > depth)
284 int const action = lyxaction.
285 getPseudoAction(LFUN_GOTO_PARAGRAPH,
286 tostr(toc_list[pos].par->id()));
287 string label(4 * max(0, toc_list[pos].depth - depth), ' ');
288 label += fixlabel(toc_list[pos].str);
289 label = limit_string_length(label);
290 if (new_pos == to && depth == 0)
291 label += extra_label;
293 if (toc_list[pos].depth == depth &&
294 ++shortcut_count <= 9)
295 shortcut = tostr(shortcut_count);
297 if (new_pos == pos + 1) {
298 label += "%x" + tostr(action + action_offset);
299 if (!shortcut.empty()) {
301 fl_addtopup(menu, label.c_str(),
304 fl_addtopup(menu, label.c_str());
306 int menu2 = get_new_submenu(smn, win);
307 add_toc2(menu2, extra_label, smn, win,
308 toc_list, pos, new_pos, depth+1);
310 if (!shortcut.empty()) {
312 fl_addtopup(menu, label.c_str(), menu2,
315 fl_addtopup(menu, label.c_str(), menu2);
323 void Menubar::Pimpl::add_toc(int menu, string const & extra_label,
324 vector<int> & smn, Window win)
326 Buffer::Lists toc_list = owner_->buffer()->getLists();
327 Buffer::Lists::const_iterator cit = toc_list.begin();
328 Buffer::Lists::const_iterator end = toc_list.end();
329 for (; cit != end; ++cit) {
330 // Handle this elsewhere
331 if (cit->first == "TOC") continue;
333 // All the rest is for floats
334 int menu2 = get_new_submenu(smn, win);
335 Buffer::SingleList::const_iterator ccit = cit->second.begin();
336 Buffer::SingleList::const_iterator eend = cit->second.end();
337 for (; ccit != eend; ++ccit) {
340 .getPseudoAction(LFUN_GOTO_PARAGRAPH,
341 tostr(ccit->par->id()));
342 string label = fixlabel(ccit->str);
343 label = limit_string_length(label);
344 label += "%x" + tostr(action + action_offset);
345 fl_addtopup(menu2, label.c_str());
347 string const m = floatList[cit->first]->second.name() + "%m";
348 fl_addtopup(menu, m.c_str(), menu2);
353 cit = toc_list.find("TOC");
355 string const tmp = _("No Table of contents%i") + extra_label;
356 fl_addtopup(menu, tmp.c_str());
359 add_toc2(menu, extra_label, smn, win,
360 cit->second, 0, cit->second.size(), 0);
365 int Menubar::Pimpl::create_submenu(Window win, LyXView * view,
366 string const & menu_name,
369 if (!menubackend_->hasMenu(menu_name)){
370 lyxerr << "ERROR:create_submenu: Unknown menu `"
371 << menu_name << "'" << endl;
375 menubackend_->getMenu(menu_name).expand(md, owner_->buffer());
377 int const menu = get_new_submenu(smn, win);
378 fl_setpup_softedge(menu, true);
379 fl_setpup_bw(menu, -1);
380 lyxerr[Debug::GUI] << "Adding menu " << menu
381 << " in deletion list" << endl;
383 // Compute the size of the largest label (because xforms is
384 // not able to support shortcuts correctly...)
387 Menu::const_iterator end = md.end();
388 for (Menu::const_iterator i = md.begin(); i != end; ++i) {
389 MenuItem const & item = (*i);
390 if (item.kind() == MenuItem::Command) {
391 string const label = item.label() + '\t';
392 int const width = string_width(label);
393 if (width > max_width) {
395 widest_label = label;
399 lyxerr[Debug::GUI] << "max_width=" << max_width
400 << ", widest_label=`" << widest_label
403 // Compute where to put separators
404 vector<string> extra_labels(md.size());
405 vector<string>::iterator it = extra_labels.begin();
406 vector<string>::iterator last = it;
407 for (Menu::const_iterator i = md.begin(); i != end; ++i, ++it)
408 if (i->kind() == MenuItem::Separator)
410 else if (!i->optional() ||
411 !(view->getLyXFunc()->getStatus(i->action()) & LyXFunc::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 LyXFunc::func_status flag =
422 view->getLyXFunc()->getStatus(item.action());
424 // handle optional entries.
425 if (item.optional() && (flag & LyXFunc::Disabled)) {
427 << "Skipping optional item "
428 << item.label() << endl;
432 // Get the keys bound to this action, but keep only the
434 string const accel = toplevel_keymap->findbinding(item.action());
435 // Build the menu label from all the info
436 string label = item.label();
438 if (!accel.empty()) {
439 // Try to be clever and add just enough
440 // tabs to align shortcuts.
443 while (string_width(label) < max_width + 5);
444 label += accel.substr(1,accel.find(']') - 1);
446 label += "%x" + tostr(item.action() + action_offset)
449 // Modify the entry using the function status
451 if (flag & (LyXFunc::Disabled | LyXFunc::Unknown))
453 if (flag & LyXFunc::ToggleOn)
455 if (flag & LyXFunc::ToggleOff)
459 // Finally the menu shortcut
460 string shortcut = item.shortcut();
462 if (!shortcut.empty()) {
463 shortcut += lowercase(shortcut[0]);
465 fl_addtopup(menu, label.c_str(),
468 fl_addtopup(menu, label.c_str());
470 lyxerr[Debug::GUI] << "Command: \""
471 << lyxaction.getActionName(item.action())
472 << "\", binding \"" << accel
473 << "\", shortcut \"" << shortcut
478 case MenuItem::Submenu: {
479 int submenu = create_submenu(win, view,
480 item.submenu(), smn);
483 string label = item.label();
484 label += extra_label + "%m";
485 string shortcut = item.shortcut();
486 if (!shortcut.empty()) {
487 shortcut += lowercase(shortcut[0]);
489 fl_addtopup(menu, label.c_str(),
490 submenu, shortcut.c_str());
492 fl_addtopup(menu, label.c_str(), submenu);
497 case MenuItem::Separator:
498 // already done, and if it was the first one,
499 // we just ignore it.
503 add_toc(menu, extra_label, smn, win);
506 case MenuItem::Documents:
507 case MenuItem::Lastfiles:
508 case MenuItem::ViewFormats:
509 case MenuItem::UpdateFormats:
510 case MenuItem::ExportFormats:
511 case MenuItem::ImportFormats:
512 case MenuItem::FloatListInsert:
513 case MenuItem::FloatInsert:
514 lyxerr << "Menubar::Pimpl::create_submenu: "
515 "this should not happen" << endl;
525 void C_Menubar_Pimpl_MenuCallback(FL_OBJECT * ob, long button)
527 Menubar::Pimpl::MenuCallback(ob, button);
531 void Menubar::Pimpl::MenuCallback(FL_OBJECT * ob, long button)
533 ItemInfo * iteminfo = static_cast<ItemInfo *>(ob->u_vdata);
534 // lyxerr << "MenuCallback: ItemInfo address=" << iteminfo
535 // << "Val=(pimpl_=" << iteminfo->pimpl_
536 // << ", item_=" << iteminfo->item_
537 // << ", obj_=" << iteminfo->obj_ << ")" <<endl;
539 LyXView * view = iteminfo->pimpl_->owner_;
540 MenuItem const * item = iteminfo->item_.get();
542 /* get the splash out of the way. It would be nicer
543 * to only have this code at the start, but xforms
544 * makes it too ugly to do
546 view->getDialogs()->destroySplash();
549 // set the pseudo menu-button
550 fl_set_object_boxtype(ob, FL_DOWN_BOX);
551 fl_set_button(ob, 0);
552 fl_redraw_object(ob);
556 lyx::Assert(item->kind() == MenuItem::Submenu);
558 // set tabstop length
559 fl_set_tabstop(menu_tabstop);
560 vector<int> submenus;
561 int menu = iteminfo->pimpl_->
562 create_submenu(FL_ObjWin(ob), view,
563 item->submenu(), submenus);
566 fl_setpup_position(view->getForm()->x + ob->x,
567 view->getForm()->y + ob->y + ob->h + 10);
568 int choice = fl_dopup(menu);
571 // set the pseudo menu-button back
572 fl_set_object_boxtype(ob, FL_FLAT_BOX);
573 fl_redraw_object(ob);
576 // If the action value is too low, then it is not a
577 // valid action, but something else.
578 if (choice >= action_offset + 1) {
579 view->getLyXFunc()->Dispatch(choice - action_offset);
583 << "MenuCallback: ignoring bogus action "
588 lyxerr << "Error in MenuCallback" << endl;
590 std::for_each(submenus.begin(), submenus.end(), fl_freepup);
591 // restore tabstop length
592 fl_set_tabstop(default_tabstop);