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 "lyx_gui_misc.h"
30 #include "MenuBackend.h"
31 #include "Menubar_pimpl.h"
35 extern kb_keymap * toplevel_keymap;
36 extern LyXAction lyxaction;
37 extern BufferList bufferlist;
38 extern LastFiles * lastfiles;
41 const int MENU_LABEL_SIZE = FL_NORMAL_SIZE;
42 const int mheight = 30;
43 const int mbheight= 22;
44 // where to place the menubar?
45 const int yloc = (mheight - mbheight)/2; //air + bw;
46 const int mbadd = 20; // menu button add (to width)
47 // Some space between buttons on the menubar
49 char const * menu_tabstop = "aa";
50 char const * default_tabstop = "aaaaaaaa";
55 void C_Menubar_Pimpl_MenuCallback(FL_OBJECT * ob, long button);
58 Menubar::Pimpl::Pimpl(LyXView * view, MenuBackend const & mb)
59 : frame_(0), owner_(view), menubackend_(&mb)
61 // Should we do something here?
64 Menubar::Pimpl::~Pimpl()
66 // Should we do something here?
69 void Menubar::Pimpl::set(string const & menu_name)
71 lyxerr[Debug::GUI] << "Entering Menubar::Pimpl::set "
72 << "for menu `" << menu_name << "'" << endl;
74 if (menu_name == current_menu) {
75 lyxerr[Debug::GUI] << "Nothing to do." << endl;
79 // If the backend has not been initialized yet, we use a
81 if (menubackend_->empty()) {
82 lyxerr << "Menubar::Pimpl::set: menubackend is empty! "
83 "using default values." << endl;
84 MenuBackend * mb = new MenuBackend();
89 if (!menubackend_->hasMenu(menu_name)){
90 lyxerr << "ERROR:set: Unknown menu `" << menu_name
95 Menu menu = menubackend_->getMenu(menu_name);
97 if (!menu.menubar()) {
98 lyxerr << "Only a menubar-type object can go in a "
99 "toplevel menu" << endl;
103 current_menu = menu_name;
104 FL_FORM * form = owner_->getForm();
106 bool form_was_open, form_was_frozen;
108 if (fl_current_form == form)
109 form_was_open = true;
110 else if (fl_current_form == 0) {
111 form_was_open = false;
115 lyxerr << "Something is wrong: unknown form "
116 << fl_current_form << " is already open"
117 << "(main form is " << form << ")" << endl;
121 form_was_frozen = true;
123 form_was_frozen = false;
124 fl_freeze_form(form);
127 // Delete old buttons if there are some.
128 for(ButtonList::const_iterator cit = buttonlist_.begin();
129 cit != buttonlist_.end(); ++cit) {
131 fl_delete_object((*cit)->obj_);
132 fl_free_object((*cit)->obj_);
138 // Create menu frame if there is non yet.
140 frame_ = fl_add_frame(FL_UP_FRAME, 0, 0, form->w, mheight, "");
141 fl_set_object_resize(frame_, FL_RESIZE_ALL);
142 fl_set_object_gravity(frame_, NorthWestGravity,
146 for (Menu::const_iterator i = menu.begin();
147 i != menu.end(); ++i) {
149 if (i->kind() != MenuItem::Submenu) {
150 lyxerr << "ERROR: Menubar::Pimpl::Pimpl:"
151 " only submenus can appear in a menubar";
154 char const * label = idex(i->label().c_str());
155 char const * shortcut = scex(i->label().c_str());
156 int width = fl_get_string_width(FL_NORMAL_STYLE,
160 obj = fl_add_button(FL_TOUCH_BUTTON,
165 fl_set_object_boxtype(obj, FL_FLAT_BOX);
166 fl_set_object_color(obj, FL_MCOL, FL_MCOL);
167 fl_set_object_lsize(obj, MENU_LABEL_SIZE);
168 fl_set_object_lstyle(obj, FL_NORMAL_STYLE);
169 fl_set_object_resize(obj, FL_RESIZE_ALL);
170 fl_set_object_gravity(obj, NorthWestGravity,
172 moffset += obj->w + air;
173 fl_set_object_shortcut(obj, shortcut, 1);
174 fl_set_object_callback(obj, C_Menubar_Pimpl_MenuCallback, 1);
175 ItemInfo * iteminfo = new ItemInfo(this,
176 new MenuItem(*i), obj);
177 buttonlist_.push_back(iteminfo);
178 obj->u_vdata = iteminfo;
179 // lyxerr << "MenuCallback: ItemInfo address=" << iteminfo
180 // << " Val=(pimpl_=" << iteminfo->pimpl_
181 // << ", item_=" << iteminfo->item_
182 // << ", obj_=" << iteminfo->obj_ << ")" <<endl;
185 if (!form_was_frozen) {
186 fl_unfreeze_form(form);
191 lyxerr[Debug::GUI] << "Menubar set." << endl;
194 void Menubar::Pimpl::openByName(string const & name)
196 for(ButtonList::const_iterator cit = buttonlist_.begin();
197 cit != buttonlist_.end(); ++cit) {
198 if ((*cit)->item_->submenu() == name) {
199 MenuCallback((*cit)->obj_, 1);
203 lyxerr << "Menubar::Pimpl::openByName: menu "
204 << name << " not found" << endl;
208 void Menubar::Pimpl::add_lastfiles(int menu, string const & extra_label,
209 std::vector<int> & /*smn*/,
213 for (LastFiles::const_iterator cit = lastfiles->begin();
214 cit != lastfiles->end() && ii < 10; ++cit, ++ii) {
217 lyxaction.getPseudoAction(LFUN_FILE_OPEN, (*cit));
218 string label = tostr(ii) + ". "
219 + MakeDisplayPath((*cit),30)
220 + "%x" + tostr(action);
221 if ((cit + 1) == lastfiles->end())
222 label += extra_label;
223 string shortcut = tostr(ii) + "#" + tostr(ii);
224 lyxerr[Debug::GUI] << "shortcut is " << shortcut <<
227 int n = fl_addtopup(menu, strpool.add(label));
228 fl_setpup_shortcut(menu, n, strpool.add(shortcut));
233 void Menubar::Pimpl::add_documents(int menu, string const & extra_label,
234 std::vector<int> & /*smn*/,
237 std::vector<string> names = bufferlist.getFileNames();
240 fl_addtopup(menu,_("No Documents Open!%i"));
244 for (std::vector<string>::const_iterator cit = names.begin();
245 cit != names.end() ; ++cit) {
247 lyxaction.getPseudoAction(LFUN_SWITCHBUFFER, *cit);
248 string label = MakeDisplayPath(*cit, 30)
249 + "%x" + tostr(action);
250 if ((cit + 1) == names.end())
251 label += extra_label;
253 fl_addtopup(menu, strpool.add(label));
259 int Menubar::Pimpl::create_submenu(Window win, LyXView * view,
260 string const & menu_name,
261 std::vector<int> & smn, StrPool & strpool)
263 if (!menubackend_->hasMenu(menu_name)){
264 lyxerr << "ERROR:create_submenu: Unknown menu `"
265 << menu_name << "'" << endl;
268 Menu md = menubackend_->getMenu(menu_name);
270 int menu = fl_newpup(win);
271 fl_setpup_softedge(menu, true);
272 fl_setpup_bw(menu, -1);
273 lyxerr[Debug::GUI] << "Adding menu " << menu
274 << " in deletion list" << endl;
277 // Compute the size of the largest label (because xforms is
278 // not able to support shortcuts correctly...)
279 int max_width = 0, max_tabs = 0;
280 int tab_width = fl_get_string_width(FL_NORMAL_STYLE, MENU_LABEL_SIZE,
281 menu_tabstop, strlen(menu_tabstop));
282 for (Menu::const_iterator i = md.begin(); i != md.end(); ++i) {
283 MenuItem item = (*i);
284 if (item.kind() == MenuItem::Command) {
285 string label = idex(item.label().c_str());
286 int width = fl_get_string_width(FL_NORMAL_STYLE,
290 if (width > max_width)
294 max_tabs = (max_width + 5)/tab_width + 1;
295 lyxerr[Debug::GUI] << "tab_width=" << tab_width
296 << ", max_width=" << max_width
297 << ", max_tabs=" << max_tabs << endl;
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());
311 // Get the keys bound to this action, but keep only the
313 string accel = toplevel_keymap->findbinding(item.action());
314 lyxerr[Debug::GUI] << "Command: "
315 << lyxaction.getActionName(item.action())
316 << " Binding " << accel << endl;
318 // Build the menu label from all the info
319 string label = idex(item.label().c_str());
321 if (!accel.empty()) {
322 // Try to be clever and add enough
323 // tabs to align shortcuts.
325 fl_get_string_width(FL_NORMAL_STYLE,
329 int nb = max_tabs - width/tab_width;
330 lyxerr[Debug::GUI] << "label=" << label
334 label += string(nb, '\t');
335 label += accel.substr(1,accel.find(']') - 1);
337 label += "%x" + tostr(item.action()) + extra_label;
339 // Modify the entry using the function status
341 if (flag & (LyXFunc::Disabled | LyXFunc::Unknown))
343 if (flag & LyXFunc::ToggleOn)
345 if (flag & LyXFunc::ToggleOff)
349 // Finally the menu shortcut
350 string shortcut = scex(item.label().c_str());
352 if (!shortcut.empty()) {
354 xfshortcut += uppercase(shortcut[0]);
356 xfshortcut += uppercase(shortcut[0]);
357 xfshortcut += lowercase(shortcut[0]);
359 xfshortcut += lowercase(shortcut[0]);
360 lyxerr[Debug::GUI] << "shortcut is "
361 << xfshortcut << endl;
363 fl_addtopup(menu, strpool.add(label),
364 strpool.add(xfshortcut));
366 fl_addtopup(menu, strpool.add(label));
371 case MenuItem::Submenu: {
372 int submenu = create_submenu(win, view,
377 string label = idex(item.label().c_str());
378 label += extra_label + "%m";
379 string shortcut = scex(item.label().c_str());
380 int n = fl_addtopup(menu, strpool.add(label), submenu);
381 fl_setpup_shortcut(menu, n, strpool.add(shortcut));
385 case MenuItem::Separator:
386 // already done, and if it was the first one,
387 // we just ignore it.
390 case MenuItem::Documents:
391 add_documents(menu, extra_label, smn, strpool);
395 case MenuItem::Lastfiles:
396 add_lastfiles(menu, extra_label, smn, strpool);
405 void C_Menubar_Pimpl_MenuCallback(FL_OBJECT * ob, long button)
407 Menubar::Pimpl::MenuCallback(ob, button);
411 void Menubar::Pimpl::MenuCallback(FL_OBJECT * ob, long button)
413 ItemInfo * iteminfo = static_cast<ItemInfo *>(ob->u_vdata);
414 // lyxerr << "MenuCallback: ItemInfo address=" << iteminfo
415 // << "Val=(pimpl_=" << iteminfo->pimpl_
416 // << ", item_=" << iteminfo->item_
417 // << ", obj_=" << iteminfo->obj_ << ")" <<endl;
419 LyXView * view = iteminfo->pimpl_->owner_;
420 MenuItem const * item = iteminfo->item_;
423 // set the pseudo menu-button
424 fl_set_object_boxtype(ob, FL_DOWN_BOX);
425 fl_set_button(ob, 0);
426 fl_redraw_object(ob);
430 Assert(item->kind() == MenuItem::Submenu);
432 // set tabstop length
433 fl_set_tabstop(menu_tabstop);
434 std::vector<int> submenus;
436 int menu = iteminfo->pimpl_->
437 create_submenu(FL_ObjWin(ob), view,
442 fl_setpup_position(view->getForm()->x + ob->x,
443 view->getForm()->y + ob->y + ob->h + 10);
444 int choice = fl_dopup(menu);
447 // set the pseudo menu-button back
448 fl_set_object_boxtype(ob, FL_FLAT_BOX);
449 fl_redraw_object(ob);
453 view->getLyXFunc()->Dispatch(choice);
457 lyxerr << "Error in MenuCallback" << endl;
459 std::for_each(submenus.begin(), submenus.end(), fl_freepup);
460 // restore tabstop length
461 fl_set_tabstop(default_tabstop);