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