1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 2000 The LyX Team.
8 * ====================================================== */
11 #pragma implementation
18 #include "support/lstrings.h"
19 #include "support/filetools.h"
20 #include "support/StrPool.h"
21 #include "support/LAssert.h"
23 #include "LyXAction.h"
26 #include "bufferlist.h"
27 #include "lastfiles.h"
29 #include "MenuBackend.h"
30 #include "Menubar_pimpl.h"
36 #include <gtk--/menu.h>
40 // temporary solution for LyXView
41 extern GLyxAppWin * mainAppWin;
44 extern boost::scoped_ptr<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)
57 Menubar::Pimpl::~Pimpl()
59 if (utoc_.connected()) utoc_.disconnect();
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;
70 if (utoc_.connected()) utoc_.disconnect();
73 vector<Gnome::UI::Info> menus;
74 composeUIInfo(current_menu_name_, menus, "");
78 mainAppWin->set_menu(Menu_);
80 // connect all menu items to correspoding action
82 ignore_action_ = true;
83 connectWidgetToAction(Menu_.gtkobj());
84 ignore_action_ = false;
86 // update state of the items
92 void Menubar::Pimpl::updateAllLists()
95 #warning Implement me! (be 20010324)
101 vector<Buffer::TocItem> toclist = (owner_->view()->buffer()->getTocList())[Buffer::TOC_TOC];
102 updateList(&toclist, &toc_);
107 int const max_number_of_items = 25;
109 void Menubar::Pimpl::updateList(vector<Buffer::TocItem> * toclist, vector<ListsHolder> * pgui)
111 vector<ListsHolder> & gui = *pgui;
112 int szGui = gui.size();
114 for (i=0; i < szGui; ++i)
116 int oldsz = gui[i].lst.size();
117 vector<Gnome::UI::Info> menu;
120 menu.push_back(Gnome::UI::Item(Gnome::UI::Icon(GNOME_STOCK_MENU_REFRESH),
121 _("Refresh"), SigC::slot(this, &Menubar::Pimpl::updateAllLists)));
123 if (toclist->size() > max_number_of_items)
124 composeTocUIInfo(menu, *toclist, toclist->begin(), 0);
127 vector<Buffer::TocItem>::const_iterator end = toclist->end();
128 for (vector<Buffer::TocItem>::const_iterator it = toclist->begin();
132 label = string(4*(*it).depth,' ')+(*it).str;
134 menu.push_back(Gnome::UI::Item(label,
135 SigC::bind<Buffer::TocItem>(SigC::slot(this, &Menubar::Pimpl::callbackToc), (*it)),
141 mainAppWin->update_menu(gui[i].path, oldsz, gui[i].lst);
145 vector<Buffer::TocItem>::const_iterator
146 Menubar::Pimpl::composeTocUIInfo(vector<Gnome::UI::Info> & menu,
147 vector<Buffer::TocItem> const & toclist,
148 vector<Buffer::TocItem>::const_iterator begin,
151 string label = _("<No Name>");
153 vector<Buffer::TocItem>::const_iterator end = toclist.end();
154 vector<Buffer::TocItem>::const_iterator it;
155 for (it = begin; it != end && (*it).depth >= mylevel; ++it)
157 if ( (*it).depth == mylevel &&
158 (it+1 == end || (*(it+1)).depth <= mylevel) )
161 menu.push_back(Gnome::UI::Item(label,
162 SigC::bind<Buffer::TocItem>(SigC::slot(this, &Menubar::Pimpl::callbackToc), (*it)),
167 vector<Gnome::UI::Info> submenu;
168 if ( (*it).depth == mylevel )
171 submenu.push_back(Gnome::UI::Item(label,
172 SigC::bind<Buffer::TocItem>(SigC::slot(this, &Menubar::Pimpl::callbackToc), (*it)),
176 it = composeTocUIInfo(submenu, toclist, it, mylevel+1);
177 menu.push_back(Gnome::UI::Menu(label,submenu,label));
184 void Menubar::Pimpl::callback(int action)
186 // Dispatch action OR record action to local variable (see connectWidgetToAction)
187 if (!ignore_action_) {
189 owner_->getLyXFunc()->Dispatch(action);
194 void Menubar::Pimpl::callbackToc(Buffer::TocItem tg)
197 if (!owner_->view()->available()) return;
199 owner_->view()->beforeChange();
200 owner_->view()->text->SetCursor( owner_->view(), tg.par, 0 );
201 owner_->view()->text->sel_cursor = owner_->view()->text->cursor;
202 owner_->view()->update(BufferView::SELECT|BufferView::FITCUR);
205 owner_->getLyXFunc()->Dispatch(LFUN_GOTO_PARAGRAPH, tg.str);
208 void Menubar::Pimpl::composeUIInfo(string const & menu_name, vector<Gnome::UI::Info> & Menus, string rootpath)
210 string path = rootpath;
212 if (!menubackend_->hasMenu(menu_name))
214 cout << "ERROR:composeUIInfo: Unknown menu `" << menu_name
220 menubackend_->getMenu(menu_name).expand(menu, owner_->buffer());
222 for (Menu::const_iterator i = menu.begin(); i != menu.end(); ++i)
224 MenuItem item = (*i);
225 switch(item.kind()) {
227 case MenuItem::Command: {
228 string label = item.label();
230 path = rootpath + label;
232 if (label.find(item.shortcut()) != string::npos)
233 label.insert(label.find(item.shortcut()), "_");
235 LyXFunc::func_status flag = owner_->getLyXFunc()->getStatus(item.action());
237 Gnome::UI::Info gitem;
238 SigC::Slot0<void> cback = SigC::bind<int>(SigC::slot(this, &Menubar::Pimpl::callback),item.action());
241 using namespace Gnome::MenuItems;
242 int ac = item.action();
245 if (lyxaction.isPseudoAction(ac))
246 action = lyxaction.retrieveActionArg(ac, argument);
248 action = static_cast<kb_action>(ac);
257 case LFUN_CLOSEBUFFER:
258 gitem = Close(cback);
264 gitem = SaveAs(cback);
266 case LFUN_BUFFER_PRINT:
267 gitem = Print(cback);
276 gitem = Paste(cback);
279 gitem = Gnome::MenuItems::Undo(cback); // confused with class Undo
284 case LFUN_DIALOG_PREFERENCES:
285 gitem = Preferences(cback);
288 gitem = Gnome::UI::Item(Gnome::UI::Icon(GNOME_STOCK_MENU_NEW),
289 label, cback, lyxaction.helpText(item.action()));
291 case LFUN_MENUNEWTMPLT:
292 gitem = Gnome::UI::Item(Gnome::UI::Icon(GNOME_STOCK_MENU_NEW),
293 label, cback, lyxaction.helpText(item.action()));
295 case LFUN_MENUSEARCH:
296 gitem = Gnome::UI::Item(Gnome::UI::Icon(GNOME_STOCK_MENU_SRCHRPL),
297 label, cback, lyxaction.helpText(item.action()));
299 case LFUN_SPELLCHECK:
300 gitem = Gnome::UI::Item(Gnome::UI::Icon(GNOME_STOCK_MENU_SPELLCHECK),
301 label, cback, lyxaction.helpText(item.action()));
304 gitem = Gnome::UI::Item(label, cback, lyxaction.helpText(item.action()));
309 // first handle optional entries.
310 if (item.optional() && (flag & LyXFunc::Disabled)) {
312 << "Skipping optional item " << item.label() << endl;
315 if ((flag & LyXFunc::ToggleOn) || (flag & LyXFunc::ToggleOff))
316 gitem = Gnome::UI::ToggleItem(label, cback, lyxaction.helpText(item.action()));
318 Menus.push_back(gitem);
322 case MenuItem::Submenu: {
323 vector<Gnome::UI::Info> submenu;
324 string label = item.label();
326 path = rootpath + label;
328 if (label.find(item.shortcut()) != string::npos)
329 label.insert(label.find(item.shortcut()), "_");
330 composeUIInfo(item.submenu(), submenu, path + "/");
331 Menus.push_back(Gnome::UI::Menu(label,submenu,label));
335 case MenuItem::Separator: {
337 path = rootpath + "<Separator>";
339 Menus.push_back(Gnome::UI::Separator());
343 case MenuItem::Toc: {
350 case MenuItem::Documents:
351 case MenuItem::Lastfiles:
352 case MenuItem::ViewFormats:
353 case MenuItem::UpdateFormats:
354 case MenuItem::ExportFormats:
355 lyxerr << "Menubar::Pimpl::create_submenu: "
356 "this should not happen" << endl;
362 void Menubar::Pimpl::connectWidgetToAction(GnomeUIInfo * guinfo)
364 for (; guinfo->type != GnomeUIInfoType(GNOME_APP_UI_ENDOFINFO); ++guinfo)
366 if ( ( guinfo->type == GnomeUIInfoType(GNOME_APP_UI_ITEM) ||
367 guinfo->type == GnomeUIInfoType(GNOME_APP_UI_TOGGLEITEM) ) &&
368 guinfo->moreinfo != 0 )
370 (*((void(*)(void *, void *))(guinfo->moreinfo)))(0, guinfo->user_data);
371 wid_act_.push_back( GtkWidgetToAction( guinfo->widget, action_ ) );
373 else if ( guinfo->type == GnomeUIInfoType(GNOME_APP_UI_SUBTREE) ||
374 guinfo->type == GnomeUIInfoType(GNOME_APP_UI_RADIOITEMS) )
376 connectWidgetToAction( (GnomeUIInfo *)(guinfo->moreinfo) );
381 void Menubar::Pimpl::update()
383 vector<GtkWidgetToAction>::const_iterator end=wid_act_.end();
384 for (vector<GtkWidgetToAction>::const_iterator i = wid_act_.begin(); i != end; ++i)
386 GtkWidgetToAction wa = (*i);
387 LyXFunc::func_status flag = owner_->getLyXFunc()->getStatus(wa.action_);
389 if ( flag & (LyXFunc::Disabled | LyXFunc::Unknown) ) gtk_widget_set_sensitive(wa.widget_, false);
390 else gtk_widget_set_sensitive(wa.widget_, true);
392 if ( flag & LyXFunc::ToggleOn )
395 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(wa.widget_), true);
396 ignore_action_=false;
399 if ( flag & LyXFunc::ToggleOff )
402 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(wa.widget_), false);
403 ignore_action_=false;
408 void Menubar::Pimpl::openByName(string const &)