]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/xforms/Tooltips.C
having broken Rob's word count update, I guess I should fix it too ;-)
[lyx.git] / src / frontends / xforms / Tooltips.C
index ef82135d45bb36cfc7666346e388ff5e8766a6ad..d5e3defd6f807d3b3d9517f8844809151067f5cb 100644 (file)
@@ -1,11 +1,14 @@
-/*
+/**
  * \file Tooltips.C
- * Copyright 2002 the LyX Team
- * Read the file COPYING
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
  *
- * \author Angus Leeming, a.leeming@ic.ac.uk
+ * \author Angus Leeming 
  *
- * Tooltips for xforms. xforms 0.89 supports them directly, but 0.88 needs
+ * Full author contact details are available in file CREDITS
+ */
+
+/* Tooltips for xforms. xforms 0.89 supports them directly, but 0.88 needs
  * a bit of jiggery pokery. This class wraps it all up in a neat interface.
  * Based on code originally in Toolbar_pimpl.C that appears to have been
  * written by Matthias Ettrich and Jean-Marc Lasgouttes.
 #endif
 
 #include "Tooltips.h"
+#include "xforms_helpers.h" // formatted
+#include "gettext.h"
+#include "support/lstrings.h"
 #include "support/LAssert.h"
+#include FORMS_H_LOCATION
+
+#include <boost/bind.hpp>
+
+bool Tooltips::enabled_ = true;
 
-//#if FL_REVISION >= 89
-// Usually, this is all that is needed for xforms 0.89
-// However, I can't see an easy way to change Tooltips on the fly
-// with this method, so for now use the jiggery pokery below. ;-)
-// Angus 6 Feb 2002
+boost::signal0<void> Tooltips::toggled;
 
-/*
-void Tooltips::activateTooltip(FL_OBJECT * ob)
+
+#if FL_VERSION > 0 || FL_REVISION >= 89
+
+Tooltips::Tooltips()
 {
-       lyx::Assert(ob);
+       toggled.connect(boost::bind(&Tooltips::set, this));
+}
+
+
+void Tooltips::toggleEnabled()
+{
+       enabled_ = !enabled_;
+       toggled();
+}
+
 
-       string const help(getTooltip(ob));
-       if (!help.empty())
-               fl_set_object_helper(ob, help.c_str()); 
+void Tooltips::set()
+{
+       if (tooltipsMap.empty())
+               return;
+
+       TooltipsMap::const_iterator it  = tooltipsMap.begin();
+       TooltipsMap::const_iterator end = tooltipsMap.end();
+       for (; it != end; ++it) {
+               FL_OBJECT * const ob = it->first;
+               char const * const c_str = enabled_ ? it->second.c_str() : 0;
+               fl_set_object_helper(ob, c_str);
+       }
 }
-*/
 
-//#else // if FL_REVISION < 89
+
+void Tooltips::init(FL_OBJECT * ob, string const & tip)
+{
+       lyx::Assert(ob && ob->form);
+
+       // Store the tooltip string
+       string const str = formatted(trim(tip), 400);
+       tooltipsMap[ob] = str;
+
+       // Set the tooltip
+       char const * const c_str = enabled_ ? str.c_str() : 0;
+       fl_set_object_helper(ob, c_str);
+}
+
+
+#else // if FL_REVISION < 89
 
 namespace {
 
 int TooltipHandler(FL_OBJECT *ob, int event);
 
 void TooltipTimerCB(FL_OBJECT * timer, long data);
+
 }
 
 extern "C" {
@@ -62,17 +103,54 @@ static void C_TooltipTimerCB(FL_OBJECT * ob, long data)
 }
 
 }
 
-void Tooltips::activateTooltip(FL_OBJECT * ob)
+
+Tooltips::Tooltips()
+       : tooltip_timer_(0)
 {
-       lyx::Assert(ob);
+       toggled.connect(boost::bind(&Tooltips::set, this));
+}
+
+
+void Tooltips::toggleEnabled()
+{
+       enabled_ = !enabled_;
+       toggled();
+}
+
+
+void Tooltips::set()
+{}
+
+
+void Tooltips::init(FL_OBJECT * ob, string const & tip)
+{
+       lyx::Assert(ob && ob->form);
+
+       // Paranoia check!
+       TooltipsMap::const_iterator it = tooltipsMap.find(ob);
+       if (it != tooltipsMap.end())
+               return;
+
+       string const str = trim(tip);
+       if (str.empty())
+               return;
+
+       // Store the tooltip string
+       tooltipsMap[ob] = formatted(str, 400);
 
        if (!tooltip_timer_) {
-               lyx::Assert(ob->form);
-               fl_addto_form(ob->form);
+               if (fl_current_form && ob->form != fl_current_form)
+                       fl_end_form();
+
+               bool const open_form = !fl_current_form;
+               if (open_form)
+                       fl_addto_form(ob->form);
+
                tooltip_timer_ = fl_add_timer(FL_HIDDEN_TIMER, 0, 0, 0, 0, "");
-               fl_end_form();
+
+               if (open_form)
+                       fl_end_form();
        }
 
        fl_set_object_posthandler(ob, C_TooltipHandler);
@@ -81,6 +159,15 @@ void Tooltips::activateTooltip(FL_OBJECT * ob)
 }
 
 
+string const Tooltips::get(FL_OBJECT * ob) const
+{
+       TooltipsMap::const_iterator it = tooltipsMap.find(ob);
+       if (it == tooltipsMap.end())
+               return string();
+       return it->second;
+}
+
+
 namespace {
 
 void TooltipTimerCB(FL_OBJECT * timer, long data)
@@ -89,8 +176,8 @@ void TooltipTimerCB(FL_OBJECT * timer, long data)
        lyx::Assert(ob && ob->form && timer && timer->u_vdata);
        FL_FORM * form = ob->form;
        Tooltips * tooltip = static_cast<Tooltips *>(timer->u_vdata);
-       
-       string const help = tooltip->getTooltip(ob);
+
+       string const help = tooltip->get(ob);
        if (help.empty())
                return;
 
@@ -99,21 +186,24 @@ void TooltipTimerCB(FL_OBJECT * timer, long data)
 }
 
 
-// post_handler for bubble-help (Matthias)
-int TooltipHandler(FL_OBJECT *ob, int event)
+// post_handler for tooltip help
+int TooltipHandler(FL_OBJECT * ob, int event)
 {
+       if (!Tooltips::enabled())
+               return 0;
+
        lyx::Assert(ob);
        FL_OBJECT * timer = reinterpret_cast<FL_OBJECT *>(ob->u_cdata);
        lyx::Assert(timer);
 
        // 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(timer,
                                       C_TooltipTimerCB,
                                       reinterpret_cast<long>(ob));
                fl_set_timer(timer, 1);
        }
-       else if (event != FL_MOTION){
+       else if (event != FL_MOTION) {
                fl_set_timer(timer, 0);
                fl_hide_oneliner();
        }
@@ -122,4 +212,4 @@ int TooltipHandler(FL_OBJECT *ob, int event)
 
 } // namespace anon
 
-//#endif // FL_REVISION < 89
+#endif // FL_REVISION >= 89