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