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