]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/Menubar_pimpl.C
f1a66d1d35ddd237683ccdf70aaa5edf93494f60
[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 "support/lstrings.h"
18 #include "support/LAssert.h"
19 #include "debug.h"
20 #include "LyXAction.h"
21 #include "lyxfunc.h"
22 #include "kbmap.h"
23 #include "buffer.h"
24 #include "LyXView.h"
25 #include "MenuBackend.h"
26 #include "Menubar_pimpl.h"
27
28 using std::endl;
29 using std::vector;
30 using std::max;
31 using std::min;
32
33 typedef vector<int>::size_type size_type;
34
35 extern boost::scoped_ptr<kb_keymap> toplevel_keymap;
36 extern LyXAction lyxaction;
37
38 // Some constants
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
52 // constant. 
53 static const int action_offset = 1000;
54
55
56 //Defined later, used in makeMenubar().
57 extern "C"
58 void C_Menubar_Pimpl_MenuCallback(FL_OBJECT * ob, long button);
59
60 // This is used a few times below.
61 static inline
62 int string_width(string const & str) 
63 {
64         return fl_get_string_widthTAB(FL_NORMAL_STYLE, MENU_LABEL_SIZE,
65                                       str.c_str(),
66                                       static_cast<int>(str.length()));
67 }
68
69
70 Menubar::Pimpl::Pimpl(LyXView * view, MenuBackend const & mb) 
71         : owner_(view), menubackend_(&mb), current_group_(0)
72 {
73         for (MenuBackend::const_iterator menu = menubackend_->begin();
74             menu != menubackend_->end() ; ++menu) {
75                 if (menu->menubar()) {
76                         FL_OBJECT * group = fl_bgn_group();
77                         makeMenubar(*menu);
78                         fl_end_group();
79                         fl_hide_object(group);
80                         lyxerr[Debug::GUI]
81                                 << "Menubar::Pimpl::Pimpl: "
82                                 << "creating and hiding group " << group
83                                 << " for menubar " << menu->name() << endl;
84                         menubarmap_[menu->name()] = group;
85                 }
86         }
87 }
88
89
90 void Menubar::Pimpl::makeMenubar(Menu const &menu)
91 {
92         FL_FORM * form = owner_->getForm(); 
93         int moffset = 0;
94
95         // Create menu frame if there is non yet.
96         FL_OBJECT * frame = fl_add_frame(FL_UP_FRAME, 0, 0,
97                                          form->w, mheight, "");
98         fl_set_object_resize(frame, FL_RESIZE_ALL);
99         fl_set_object_gravity(frame, NorthWestGravity, 
100                               NorthEastGravity);
101
102         for (Menu::const_iterator i = menu.begin(); 
103              i != menu.end(); ++i) {
104                 FL_OBJECT * obj;
105                 if (i->kind() != MenuItem::Submenu) {
106                         lyxerr << "ERROR: Menubar::Pimpl::createMenubar:"
107                                 " only submenus can appear in a menubar";
108                         break;
109                 }
110                 string label = i->label();
111                 string shortcut = "#" + i->shortcut();
112                 int width = string_width(label);
113                 obj = fl_add_button(FL_MENU_BUTTON,
114                                     air + moffset, yloc,
115                                     width + mbadd,
116                                     mbheight, 
117                                     label.c_str());
118                 fl_set_object_boxtype(obj, FL_FLAT_BOX);
119                 fl_set_object_color(obj, FL_MCOL, FL_MCOL);
120                 fl_set_object_lsize(obj, MENU_LABEL_SIZE);
121                 fl_set_object_lstyle(obj, FL_NORMAL_STYLE);
122                 fl_set_object_resize(obj, FL_RESIZE_ALL);
123                 fl_set_object_gravity(obj, NorthWestGravity, 
124                                       NorthWestGravity);
125                 moffset += obj->w + air;
126                 fl_set_object_shortcut(obj, shortcut.c_str(), 1);
127                 fl_set_object_callback(obj, C_Menubar_Pimpl_MenuCallback, 1);
128
129                 boost::shared_ptr<ItemInfo> iteminfo(new ItemInfo(this, 
130                                                    new MenuItem(*i), obj));
131                 buttonlist_.push_back(iteminfo);
132                 obj->u_vdata = iteminfo.get();
133         }
134
135 }
136
137 void Menubar::Pimpl::set(string const & menu_name) 
138 {
139         lyxerr[Debug::GUI] << "Entering Menubar::Pimpl::set " 
140                            << "for menu `" << menu_name << "'" << endl;
141
142         if (menu_name != current_menu_name_) {
143                 MenubarMap::iterator mbit = menubarmap_.find(menu_name);
144
145                 if (mbit == menubarmap_.end()) {
146                         lyxerr << "ERROR:set: Unknown menu `" << menu_name
147                                << "'" << endl;
148                         return;
149                 }
150
151                 if (current_group_) {
152                         lyxerr[Debug::GUI] << "  hiding group "
153                                            << current_group_ << endl;
154                         fl_hide_object(current_group_);
155                 }
156                 
157                 lyxerr[Debug::GUI] << "  showing group "
158                                    << mbit->second << endl;
159                 fl_show_object(mbit->second);
160                 current_menu_name_ = menu_name;
161                 current_group_ = mbit->second;
162                 lyxerr[Debug::GUI] << "Menubar::Pimpl::set: Menubar set."
163                                    << endl;
164         }
165         else
166                 lyxerr [Debug::GUI] << "Menubar::Pimpl::set: Nothing to do."
167                                     << endl;
168
169
170 void Menubar::Pimpl::openByName(string const & name)
171 {
172         if (menubackend_->getMenu(current_menu_name_).hasSubmenu(name)) {
173                 for (ButtonList::const_iterator cit = buttonlist_.begin();
174                      cit != buttonlist_.end(); ++cit) {
175                         if ((*cit)->item_->submenu() == name) {
176                                 MenuCallback((*cit)->obj_, 1);
177                                 return;
178                         }
179                 }
180         }
181         lyxerr << "Menubar::Pimpl::openByName: menu "
182                << name << " not found" << endl;
183 }
184
185
186 static inline
187 string const limit_string_length(string const & str)
188 {
189         string::size_type const max_item_length = 45;
190
191         if (str.size() > max_item_length)
192                 return str.substr(0, max_item_length - 3) + "...";
193         else
194                 return str;
195 }
196
197
198 static
199 int get_new_submenu(vector<int> & smn, Window win)
200 {
201         static size_type max_number_of_menus = 32;
202         if (smn.size() >= max_number_of_menus)
203                 max_number_of_menus =
204                     fl_setpup_maxpup(static_cast<int>(2*smn.size()));
205         int menu = fl_newpup(win);
206         smn.push_back(menu);
207         return menu;
208 }
209
210
211 size_type const max_number_of_items = 25;
212
213 static inline
214 string const fixlabel(string const & str)
215 {
216 #if FL_REVISION < 89
217         return subst(str, '%', '?');
218 #else
219         return subst(str, "%", "%%");
220 #endif
221 }
222
223
224 void add_toc2(int menu, string const & extra_label,
225               vector<int> & smn, Window win,
226               vector<Buffer::TocItem> const & toc_list,
227               size_type from, size_type to, int depth)
228 {
229         int shortcut_count = 0;
230         if (to - from <= max_number_of_items) {
231                 for (size_type i = from; i < to; ++i) {
232                         int const action = lyxaction.
233                                 getPseudoAction(LFUN_GOTO_PARAGRAPH,
234                                                 tostr(toc_list[i].par->id()));
235                         string label(4 * max(0, toc_list[i].depth - depth),' ');
236                         label += fixlabel(toc_list[i].str);
237                         label = limit_string_length(label);
238                         label += "%x" + tostr(action + action_offset);
239                         if (i == to - 1 && depth == 0)
240                                 label += extra_label;
241                         if (toc_list[i].depth == depth
242                             && ++shortcut_count <= 9) {
243                                 label += "%h";
244                                 fl_addtopup(menu, label.c_str(),
245                                             tostr(shortcut_count).c_str());
246                         } else
247                                 fl_addtopup(menu, label.c_str());
248                 }
249         } else {
250                 size_type pos = from;
251                 size_type count = 0;
252                 while (pos < to) {
253                         ++count;
254                         if (count > max_number_of_items) {
255                                 int menu2 = get_new_submenu(smn, win);
256                                 add_toc2(menu2, extra_label, smn, win,
257                                          toc_list, pos, to, depth);
258                                 string label = _("More");
259                                 label += "...%m";
260                                 if (depth == 0)
261                                         label += extra_label;
262                                 fl_addtopup(menu, label.c_str(), menu2);
263                                 break;
264                         }
265                         size_type new_pos = pos+1;
266                         while (new_pos < to &&
267                                toc_list[new_pos].depth > depth)
268                                 ++new_pos;
269
270                         int const action = lyxaction.
271                                 getPseudoAction(LFUN_GOTO_PARAGRAPH,
272                                                 tostr(toc_list[pos].par->id()));
273                         string label(4 * max(0, toc_list[pos].depth - depth), ' ');
274                         label += fixlabel(toc_list[pos].str);
275                         label = limit_string_length(label);
276                         if (new_pos == to && depth == 0)
277                                 label += extra_label;
278                         string shortcut;
279                         if (toc_list[pos].depth == depth &&
280                             ++shortcut_count <= 9)
281                                 shortcut = tostr(shortcut_count);
282
283                         if (new_pos == pos + 1) {
284                                 label += "%x" + tostr(action + action_offset);
285                                 if (!shortcut.empty()) {
286                                         label += "%h";
287                                         fl_addtopup(menu, label.c_str(),
288                                                     shortcut.c_str());
289                                 } else
290                                         fl_addtopup(menu, label.c_str());
291                         } else {
292                                 int menu2 = get_new_submenu(smn, win);
293                                 add_toc2(menu2, extra_label, smn, win,
294                                          toc_list, pos, new_pos, depth+1);
295                                 label += "%m";
296                                 if (!shortcut.empty()) {
297                                         label += "%h";
298                                         fl_addtopup(menu, label.c_str(), menu2,
299                                                     shortcut.c_str());
300                                 } else
301                                         fl_addtopup(menu, label.c_str(), menu2);
302                         }
303                         pos = new_pos;
304                 }
305         }
306 }
307
308
309 void Menubar::Pimpl::add_toc(int menu, string const & extra_label,
310                              vector<int> & smn, Window win)
311 {
312 #if 0
313         //xgettext:no-c-format
314         static char const * MenuNames[3] = { N_("List of Figures%m"),
315         //xgettext:no-c-format
316                                              N_("List of Tables%m"),
317         //xgettext:no-c-format
318                                              N_("List of Algorithms%m") };
319
320         vector<vector<Buffer::TocItem> > toc_list =
321                 owner_->buffer()->getTocList();
322
323         // Handle LOF/LOT/LOA
324         int max_nonempty = 0;
325         for (int i = 1; i <= 3; ++i)
326                 if (!toc_list[i].empty())
327                         max_nonempty = i;
328
329         for (int j = 1; j <= 3; ++j)
330                 if (!toc_list[j].empty()) {
331                         int menu2 = get_new_submenu(smn, win);
332                         for (size_type i = 0; i < toc_list[j].size(); ++i) {
333                                 if (i > max_number_of_items) {
334                                         fl_addtopup(menu2, ". . .%d");
335                                         break;
336                                 }
337                                 int const action = lyxaction.
338                                         getPseudoAction(LFUN_GOTO_PARAGRAPH,
339                                                         tostr(toc_list[j][i].par->id()));
340                                 string label = fixlabel(toc_list[j][i].str);
341                                 label = limit_string_length(label);
342                                 label += "%x" + tostr(action + action_offset);
343                                 fl_addtopup(menu2, label.c_str());
344                         }
345                         if (j == max_nonempty) {
346                                 string label = _(MenuNames[j - 1]);
347                                 label += "%l";
348                                 fl_addtopup(menu, label.c_str(), menu2);
349                         } else
350                                 fl_addtopup(menu, _(MenuNames[j - 1]), menu2);
351                 }
352
353         // Handle normal TOC
354         if (max_nonempty == 0 && toc_list[0].empty()) {
355                 fl_addtopup(menu, (_("No Table of Contents%i")
356                                    + extra_label).c_str());
357                 return;
358         }
359
360         add_toc2(menu, extra_label, smn, win,
361                  toc_list[0], 0, toc_list[0].size(), 0);
362 #else
363 #warning Fix Me! (Lgb)
364         Buffer::Lists toc_list = owner_->buffer()->getLists();
365         Buffer::Lists::const_iterator cit = toc_list.begin();
366         Buffer::Lists::const_iterator end = toc_list.end();
367         for (; cit != end; ++cit) {
368                 // Handle this elsewhere
369                 if (cit->first == "TOC") continue;
370                 
371                 int menu2 = get_new_submenu(smn, win);
372                 Buffer::SingleList::const_iterator ccit = cit->second.begin();
373                 Buffer::SingleList::const_iterator eend = cit->second.end();
374                 for (; ccit != eend; ++ccit) {
375                         int const action =
376                                 lyxaction
377                                 .getPseudoAction(LFUN_GOTO_PARAGRAPH,
378                                                  tostr(ccit->par->id()));
379                         string label = fixlabel(ccit->str);
380                         label = limit_string_length(label);
381                         label += "%x" + tostr(action + action_offset);
382                         fl_addtopup(menu2, label.c_str());
383                         lyxerr << "[" << cit->first << "] " << label << endl;
384                 }
385                 string const m = cit->first + "%m";
386                 fl_addtopup(menu, m.c_str(), menu2);
387         }
388         
389         
390         // Handle normal TOC
391         cit = toc_list.find("TOC");
392         if (cit == end) {
393                 string const tmp = _("No Table of contents%i") + extra_label;
394                 fl_addtopup(menu, tmp.c_str());
395                 return;
396         } else {
397                 add_toc2(menu, extra_label, smn, win,
398                          cit->second, 0, cit->second.size(), 0);
399         }
400         
401 #endif
402 }
403
404 int Menubar::Pimpl::create_submenu(Window win, LyXView * view, 
405                                    string const & menu_name, 
406                                    vector<int> & smn) 
407 {
408         if (!menubackend_->hasMenu(menu_name)){ 
409                 lyxerr << "ERROR:create_submenu: Unknown menu `" 
410                        << menu_name << "'" << endl;
411                 return -1;
412         }
413         Menu md = Menu();
414         menubackend_->getMenu(menu_name).expand(md, owner_->buffer());
415
416         int menu = get_new_submenu(smn, win);
417         fl_setpup_softedge(menu, true);
418         fl_setpup_bw(menu, -1);
419         lyxerr[Debug::GUI] << "Adding menu " << menu 
420                            << " in deletion list" << endl;
421
422         // Compute the size of the largest label (because xforms is
423         // not able to support shortcuts correctly...)
424         int max_width = 0;
425         string widest_label;
426         Menu::const_iterator end = md.end();
427         for (Menu::const_iterator i = md.begin(); i != end; ++i) {
428                 MenuItem const & item = (*i);
429                 if (item.kind() == MenuItem::Command) {
430                         string label = item.label() + '\t';
431                         int width = string_width(label);
432                         if (width > max_width) {
433                                 max_width = width;
434                                 widest_label = label;
435                         }
436                 }
437         }
438         lyxerr[Debug::GUI] << "max_width=" << max_width 
439                            << ", widest_label=`" << widest_label 
440                            << "'" << endl;
441
442         // Compute where to put separators
443         vector<string> extra_labels(md.size());
444         vector<string>::iterator it = extra_labels.begin();
445         vector<string>::iterator last = it;
446         for (Menu::const_iterator i = md.begin(); i != end; ++i, ++it)
447                 if (i->kind() == MenuItem::Separator)
448                         *last = "%l";
449                 else if (!i->optional() ||
450                          !(view->getLyXFunc()->getStatus(i->action()) & LyXFunc::Disabled))
451                         last = it;
452
453         it = extra_labels.begin();
454         for (Menu::const_iterator i = md.begin(); i != end; ++i, ++it) {
455                 MenuItem const & item = (*i);
456                 string & extra_label = *it;
457
458                 switch (item.kind()) {
459                 case MenuItem::Command: {
460                         LyXFunc::func_status flag = 
461                                 view->getLyXFunc()->getStatus(item.action()); 
462
463                         // handle optional entries.
464                         if (item.optional() && (flag & LyXFunc::Disabled)) {
465                                 lyxerr[Debug::GUI] 
466                                         << "Skipping optional item " 
467                                         << item.label() << endl; 
468                                 break;
469                         }
470
471                         // Get the keys bound to this action, but keep only the
472                         // first one later
473                         string const accel = toplevel_keymap->findbinding(item.action());
474                         // Build the menu label from all the info
475                         string label = item.label();
476
477                         if (!accel.empty()) {
478                                 // Try to be clever and add  just enough
479                                 // tabs to align shortcuts.
480                                 do 
481                                         label += '\t';
482                                 while (string_width(label) < max_width + 5);
483                                 label += accel.substr(1,accel.find(']') - 1);
484                         }
485                         label += "%x" + tostr(item.action() + action_offset)
486                                 + extra_label;
487                         
488                         // Modify the entry using the function status
489                         string pupmode;
490                         if (flag & (LyXFunc::Disabled | LyXFunc::Unknown))
491                                 pupmode += "%i";
492                         if (flag & LyXFunc::ToggleOn)
493                                 pupmode += "%B";
494                         if (flag & LyXFunc::ToggleOff)
495                                 pupmode += "%b";
496                         label += pupmode;
497
498                         // Finally the menu shortcut
499                         string shortcut = item.shortcut();
500
501                         if (!shortcut.empty()) {
502                                 shortcut += lowercase(shortcut[0]);
503                                 label += "%h";
504                                 fl_addtopup(menu, label.c_str(), 
505                                             shortcut.c_str());
506                         } else
507                                 fl_addtopup(menu, label.c_str());
508                         
509                         lyxerr[Debug::GUI] << "Command: \""  
510                                            << lyxaction.getActionName(item.action())
511                                            << "\", binding \"" << accel
512                                            << "\", shortcut \"" << shortcut 
513                                            << "\"" << endl;
514                         break;
515                 }
516
517                 case MenuItem::Submenu: {
518                         int submenu = create_submenu(win, view, 
519                                                      item.submenu(), smn);
520                         if (submenu == -1)
521                                 return -1;
522                         string label = item.label();
523                         label += extra_label + "%m";
524                         string shortcut = item.shortcut();
525                         if (!shortcut.empty()) {
526                                 shortcut += lowercase(shortcut[0]);
527                                 label += "%h";
528                                 fl_addtopup(menu, label.c_str(),
529                                             submenu, shortcut.c_str());
530                         }
531                         else {
532                                 fl_addtopup(menu, label.c_str(), submenu);
533                         }
534                         break;
535                 }
536
537                 case MenuItem::Separator:
538                         // already done, and if it was the first one,
539                         // we just ignore it.
540                         break;
541
542                 case MenuItem::Toc:
543                         add_toc(menu, extra_label, smn, win);
544                         break;
545
546                 case MenuItem::Documents: 
547                 case MenuItem::Lastfiles: 
548                 case MenuItem::ViewFormats:
549                 case MenuItem::UpdateFormats:
550                 case MenuItem::ExportFormats:
551                 case MenuItem::ImportFormats:
552                         lyxerr << "Menubar::Pimpl::create_submenu: "
553                                 "this should not happen" << endl;
554                         break;
555
556                 }
557         }
558         return menu;
559 }
560
561
562 extern "C"
563 void C_Menubar_Pimpl_MenuCallback(FL_OBJECT * ob, long button)
564 {
565         Menubar::Pimpl::MenuCallback(ob, button);
566 }
567
568
569 void Menubar::Pimpl::MenuCallback(FL_OBJECT * ob, long button)
570 {
571         ItemInfo * iteminfo = static_cast<ItemInfo *>(ob->u_vdata);
572 //      lyxerr << "MenuCallback: ItemInfo address=" << iteminfo
573 //             << "Val=(pimpl_=" << iteminfo->pimpl_
574 //             << ", item_=" << iteminfo->item_
575 //             << ", obj_=" << iteminfo->obj_ << ")" <<endl;
576
577         LyXView * view = iteminfo->pimpl_->owner_;
578         MenuItem const * item = iteminfo->item_.get();
579
580         if (button == 1) {
581                 // set the pseudo menu-button
582                 fl_set_object_boxtype(ob, FL_DOWN_BOX);
583                 fl_set_button(ob, 0);
584                 fl_redraw_object(ob);
585         }
586
587         // Paranoia check
588         Assert(item->kind() == MenuItem::Submenu);
589         
590         // set tabstop length
591         fl_set_tabstop(menu_tabstop);
592         vector<int> submenus;
593         int menu = iteminfo->pimpl_->
594                 create_submenu(FL_ObjWin(ob), view, 
595                                item->submenu(), submenus);
596         if (menu != -1) {
597                 // place popup
598                 fl_setpup_position(view->getForm()->x + ob->x,
599                                    view->getForm()->y + ob->y + ob->h + 10);   
600                 int choice = fl_dopup(menu);
601                 
602                 if (button == 1) {
603                                 // set the pseudo menu-button back
604                         fl_set_object_boxtype(ob, FL_FLAT_BOX);
605                         fl_redraw_object(ob);
606                 }
607
608                 // If the action value is too low, then it is not a
609                 // valid action, but something else.
610                 if (choice >= action_offset + 1) {
611                         view->getLyXFunc()->Dispatch(choice - action_offset);
612                 }
613                 else {
614                         lyxerr[Debug::GUI]
615                                 << "MenuCallback: ignoring bogus action "
616                                 << choice << endl;
617                 }
618         }
619         else 
620                 lyxerr << "Error in MenuCallback" << endl;
621         
622         std::for_each(submenus.begin(), submenus.end(), fl_freepup);
623         // restore tabstop length
624         fl_set_tabstop(default_tabstop);
625
626 }