1999-01-13 22:28:54 +03:00
|
|
|
<HTML><BODY>
|
|
|
|
<H1 ALIGN=RIGHT><A NAME=subclassing>7 - Adding and Extending Widgets</A></H1>
|
2001-11-29 22:24:00 +03:00
|
|
|
This chapter describes how to add your own widgets or extend existing
|
|
|
|
widgets in FLTK.
|
1998-12-29 17:21:17 +03:00
|
|
|
<H2>Subclassing</H2>
|
2001-11-29 22:24:00 +03:00
|
|
|
New widgets are created by <I>subclassing</I> an existing FLTK widget,
|
|
|
|
typically <TT>Fl_Widget</TT> for controls and <TT>Fl_Group</TT> for
|
|
|
|
composite widgets.
|
|
|
|
<P>A control widget typically interacts with the user to receive and/or
|
1999-01-13 22:28:54 +03:00
|
|
|
display a value of some sort. </P>
|
2001-11-29 22:24:00 +03:00
|
|
|
<P>A composite widget widget holds a list of child widgets and handles moving,
|
|
|
|
sizing, showing, or hiding them as needed. <TT>Fl_Group</TT> is the
|
1999-01-27 00:36:02 +03:00
|
|
|
main composite widget widget class in FLTK, and all of the other composite widgets (<TT>
|
2001-11-29 22:24:00 +03:00
|
|
|
Fl_Pack</TT>, <TT>Fl_Scroll</TT>, <TT>Fl_Tabs</TT>, <TT>Fl_Tile</TT>,
|
1999-01-13 22:28:54 +03:00
|
|
|
and <TT>Fl_Window</TT>) are subclasses of it. </P>
|
2001-11-29 22:24:00 +03:00
|
|
|
<P>You can also subclass other existing widgets to provide a different
|
|
|
|
look or user-interface. For example, the button widgets are all
|
|
|
|
subclasses of <TT>Fl_Button</TT> since they all interact with the user
|
|
|
|
via a mouse button click. The only difference is the code that draws
|
1999-01-13 22:28:54 +03:00
|
|
|
the face of the button. </P>
|
1999-01-07 19:36:11 +03:00
|
|
|
<H2>Making a Subclass of Fl_Widget</H2>
|
2001-11-29 22:24:00 +03:00
|
|
|
Your subclasses can directly descend from <TT>Fl_Widget</TT> or any
|
|
|
|
subclass of <TT>Fl_Widget</TT>. <TT>Fl_Widget</TT> has only four
|
|
|
|
virtual methods, and overriding some or all of these may be necessary.
|
1999-01-07 19:36:11 +03:00
|
|
|
<H2>The Constructor</H2>
|
2001-11-29 22:24:00 +03:00
|
|
|
The constructor should have the following arguments:
|
|
|
|
<UL><PRE>
|
1999-01-07 19:36:11 +03:00
|
|
|
MyClass(int x, int y, int w, int h, const char *label = 0);
|
2001-11-29 22:24:00 +03:00
|
|
|
</PRE></UL>
|
2002-02-13 06:33:53 +03:00
|
|
|
This will allow the class to be used in <A href="fluid.html#FLUID">FLUID</A>
|
2001-11-29 22:24:00 +03:00
|
|
|
without problems.
|
|
|
|
<P>The constructor must call the constructor for the base class and
|
1999-01-13 22:28:54 +03:00
|
|
|
pass the same arguments: </P>
|
2001-11-29 22:24:00 +03:00
|
|
|
<UL><PRE>
|
1999-01-07 19:36:11 +03:00
|
|
|
MyClass::MyClass(int x, int y, int w, int h, const char *label)
|
|
|
|
: Fl_Widget(x, y, w, h, label) {
|
|
|
|
// do initialization stuff...
|
|
|
|
}
|
2001-11-29 22:24:00 +03:00
|
|
|
</PRE></UL>
|
1999-01-27 00:36:02 +03:00
|
|
|
<TT>Fl_Widget</TT>'s protected constructor sets <TT>x()</TT>, <TT>y()</TT>,
|
2001-11-29 22:24:00 +03:00
|
|
|
<TT>w()</TT>, <TT>h()</TT>, and <TT>label()</TT> to the passed values
|
|
|
|
and initializes the other instance variables to:
|
|
|
|
<UL><PRE>
|
1999-01-07 19:36:11 +03:00
|
|
|
type(0);
|
|
|
|
box(FL_NO_BOX);
|
|
|
|
color(FL_GRAY);
|
|
|
|
selection_color(FL_GRAY);
|
|
|
|
labeltype(FL_NORMAL_LABEL);
|
|
|
|
labelstyle(FL_NORMAL_STYLE);
|
|
|
|
labelsize(FL_NORMAL_SIZE);
|
|
|
|
labelcolor(FL_BLACK);
|
|
|
|
align(FL_ALIGN_CENTER);
|
|
|
|
callback(default_callback,0);
|
|
|
|
flags(ACTIVE|VISIBLE);
|
2001-11-29 22:24:00 +03:00
|
|
|
image(0);
|
|
|
|
deimage(0);
|
|
|
|
</PRE></UL>
|
1999-01-07 19:36:11 +03:00
|
|
|
<H2>Protected Methods of Fl_Widget</H2>
|
2001-11-29 22:24:00 +03:00
|
|
|
The following methods are provided for subclasses to use:
|
1999-01-13 22:28:54 +03:00
|
|
|
<UL>
|
1999-01-27 00:36:02 +03:00
|
|
|
<LI><A href=#clear_visible><TT>Fl_Widget::clear_visible</TT></A></LI>
|
|
|
|
<LI><A href=#damage><TT>Fl_Widget::damage</TT></A></LI>
|
|
|
|
<LI><A href=#draw_box><TT>Fl_Widget::draw_box</TT></A></LI>
|
2001-11-29 22:24:00 +03:00
|
|
|
<LI><A href=#draw_focus><TT>Fl_Widget::draw_focus</TT></A></LI>
|
1999-01-27 00:36:02 +03:00
|
|
|
<LI><A href=#draw_label><TT>Fl_Widget::draw_label</TT></A></LI>
|
|
|
|
<LI><A href=#set_flag><TT>Fl_Widget::set_flag</TT></A></LI>
|
|
|
|
<LI><A href=#set_visible><TT>Fl_Widget::set_visible</TT></A></LI>
|
|
|
|
<LI><A href=#test_shortcut><TT>Fl_Widget::test_shortcut</TT></A></LI>
|
|
|
|
<LI><A href=#type><TT>Fl_Widget::type</TT></A></LI>
|
1999-01-13 22:28:54 +03:00
|
|
|
</UL>
|
1999-01-31 10:43:16 +03:00
|
|
|
<H4><A name=damage>void Fl_Widget::damage(uchar mask)
|
1999-01-13 22:28:54 +03:00
|
|
|
<BR> void Fl_Widget::damage(uchar mask, int x, int y, int w, int h)
|
1999-01-31 10:43:16 +03:00
|
|
|
<BR> uchar Fl_Widget::damage()</A></H4>
|
2001-11-29 22:24:00 +03:00
|
|
|
The first form indicates that a partial update of the object is
|
1999-01-13 22:28:54 +03:00
|
|
|
needed. The bits in mask are OR'd into <TT>damage()</TT>. Your <TT>
|
1999-01-19 23:53:39 +03:00
|
|
|
draw()</TT> routine can examine these bits to limit what it is
|
|
|
|
drawing. The public method <TT>Fl_Widget::redraw()</TT> simply does
|
|
|
|
<TT> Fl_Widget::damage(FL_DAMAGE_ALL)</TT>, but the implementation of
|
2001-11-29 22:24:00 +03:00
|
|
|
your widget can call the private <TT>damage(n)</TT>.
|
|
|
|
<P>The second form indicates that a region is damaged. If only these
|
|
|
|
calls are done in a window (no calls to <TT>damage(n)</TT>) then FLTK
|
|
|
|
will clip to the union of all these calls before drawing anything.
|
|
|
|
This can greatly speed up incremental displays. The mask bits are
|
1999-01-27 00:36:02 +03:00
|
|
|
OR'd into <TT>damage()</TT> unless this is a <TT>Fl_Window</TT> widget. </P>
|
1999-01-13 22:28:54 +03:00
|
|
|
<P>The third form returns the bitwise-OR of all <TT>damage(n)</TT>
|
1999-01-19 23:53:39 +03:00
|
|
|
calls done since the last <TT>draw()</TT>.</P>
|
1999-01-27 00:36:02 +03:00
|
|
|
<P><I>When redrawing your widgets you should look at the damage bits to
|
|
|
|
see what parts of your widget need redrawing.</I> The <tt>handle()</tt>
|
|
|
|
method can then set individual damage bits to limit the amount of drawing
|
|
|
|
that needs to be done:
|
|
|
|
<UL><PRE>
|
|
|
|
MyClass::handle(int event) {
|
|
|
|
...
|
|
|
|
if (change_to_part1) damage(1);
|
|
|
|
if (change_to_part2) damage(2);
|
|
|
|
if (change_to_part3) damage(4);
|
|
|
|
}
|
|
|
|
|
|
|
|
MyClass::draw() {
|
2001-05-06 20:25:05 +04:00
|
|
|
if (damage() & FL_DAMAGE_ALL) {
|
1999-01-27 00:36:02 +03:00
|
|
|
... draw frame/box and other static stuff ...
|
|
|
|
}
|
|
|
|
|
2001-05-06 20:25:05 +04:00
|
|
|
if (damage() & (FL_DAMAGE_ALL | 1)) draw_part1();
|
|
|
|
if (damage() & (FL_DAMAGE_ALL | 2)) draw_part2();
|
|
|
|
if (damage() & (FL_DAMAGE_ALL | 4)) draw_part3();
|
1999-01-27 00:36:02 +03:00
|
|
|
}
|
|
|
|
</PRE></UL>
|
1999-01-31 10:43:16 +03:00
|
|
|
<H4><A name=draw_box>void Fl_Widget::draw_box() const
|
|
|
|
<BR></A>void Fl_Widget::draw_box(Fl_Boxtype b, ulong c) const</H4>
|
2001-11-29 22:24:00 +03:00
|
|
|
The first form draws this widget's <TT>box()</TT>, using the
|
|
|
|
dimensions of the widget. The second form uses <TT>b</TT> as the box
|
|
|
|
type and <TT>c</TT> as the color for the box.
|
|
|
|
|
|
|
|
<H4><A name="draw_focus">void Fl_Widget::draw_focus() const
|
|
|
|
<BR>void Fl_Widget::draw_focus(Fl_Boxtype b, int x, int y, int w, int h) const</A></H4>
|
|
|
|
|
|
|
|
<P>Draws a focus box inside the widgets bounding box. The second
|
|
|
|
form allows you to specify a different bounding box.
|
|
|
|
|
1999-01-31 10:43:16 +03:00
|
|
|
<H4><A name=draw_label>void Fl_Widget::draw_label() const
|
1999-01-13 22:28:54 +03:00
|
|
|
<BR> void Fl_Widget::draw_label(int x, int y, int w, int h) const
|
2001-11-29 22:24:00 +03:00
|
|
|
<BR> void Fl_Widget::draw_label(int x, int y, int w, int h, Fl_Align
|
1999-01-31 10:43:16 +03:00
|
|
|
align) const</A></H4>
|
2001-11-29 22:24:00 +03:00
|
|
|
This is the usual function for a <TT>draw()</TT> method to call to
|
|
|
|
draw the widget's label. It does not draw the label if it is supposed
|
|
|
|
to be outside the box (on the assumption that the enclosing group will
|
|
|
|
draw those labels).
|
|
|
|
<P>The second form uses the passed bounding box instead of the widget's
|
|
|
|
bounding box. This is useful so "centered" labels are aligned with some
|
1999-01-27 00:36:02 +03:00
|
|
|
feature, like a moving slider. </P>
|
1999-01-13 22:28:54 +03:00
|
|
|
<P>The third form draws the label anywhere. It acts as though <TT>
|
2001-11-29 22:24:00 +03:00
|
|
|
FL_ALIGN_INSIDE</TT> has been forced on so the label will appear inside
|
|
|
|
the passed bounding box. This is designed for parent groups to draw
|
1999-01-13 22:28:54 +03:00
|
|
|
labels with. </P>
|
1999-01-31 10:43:16 +03:00
|
|
|
<H4><A name=set_flag>void Fl_Widget::set_flag(SHORTCUT_LABEL)</A></H4>
|
1999-01-27 00:36:02 +03:00
|
|
|
Modifies <TT>draw_label()</TT> so that '&' characters cause an underscore
|
2001-11-29 22:24:00 +03:00
|
|
|
to be printed under the next letter.
|
1999-01-31 10:43:16 +03:00
|
|
|
<H4><A name=set_visible>void Fl_Widget::set_visible()</A>
|
|
|
|
<BR><A name=clear_visible>void Fl_Widget::clear_visible()</A></H4>
|
1999-01-13 22:28:54 +03:00
|
|
|
Fast inline versions of <TT>Fl_Widget::hide()</TT> and <TT>
|
|
|
|
Fl_Widget::show()</TT>. These do not send the <TT>FL_HIDE</TT> and <TT>
|
2001-11-29 22:24:00 +03:00
|
|
|
FL_SHOW</TT> events to the widget.
|
1999-01-31 10:43:16 +03:00
|
|
|
<H4><A name=test_shortcut>int Fl_Widget::test_shortcut() const
|
|
|
|
<BR> static int Fl_Widget::test_shortcut(const char *s)</A></H4>
|
2001-11-29 22:24:00 +03:00
|
|
|
The first version tests <TT>Fl_Widget::label()</TT> against the
|
|
|
|
current event (which should be a <TT>FL_SHORTCUT</TT> event). If the
|
|
|
|
label contains a '&' character and the character after it matches the key
|
1999-01-13 22:28:54 +03:00
|
|
|
press, this returns true. This returns false if the <TT>SHORTCUT_LABEL</TT>
|
2001-11-29 22:24:00 +03:00
|
|
|
flag is off, if the label is <TT>NULL</TT> or does not have a
|
|
|
|
'&' character in it, or if the keypress does not match the character.
|
|
|
|
<P>The second version lets you do this test against an arbitrary
|
1999-01-13 22:28:54 +03:00
|
|
|
string. </P>
|
1999-01-31 10:43:16 +03:00
|
|
|
<H4><A name=type>uchar Fl_Widget::type() const
|
|
|
|
<BR> void Fl_Widget::type(uchar t)</A></H4>
|
2001-11-29 22:24:00 +03:00
|
|
|
The property <TT>Fl_Widget::type()</TT> can return an arbitrary 8-bit
|
1999-01-13 22:28:54 +03:00
|
|
|
identifier, and can be set with the protected method <TT>type(uchar t)</TT>
|
2001-11-29 22:24:00 +03:00
|
|
|
. This value had to be provided for Forms compatibility, but you can
|
|
|
|
use it for any purpose you want. Try to keep the value less than 100
|
|
|
|
to not interfere with reserved values.
|
|
|
|
<P>FLTK does not use RTTI (Run Time Typing Infomation), to enhance
|
|
|
|
portability. But this may change in the near future if RTTI becomes
|
1999-01-13 22:28:54 +03:00
|
|
|
standard everywhere. </P>
|
2001-11-29 22:24:00 +03:00
|
|
|
<P>If you don't have RTTI you can use the clumsy FLTK mechanisim, by
|
|
|
|
having <TT>type()</TT> use a unique value. These unique values must
|
|
|
|
be greater than the symbol <TT>FL_RESERVED_TYPE</TT> (which is 100).
|
|
|
|
Look through the header files for <TT>FL_RESERVED_TYPE</TT> to find an
|
1999-01-19 23:53:39 +03:00
|
|
|
unused number. If you make a subclass of <TT>Fl_Window</TT>
|
2001-11-29 22:24:00 +03:00
|
|
|
you must use <TT>FL_WINDOW + n</TT> (<TT>n</tt> must be in the
|
1999-01-19 23:53:39 +03:00
|
|
|
range 1 to 7). </P>
|
2001-10-01 00:25:36 +04:00
|
|
|
<H2><A NAME="handle">Handling Events</A></H2>
|
2001-11-29 22:24:00 +03:00
|
|
|
The virtual method <TT>int Fl_Widget::handle(int event)</TT> is called
|
|
|
|
to handle each event passed to the widget. It can:
|
1999-01-13 22:28:54 +03:00
|
|
|
<UL>
|
|
|
|
<LI>Change the state of the widget. </LI>
|
|
|
|
<LI>Call <A href=Fl_Widget.html#Fl_Widget.redraw><TT>Fl_Widget::redraw()</TT>
|
|
|
|
</A> if the widget needs to be redisplayed. </LI>
|
|
|
|
<LI>Call <A href=Fl_Widget.html#Fl_Widget.damage><TT>
|
2001-11-29 22:24:00 +03:00
|
|
|
Fl_Widget::damage(n)</TT></A> if the widget needs a partial-update
|
2001-10-01 00:25:36 +04:00
|
|
|
(assuming you provide support for this in your <A HREF="#draw"><TT>Fl_Widget::draw()</TT></A>
|
1999-01-13 22:28:54 +03:00
|
|
|
method). </LI>
|
|
|
|
<LI>Call <A href=Fl_Widget.html#Fl_Widget.do_callback><TT>
|
|
|
|
Fl_Widget::do_callback()</TT></A> if a callback should be generated. </LI>
|
|
|
|
<LI>Call <TT>Fl_Widget::handle()</TT> on child widgets. </LI>
|
|
|
|
</UL>
|
2001-11-29 22:24:00 +03:00
|
|
|
Events are identified by the integer argument. Other information
|
|
|
|
about the most recent event is stored in static locations and aquired
|
1999-01-13 22:28:54 +03:00
|
|
|
by calling the <A href=events.html#events><TT>Fl::event_*()</TT></A>
|
2001-11-29 22:24:00 +03:00
|
|
|
functions. This information remains valid until another event is
|
|
|
|
handled.
|
|
|
|
<P>Here is a sample <TT>handle()</TT> method for a widget that acts as
|
1999-01-13 22:28:54 +03:00
|
|
|
a pushbutton and also accepts the keystroke 'x' to cause the callback: </P>
|
2001-11-29 22:24:00 +03:00
|
|
|
<UL><PRE>
|
1999-01-07 19:36:11 +03:00
|
|
|
int MyClass::handle(int event) {
|
1998-12-29 17:21:17 +03:00
|
|
|
switch(event) {
|
|
|
|
case FL_PUSH:
|
1999-01-07 19:36:11 +03:00
|
|
|
highlight = 1;
|
|
|
|
redraw();
|
1998-12-29 17:21:17 +03:00
|
|
|
return 1;
|
1999-01-07 19:36:11 +03:00
|
|
|
case FL_DRAG: {
|
|
|
|
int t = Fl::event_inside(this);
|
|
|
|
if (t != highlight) {
|
|
|
|
highlight = t;
|
|
|
|
redraw();
|
|
|
|
}
|
|
|
|
}
|
1998-12-29 17:21:17 +03:00
|
|
|
return 1;
|
|
|
|
case FL_RELEASE:
|
|
|
|
if (highlight) {
|
1999-01-07 19:36:11 +03:00
|
|
|
highlight = 0;
|
|
|
|
redraw();
|
1998-12-29 17:21:17 +03:00
|
|
|
do_callback();
|
1999-01-07 19:36:11 +03:00
|
|
|
// never do anything after a callback, as the callback
|
1998-12-29 17:21:17 +03:00
|
|
|
// may delete the widget!
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
case FL_SHORTCUT:
|
1999-01-07 19:36:11 +03:00
|
|
|
if (Fl::event_key() == 'x') {
|
|
|
|
do_callback();
|
|
|
|
return 1;
|
|
|
|
}
|
1998-12-29 17:21:17 +03:00
|
|
|
return 0;
|
|
|
|
default:
|
2000-03-15 21:57:07 +03:00
|
|
|
return Fl_Widget::handle(event);
|
1998-12-29 17:21:17 +03:00
|
|
|
}
|
|
|
|
}
|
2001-11-29 22:24:00 +03:00
|
|
|
</PRE></UL>
|
|
|
|
|
|
|
|
<P>You must return non-zero if your <TT>handle()</TT> method
|
|
|
|
uses the event. If you return zero, the parent widget will try
|
|
|
|
sending the event to another widget.
|
|
|
|
|
2001-10-01 00:25:36 +04:00
|
|
|
<H2><A NAME="draw">Drawing the Widget</A></H2>
|
2001-11-29 22:24:00 +03:00
|
|
|
|
|
|
|
<P>The <TT>draw()</TT> virtual method is called when FLTK wants
|
|
|
|
you to redraw your widget. It will be called if and only if
|
|
|
|
<TT>damage()</TT> is non-zero, and <TT>damage()</TT> will be
|
|
|
|
cleared to zero after it returns. The <TT>draw()</TT> method
|
|
|
|
should be declared protected so that it can't be called from
|
|
|
|
non-drawing code.
|
|
|
|
|
|
|
|
<P>The <TT>damage()</TT> value contains the bitwise-OR of all
|
|
|
|
the <TT>damage(n)</TT> calls to this widget since it was last
|
|
|
|
drawn. This can be used for minimal update, by only redrawing
|
|
|
|
the parts whose bits are set. FLTK will turn on the
|
|
|
|
<TT>FL_DAMAGE_ALL</TT> bit if it thinks the entire widget must
|
|
|
|
be redrawn, e.g. for an expose event. </P>
|
|
|
|
|
1999-01-13 22:28:54 +03:00
|
|
|
<P>Expose events (and the above <TT>damage(b,x,y,w,h)</TT>) will cause <TT>
|
|
|
|
draw()</TT> to be called with FLTK's <A href=drawing.html#clipping>
|
2001-11-29 22:24:00 +03:00
|
|
|
clipping</A> turned on. You can greatly speed up redrawing in some
|
|
|
|
cases by testing <TT>fl_not_clipped(x,y,w,h)</TT> or <TT>fl_clip_box(...)</TT> and
|
1999-01-13 22:28:54 +03:00
|
|
|
skipping invisible parts. </P>
|
2001-11-29 22:24:00 +03:00
|
|
|
<P>Besides the protected methods described above, FLTK provides a large
|
1999-01-13 22:28:54 +03:00
|
|
|
number of basic drawing functions, which are described <A href=drawing.html#drawing>
|
|
|
|
below</A>. </P>
|
1999-01-07 19:36:11 +03:00
|
|
|
<H2>Resizing the Widget</H2>
|
2001-11-29 22:24:00 +03:00
|
|
|
The <TT>resize(int x, int y, int w, int h)</TT> method is called when
|
|
|
|
the widget is being resized or moved. The arguments are the new
|
|
|
|
position, width, and height. <TT>x()</TT>, <TT>y()</TT>, <TT>w()</TT>,
|
1999-01-13 22:28:54 +03:00
|
|
|
and <TT>h()</TT> still remain the old size. You must call <TT>resize()</TT>
|
2001-11-29 22:24:00 +03:00
|
|
|
on your base class with the same arguments to get the widget size to
|
|
|
|
actually change.
|
1999-01-13 22:28:54 +03:00
|
|
|
<P>This should <I>not</I> call <TT>redraw()</TT>, at least if only the <TT>
|
1999-01-27 00:36:02 +03:00
|
|
|
x()</TT> and <TT>y()</TT> change. This is because composite widgets like <A href=Fl_Scroll.html#Fl_Scroll>
|
2001-11-29 22:24:00 +03:00
|
|
|
<TT>Fl_Scroll</TT></A> may have a more efficient way of drawing the new
|
1999-01-13 22:28:54 +03:00
|
|
|
position. </P>
|
1999-01-27 00:36:02 +03:00
|
|
|
<H2>Making a Composite Widget</H2>
|
|
|
|
A "composite" widget contains one or more "child" widgets.
|
|
|
|
To make a composite widget you should subclass <A href=Fl_Group.html#Fl_Group><TT>Fl_Group</TT></A>
|
1999-01-13 22:28:54 +03:00
|
|
|
. It is possible to make a composite object that is not a subclass of <TT>
|
|
|
|
Fl_Group</TT>, but you'll have to duplicate the code in <TT>Fl_Group</TT>
|
2001-11-29 22:24:00 +03:00
|
|
|
anyways.
|
1999-01-13 22:28:54 +03:00
|
|
|
<P>Instances of the child widgets may be included in the parent: </P>
|
2001-11-29 22:24:00 +03:00
|
|
|
<UL><PRE>
|
1999-01-07 19:36:11 +03:00
|
|
|
class MyClass : public Fl_Group {
|
1998-12-29 17:21:17 +03:00
|
|
|
Fl_Button the_button;
|
|
|
|
Fl_Slider the_slider;
|
|
|
|
...
|
|
|
|
};
|
2001-11-29 22:24:00 +03:00
|
|
|
</PRE></UL>
|
|
|
|
The constructor has to initialize these instances. They are
|
1999-01-13 22:28:54 +03:00
|
|
|
automatically <TT>add()</TT>ed to the group, since the <TT>Fl_Group</TT>
|
|
|
|
constructor does <TT>begin()</TT>. <I>Don't forget to call <TT>end()</TT>
|
|
|
|
or use the <A href=Fl_End.html#Fl_End><TT>Fl_End</TT></A> pseudo-class:</I>
|
2001-11-29 22:24:00 +03:00
|
|
|
<UL><PRE>
|
1999-01-07 19:36:11 +03:00
|
|
|
MyClass::MyClass(int x, int y, int w, int h) :
|
|
|
|
Fl_Group(x, y, w, h),
|
|
|
|
the_button(x + 5, y + 5, 100, 20),
|
|
|
|
the_slider(x, y + 50, w, 20)
|
1998-12-29 17:21:17 +03:00
|
|
|
{
|
|
|
|
...(you could add dynamically created child widgets here)...
|
|
|
|
end(); // don't forget to do this!
|
|
|
|
}
|
2001-11-29 22:24:00 +03:00
|
|
|
</PRE></UL>
|
|
|
|
The child widgets need callbacks. These will be called with a pointer
|
1999-01-13 22:28:54 +03:00
|
|
|
to the children, but the widget itself may be found in the <TT>parent()</TT>
|
2001-11-29 22:24:00 +03:00
|
|
|
pointer of the child. Usually these callbacks can be static private
|
|
|
|
methods, with a matching private method:
|
|
|
|
<UL><PRE>
|
|
|
|
void MyClass::static_slider_cb(Fl_Widget* v, void *) { // static method
|
1999-01-13 22:28:54 +03:00
|
|
|
((MyClass*)(v->parent())->slider_cb();
|
1998-12-29 17:21:17 +03:00
|
|
|
}
|
|
|
|
void MyClass::slider_cb() { // normal method
|
1999-01-13 22:28:54 +03:00
|
|
|
use(the_slider->value());
|
1998-12-29 17:21:17 +03:00
|
|
|
}
|
2001-11-29 22:24:00 +03:00
|
|
|
</PRE></UL>
|
|
|
|
If you make the <TT>handle()</TT> method, you can quickly pass all the
|
|
|
|
events to the children using the <TT>Fl_Group::handle()</TT> method.
|
1999-01-19 23:53:39 +03:00
|
|
|
You don't need to override <TT>handle()</TT> if your composite widget
|
2001-11-29 22:24:00 +03:00
|
|
|
does nothing other than pass events to the children:
|
|
|
|
<UL><PRE>
|
1999-01-07 19:36:11 +03:00
|
|
|
int MyClass::handle(int event) {
|
1998-12-29 17:21:17 +03:00
|
|
|
if (Fl_Group::handle(event)) return 1;
|
|
|
|
... handle events that children don't want ...
|
|
|
|
}
|
2001-11-29 22:24:00 +03:00
|
|
|
</PRE></UL>
|
|
|
|
|
|
|
|
<P>If you override <TT>draw()</TT> you need to draw all the
|
|
|
|
children. If <TT>redraw()</TT> or <TT>damage()</TT> is called
|
|
|
|
on a child, <TT>damage(FL_DAMAGE_CHILD)</TT> is done to the
|
|
|
|
group, so this bit of <TT>damage()</TT> can be used to indicate
|
|
|
|
that a child needs to be drawn. It is fastest if you avoid
|
|
|
|
drawing anything else in this case:
|
|
|
|
|
|
|
|
<UL><PRE>
|
1999-01-07 19:36:11 +03:00
|
|
|
int MyClass::draw() {
|
|
|
|
Fl_Widget *const*a = array();
|
|
|
|
if (damage() == FL_DAMAGE_CHILD) { // only redraw some children
|
|
|
|
for (int i = children(); i --; a ++) update_child(**a);
|
1998-12-29 17:21:17 +03:00
|
|
|
} else { // total redraw
|
|
|
|
... draw background graphics ...
|
|
|
|
// now draw all the children atop the background:
|
1999-01-07 19:36:11 +03:00
|
|
|
for (int i = children_; i --; a ++) {
|
1998-12-29 17:21:17 +03:00
|
|
|
draw_child(**a);
|
2001-11-29 22:24:00 +03:00
|
|
|
draw_outside_label(**a); // you may not need to do this
|
1998-12-29 17:21:17 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2001-11-29 22:24:00 +03:00
|
|
|
</PRE></UL>
|
|
|
|
<TT>Fl_Group</TT> provides some protected methods to make drawing
|
|
|
|
easier:
|
1999-01-13 22:28:54 +03:00
|
|
|
<UL>
|
|
|
|
<LI><A href=#draw_child>draw_child</A></LI>
|
|
|
|
<LI><A href=#draw_outside_label>draw_outside_label</A></LI>
|
|
|
|
<LI><A href=#update_child>update_child</A></LI>
|
|
|
|
</UL>
|
1999-01-31 10:43:16 +03:00
|
|
|
<H4><A name=draw_child>void Fl_Group::draw_child(Fl_Widget&)</A></H4>
|
1999-01-13 22:28:54 +03:00
|
|
|
This will force the child's <TT>damage()</TT> bits all to one and call <TT>
|
2001-11-29 22:24:00 +03:00
|
|
|
draw()</TT> on it, then clear the <TT>damage()</TT>. You should call
|
|
|
|
this on all children if a total redraw of your widget is requested, or
|
|
|
|
if you draw something (like a background box) that damages the child.
|
|
|
|
Nothing is done if the child is not <TT>visible()</TT> or if it is
|
|
|
|
clipped.
|
|
|
|
<H4><A name=draw_outside_label>void
|
1999-01-31 10:43:16 +03:00
|
|
|
Fl_Group::draw_outside_label(Fl_Widget&) const</A></H4>
|
1999-01-13 22:28:54 +03:00
|
|
|
Draw the labels that are <I>not</I> drawn by <A href=#draw_label><TT>
|
2001-11-29 22:24:00 +03:00
|
|
|
draw_label()</TT></A>. If you want more control over the label
|
|
|
|
positions you might want to call <TT>child->draw_label(x,y,w,h,a)</TT>.
|
1999-01-31 10:43:16 +03:00
|
|
|
<H4><A name=update_child>void Fl_Group::update_child(Fl_Widget&)</A></H4>
|
2001-11-29 22:24:00 +03:00
|
|
|
Draws the child only if its <TT>damage()</TT> is non-zero. You
|
|
|
|
should call this on all the children if your own damage is equal to
|
1999-01-13 22:28:54 +03:00
|
|
|
FL_DAMAGE_CHILD. Nothing is done if the child is not <TT>visible()</TT>
|
2001-11-29 22:24:00 +03:00
|
|
|
or if it is clipped.
|
2002-02-25 00:09:51 +03:00
|
|
|
|
1999-01-07 19:36:11 +03:00
|
|
|
<H2>Cut and Paste Support</H2>
|
2001-11-29 22:24:00 +03:00
|
|
|
FLTK provides routines to cut and paste 8-bit text (in the future this
|
|
|
|
may be UTF-8) between applications:
|
1999-01-13 22:28:54 +03:00
|
|
|
<UL>
|
2001-11-29 22:24:00 +03:00
|
|
|
<LI><A href="Fl.html#Fl.paste"><TT>Fl::paste</TT></A></LI>
|
|
|
|
<LI><A href="Fl.html#Fl.selection"><TT>Fl::selection</TT></A></LI>
|
|
|
|
<LI><A href="Fl.html#Fl.selection_owner"><TT>Fl::selection_owner</TT></A></LI>
|
1999-01-13 22:28:54 +03:00
|
|
|
</UL>
|
2000-04-28 22:15:26 +04:00
|
|
|
It may be possible to cut/paste non-text data by using <A href=osissues.html#add_handler>
|
2001-11-29 22:24:00 +03:00
|
|
|
<TT>Fl::add_handler()</TT></A>.
|
2002-02-25 00:09:51 +03:00
|
|
|
|
|
|
|
<H2>Drag And Drop Support</H2>
|
|
|
|
|
|
|
|
FLTK provides routines to drag and drop 8-bit text between applications:
|
|
|
|
|
|
|
|
<P>Drag'n'drop operations are are initiated by copying data to the
|
|
|
|
clipboard and calling the function
|
|
|
|
<A href="Fl.html#Fl.dnd"><TT>Fl::dnd()</TT></A>.
|
|
|
|
|
|
|
|
<P>Drop attempts are handled via <A href="events.html#dnd">events</A>:
|
|
|
|
<UL>
|
|
|
|
<LI><TT>FL_DND_ENTER</TT></LI>
|
|
|
|
<LI><TT>FL_DND_DRAG</TT></LI>
|
|
|
|
<LI><TT>FL_DND_LEAVE</TT></LI>
|
|
|
|
<LI><TT>FL_DND_RELEASE</TT></LI>
|
|
|
|
<LI><TT>FL_PASTE</TT></LI>
|
|
|
|
</UL>
|
|
|
|
|
1999-01-07 19:36:11 +03:00
|
|
|
<H2>Making a subclass of Fl_Window</H2>
|
2001-11-29 22:24:00 +03:00
|
|
|
|
|
|
|
<P>You may want your widget to be a subclass of
|
|
|
|
<TT>Fl_Window</TT>, <TT>Fl_Double_Window</TT>, or
|
|
|
|
<TT>FL_Gl_Window</TT>. This can be useful if your widget wants
|
|
|
|
to occupy an entire window, and can also be used to take
|
|
|
|
advantage of system-provided clipping, or to work with a library
|
|
|
|
that expects a system window ID to indicate where to draw.
|
|
|
|
|
|
|
|
<P>Subclassing <TT>Fl_Window</TT>is almost exactly like
|
|
|
|
subclassing <TT>Fl_Group</TT>, and in fact you can easily
|
|
|
|
switch a subclass back and forth. Watch out for the following
|
|
|
|
differences: </P>
|
|
|
|
|
1999-01-13 22:28:54 +03:00
|
|
|
<OL>
|
2001-11-29 22:24:00 +03:00
|
|
|
|
|
|
|
<LI><TT>Fl_Window</TT> is a subclass of
|
|
|
|
<TT>Fl_Group</TT> so <I>make sure your constructor calls
|
|
|
|
<TT>end()</TT></I> unless you actually want children
|
|
|
|
added to your window.</LI>
|
|
|
|
|
|
|
|
<LI>When handling events and drawing, the upper-left
|
|
|
|
corner is at 0,0, not <TT>x(),y()</TT> as in other
|
|
|
|
<TT>Fl_Widget</TT>'s. For instance, to draw a box
|
|
|
|
around the widget, call <TT>draw_box(0, 0, w(),
|
|
|
|
h())</TT>, rather than <TT>draw_box(x(), y(), w(),
|
|
|
|
h())</TT>.</LI>
|
|
|
|
|
1999-01-13 22:28:54 +03:00
|
|
|
</OL>
|
2001-11-29 22:24:00 +03:00
|
|
|
|
|
|
|
<P>You may also want to subclass <TT>Fl_Window</TT> in order to
|
|
|
|
get access to different visuals or to change other attributes of
|
|
|
|
the windows. See <A href="osissues.html">"Appendix F - Operating
|
|
|
|
System Issues"</A> for more information.
|
|
|
|
|
|
|
|
</BODY>
|
|
|
|
</HTML>
|