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()
95 #warning Implement me! (be 20010324)
100 vector<Buffer::TocItem> toclist = (owner_->view()->buffer()->getTocList())[Buffer::TOC_TOC];
101 updateList(&toclist, &toc_);
106 int const max_number_of_items = 25;
108 void Menubar::Pimpl::updateList(vector<Buffer::TocItem> * toclist, vector<ListsHolder> * pgui)
110 vector<ListsHolder> & gui = *pgui;
111 int szGui = gui.size();
113 for (i=0; i < szGui; ++i)
115 int oldsz = gui[i].lst.size();
116 vector<Gnome::UI::Info> menu;
119 menu.push_back(Gnome::UI::Item(Gnome::UI::Icon(GNOME_STOCK_MENU_REFRESH),
120 _("Refresh"), SigC::slot(this, &Menubar::Pimpl::updateAllLists)));
122 if (toclist->size() > max_number_of_items)
123 composeTocUIInfo(menu, *toclist, toclist->begin(), 0);
126 vector<Buffer::TocItem>::const_iterator end = toclist->end();
127 for (vector<Buffer::TocItem>::const_iterator it = toclist->begin();
131 label = string(4*(*it).depth,' ')+(*it).str;
133 menu.push_back(Gnome::UI::Item(label,
134 SigC::bind<Buffer::TocItem>(SigC::slot(this, &Menubar::Pimpl::callbackToc), (*it)),
140 mainAppWin->update_menu(gui[i].path, oldsz, gui[i].lst);
144 vector<Buffer::TocItem>::const_iterator
145 Menubar::Pimpl::composeTocUIInfo(vector<Gnome::UI::Info> & menu,
146 vector<Buffer::TocItem> const & toclist,
147 vector<Buffer::TocItem>::const_iterator begin,
150 string label = _("<No Name>");
152 vector<Buffer::TocItem>::const_iterator end = toclist.end();
153 vector<Buffer::TocItem>::const_iterator it;
154 for (it = begin; it != end && (*it).depth >= mylevel; ++it)
156 if ( (*it).depth == mylevel &&
157 (it+1 == end || (*(it+1)).depth <= mylevel) )
160 menu.push_back(Gnome::UI::Item(label,
161 SigC::bind<Buffer::TocItem>(SigC::slot(this, &Menubar::Pimpl::callbackToc), (*it)),
166 vector<Gnome::UI::Info> submenu;
167 if ( (*it).depth == mylevel )
170 submenu.push_back(Gnome::UI::Item(label,
171 SigC::bind<Buffer::TocItem>(SigC::slot(this, &Menubar::Pimpl::callbackToc), (*it)),
175 it = composeTocUIInfo(submenu, toclist, it, mylevel+1);
176 menu.push_back(Gnome::UI::Menu(label,submenu,label));
183 void Menubar::Pimpl::callback(int action)
185 // Dispatch action OR record action to local variable (see connectWidgetToAction)
186 if (!ignore_action_) {
188 owner_->getLyXFunc()->Dispatch(action);
193 void Menubar::Pimpl::callbackToc(Buffer::TocItem tg)
196 if (!owner_->view()->available()) return;
198 owner_->view()->beforeChange();
199 owner_->view()->text->SetCursor( owner_->view(), tg.par, 0 );
200 owner_->view()->text->sel_cursor = owner_->view()->text->cursor;
201 owner_->view()->update(BufferView::SELECT|BufferView::FITCUR);
204 owner_->getLyXFunc()->Dispatch(LFUN_GOTO_PARAGRAPH, tg.str);
207 void Menubar::Pimpl::composeUIInfo(string const & menu_name, vector<Gnome::UI::Info> & Menus, string rootpath)
209 string path = rootpath;
211 if (!menubackend_->hasMenu(menu_name))
213 cout << "ERROR:composeUIInfo: Unknown menu `" << menu_name
219 menubackend_->getMenu(menu_name).expand(menu, owner_->buffer());
221 for (Menu::const_iterator i = menu.begin(); i != menu.end(); ++i)
223 MenuItem item = (*i);
224 switch(item.kind()) {
226 case MenuItem::Command: {
227 string label = item.label();
229 path = rootpath + label;
231 if (label.find(item.shortcut()) != string::npos)
232 label.insert(label.find(item.shortcut()), "_");
234 LyXFunc::func_status flag = owner_->getLyXFunc()->getStatus(item.action());
236 Gnome::UI::Info gitem;
237 SigC::Slot0<void> cback = SigC::bind<int>(SigC::slot(this, &Menubar::Pimpl::callback),item.action());
240 using namespace Gnome::MenuItems;
241 int ac = item.action();
244 if (lyxaction.isPseudoAction(ac))
245 action = lyxaction.retrieveActionArg(ac, argument);
247 action = static_cast<kb_action>(ac);
256 case LFUN_CLOSEBUFFER:
257 gitem = Close(cback);
263 gitem = SaveAs(cback);
265 case LFUN_BUFFER_PRINT:
266 gitem = Print(cback);
275 gitem = Paste(cback);
278 gitem = Gnome::MenuItems::Undo(cback); // confused with class Undo
283 case LFUN_DIALOG_PREFERENCES:
284 gitem = Preferences(cback);
287 gitem = Gnome::UI::Item(Gnome::UI::Icon(GNOME_STOCK_MENU_NEW),
288 label, cback, lyxaction.helpText(item.action()));
290 case LFUN_MENUNEWTMPLT:
291 gitem = Gnome::UI::Item(Gnome::UI::Icon(GNOME_STOCK_MENU_NEW),
292 label, cback, lyxaction.helpText(item.action()));
294 case LFUN_MENUSEARCH:
295 gitem = Gnome::UI::Item(Gnome::UI::Icon(GNOME_STOCK_MENU_SRCHRPL),
296 label, cback, lyxaction.helpText(item.action()));
298 case LFUN_SPELLCHECK:
299 gitem = Gnome::UI::Item(Gnome::UI::Icon(GNOME_STOCK_MENU_SPELLCHECK),
300 label, cback, lyxaction.helpText(item.action()));
303 gitem = Gnome::UI::Item(label, cback, lyxaction.helpText(item.action()));
308 // first handle optional entries.
309 if (item.optional() && (flag & LyXFunc::Disabled)) {
311 << "Skipping optional item " << item.label() << endl;
314 if ((flag & LyXFunc::ToggleOn) || (flag & LyXFunc::ToggleOff))
315 gitem = Gnome::UI::ToggleItem(label, cback, lyxaction.helpText(item.action()));
317 Menus.push_back(gitem);
321 case MenuItem::Submenu: {
322 vector<Gnome::UI::Info> submenu;
323 string label = item.label();
325 path = rootpath + label;
327 if (label.find(item.shortcut()) != string::npos)
328 label.insert(label.find(item.shortcut()), "_");
329 composeUIInfo(item.submenu(), submenu, path + "/");
330 Menus.push_back(Gnome::UI::Menu(label,submenu,label));
334 case MenuItem::Separator: {
336 path = rootpath + "<Separator>";
338 Menus.push_back(Gnome::UI::Separator());
342 case MenuItem::Toc: {
349 case MenuItem::Documents:
350 case MenuItem::Lastfiles:
351 case MenuItem::ViewFormats:
352 case MenuItem::UpdateFormats:
353 case MenuItem::ExportFormats:
354 lyxerr << "Menubar::Pimpl::create_submenu: "
355 "this should not happen" << endl;
361 void Menubar::Pimpl::connectWidgetToAction(GnomeUIInfo * guinfo)
363 for (; guinfo->type != GnomeUIInfoType(GNOME_APP_UI_ENDOFINFO); ++guinfo)
365 if ( ( guinfo->type == GnomeUIInfoType(GNOME_APP_UI_ITEM) ||
366 guinfo->type == GnomeUIInfoType(GNOME_APP_UI_TOGGLEITEM) ) &&
367 guinfo->moreinfo != 0 )
369 (*((void(*)(void *, void *))(guinfo->moreinfo)))(0, guinfo->user_data);
370 wid_act_.push_back( GtkWidgetToAction( guinfo->widget, action_ ) );
372 else if ( guinfo->type == GnomeUIInfoType(GNOME_APP_UI_SUBTREE) ||
373 guinfo->type == GnomeUIInfoType(GNOME_APP_UI_RADIOITEMS) )
375 connectWidgetToAction( (GnomeUIInfo *)(guinfo->moreinfo) );
380 void Menubar::Pimpl::update()
382 vector<GtkWidgetToAction>::const_iterator end=wid_act_.end();
383 for (vector<GtkWidgetToAction>::const_iterator i = wid_act_.begin(); i != end; ++i)
385 GtkWidgetToAction wa = (*i);
386 LyXFunc::func_status flag = owner_->getLyXFunc()->getStatus(wa.action_);
388 if ( flag & (LyXFunc::Disabled | LyXFunc::Unknown) ) gtk_widget_set_sensitive(wa.widget_, false);
389 else gtk_widget_set_sensitive(wa.widget_, true);
391 if ( flag & LyXFunc::ToggleOn )
394 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(wa.widget_), true);
395 ignore_action_=false;
398 if ( flag & LyXFunc::ToggleOff )
401 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(wa.widget_), false);
402 ignore_action_=false;
407 void Menubar::Pimpl::openByName(string const &)