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