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