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