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 LyXAction lyxaction;
45 extern BufferList bufferlist;
46 extern LastFiles * lastfiles;
50 Menubar::Pimpl::Pimpl(LyXView * view, MenuBackend const & mb)
51 : owner_(view), menubackend_(&mb), ignore_action_(false)
53 // Should we do something here?
56 Menubar::Pimpl::~Pimpl()
58 // Should we do something here?
61 void Menubar::Pimpl::set(string const & menu_name)
63 // if (current_menu_name_ != menu_name) // disabled until Lastfiles and Documents are added dynamically to menu
65 current_menu_name_ = menu_name;
67 vector<Gnome::UI::Info> menus;
68 composeUIInfo(current_menu_name_, menus);
72 mainAppWin->set_menu(Menu_);
74 // connect all menu items to correspoding action
76 ignore_action_ = true;
77 connectWidgetToAction(Menu_.gtkobj());
78 ignore_action_ = false;
80 // update state of the items
85 void Menubar::Pimpl::callback(int action)
87 // Dispatch action OR record action to local variable (see connectWidgetToAction)
88 if (!ignore_action_) owner_->getLyXFunc()->Dispatch(action);
89 else action_ = action;
92 void Menubar::Pimpl::composeUIInfo(string const & menu_name, vector<Gnome::UI::Info> & Menus)
94 if (!menubackend_->hasMenu(menu_name))
96 cout << "ERROR:composeUIInfo: Unknown menu `" << menu_name
101 Menu menu = menubackend_->getMenu(menu_name);
103 for (Menu::const_iterator i = menu.begin(); i != menu.end(); ++i)
105 MenuItem item = (*i);
106 switch(item.kind()) {
108 case MenuItem::Command: {
109 string label = item.label();
110 if (label.find(item.shortcut()) != string::npos)
111 label.insert(label.find(item.shortcut()), "_");
113 LyXFunc::func_status flag = owner_->getLyXFunc()->getStatus(item.action());
115 Gnome::UI::Info gitem;
116 SigC::Slot0<void> cback = bind<int>(slot(this, &Menubar::Pimpl::callback),item.action());
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()));
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()));
155 Menus.push_back(gitem);
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));
169 case MenuItem::Separator: {
170 Menus.push_back(Gnome::UI::Separator());
174 case MenuItem::Lastfiles: {
176 for (LastFiles::const_iterator cit = lastfiles->begin();
177 cit != lastfiles->end() && ii < 10; ++cit, ++ii)
179 int action = lyxaction.getPseudoAction(LFUN_FILE_OPEN, (*cit));
180 string label = "_" + tostr(ii) + ". " + MakeDisplayPath((*cit),30);
182 Menus.push_back(Gnome::UI::Item(label,
183 bind<int>(slot(this, &Menubar::Pimpl::callback), action),
189 case MenuItem::Documents: {
190 std::vector<string> names = bufferlist.getFileNames();
192 for (std::vector<string>::const_iterator cit = names.begin();
193 cit != names.end() ; ++cit)
195 int action = lyxaction.getPseudoAction(LFUN_SWITCHBUFFER, *cit);
196 string label = MakeDisplayPath(*cit, 30);
198 Menus.push_back(Gnome::UI::Item(label,
199 bind<int>(slot(this, &Menubar::Pimpl::callback), action),
209 void Menubar::Pimpl::connectWidgetToAction(GnomeUIInfo * guinfo)
211 for (; guinfo->type != GnomeUIInfoType(GNOME_APP_UI_ENDOFINFO); ++guinfo)
213 if ( guinfo->type == GnomeUIInfoType(GNOME_APP_UI_ITEM) ||
214 guinfo->type == GnomeUIInfoType(GNOME_APP_UI_TOGGLEITEM) )
216 (*((void(*)(void *, void *))(guinfo->moreinfo)))(NULL, guinfo->user_data);
217 wid_act_.push_back( GtkWidgetToAction( guinfo->widget, action_ ) );
219 else if ( guinfo->type == GnomeUIInfoType(GNOME_APP_UI_SUBTREE) ||
220 guinfo->type == GnomeUIInfoType(GNOME_APP_UI_RADIOITEMS) )
222 connectWidgetToAction( (GnomeUIInfo *)(guinfo->moreinfo) );
227 void Menubar::Pimpl::update()
229 vector<GtkWidgetToAction>::const_iterator end=wid_act_.end();
230 for (vector<GtkWidgetToAction>::const_iterator i = wid_act_.begin(); i != end; ++i)
232 GtkWidgetToAction wa = (*i);
233 LyXFunc::func_status flag = owner_->getLyXFunc()->getStatus(wa.action_);
235 if ( flag & (LyXFunc::Disabled | LyXFunc::Unknown) ) gtk_widget_set_sensitive(wa.widget_, false);
236 else gtk_widget_set_sensitive(wa.widget_, true);
238 //don't know how to toggle/untoggle GtkCheckMenuItem
239 if ( flag & LyXFunc::ToggleOn )
242 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(wa.widget_), true);
243 ignore_action_=false;
246 if ( flag & LyXFunc::ToggleOff )
249 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(wa.widget_), false);
250 ignore_action_=false;