Author: greg.ercolano
Date: 2010-12-02 09:58:58 -0800 (Thu, 02 Dec 2010)
New Revision: 7940
Log:
STR#2466: Added copy_tooltip().



Modified:
   branches/branch-1.3/FL/Fl_Tooltip.H
   branches/branch-1.3/FL/Fl_Widget.H
   branches/branch-1.3/src/Fl_Tooltip.cxx
   branches/branch-1.3/src/Fl_Widget.cxx

Modified: branches/branch-1.3/FL/Fl_Tooltip.H
===================================================================
--- branches/branch-1.3/FL/Fl_Tooltip.H 2010-12-02 13:01:44 UTC (rev 7939)
+++ branches/branch-1.3/FL/Fl_Tooltip.H 2010-12-02 17:58:58 UTC (rev 7940)
@@ -92,8 +92,10 @@
   // fabien: made it private with only a friend function access
 private:
   friend void Fl_Widget::tooltip(const char *);
+  friend void Fl_Widget::copy_tooltip(const char *);
   static void enter_(Fl_Widget* w);
   static void exit_(Fl_Widget *w);
+  static void set_enter_exit_once_();
 
 private:
   static float delay_; //!< delay before a tooltip is shown

Modified: branches/branch-1.3/FL/Fl_Widget.H
===================================================================
--- branches/branch-1.3/FL/Fl_Widget.H  2010-12-02 13:01:44 UTC (rev 7939)
+++ branches/branch-1.3/FL/Fl_Widget.H  2010-12-02 17:58:58 UTC (rev 7940)
@@ -162,6 +162,7 @@
         MODAL           = 1<<14,  ///< a window blocking input to all other 
winows (Fl_Window)
         NO_OVERLAY      = 1<<15,  ///< window not using a hardware overlay 
plane (Fl_Menu_Window)
         GROUP_RELATIVE  = 1<<16,  ///< position this idget relative to the 
parent group, not to the window
+        COPIED_TOOLTIP  = 1<<17,  ///< the widget tooltip is internally 
copied, its destruction is handled by the widget
         // (space for more flags)
         USERFLAG3       = 1<<29,  ///< reserved for 3rd party extensions
         USERFLAG2       = 1<<30,  ///< reserved for 3rd party extensions
@@ -533,23 +534,13 @@
 
   /** Gets the current tooltip text.
       \return a pointer to the tooltip text or NULL
+      \see tooltip(const char*), copy_tooltip(const char*)
    */
   const char *tooltip() const {return tooltip_;}
 
-  /** Sets the current tooltip text. 
-      Sets a string of text to display in a popup tooltip window when the user 
-      hovers the mouse over the widget. The string is <I>not</I> copied, so 
-      make sure any formatted string is stored in a static, global, 
-      or allocated buffer.
+  void tooltip(const char *text);              // see Fl_Tooltip
+  void copy_tooltip(const char *text);         // see Fl_Tooltip
 
-      If no tooltip is set, the tooltip of the parent is inherited. Setting a 
-      tooltip for a group and setting no tooltip for a child will show the 
-      group's tooltip instead. To avoid this behavior, you can set the child's 
-      tooltip to an empty string ("").
-      \param[in] t new tooltip
-   */
-  void tooltip(const char *t);
-
   /** Gets the current callback function for the widget.
       Each widget has a single callback.
       \return current callback

Modified: branches/branch-1.3/src/Fl_Tooltip.cxx
===================================================================
--- branches/branch-1.3/src/Fl_Tooltip.cxx      2010-12-02 13:01:44 UTC (rev 
7939)
+++ branches/branch-1.3/src/Fl_Tooltip.cxx      2010-12-02 17:58:58 UTC (rev 
7940)
@@ -30,6 +30,7 @@
 #include <FL/Fl_Menu_Window.H>
 
 #include <stdio.h>
+#include <string.h>    // strdup()
 
 float          Fl_Tooltip::delay_ = 1.0f;
 float          Fl_Tooltip::hoverdelay_ = 0.2f;
@@ -275,16 +276,70 @@
 #endif // DEBUG
 }
 
-void Fl_Widget::tooltip(const char *tt) {
+void Fl_Tooltip::set_enter_exit_once_() {
   static char beenhere = 0;
   if (!beenhere) {
     beenhere          = 1;
     Fl_Tooltip::enter = Fl_Tooltip::enter_;
     Fl_Tooltip::exit  = Fl_Tooltip::exit_;
   }
-  tooltip_ = tt;
 }
 
+/**
+  Sets the current tooltip text. 
+
+  Sets a string of text to display in a popup tooltip window when the user 
+  hovers the mouse over the widget. The string is <I>not</I> copied, so 
+  make sure any formatted string is stored in a static, global, 
+  or allocated buffer. If you want a copy made and managed for you,
+  use the copy_tooltip() method, which will manage the tooltip string
+  automatically.
+
+  If no tooltip is set, the tooltip of the parent is inherited. Setting a 
+  tooltip for a group and setting no tooltip for a child will show the 
+  group's tooltip instead. To avoid this behavior, you can set the child's 
+  tooltip to an empty string ("").
+  \param[in] text New tooltip text (no copy is made)
+  \see copy_tooltip(const char*), tooltip()
+*/
+void Fl_Widget::tooltip(const char *text) {
+  Fl_Tooltip::set_enter_exit_once_();
+  if (flags() & COPIED_TOOLTIP) {
+    // reassigning a copied tooltip remains the same copied tooltip
+    if (tooltip_ == text) return;
+    free((void*)(tooltip_));            // free maintained copy
+    clear_flag(COPIED_TOOLTIP);         // disable copy flag (WE don't make 
copies)
+  }
+  tooltip_ = text;
+}
+
+/**
+  Sets the current tooltip text. 
+  Unlike tooltip(), this method allocates a copy of the tooltip 
+  string instead of using the original string pointer.
+
+  The internal copy will automatically be freed whenever you assign
+  a new tooltip or when the widget is destroyed.
+
+  If no tooltip is set, the tooltip of the parent is inherited. Setting a 
+  tooltip for a group and setting no tooltip for a child will show the 
+  group's tooltip instead. To avoid this behavior, you can set the child's 
+  tooltip to an empty string ("").
+  \param[in] text New tooltip text (an internal copy is made and managed)
+  \see tooltip(const char*), tooltip()
+*/
+void Fl_Widget::copy_tooltip(const char *text) {
+  Fl_Tooltip::set_enter_exit_once_();
+  if (flags() & COPIED_TOOLTIP) free((void *)(tooltip_));
+  if (text) {
+    set_flag(COPIED_TOOLTIP);
+    tooltip_ = strdup(text);
+  } else {
+    clear_flag(COPIED_TOOLTIP);
+    tooltip_ = (char *)0;
+  }
+}
+
 //
 // End of "$Id$".
 //

Modified: branches/branch-1.3/src/Fl_Widget.cxx
===================================================================
--- branches/branch-1.3/src/Fl_Widget.cxx       2010-12-02 13:01:44 UTC (rev 
7939)
+++ branches/branch-1.3/src/Fl_Widget.cxx       2010-12-02 17:58:58 UTC (rev 
7940)
@@ -168,6 +168,7 @@
 Fl_Widget::~Fl_Widget() {
   Fl::clear_widget_pointer(this);
   if (flags() & COPIED_LABEL) free((void *)(label_.value));
+  if (flags() & COPIED_TOOLTIP) free((void *)(tooltip_));
   // remove from parent group
   if (parent_) parent_->remove(this);
 #ifdef DEBUG_DELETE

_______________________________________________
fltk-commit mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk-commit

Reply via email to