]> git.lyx.org Git - lyx.git/blob - src/frontends/gnome/Menubar_pimpl.C
major GUII cleanup + Baruchs patch + Angus's patch + removed a couple of generated...
[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
33 #include "mainapp.h"
34
35 using SigC::slot;
36 using SigC::bind;
37
38 using std::endl;
39
40 // temporary solution for LyXView
41 extern GLyxAppWin * mainAppWin;
42
43 // Some constants
44 extern LyXAction lyxaction;
45 extern BufferList bufferlist;
46 extern LastFiles * lastfiles; 
47
48
49
50 Menubar::Pimpl::Pimpl(LyXView * view, MenuBackend const & mb) 
51   : owner_(view), menubackend_(&mb), ignore_action_(false)
52 {
53         // Should we do something here?
54 }
55
56 Menubar::Pimpl::~Pimpl() 
57 {
58         // Should we do something here?
59 }
60
61 void Menubar::Pimpl::set(string const & menu_name) 
62 {
63   // if (current_menu_name_ != menu_name)  // disabled until Lastfiles and Documents are added dynamically to menu
64     {
65       current_menu_name_ = menu_name;
66       // compose new menu
67       vector<Gnome::UI::Info> menus;
68       composeUIInfo(current_menu_name_, menus);
69
70       // set menu
71       Menu_ = menus;
72       mainAppWin->set_menu(Menu_);
73
74       // connect all menu items to correspoding action
75       wid_act_.clear();
76       ignore_action_ = true;
77       connectWidgetToAction(Menu_.gtkobj());
78       ignore_action_ = false;
79
80       // update state of the items
81       update();
82     }
83 }
84
85 void Menubar::Pimpl::callback(int action)
86 {
87   // Dispatch action OR record action to local variable (see connectWidgetToAction)
88   if (!ignore_action_) owner_->getLyXFunc()->Dispatch(action);
89   else action_ = action;
90 }
91
92 void Menubar::Pimpl::composeUIInfo(string const & menu_name, vector<Gnome::UI::Info> & Menus)
93 {
94   if (!menubackend_->hasMenu(menu_name))
95     {
96       cout << "ERROR:composeUIInfo: Unknown menu `" << menu_name
97            << "'" << endl;
98       return;
99     }
100
101   Menu menu = menubackend_->getMenu(menu_name);
102
103   for (Menu::const_iterator i = menu.begin(); i != menu.end(); ++i)
104     {
105       MenuItem item = (*i);
106       switch(item.kind()) {
107
108       case MenuItem::Command: {
109         string label = item.label();
110         if (label.find(item.shortcut()) != string::npos)
111           label.insert(label.find(item.shortcut()), "_");
112
113         LyXFunc::func_status flag = owner_->getLyXFunc()->getStatus(item.action());
114
115         Gnome::UI::Info gitem;
116         SigC::Slot0<void> cback = bind<int>(slot(this, &Menubar::Pimpl::callback),item.action());
117
118         {
119           using namespace Gnome::MenuItems;
120           string actionname = lyxaction.getActionName(item.action());
121           if ( actionname ==  "buffer-open") gitem = Open(cback);
122           else if ( actionname ==  "lyx-quit") gitem = Exit(cback);
123           else if ( actionname ==  "buffer-close") gitem = Close(cback);
124           else if ( actionname ==  "buffer-write") gitem = Save(cback);
125           else if ( actionname ==  "buffer-write-as") gitem = SaveAs(cback);
126           else if ( actionname ==  "buffer-print") gitem = Print(cback);
127           else if ( actionname ==  "cut") gitem = Cut(cback);
128           else if ( actionname ==  "copy") gitem = Copy(cback);
129           else if ( actionname ==  "paste") gitem = Paste(cback);
130           else if ( actionname ==  "undo") gitem = Gnome::MenuItems::Undo(cback); // confused with class Undo
131           else if ( actionname ==  "redo") gitem = Redo(cback);
132           else if ( actionname ==  "dialog-preferences") gitem = Preferences(cback);
133           else if ( actionname ==  "buffer-new")
134             gitem = Gnome::UI::Item(Gnome::UI::Icon(GNOME_STOCK_MENU_NEW),
135                                     label, cback, lyxaction.helpText(item.action()));
136           else if ( actionname ==  "buffer-new-template")
137             gitem = Gnome::UI::Item(Gnome::UI::Icon(GNOME_STOCK_MENU_NEW), 
138                                     label, cback, lyxaction.helpText(item.action()));
139           else if ( actionname ==  "find-replace" )
140             gitem = Gnome::UI::Item(Gnome::UI::Icon(GNOME_STOCK_MENU_SRCHRPL), 
141                                     label, cback, lyxaction.helpText(item.action()));
142           else if ( actionname ==  "spellchecker" )
143             gitem = Gnome::UI::Item(Gnome::UI::Icon(GNOME_STOCK_MENU_SPELLCHECK), 
144                                     label, cback, lyxaction.helpText(item.action()));
145 //        else if ( actionname ==  "" )
146 //          gitem = Gnome::UI::Item(Gnome::UI::Icon(), 
147 //                                  label, cback, lyxaction.helpText(item.action()));
148           else gitem = Gnome::UI::Item(label, cback, lyxaction.helpText(item.action()));
149         }
150
151         // DON'T KNOW HOW TO TOGGLE/UNTOGGLE IT (Marko)
152         if ((flag & LyXFunc::ToggleOn) || (flag & LyXFunc::ToggleOff))
153           gitem = Gnome::UI::ToggleItem(label, cback, lyxaction.helpText(item.action()));
154         
155         Menus.push_back(gitem);
156         break;
157       }
158       
159       case MenuItem::Submenu: {
160         vector<Gnome::UI::Info> submenu;
161         string label = item.label();
162         if (label.find(item.shortcut()) != string::npos)
163           label.insert(label.find(item.shortcut()), "_");
164         composeUIInfo(item.submenu(), submenu);
165         Menus.push_back(Gnome::UI::Menu(label,submenu,label));
166         break;
167       }
168
169       case MenuItem::Separator: {
170         Menus.push_back(Gnome::UI::Separator());
171         break;
172       }
173
174       case MenuItem::Lastfiles: {
175         int ii = 1;
176         for (LastFiles::const_iterator cit = lastfiles->begin();
177              cit != lastfiles->end() && ii < 10; ++cit, ++ii)
178           {
179             int action = lyxaction.getPseudoAction(LFUN_FILE_OPEN, (*cit));
180             string label = "_" + tostr(ii) + ". " + MakeDisplayPath((*cit),30);
181
182             Menus.push_back(Gnome::UI::Item(label,
183                                             bind<int>(slot(this, &Menubar::Pimpl::callback), action),
184                                             label));
185           }
186         break;
187       }
188       
189       case MenuItem::Documents: {
190         std::vector<string> names = bufferlist.getFileNames();
191
192         for (std::vector<string>::const_iterator cit = names.begin();
193              cit != names.end() ; ++cit)
194           {
195             int action = lyxaction.getPseudoAction(LFUN_SWITCHBUFFER, *cit);
196             string label = MakeDisplayPath(*cit, 30);
197
198             Menus.push_back(Gnome::UI::Item(label,
199                                             bind<int>(slot(this, &Menubar::Pimpl::callback), action),
200                                             label));
201             
202           }
203         break;
204       }
205       }
206     }
207 }
208
209 void Menubar::Pimpl::connectWidgetToAction(GnomeUIInfo * guinfo)
210 {
211   for (; guinfo->type !=  GnomeUIInfoType(GNOME_APP_UI_ENDOFINFO); ++guinfo)
212     {
213       if ( guinfo->type == GnomeUIInfoType(GNOME_APP_UI_ITEM) ||
214            guinfo->type == GnomeUIInfoType(GNOME_APP_UI_TOGGLEITEM) )
215         {
216           (*((void(*)(void *, void *))(guinfo->moreinfo)))(NULL, guinfo->user_data);
217           wid_act_.push_back( GtkWidgetToAction( guinfo->widget, action_ ) );
218         }
219       else if ( guinfo->type == GnomeUIInfoType(GNOME_APP_UI_SUBTREE) ||
220                 guinfo->type == GnomeUIInfoType(GNOME_APP_UI_RADIOITEMS) )
221         {
222           connectWidgetToAction(  (GnomeUIInfo *)(guinfo->moreinfo) );
223         }
224     }
225 }
226
227 void Menubar::Pimpl::update()
228 {
229   vector<GtkWidgetToAction>::const_iterator end=wid_act_.end();
230   for (vector<GtkWidgetToAction>::const_iterator i = wid_act_.begin(); i != end; ++i)
231     {
232       GtkWidgetToAction wa = (*i);
233       LyXFunc::func_status flag = owner_->getLyXFunc()->getStatus(wa.action_);
234
235       if ( flag & (LyXFunc::Disabled | LyXFunc::Unknown) ) gtk_widget_set_sensitive(wa.widget_, false);
236       else gtk_widget_set_sensitive(wa.widget_, true);
237
238       //don't know how to toggle/untoggle GtkCheckMenuItem
239       if ( flag & LyXFunc::ToggleOn )
240         {
241           ignore_action_=true;
242           gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(wa.widget_), true);
243           ignore_action_=false;
244         }
245
246       if ( flag & LyXFunc::ToggleOff )
247         {
248           ignore_action_=true;
249           gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(wa.widget_), false);
250           ignore_action_=false;
251         }
252     }
253 }