2 /* This file is part of
3 * ======================================================
5 * LyX, The Document Processor
7 * Copyright 2000 The LyX Team.
9 * ====================================================== */
12 #pragma implementation
19 #include "support/lstrings.h"
20 #include "support/filetools.h"
21 #include "support/StrPool.h"
22 #include "support/LAssert.h"
24 #include "LyXAction.h"
27 #include "bufferlist.h"
28 #include "lastfiles.h"
30 #include "MenuBackend.h"
31 #include "Menubar_pimpl.h"
40 // temporary solution for LyXView
41 extern GLyxAppWin * mainAppWin;
44 extern kb_keymap * toplevel_keymap;
45 extern LyXAction lyxaction;
46 extern BufferList bufferlist;
47 extern LastFiles * lastfiles;
51 Menubar::Pimpl::Pimpl(LyXView * view, MenuBackend const & mb)
52 : owner_(view), menubackend_(&mb), ignore_action_(false)
54 // Should we do something here?
57 Menubar::Pimpl::~Pimpl()
59 // Should we do something here?
62 void Menubar::Pimpl::set(string const & menu_name)
64 // if (current_menu_name_ != menu_name) // disabled until Lastfiles and Documents are added dynamically to menu
66 current_menu_name_ = menu_name;
68 vector<Gnome::UI::Info> menus;
69 composeUIInfo(current_menu_name_, menus);
73 mainAppWin->set_menu(Menu_);
75 // connect all menu items to correspoding action
77 ignore_action_ = true;
78 connectWidgetToAction(Menu_.gtkobj());
79 ignore_action_ = false;
81 // update state of the items
86 void Menubar::Pimpl::callback(int action)
88 // Dispatch action OR record action to local variable (see connectWidgetToAction)
89 if (!ignore_action_) {
91 owner_->getLyXFunc()->Dispatch(action);
96 void Menubar::Pimpl::composeUIInfo(string const & menu_name, vector<Gnome::UI::Info> & Menus)
98 if (!menubackend_->hasMenu(menu_name))
100 cout << "ERROR:composeUIInfo: Unknown menu `" << menu_name
105 Menu menu = menubackend_->getMenu(menu_name);
107 for (Menu::const_iterator i = menu.begin(); i != menu.end(); ++i)
109 MenuItem item = (*i);
110 switch(item.kind()) {
112 case MenuItem::Command: {
113 string label = item.label();
114 if (label.find(item.shortcut()) != string::npos)
115 label.insert(label.find(item.shortcut()), "_");
117 LyXFunc::func_status flag = owner_->getLyXFunc()->getStatus(item.action());
119 Gnome::UI::Info gitem;
120 SigC::Slot0<void> cback = bind<int>(slot(this, &Menubar::Pimpl::callback),item.action());
123 using namespace Gnome::MenuItems;
124 int ac = item.action();
127 if (lyxaction.isPseudoAction(ac))
128 action = lyxaction.retrieveActionArg(ac, argument);
130 action = static_cast<kb_action>(ac);
139 case LFUN_CLOSEBUFFER:
140 gitem = Close(cback);
145 case LFUN_MENUWRITEAS:
146 gitem = SaveAs(cback);
148 case LFUN_BUFFER_PRINT:
149 gitem = Print(cback);
158 gitem = Paste(cback);
161 gitem = Gnome::MenuItems::Undo(cback); // confused with class Undo
166 case LFUN_DIALOG_PREFERENCES:
167 gitem = Preferences(cback);
170 gitem = Gnome::UI::Item(Gnome::UI::Icon(GNOME_STOCK_MENU_NEW),
171 label, cback, lyxaction.helpText(item.action()));
173 case LFUN_MENUNEWTMPLT:
174 gitem = Gnome::UI::Item(Gnome::UI::Icon(GNOME_STOCK_MENU_NEW),
175 label, cback, lyxaction.helpText(item.action()));
177 case LFUN_MENUSEARCH:
178 gitem = Gnome::UI::Item(Gnome::UI::Icon(GNOME_STOCK_MENU_SRCHRPL),
179 label, cback, lyxaction.helpText(item.action()));
181 case LFUN_SPELLCHECK:
182 gitem = Gnome::UI::Item(Gnome::UI::Icon(GNOME_STOCK_MENU_SPELLCHECK),
183 label, cback, lyxaction.helpText(item.action()));
186 gitem = Gnome::UI::Item(label, cback, lyxaction.helpText(item.action()));
191 // first handle optional entries.
192 if (item.optional() && (flag & LyXFunc::Disabled)) {
194 << "Skipping optional item " << item.label() << endl;
197 if ((flag & LyXFunc::ToggleOn) || (flag & LyXFunc::ToggleOff))
198 gitem = Gnome::UI::ToggleItem(label, cback, lyxaction.helpText(item.action()));
200 Menus.push_back(gitem);
204 case MenuItem::Submenu: {
205 vector<Gnome::UI::Info> submenu;
206 string label = item.label();
207 if (label.find(item.shortcut()) != string::npos)
208 label.insert(label.find(item.shortcut()), "_");
209 composeUIInfo(item.submenu(), submenu);
210 Menus.push_back(Gnome::UI::Menu(label,submenu,label));
214 case MenuItem::Separator: {
215 Menus.push_back(Gnome::UI::Separator());
219 case MenuItem::Lastfiles: {
221 for (LastFiles::const_iterator cit = lastfiles->begin();
222 cit != lastfiles->end() && ii < 10; ++cit, ++ii)
224 int action = lyxaction.getPseudoAction(LFUN_FILE_OPEN, (*cit));
225 string label = "_" + tostr(ii) + ". " + MakeDisplayPath((*cit),30);
227 Menus.push_back(Gnome::UI::Item(label,
228 bind<int>(slot(this, &Menubar::Pimpl::callback), action),
234 case MenuItem::Documents: {
235 std::vector<string> names = bufferlist.getFileNames();
237 for (std::vector<string>::const_iterator cit = names.begin();
238 cit != names.end() ; ++cit)
240 int action = lyxaction.getPseudoAction(LFUN_SWITCHBUFFER, *cit);
241 string label = MakeDisplayPath(*cit, 30);
243 Menus.push_back(Gnome::UI::Item(label,
244 bind<int>(slot(this, &Menubar::Pimpl::callback), action),
254 void Menubar::Pimpl::connectWidgetToAction(GnomeUIInfo * guinfo)
256 for (; guinfo->type != GnomeUIInfoType(GNOME_APP_UI_ENDOFINFO); ++guinfo)
258 if ( guinfo->type == GnomeUIInfoType(GNOME_APP_UI_ITEM) ||
259 guinfo->type == GnomeUIInfoType(GNOME_APP_UI_TOGGLEITEM) )
261 (*((void(*)(void *, void *))(guinfo->moreinfo)))(NULL, guinfo->user_data);
262 wid_act_.push_back( GtkWidgetToAction( guinfo->widget, action_ ) );
264 else if ( guinfo->type == GnomeUIInfoType(GNOME_APP_UI_SUBTREE) ||
265 guinfo->type == GnomeUIInfoType(GNOME_APP_UI_RADIOITEMS) )
267 connectWidgetToAction( (GnomeUIInfo *)(guinfo->moreinfo) );
272 void Menubar::Pimpl::update()
274 vector<GtkWidgetToAction>::const_iterator end=wid_act_.end();
275 for (vector<GtkWidgetToAction>::const_iterator i = wid_act_.begin(); i != end; ++i)
277 GtkWidgetToAction wa = (*i);
278 LyXFunc::func_status flag = owner_->getLyXFunc()->getStatus(wa.action_);
280 if ( flag & (LyXFunc::Disabled | LyXFunc::Unknown) ) gtk_widget_set_sensitive(wa.widget_, false);
281 else gtk_widget_set_sensitive(wa.widget_, true);
283 if ( flag & LyXFunc::ToggleOn )
286 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(wa.widget_), true);
287 ignore_action_=false;
290 if ( flag & LyXFunc::ToggleOff )
293 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(wa.widget_), false);
294 ignore_action_=false;
299 void Menubar::Pimpl::openByName(string const &)