1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright (C) 1999 The LyX Team.
8 *======================================================*/
11 #pragma implementation
18 #include "support/lstrings.h"
19 #include "support/filetools.h"
20 #include "support/StrPool.h"
21 #include "support/LAssert.h"
23 #include "LyXAction.h"
26 #include "bufferlist.h"
27 #include "lastfiles.h"
29 #include "MenuBackend.h"
30 #include "Menubar_pimpl.h"
34 extern kb_keymap * toplevel_keymap;
35 extern LyXAction lyxaction;
36 extern BufferList bufferlist;
37 extern LastFiles * lastfiles;
40 const int MENU_LABEL_SIZE = FL_NORMAL_SIZE;
41 const int mheight = 30;
42 const int mbheight= 22;
43 // where to place the menubar?
44 const int yloc = (mheight - mbheight)/2; //air + bw;
45 const int mbadd = 20; // menu button add (to width)
46 // Some space between buttons on the menubar
48 char const * menu_tabstop = "aa";
49 char const * default_tabstop = "aaaaaaaa";
52 Menubar::Pimpl::Pimpl(LyXView * view, MenuBackend const & mb)
53 : frame_(0), owner_(view), menubackend_(&mb)
55 // Should we do something here?
58 Menubar::Pimpl::~Pimpl()
60 // Should we do something here?
63 // This is used a few times below.
65 int string_width(string const & str)
67 return fl_get_string_widthTAB(FL_NORMAL_STYLE, MENU_LABEL_SIZE,
68 str.c_str(), str.length());
71 //Defined later, used in set().
73 void C_Menubar_Pimpl_MenuCallback(FL_OBJECT * ob, long button);
75 void Menubar::Pimpl::set(string const & menu_name)
77 lyxerr[Debug::GUI] << "Entering Menubar::Pimpl::set "
78 << "for menu `" << menu_name << "'" << endl;
80 if (menu_name == current_menu) {
81 lyxerr[Debug::GUI] << "Nothing to do." << endl;
85 // If the backend has not been initialized yet, we use a
87 if (menubackend_->empty()) {
88 lyxerr << "Menubar::Pimpl::set: menubackend is empty! "
89 "using default values." << endl;
90 MenuBackend * mb = new MenuBackend();
95 if (!menubackend_->hasMenu(menu_name)){
96 lyxerr << "ERROR:set: Unknown menu `" << menu_name
101 Menu menu = menubackend_->getMenu(menu_name);
103 if (!menu.menubar()) {
104 lyxerr << "Only a menubar-type object can go in a "
105 "toplevel menu" << endl;
109 current_menu = menu_name;
110 FL_FORM * form = owner_->getForm();
112 bool form_was_open, form_was_frozen;
114 if (fl_current_form == form)
115 form_was_open = true;
116 else if (fl_current_form == 0) {
117 form_was_open = false;
121 lyxerr << "Something is wrong: unknown form "
122 << fl_current_form << " is already open"
123 << "(main form is " << form << ")" << endl;
127 form_was_frozen = true;
129 form_was_frozen = false;
130 fl_freeze_form(form);
133 // Delete old buttons if there are some.
134 for(ButtonList::const_iterator cit = buttonlist_.begin();
135 cit != buttonlist_.end(); ++cit) {
137 fl_delete_object((*cit)->obj_);
138 fl_free_object((*cit)->obj_);
144 // Create menu frame if there is non yet.
146 frame_ = fl_add_frame(FL_UP_FRAME, 0, 0, form->w, mheight, "");
147 fl_set_object_resize(frame_, FL_RESIZE_ALL);
148 fl_set_object_gravity(frame_, NorthWestGravity,
152 for (Menu::const_iterator i = menu.begin();
153 i != menu.end(); ++i) {
155 if (i->kind() != MenuItem::Submenu) {
156 lyxerr << "ERROR: Menubar::Pimpl::Pimpl:"
157 " only submenus can appear in a menubar";
160 string label = i->label();
161 string shortcut = i->shortcut();
162 int width = string_width(label);
163 obj = fl_add_button(FL_TOUCH_BUTTON,
168 fl_set_object_boxtype(obj, FL_FLAT_BOX);
169 fl_set_object_color(obj, FL_MCOL, FL_MCOL);
170 fl_set_object_lsize(obj, MENU_LABEL_SIZE);
171 fl_set_object_lstyle(obj, FL_NORMAL_STYLE);
172 fl_set_object_resize(obj, FL_RESIZE_ALL);
173 fl_set_object_gravity(obj, NorthWestGravity,
175 moffset += obj->w + air;
176 fl_set_object_shortcut(obj, shortcut.c_str(), 1);
177 fl_set_object_callback(obj, C_Menubar_Pimpl_MenuCallback, 1);
178 ItemInfo * iteminfo = new ItemInfo(this,
179 new MenuItem(*i), obj);
180 buttonlist_.push_back(iteminfo);
181 obj->u_vdata = iteminfo;
182 // lyxerr << "MenuCallback: ItemInfo address=" << iteminfo
183 // << " Val=(pimpl_=" << iteminfo->pimpl_
184 // << ", item_=" << iteminfo->item_
185 // << ", obj_=" << iteminfo->obj_ << ")" <<endl;
188 if (!form_was_frozen) {
189 fl_unfreeze_form(form);
194 lyxerr[Debug::GUI] << "Menubar set." << endl;
197 void Menubar::Pimpl::openByName(string const & name)
199 for(ButtonList::const_iterator cit = buttonlist_.begin();
200 cit != buttonlist_.end(); ++cit) {
201 if ((*cit)->item_->submenu() == name) {
202 MenuCallback((*cit)->obj_, 1);
206 lyxerr << "Menubar::Pimpl::openByName: menu "
207 << name << " not found" << endl;
211 void Menubar::Pimpl::add_lastfiles(int menu, string const & extra_label,
212 std::vector<int> & /*smn*/,
216 for (LastFiles::const_iterator cit = lastfiles->begin();
217 cit != lastfiles->end() && ii < 10; ++cit, ++ii) {
220 lyxaction.getPseudoAction(LFUN_FILE_OPEN, (*cit));
221 string label = tostr(ii) + ". "
222 + MakeDisplayPath((*cit),30)
223 + "%x" + tostr(action);
224 if ((cit + 1) == lastfiles->end())
225 label += extra_label;
226 string shortcut = tostr(ii) + "#" + tostr(ii);
227 lyxerr[Debug::GUI] << "shortcut is " << shortcut <<
230 int n = fl_addtopup(menu, strpool.add(label));
231 fl_setpup_shortcut(menu, n, strpool.add(shortcut));
236 void Menubar::Pimpl::add_documents(int menu, string const & extra_label,
237 std::vector<int> & /*smn*/,
240 std::vector<string> names = bufferlist.getFileNames();
243 fl_addtopup(menu,_("No Documents Open!%i"));
247 for (std::vector<string>::const_iterator cit = names.begin();
248 cit != names.end() ; ++cit) {
250 lyxaction.getPseudoAction(LFUN_SWITCHBUFFER, *cit);
251 string label = MakeDisplayPath(*cit, 30)
252 + "%x" + tostr(action);
253 if ((cit + 1) == names.end())
254 label += extra_label;
256 fl_addtopup(menu, strpool.add(label));
262 int Menubar::Pimpl::create_submenu(Window win, LyXView * view,
263 string const & menu_name,
264 std::vector<int> & smn, StrPool & strpool)
266 if (!menubackend_->hasMenu(menu_name)){
267 lyxerr << "ERROR:create_submenu: Unknown menu `"
268 << menu_name << "'" << endl;
271 Menu md = menubackend_->getMenu(menu_name);
273 int menu = fl_newpup(win);
274 fl_setpup_softedge(menu, true);
275 fl_setpup_bw(menu, -1);
276 lyxerr[Debug::GUI] << "Adding menu " << menu
277 << " in deletion list" << endl;
280 // Compute the size of the largest label (because xforms is
281 // not able to support shortcuts correctly...)
284 for (Menu::const_iterator i = md.begin(); i != md.end(); ++i) {
285 MenuItem item = (*i);
286 if (item.kind() == MenuItem::Command) {
287 string label = item.label() + '\t';
288 int width = string_width(label);
289 if (width > max_width) {
291 widest_label = label;
295 lyxerr[Debug::GUI] << "max_width=" << max_width
296 << ", widest_label=`" << widest_label
299 for (Menu::const_iterator i = md.begin(); i != md.end(); ++i) {
300 MenuItem item = (*i);
301 // Is there a separator after this item?
303 if ((i+1) != md.end()
304 && (i+1)->kind() == MenuItem::Separator)
307 switch(item.kind()) {
308 case MenuItem::Command: {
309 LyXFunc::func_status flag =
310 view->getLyXFunc()->getStatus(item.action());
312 // handle optional entries.
313 if (item.optional() && (flag & LyXFunc::Disabled)) {
315 << "Skipping optional item "
316 << item.label() << endl;
320 // Get the keys bound to this action, but keep only the
322 string accel = toplevel_keymap->findbinding(item.action());
323 // Build the menu label from all the info
324 string label = item.label();
326 if (!accel.empty()) {
327 // Try to be clever and add just enough
328 // tabs to align shortcuts.
331 while (string_width(label) < max_width);
332 label += accel.substr(1,accel.find(']') - 1);
334 label += "%x" + tostr(item.action()) + extra_label;
336 // Modify the entry using the function status
338 if (flag & (LyXFunc::Disabled | LyXFunc::Unknown))
340 if (flag & LyXFunc::ToggleOn)
342 if (flag & LyXFunc::ToggleOff)
346 // Finally the menu shortcut
347 string shortcut = item.shortcut();
349 if (!shortcut.empty()) {
350 xfshortcut += uppercase(shortcut[0]);
352 xfshortcut += uppercase(shortcut[0]);
353 xfshortcut += lowercase(shortcut[0]);
355 xfshortcut += lowercase(shortcut[0]);
356 lyxerr[Debug::GUI] << "shortcut is "
357 << xfshortcut << endl;
359 fl_addtopup(menu, strpool.add(label),
360 strpool.add(xfshortcut));
362 fl_addtopup(menu, strpool.add(label));
364 lyxerr[Debug::GUI] << "Command: \""
365 << lyxaction.getActionName(item.action())
366 << "\", Binding " << accel
367 << ", shortcut " << xfshortcut
374 case MenuItem::Submenu: {
375 int submenu = create_submenu(win, view,
380 string label = item.label();
381 label += extra_label + "%m";
382 string shortcut = item.shortcut();
383 int n = fl_addtopup(menu, strpool.add(label), submenu);
384 fl_setpup_shortcut(menu, n, strpool.add(shortcut));
388 case MenuItem::Separator:
389 // already done, and if it was the first one,
390 // we just ignore it.
393 case MenuItem::Documents:
394 add_documents(menu, extra_label, smn, strpool);
398 case MenuItem::Lastfiles:
399 add_lastfiles(menu, extra_label, smn, strpool);
408 void C_Menubar_Pimpl_MenuCallback(FL_OBJECT * ob, long button)
410 Menubar::Pimpl::MenuCallback(ob, button);
414 void Menubar::Pimpl::MenuCallback(FL_OBJECT * ob, long button)
416 ItemInfo * iteminfo = static_cast<ItemInfo *>(ob->u_vdata);
417 // lyxerr << "MenuCallback: ItemInfo address=" << iteminfo
418 // << "Val=(pimpl_=" << iteminfo->pimpl_
419 // << ", item_=" << iteminfo->item_
420 // << ", obj_=" << iteminfo->obj_ << ")" <<endl;
422 LyXView * view = iteminfo->pimpl_->owner_;
423 MenuItem const * item = iteminfo->item_;
426 // set the pseudo menu-button
427 fl_set_object_boxtype(ob, FL_DOWN_BOX);
428 fl_set_button(ob, 0);
429 fl_redraw_object(ob);
433 Assert(item->kind() == MenuItem::Submenu);
435 // set tabstop length
436 fl_set_tabstop(menu_tabstop);
437 std::vector<int> submenus;
439 int menu = iteminfo->pimpl_->
440 create_submenu(FL_ObjWin(ob), view,
445 fl_setpup_position(view->getForm()->x + ob->x,
446 view->getForm()->y + ob->y + ob->h + 10);
447 int choice = fl_dopup(menu);
450 // set the pseudo menu-button back
451 fl_set_object_boxtype(ob, FL_FLAT_BOX);
452 fl_redraw_object(ob);
456 view->getLyXFunc()->Dispatch(choice);
460 lyxerr << "Error in MenuCallback" << endl;
462 std::for_each(submenus.begin(), submenus.end(), fl_freepup);
463 // restore tabstop length
464 fl_set_tabstop(default_tabstop);