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