1998-10-20 00:46:58 +04:00
|
|
|
//
|
|
|
|
// Button widget for the Fast Light Tool Kit (FLTK).
|
|
|
|
//
|
2014-10-20 00:17:17 +04:00
|
|
|
// Copyright 1998-2014 by Bill Spitzak and others.
|
1998-10-20 00:46:58 +04:00
|
|
|
//
|
2011-07-19 08:49:30 +04:00
|
|
|
// This library is free software. Distribution and use rights are outlined in
|
2014-10-20 00:17:17 +04:00
|
|
|
// the file "COPYING" which should have been included with this file. If this
|
2011-07-19 08:49:30 +04:00
|
|
|
// file is missing or damaged, see the license at:
|
1998-10-20 00:46:58 +04:00
|
|
|
//
|
2020-07-01 19:03:10 +03:00
|
|
|
// https://www.fltk.org/COPYING.php
|
1998-10-20 00:46:58 +04:00
|
|
|
//
|
2020-07-01 19:03:10 +03:00
|
|
|
// Please see the following page on how to report bugs and issues:
|
2005-04-16 04:13:17 +04:00
|
|
|
//
|
2020-07-01 19:03:10 +03:00
|
|
|
// https://www.fltk.org/bugs.php
|
1998-10-20 00:46:58 +04:00
|
|
|
//
|
1998-10-06 22:21:25 +04:00
|
|
|
|
|
|
|
#include <FL/Fl.H>
|
|
|
|
#include <FL/Fl_Button.H>
|
|
|
|
#include <FL/Fl_Group.H>
|
2001-12-16 15:37:03 +03:00
|
|
|
#include <FL/Fl_Window.H>
|
2023-09-03 01:09:32 +03:00
|
|
|
#include <FL/fl_draw.H>
|
1998-10-06 22:21:25 +04:00
|
|
|
|
2012-07-24 08:37:22 +04:00
|
|
|
#include <FL/Fl_Radio_Button.H>
|
|
|
|
#include <FL/Fl_Toggle_Button.H>
|
|
|
|
|
2010-11-14 14:02:18 +03:00
|
|
|
|
|
|
|
Fl_Widget_Tracker *Fl_Button::key_release_tracker = 0;
|
|
|
|
|
|
|
|
|
1998-10-20 00:46:58 +04:00
|
|
|
// There are a lot of subclasses, named Fl_*_Button. Some of
|
|
|
|
// them are implemented by setting the type() value and testing it
|
|
|
|
// here. This includes Fl_Radio_Button and Fl_Toggle_Button
|
|
|
|
|
2008-09-16 10:49:08 +04:00
|
|
|
/**
|
2024-01-17 20:54:01 +03:00
|
|
|
Sets the current value of the button.
|
|
|
|
A non-zero value sets the button to 1 (ON), and zero sets it to 0 (OFF).
|
|
|
|
\param[in] v button value.
|
|
|
|
\see set(), clear()
|
2008-09-16 10:49:08 +04:00
|
|
|
*/
|
1998-10-06 22:21:25 +04:00
|
|
|
int Fl_Button::value(int v) {
|
|
|
|
v = v ? 1 : 0;
|
|
|
|
oldval = v;
|
|
|
|
clear_changed();
|
2002-06-02 21:52:36 +04:00
|
|
|
if (value_ != v) {
|
|
|
|
value_ = v;
|
2006-09-23 19:28:09 +04:00
|
|
|
if (box()) redraw();
|
|
|
|
else redraw_label();
|
2002-06-02 21:52:36 +04:00
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
|
|
|
|
2008-09-16 10:49:08 +04:00
|
|
|
/**
|
2024-01-17 20:54:01 +03:00
|
|
|
Turns on this button and turns off all other radio buttons in the group
|
|
|
|
(calling \c value(1) or \c set() does not do this).
|
2008-09-16 10:49:08 +04:00
|
|
|
*/
|
1998-10-06 22:21:25 +04:00
|
|
|
void Fl_Button::setonly() { // set this radio button on, turn others off
|
|
|
|
value(1);
|
2010-02-21 00:14:47 +03:00
|
|
|
Fl_Group* g = parent();
|
1998-10-06 22:21:25 +04:00
|
|
|
Fl_Widget*const* a = g->array();
|
|
|
|
for (int i = g->children(); i--;) {
|
|
|
|
Fl_Widget* o = *a++;
|
|
|
|
if (o != this && o->type()==FL_RADIO_BUTTON) ((Fl_Button*)o)->value(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Fl_Button::draw() {
|
Buttons with box(FL_NO_BOX) did not draw. Apparently they did in
older versions of fltk, I restored this. (bug 108771)
Removed 8-bit colormap drawing code that was not doing anything in
fl_draw_image due to Mike's changes. I also made fl_color(r,g,b)
actually allocate the requested color rather than the nearest fltk
color-cube color (this is only done for the first color that maps to a
given entry in the fltk color cube), the result is that pixmaps with a
small number of colors are drawn much more accurately. The resulting
code seems to produce better images and is a good deal smaller!
Fixed makeinclude.in so CFLAGS are used for c source code instead of
CXXFLAGS. (bug 108694)
Better fix for gif files suggested by pauly (bug 108770)
Performance of Fl_Gl_Window may be improved on some types of OpenGL
implementations, in particular MESA or other software emulators, by
setting the GL_SWAP_TYPE environment variable. This variable
declares what is in the back buffer after you do a swapbuffers.
setenv GL_SWAP_TYPE COPY
This indicates that the back buffer is copied to the front buffer,
and still contains it's old data. This is true of many hardware
implementations. Setting this will speed up emulation of
overlays, and widgets that can do partial update can take
advantage of this as damage() will not be cleared to -1.
setenv GL_SWAP_TYPE NODAMAGE
This indicates that nothing changes the back buffer except drawing
into it. This is true of MESA and Win32 software emulation and
perhaps some hardware emulation on systems with lots of memory.
All other values for GL_SWAP_TYPE, and not setting the variable,
cause fltk to assumme that the back buffer must be completely
redrawn after a swap.
This is easily tested by running the gl_overlay demo program and
seeing if the display is correct when you drag another window over
it or if you drag the window off the screen and back on. You have to
exit and run the program again for it to see any changes to the
environment variable.
git-svn-id: file:///fltk/svn/fltk/branches/branch-1.0@1246 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
2000-07-07 12:38:58 +04:00
|
|
|
if (type() == FL_HIDDEN_BUTTON) return;
|
1998-10-06 22:21:25 +04:00
|
|
|
Fl_Color col = value() ? selection_color() : color();
|
2023-09-03 01:09:32 +03:00
|
|
|
Fl_Boxtype bt = value() ? (down_box()?down_box():fl_down(box())) : box();
|
|
|
|
if (compact_ && parent()) {
|
|
|
|
Fl_Widget *p = parent();
|
|
|
|
int px, py, pw = p->w(), ph = p->h();
|
|
|
|
if (p->as_window()) { px = 0; py = 0; } else { px = p->x(); py = p->y(); }
|
|
|
|
fl_push_clip(x(), y(), w(), h());
|
|
|
|
draw_box(bt, px, py, pw, ph, col);
|
|
|
|
fl_pop_clip();
|
|
|
|
const int hh = 5, ww = 5;
|
|
|
|
Fl_Color divider_color = fl_gray_ramp(FL_NUM_GRAY/3);
|
|
|
|
if (!active_r())
|
|
|
|
divider_color = fl_inactive(divider_color);
|
|
|
|
if (x()+w() != px+pw) {
|
|
|
|
fl_color(divider_color);
|
|
|
|
fl_yxline(x()+w()-1, y()+hh, y()+h()-1-hh);
|
|
|
|
}
|
|
|
|
if (y()+h() != py+ph) {
|
|
|
|
fl_color(divider_color);
|
|
|
|
fl_xyline(x()+ww, y()+h()-1, x()+w()-1-ww);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
draw_box(bt, col);
|
|
|
|
}
|
2010-04-10 02:18:05 +04:00
|
|
|
draw_backdrop();
|
2006-09-25 20:36:52 +04:00
|
|
|
if (labeltype() == FL_NORMAL_LABEL && value()) {
|
2006-09-17 18:58:25 +04:00
|
|
|
Fl_Color c = labelcolor();
|
|
|
|
labelcolor(fl_contrast(c, col));
|
|
|
|
draw_label();
|
|
|
|
labelcolor(c);
|
|
|
|
} else draw_label();
|
2001-08-05 00:17:10 +04:00
|
|
|
if (Fl::focus() == this) draw_focus();
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
2002-06-02 21:52:36 +04:00
|
|
|
|
1998-10-06 22:21:25 +04:00
|
|
|
int Fl_Button::handle(int event) {
|
|
|
|
int newval;
|
|
|
|
switch (event) {
|
2024-01-17 20:54:01 +03:00
|
|
|
case FL_ENTER: /* FALLTHROUGH */
|
|
|
|
case FL_LEAVE:
|
|
|
|
// if ((value_?selection_color():color())==FL_GRAY) redraw();
|
|
|
|
return 1;
|
|
|
|
case FL_PUSH:
|
|
|
|
if (Fl::visible_focus() && handle(FL_FOCUS)) Fl::focus(this);
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case FL_DRAG:
|
|
|
|
if (Fl::event_inside(this)) {
|
|
|
|
if (type() == FL_RADIO_BUTTON) newval = 1;
|
|
|
|
else newval = !oldval;
|
2024-01-14 00:36:28 +03:00
|
|
|
} else {
|
2024-01-17 20:54:01 +03:00
|
|
|
clear_changed();
|
|
|
|
newval = oldval;
|
|
|
|
}
|
|
|
|
if (newval != value_) {
|
|
|
|
value_ = newval;
|
|
|
|
set_changed();
|
|
|
|
redraw();
|
|
|
|
if (when() & FL_WHEN_CHANGED) do_callback(FL_REASON_CHANGED);
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
case FL_RELEASE:
|
|
|
|
if (value_ == oldval) {
|
|
|
|
if (when() & FL_WHEN_NOT_CHANGED) do_callback(FL_REASON_SELECTED);
|
|
|
|
return 1;
|
2010-02-20 20:40:07 +03:00
|
|
|
}
|
2024-01-14 00:36:28 +03:00
|
|
|
set_changed();
|
2024-01-17 20:54:01 +03:00
|
|
|
if (type() == FL_RADIO_BUTTON) setonly();
|
|
|
|
else if (type() == FL_TOGGLE_BUTTON) oldval = value_;
|
|
|
|
else {
|
|
|
|
value(oldval);
|
|
|
|
set_changed();
|
|
|
|
if (when() & FL_WHEN_CHANGED) {
|
|
|
|
Fl_Widget_Tracker wp(this);
|
|
|
|
do_callback(FL_REASON_CHANGED);
|
|
|
|
if (wp.deleted()) return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (when() & FL_WHEN_RELEASE) do_callback(FL_REASON_RELEASED);
|
2008-12-06 14:04:37 +03:00
|
|
|
return 1;
|
2024-01-17 20:54:01 +03:00
|
|
|
case FL_SHORTCUT:
|
|
|
|
if (!(shortcut() ?
|
|
|
|
Fl::test_shortcut(shortcut()) : test_shortcut())) return 0;
|
|
|
|
if (Fl::visible_focus() && handle(FL_FOCUS)) Fl::focus(this);
|
|
|
|
goto triggered_by_keyboard;
|
|
|
|
case FL_FOCUS :
|
|
|
|
case FL_UNFOCUS :
|
|
|
|
if (Fl::visible_focus()) {
|
|
|
|
if (box() == FL_NO_BOX) {
|
|
|
|
// Widgets with the FL_NO_BOX boxtype need a parent to
|
|
|
|
// redraw, since it is responsible for redrawing the
|
|
|
|
// background...
|
|
|
|
int X = x() > 0 ? x() - 1 : 0;
|
|
|
|
int Y = y() > 0 ? y() - 1 : 0;
|
|
|
|
if (window()) window()->damage(FL_DAMAGE_ALL, X, Y, w() + 2, h() + 2);
|
|
|
|
} else redraw();
|
|
|
|
return 1;
|
|
|
|
} else return 0;
|
|
|
|
/* NOTREACHED */
|
|
|
|
case FL_KEYBOARD :
|
|
|
|
if (Fl::focus() == this && Fl::event_key() == ' ' &&
|
|
|
|
!(Fl::event_state() & (FL_SHIFT | FL_CTRL | FL_ALT | FL_META))) {
|
2024-01-17 21:47:41 +03:00
|
|
|
triggered_by_keyboard: // from FL_SHORTCUT
|
2024-01-17 20:54:01 +03:00
|
|
|
if (type() == FL_RADIO_BUTTON) {
|
|
|
|
if (!value_) {
|
|
|
|
setonly();
|
2024-01-17 21:47:41 +03:00
|
|
|
set_changed();
|
|
|
|
if (when() & FL_WHEN_CHANGED)
|
|
|
|
do_callback(FL_REASON_CHANGED);
|
|
|
|
else if (when() & FL_WHEN_RELEASE)
|
|
|
|
do_callback(FL_REASON_RELEASED);
|
|
|
|
} else {
|
|
|
|
if (when() & FL_WHEN_NOT_CHANGED)
|
|
|
|
do_callback(FL_REASON_SELECTED);
|
2024-01-17 20:54:01 +03:00
|
|
|
}
|
|
|
|
} else if (type() == FL_TOGGLE_BUTTON) {
|
|
|
|
value(!value());
|
2024-01-17 21:47:41 +03:00
|
|
|
set_changed();
|
|
|
|
if (when() & FL_WHEN_CHANGED)
|
|
|
|
do_callback(FL_REASON_CHANGED);
|
|
|
|
else if (when() & FL_WHEN_RELEASE)
|
|
|
|
do_callback(FL_REASON_RELEASED);
|
2024-01-14 00:36:28 +03:00
|
|
|
} else {
|
2024-01-17 20:54:01 +03:00
|
|
|
simulate_key_action();
|
2024-01-17 21:47:41 +03:00
|
|
|
value(1);
|
|
|
|
if (when() & FL_WHEN_CHANGED) {
|
|
|
|
set_changed();
|
|
|
|
Fl_Widget_Tracker wp(this);
|
|
|
|
do_callback(FL_REASON_CHANGED);
|
|
|
|
if (wp.deleted()) return 1;
|
|
|
|
value(0);
|
|
|
|
set_changed();
|
|
|
|
do_callback(FL_REASON_RELEASED);
|
|
|
|
} else if (when() & FL_WHEN_RELEASE) {
|
|
|
|
value(0);
|
|
|
|
set_changed();
|
|
|
|
do_callback(FL_REASON_RELEASED);
|
|
|
|
}
|
2011-10-26 14:29:13 +04:00
|
|
|
}
|
2024-01-17 20:54:01 +03:00
|
|
|
return 1;
|
2008-12-06 14:02:03 +03:00
|
|
|
}
|
2024-01-17 20:54:01 +03:00
|
|
|
/* FALLTHROUGH */
|
|
|
|
default:
|
|
|
|
return 0;
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-14 14:02:18 +03:00
|
|
|
void Fl_Button::simulate_key_action()
|
|
|
|
{
|
|
|
|
if (key_release_tracker) {
|
|
|
|
Fl::remove_timeout(key_release_timeout, key_release_tracker);
|
|
|
|
key_release_timeout(key_release_tracker);
|
|
|
|
}
|
2014-10-20 00:17:17 +04:00
|
|
|
value(1);
|
2010-11-14 14:02:18 +03:00
|
|
|
redraw();
|
|
|
|
key_release_tracker = new Fl_Widget_Tracker(this);
|
|
|
|
Fl::add_timeout(0.15, key_release_timeout, key_release_tracker);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Fl_Button::key_release_timeout(void *d)
|
|
|
|
{
|
|
|
|
Fl_Widget_Tracker *wt = (Fl_Widget_Tracker*)d;
|
|
|
|
if (!wt)
|
|
|
|
return;
|
2014-10-20 00:17:17 +04:00
|
|
|
if (wt==key_release_tracker)
|
2010-11-14 14:02:18 +03:00
|
|
|
key_release_tracker = 0L;
|
|
|
|
Fl_Button *btn = (Fl_Button*)wt->widget();
|
|
|
|
if (btn) {
|
|
|
|
btn->value(0);
|
|
|
|
btn->redraw();
|
|
|
|
}
|
|
|
|
delete wt;
|
|
|
|
}
|
|
|
|
|
2008-09-16 10:49:08 +04:00
|
|
|
/**
|
2024-01-17 20:54:01 +03:00
|
|
|
The constructor creates the button using the given position, size, and label.
|
2024-01-14 00:36:28 +03:00
|
|
|
|
2024-01-17 20:54:01 +03:00
|
|
|
The default box type is box(FL_UP_BOX).
|
2024-01-14 00:36:28 +03:00
|
|
|
|
2024-01-17 20:54:01 +03:00
|
|
|
You can control how the button is drawn when ON by setting down_box().
|
|
|
|
The default is FL_NO_BOX (0) which will select an appropriate box type
|
|
|
|
using the normal (OFF) box type by using fl_down(box()).
|
2024-01-14 00:36:28 +03:00
|
|
|
|
2024-01-17 20:54:01 +03:00
|
|
|
Derived classes may handle this differently.
|
2024-01-14 00:36:28 +03:00
|
|
|
|
2024-01-17 20:54:01 +03:00
|
|
|
A button may reequest callbacks with \p whne() \p FL_WHEN_CHANGED,
|
|
|
|
\p FL_WHEN_NOT_CHANGED, and \p FL_WHEN_RELEASE, triggering the callback
|
|
|
|
reasons \p FL_REASON_CHANGED, \p FL_REASON_SELECTED,
|
|
|
|
and \p FL_REASON_DESELECTED.
|
2023-01-05 15:51:30 +03:00
|
|
|
|
2024-01-17 20:54:01 +03:00
|
|
|
\param[in] X, Y, W, H position and size of the widget
|
|
|
|
\param[in] L widget label, default is no label
|
2008-09-16 10:49:08 +04:00
|
|
|
*/
|
|
|
|
Fl_Button::Fl_Button(int X, int Y, int W, int H, const char *L)
|
2023-09-03 01:09:32 +03:00
|
|
|
: Fl_Widget(X,Y,W,H,L),
|
2024-01-17 20:54:01 +03:00
|
|
|
shortcut_(0),
|
|
|
|
value_(0),
|
|
|
|
oldval(0),
|
|
|
|
down_box_(FL_NO_BOX),
|
|
|
|
compact_(0)
|
2023-09-03 01:09:32 +03:00
|
|
|
{
|
1998-10-06 22:21:25 +04:00
|
|
|
box(FL_UP_BOX);
|
|
|
|
set_flag(SHORTCUT_LABEL);
|
|
|
|
}
|
1998-10-20 00:46:58 +04:00
|
|
|
|
2014-10-20 00:17:17 +04:00
|
|
|
/**
|
2024-01-17 20:54:01 +03:00
|
|
|
The constructor creates the button using the given position, size, and label.
|
2014-10-20 00:17:17 +04:00
|
|
|
|
2024-01-17 20:54:01 +03:00
|
|
|
The Button type() is set to FL_RADIO_BUTTON.
|
2012-07-24 08:37:22 +04:00
|
|
|
|
2024-01-17 20:54:01 +03:00
|
|
|
\param[in] X, Y, W, H position and size of the widget
|
|
|
|
\param[in] L widget label, default is no label
|
2014-10-20 00:17:17 +04:00
|
|
|
*/
|
2012-07-24 08:37:22 +04:00
|
|
|
Fl_Radio_Button::Fl_Radio_Button(int X,int Y,int W,int H,const char *L)
|
|
|
|
: Fl_Button(X, Y, W, H, L) {
|
|
|
|
type(FL_RADIO_BUTTON);
|
|
|
|
}
|
|
|
|
|
2014-10-20 00:17:17 +04:00
|
|
|
/**
|
2024-01-17 20:54:01 +03:00
|
|
|
The constructor creates the button using the given position, size, and label.
|
2014-10-20 00:17:17 +04:00
|
|
|
|
2024-01-17 20:54:01 +03:00
|
|
|
The Button type() is set to FL_TOGGLE_BUTTON.
|
2012-07-24 08:37:22 +04:00
|
|
|
|
2024-01-17 20:54:01 +03:00
|
|
|
\param[in] X, Y, W, H position and size of the widget
|
|
|
|
\param[in] L widget label, default is no label
|
2014-10-20 00:17:17 +04:00
|
|
|
*/
|
|
|
|
Fl_Toggle_Button::Fl_Toggle_Button(int X,int Y,int W,int H,const char *L)
|
|
|
|
: Fl_Button(X,Y,W,H,L)
|
2012-07-24 08:37:22 +04:00
|
|
|
{
|
|
|
|
type(FL_TOGGLE_BUTTON);
|
|
|
|
}
|
2023-09-03 01:09:32 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
Decide if buttons should be rendered in compact mode.
|
|
|
|
|
|
|
|
\image html compact_buttons_gtk.png "compact button keypad using GTK+ Scheme"
|
|
|
|
\image latex compact_buttons_gtk.png "compact button keypad using GTK+ Scheme" width=4cm
|
|
|
|
|
|
|
|
\image html compact_buttons_gleam.png "compact buttons in Gleam"
|
|
|
|
\image latex compact_buttons_gleam.png "compact buttons in Gleam" width=4cm
|
|
|
|
|
|
|
|
In compact mode, the button's surrounding border is altered to visually signal
|
|
|
|
that multiple buttons are functionally linked together. To ensure the correct
|
|
|
|
rendering of buttons in compact mode, all buttons must be part of the same
|
|
|
|
group, positioned close to each other, and aligned with the edges of the
|
|
|
|
group. Any button outlines not in contact with the parent group's outline
|
|
|
|
will be displayed as separators.
|
|
|
|
|
|
|
|
\param[in] v switch compact mode on (1) or off (0)
|
|
|
|
*/
|
|
|
|
void Fl_Button::compact(uchar v) { compact_ = v; }
|