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"
25 #include "FuncStatus.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()
96 #warning Implement me! (be 20010324)
102 vector<Buffer::TocItem> toclist = (owner_->view()->buffer()->getTocList())[Buffer::TOC_TOC];
103 updateList(&toclist, &toc_);
108 int const max_number_of_items = 25;
110 void Menubar::Pimpl::updateList(vector<Buffer::TocItem> * toclist, vector<ListsHolder> * pgui)
112 vector<ListsHolder> & gui = *pgui;
113 int szGui = gui.size();
115 for (i=0; i < szGui; ++i)
117 int oldsz = gui[i].lst.size();
118 vector<Gnome::UI::Info> menu;
121 menu.push_back(Gnome::UI::Item(Gnome::UI::Icon(GNOME_STOCK_MENU_REFRESH),
122 _("Refresh"), SigC::slot(this, &Menubar::Pimpl::updateAllLists)));
124 if (toclist->size() > max_number_of_items)
125 composeTocUIInfo(menu, *toclist, toclist->begin(), 0);
128 vector<Buffer::TocItem>::const_iterator end = toclist->end();
129 for (vector<Buffer::TocItem>::const_iterator it = toclist->begin();
133 label = string(4*(*it).depth,' ')+(*it).str;
135 menu.push_back(Gnome::UI::Item(label,
136 SigC::bind<Buffer::TocItem>(SigC::slot(this, &Menubar::Pimpl::callbackToc), (*it)),
142 mainAppWin->update_menu(gui[i].path, oldsz, gui[i].lst);
146 vector<Buffer::TocItem>::const_iterator
147 Menubar::Pimpl::composeTocUIInfo(vector<Gnome::UI::Info> & menu,
148 vector<Buffer::TocItem> const & toclist,
149 vector<Buffer::TocItem>::const_iterator begin,
152 string label = _("<No Name>");
154 vector<Buffer::TocItem>::const_iterator end = toclist.end();
155 vector<Buffer::TocItem>::const_iterator it;
156 for (it = begin; it != end && (*it).depth >= mylevel; ++it)
158 if ( (*it).depth == mylevel &&
159 (it+1 == end || (*(it+1)).depth <= mylevel) )
162 menu.push_back(Gnome::UI::Item(label,
163 SigC::bind<Buffer::TocItem>(SigC::slot(this, &Menubar::Pimpl::callbackToc), (*it)),
168 vector<Gnome::UI::Info> submenu;
169 if ( (*it).depth == mylevel )
172 submenu.push_back(Gnome::UI::Item(label,
173 SigC::bind<Buffer::TocItem>(SigC::slot(this, &Menubar::Pimpl::callbackToc), (*it)),
177 it = composeTocUIInfo(submenu, toclist, it, mylevel+1);
178 menu.push_back(Gnome::UI::Menu(label,submenu,label));
185 void Menubar::Pimpl::callback(int action)
187 // Dispatch action OR record action to local variable (see connectWidgetToAction)
188 if (!ignore_action_) {
190 owner_->getLyXFunc()->dispatch(action);
195 void Menubar::Pimpl::callbackToc(Buffer::TocItem tg)
198 if (!owner_->view()->available()) return;
200 owner_->view()->beforeChange();
201 owner_->view()->text->SetCursor( owner_->view(), tg.par, 0 );
202 owner_->view()->text->sel_cursor = owner_->view()->text->cursor;
203 owner_->view()->update(BufferView::SELECT|BufferView::FITCUR);
206 owner_->getLyXFunc()->dispatch(LFUN_GOTO_PARAGRAPH, tg.str);
209 void Menubar::Pimpl::composeUIInfo(string const & menu_name, vector<Gnome::UI::Info> & Menus, string rootpath)
211 string path = rootpath;
213 if (!menubackend_->hasMenu(menu_name))
215 cout << "ERROR:composeUIInfo: Unknown menu `" << menu_name
221 menubackend_->getMenu(menu_name).expand(menu, owner_->buffer());
223 for (Menu::const_iterator i = menu.begin(); i != menu.end(); ++i)
225 MenuItem item = (*i);
226 switch(item.kind()) {
228 case MenuItem::Command: {
229 string label = item.label();
231 path = rootpath + label;
233 if (label.find(item.shortcut()) != string::npos)
234 label.insert(label.find(item.shortcut()), "_");
236 FuncStatus flag = owner_->getLyXFunc()->getStatus(item.action());
238 Gnome::UI::Info gitem;
239 SigC::Slot0<void> cback = SigC::bind<int>(SigC::slot(this, &Menubar::Pimpl::callback),item.action());
242 using namespace Gnome::MenuItems;
243 int ac = item.action();
246 if (lyxaction.isPseudoAction(ac))
247 action = lyxaction.retrieveActionArg(ac, argument);
249 action = static_cast<kb_action>(ac);
258 case LFUN_CLOSEBUFFER:
259 gitem = Close(cback);
265 gitem = SaveAs(cback);
267 case LFUN_BUFFER_PRINT:
268 gitem = Print(cback);
277 gitem = Paste(cback);
280 gitem = Gnome::MenuItems::Undo(cback); // confused with class Undo
285 case LFUN_DIALOG_PREFERENCES:
286 gitem = Preferences(cback);
289 gitem = Gnome::UI::Item(Gnome::UI::Icon(GNOME_STOCK_MENU_NEW),
290 label, cback, lyxaction.helpText(item.action()));
292 case LFUN_MENUNEWTMPLT:
293 gitem = Gnome::UI::Item(Gnome::UI::Icon(GNOME_STOCK_MENU_NEW),
294 label, cback, lyxaction.helpText(item.action()));
296 case LFUN_MENUSEARCH:
297 gitem = Gnome::UI::Item(Gnome::UI::Icon(GNOME_STOCK_MENU_SRCHRPL),
298 label, cback, lyxaction.helpText(item.action()));
300 case LFUN_SPELLCHECK:
301 gitem = Gnome::UI::Item(Gnome::UI::Icon(GNOME_STOCK_MENU_SPELLCHECK),
302 label, cback, lyxaction.helpText(item.action()));
305 gitem = Gnome::UI::Item(label, cback, lyxaction.helpText(item.action()));
310 // first handle optional entries.
311 if (item.optional() && (flag.disabled())) {
313 << "Skipping optional item " << item.label() << endl;
316 if ((flag.onoff(true)) ||
318 gitem = Gnome::UI::ToggleItem(label, cback, lyxaction.helpText(item.action()));
320 Menus.push_back(gitem);
324 case MenuItem::Submenu: {
325 vector<Gnome::UI::Info> submenu;
326 string label = item.label();
328 path = rootpath + label;
330 if (label.find(item.shortcut()) != string::npos)
331 label.insert(label.find(item.shortcut()), "_");
332 composeUIInfo(item.submenu(), submenu, path + "/");
333 Menus.push_back(Gnome::UI::Menu(label,submenu,label));
337 case MenuItem::Separator: {
339 path = rootpath + "<Separator>";
341 Menus.push_back(Gnome::UI::Separator());
345 case MenuItem::Toc: {
352 case MenuItem::Documents:
353 case MenuItem::Lastfiles:
354 case MenuItem::ViewFormats:
355 case MenuItem::UpdateFormats:
356 case MenuItem::ExportFormats:
357 lyxerr << "Menubar::Pimpl::create_submenu: "
358 "this should not happen" << endl;
364 void Menubar::Pimpl::connectWidgetToAction(GnomeUIInfo * guinfo)
366 for (; guinfo->type != GnomeUIInfoType(GNOME_APP_UI_ENDOFINFO); ++guinfo)
368 if ( ( guinfo->type == GnomeUIInfoType(GNOME_APP_UI_ITEM) ||
369 guinfo->type == GnomeUIInfoType(GNOME_APP_UI_TOGGLEITEM) ) &&
370 guinfo->moreinfo != 0 )
372 (*((void(*)(void *, void *))(guinfo->moreinfo)))(0, guinfo->user_data);
373 wid_act_.push_back( GtkWidgetToAction( guinfo->widget, action_ ) );
375 else if ( guinfo->type == GnomeUIInfoType(GNOME_APP_UI_SUBTREE) ||
376 guinfo->type == GnomeUIInfoType(GNOME_APP_UI_RADIOITEMS) )
378 connectWidgetToAction( (GnomeUIInfo *)(guinfo->moreinfo) );
383 void Menubar::Pimpl::update()
385 vector<GtkWidgetToAction>::const_iterator end=wid_act_.end();
386 for (vector<GtkWidgetToAction>::const_iterator i = wid_act_.begin(); i != end; ++i)
388 GtkWidgetToAction wa = (*i);
389 FuncStatus flag = owner_->getLyXFunc()->getStatus(wa.action_);
391 if ( flag.disabled() || flag.unknown()) gtk_widget_set_sensitive(wa.widget_, false);
392 else gtk_widget_set_sensitive(wa.widget_, true);
394 if ( flag.onoff(true))
397 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(wa.widget_), true);
398 ignore_action_=false;
401 if ( flag.onoff(false))
404 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(wa.widget_), false);
405 ignore_action_=false;
410 void Menubar::Pimpl::openByName(string const &)