]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/xforms/XFormsMenubar.C
Change glob() API to accept a dir parameter.
[lyx.git] / src / frontends / xforms / XFormsMenubar.C
index 424b8553195525cae96d8d707d4c99be5f27f47f..31aaac9475bef07ca9aed992bc8029d9fccbe71b 100644 (file)
 #include "lyxfunc.h"
 #include "MenuBackend.h"
 
-#include "support/LAssert.h"
 #include "support/lstrings.h"
 #include "support/tostr.h"
 
+#include <boost/bind.hpp>
+
 #include "lyx_forms.h"
 
-using namespace lyx::support;
+using boost::shared_ptr;
 
+using std::distance;
 using std::endl;
 using std::for_each;
-using std::max;
-using std::min;
+using std::string;
 using std::vector;
 
+namespace lyx {
+
+using support::lowercase;
+using support::subst;
+
+namespace frontend {
 
 typedef vector<int>::size_type size_type;
 
@@ -82,8 +89,12 @@ extern "C" {
 
 
 XFormsMenubar::XFormsMenubar(LyXView * view, MenuBackend const & mb)
-       : owner_(static_cast<XFormsView*>(view)), menubackend_(&mb)
+       : owner_(static_cast<XFormsView*>(view)),
+         menubackend_(&mb)
 {
+       owner_->metricsUpdated.connect(boost::bind(&WidgetMap::updateMetrics,
+                                                  &widgets_));
+
        makeMenubar(menubackend_->getMenubar());
 }
 
@@ -94,16 +105,27 @@ XFormsMenubar::~XFormsMenubar()
 
 void XFormsMenubar::makeMenubar(Menu const & menu)
 {
-       FL_FORM * form = owner_->getForm();
-       int moffset = 0;
+       // Draw a frame around the whole.
+       BoxList & boxlist = owner_->getBox(XFormsView::Top)->children();
 
-       // Create menu frame if there is non yet.
-       FL_OBJECT * frame = fl_add_frame(FL_UP_FRAME, 0, 0,
-                                        form->w, mheight, "");
+       FL_OBJECT * frame = fl_add_frame(FL_UP_FRAME, 0, 0, 0, 0, "");
        fl_set_object_resize(frame, FL_RESIZE_ALL);
-       fl_set_object_gravity(frame, NorthWestGravity,
-                             NorthEastGravity);
+       fl_set_object_gravity(frame, NorthWestGravity, NorthEastGravity);
+
+       menubar_ = widgets_.add(frame, boxlist, 0, mheight);
+
+       // The menubar contains three vertically-aligned Boxes,
+       // the center one of which is to contain the buttons,
+       // aligned horizontally.
+       // The other two provide some visual padding.
+       menubar_->children().push_back(Box(0, yloc));
+       shared_ptr<Box> menubar_center = menubar_->children().push_back(Box(0,0));
+       menubar_center->set(Box::Horizontal);
+       menubar_->children().push_back(Box(0, yloc));
+
+       BoxList & menubar_buttons = menubar_center->children();
 
+       // Add the buttons.
        Menu::const_iterator i = menu.begin();
        Menu::const_iterator end = menu.end();
        for (; i != end; ++i) {
@@ -117,11 +139,12 @@ void XFormsMenubar::makeMenubar(Menu const & menu)
                string const label = i->label();
                string const shortcut = '#' + i->shortcut();
                int const width = string_width(label);
-               obj = fl_add_button(FL_MENU_BUTTON,
-                                   air + moffset, yloc,
-                                   width + mbadd,
-                                   mbheight,
-                                   label.c_str());
+
+               obj = fl_add_button(FL_MENU_BUTTON, 0, 0, 0, 0, label.c_str());
+
+               menubar_buttons.push_back(Box(air, 0));
+               widgets_.add(obj, menubar_buttons, width + mbadd, mbheight);
+
                fl_set_object_boxtype(obj, FL_FLAT_BOX);
                fl_set_object_color(obj, FL_MCOL, FL_MCOL);
                fl_set_object_lsize(obj, MENU_LABEL_SIZE);
@@ -129,7 +152,6 @@ void XFormsMenubar::makeMenubar(Menu const & menu)
                fl_set_object_resize(obj, FL_RESIZE_ALL);
                fl_set_object_gravity(obj, NorthWestGravity,
                                      NorthWestGravity);
-               moffset += obj->w + air;
                fl_set_object_shortcut(obj, shortcut.c_str(), 1);
                fl_set_object_callback(obj, C_XFormsMenubar_MenuCallback, 1);
 
@@ -138,7 +160,6 @@ void XFormsMenubar::makeMenubar(Menu const & menu)
                buttonlist_.push_back(iteminfo);
                obj->u_vdata = iteminfo.get();
        }
-
 }
 
 
@@ -193,9 +214,10 @@ string const fixlabel(string const & str)
 
 
 int XFormsMenubar::create_submenu(Window win, XFormsView * view,
-                                  Menu const & menu, vector<int> & smn)
+                                 Menu const & menu,
+                                 vector<int> & smn, Funcs & funcs)
 {
-       const int menuid = get_new_submenu(smn, win);
+       int const menuid = get_new_submenu(smn, win);
        lyxerr[Debug::GUI] << "XFormsMenubar::create_submenu: creating "
                           << menu.name() << " as menuid=" << menuid << endl;
 
@@ -227,7 +249,7 @@ int XFormsMenubar::create_submenu(Window win, XFormsView * view,
                ++count;
                // add a More... submenu if the menu is too long (but
                // not just for one extra entry!)
-               if (count > max_number_of_items && (i+1) != end) {
+               if (count > max_number_of_items && (i + 1) != end) {
                        int tmpmenuid = get_new_submenu(smn, win);
                        lyxerr[Debug::GUI] << "Too many items, creating "
                                           << "new menu " << tmpmenuid << endl;
@@ -259,7 +281,7 @@ int XFormsMenubar::create_submenu(Window win, XFormsView * view,
                        }
 
                        // Is there a separator after the item?
-                       if ((i+1) != end
+                       if ((i + 1) != end
                            && (i + 1)->kind() == MenuItem::Separator)
                                label += "%l";
 
@@ -268,7 +290,7 @@ int XFormsMenubar::create_submenu(Window win, XFormsView * view,
                                label += "%B";
                        if (flag.onoff(false))
                                label += "%b";
-                       if (flag.disabled())
+                       if (!flag.enabled())
                                label += "%i";
 
                        // Add the shortcut
@@ -283,7 +305,7 @@ int XFormsMenubar::create_submenu(Window win, XFormsView * view,
                                // create the submenu
                                submenuid =
                                        create_submenu(win, view,
-                                                      *item.submenu(), smn);
+                                                      *item.submenu(), smn, funcs);
                                if (submenuid == -1)
                                        return -1;
                                label += "%x" + tostr(smn.size());
@@ -293,10 +315,16 @@ int XFormsMenubar::create_submenu(Window win, XFormsView * view,
                                        << "), ";
                        } else {
                                // Add the action
-                               label += "%x" + tostr(item.action()
-                                                     + action_offset);
+                               Funcs::iterator fit =
+                                       funcs.insert(funcs.end(), item.func());
+                               int const action_count =
+                                       distance(funcs.begin(), fit);
+
+                               label += "%x" + tostr(action_count + action_offset);
                                lyxerr[Debug::GUI] << "Action: \""
-                                                  << item.action() << "\", ";
+                                                  << item.func().action
+                                                  << "(" << item.func().argument
+                                                  << ")\", ";
                        }
 
                        // Add everything to the menu
@@ -345,7 +373,7 @@ void XFormsMenubar::MenuCallback(FL_OBJECT * ob, long button)
        }
 
        // Paranoia check
-       Assert(item->kind() == MenuItem::Submenu);
+       BOOST_ASSERT(item->kind() == MenuItem::Submenu);
 
        // set tabstop length
        fl_set_tabstop(menu_tabstop);
@@ -354,9 +382,10 @@ void XFormsMenubar::MenuCallback(FL_OBJECT * ob, long button)
        Menu tomenu;
        Menu const frommenu = menubackend_->getMenu(item->submenuname());
        menubackend_->expand(frommenu, tomenu, view);
+       Funcs funcs;
        vector<int> submenus;
        int menu = iteminfo->menubar_->create_submenu(FL_ObjWin(ob), view,
-                                                   tomenu, submenus);
+                                                   tomenu, submenus, funcs);
        if (menu != -1) {
                // place popup
                fl_setpup_position(view->getForm()->x + ob->x,
@@ -371,8 +400,8 @@ void XFormsMenubar::MenuCallback(FL_OBJECT * ob, long button)
 
                // If the action value is too low, then it is not a
                // valid action, but something else.
-               if (choice >= action_offset + 1) {
-                       view->getLyXFunc().dispatch(choice - action_offset, true);
+               if (choice >= action_offset) {
+                       view->getLyXFunc().dispatch(funcs[choice - action_offset]);
                } else {
                        lyxerr[Debug::GUI]
                                << "MenuCallback: ignoring bogus action "
@@ -399,3 +428,6 @@ XFormsMenubar::ItemInfo::ItemInfo
 
 XFormsMenubar::ItemInfo::~ItemInfo()
 {}
+
+} // namespace frontend
+} // namespace lyx