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>
44 // temporary solution for LyXView
45 extern GLyxAppWin * mainAppWin;
48 extern kb_keymap * toplevel_keymap;
49 extern LyXAction lyxaction;
50 extern BufferList bufferlist;
51 extern LastFiles * lastfiles;
55 Menubar::Pimpl::Pimpl(LyXView * view, MenuBackend const & mb)
56 : owner_(view), menubackend_(&mb), ignore_action_(false)
61 Menubar::Pimpl::~Pimpl()
63 if (utoc_.connected()) utoc_.disconnect();
66 void Menubar::Pimpl::set(string const & menu_name)
68 // if (current_menu_name_ != menu_name) // disabled until Lastfiles and Documents are added dynamically to menu
70 current_menu_name_ = menu_name;
74 if (utoc_.connected()) utoc_.disconnect();
77 vector<Gnome::UI::Info> menus;
78 composeUIInfo(current_menu_name_, menus, "");
82 mainAppWin->set_menu(Menu_);
84 // connect all menu items to correspoding action
86 ignore_action_ = true;
87 connectWidgetToAction(Menu_.gtkobj());
88 ignore_action_ = false;
90 // update state of the items
96 void Menubar::Pimpl::updateAllLists()
101 vector<Buffer::TocItem> toclist = (owner_->view()->buffer()->getTocList())[Buffer::TOC_TOC];
102 updateList(&toclist, &toc_);
106 int const max_number_of_items = 25;
107 void Menubar::Pimpl::updateList(vector<Buffer::TocItem> * toclist, vector<ListsHolder> * pgui)
109 vector<ListsHolder> & gui = *pgui;
110 int szGui = gui.size();
112 for (i=0; i < szGui; ++i)
114 int oldsz = gui[i].lst.size();
115 vector<Gnome::UI::Info> menu;
118 menu.push_back(Gnome::UI::Item(Gnome::UI::Icon(GNOME_STOCK_MENU_REFRESH),
119 _("Refresh"), slot(this, &Menubar::Pimpl::updateAllLists)));
121 if (toclist->size() > max_number_of_items)
122 composeTocUIInfo(menu, *toclist, toclist->begin(), 0);
125 vector<Buffer::TocItem>::const_iterator end = toclist->end();
126 for (vector<Buffer::TocItem>::const_iterator it = toclist->begin();
130 label = string(4*(*it).depth,' ')+(*it).str;
132 menu.push_back(Gnome::UI::Item(label,
133 bind<Buffer::TocItem>(slot(this, &Menubar::Pimpl::callbackToc), (*it)),
139 mainAppWin->update_menu(gui[i].path, oldsz, gui[i].lst);
143 vector<Buffer::TocItem>::const_iterator
144 Menubar::Pimpl::composeTocUIInfo(vector<Gnome::UI::Info> & menu,
145 vector<Buffer::TocItem> const & toclist,
146 vector<Buffer::TocItem>::const_iterator begin,
149 string label = _("<No Name>");
151 vector<Buffer::TocItem>::const_iterator end = toclist.end();
152 vector<Buffer::TocItem>::const_iterator it;
153 for (it = begin; it != end && (*it).depth >= mylevel; ++it)
155 if ( (*it).depth == mylevel &&
156 (it+1 == end || (*(it+1)).depth <= mylevel) )
159 menu.push_back(Gnome::UI::Item(label,
160 bind<Buffer::TocItem>(slot(this, &Menubar::Pimpl::callbackToc), (*it)),
165 vector<Gnome::UI::Info> submenu;
166 if ( (*it).depth == mylevel )
169 submenu.push_back(Gnome::UI::Item(label,
170 bind<Buffer::TocItem>(slot(this, &Menubar::Pimpl::callbackToc), (*it)),
174 it = composeTocUIInfo(submenu, toclist, it, mylevel+1);
175 menu.push_back(Gnome::UI::Menu(label,submenu,label));
182 void Menubar::Pimpl::callback(int action)
184 // Dispatch action OR record action to local variable (see connectWidgetToAction)
185 if (!ignore_action_) {
187 owner_->getLyXFunc()->Dispatch(action);
192 void Menubar::Pimpl::callbackToc(Buffer::TocItem tg)
194 if (!owner_->view()->available()) return;
196 owner_->view()->beforeChange();
197 owner_->view()->text->SetCursor( owner_->view(), tg.par, 0 );
198 owner_->view()->text->sel_cursor = owner_->view()->text->cursor;
199 owner_->view()->update(BufferView::SELECT|BufferView::FITCUR);
202 void Menubar::Pimpl::composeUIInfo(string const & menu_name, vector<Gnome::UI::Info> & Menus, string rootpath)
204 string path = rootpath;
206 if (!menubackend_->hasMenu(menu_name))
208 cout << "ERROR:composeUIInfo: Unknown menu `" << menu_name
214 menubackend_->getMenu(menu_name).expand(menu, owner_->buffer());
216 for (Menu::const_iterator i = menu.begin(); i != menu.end(); ++i)
218 MenuItem item = (*i);
219 switch(item.kind()) {
221 case MenuItem::Command: {
222 string label = item.label();
224 path = rootpath + label;
226 if (label.find(item.shortcut()) != string::npos)
227 label.insert(label.find(item.shortcut()), "_");
229 LyXFunc::func_status flag = owner_->getLyXFunc()->getStatus(item.action());
231 Gnome::UI::Info gitem;
232 SigC::Slot0<void> cback = bind<int>(slot(this, &Menubar::Pimpl::callback),item.action());
235 using namespace Gnome::MenuItems;
236 int ac = item.action();
239 if (lyxaction.isPseudoAction(ac))
240 action = lyxaction.retrieveActionArg(ac, argument);
242 action = static_cast<kb_action>(ac);
251 case LFUN_CLOSEBUFFER:
252 gitem = Close(cback);
257 case LFUN_MENUWRITEAS:
258 gitem = SaveAs(cback);
260 case LFUN_BUFFER_PRINT:
261 gitem = Print(cback);
270 gitem = Paste(cback);
273 gitem = Gnome::MenuItems::Undo(cback); // confused with class Undo
278 case LFUN_DIALOG_PREFERENCES:
279 gitem = Preferences(cback);
282 gitem = Gnome::UI::Item(Gnome::UI::Icon(GNOME_STOCK_MENU_NEW),
283 label, cback, lyxaction.helpText(item.action()));
285 case LFUN_MENUNEWTMPLT:
286 gitem = Gnome::UI::Item(Gnome::UI::Icon(GNOME_STOCK_MENU_NEW),
287 label, cback, lyxaction.helpText(item.action()));
289 case LFUN_MENUSEARCH:
290 gitem = Gnome::UI::Item(Gnome::UI::Icon(GNOME_STOCK_MENU_SRCHRPL),
291 label, cback, lyxaction.helpText(item.action()));
293 case LFUN_SPELLCHECK:
294 gitem = Gnome::UI::Item(Gnome::UI::Icon(GNOME_STOCK_MENU_SPELLCHECK),
295 label, cback, lyxaction.helpText(item.action()));
298 gitem = Gnome::UI::Item(label, cback, lyxaction.helpText(item.action()));
303 // first handle optional entries.
304 if (item.optional() && (flag & LyXFunc::Disabled)) {
306 << "Skipping optional item " << item.label() << endl;
309 if ((flag & LyXFunc::ToggleOn) || (flag & LyXFunc::ToggleOff))
310 gitem = Gnome::UI::ToggleItem(label, cback, lyxaction.helpText(item.action()));
312 Menus.push_back(gitem);
316 case MenuItem::Submenu: {
317 vector<Gnome::UI::Info> submenu;
318 string label = item.label();
320 path = rootpath + label;
322 if (label.find(item.shortcut()) != string::npos)
323 label.insert(label.find(item.shortcut()), "_");
324 composeUIInfo(item.submenu(), submenu, path + "/");
325 Menus.push_back(Gnome::UI::Menu(label,submenu,label));
329 case MenuItem::Separator: {
331 path = rootpath + "<Separator>";
333 Menus.push_back(Gnome::UI::Separator());
337 case MenuItem::Toc: {
344 case MenuItem::Documents:
345 case MenuItem::Lastfiles:
346 case MenuItem::ViewFormats:
347 case MenuItem::UpdateFormats:
348 case MenuItem::ExportFormats:
349 lyxerr << "Menubar::Pimpl::create_submenu: "
350 "this should not happen" << endl;
356 void Menubar::Pimpl::connectWidgetToAction(GnomeUIInfo * guinfo)
358 for (; guinfo->type != GnomeUIInfoType(GNOME_APP_UI_ENDOFINFO); ++guinfo)
360 if ( ( guinfo->type == GnomeUIInfoType(GNOME_APP_UI_ITEM) ||
361 guinfo->type == GnomeUIInfoType(GNOME_APP_UI_TOGGLEITEM) ) &&
362 guinfo->moreinfo != NULL )
364 (*((void(*)(void *, void *))(guinfo->moreinfo)))(NULL, guinfo->user_data);
365 wid_act_.push_back( GtkWidgetToAction( guinfo->widget, action_ ) );
367 else if ( guinfo->type == GnomeUIInfoType(GNOME_APP_UI_SUBTREE) ||
368 guinfo->type == GnomeUIInfoType(GNOME_APP_UI_RADIOITEMS) )
370 connectWidgetToAction( (GnomeUIInfo *)(guinfo->moreinfo) );
375 void Menubar::Pimpl::update()
377 vector<GtkWidgetToAction>::const_iterator end=wid_act_.end();
378 for (vector<GtkWidgetToAction>::const_iterator i = wid_act_.begin(); i != end; ++i)
380 GtkWidgetToAction wa = (*i);
381 LyXFunc::func_status flag = owner_->getLyXFunc()->getStatus(wa.action_);
383 if ( flag & (LyXFunc::Disabled | LyXFunc::Unknown) ) gtk_widget_set_sensitive(wa.widget_, false);
384 else gtk_widget_set_sensitive(wa.widget_, true);
386 if ( flag & LyXFunc::ToggleOn )
389 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(wa.widget_), true);
390 ignore_action_=false;
393 if ( flag & LyXFunc::ToggleOff )
396 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(wa.widget_), false);
397 ignore_action_=false;
402 void Menubar::Pimpl::openByName(string const &)