]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/Menubar_pimpl.C
8f465b990f673e85b84ca173e540f88b42fe4eb0
[lyx.git] / src / frontends / xforms / Menubar_pimpl.C
1 /* This file is part of
2 * ======================================================
3
4 *           LyX, The Document Processor
5 *        
6 *           Copyright (C) 1999 The LyX Team.
7 *
8 *======================================================*/
9
10 #ifdef __GNUG__
11 #pragma implementation
12 #endif
13
14 #include <config.h>
15
16 #include <algorithm>
17 //#include <cctype>
18 #include "support/lstrings.h"
19 #include "support/LAssert.h"
20 #include "debug.h"
21 #include "LyXAction.h"
22 #include "lyxfunc.h"
23 #include "kbmap.h"
24 #include "buffer.h"
25 #include "LyXView.h"
26 #include "MenuBackend.h"
27 #include "Menubar_pimpl.h"
28
29 using std::endl;
30 using std::vector;
31 using std::max;
32 using std::min;
33
34 typedef vector<int>::size_type size_type;
35
36 extern kb_keymap * toplevel_keymap;
37 extern LyXAction lyxaction;
38
39 // Some constants
40 const int MENU_LABEL_SIZE = FL_NORMAL_SIZE;
41 const int mheight = 30;
42 const int mbheight= 22;
43 // where to place the menubar?
44 const int yloc = (mheight - mbheight)/2; //air + bw;
45 const int mbadd = 20; // menu button add (to width)
46 // Some space between buttons on the menubar 
47 const int air = 2;
48 char const * menu_tabstop = "aa";
49 char const * default_tabstop = "aaaaaaaa";
50
51 //Defined later, used in makeMenubar().
52 extern "C"
53 void C_Menubar_Pimpl_MenuCallback(FL_OBJECT * ob, long button);
54
55 // This is used a few times below.
56 inline
57 int string_width(string const & str) 
58 {
59         return fl_get_string_widthTAB(FL_NORMAL_STYLE, MENU_LABEL_SIZE,
60                                       str.c_str(), 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 size_type const max_number_of_menus = 32;
195 size_type const max_number_of_items = 25;
196
197 void add_toc2(int menu, string const & extra_label,
198               vector<int> & smn, Window win,
199               vector<Buffer::TocItem> const & toc_list,
200               size_type from, size_type to, int depth)
201 {
202         if (to - from <= max_number_of_items) {
203                 for (size_type i = from; i < to; ++i) {
204                         int action = lyxaction.
205                                 getPseudoAction(LFUN_GOTO_PARAGRAPH,
206                                                 tostr(toc_list[i].par->id()));
207                         string label(4 * max(0, toc_list[i].depth - depth),' ');
208                         label += toc_list[i].str;
209                         label = limit_string_length(label);
210                         label += "%x" + tostr(action);
211                         if (i == to - 1 && depth == 0)
212                                 label += extra_label;
213                         fl_addtopup(menu, label.c_str());
214                 }
215         } else {
216                 size_type pos = from;
217                 size_type count = 0;
218                 while (pos < to) {
219                         ++count;
220                         if (count > max_number_of_items) {
221                                 fl_addtopup(menu, ". . .%d");
222                                 break;
223                         }
224                         size_type new_pos = pos+1;
225                         while (new_pos < to &&
226                                toc_list[new_pos].depth > depth)
227                                 ++new_pos;
228
229                         int action = lyxaction.
230                                 getPseudoAction(LFUN_GOTO_PARAGRAPH,
231                                                 tostr(toc_list[pos].par->id()));
232                         string label(4 * max(0, toc_list[pos].depth - depth), ' ');
233                         label += toc_list[pos].str;
234                         label = limit_string_length(label);
235                         if (new_pos == to && depth == 0)
236                                 label += extra_label;
237
238                         if (new_pos == pos + 1) {
239                                 label += "%x" + tostr(action);
240                                 fl_addtopup(menu, label.c_str());
241                         } else if (smn.size() < max_number_of_menus) {
242                                 int menu2 = fl_newpup(win);
243                                 smn.push_back(menu2);
244                                 add_toc2(menu2, extra_label, smn, win,
245                                          toc_list, pos, new_pos, depth+1);
246                                 label += "%m";
247                                 fl_addtopup(menu, label.c_str(), menu2);
248                         } else {
249                                 label += "%d";
250                                 fl_addtopup(menu, label.c_str());
251                         }
252                         pos = new_pos;
253                 }
254         }
255 }
256
257 void Menubar::Pimpl::add_toc(int menu, string const & extra_label,
258                              vector<int> & smn, Window win)
259 {
260         //xgettext:no-c-format
261         static char const * MenuNames[3] = { N_("List of Figures%m"),
262         //xgettext:no-c-format
263                                              N_("List of Tables%m"),
264         //xgettext:no-c-format
265                                              N_("List of Algorithms%m") };
266
267         vector<vector<Buffer::TocItem> > toc_list =
268                 owner_->buffer()->getTocList();
269
270         // Handle LOF/LOT/LOA
271         int max_nonempty = 0;
272         for (int i = 1; i <= 3; ++i)
273                 if (!toc_list[i].empty())
274                         max_nonempty = i;
275
276         for (int j = 1; j <= 3; ++j)
277                 if (!toc_list[j].empty()) {
278                         int menu2 = fl_newpup(win);
279                         smn.push_back(menu2);
280                         for (size_type i = 0; i < toc_list[j].size(); ++i) {
281                                 if (i > max_number_of_items) {
282                                         fl_addtopup(menu2, ". . .%d");
283                                         break;
284                                 }
285                                 int action = lyxaction.
286                                         getPseudoAction(LFUN_GOTO_PARAGRAPH,
287                                                         tostr(toc_list[j][i].par->id()));
288                                 string label =
289                                         limit_string_length(toc_list[j][i].str);
290                                 label += "%x" + tostr(action);
291                                 fl_addtopup(menu2, label.c_str());
292                         }
293                         if (j == max_nonempty) {
294                                 string label = _(MenuNames[j-1]);
295                                 label += "%l";
296                                 fl_addtopup(menu, label.c_str(), menu2);
297                         } else
298                                 fl_addtopup(menu, _(MenuNames[j-1]), menu2);
299                 }
300
301         // Handle normal TOC
302         if (max_nonempty == 0 && toc_list[0].empty()) {
303                 fl_addtopup(menu,_("No Table of Contents%i"));
304                 return;
305         }
306
307         add_toc2(menu, extra_label, smn, win,
308                  toc_list[0], 0, toc_list[0].size(), 0);
309
310 }
311
312 void add_references2(int menu, vector<int> & smn, Window win,
313                      vector<string> const & label_list, string const & type)
314 {
315         size_type const max_number_of_items = 25;
316         size_type const max_number_of_items2 = 20;
317         string::size_type const max_item_length = 40;
318         string::size_type const max_item_length2 = 20;
319
320         if (label_list.size() <= max_number_of_items)
321                 for (size_type i = 0; i < label_list.size(); ++i) {
322                         int action = (type == "goto")
323                                 ? lyxaction.getPseudoAction(LFUN_REF_GOTO, 
324                                                             label_list[i])
325                                 : lyxaction.getPseudoAction(LFUN_REF_INSERT,
326                                                             type + "|++||++|"
327                                                             + label_list[i]);
328                         string label = label_list[i];
329                         if (label.size() > max_item_length)
330                                 label = label.substr(0, max_item_length-1) + "$";
331                         label += "%x" + tostr(action);
332                         fl_addtopup(menu, label.c_str());
333                 }
334         else {
335                 size_type count = 0;
336                 for (size_type i = 0; i < label_list.size();
337                      i += max_number_of_items2) {
338                         ++count;
339                         if (count > max_number_of_items) {
340                                 fl_addtopup(menu, ". . .%d");
341                                 break;
342                         }
343                         size_type j = min(label_list.size(),
344                                           i+max_number_of_items2);
345
346                         string label;
347                         label += (label_list[i].size() > max_item_length2)
348                                 ? label_list[i].substr(0, max_item_length2-1) + "$"
349                                 : label_list[i];
350                         label += "..";
351                         label += (label_list[j-1].size() > max_item_length2)
352                                 ? label_list[j-1].substr(0, max_item_length2-1) + "$"
353                                 : label += label_list[j-1];
354
355                         if (smn.size() < max_number_of_menus) {
356                                 int menu2 = fl_newpup(win);
357                                 smn.push_back(menu2);
358                                 for (size_type k = i;  k < j; ++k) {
359                                         int action = (type == "goto")
360                                                 ? lyxaction.getPseudoAction(LFUN_REF_GOTO, 
361                                                                             label_list[k])
362                                                 : lyxaction.getPseudoAction(LFUN_REF_INSERT,
363                                                                             type + "|++||++|"
364                                                                             + label_list[k]);
365                                         string label2 = label_list[k];
366                                         if (label2.size() > max_item_length)
367                                                 label2 = label2.substr(0, max_item_length-1) + "$";
368                                         label2 += "%x" + tostr(action);
369                                         fl_addtopup(menu2, label2.c_str());
370                                 }
371                                 label += "%m";
372                                 fl_addtopup(menu, label.c_str(), menu2);
373                         } else {
374                                 label += "%d";
375                                 fl_addtopup(menu, label.c_str());
376                         }
377                 }
378         }
379 }
380
381
382 void Menubar::Pimpl::add_references(int menu, string const & extra_label,
383                                     vector<int> & smn, Window win)
384 {
385         //xgettext:no-c-format
386         static char const * MenuNames[6] = { N_("Insert Reference%m"),
387         //xgettext:no-c-format
388                                              N_("Insert Page Number%m"),
389         //xgettext:no-c-format
390                                              N_("Insert vref%m"),
391         //xgettext:no-c-format
392                                              N_("Insert vpageref%m"),
393         //xgettext:no-c-format
394                                              N_("Insert Pretty Ref%m"),
395         //xgettext:no-c-format
396                                              N_("Goto Reference%m") };
397
398         int const EMPTY = 1;
399         int const SGML = 2;
400         int const READONLY = 4;
401
402         static int MenuFlags[6] = {
403                 EMPTY | READONLY,
404                 EMPTY | READONLY,
405                 EMPTY | READONLY | SGML,
406                 EMPTY | READONLY | SGML,
407                 EMPTY | READONLY | SGML,
408                 EMPTY };
409
410         static string const MenuTypes[6] = {
411                 "ref", "pageref", "vref", "vpageref", "prettyref", "goto" };
412
413         vector<string> label_list = owner_->buffer()->getLabelList();
414
415         int flag = 0;
416         if (label_list.empty())
417                 flag += EMPTY;
418         if (owner_->buffer()->isSGML())
419                 flag += SGML;
420         if (owner_->buffer()->isReadonly())
421                 flag += READONLY;
422
423         int max_nonempty = -1;
424         for (int i = 0; i < 6; ++i)
425                 if ((MenuFlags[i] & flag) == 0)
426                         max_nonempty = i;
427
428         for (int i = 0; i < 6; ++i) {
429                 if ((MenuFlags[i] & flag) == 0) {
430                         string label = _(MenuNames[i]);
431                         if (i == max_nonempty)
432                                 label += extra_label;
433                         if (smn.size() < max_number_of_menus) {
434                                 int menu2 = fl_newpup(win);
435                                 smn.push_back(menu2);
436                                 add_references2(menu2, smn, win, label_list,
437                                                 MenuTypes[i]);
438                                 fl_addtopup(menu, label.c_str(), menu2);
439                         } else {
440                                 label += "%d";
441                                 fl_addtopup(menu, label.c_str());       
442                         }
443                 }
444         }
445 }
446
447
448 int Menubar::Pimpl::create_submenu(Window win, LyXView * view, 
449                                    string const & menu_name, 
450                                    vector<int> & smn) 
451 {
452         if (!menubackend_->hasMenu(menu_name)){ 
453                 lyxerr << "ERROR:create_submenu: Unknown menu `" 
454                        << menu_name << "'" << endl;
455                 return -1;
456         }
457         Menu md = Menu();
458         menubackend_->getMenu(menu_name).expand(md, owner_->buffer());
459
460         int menu = fl_newpup(win);
461         fl_setpup_softedge(menu, true);
462         fl_setpup_bw(menu, -1);
463         lyxerr[Debug::GUI] << "Adding menu " << menu 
464                            << " in deletion list" << endl;
465         smn.push_back(menu);
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                                 fl_addtopup(menu, (label + "%h").c_str(),
574                                             submenu, shortcut.c_str());
575                         }
576                         else {
577                                 fl_addtopup(menu, label.c_str(), submenu);
578                         }
579                         break;
580                 }
581
582                 case MenuItem::Separator:
583                         // already done, and if it was the first one,
584                         // we just ignore it.
585                         break;
586
587                 case MenuItem::Toc:
588                         add_toc(menu, extra_label, smn, win);
589                         break;
590
591                 case MenuItem::References:
592                         add_references(menu, extra_label, smn, win);
593                         break;
594
595                 case MenuItem::Documents: 
596                 case MenuItem::Lastfiles: 
597                 case MenuItem::ViewFormats:
598                 case MenuItem::UpdateFormats:
599                 case MenuItem::ExportFormats:
600                         lyxerr << "Menubar::Pimpl::create_submenu: "
601                                 "this should not happen" << endl;
602                         break;
603
604                 }
605         }
606         return menu;
607 }
608
609 extern "C"
610 void C_Menubar_Pimpl_MenuCallback(FL_OBJECT * ob, long button)
611 {
612         Menubar::Pimpl::MenuCallback(ob, button);
613 }
614
615
616 void Menubar::Pimpl::MenuCallback(FL_OBJECT * ob, long button)
617 {
618         ItemInfo * iteminfo = static_cast<ItemInfo *>(ob->u_vdata);
619 //      lyxerr << "MenuCallback: ItemInfo address=" << iteminfo
620 //             << "Val=(pimpl_=" << iteminfo->pimpl_
621 //             << ", item_=" << iteminfo->item_
622 //             << ", obj_=" << iteminfo->obj_ << ")" <<endl;
623
624         LyXView * view = iteminfo->pimpl_->owner_;
625         MenuItem const * item = iteminfo->item_;
626
627         if (button == 1) {
628                 // set the pseudo menu-button
629                 fl_set_object_boxtype(ob, FL_DOWN_BOX);
630                 fl_set_button(ob, 0);
631                 fl_redraw_object(ob);
632         }
633
634         // Paranoia check
635         Assert(item->kind() == MenuItem::Submenu);
636         
637         // set tabstop length
638         fl_set_tabstop(menu_tabstop);
639         vector<int> submenus;
640         int menu = iteminfo->pimpl_->
641                 create_submenu(FL_ObjWin(ob), view, 
642                                item->submenu(), submenus);
643         if (menu != -1) {
644                 // place popup
645                 fl_setpup_position(view->getForm()->x + ob->x,
646                                    view->getForm()->y + ob->y + ob->h + 10);   
647                 int choice = fl_dopup(menu);
648                 
649                 if (button == 1) {
650                                 // set the pseudo menu-button back
651                         fl_set_object_boxtype(ob, FL_FLAT_BOX);
652                         fl_redraw_object(ob);
653                 }
654                 
655                 if (choice >= 1) {
656                         view->getLyXFunc()->Dispatch(choice);
657                 }
658         }
659         else 
660                 lyxerr << "Error in MenuCallback" << endl;
661         
662         std::for_each(submenus.begin(), submenus.end(), fl_freepup);
663         // restore tabstop length
664         fl_set_tabstop(default_tabstop);
665
666 }