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