]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/Menubar_pimpl.C
Patch for TOC menu by dekel, kde cleanup patch from John, plus a few things
[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
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 = fl_setpup_maxpup(2*smn.size());
200         int menu = fl_newpup(win);
201         smn.push_back(menu);
202         return menu;
203 }
204
205 size_type const max_number_of_items = 25;
206
207 void add_toc2(int menu, string const & extra_label,
208               vector<int> & smn, Window win,
209               vector<Buffer::TocItem> const & toc_list,
210               size_type from, size_type to, int depth)
211 {
212         if (to - from <= max_number_of_items) {
213                 for (size_type i = from; i < to; ++i) {
214                         int action = lyxaction.
215                                 getPseudoAction(LFUN_GOTO_PARAGRAPH,
216                                                 tostr(toc_list[i].par->id()));
217                         string label(4 * max(0, toc_list[i].depth - depth),' ');
218                         label += toc_list[i].str;
219                         label = limit_string_length(label);
220                         label += "%x" + tostr(action);
221                         if (i == to - 1 && depth == 0)
222                                 label += extra_label;
223                         fl_addtopup(menu, label.c_str());
224                 }
225         } else {
226                 size_type pos = from;
227                 size_type count = 0;
228                 while (pos < to) {
229                         ++count;
230                         if (count > max_number_of_items) {
231                                 int menu2 = get_new_submenu(smn, win);
232                                 add_toc2(menu2, extra_label, smn, win,
233                                          toc_list, pos, to, depth);
234                                 string label = _("More");
235                                 label += "...%m";
236                                 if (depth == 0)
237                                         label += extra_label;
238                                 fl_addtopup(menu, label.c_str(), menu2);
239                                 break;
240                         }
241                         size_type new_pos = pos+1;
242                         while (new_pos < to &&
243                                toc_list[new_pos].depth > depth)
244                                 ++new_pos;
245
246                         int action = lyxaction.
247                                 getPseudoAction(LFUN_GOTO_PARAGRAPH,
248                                                 tostr(toc_list[pos].par->id()));
249                         string label(4 * max(0, toc_list[pos].depth - depth), ' ');
250                         label += toc_list[pos].str;
251                         label = limit_string_length(label);
252                         if (new_pos == to && depth == 0)
253                                 label += extra_label;
254
255                         if (new_pos == pos + 1) {
256                                 label += "%x" + tostr(action);
257                                 fl_addtopup(menu, label.c_str());
258                         } else {
259                                 int menu2 = get_new_submenu(smn, win);
260                                 add_toc2(menu2, extra_label, smn, win,
261                                          toc_list, pos, new_pos, depth+1);
262                                 label += "%m";
263                                 fl_addtopup(menu, label.c_str(), menu2);
264                         }
265                         pos = new_pos;
266                 }
267         }
268 }
269
270 void Menubar::Pimpl::add_toc(int menu, string const & extra_label,
271                              vector<int> & smn, Window win)
272 {
273         //xgettext:no-c-format
274         static char const * MenuNames[3] = { N_("List of Figures%m"),
275         //xgettext:no-c-format
276                                              N_("List of Tables%m"),
277         //xgettext:no-c-format
278                                              N_("List of Algorithms%m") };
279
280         vector<vector<Buffer::TocItem> > toc_list =
281                 owner_->buffer()->getTocList();
282
283         // Handle LOF/LOT/LOA
284         int max_nonempty = 0;
285         for (int i = 1; i <= 3; ++i)
286                 if (!toc_list[i].empty())
287                         max_nonempty = i;
288
289         for (int j = 1; j <= 3; ++j)
290                 if (!toc_list[j].empty()) {
291                         int menu2 = get_new_submenu(smn, win);
292                         for (size_type i = 0; i < toc_list[j].size(); ++i) {
293                                 if (i > max_number_of_items) {
294                                         fl_addtopup(menu2, ". . .%d");
295                                         break;
296                                 }
297                                 int action = lyxaction.
298                                         getPseudoAction(LFUN_GOTO_PARAGRAPH,
299                                                         tostr(toc_list[j][i].par->id()));
300                                 string label =
301                                         limit_string_length(toc_list[j][i].str);
302                                 label += "%x" + tostr(action);
303                                 fl_addtopup(menu2, label.c_str());
304                         }
305                         if (j == max_nonempty) {
306                                 string label = _(MenuNames[j-1]);
307                                 label += "%l";
308                                 fl_addtopup(menu, label.c_str(), menu2);
309                         } else
310                                 fl_addtopup(menu, _(MenuNames[j-1]), menu2);
311                 }
312
313         // Handle normal TOC
314         if (max_nonempty == 0 && toc_list[0].empty()) {
315                 fl_addtopup(menu,_("No Table of Contents%i"));
316                 return;
317         }
318
319         add_toc2(menu, extra_label, smn, win,
320                  toc_list[0], 0, toc_list[0].size(), 0);
321
322 }
323
324 void add_references2(int menu, vector<int> & smn, Window win,
325                      vector<string> const & label_list, string const & type)
326 {
327         size_type const max_number_of_items = 25;
328         size_type const max_number_of_items2 = 20;
329         string::size_type const max_item_length = 40;
330         string::size_type const max_item_length2 = 20;
331
332         if (label_list.size() <= max_number_of_items)
333                 for (size_type i = 0; i < label_list.size(); ++i) {
334                         int action = (type == "goto")
335                                 ? lyxaction.getPseudoAction(LFUN_REF_GOTO, 
336                                                             label_list[i])
337                                 : lyxaction.getPseudoAction(LFUN_REF_INSERT,
338                                                             type + "|++||++|"
339                                                             + label_list[i]);
340                         string label = label_list[i];
341                         if (label.size() > max_item_length)
342                                 label = label.substr(0, max_item_length-1) + "$";
343                         label += "%x" + tostr(action);
344                         fl_addtopup(menu, label.c_str());
345                 }
346         else {
347                 size_type count = 0;
348                 for (size_type i = 0; i < label_list.size();
349                      i += max_number_of_items2) {
350                         ++count;
351                         if (count > max_number_of_items) {
352                                 fl_addtopup(menu, ". . .%d");
353                                 break;
354                         }
355                         size_type j = min(label_list.size(),
356                                           i+max_number_of_items2);
357
358                         string label;
359                         label += (label_list[i].size() > max_item_length2)
360                                 ? label_list[i].substr(0, max_item_length2-1) + "$"
361                                 : label_list[i];
362                         label += "..";
363                         label += (label_list[j-1].size() > max_item_length2)
364                                 ? label_list[j-1].substr(0, max_item_length2-1) + "$"
365                                 : label += label_list[j-1];
366
367                         int menu2 = get_new_submenu(smn, win);
368                         for (size_type k = i;  k < j; ++k) {
369                                 int action = (type == "goto")
370                                         ? lyxaction.getPseudoAction(LFUN_REF_GOTO, 
371                                                                     label_list[k])
372                                         : lyxaction.getPseudoAction(LFUN_REF_INSERT,
373                                                                     type + "|++||++|"
374                                                                     + label_list[k]);
375                                 string label2 = label_list[k];
376                                 if (label2.size() > max_item_length)
377                                         label2 = label2.substr(0, max_item_length-1) + "$";
378                                 label2 += "%x" + tostr(action);
379                                 fl_addtopup(menu2, label2.c_str());
380                         }
381                         label += "%m";
382                         fl_addtopup(menu, label.c_str(), menu2);
383                 }
384         }
385 }
386
387
388 void Menubar::Pimpl::add_references(int menu, string const & extra_label,
389                                     vector<int> & smn, Window win)
390 {
391         //xgettext:no-c-format
392         static char const * MenuNames[6] = { N_("Insert Reference%m"),
393         //xgettext:no-c-format
394                                              N_("Insert Page Number%m"),
395         //xgettext:no-c-format
396                                              N_("Insert vref%m"),
397         //xgettext:no-c-format
398                                              N_("Insert vpageref%m"),
399         //xgettext:no-c-format
400                                              N_("Insert Pretty Ref%m"),
401         //xgettext:no-c-format
402                                              N_("Goto Reference%m") };
403
404         int const EMPTY = 1;
405         int const SGML = 2;
406         int const READONLY = 4;
407
408         static int MenuFlags[6] = {
409                 EMPTY | READONLY,
410                 EMPTY | READONLY,
411                 EMPTY | READONLY | SGML,
412                 EMPTY | READONLY | SGML,
413                 EMPTY | READONLY | SGML,
414                 EMPTY };
415
416         static string const MenuTypes[6] = {
417                 "ref", "pageref", "vref", "vpageref", "prettyref", "goto" };
418
419         vector<string> label_list = owner_->buffer()->getLabelList();
420
421         int flag = 0;
422         if (label_list.empty())
423                 flag += EMPTY;
424         if (owner_->buffer()->isSGML())
425                 flag += SGML;
426         if (owner_->buffer()->isReadonly())
427                 flag += READONLY;
428
429         int max_nonempty = -1;
430         for (int i = 0; i < 6; ++i)
431                 if ((MenuFlags[i] & flag) == 0)
432                         max_nonempty = i;
433
434         for (int i = 0; i < 6; ++i) {
435                 if ((MenuFlags[i] & flag) == 0) {
436                         string label = _(MenuNames[i]);
437                         if (i == max_nonempty)
438                                 label += extra_label;
439                         int menu2 = get_new_submenu(smn, win);
440                         add_references2(menu2, smn, win, label_list,
441                                         MenuTypes[i]);
442                         fl_addtopup(menu, label.c_str(), menu2);
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 = get_new_submenu(smn, 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
466         // Compute the size of the largest label (because xforms is
467         // not able to support shortcuts correctly...)
468         int max_width = 0;
469         string widest_label;
470         Menu::const_iterator end = md.end();
471         for (Menu::const_iterator i = md.begin(); i != end; ++i) {
472                 MenuItem const & item = (*i);
473                 if (item.kind() == MenuItem::Command) {
474                         string label = item.label() + '\t';
475                         int width = string_width(label);
476                         if (width > max_width) {
477                                 max_width = width;
478                                 widest_label = label;
479                         }
480                 }
481         }
482         lyxerr[Debug::GUI] << "max_width=" << max_width 
483                            << ", widest_label=`" << widest_label 
484                            << "'" << endl;
485
486         // Compute where to put separators
487         vector<string> extra_labels(md.size());
488         vector<string>::iterator it = extra_labels.begin();
489         vector<string>::iterator last = it;
490         for (Menu::const_iterator i = md.begin(); i != end; ++i, ++it)
491                 if (i->kind() == MenuItem::Separator)
492                         *last = "%l";
493                 else if (!i->optional() ||
494                          !(view->getLyXFunc()->getStatus(i->action()) & LyXFunc::Disabled))
495                         last = it;
496
497         it = extra_labels.begin();
498         for (Menu::const_iterator i = md.begin(); i != end; ++i, ++it) {
499                 MenuItem const & item = (*i);
500                 string & extra_label = *it;
501
502                 switch(item.kind()) {
503                 case MenuItem::Command: {
504                         LyXFunc::func_status flag = 
505                                 view->getLyXFunc()->getStatus(item.action()); 
506
507                         // handle optional entries.
508                         if (item.optional() && (flag & LyXFunc::Disabled)) {
509                                 lyxerr[Debug::GUI] 
510                                         << "Skipping optional item " 
511                                         << item.label() << endl; 
512                                 break;
513                         }
514
515                         // Get the keys bound to this action, but keep only the
516                         // first one later
517                         string accel = toplevel_keymap->findbinding(item.action());
518                         // Build the menu label from all the info
519                         string label = item.label();
520
521                         if (!accel.empty()) {
522                                 // Try to be clever and add  just enough
523                                 // tabs to align shortcuts.
524                                 do 
525                                         label += '\t';
526                                 while (string_width(label) < max_width);
527                                 label += accel.substr(1,accel.find(']') - 1);
528                         }
529                         label += "%x" + tostr(item.action()) + extra_label;
530                         
531                         // Modify the entry using the function status
532                         string pupmode;
533                         if (flag & (LyXFunc::Disabled | LyXFunc::Unknown))
534                                 pupmode += "%i";
535                         if (flag & LyXFunc::ToggleOn)
536                                 pupmode += "%B";
537                         if (flag & LyXFunc::ToggleOff)
538                                 pupmode += "%b";
539                         label += pupmode;
540
541                         // Finally the menu shortcut
542                         string shortcut = item.shortcut();
543
544                         if (!shortcut.empty()) {
545                                 shortcut += lowercase(shortcut[0]);
546                                 label += "%h";
547                                 fl_addtopup(menu, label.c_str(), 
548                                             shortcut.c_str());
549                         } else
550                                 fl_addtopup(menu, label.c_str());
551                         
552                         lyxerr[Debug::GUI] << "Command: \""  
553                                            << lyxaction.getActionName(item.action())
554                                            << "\", Binding " << accel 
555                                            << ", shortcut " << shortcut 
556                                            << endl;
557
558
559                         break;
560                 }
561
562                 case MenuItem::Submenu: {
563                         int submenu = create_submenu(win, view, 
564                                                      item.submenu(), smn);
565                         if (submenu == -1)
566                                 return -1;
567                         string label = item.label();
568                         label += extra_label + "%m";
569                         string shortcut = item.shortcut();
570                         if (!shortcut.empty()) {
571                                 shortcut += lowercase(shortcut[0]);
572                                 fl_addtopup(menu, (label + "%h").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 }