]> git.lyx.org Git - lyx.git/blob - src/frontends/gnome/Menubar_pimpl.C
Ed's credits patch and Baruch's gnome patch.
[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 boost::scoped_ptr<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                                      _("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 = _("<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 0 
195   if (!owner_->view()->available()) return;
196   
197   owner_->view()->beforeChange();
198   owner_->view()->text->SetCursor( owner_->view(), tg.par, 0 );
199   owner_->view()->text->sel_cursor = owner_->view()->text->cursor;
200   owner_->view()->update(BufferView::SELECT|BufferView::FITCUR);
201 #endif
202
203   owner_->getLyXFunc()->Dispatch(LFUN_GOTO_PARAGRAPH, tg.str);
204 }
205
206 void Menubar::Pimpl::composeUIInfo(string const & menu_name, vector<Gnome::UI::Info> & Menus, string rootpath)
207 {
208   string path = rootpath;
209     
210   if (!menubackend_->hasMenu(menu_name))
211     {
212       cout << "ERROR:composeUIInfo: Unknown menu `" << menu_name
213            << "'" << endl;
214       return;
215     }
216
217   Menu menu = Menu();
218   menubackend_->getMenu(menu_name).expand(menu, owner_->buffer());
219
220   for (Menu::const_iterator i = menu.begin(); i != menu.end(); ++i)
221     {
222       MenuItem item = (*i);
223       switch(item.kind()) {
224
225       case MenuItem::Command: {
226         string label = item.label();
227
228         path = rootpath + label;
229         
230         if (label.find(item.shortcut()) != string::npos)
231           label.insert(label.find(item.shortcut()), "_");
232
233         LyXFunc::func_status flag = owner_->getLyXFunc()->getStatus(item.action());
234
235         Gnome::UI::Info gitem;
236         SigC::Slot0<void> cback = bind<int>(slot(this, &Menubar::Pimpl::callback),item.action());
237
238         {
239           using namespace Gnome::MenuItems;
240           int ac = item.action();
241           kb_action action;
242           string argument;
243           if (lyxaction.isPseudoAction(ac))
244             action = lyxaction.retrieveActionArg(ac, argument);
245           else
246             action = static_cast<kb_action>(ac);
247
248           switch(action) {
249           case LFUN_MENUOPEN:
250             gitem = Open(cback);
251             break;
252           case LFUN_QUIT:
253             gitem = Exit(cback);
254             break;
255           case LFUN_CLOSEBUFFER:
256             gitem = Close(cback);
257             break;
258           case LFUN_MENUWRITE:
259             gitem = Save(cback);
260             break;
261           case LFUN_MENUWRITEAS:
262             gitem = SaveAs(cback);
263             break;
264           case LFUN_BUFFER_PRINT:
265             gitem = Print(cback);
266             break;
267           case LFUN_CUT:
268             gitem = Cut(cback);
269             break;
270           case LFUN_COPY:
271             gitem = Copy(cback);
272             break;
273           case LFUN_PASTE:
274             gitem = Paste(cback);
275             break;
276           case LFUN_UNDO:
277             gitem = Gnome::MenuItems::Undo(cback); // confused with class Undo
278             break;
279           case LFUN_REDO:
280             gitem = Redo(cback);
281             break;
282           case LFUN_DIALOG_PREFERENCES:
283             gitem = Preferences(cback);
284             break;
285           case LFUN_MENUNEW:
286             gitem = Gnome::UI::Item(Gnome::UI::Icon(GNOME_STOCK_MENU_NEW),
287                                     label, cback, lyxaction.helpText(item.action()));
288             break;
289           case LFUN_MENUNEWTMPLT:
290             gitem = Gnome::UI::Item(Gnome::UI::Icon(GNOME_STOCK_MENU_NEW), 
291                                     label, cback, lyxaction.helpText(item.action()));
292             break;
293           case LFUN_MENUSEARCH:
294             gitem = Gnome::UI::Item(Gnome::UI::Icon(GNOME_STOCK_MENU_SRCHRPL), 
295                                     label, cback, lyxaction.helpText(item.action()));
296             break;
297           case LFUN_SPELLCHECK:
298             gitem = Gnome::UI::Item(Gnome::UI::Icon(GNOME_STOCK_MENU_SPELLCHECK), 
299                                     label, cback, lyxaction.helpText(item.action()));
300             break;
301           default:
302             gitem = Gnome::UI::Item(label, cback, lyxaction.helpText(item.action()));
303             break;
304           }
305         }
306
307         // first handle optional entries.
308         if (item.optional() && (flag & LyXFunc::Disabled)) {
309             lyxerr[Debug::GUI] 
310                 << "Skipping optional item " << item.label() << endl; 
311             break;
312         }
313         if ((flag & LyXFunc::ToggleOn) || (flag & LyXFunc::ToggleOff))
314           gitem = Gnome::UI::ToggleItem(label, cback, lyxaction.helpText(item.action()));
315
316         Menus.push_back(gitem);
317         break;
318       }
319       
320       case MenuItem::Submenu: {
321         vector<Gnome::UI::Info> submenu;
322         string label = item.label();
323
324         path = rootpath + label;
325         
326         if (label.find(item.shortcut()) != string::npos)
327           label.insert(label.find(item.shortcut()), "_");
328         composeUIInfo(item.submenu(), submenu, path + "/");
329         Menus.push_back(Gnome::UI::Menu(label,submenu,label));
330         break;
331       }
332
333       case MenuItem::Separator: {
334
335         path = rootpath + "<Separator>";
336         
337         Menus.push_back(Gnome::UI::Separator());
338         break;
339       }
340
341       case MenuItem::Toc: {
342         ListsHolder t;
343         t.path = path;
344         toc_.push_back(t);
345         break;
346       }
347       
348       case MenuItem::Documents: 
349       case MenuItem::Lastfiles: 
350       case MenuItem::ViewFormats:
351       case MenuItem::UpdateFormats:
352       case MenuItem::ExportFormats:
353                         lyxerr << "Menubar::Pimpl::create_submenu: "
354                           "this should not happen" << endl;
355                         break;
356       }
357     }
358 }
359
360 void Menubar::Pimpl::connectWidgetToAction(GnomeUIInfo * guinfo)
361 {
362   for (; guinfo->type !=  GnomeUIInfoType(GNOME_APP_UI_ENDOFINFO); ++guinfo)
363     {
364       if ( ( guinfo->type == GnomeUIInfoType(GNOME_APP_UI_ITEM) ||
365              guinfo->type == GnomeUIInfoType(GNOME_APP_UI_TOGGLEITEM) ) &&
366            guinfo->moreinfo != 0 )
367         {
368           (*((void(*)(void *, void *))(guinfo->moreinfo)))(0, guinfo->user_data);
369           wid_act_.push_back( GtkWidgetToAction( guinfo->widget, action_ ) );
370         }
371       else if ( guinfo->type == GnomeUIInfoType(GNOME_APP_UI_SUBTREE) ||
372                 guinfo->type == GnomeUIInfoType(GNOME_APP_UI_RADIOITEMS) )
373         {
374           connectWidgetToAction(  (GnomeUIInfo *)(guinfo->moreinfo) );
375         }
376     }
377 }
378
379 void Menubar::Pimpl::update()
380 {
381   vector<GtkWidgetToAction>::const_iterator end=wid_act_.end();
382   for (vector<GtkWidgetToAction>::const_iterator i = wid_act_.begin(); i != end; ++i)
383     {
384       GtkWidgetToAction wa = (*i);
385       LyXFunc::func_status flag = owner_->getLyXFunc()->getStatus(wa.action_);
386
387       if ( flag & (LyXFunc::Disabled | LyXFunc::Unknown) ) gtk_widget_set_sensitive(wa.widget_, false);
388       else gtk_widget_set_sensitive(wa.widget_, true);
389
390       if ( flag & LyXFunc::ToggleOn )
391         {
392           ignore_action_=true;
393           gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(wa.widget_), true);
394           ignore_action_=false;
395         }
396
397       if ( flag & LyXFunc::ToggleOff )
398         {
399           ignore_action_=true;
400           gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(wa.widget_), false);
401           ignore_action_=false;
402         }
403     }
404 }
405
406 void Menubar::Pimpl::openByName(string const &)
407 {
408 //    Pimpl::update();
409 }