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