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"
37 #include <gtk--/menu.h>
41 // temporary solution for LyXView
42 extern GLyxAppWin * mainAppWin;
45 extern boost::scoped_ptr<kb_keymap> toplevel_keymap;
46 extern LyXAction lyxaction;
47 extern BufferList bufferlist;
48 extern LastFiles * lastfiles;
52 Menubar::Pimpl::Pimpl(LyXView * view, MenuBackend const & mb)
53 : owner_(view), menubackend_(&mb), ignore_action_(false)
58 Menubar::Pimpl::~Pimpl()
60 if (utoc_.connected()) utoc_.disconnect();
63 void Menubar::Pimpl::set(string const & menu_name)
65 // if (current_menu_name_ != menu_name) // disabled until Lastfiles and Documents are added dynamically to menu
67 current_menu_name_ = menu_name;
71 if (utoc_.connected()) utoc_.disconnect();
74 vector<Gnome::UI::Info> menus;
75 composeUIInfo(current_menu_name_, menus, "");
79 mainAppWin->set_menu(Menu_);
81 // connect all menu items to correspoding action
83 ignore_action_ = true;
84 connectWidgetToAction(Menu_.gtkobj());
85 ignore_action_ = false;
87 // update state of the items
93 void Menubar::Pimpl::updateAllLists()
98 vector<Buffer::TocItem> toclist = (owner_->view()->buffer()->getTocList())[Buffer::TOC_TOC];
99 updateList(&toclist, &toc_);
103 int const max_number_of_items = 25;
105 void Menubar::Pimpl::updateList(vector<Buffer::TocItem> * toclist, vector<ListsHolder> * pgui)
107 vector<ListsHolder> & gui = *pgui;
108 int szGui = gui.size();
110 for (i=0; i < szGui; ++i)
112 int oldsz = gui[i].lst.size();
113 vector<Gnome::UI::Info> menu;
116 menu.push_back(Gnome::UI::Item(Gnome::UI::Icon(GNOME_STOCK_MENU_REFRESH),
117 _("Refresh"), SigC::slot(this, &Menubar::Pimpl::updateAllLists)));
119 if (toclist->size() > max_number_of_items)
120 composeTocUIInfo(menu, *toclist, toclist->begin(), 0);
123 vector<Buffer::TocItem>::const_iterator end = toclist->end();
124 for (vector<Buffer::TocItem>::const_iterator it = toclist->begin();
128 label = string(4*(*it).depth,' ')+(*it).str;
130 menu.push_back(Gnome::UI::Item(label,
131 SigC::bind<Buffer::TocItem>(SigC::slot(this, &Menubar::Pimpl::callbackToc), (*it)),
137 mainAppWin->update_menu(gui[i].path, oldsz, gui[i].lst);
141 vector<Buffer::TocItem>::const_iterator
142 Menubar::Pimpl::composeTocUIInfo(vector<Gnome::UI::Info> & menu,
143 vector<Buffer::TocItem> const & toclist,
144 vector<Buffer::TocItem>::const_iterator begin,
147 string label = _("<No Name>");
149 vector<Buffer::TocItem>::const_iterator end = toclist.end();
150 vector<Buffer::TocItem>::const_iterator it;
151 for (it = begin; it != end && (*it).depth >= mylevel; ++it)
153 if ( (*it).depth == mylevel &&
154 (it+1 == end || (*(it+1)).depth <= mylevel) )
157 menu.push_back(Gnome::UI::Item(label,
158 SigC::bind<Buffer::TocItem>(SigC::slot(this, &Menubar::Pimpl::callbackToc), (*it)),
163 vector<Gnome::UI::Info> submenu;
164 if ( (*it).depth == mylevel )
167 submenu.push_back(Gnome::UI::Item(label,
168 SigC::bind<Buffer::TocItem>(SigC::slot(this, &Menubar::Pimpl::callbackToc), (*it)),
172 it = composeTocUIInfo(submenu, toclist, it, mylevel+1);
173 menu.push_back(Gnome::UI::Menu(label,submenu,label));
180 void Menubar::Pimpl::callback(int action)
182 // Dispatch action OR record action to local variable (see connectWidgetToAction)
183 if (!ignore_action_) {
185 owner_->getLyXFunc()->Dispatch(action);
190 void Menubar::Pimpl::callbackToc(Buffer::TocItem tg)
193 if (!owner_->view()->available()) return;
195 owner_->view()->beforeChange();
196 owner_->view()->text->SetCursor( owner_->view(), tg.par, 0 );
197 owner_->view()->text->sel_cursor = owner_->view()->text->cursor;
198 owner_->view()->update(BufferView::SELECT|BufferView::FITCUR);
201 owner_->getLyXFunc()->Dispatch(LFUN_GOTO_PARAGRAPH, tg.str);
204 void Menubar::Pimpl::composeUIInfo(string const & menu_name, vector<Gnome::UI::Info> & Menus, string rootpath)
206 string path = rootpath;
208 if (!menubackend_->hasMenu(menu_name))
210 cout << "ERROR:composeUIInfo: Unknown menu `" << menu_name
216 menubackend_->getMenu(menu_name).expand(menu, owner_->buffer());
218 for (Menu::const_iterator i = menu.begin(); i != menu.end(); ++i)
220 MenuItem item = (*i);
221 switch(item.kind()) {
223 case MenuItem::Command: {
224 string label = item.label();
226 path = rootpath + label;
228 if (label.find(item.shortcut()) != string::npos)
229 label.insert(label.find(item.shortcut()), "_");
231 LyXFunc::func_status flag = owner_->getLyXFunc()->getStatus(item.action());
233 Gnome::UI::Info gitem;
234 SigC::Slot0<void> cback = SigC::bind<int>(SigC::slot(this, &Menubar::Pimpl::callback),item.action());
237 using namespace Gnome::MenuItems;
238 int ac = item.action();
241 if (lyxaction.isPseudoAction(ac))
242 action = lyxaction.retrieveActionArg(ac, argument);
244 action = static_cast<kb_action>(ac);
253 case LFUN_CLOSEBUFFER:
254 gitem = Close(cback);
260 gitem = SaveAs(cback);
262 case LFUN_BUFFER_PRINT:
263 gitem = Print(cback);
272 gitem = Paste(cback);
275 gitem = Gnome::MenuItems::Undo(cback); // confused with class Undo
280 case LFUN_DIALOG_PREFERENCES:
281 gitem = Preferences(cback);
284 gitem = Gnome::UI::Item(Gnome::UI::Icon(GNOME_STOCK_MENU_NEW),
285 label, cback, lyxaction.helpText(item.action()));
287 case LFUN_MENUNEWTMPLT:
288 gitem = Gnome::UI::Item(Gnome::UI::Icon(GNOME_STOCK_MENU_NEW),
289 label, cback, lyxaction.helpText(item.action()));
291 case LFUN_MENUSEARCH:
292 gitem = Gnome::UI::Item(Gnome::UI::Icon(GNOME_STOCK_MENU_SRCHRPL),
293 label, cback, lyxaction.helpText(item.action()));
295 case LFUN_SPELLCHECK:
296 gitem = Gnome::UI::Item(Gnome::UI::Icon(GNOME_STOCK_MENU_SPELLCHECK),
297 label, cback, lyxaction.helpText(item.action()));
300 gitem = Gnome::UI::Item(label, cback, lyxaction.helpText(item.action()));
305 // first handle optional entries.
306 if (item.optional() && (flag & LyXFunc::Disabled)) {
308 << "Skipping optional item " << item.label() << endl;
311 if ((flag & LyXFunc::ToggleOn) || (flag & LyXFunc::ToggleOff))
312 gitem = Gnome::UI::ToggleItem(label, cback, lyxaction.helpText(item.action()));
314 Menus.push_back(gitem);
318 case MenuItem::Submenu: {
319 vector<Gnome::UI::Info> submenu;
320 string label = item.label();
322 path = rootpath + label;
324 if (label.find(item.shortcut()) != string::npos)
325 label.insert(label.find(item.shortcut()), "_");
326 composeUIInfo(item.submenu(), submenu, path + "/");
327 Menus.push_back(Gnome::UI::Menu(label,submenu,label));
331 case MenuItem::Separator: {
333 path = rootpath + "<Separator>";
335 Menus.push_back(Gnome::UI::Separator());
339 case MenuItem::Toc: {
346 case MenuItem::Documents:
347 case MenuItem::Lastfiles:
348 case MenuItem::ViewFormats:
349 case MenuItem::UpdateFormats:
350 case MenuItem::ExportFormats:
351 lyxerr << "Menubar::Pimpl::create_submenu: "
352 "this should not happen" << endl;
358 void Menubar::Pimpl::connectWidgetToAction(GnomeUIInfo * guinfo)
360 for (; guinfo->type != GnomeUIInfoType(GNOME_APP_UI_ENDOFINFO); ++guinfo)
362 if ( ( guinfo->type == GnomeUIInfoType(GNOME_APP_UI_ITEM) ||
363 guinfo->type == GnomeUIInfoType(GNOME_APP_UI_TOGGLEITEM) ) &&
364 guinfo->moreinfo != 0 )
366 (*((void(*)(void *, void *))(guinfo->moreinfo)))(0, guinfo->user_data);
367 wid_act_.push_back( GtkWidgetToAction( guinfo->widget, action_ ) );
369 else if ( guinfo->type == GnomeUIInfoType(GNOME_APP_UI_SUBTREE) ||
370 guinfo->type == GnomeUIInfoType(GNOME_APP_UI_RADIOITEMS) )
372 connectWidgetToAction( (GnomeUIInfo *)(guinfo->moreinfo) );
377 void Menubar::Pimpl::update()
379 vector<GtkWidgetToAction>::const_iterator end=wid_act_.end();
380 for (vector<GtkWidgetToAction>::const_iterator i = wid_act_.begin(); i != end; ++i)
382 GtkWidgetToAction wa = (*i);
383 LyXFunc::func_status flag = owner_->getLyXFunc()->getStatus(wa.action_);
385 if ( flag & (LyXFunc::Disabled | LyXFunc::Unknown) ) gtk_widget_set_sensitive(wa.widget_, false);
386 else gtk_widget_set_sensitive(wa.widget_, true);
388 if ( flag & LyXFunc::ToggleOn )
391 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(wa.widget_), true);
392 ignore_action_=false;
395 if ( flag & LyXFunc::ToggleOff )
398 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(wa.widget_), false);
399 ignore_action_=false;
404 void Menubar::Pimpl::openByName(string const &)