]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/Menubar_pimpl.C
d3bd54caae96d2ae0993d6acec6c9bed4fd01bc4
[lyx.git] / src / frontends / xforms / Menubar_pimpl.C
1 /* This file is part of
2 * ======================================================
3
4 *           LyX, The Document Processor
5 *        
6 *           Copyright (C) 1999 The LyX Team.
7 *
8 *======================================================*/
9
10 #ifdef __GNUG__
11 #pragma implementation
12 #endif
13
14 #include <config.h>
15
16 #include <algorithm>
17 #include <cctype>
18 #include "support/lstrings.h"
19 #include "support/filetools.h"
20 #include "support/StrPool.h"
21 #include "support/LAssert.h"
22 #include "debug.h"
23 #include "LyXAction.h"
24 #include "lyxfunc.h"
25 #include "kbmap.h"
26 #include "bufferlist.h"
27 #include "lastfiles.h"
28 #include "LyXView.h"
29 #include "lyx_gui_misc.h"
30 #include "MenuBackend.h"
31 #include "Menubar_pimpl.h"
32
33 using std::endl;
34
35 extern kb_keymap * toplevel_keymap;
36 extern LyXAction lyxaction;
37 extern BufferList bufferlist;
38 extern LastFiles * lastfiles; 
39
40 // Some constants
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 
48 const int air = 2;
49 char const * menu_tabstop = "aa";
50 char const * default_tabstop = "aaaaaaaa";
51
52
53 //Defined later.
54 extern "C"
55 void C_Menubar_Pimpl_MenuCallback(FL_OBJECT * ob, long button);
56
57
58 Menubar::Pimpl::Pimpl(LyXView * view, MenuBackend const & mb) 
59         : frame_(0), owner_(view), menubackend_(&mb)
60 {
61         // Should we do something here?
62 }
63
64 Menubar::Pimpl::~Pimpl() 
65 {
66         // Should we do something here?
67 }
68
69 void Menubar::Pimpl::set(string const & menu_name) 
70 {
71         lyxerr[Debug::GUI] << "Entering Menubar::Pimpl::set " 
72                            << "for menu `" << menu_name << "'" << endl;
73
74         if (menu_name == current_menu) {
75                 lyxerr[Debug::GUI] << "Nothing to do." << endl;
76                 return;
77         }
78
79         // If the backend has not been initialized yet, we use a
80         // default instead.  
81         if (menubackend_->empty()) {
82                 lyxerr << "Menubar::Pimpl::set: menubackend is empty! "
83                         "using default values." << endl;
84                 MenuBackend * mb = new MenuBackend();
85                 mb->defaults();
86                 menubackend_ = mb;
87         }
88
89         if (!menubackend_->hasMenu(menu_name)){ 
90                 lyxerr << "ERROR:set: Unknown menu `" << menu_name
91                        << "'" << endl;
92                 return;
93         }
94
95         Menu menu = menubackend_->getMenu(menu_name);
96
97         if (!menu.menubar()) {
98                 lyxerr << "Only a menubar-type object can go in a "
99                         "toplevel menu" << endl;
100                 return;
101         }
102
103         current_menu = menu_name;
104         FL_FORM * form = owner_->getForm(); 
105         int moffset = 0;
106         bool form_was_open, form_was_frozen;
107
108         if (fl_current_form == form)
109                 form_was_open = true;
110         else if (fl_current_form == 0) {
111                 form_was_open = false;
112                 fl_addto_form(form);
113         } 
114         else {
115                 lyxerr << "Something is wrong: unknown form " 
116                        << fl_current_form << " is already open" 
117                        << "(main form is " << form << ")" << endl;
118                 return;
119         }
120         if (form->frozen)
121                 form_was_frozen = true;
122         else {
123                 form_was_frozen = false;
124                 fl_freeze_form(form);
125         }
126
127         // Delete old buttons if there are some.
128         for(ButtonList::const_iterator cit = buttonlist_.begin();
129             cit != buttonlist_.end(); ++cit) {
130                 if ((*cit)->obj_) {
131                         fl_delete_object((*cit)->obj_);
132                         fl_free_object((*cit)->obj_);
133                 }
134                 delete (*cit);
135         }
136         buttonlist_.clear();
137
138         // Create menu frame if there is non yet.
139         if (!frame_) {
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, 
143                                       NorthEastGravity);
144         } 
145
146         for (Menu::const_iterator i = menu.begin(); 
147              i != menu.end(); ++i) {
148                 FL_OBJECT * obj;
149                 if (i->kind() != MenuItem::Submenu) {
150                         lyxerr << "ERROR: Menubar::Pimpl::Pimpl:"
151                                 " only submenus can appear in a menubar";
152                         break;
153                 }
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,
157                                                 MENU_LABEL_SIZE,
158                                                 label,
159                                                 strlen(label));
160                 obj = fl_add_button(FL_TOUCH_BUTTON,
161                                     air + moffset, yloc,
162                                     width + mbadd,
163                                     mbheight, 
164                                     label);
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, 
171                                       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;
183         }
184
185         if (!form_was_frozen) {
186                 fl_unfreeze_form(form);
187         }
188         if (!form_was_open) 
189                 fl_end_form();
190
191         lyxerr[Debug::GUI] << "Menubar set." << endl;
192
193
194 void Menubar::Pimpl::openByName(string const & name)
195 {
196         for(ButtonList::const_iterator cit = buttonlist_.begin();
197             cit != buttonlist_.end(); ++cit) {
198                 if ((*cit)->item_->submenu() == name) {
199                         MenuCallback((*cit)->obj_, 1);
200                         return;
201                 }
202         }
203         lyxerr << "Menubar::Pimpl::openByName: menu "
204                << name << " not found" << endl;
205 }
206
207
208 void Menubar::Pimpl::add_lastfiles(int menu, string const & extra_label,
209                                    std::vector<int> & /*smn*/, 
210                                    StrPool & strpool) 
211 {
212         int ii = 1;
213         for (LastFiles::const_iterator cit = lastfiles->begin();
214              cit != lastfiles->end() && ii < 10; ++cit, ++ii) {
215
216                 int action =
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 <<
225                         endl;
226
227                 int n = fl_addtopup(menu, strpool.add(label));
228                 fl_setpup_shortcut(menu, n, strpool.add(shortcut));
229         }
230
231 }
232
233 void Menubar::Pimpl::add_documents(int menu, string const & extra_label,
234                                    std::vector<int> & /*smn*/, 
235                                    StrPool & strpool) 
236 {
237         std::vector<string> names = bufferlist.getFileNames();
238
239         if (names.empty()) {
240                 fl_addtopup(menu,_("No Documents Open!%i"));
241                 return;
242         }
243
244         for (std::vector<string>::const_iterator cit = names.begin();
245              cit != names.end() ; ++cit) {
246                 int action =
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;
252                                 
253                 fl_addtopup(menu, strpool.add(label));
254         }
255
256 }
257
258
259 int Menubar::Pimpl::create_submenu(Window win, LyXView * view, 
260                                    string const & menu_name, 
261                                    std::vector<int> & smn, StrPool & strpool) 
262 {
263         if (!menubackend_->hasMenu(menu_name)){ 
264                 lyxerr << "ERROR:create_submenu: Unknown menu `" 
265                        << menu_name << "'" << endl;
266                 return -1;
267         }
268         Menu md = menubackend_->getMenu(menu_name);
269
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;
275         smn.push_back(menu);
276
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,
287                                                         MENU_LABEL_SIZE,
288                                                         label.c_str(),
289                                                         label.length());
290                         if (width > max_width)
291                                 max_width = width;
292                 }
293         }
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;
298
299         for (Menu::const_iterator i = md.begin(); i != md.end(); ++i) {
300                 MenuItem item = (*i);
301                 // Is there a separator after this item?
302                 string extra_label;
303                 if ((i+1) != md.end()  
304                     && (i+1)->kind() == MenuItem::Separator)
305                         extra_label = "%l";
306
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
312                         // first one later
313                         string accel = toplevel_keymap->findbinding(item.action());
314                         lyxerr[Debug::GUI] << "Command: "  
315                                            << lyxaction.getActionName(item.action())
316                                            << " Binding " << accel << endl;
317
318                         // Build the menu label from all the info
319                         string label = idex(item.label().c_str());
320
321                         if (!accel.empty()) {
322                                 // Try to be clever and add enough
323                                 // tabs to align shortcuts.
324                                 int width = 
325                                         fl_get_string_width(FL_NORMAL_STYLE,
326                                                             MENU_LABEL_SIZE,
327                                                             label.c_str(),
328                                                             label.length());
329                                 int nb = max_tabs - width/tab_width;
330                                 lyxerr[Debug::GUI] << "label=" << label 
331                                                    << ", tabs=" << nb 
332                                                    << endl;
333                                 if (nb > 0)
334                                         label += string(nb, '\t');
335                                 label += accel.substr(1,accel.find(']') - 1);
336                         }
337                         label += "%x" + tostr(item.action()) + extra_label;
338                         
339                         // Modify the entry using the function status
340                         string pupmode;
341                         if (flag & (LyXFunc::Disabled | LyXFunc::Unknown))
342                                 pupmode += "%i";
343                         if (flag & LyXFunc::ToggleOn)
344                                 pupmode += "%B";
345                         if (flag & LyXFunc::ToggleOff)
346                                 pupmode += "%b";
347                         label += pupmode;
348
349                         // Finally the menu shortcut
350                         string shortcut = scex(item.label().c_str());
351
352                         if (!shortcut.empty()) {
353                                 string xfshortcut;
354                                 xfshortcut += uppercase(shortcut[0]);
355                                 xfshortcut += '#';
356                                 xfshortcut += uppercase(shortcut[0]);
357                                 xfshortcut += lowercase(shortcut[0]);
358                                 xfshortcut += '#';
359                                 xfshortcut += lowercase(shortcut[0]);
360                                 lyxerr[Debug::GUI] << "shortcut is " 
361                                                    << xfshortcut << endl;
362                                 label += "%h";
363                                 fl_addtopup(menu, strpool.add(label), 
364                                             strpool.add(xfshortcut));
365                         } else
366                                 fl_addtopup(menu, strpool.add(label));
367
368                         break;
369                 }
370
371                 case MenuItem::Submenu: {
372                         int submenu = create_submenu(win, view, 
373                                                      item.submenu(),
374                                                      smn, strpool);
375                         if (submenu == -1)
376                                 return -1;
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));
382                         break;
383                 }
384
385                 case MenuItem::Separator:
386                         // already done, and if it was the first one,
387                         // we just ignore it.
388                         break;
389
390                 case MenuItem::Documents: 
391                         add_documents(menu, extra_label, smn, strpool);
392                         break;
393
394
395                 case MenuItem::Lastfiles: 
396                         add_lastfiles(menu, extra_label, smn, strpool);
397                         break;
398
399                 }
400         }
401         return menu;
402 }
403
404 extern "C"
405 void C_Menubar_Pimpl_MenuCallback(FL_OBJECT * ob, long button)
406 {
407         Menubar::Pimpl::MenuCallback(ob, button);
408 }
409
410
411 void Menubar::Pimpl::MenuCallback(FL_OBJECT * ob, long button)
412 {
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;
418
419         LyXView * view = iteminfo->pimpl_->owner_;
420         MenuItem const * item = iteminfo->item_;
421
422         if (button == 1) {
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);
427         }
428
429         // Paranoia check
430         Assert(item->kind() == MenuItem::Submenu);
431         
432         // set tabstop length
433         fl_set_tabstop(menu_tabstop);
434         std::vector<int> submenus;
435         StrPool strpool;
436         int menu = iteminfo->pimpl_->
437                 create_submenu(FL_ObjWin(ob), view, 
438                                item->submenu(), 
439                                submenus, strpool);
440         if (menu != -1) {
441                 // place popup
442                 fl_setpup_position(view->getForm()->x + ob->x,
443                                    view->getForm()->y + ob->y + ob->h + 10);   
444                 int choice = fl_dopup(menu);
445                 
446                 if (button == 1) {
447                                 // set the pseudo menu-button back
448                         fl_set_object_boxtype(ob, FL_FLAT_BOX);
449                         fl_redraw_object(ob);
450                 }
451                 
452                 if (choice >= 1) {
453                         view->getLyXFunc()->Dispatch(choice);
454                 }
455         }
456         else 
457                 lyxerr << "Error in MenuCallback" << endl;
458         
459         std::for_each(submenus.begin(), submenus.end(), fl_freepup);
460         // restore tabstop length
461         fl_set_tabstop(default_tabstop);
462
463 }