1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright (C) 1999 The LyX Team.
8 *======================================================*/
11 #pragma implementation
17 #include "support/lstrings.h"
18 #include "support/LAssert.h"
20 #include "LyXAction.h"
25 #include "MenuBackend.h"
26 #include "Menubar_pimpl.h"
33 typedef vector<int>::size_type size_type;
35 extern boost::scoped_ptr<kb_keymap> toplevel_keymap;
36 extern LyXAction lyxaction;
39 static const int MENU_LABEL_SIZE = FL_NORMAL_SIZE;
40 static const int mheight = 30;
41 static const int mbheight= 22;
42 // where to place the menubar?
43 static const int yloc = (mheight - mbheight)/2; //air + bw;
44 static const int mbadd = 20; // menu button add (to width)
45 // Some space between buttons on the menubar
46 static const int air = 2;
47 static char const * menu_tabstop = "aa";
48 static char const * default_tabstop = "aaaaaaaa";
49 // We do not want to mix position values in a menu (like the index of
50 // a submenu) with the action numbers which convey actual information.
51 // Therefore we offset all the action values by an arbitrary large
53 static const int action_offset = 1000;
56 //Defined later, used in makeMenubar().
58 void C_Menubar_Pimpl_MenuCallback(FL_OBJECT * ob, long button);
60 // This is used a few times below.
62 int string_width(string const & str)
64 return fl_get_string_widthTAB(FL_NORMAL_STYLE, MENU_LABEL_SIZE,
66 static_cast<int>(str.length()));
70 Menubar::Pimpl::Pimpl(LyXView * view, MenuBackend const & mb)
71 : owner_(view), menubackend_(&mb), current_group_(0)
73 for (MenuBackend::const_iterator menu = menubackend_->begin();
74 menu != menubackend_->end() ; ++menu) {
75 if (menu->menubar()) {
76 FL_OBJECT * group = fl_bgn_group();
79 fl_hide_object(group);
81 << "Menubar::Pimpl::Pimpl: "
82 << "creating and hiding group " << group
83 << " for menubar " << menu->name() << endl;
84 menubarmap_[menu->name()] = group;
90 void Menubar::Pimpl::makeMenubar(Menu const &menu)
92 FL_FORM * form = owner_->getForm();
95 // Create menu frame if there is non yet.
96 FL_OBJECT * frame = fl_add_frame(FL_UP_FRAME, 0, 0,
97 form->w, mheight, "");
98 fl_set_object_resize(frame, FL_RESIZE_ALL);
99 fl_set_object_gravity(frame, NorthWestGravity,
102 for (Menu::const_iterator i = menu.begin();
103 i != menu.end(); ++i) {
105 if (i->kind() != MenuItem::Submenu) {
106 lyxerr << "ERROR: Menubar::Pimpl::createMenubar:"
107 " only submenus can appear in a menubar";
110 string label = i->label();
111 string shortcut = "#" + i->shortcut();
112 int width = string_width(label);
113 obj = fl_add_button(FL_MENU_BUTTON,
118 fl_set_object_boxtype(obj, FL_FLAT_BOX);
119 fl_set_object_color(obj, FL_MCOL, FL_MCOL);
120 fl_set_object_lsize(obj, MENU_LABEL_SIZE);
121 fl_set_object_lstyle(obj, FL_NORMAL_STYLE);
122 fl_set_object_resize(obj, FL_RESIZE_ALL);
123 fl_set_object_gravity(obj, NorthWestGravity,
125 moffset += obj->w + air;
126 fl_set_object_shortcut(obj, shortcut.c_str(), 1);
127 fl_set_object_callback(obj, C_Menubar_Pimpl_MenuCallback, 1);
129 boost::shared_ptr<ItemInfo> iteminfo(new ItemInfo(this,
130 new MenuItem(*i), obj));
131 buttonlist_.push_back(iteminfo);
132 obj->u_vdata = iteminfo.get();
137 void Menubar::Pimpl::set(string const & menu_name)
139 lyxerr[Debug::GUI] << "Entering Menubar::Pimpl::set "
140 << "for menu `" << menu_name << "'" << endl;
142 if (menu_name != current_menu_name_) {
143 MenubarMap::iterator mbit = menubarmap_.find(menu_name);
145 if (mbit == menubarmap_.end()) {
146 lyxerr << "ERROR:set: Unknown menu `" << menu_name
151 if (current_group_) {
152 lyxerr[Debug::GUI] << " hiding group "
153 << current_group_ << endl;
154 fl_hide_object(current_group_);
157 lyxerr[Debug::GUI] << " showing group "
158 << mbit->second << endl;
159 fl_show_object(mbit->second);
160 current_menu_name_ = menu_name;
161 current_group_ = mbit->second;
162 lyxerr[Debug::GUI] << "Menubar::Pimpl::set: Menubar set."
166 lyxerr [Debug::GUI] << "Menubar::Pimpl::set: Nothing to do."
170 void Menubar::Pimpl::openByName(string const & name)
172 if (menubackend_->getMenu(current_menu_name_).hasSubmenu(name)) {
173 for (ButtonList::const_iterator cit = buttonlist_.begin();
174 cit != buttonlist_.end(); ++cit) {
175 if ((*cit)->item_->submenu() == name) {
176 MenuCallback((*cit)->obj_, 1);
181 lyxerr << "Menubar::Pimpl::openByName: menu "
182 << name << " not found" << endl;
187 string const limit_string_length(string const & str)
189 string::size_type const max_item_length = 45;
191 if (str.size() > max_item_length)
192 return str.substr(0, max_item_length - 3) + "...";
199 int get_new_submenu(vector<int> & smn, Window win)
201 static size_type max_number_of_menus = 32;
202 if (smn.size() >= max_number_of_menus)
203 max_number_of_menus =
204 fl_setpup_maxpup(static_cast<int>(2*smn.size()));
205 int menu = fl_newpup(win);
211 size_type const max_number_of_items = 25;
214 string const fixlabel(string const & str)
217 return subst(str, '%', '?');
219 return subst(str, "%", "%%");
224 void add_toc2(int menu, string const & extra_label,
225 vector<int> & smn, Window win,
226 vector<Buffer::TocItem> const & toc_list,
227 size_type from, size_type to, int depth)
229 int shortcut_count = 0;
230 if (to - from <= max_number_of_items) {
231 for (size_type i = from; i < to; ++i) {
232 int const action = lyxaction.
233 getPseudoAction(LFUN_GOTO_PARAGRAPH,
234 tostr(toc_list[i].par->id()));
235 string label(4 * max(0, toc_list[i].depth - depth),' ');
236 label += fixlabel(toc_list[i].str);
237 label = limit_string_length(label);
238 label += "%x" + tostr(action + action_offset);
239 if (i == to - 1 && depth == 0)
240 label += extra_label;
241 if (toc_list[i].depth == depth
242 && ++shortcut_count <= 9) {
244 fl_addtopup(menu, label.c_str(),
245 tostr(shortcut_count).c_str());
247 fl_addtopup(menu, label.c_str());
250 size_type pos = from;
254 if (count > max_number_of_items) {
255 int menu2 = get_new_submenu(smn, win);
256 add_toc2(menu2, extra_label, smn, win,
257 toc_list, pos, to, depth);
258 string label = _("More");
261 label += extra_label;
262 fl_addtopup(menu, label.c_str(), menu2);
265 size_type new_pos = pos+1;
266 while (new_pos < to &&
267 toc_list[new_pos].depth > depth)
270 int const action = lyxaction.
271 getPseudoAction(LFUN_GOTO_PARAGRAPH,
272 tostr(toc_list[pos].par->id()));
273 string label(4 * max(0, toc_list[pos].depth - depth), ' ');
274 label += fixlabel(toc_list[pos].str);
275 label = limit_string_length(label);
276 if (new_pos == to && depth == 0)
277 label += extra_label;
279 if (toc_list[pos].depth == depth &&
280 ++shortcut_count <= 9)
281 shortcut = tostr(shortcut_count);
283 if (new_pos == pos + 1) {
284 label += "%x" + tostr(action + action_offset);
285 if (!shortcut.empty()) {
287 fl_addtopup(menu, label.c_str(),
290 fl_addtopup(menu, label.c_str());
292 int menu2 = get_new_submenu(smn, win);
293 add_toc2(menu2, extra_label, smn, win,
294 toc_list, pos, new_pos, depth+1);
296 if (!shortcut.empty()) {
298 fl_addtopup(menu, label.c_str(), menu2,
301 fl_addtopup(menu, label.c_str(), menu2);
309 void Menubar::Pimpl::add_toc(int menu, string const & extra_label,
310 vector<int> & smn, Window win)
312 //xgettext:no-c-format
313 static char const * MenuNames[3] = { N_("List of Figures%m"),
314 //xgettext:no-c-format
315 N_("List of Tables%m"),
316 //xgettext:no-c-format
317 N_("List of Algorithms%m") };
319 vector<vector<Buffer::TocItem> > toc_list =
320 owner_->buffer()->getTocList();
322 // Handle LOF/LOT/LOA
323 int max_nonempty = 0;
324 for (int i = 1; i <= 3; ++i)
325 if (!toc_list[i].empty())
328 for (int j = 1; j <= 3; ++j)
329 if (!toc_list[j].empty()) {
330 int menu2 = get_new_submenu(smn, win);
331 for (size_type i = 0; i < toc_list[j].size(); ++i) {
332 if (i > max_number_of_items) {
333 fl_addtopup(menu2, ". . .%d");
336 int const action = lyxaction.
337 getPseudoAction(LFUN_GOTO_PARAGRAPH,
338 tostr(toc_list[j][i].par->id()));
339 string label = fixlabel(toc_list[j][i].str);
340 label = limit_string_length(label);
341 label += "%x" + tostr(action + action_offset);
342 fl_addtopup(menu2, label.c_str());
344 if (j == max_nonempty) {
345 string label = _(MenuNames[j - 1]);
347 fl_addtopup(menu, label.c_str(), menu2);
349 fl_addtopup(menu, _(MenuNames[j - 1]), menu2);
353 if (max_nonempty == 0 && toc_list[0].empty()) {
354 fl_addtopup(menu, (_("No Table of Contents%i")
355 + extra_label).c_str());
359 add_toc2(menu, extra_label, smn, win,
360 toc_list[0], 0, toc_list[0].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 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 label = item.label() + '\t';
391 int 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()) & LyXFunc::Disabled))
413 it = extra_labels.begin();
414 for (Menu::const_iterator i = md.begin(); i != end; ++i, ++it) {
415 MenuItem const & item = (*i);
416 string & extra_label = *it;
418 switch (item.kind()) {
419 case MenuItem::Command: {
420 LyXFunc::func_status flag =
421 view->getLyXFunc()->getStatus(item.action());
423 // handle optional entries.
424 if (item.optional() && (flag & LyXFunc::Disabled)) {
426 << "Skipping optional item "
427 << item.label() << endl;
431 // Get the keys bound to this action, but keep only the
433 string const accel = toplevel_keymap->findbinding(item.action());
434 // Build the menu label from all the info
435 string label = item.label();
437 if (!accel.empty()) {
438 // Try to be clever and add just enough
439 // tabs to align shortcuts.
442 while (string_width(label) < max_width + 5);
443 label += accel.substr(1,accel.find(']') - 1);
445 label += "%x" + tostr(item.action() + action_offset)
448 // Modify the entry using the function status
450 if (flag & (LyXFunc::Disabled | LyXFunc::Unknown))
452 if (flag & LyXFunc::ToggleOn)
454 if (flag & LyXFunc::ToggleOff)
458 // Finally the menu shortcut
459 string shortcut = item.shortcut();
461 if (!shortcut.empty()) {
462 shortcut += lowercase(shortcut[0]);
464 fl_addtopup(menu, label.c_str(),
467 fl_addtopup(menu, label.c_str());
469 lyxerr[Debug::GUI] << "Command: \""
470 << lyxaction.getActionName(item.action())
471 << "\", binding \"" << accel
472 << "\", shortcut \"" << shortcut
477 case MenuItem::Submenu: {
478 int submenu = create_submenu(win, view,
479 item.submenu(), smn);
482 string label = item.label();
483 label += extra_label + "%m";
484 string shortcut = item.shortcut();
485 if (!shortcut.empty()) {
486 shortcut += lowercase(shortcut[0]);
488 fl_addtopup(menu, label.c_str(),
489 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 lyxerr << "Menubar::Pimpl::create_submenu: "
513 "this should not happen" << endl;
523 void C_Menubar_Pimpl_MenuCallback(FL_OBJECT * ob, long button)
525 Menubar::Pimpl::MenuCallback(ob, button);
529 void Menubar::Pimpl::MenuCallback(FL_OBJECT * ob, long button)
531 ItemInfo * iteminfo = static_cast<ItemInfo *>(ob->u_vdata);
532 // lyxerr << "MenuCallback: ItemInfo address=" << iteminfo
533 // << "Val=(pimpl_=" << iteminfo->pimpl_
534 // << ", item_=" << iteminfo->item_
535 // << ", obj_=" << iteminfo->obj_ << ")" <<endl;
537 LyXView * view = iteminfo->pimpl_->owner_;
538 MenuItem const * item = iteminfo->item_.get();
541 // set the pseudo menu-button
542 fl_set_object_boxtype(ob, FL_DOWN_BOX);
543 fl_set_button(ob, 0);
544 fl_redraw_object(ob);
548 Assert(item->kind() == MenuItem::Submenu);
550 // set tabstop length
551 fl_set_tabstop(menu_tabstop);
552 vector<int> submenus;
553 int menu = iteminfo->pimpl_->
554 create_submenu(FL_ObjWin(ob), view,
555 item->submenu(), submenus);
558 fl_setpup_position(view->getForm()->x + ob->x,
559 view->getForm()->y + ob->y + ob->h + 10);
560 int choice = fl_dopup(menu);
563 // set the pseudo menu-button back
564 fl_set_object_boxtype(ob, FL_FLAT_BOX);
565 fl_redraw_object(ob);
568 // If the action value is too low, then it is not a
569 // valid action, but something else.
570 if (choice >= action_offset + 1) {
571 view->getLyXFunc()->Dispatch(choice - action_offset);
575 << "MenuCallback: ignoring bogus action "
580 lyxerr << "Error in MenuCallback" << endl;
582 std::for_each(submenus.begin(), submenus.end(), fl_freepup);
583 // restore tabstop length
584 fl_set_tabstop(default_tabstop);