]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/xforms/Toolbar_pimpl.C
layout as string
[lyx.git] / src / frontends / xforms / Toolbar_pimpl.C
index 03da2f7fa580e3f8b015c82ab76967773dba2b52..3785955688390797f3d61a79d8b39733fa9d6b6a 100644 (file)
@@ -4,7 +4,7 @@
  *           LyX, The Document Processor
  *
  *           Copyright 1995 Matthias Ettrich
- *           Copyright 1995-2000 The LyX Team.
+ *           Copyright 1995-2001 The LyX Team.
  *
  *           This file is Copyright 1996-1998
  *           Lars Gullik Bjønnes
 #endif
 
 #include "Toolbar_pimpl.h"
-#include "lyxfunc.h"
 #include "debug.h"
-#include "LyXView.h"
+#include "XFormsView.h"
+#include "lyxfunc.h"
+#include "FuncStatus.h"
 #include "BufferView.h"
 #include "buffer.h"
+#include "lyxtextclasslist.h"
 #include "LyXAction.h"
-#include "support/filetools.h"
+#include "MathsSymbols.h" 
 #include "gettext.h"
 
+#include "support/filetools.h"
+#include "support/lstrings.h" 
+
 using std::endl;
 
-// this one is not "C" because combox callbacks are really C++ %-|
-extern void LayoutsCB(int, void *);
-extern char const ** get_pixmap_from_symbol(char const * arg, int, int);
 extern LyXAction lyxaction;
 
 // some constants
@@ -42,18 +44,21 @@ const int sepspace = 6; // extra space
 const int buttonwidth = 30; // the standard button width
 const int height = 30; // the height of all items in the toolbar
 
