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