Skip to content

Commit

Permalink
Added the 2.0 Fl_Widget::copy_label() method to allow FLTK 1.x
Browse files Browse the repository at this point in the history
applications to have their label strings managed by FLTK (STR
#630)

Added Fl::delete_widget() method to safely delete widgets in
callback methods (STR #629)


git-svn-id: file:///fltk/svn/fltk/branches/branch-1.1@3917 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
  • Loading branch information
michaelrsweet committed Nov 23, 2004
1 parent 5cc0f07 commit a42ded7
Show file tree
Hide file tree
Showing 10 changed files with 127 additions and 19 deletions.
5 changes: 5 additions & 0 deletions CHANGES
@@ -1,6 +1,11 @@
CHANGES IN FLTK 1.1.6

- Documentation updates (STR #552, STR #608)
- Added the 2.0 Fl_Widget::copy_label() method to
allow FLTK 1.x applications to have their label
strings managed by FLTK (STR #630)
- Added Fl::delete_widget() method to safely delete
widgets in callback methods (STR #629)
- Fl_Widget::damage(uchar,int,int,int,int) didn't clip
the bounding box properly (STR #626)
- Windows could appear on the wrong screen on OSX (STR
Expand Down
8 changes: 6 additions & 2 deletions FL/Fl.H
@@ -1,5 +1,5 @@
//
// "$Id: Fl.H,v 1.8.2.11.2.23 2004/04/11 04:38:53 easysw Exp $"
// "$Id: Fl.H,v 1.8.2.11.2.24 2004/11/23 19:47:50 easysw Exp $"
//
// Main header file for the Fast Light Tool Kit (FLTK).
//
Expand Down Expand Up @@ -257,10 +257,14 @@ public:
static void unlock();
static void awake(void* message = 0);
static void* thread_message();

// Widget deletion:
static void delete_widget(Fl_Widget *w);
static void do_widget_deletion();
};

#endif // !Fl_H

//
// End of "$Id: Fl.H,v 1.8.2.11.2.23 2004/04/11 04:38:53 easysw Exp $".
// End of "$Id: Fl.H,v 1.8.2.11.2.24 2004/11/23 19:47:50 easysw Exp $".
//
9 changes: 5 additions & 4 deletions FL/Fl_Widget.H
@@ -1,5 +1,5 @@
//
// "$Id: Fl_Widget.H,v 1.6.2.4.2.25 2004/09/24 16:00:08 easysw Exp $"
// "$Id: Fl_Widget.H,v 1.6.2.4.2.26 2004/11/23 19:47:50 easysw Exp $"
//
// Widget header file for the Fast Light Tool Kit (FLTK).
//
Expand Down Expand Up @@ -90,7 +90,7 @@ protected:
void set_flag(int c) {flags_ |= c;}
void clear_flag(int c) {flags_ &= ~c;}
enum {INACTIVE=1, INVISIBLE=2, OUTPUT=4, SHORTCUT_LABEL=64,
CHANGED=128, VISIBLE_FOCUS=512};
CHANGED=128, VISIBLE_FOCUS=512, COPIED_LABEL = 1024};

void draw_box() const;
void draw_box(Fl_Boxtype, Fl_Color) const;
Expand Down Expand Up @@ -131,7 +131,8 @@ public:
void selection_color(unsigned a) {color2_ = a;}
void color(unsigned a, unsigned b) {color_=a; color2_=b;}
const char* label() const {return label_.value;}
void label(const char* a) {label_.value=a; redraw_label();}
void label(const char* a);
void copy_label(const char* a);
void label(Fl_Labeltype a,const char* b) {label_.type = a; label_.value = b;}
Fl_Labeltype labeltype() const {return (Fl_Labeltype)label_.type;}
void labeltype(Fl_Labeltype a) {label_.type = a;}
Expand Down Expand Up @@ -217,5 +218,5 @@ public:
#endif

//
// End of "$Id: Fl_Widget.H,v 1.6.2.4.2.25 2004/09/24 16:00:08 easysw Exp $".
// End of "$Id: Fl_Widget.H,v 1.6.2.4.2.26 2004/11/23 19:47:50 easysw Exp $".
//
11 changes: 11 additions & 0 deletions documentation/Fl.html
Expand Up @@ -49,6 +49,7 @@ <H3>Methods</H3>
<LI><A HREF="#Fl.copy">copy</A></LI>
<LI><A HREF="#Fl.damage">damage</A></LI>
<LI><A HREF="#Fl.default_atclose">default_atclose</A></LI>
<LI><A HREF="#Fl.delete_widget">delete_widget</A></LI>
<LI><A HREF="#Fl.display">display</A></LI>
<LI><A HREF="#Fl.dnd">dnd</A></LI>
<LI><A HREF="#Fl.dnd_text_ops">dnd_text_ops</A></LI>
Expand Down Expand Up @@ -491,6 +492,16 @@ <H4><A NAME="Fl.damage">int damage();<BR>

<H4><A NAME="Fl.default_atclose">void default_atclose(Fl_Window*,void*);</A></H4>

<p>This is the default callback for window widgets. It hides the
window and then calls the default widget callback.</p>

<H4><A NAME="Fl.delete_widget">void delete_widget(Fl_Widget*);</A></H4>

<p>Schedules a widget for deletion when it is safe to do so. Use
this method to delete a widget inside a callback function. When
deleting groups or windows, you must only delete the group or
window widget and not the individual child widgets.</p>

<H4><A NAME="Fl.display">void display(const char*);</A></H4>

<P>Sets the X display to use for all windows. Actually this just sets
Expand Down
16 changes: 14 additions & 2 deletions documentation/Fl_Widget.html
Expand Up @@ -53,6 +53,7 @@ <H3>Methods</H3>
<UL>
<LI><A href=#Fl_Widget.color>color</A></LI>
<LI><A href=#Fl_Widget.contains>contains</A></LI>
<LI><A href=#Fl_Widget.copy_label>copy_label</A></LI>
<LI><A href=#Fl_Widget.damage>damage</A></LI>
<LI><A href=#Fl_Widget.deactivate>deactivate</A></LI>
<LI><A href=#Fl_Widget.default_callback>default_callback</A></LI>
Expand All @@ -64,10 +65,10 @@ <H3>Methods</H3>
<LI><A href=#Fl_Widget.image>image</A></LI>
<LI><A href=#Fl_Widget.inside>inside</A></LI>
<LI><A href=#Fl_Widget.label>label</A></LI>
<LI><A href=#Fl_Widget.labelcolor>labelcolor</A></LI>
</UL>
</TD><TD align=left valign=top>
<UL>
<LI><A href=#Fl_Widget.labelcolor>labelcolor</A></LI>
<LI><A href=#Fl_Widget.labelfont>labelfont</A></LI>
<LI><A href=#Fl_Widget.labelsize>labelsize</A></LI>
<LI><A href=#Fl_Widget.labeltype>labeltype</A></LI>
Expand Down Expand Up @@ -258,6 +259,14 @@ <h4><a name="Fl_Widget.contains">int Fl_Widget::contains(Fl_Widget* b) const</A>
equal to this widget. Returns 0 if <TT>b</TT> is <TT>NULL</TT>.


<H4><A name='Fl_Widget.copy_label'>void Fl_Widget::copy_label(const char*)</A></H4>

<P>Sets the current label. Unlike <a
href='#Fl_Widget.label'><tt>label()</tt></a>, this method
allocates a copy of the label string instead of using the
original string pointer.</p>


<H4><A name=Fl_Widget.damage>uchar Fl_Widget::damage() const<BR>
void damage(uchar c);<BR>
void damage(uchar c, int X, int Y, int W, int H);</A></H4>
Expand Down Expand Up @@ -329,7 +338,10 @@ <H4><A name=Fl_Widget.label>const char* Fl_Widget::label() const
somewhere on or next to the widget. The passed pointer is stored
unchanged in the widget (the string is <I>not</I> copied), so if
you need to set the label to a formatted value, make sure the
buffer is <TT>static</TT>, global, or allocated.
buffer is <TT>static</TT>, global, or allocated. The <a
href='#Fl_Widget.copy_label'><tt>copy_label()</tt></a> method
can be used to make a copy of the label string
automatically.</p>


<H4><A name=Fl_Widget.labelcolor>Fl_Color Fl_Widget::labelcolor() const
Expand Down
11 changes: 10 additions & 1 deletion documentation/common.html
Expand Up @@ -532,7 +532,16 @@ <H2>Callbacks</H2>

<CENTER><TABLE WIDTH="80%" BORDER="1" CELLPADDING="5" CELLSPACING="0" BGCOLOR="#cccccc">
<TR>
<TD><B>Hint:</B>
<TD><B>Note:</B>

<P>You cannot delete a widget inside a callback, as the
widget may still be accessed by FLTK after your callback
is completed. Instead, use the <a
href='Fl.html#Fl.delete_widget'><tt>Fl::delete_widget()</tt></a>
method to mark your widget for deletion when it is safe
to do so.</p>

<p><B>Hint:</B>

<P>Many programmers new to FLTK or C++ try to use a
non-static class method instead of a static class method
Expand Down
6 changes: 3 additions & 3 deletions documentation/index.html
@@ -1,7 +1,7 @@
<HTML>
<HEAD>
<META NAME="robots" CONTENT="noindex">
<TITLE>FLTK 1.1.5 Programming Manual</TITLE>
<TITLE>FLTK 1.1.6 Programming Manual</TITLE>
</HEAD>
<BODY>

Expand All @@ -10,8 +10,8 @@
<TD VALIGN="MIDDLE">
<IMG SRC="FL.gif" WIDTH="200" HEIGHT="100" ALIGN="ABSMIDDLE" ALT="FL"></TD>
<TD ALIGN="CENTER" VALIGN="MIDDLE">
<H1>FLTK 1.1.5 Programming Manual</H1>
<P>Revision 5 by Michael Sweet, Craig P. Earls, and Bill Spitzak<BR>
<H1>FLTK 1.1.6 Programming Manual</H1>
<P>Revision 6 by Michael Sweet, Craig P. Earls, and Bill Spitzak<BR>
Copyright 1998-2004 by Bill Spitzak and others.</P>
</TD>
</TR>
Expand Down
6 changes: 3 additions & 3 deletions documentation/preface.html
Expand Up @@ -2,15 +2,15 @@
<HEAD>
<META CONTENT="Written by Michael Sweet, Craig P. Earls, and Bill Spitzak" NAME="Author">
<META CONTENT="Copyright 1998-2004 by Bill Spitzak and Others." NAME="Copyright">
<META CONTENT="Revision 5" NAME="DocNumber">
<TITLE>FLTK 1.1.5 Programming Manual</TITLE>
<META CONTENT="Revision 6" NAME="DocNumber">
<TITLE>FLTK 1.1.6 Programming Manual</TITLE>
</HEAD>
<BODY>

<H1 ALIGN="RIGHT"><A NAME="preface">Preface</A></H1>

<P>This manual describes the Fast Light Tool Kit (&quot;FLTK&quot;)
version 1.1.5, a C++ Graphical User Interface
version 1.1.6, a C++ Graphical User Interface
(&quot;GUI&quot;) toolkit for UNIX, Microsoft Windows and MacOS. Each
of the chapters in this manual is designed as a tutorial for
using FLTK, while the appendices provide a convenient reference
Expand Down
48 changes: 46 additions & 2 deletions src/Fl.cxx
@@ -1,5 +1,5 @@
//
// "$Id: Fl.cxx,v 1.24.2.41.2.69 2004/11/23 19:09:55 easysw Exp $"
// "$Id: Fl.cxx,v 1.24.2.41.2.70 2004/11/23 19:47:51 easysw Exp $"
//
// Main event handling code for the Fast Light Tool Kit (FLTK).
//
Expand Down Expand Up @@ -236,6 +236,8 @@ extern int fl_wait(double time); // in Fl_<platform>.cxx
static char in_idle;

double Fl::wait(double time_to_wait) {
do_widget_deletions();

if (first_timeout) {
elapse_timeouts();
Timeout *t;
Expand Down Expand Up @@ -1051,6 +1053,48 @@ void Fl_Window::flush() {
draw();
}


//
// The following methods allow callbacks to schedule the deletion of
// widgets at "safe" times.
//

static int num_dwidgets = 0, alloc_dwidgets = 0;
static Fl_Widget **dwidgets = 0;

void
Fl::delete_widget(Fl_Widget *w) {
if (!w) return;

if (num_dwidgets >= alloc_dwidgets) {
Fl_Widget **temp;

temp = new Fl_Widget *[alloc_dwidgets + 10];
if (alloc_dwidgets) {
memcpy(temp, dwidgets, alloc_dwidgets * sizeof(Fl_Widget *));
delete[] dwidgets;
}

dwidgets = temp;
alloc_dwidgets += 10;
}

dwidgets[num_dwidgets] = w;
num_dwidgets ++;
}


void
Fl::do_widget_deletion() {
if (!num_dwidgets) return;

for (int i = 0; i < num_dwidgets; i ++)
delete dwidgets[i];

num_dwidgets = 0;
}


//
// End of "$Id: Fl.cxx,v 1.24.2.41.2.69 2004/11/23 19:09:55 easysw Exp $".
// End of "$Id: Fl.cxx,v 1.24.2.41.2.70 2004/11/23 19:47:51 easysw Exp $".
//
26 changes: 24 additions & 2 deletions src/Fl_Widget.cxx
@@ -1,5 +1,5 @@
//
// "$Id: Fl_Widget.cxx,v 1.5.2.4.2.24 2004/08/25 00:20:25 matthiaswm Exp $"
// "$Id: Fl_Widget.cxx,v 1.5.2.4.2.25 2004/11/23 19:47:52 easysw Exp $"
//
// Base widget class for the Fast Light Tool Kit (FLTK).
//
Expand Down Expand Up @@ -129,6 +129,7 @@ extern void fl_throw_focus(Fl_Widget*); // in Fl_x.cxx
// However, it is only legal to destroy a "root" such as an Fl_Window,
// and automatic destructors may be called.
Fl_Widget::~Fl_Widget() {
if (flags() & COPIED_LABEL) free(label_.value);
parent_ = 0; // Don't throw focus to a parent widget.
fl_throw_focus(this);
}
Expand Down Expand Up @@ -244,6 +245,27 @@ int Fl_Widget::contains(const Fl_Widget *o) const {
return 0;
}


void
Fl_Widget::label(const char *a) {
if (flags() & COPIED_LABEL) {
free(label_.value);
clear_flag(COPIED_LABEL);
}
label_.value=a;
redraw_label();
}


void
Fl_Widget::copy_label(const char *a) {
if (flags() & COPIED_LABEL) free(label_.value);
set_flag(COPIED_LABEL);
label_.value=strdup(a);
redraw_label();
}


//
// End of "$Id: Fl_Widget.cxx,v 1.5.2.4.2.24 2004/08/25 00:20:25 matthiaswm Exp $".
// End of "$Id: Fl_Widget.cxx,v 1.5.2.4.2.25 2004/11/23 19:47:52 easysw Exp $".
//

0 comments on commit a42ded7

Please sign in to comment.