-Toolbar::Pimpl::toolbarItem::toolbarItem() {
+Toolbar::Pimpl::toolbarItem::toolbarItem()
+{
        action = LFUN_NOACTION;
        icon = 0;
 }
 
 
-Toolbar::Pimpl::toolbarItem::~toolbarItem() {
+Toolbar::Pimpl::toolbarItem::~toolbarItem()
+{
        clean();
 }
 
 
-void Toolbar::Pimpl::toolbarItem::clean() {
+void Toolbar::Pimpl::toolbarItem::clean()
+{
        if (icon) {
                fl_delete_object(icon);
                fl_free_object(icon);
@@ -63,7 +68,15 @@ void Toolbar::Pimpl::toolbarItem::clean() {
 
 
 Toolbar::Pimpl::toolbarItem & 
-Toolbar::Pimpl::toolbarItem::operator=(const toolbarItem & ti) {
+Toolbar::Pimpl::toolbarItem::operator=(toolbarItem const & ti)
+{
+       // Are we assigning the object onto itself?
+       if (this == &ti)
+               return *this;
+
+       // If we already have an icon, release it.
+       clean();
+    
        // do we have to check icon too?
        action = ti.action;
        icon = 0; // locally we need to get the icon anew
@@ -74,7 +87,7 @@ Toolbar::Pimpl::toolbarItem::operator=(const toolbarItem & ti) {
 
 
 Toolbar::Pimpl::Pimpl(LyXView * o, int x, int y)
-       : owner(o), sxpos(x), sypos(y)
+       : owner(static_cast<XFormsView *>(o)), sxpos(x), sypos(y)
 {
        combox = 0;
 #if FL_REVISION < 89
@@ -83,43 +96,47 @@ Toolbar::Pimpl::Pimpl(LyXView * o, int x, int y)
 }
 
 
+namespace {
+
 #if FL_REVISION < 89
 // timer-cb for bubble-help (Matthias)
-static 
 void BubbleTimerCB(FL_OBJECT *, long data)
 {
        FL_OBJECT * ob = reinterpret_cast<FL_OBJECT*>(data);
        // The trick we use to get the help text is to read the
        // argument of the callback that has been registered for
        // ToolBarCB.  (JMarc)
-       string help = lyxaction.helpText(ob->argument);
+       string help = _(lyxaction.helpText(ob->argument));
        fl_show_oneliner(help.c_str(), ob->form->x + ob->x,
                         ob->form->y + ob->y + ob->h);
 }
 
 
-extern "C" void C_Toolbar_BubbleTimerCB(FL_OBJECT * ob, long data)
+extern "C" {
+       
+static
+void C_Toolbar_BubbleTimerCB(FL_OBJECT * ob, long data)
 {
        BubbleTimerCB(ob, data);
 }
 
+}
 
 // post_handler for bubble-help (Matthias)
-static
 int BubblePost(FL_OBJECT *ob, int event,
-                       FL_Coord /*mx*/, FL_Coord /*my*/,
-                       int /*key*/, void */*xev*/)
+              FL_Coord /*mx*/, FL_Coord /*my*/,
+              int /*key*/, void * /*xev*/)
 {
        FL_OBJECT * bubble_timer = reinterpret_cast<FL_OBJECT *>(ob->u_cdata);
        
        // We do not test for empty help here, since this can never happen
-       if(event == FL_ENTER){
+       if (event == FL_ENTER) {
                fl_set_object_callback(bubble_timer,
                                       C_Toolbar_BubbleTimerCB,
                                       reinterpret_cast<long>(ob));
                fl_set_timer(bubble_timer, 1);
        }
-       else if(event != FL_MOTION){
+       else if (event != FL_MOTION) {
                fl_set_timer(bubble_timer, 0);
                fl_hide_oneliner();
        }
@@ -127,19 +144,27 @@ int BubblePost(FL_OBJECT *ob, int event,
 }
 
 
-extern "C" int C_Toolbar_BubblePost(FL_OBJECT * ob, int event,
-                                   FL_Coord /*mx*/, FL_Coord /*my*/, 
-                                   int key, void * xev)
+extern "C" {
+       
+static
+int C_Toolbar_BubblePost(FL_OBJECT * ob, int event,
+                        FL_Coord /*mx*/, FL_Coord /*my*/, 
+                        int key, void * xev)
 {
        return BubblePost(ob, event, 0, 0, key, xev);
 }
+
+}
 #endif
 
+} // namespace anon
+
 
 void Toolbar::Pimpl::activate()
 {
        ToolbarList::const_iterator p = toollist.begin();
-       for (; p != toollist.end(); ++p) {
+       ToolbarList::const_iterator end = toollist.end();
+       for (; p != end; ++p) {
                if (p->icon) {
                        fl_activate_object(p->icon);
                }
@@ -150,20 +175,23 @@ void Toolbar::Pimpl::activate()
 void Toolbar::Pimpl::deactivate()
 {
        ToolbarList::const_iterator p = toollist.begin();
-       for (; p != toollist.end(); ++p) {
+       ToolbarList::const_iterator end = toollist.end();
+       for (; p != end; ++p) {
                if (p->icon) {
                        fl_deactivate_object(p->icon);
                }
        }
 }
 
+
 void Toolbar::Pimpl::update()
 {
        ToolbarList::const_iterator p = toollist.begin();
-       for (; p != toollist.end(); ++p) {
+       ToolbarList::const_iterator end = toollist.end();
+       for (; p != end; ++p) {
                if (p->icon) {
-                       int status = owner->getLyXFunc()->getStatus(p->action);
-                       if (status & LyXFunc::ToggleOn) {
+                       FuncStatus status = owner->getLyXFunc()->getStatus(p->action);
+                       if (status.onoff(true)) {
                                // I'd like to use a different color
                                // here, but then the problem is to
                                // know how to use transparency with
@@ -175,8 +203,7 @@ void Toolbar::Pimpl::update()
                                fl_set_object_color(p->icon, FL_MCOL, FL_BLUE);
                                fl_set_object_boxtype(p->icon, FL_UP_BOX);
                        }
-
-                       if (status & LyXFunc::Disabled) {
+                       if (status.disabled()) {
                                // Is there a way here to specify a
                                // mask in order to show that the
                                // button is disabled? (JMarc)
@@ -184,14 +211,51 @@ void Toolbar::Pimpl::update()
                        }
                        else
                                fl_activate_object(p->icon);
+               } else if (p->action == ToolbarDefaults::LAYOUTS && combox) {
+                       if (owner->getLyXFunc()->getStatus(LFUN_LAYOUT).disabled())
+                               combox->deactivate();
+                       else
+                               combox->activate();
                }
        }
 }
 
 
-void Toolbar::Pimpl::setLayout(int layout) {
-       if (combox)
-               combox->select(layout+1);
+// this one is not "C" because combox callbacks are really C++ %-|
+void Toolbar::Pimpl::layoutSelectedCB(int, void * arg, Combox *)
+{
+       Toolbar::Pimpl * tb = reinterpret_cast<Toolbar::Pimpl *>(arg);
+
+       tb->layoutSelected();
+}
+
+
+void Toolbar::Pimpl::layoutSelected()
+{
+       string const & layoutguiname = combox->getline();
+       LyXTextClass const & tc =
+               textclasslist[owner->buffer()->params.textclass];
+       
+       LyXTextClass::const_iterator end = tc.end();
+       for (LyXTextClass::const_iterator cit = tc.begin();
+            cit != end; ++cit) {
+               if (_(cit->name()) == layoutguiname) {
+                       owner->getLyXFunc()->dispatch(LFUN_LAYOUT, cit->name());
+                       return;
+               }
+       }
+       lyxerr << "ERROR (Toolbar::Pimpl::layoutSelected): layout not found!"
+              << endl;
+}
+
+void Toolbar::Pimpl::setLayout(string const & layout)
+{
+       if (combox) {
+               LyXTextClass const & tc =
+                       textclasslist[owner->buffer()->params.textclass];
+               combox->select(_(tc[layout].name()));
+       }
 }
 
 
@@ -204,82 +268,68 @@ void Toolbar::Pimpl::updateLayoutList(bool force)
        if (combox->empty() || force) {
                combox->clear();
                LyXTextClass const & tc =
-                       textclasslist.TextClass(owner->buffer()->
-                                               params.textclass);
+                       textclasslist[owner->buffer()->params.textclass];
+               LyXTextClass::const_iterator end = tc.end();
                for (LyXTextClass::const_iterator cit = tc.begin();
-                    cit != tc.end(); ++cit) {
-                       if ((*cit).obsoleted_by().empty())
-                               combox->addline(_((*cit).name().c_str()));
-                       else
-                               combox->addline(("@N" + string(_((*cit).name().c_str()))).c_str());
+                    cit != end; ++cit) {
+                       // ignore obsolete entries
+                       if (cit->obsoleted_by().empty())
+                               combox->addline(_(cit->name()));
                }
        }
        // we need to do this.
-       combox->Redraw();
+       combox->redraw();
 }
 
+
 void Toolbar::Pimpl::clearLayoutList()
 {
        if (combox) {
                combox->clear();
-               combox->Redraw();
+               combox->redraw();
        }
 }
 
+
 void Toolbar::Pimpl::openLayoutList()
 {
        if (combox)
-               combox->Show();
+               combox->show();
 }
 
-static
+
+namespace {
+
 void ToolbarCB(FL_OBJECT * ob, long ac)
 {
-       LyXView * owner = static_cast<LyXView *>(ob->u_vdata);
+       XFormsView * owner = static_cast<XFormsView *>(ob->u_vdata);
        
-       string res = owner->getLyXFunc()->Dispatch(int(ac));
-       if(!res.empty())
-               lyxerr[Debug::GUI] << "ToolbarCB: Function returned: " 
-                                  << res << endl;
+       owner->getLyXFunc()->verboseDispatch(int(ac), true);
 }
 
 
-extern "C" void C_Toolbar_ToolbarCB(FL_OBJECT * ob, long data)
-{
-       ToolbarCB(ob, data);
-}
-
-#if 0
-// What are we supposed to do with that??
-int Toolbar::get_toolbar_func(string const & func)
-{
-       int action = lyxaction.LookupFunc(func.c_str());
-       if (action == -1) {
-               if (func == "separator"){
-                       action = TOOL_SEPARATOR;
-               } else if (func == "layouts"){
-                        action = TOOL_LAYOUTS;
-                } else action = 0;
+extern "C" {
+       
+       static
+       void C_Toolbar_ToolbarCB(FL_OBJECT * ob, long data)
+       {
+               ToolbarCB(ob, data);
        }
-       return action;
+       
 }
-#endif
 
 
-static
-void setPixmap(FL_OBJECT * obj, int action, int buttonwidth, int height) {
-       string name, arg, xpm_name;
-       kb_action act;
+void setPixmap(FL_OBJECT * obj, int action, int buttonwidth, int height)
+{
+       string arg;
+       string xpm_name;
 
-       if (lyxaction.isPseudoAction(action)) {
-               act = lyxaction.retrieveActionArg(action, arg);
-               name = lyxaction.getActionName(act);
+       const kb_action act = lyxaction.retrieveActionArg(action, arg);
+       string const name = lyxaction.getActionName(act);
+       if (!arg.empty())
                xpm_name = subst(name + ' ' + arg, ' ','_');
-       } else {
-               act = (kb_action)action;
-               name = lyxaction.getActionName(action);
+       else 
                xpm_name = name;
-       }
 
        string fullname = LibFileSearch("images", xpm_name, "xpm");
 
@@ -291,12 +341,16 @@ void setPixmap(FL_OBJECT * obj, int action, int buttonwidth, int height) {
        }
 
        if (act == LFUN_INSERT_MATH && !arg.empty()) {
-               lyxerr[Debug::GUI] << "Using mathed-provided icon" << endl;
                char const ** pixmap = get_pixmap_from_symbol(arg.c_str(),
                                                        buttonwidth,
                                                        height);
-               fl_set_pixmapbutton_data(obj, const_cast<char **>(pixmap));
-               return;
+               if (pixmap) {
+                       lyxerr[Debug::GUI] << "Using mathed-provided icon"
+                                          << endl;
+                       fl_set_pixmapbutton_data(obj,
+                                                const_cast<char **>(pixmap));
+                       return;
+               }
        }
        
        lyxerr << "Unable to find icon `" << xpm_name << "'" << endl;
@@ -308,6 +362,9 @@ void setPixmap(FL_OBJECT * obj, int action, int buttonwidth, int height) {
        }
 }
 
+} // namespace anon
+
+
 void Toolbar::Pimpl::set(bool doingmain)
 {
        // we shouldn't set if we have not cleaned
@@ -328,8 +385,9 @@ void Toolbar::Pimpl::set(bool doingmain)
 #endif
        
        ToolbarList::iterator item = toollist.begin();
-       for (; item != toollist.end(); ++item) {
-               switch(item->action){
+       ToolbarList::iterator end = toollist.end();
+       for (; item != end; ++item) {
+               switch (item->action) {
                case ToolbarDefaults::SEPARATOR:
                        xpos += sepspace;
                        break;
@@ -341,7 +399,7 @@ void Toolbar::Pimpl::set(bool doingmain)
                        if (!combox)
                                combox = new Combox(FL_COMBOX_DROPLIST);
                        combox->add(xpos, ypos, 135, height, 400);
-                       combox->setcallback(LayoutsCB);
+                       combox->setcallback(layoutSelectedCB, this);
                        combox->resize(FL_RESIZE_ALL);
                        combox->gravity(NorthWestGravity, NorthWestGravity);
                        xpos += 135;
@@ -364,7 +422,7 @@ void Toolbar::Pimpl::set(bool doingmain)
 
                        // Set the tooltip
 #if FL_REVISION >= 89
-                       string help = lyxaction.helpText(item->action);
+                       string const help(_(lyxaction.helpText(item->action)));
                        fl_set_object_helper(obj, help.c_str());        
 #else
                        fl_set_object_posthandler(obj, C_Toolbar_BubblePost);
@@ -417,7 +475,8 @@ void Toolbar::Pimpl::add(int action, bool doclean)
                fl_freeze_form(owner->getForm());
 
                ToolbarList::iterator p = toollist.begin();
-               for (; p != toollist.end(); ++p) {
+               ToolbarList::iterator end = toollist.end();
+               for (; p != end; ++p) {
                        p->clean();
                }
 
@@ -484,6 +543,7 @@ void Toolbar::Pimpl::reset()
        lightReset();
 }
 
+
 void Toolbar::Pimpl::lightReset() {
        xpos = sxpos - standardspacing;
        ypos = sypos;