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