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