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 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 Menubar::Pimpl::~Pimpl()
92 // Should we do something here?
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 for (Menu::const_iterator i = menu.begin();
109 i != menu.end(); ++i) {
111 if (i->kind() != MenuItem::Submenu) {
112 lyxerr << "ERROR: Menubar::Pimpl::createMenubar:"
113 " only submenus can appear in a menubar";
116 string label = i->label();
117 string shortcut = "#" + i->shortcut();
118 int width = string_width(label);
119 obj = fl_add_button(FL_MENU_BUTTON,
124 fl_set_object_boxtype(obj, FL_FLAT_BOX);
125 fl_set_object_color(obj, FL_MCOL, FL_MCOL);
126 fl_set_object_lsize(obj, MENU_LABEL_SIZE);
127 fl_set_object_lstyle(obj, FL_NORMAL_STYLE);
128 fl_set_object_resize(obj, FL_RESIZE_ALL);
129 fl_set_object_gravity(obj, NorthWestGravity,
131 moffset += obj->w + air;
132 fl_set_object_shortcut(obj, shortcut.c_str(), 1);
133 fl_set_object_callback(obj, C_Menubar_Pimpl_MenuCallback, 1);
135 ItemInfo * iteminfo = new ItemInfo(this,
136 new MenuItem(*i), obj);
137 buttonlist_.push_back(iteminfo);
138 obj->u_vdata = iteminfo;
143 void Menubar::Pimpl::set(string const & menu_name)
145 lyxerr[Debug::GUI] << "Entering Menubar::Pimpl::set "
146 << "for menu `" << menu_name << "'" << endl;
148 if (menu_name != current_menu_name_) {
149 MenubarMap::iterator mbit = menubarmap_.find(menu_name);
151 if (mbit == menubarmap_.end()) {
152 lyxerr << "ERROR:set: Unknown menu `" << menu_name
157 if (current_group_) {
158 lyxerr[Debug::GUI] << " hiding group "
159 << current_group_ << endl;
160 fl_hide_object(current_group_);
163 lyxerr[Debug::GUI] << " showing group "
164 << mbit->second << endl;
165 fl_show_object(mbit->second);
166 current_menu_name_ = menu_name;
167 current_group_ = mbit->second;
168 lyxerr[Debug::GUI] << "Menubar::Pimpl::set: Menubar set."
172 lyxerr [Debug::GUI] << "Menubar::Pimpl::set: Nothing to do."
176 void Menubar::Pimpl::openByName(string const & name)
178 if (menubackend_->getMenu(current_menu_name_).hasSubmenu(name)) {
179 for (ButtonList::const_iterator cit = buttonlist_.begin();
180 cit != buttonlist_.end(); ++cit) {
181 if ((*cit)->item_->submenu() == name) {
182 MenuCallback((*cit)->obj_, 1);
187 lyxerr << "Menubar::Pimpl::openByName: menu "
188 << name << " not found" << endl;
193 string const limit_string_length(string const & str)
195 string::size_type const max_item_length = 45;
197 if (str.size() > max_item_length)
198 return str.substr(0, max_item_length - 3) + "...";
205 int get_new_submenu(vector<int> & smn, Window win)
207 static size_type max_number_of_menus = 32;
208 if (smn.size() >= max_number_of_menus)
209 max_number_of_menus =
210 fl_setpup_maxpup(static_cast<int>(2*smn.size()));
211 int menu = fl_newpup(win);
217 size_type const max_number_of_items = 25;
220 string const fixlabel(string const & str)
223 return subst(str, '%', '?');
225 return subst(str, "%", "%%");
230 void add_toc2(int menu, string const & extra_label,
231 vector<int> & smn, Window win,
232 vector<Buffer::TocItem> const & toc_list,
233 size_type from, size_type to, int depth)
235 int shortcut_count = 0;
236 if (to - from <= max_number_of_items) {
237 for (size_type i = from; i < to; ++i) {
238 int const action = lyxaction.
239 getPseudoAction(LFUN_GOTO_PARAGRAPH,
240 tostr(toc_list[i].par->id()));
241 string label(4 * max(0, toc_list[i].depth - depth),' ');
242 label += fixlabel(toc_list[i].str);
243 label = limit_string_length(label);
244 label += "%x" + tostr(action + action_offset);
245 if (i == to - 1 && depth == 0)
246 label += extra_label;
247 if (toc_list[i].depth == depth
248 && ++shortcut_count <= 9) {
250 fl_addtopup(menu, label.c_str(),
251 tostr(shortcut_count).c_str());
253 fl_addtopup(menu, label.c_str());
256 size_type pos = from;
260 if (count > max_number_of_items) {
261 int menu2 = get_new_submenu(smn, win);
262 add_toc2(menu2, extra_label, smn, win,
263 toc_list, pos, to, depth);
264 string label = _("More");
267 label += extra_label;
268 fl_addtopup(menu, label.c_str(), menu2);
271 size_type new_pos = pos+1;
272 while (new_pos < to &&
273 toc_list[new_pos].depth > depth)
276 int const action = lyxaction.
277 getPseudoAction(LFUN_GOTO_PARAGRAPH,
278 tostr(toc_list[pos].par->id()));
279 string label(4 * max(0, toc_list[pos].depth - depth), ' ');
280 label += fixlabel(toc_list[pos].str);
281 label = limit_string_length(label);
282 if (new_pos == to && depth == 0)
283 label += extra_label;
285 if (toc_list[pos].depth == depth &&
286 ++shortcut_count <= 9)
287 shortcut = tostr(shortcut_count);
289 if (new_pos == pos + 1) {
290 label += "%x" + tostr(action + action_offset);
291 if (!shortcut.empty()) {
293 fl_addtopup(menu, label.c_str(),
296 fl_addtopup(menu, label.c_str());
298 int menu2 = get_new_submenu(smn, win);
299 add_toc2(menu2, extra_label, smn, win,
300 toc_list, pos, new_pos, depth+1);
302 if (!shortcut.empty()) {
304 fl_addtopup(menu, label.c_str(), menu2,
307 fl_addtopup(menu, label.c_str(), menu2);
315 void Menubar::Pimpl::add_toc(int menu, string const & extra_label,
316 vector<int> & smn, Window win)
318 //xgettext:no-c-format
319 static char const * MenuNames[3] = { N_("List of Figures%m"),
320 //xgettext:no-c-format
321 N_("List of Tables%m"),
322 //xgettext:no-c-format
323 N_("List of Algorithms%m") };
325 vector<vector<Buffer::TocItem> > toc_list =
326 owner_->buffer()->getTocList();
328 // Handle LOF/LOT/LOA
329 int max_nonempty = 0;
330 for (int i = 1; i <= 3; ++i)
331 if (!toc_list[i].empty())
334 for (int j = 1; j <= 3; ++j)
335 if (!toc_list[j].empty()) {
336 int menu2 = get_new_submenu(smn, win);
337 for (size_type i = 0; i < toc_list[j].size(); ++i) {
338 if (i > max_number_of_items) {
339 fl_addtopup(menu2, ". . .%d");
342 int const action = lyxaction.
343 getPseudoAction(LFUN_GOTO_PARAGRAPH,
344 tostr(toc_list[j][i].par->id()));
345 string label = fixlabel(toc_list[j][i].str);
346 label = limit_string_length(label);
347 label += "%x" + tostr(action + action_offset);
348 fl_addtopup(menu2, label.c_str());
350 if (j == max_nonempty) {
351 string label = _(MenuNames[j - 1]);
353 fl_addtopup(menu, label.c_str(), menu2);
355 fl_addtopup(menu, _(MenuNames[j - 1]), menu2);
359 if (max_nonempty == 0 && toc_list[0].empty()) {
360 fl_addtopup(menu, (_("No Table of Contents%i")
361 + extra_label).c_str());
365 add_toc2(menu, extra_label, smn, win,
366 toc_list[0], 0, toc_list[0].size(), 0);
370 int Menubar::Pimpl::create_submenu(Window win, LyXView * view,
371 string const & menu_name,
374 if (!menubackend_->hasMenu(menu_name)){
375 lyxerr << "ERROR:create_submenu: Unknown menu `"
376 << menu_name << "'" << endl;
380 menubackend_->getMenu(menu_name).expand(md, owner_->buffer());
382 int menu = get_new_submenu(smn, win);
383 fl_setpup_softedge(menu, true);
384 fl_setpup_bw(menu, -1);
385 lyxerr[Debug::GUI] << "Adding menu " << menu
386 << " in deletion list" << endl;
388 // Compute the size of the largest label (because xforms is
389 // not able to support shortcuts correctly...)
392 Menu::const_iterator end = md.end();
393 for (Menu::const_iterator i = md.begin(); i != end; ++i) {
394 MenuItem const & item = (*i);
395 if (item.kind() == MenuItem::Command) {
396 string label = item.label() + '\t';
397 int width = string_width(label);
398 if (width > max_width) {
400 widest_label = label;
404 lyxerr[Debug::GUI] << "max_width=" << max_width
405 << ", widest_label=`" << widest_label
408 // Compute where to put separators
409 vector<string> extra_labels(md.size());
410 vector<string>::iterator it = extra_labels.begin();
411 vector<string>::iterator last = it;
412 for (Menu::const_iterator i = md.begin(); i != end; ++i, ++it)
413 if (i->kind() == MenuItem::Separator)
415 else if (!i->optional() ||
416 !(view->getLyXFunc()->getStatus(i->action()) & LyXFunc::Disabled))
419 it = extra_labels.begin();
420 for (Menu::const_iterator i = md.begin(); i != end; ++i, ++it) {
421 MenuItem const & item = (*i);
422 string & extra_label = *it;
424 switch (item.kind()) {
425 case MenuItem::Command: {
426 LyXFunc::func_status flag =
427 view->getLyXFunc()->getStatus(item.action());
429 // handle optional entries.
430 if (item.optional() && (flag & LyXFunc::Disabled)) {
432 << "Skipping optional item "
433 << item.label() << endl;
437 // Get the keys bound to this action, but keep only the
439 string const accel = toplevel_keymap->findbinding(item.action());
440 // Build the menu label from all the info
441 string label = item.label();
443 if (!accel.empty()) {
444 // Try to be clever and add just enough
445 // tabs to align shortcuts.
448 while (string_width(label) < max_width + 5);
449 label += accel.substr(1,accel.find(']') - 1);
451 label += "%x" + tostr(item.action() + action_offset)
454 // Modify the entry using the function status
456 if (flag & (LyXFunc::Disabled | LyXFunc::Unknown))
458 if (flag & LyXFunc::ToggleOn)
460 if (flag & LyXFunc::ToggleOff)
464 // Finally the menu shortcut
465 string shortcut = item.shortcut();
467 if (!shortcut.empty()) {
468 shortcut += lowercase(shortcut[0]);
470 fl_addtopup(menu, label.c_str(),
473 fl_addtopup(menu, label.c_str());
475 lyxerr[Debug::GUI] << "Command: \""
476 << lyxaction.getActionName(item.action())
477 << "\", binding \"" << accel
478 << "\", shortcut \"" << shortcut
483 case MenuItem::Submenu: {
484 int submenu = create_submenu(win, view,
485 item.submenu(), smn);
488 string label = item.label();
489 label += extra_label + "%m";
490 string shortcut = item.shortcut();
491 if (!shortcut.empty()) {
492 shortcut += lowercase(shortcut[0]);
494 fl_addtopup(menu, label.c_str(),
495 submenu, shortcut.c_str());
498 fl_addtopup(menu, label.c_str(), submenu);
503 case MenuItem::Separator:
504 // already done, and if it was the first one,
505 // we just ignore it.
509 add_toc(menu, extra_label, smn, win);
512 case MenuItem::Documents:
513 case MenuItem::Lastfiles:
514 case MenuItem::ViewFormats:
515 case MenuItem::UpdateFormats:
516 case MenuItem::ExportFormats:
517 case MenuItem::ImportFormats:
518 lyxerr << "Menubar::Pimpl::create_submenu: "
519 "this should not happen" << endl;
529 void C_Menubar_Pimpl_MenuCallback(FL_OBJECT * ob, long button)
531 Menubar::Pimpl::MenuCallback(ob, button);
535 void Menubar::Pimpl::MenuCallback(FL_OBJECT * ob, long button)
537 ItemInfo * iteminfo = static_cast<ItemInfo *>(ob->u_vdata);
538 // lyxerr << "MenuCallback: ItemInfo address=" << iteminfo
539 // << "Val=(pimpl_=" << iteminfo->pimpl_
540 // << ", item_=" << iteminfo->item_
541 // << ", obj_=" << iteminfo->obj_ << ")" <<endl;
543 LyXView * view = iteminfo->pimpl_->owner_;
544 MenuItem const * item = iteminfo->item_;
547 // set the pseudo menu-button
548 fl_set_object_boxtype(ob, FL_DOWN_BOX);
549 fl_set_button(ob, 0);
550 fl_redraw_object(ob);
554 Assert(item->kind() == MenuItem::Submenu);
556 // set tabstop length
557 fl_set_tabstop(menu_tabstop);
558 vector<int> submenus;
559 int menu = iteminfo->pimpl_->
560 create_submenu(FL_ObjWin(ob), view,
561 item->submenu(), submenus);
564 fl_setpup_position(view->getForm()->x + ob->x,
565 view->getForm()->y + ob->y + ob->h + 10);
566 int choice = fl_dopup(menu);
569 // set the pseudo menu-button back
570 fl_set_object_boxtype(ob, FL_FLAT_BOX);
571 fl_redraw_object(ob);
574 // If the action value is too low, then it is not a
575 // valid action, but something else.
576 if (choice >= action_offset + 1) {
577 view->getLyXFunc()->Dispatch(choice - action_offset);
581 << "MenuCallback: ignoring bogus action "
586 lyxerr << "Error in MenuCallback" << endl;
588 std::for_each(submenus.begin(), submenus.end(), fl_freepup);
589 // restore tabstop length
590 fl_set_tabstop(default_tabstop);