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