]> git.lyx.org Git - lyx.git/blob - src/frontends/gnome/Menubar_pimpl.C
Mathed fix from Dekel, GNOME patch from Marko, language-code from Garst
[lyx.git] / src / frontends / gnome / Menubar_pimpl.C
1 // -*- C++ -*-
2 /* This file is part of
3  * ====================================================== 
4  * 
5  *           LyX, The Document Processor
6  *       
7  *          Copyright 2000 The LyX Team.
8  *
9  * ====================================================== */
10
11 #ifdef __GNUG__
12 #pragma implementation
13 #endif
14
15 #include <config.h>
16
17 #include <algorithm>
18 #include <cctype>
19 #include "support/lstrings.h"
20 #include "support/filetools.h"
21 #include "support/StrPool.h"
22 #include "support/LAssert.h"
23 #include "debug.h"
24 #include "LyXAction.h"
25 #include "lyxfunc.h"
26 #include "kbmap.h"
27 #include "bufferlist.h"
28 #include "lastfiles.h"
29 #include "LyXView.h"
30 #include "MenuBackend.h"
31 #include "Menubar_pimpl.h"
32 #include "lyxtext.h"
33 #include "exporter.h"
34
35 #include "mainapp.h"
36
37 #include <gtk--/menu.h>
38
39 using SigC::slot;
40 using SigC::bind;
41
42 using std::endl;
43
44 // temporary solution for LyXView
45 extern GLyxAppWin * mainAppWin;
46
47 // Some constants
48 extern kb_keymap * toplevel_keymap;
49 extern LyXAction lyxaction;
50 extern BufferList bufferlist;
51 extern LastFiles * lastfiles; 
52
53
54
55 Menubar::Pimpl::Pimpl(LyXView * view, MenuBackend const & mb) 
56   : owner_(view), menubackend_(&mb), ignore_action_(false)
57 {
58   
59 }
60
61 Menubar::Pimpl::~Pimpl() 
62 {
63   if (utoc_.connected()) utoc_.disconnect();
64 }
65
66 void Menubar::Pimpl::set(string const & menu_name) 
67 {
68   // if (current_menu_name_ != menu_name)  // disabled until Lastfiles and Documents are added dynamically to menu
69     {
70       current_menu_name_ = menu_name;
71
72       // clean up the lists
73       toc_.clear();
74       if (utoc_.connected()) utoc_.disconnect();
75       
76       // compose new menu
77       vector<Gnome::UI::Info> menus;
78       composeUIInfo(current_menu_name_, menus, "");
79
80       // set menu
81       Menu_ = menus;
82       mainAppWin->set_menu(Menu_);
83
84       // connect all menu items to correspoding action
85       wid_act_.clear();
86       ignore_action_ = true;
87       connectWidgetToAction(Menu_.gtkobj());
88       ignore_action_ = false;
89
90       // update state of the items
91       update();
92       updateAllLists();
93     }
94 }
95
96 void Menubar::Pimpl::updateAllLists()
97 {
98   // update lists
99   if (toc_.size() > 0)
100     {
101       vector<Buffer::TocItem> toclist = (owner_->view()->buffer()->getTocList())[Buffer::TOC_TOC];
102       updateList(&toclist, &toc_);
103     }
104 }
105
106 int const max_number_of_items = 25;
107 void Menubar::Pimpl::updateList(vector<Buffer::TocItem> * toclist, vector<ListsHolder> * pgui) 
108 {
109   vector<ListsHolder> & gui = *pgui;
110   int szGui = gui.size();
111   int i;
112   for (i=0; i < szGui; ++i)
113     {
114       int oldsz = gui[i].lst.size();
115       vector<Gnome::UI::Info> menu;
116       string label;
117
118       menu.push_back(Gnome::UI::Item(Gnome::UI::Icon(GNOME_STOCK_MENU_REFRESH),
119                                      N_("Refresh"), slot(this, &Menubar::Pimpl::updateAllLists)));
120
121       if (toclist->size() > max_number_of_items)
122         composeTocUIInfo(menu, *toclist, toclist->begin(), 0);
123       else
124         {
125           vector<Buffer::TocItem>::const_iterator end = toclist->end();
126           for (vector<Buffer::TocItem>::const_iterator it = toclist->begin();
127                it != end; ++it)
128             
129             {
130               label = string(4*(*it).depth,' ')+(*it).str;
131               
132               menu.push_back(Gnome::UI::Item(label,
133                                              bind<Buffer::TocItem>(slot(this, &Menubar::Pimpl::callbackToc), (*it)),
134                                              label));
135             }
136         }
137       
138       gui[i].lst = menu;
139       mainAppWin->update_menu(gui[i].path, oldsz, gui[i].lst);
140     }
141 }
142
143 vector<Buffer::TocItem>::const_iterator
144 Menubar::Pimpl::composeTocUIInfo(vector<Gnome::UI::Info> & menu,
145                                  vector<Buffer::TocItem> const & toclist,
146                                  vector<Buffer::TocItem>::const_iterator begin,
147                                  int mylevel)
148 {
149   string label = N_("<No Name>");
150
151   vector<Buffer::TocItem>::const_iterator end = toclist.end();
152   vector<Buffer::TocItem>::const_iterator it;
153   for (it = begin; it != end && (*it).depth >= mylevel; ++it)
154     {
155       if ( (*it).depth == mylevel &&
156            (it+1 == end || (*(it+1)).depth <= mylevel) )
157         {
158           label = (*it).str;
159           menu.push_back(Gnome::UI::Item(label,
160                                        bind<Buffer::TocItem>(slot(this, &Menubar::Pimpl::callbackToc), (*it)),
161                                          label));
162         }
163       else
164         {
165           vector<Gnome::UI::Info> submenu;
166           if ( (*it).depth == mylevel )
167             {
168               label = (*it).str;
169               submenu.push_back(Gnome::UI::Item(label,
170                                                 bind<Buffer::TocItem>(slot(this, &Menubar::Pimpl::callbackToc), (*it)),
171                                                 label));
172               ++it;    
173             }
174           it = composeTocUIInfo(submenu, toclist, it, mylevel+1);
175           menu.push_back(Gnome::UI::Menu(label,submenu,label));
176         }
177     }
178   --it;
179   return it;
180 }
181
182 void Menubar::Pimpl::callback(int action)
183 {
184   // Dispatch action OR record action to local variable (see connectWidgetToAction)
185   if (!ignore_action_) {
186       Pimpl::update();
187       owner_->getLyXFunc()->Dispatch(action);
188   } else
189       action_ = action;
190 }
191
192 void Menubar::Pimpl::callbackToc(Buffer::TocItem tg)
193 {
194   if (!owner_->view()->available()) return;
195   
196   owner_->view()->beforeChange();
197   owner_->view()->text->SetCursor( owner_->view(), tg.par, 0 );
198   owner_->view()->text->sel_cursor = owner_->view()->text->cursor;
199   owner_->view()->update(BufferView::SELECT|BufferView::FITCUR);
200 }
201
202 void Menubar::Pimpl::composeUIInfo(string const & menu_name, vector<Gnome::UI::Info> & Menus, string rootpath)
203 {
204   string path = rootpath;
205     
206   if (!menubackend_->hasMenu(menu_name))
207     {
208       cout << "ERROR:composeUIInfo: Unknown menu `" << menu_name
209            << "'" << endl;
210       return;
211     }
212
213   Menu menu = menubackend_->getMenu(menu_name);
214
215   for (Menu::const_iterator i = menu.begin(); i != menu.end(); ++i)
216     {
217       MenuItem item = (*i);
218       switch(item.kind()) {
219
220       case MenuItem::Command: {
221         string label = item.label();
222
223         path = rootpath + label;
224         
225         if (label.find(item.shortcut()) != string::npos)
226           label.insert(label.find(item.shortcut()), "_");
227
228         LyXFunc::func_status flag = owner_->getLyXFunc()->getStatus(item.action());
229
230         Gnome::UI::Info gitem;
231         SigC::Slot0<void> cback = bind<int>(slot(this, &Menubar::Pimpl::callback),item.action());
232
233         {
234           using namespace Gnome::MenuItems;
235           int ac = item.action();
236           kb_action action;
237           string argument;
238           if (lyxaction.isPseudoAction(ac))
239             action = lyxaction.retrieveActionArg(ac, argument);
240           else
241             action = static_cast<kb_action>(ac);
242
243           switch(action) {
244           case LFUN_MENUOPEN:
245             gitem = Open(cback);
246             break;
247           case LFUN_QUIT:
248             gitem = Exit(cback);
249             break;
250           case LFUN_CLOSEBUFFER:
251             gitem = Close(cback);
252             break;
253           case LFUN_MENUWRITE:
254             gitem = Save(cback);
255             break;
256           case LFUN_MENUWRITEAS:
257             gitem = SaveAs(cback);
258             break;
259           case LFUN_BUFFER_PRINT:
260             gitem = Print(cback);
261             break;
262           case LFUN_CUT:
263             gitem = Cut(cback);
264             break;
265           case LFUN_COPY:
266             gitem = Copy(cback);
267             break;
268           case LFUN_PASTE:
269             gitem = Paste(cback);
270             break;
271           case LFUN_UNDO:
272             gitem = Gnome::MenuItems::Undo(cback); // confused with class Undo
273             break;
274           case LFUN_REDO:
275             gitem = Redo(cback);
276             break;
277           case LFUN_DIALOG_PREFERENCES:
278             gitem = Preferences(cback);
279             break;
280           case LFUN_MENUNEW:
281             gitem = Gnome::UI::Item(Gnome::UI::Icon(GNOME_STOCK_MENU_NEW),
282                                     label, cback, lyxaction.helpText(item.action()));
283             break;
284           case LFUN_MENUNEWTMPLT:
285             gitem = Gnome::UI::Item(Gnome::UI::Icon(GNOME_STOCK_MENU_NEW), 
286                                     label, cback, lyxaction.helpText(item.action()));
287             break;
288           case LFUN_MENUSEARCH:
289             gitem = Gnome::UI::Item(Gnome::UI::Icon(GNOME_STOCK_MENU_SRCHRPL), 
290                                     label, cback, lyxaction.helpText(item.action()));
291             break;
292           case LFUN_SPELLCHECK:
293             gitem = Gnome::UI::Item(Gnome::UI::Icon(GNOME_STOCK_MENU_SPELLCHECK), 
294                                     label, cback, lyxaction.helpText(item.action()));
295             break;
296           default:
297             gitem = Gnome::UI::Item(label, cback, lyxaction.helpText(item.action()));
298             break;
299           }
300         }
301
302         // first handle optional entries.
303         if (item.optional() && (flag & LyXFunc::Disabled)) {
304             lyxerr[Debug::GUI] 
305                 << "Skipping optional item " << item.label() << endl; 
306             break;
307         }
308         if ((flag & LyXFunc::ToggleOn) || (flag & LyXFunc::ToggleOff))
309           gitem = Gnome::UI::ToggleItem(label, cback, lyxaction.helpText(item.action()));
310
311         Menus.push_back(gitem);
312         break;
313       }
314       
315       case MenuItem::Submenu: {
316         vector<Gnome::UI::Info> submenu;
317         string label = item.label();
318
319         path = rootpath + label;
320         
321         if (label.find(item.shortcut()) != string::npos)
322           label.insert(label.find(item.shortcut()), "_");
323         composeUIInfo(item.submenu(), submenu, path + "/");
324         Menus.push_back(Gnome::UI::Menu(label,submenu,label));
325         break;
326       }
327
328       case MenuItem::Separator: {
329
330         path = rootpath + "<Separator>";
331         
332         Menus.push_back(Gnome::UI::Separator());
333         break;
334       }
335
336       case MenuItem::Lastfiles: {
337         int ii = 1;
338         for (LastFiles::const_iterator cit = lastfiles->begin();
339              cit != lastfiles->end() && ii < 10; ++cit, ++ii)
340           {
341             int action = lyxaction.getPseudoAction(LFUN_FILE_OPEN, (*cit));
342             string label = tostr(ii) + ". " + MakeDisplayPath((*cit),30);
343
344             path = rootpath + label;
345             label = "_" + label;
346             
347             Menus.push_back(Gnome::UI::Item(label,
348                                             bind<int>(slot(this, &Menubar::Pimpl::callback), action),
349                                             label));
350           }
351         break;
352       }
353       
354       case MenuItem::Documents: {
355         std::vector<string> names = bufferlist.getFileNames();
356
357         for (std::vector<string>::const_iterator cit = names.begin();
358              cit != names.end() ; ++cit)
359           {
360             int action = lyxaction.getPseudoAction(LFUN_SWITCHBUFFER, *cit);
361             string label = MakeDisplayPath(*cit, 30);
362
363             path = rootpath + label;
364             
365             Menus.push_back(Gnome::UI::Item(label,
366                                             bind<int>(slot(this, &Menubar::Pimpl::callback), action),
367                                             label));
368             
369           }
370         break;
371       }
372
373       case MenuItem::Toc: {
374         ListsHolder t;
375         t.path = path;
376         toc_.push_back(t);
377         break;
378       }
379       
380       case MenuItem::ViewFormats: {
381         add_formats(Menus, LFUN_PREVIEW, true);
382         break;
383       }
384         
385       case MenuItem::UpdateFormats: {
386         add_formats(Menus, LFUN_UPDATE, true);
387         break;  
388       }
389         
390       case MenuItem::ExportFormats: {
391         add_formats(Menus, LFUN_EXPORT, false);
392         break;
393       }
394       }
395     }
396 }
397
398 void Menubar::Pimpl::add_formats(vector<Gnome::UI::Info> & Menus, kb_action action, bool viewable)
399 {
400   vector<pair<string,string> > names = 
401     viewable
402     ? Exporter::GetViewableFormats(owner_->buffer())
403     : Exporter::GetExportableFormats(owner_->buffer());
404   
405   for (vector<pair<string,string> >::const_iterator cit = names.begin();
406        cit != names.end() ; ++cit) {
407     int action2 = lyxaction.getPseudoAction(action, (*cit).first);
408     string label = (*cit).second;
409
410     Menus.push_back(Gnome::UI::Item(label,
411                                     bind<int>(slot(this, &Menubar::Pimpl::callback), action2),
412                                     label));
413   }
414 }
415
416 void Menubar::Pimpl::connectWidgetToAction(GnomeUIInfo * guinfo)
417 {
418   for (; guinfo->type !=  GnomeUIInfoType(GNOME_APP_UI_ENDOFINFO); ++guinfo)
419     {
420       if ( ( guinfo->type == GnomeUIInfoType(GNOME_APP_UI_ITEM) ||
421              guinfo->type == GnomeUIInfoType(GNOME_APP_UI_TOGGLEITEM) ) &&
422            guinfo->moreinfo != NULL )
423         {
424           (*((void(*)(void *, void *))(guinfo->moreinfo)))(NULL, guinfo->user_data);
425           wid_act_.push_back( GtkWidgetToAction( guinfo->widget, action_ ) );
426         }
427       else if ( guinfo->type == GnomeUIInfoType(GNOME_APP_UI_SUBTREE) ||
428                 guinfo->type == GnomeUIInfoType(GNOME_APP_UI_RADIOITEMS) )
429         {
430           connectWidgetToAction(  (GnomeUIInfo *)(guinfo->moreinfo) );
431         }
432     }
433 }
434
435 void Menubar::Pimpl::update()
436 {
437   vector<GtkWidgetToAction>::const_iterator end=wid_act_.end();
438   for (vector<GtkWidgetToAction>::const_iterator i = wid_act_.begin(); i != end; ++i)
439     {
440       GtkWidgetToAction wa = (*i);
441       LyXFunc::func_status flag = owner_->getLyXFunc()->getStatus(wa.action_);
442
443       if ( flag & (LyXFunc::Disabled | LyXFunc::Unknown) ) gtk_widget_set_sensitive(wa.widget_, false);
444       else gtk_widget_set_sensitive(wa.widget_, true);
445
446       if ( flag & LyXFunc::ToggleOn )
447         {
448           ignore_action_=true;
449           gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(wa.widget_), true);
450           ignore_action_=false;
451         }
452
453       if ( flag & LyXFunc::ToggleOff )
454         {
455           ignore_action_=true;
456           gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(wa.widget_), false);
457           ignore_action_=false;
458         }
459     }
460 }
461
462 void Menubar::Pimpl::openByName(string const &)
463 {
464 //    Pimpl::update();
465 }