]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/Menubar_pimpl.C
54ce4c9f706c78cf3f763441697a78e13ba41a9a
[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 "MenuBackend.h"
30 #include "Menubar_pimpl.h"
31 #include "exporter.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 Menubar::Pimpl::Pimpl(LyXView * view, MenuBackend const & mb) 
54         : frame_(0), owner_(view), menubackend_(&mb)
55 {
56         // Should we do something here?
57 }
58
59 Menubar::Pimpl::~Pimpl() 
60 {
61         // Should we do something here?
62 }
63
64 // This is used a few times below.
65 inline
66 int string_width(string const & str) 
67 {
68         return fl_get_string_widthTAB(FL_NORMAL_STYLE, MENU_LABEL_SIZE,
69                                       str.c_str(),      str.length());
70 }
71
72 //Defined later, used in set().
73 extern "C"
74 void C_Menubar_Pimpl_MenuCallback(FL_OBJECT * ob, long button);
75
76 void Menubar::Pimpl::set(string const & menu_name) 
77 {
78         lyxerr[Debug::GUI] << "Entering Menubar::Pimpl::set " 
79                            << "for menu `" << menu_name << "'" << endl;
80
81         if (menu_name == current_menu) {
82                 lyxerr[Debug::GUI] << "Nothing to do." << endl;
83                 return;
84         }
85
86         // If the backend has not been initialized yet, we use a
87         // default instead.  
88         if (menubackend_->empty()) {
89                 lyxerr << "Menubar::Pimpl::set: menubackend is empty! "
90                         "using default values." << endl;
91                 MenuBackend * mb = new MenuBackend();
92                 mb->defaults();
93                 menubackend_ = mb;
94         }
95
96         if (!menubackend_->hasMenu(menu_name)){ 
97                 lyxerr << "ERROR:set: Unknown menu `" << menu_name
98                        << "'" << endl;
99                 return;
100         }
101
102         Menu menu = menubackend_->getMenu(menu_name);
103
104         if (!menu.menubar()) {
105                 lyxerr << "Only a menubar-type object can go in a "
106                         "toplevel menu" << endl;
107                 return;
108         }
109
110         current_menu = menu_name;
111         FL_FORM * form = owner_->getForm(); 
112         int moffset = 0;
113         bool form_was_open, form_was_frozen;
114
115         if (fl_current_form == form)
116                 form_was_open = true;
117         else if (fl_current_form == 0) {
118                 form_was_open = false;
119                 fl_addto_form(form);
120         } 
121         else {
122                 lyxerr << "Something is wrong: unknown form " 
123                        << fl_current_form << " is already open" 
124                        << "(main form is " << form << ")" << endl;
125                 return;
126         }
127         if (form->frozen)
128                 form_was_frozen = true;
129         else {
130                 form_was_frozen = false;
131                 fl_freeze_form(form);
132         }
133
134         // Delete old buttons if there are some.
135         for(ButtonList::const_iterator cit = buttonlist_.begin();
136             cit != buttonlist_.end(); ++cit) {
137                 if ((*cit)->obj_) {
138                         fl_delete_object((*cit)->obj_);
139                         fl_free_object((*cit)->obj_);
140                 }
141                 delete (*cit);
142         }
143         buttonlist_.clear();
144
145         // Create menu frame if there is non yet.
146         if (!frame_) {
147                 frame_ = fl_add_frame(FL_UP_FRAME, 0, 0, form->w, mheight, "");
148                 fl_set_object_resize(frame_, FL_RESIZE_ALL);
149                 fl_set_object_gravity(frame_, NorthWestGravity, 
150                                       NorthEastGravity);
151         } 
152
153         for (Menu::const_iterator i = menu.begin(); 
154              i != menu.end(); ++i) {
155                 FL_OBJECT * obj;
156                 if (i->kind() != MenuItem::Submenu) {
157                         lyxerr << "ERROR: Menubar::Pimpl::Pimpl:"
158                                 " only submenus can appear in a menubar";
159                         break;
160                 }
161                 string label = i->label();
162                 string shortcut = '#' + i->shortcut();
163                 int width = string_width(label);
164                 obj = fl_add_button(FL_TOUCH_BUTTON,
165                                     air + moffset, yloc,
166                                     width + mbadd,
167                                     mbheight, 
168                                     label.c_str());
169                 fl_set_object_boxtype(obj, FL_FLAT_BOX);
170                 fl_set_object_color(obj, FL_MCOL, FL_MCOL);
171                 fl_set_object_lsize(obj, MENU_LABEL_SIZE);
172                 fl_set_object_lstyle(obj, FL_NORMAL_STYLE);
173                 fl_set_object_resize(obj, FL_RESIZE_ALL);
174                 fl_set_object_gravity(obj, NorthWestGravity, 
175                                       NorthWestGravity);
176                 moffset += obj->w + air;
177                 fl_set_object_shortcut(obj, shortcut.c_str(), 1);
178                 fl_set_object_callback(obj, C_Menubar_Pimpl_MenuCallback, 1);
179
180                 ItemInfo * iteminfo = new ItemInfo(this, 
181                                                    new MenuItem(*i), obj);
182                 buttonlist_.push_back(iteminfo);
183                 obj->u_vdata = iteminfo;
184 //              lyxerr << "MenuCallback: ItemInfo address=" << iteminfo
185 //                     << " Val=(pimpl_=" << iteminfo->pimpl_
186 //                     << ", item_=" << iteminfo->item_
187 //                     << ", obj_=" << iteminfo->obj_ << ")" <<endl;
188         }
189
190         if (!form_was_frozen) {
191                 fl_unfreeze_form(form);
192         }
193         if (!form_was_open) 
194                 fl_end_form();
195
196         // Force the redraw of the buttons (probably not the best
197         // method, but...) 
198         for(ButtonList::const_iterator cit = buttonlist_.begin();
199             cit != buttonlist_.end(); ++cit) {
200                 if ((*cit)->obj_) {
201                         fl_redraw_object((*cit)->obj_);
202                 }
203         }
204
205         lyxerr[Debug::GUI] << "Menubar set." << endl;
206
207
208 void Menubar::Pimpl::openByName(string const & name)
209 {
210         for(ButtonList::const_iterator cit = buttonlist_.begin();
211             cit != buttonlist_.end(); ++cit) {
212                 if ((*cit)->item_->submenu() == name) {
213                         MenuCallback((*cit)->obj_, 1);
214                         return;
215                 }
216         }
217         lyxerr << "Menubar::Pimpl::openByName: menu "
218                << name << " not found" << endl;
219 }
220
221
222 void Menubar::Pimpl::add_lastfiles(int menu, string const & extra_label,
223                                    std::vector<int> & /*smn*/, 
224                                    StrPool & strpool) 
225 {
226         int ii = 1;
227         for (LastFiles::const_iterator cit = lastfiles->begin();
228              cit != lastfiles->end() && ii < 10; ++cit, ++ii) {
229
230                 int action =
231                         lyxaction.getPseudoAction(LFUN_FILE_OPEN, (*cit));
232                 string label = tostr(ii) + ". "
233                         + MakeDisplayPath((*cit),30)
234                         + "%x" + tostr(action) + "%h";
235                 if ((cit + 1) == lastfiles->end())
236                         label += extra_label;
237                 string shortcut = tostr(ii) + "#" + tostr(ii); 
238                 lyxerr[Debug::GUI] << "shortcut is " << shortcut <<
239                         endl;
240
241                 fl_addtopup(menu, strpool.add(label), strpool.add(shortcut));
242         }
243
244 }
245
246 void Menubar::Pimpl::add_documents(int menu, string const & extra_label,
247                                    std::vector<int> & /*smn*/, 
248                                    StrPool & strpool) 
249 {
250         std::vector<string> names = bufferlist.getFileNames();
251
252         if (names.empty()) {
253                 fl_addtopup(menu,_("No Documents Open!%i"));
254                 return;
255         }
256
257         for (std::vector<string>::const_iterator cit = names.begin();
258              cit != names.end() ; ++cit) {
259                 int action =
260                         lyxaction.getPseudoAction(LFUN_SWITCHBUFFER, *cit);
261                 string label = MakeDisplayPath(*cit, 30)
262                         + "%x" + tostr(action);
263                 if ((cit + 1) == names.end())
264                         label += extra_label;
265                                 
266                 fl_addtopup(menu, strpool.add(label));
267         }
268
269 }
270
271
272 void Menubar::Pimpl::add_formats(int menu, string const & extra_label,
273                                  std::vector<int> & /*smn*/, 
274                                  StrPool & strpool,
275                                  kb_action action, bool viewable)
276 {
277         std::vector<pair<string,string> > names = 
278                 viewable
279                 ? Exporter::GetViewableFormats(owner_->buffer())
280                 : Exporter::GetExportableFormats(owner_->buffer());
281
282         for (std::vector<pair<string,string> >::const_iterator cit = names.begin();
283              cit != names.end() ; ++cit) {
284                 int action2 =
285                         lyxaction.getPseudoAction(action, (*cit).first);
286                 string label = (*cit).second
287                         + "%x" + tostr(action2);
288                 if ((cit + 1) == names.end())
289                         label += extra_label;
290                                 
291                 fl_addtopup(menu, strpool.add(label));
292         }
293 }
294
295
296 int Menubar::Pimpl::create_submenu(Window win, LyXView * view, 
297                                    string const & menu_name, 
298                                    std::vector<int> & smn, StrPool & strpool) 
299 {
300         if (!menubackend_->hasMenu(menu_name)){ 
301                 lyxerr << "ERROR:create_submenu: Unknown menu `" 
302                        << menu_name << "'" << endl;
303                 return -1;
304         }
305         Menu md = menubackend_->getMenu(menu_name);
306
307         int menu = fl_newpup(win);
308         fl_setpup_softedge(menu, true);
309         fl_setpup_bw(menu, -1);
310         lyxerr[Debug::GUI] << "Adding menu " << menu 
311                            << " in deletion list" << endl;
312         smn.push_back(menu);
313
314         // Compute the size of the largest label (because xforms is
315         // not able to support shortcuts correctly...)
316         int max_width = 0;
317         string widest_label;
318         Menu::const_iterator end = md.end();
319         for (Menu::const_iterator i = md.begin(); i != end; ++i) {
320                 MenuItem item = (*i);
321                 if (item.kind() == MenuItem::Command) {
322                         string label = item.label() + '\t';
323                         int width = string_width(label);
324                         if (width > max_width) {
325                                 max_width = width;
326                                 widest_label = label;
327                         }
328                 }
329         }
330         lyxerr[Debug::GUI] << "max_width=" << max_width 
331                            << ", widest_label=`" << widest_label 
332                            << "'" << endl;
333
334         for (Menu::const_iterator i = md.begin(); i != end; ++i) {
335                 MenuItem item = (*i);
336                 // Is there a separator after this item?
337                 string extra_label;
338                 if ((i+1) != end  
339                     && (i+1)->kind() == MenuItem::Separator)
340                         extra_label = "%l";
341
342                 switch(item.kind()) {
343                 case MenuItem::Command: {
344                         LyXFunc::func_status flag = 
345                                 view->getLyXFunc()->getStatus(item.action()); 
346
347                         // handle optional entries.
348                         if (item.optional() && (flag & LyXFunc::Disabled)) {
349                                 lyxerr[Debug::GUI] 
350                                         << "Skipping optional item " 
351                                         << item.label() << endl; 
352                                 break;
353                         }
354
355                         // Get the keys bound to this action, but keep only the
356                         // first one later
357                         string accel = toplevel_keymap->findbinding(item.action());
358                         // Build the menu label from all the info
359                         string label = item.label();
360
361                         if (!accel.empty()) {
362                                 // Try to be clever and add  just enough
363                                 // tabs to align shortcuts.
364                                 do 
365                                         label += '\t';
366                                 while (string_width(label) < max_width);
367                                 label += accel.substr(1,accel.find(']') - 1);
368                         }
369                         label += "%x" + tostr(item.action()) + extra_label;
370                         
371                         // Modify the entry using the function status
372                         string pupmode;
373                         if (flag & (LyXFunc::Disabled | LyXFunc::Unknown))
374                                 pupmode += "%i";
375                         if (flag & LyXFunc::ToggleOn)
376                                 pupmode += "%B";
377                         if (flag & LyXFunc::ToggleOff)
378                                 pupmode += "%b";
379                         label += pupmode;
380
381                         // Finally the menu shortcut
382                         string shortcut = item.shortcut();
383
384                         if (!shortcut.empty()) {
385                                 shortcut += lowercase(shortcut[0]);
386                                 label += "%h";
387                                 fl_addtopup(menu, strpool.add(label), 
388                                             strpool.add(shortcut));
389                         } else
390                                 fl_addtopup(menu, strpool.add(label));
391                         
392                         lyxerr[Debug::GUI] << "Command: \""  
393                                            << lyxaction.getActionName(item.action())
394                                            << "\", Binding " << accel 
395                                            << ", shortcut " << shortcut 
396                                            << endl;
397
398
399                         break;
400                 }
401
402                 case MenuItem::Submenu: {
403                         int submenu = create_submenu(win, view, 
404                                                      item.submenu(),
405                                                      smn, strpool);
406                         if (submenu == -1)
407                                 return -1;
408                         string label = item.label();
409                         label += extra_label + "%m";
410                         string shortcut = item.shortcut();
411                         if (!shortcut.empty()) {
412                                 shortcut += lowercase(shortcut[0]);
413                                 fl_addtopup(menu, strpool.add(label + "%h"),
414                                             submenu, strpool.add(shortcut));
415                         }
416                         else {
417                                 fl_addtopup(menu, strpool.add(label), submenu);
418                         }
419                         break;
420                 }
421
422                 case MenuItem::Separator:
423                         // already done, and if it was the first one,
424                         // we just ignore it.
425                         break;
426
427                 case MenuItem::Documents: 
428                         add_documents(menu, extra_label, smn, strpool);
429                         break;
430
431                 case MenuItem::Lastfiles: 
432                         add_lastfiles(menu, extra_label, smn, strpool);
433                         break;
434
435                 case MenuItem::ViewFormats:
436                         add_formats(menu, extra_label, smn, strpool,
437                                     LFUN_PREVIEW, true);
438                         break;
439
440                 case MenuItem::UpdateFormats:
441                         add_formats(menu, extra_label, smn, strpool,
442                                     LFUN_UPDATE, true);
443                         break;  
444
445                 case MenuItem::ExportFormats:
446                         add_formats(menu, extra_label, smn, strpool,
447                                     LFUN_EXPORT, false);
448                         break;
449
450                 }
451         }
452         return menu;
453 }
454
455 extern "C"
456 void C_Menubar_Pimpl_MenuCallback(FL_OBJECT * ob, long button)
457 {
458         Menubar::Pimpl::MenuCallback(ob, button);
459 }
460
461
462 void Menubar::Pimpl::MenuCallback(FL_OBJECT * ob, long button)
463 {
464         ItemInfo * iteminfo = static_cast<ItemInfo *>(ob->u_vdata);
465 //      lyxerr << "MenuCallback: ItemInfo address=" << iteminfo
466 //             << "Val=(pimpl_=" << iteminfo->pimpl_
467 //             << ", item_=" << iteminfo->item_
468 //             << ", obj_=" << iteminfo->obj_ << ")" <<endl;
469
470         LyXView * view = iteminfo->pimpl_->owner_;
471         MenuItem const * item = iteminfo->item_;
472
473         if (button == 1) {
474                 // set the pseudo menu-button
475                 fl_set_object_boxtype(ob, FL_DOWN_BOX);
476                 fl_set_button(ob, 0);
477                 fl_redraw_object(ob);
478         }
479
480         // Paranoia check
481         Assert(item->kind() == MenuItem::Submenu);
482         
483         // set tabstop length
484         fl_set_tabstop(menu_tabstop);
485         std::vector<int> submenus;
486         StrPool strpool;
487         int menu = iteminfo->pimpl_->
488                 create_submenu(FL_ObjWin(ob), view, 
489                                item->submenu(), 
490                                submenus, strpool);
491         if (menu != -1) {
492                 // place popup
493                 fl_setpup_position(view->getForm()->x + ob->x,
494                                    view->getForm()->y + ob->y + ob->h + 10);   
495                 int choice = fl_dopup(menu);
496                 
497                 if (button == 1) {
498                                 // set the pseudo menu-button back
499                         fl_set_object_boxtype(ob, FL_FLAT_BOX);
500                         fl_redraw_object(ob);
501                 }
502                 
503                 if (choice >= 1) {
504                         view->getLyXFunc()->Dispatch(choice);
505                 }
506         }
507         else 
508                 lyxerr << "Error in MenuCallback" << endl;
509         
510         std::for_each(submenus.begin(), submenus.end(), fl_freepup);
511         // restore tabstop length
512         fl_set_tabstop(default_tabstop);
513
514 }