1998-10-20 00:46:58 +04:00
|
|
|
//
|
2005-02-04 17:11:15 +03:00
|
|
|
// "$Id$"
|
1998-10-20 00:46:58 +04:00
|
|
|
//
|
|
|
|
// Choice widget for the Fast Light Tool Kit (FLTK).
|
|
|
|
//
|
2015-01-29 19:56:12 +03:00
|
|
|
// Copyright 1998-2015 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
|
|
|
|
// the file "COPYING" which should have been included with this file. If this
|
|
|
|
// file is missing or damaged, see the license at:
|
|
|
|
//
|
|
|
|
// http://www.fltk.org/COPYING.php
|
1998-10-20 00:46:58 +04:00
|
|
|
//
|
2005-04-16 04:13:17 +04:00
|
|
|
// Please report all bugs and problems on the following page:
|
|
|
|
//
|
|
|
|
// http://www.fltk.org/str.php
|
1998-10-20 00:46:58 +04:00
|
|
|
//
|
1998-10-06 22:21:25 +04:00
|
|
|
|
|
|
|
#include <FL/Fl.H>
|
|
|
|
#include <FL/Fl_Choice.H>
|
|
|
|
#include <FL/fl_draw.H>
|
2006-09-24 08:39:55 +04:00
|
|
|
#include "flstring.h"
|
1998-10-06 22:21:25 +04:00
|
|
|
|
1998-10-20 00:46:58 +04:00
|
|
|
// Emulates the Forms choice widget. This is almost exactly the same
|
|
|
|
// as an Fl_Menu_Button. The only difference is the appearance of the
|
|
|
|
// button: it draws the text of the current pick and a down-arrow.
|
|
|
|
|
1998-10-06 22:21:25 +04:00
|
|
|
void Fl_Choice::draw() {
|
2016-08-30 22:16:50 +03:00
|
|
|
Fl_Boxtype btype = Fl::scheme() ? FL_UP_BOX // non-default uses up box
|
|
|
|
: FL_DOWN_BOX; // default scheme uses down box
|
|
|
|
int dx = Fl::box_dx(btype);
|
|
|
|
int dy = Fl::box_dy(btype);
|
|
|
|
|
|
|
|
// Arrow area
|
2001-08-05 00:17:10 +04:00
|
|
|
int H = h() - 2 * dy;
|
2016-08-30 22:16:50 +03:00
|
|
|
int W = Fl::is_scheme("gtk+") ? 20 : // gtk+ -- fixed size
|
|
|
|
Fl::is_scheme("gleam") ? 20 : // gleam -- fixed size
|
|
|
|
Fl::is_scheme("plastic") ? ((H > 20) ? 20 : H) // plastic: shrink if H<20
|
|
|
|
: ((H > 20) ? 20 : H); // default: shrink if H<20
|
2001-11-17 21:18:53 +03:00
|
|
|
int X = x() + w() - W - dx;
|
2001-08-05 00:17:10 +04:00
|
|
|
int Y = y() + dy;
|
2016-08-30 22:16:50 +03:00
|
|
|
|
|
|
|
// Arrow object
|
2001-11-17 21:18:53 +03:00
|
|
|
int w1 = (W - 4) / 3; if (w1 < 1) w1 = 1;
|
|
|
|
int x1 = X + (W - 2 * w1 - 1) / 2;
|
2001-08-05 00:17:10 +04:00
|
|
|
int y1 = Y + (H - w1 - 1) / 2;
|
|
|
|
|
2002-03-26 03:50:16 +03:00
|
|
|
if (Fl::scheme()) {
|
2016-08-30 22:16:50 +03:00
|
|
|
// NON-DEFAULT SCHEME
|
2001-08-05 00:17:10 +04:00
|
|
|
|
2016-08-30 22:16:50 +03:00
|
|
|
// Draw widget box
|
|
|
|
draw_box(btype, color());
|
|
|
|
|
|
|
|
// Draw arrow area
|
2002-03-26 03:50:16 +03:00
|
|
|
fl_color(active_r() ? labelcolor() : fl_inactive(labelcolor()));
|
2015-01-29 19:56:12 +03:00
|
|
|
if (Fl::is_scheme("plastic")) {
|
2006-09-24 08:39:55 +04:00
|
|
|
// Show larger up/down arrows...
|
|
|
|
fl_polygon(x1, y1 + 3, x1 + w1, y1 + w1 + 3, x1 + 2 * w1, y1 + 3);
|
|
|
|
fl_polygon(x1, y1 + 1, x1 + w1, y1 - w1 + 1, x1 + 2 * w1, y1 + 1);
|
|
|
|
} else {
|
|
|
|
// Show smaller up/down arrows with a divider...
|
|
|
|
x1 = x() + w() - 13 - dx;
|
|
|
|
y1 = y() + h() / 2;
|
|
|
|
fl_polygon(x1, y1 - 2, x1 + 3, y1 - 5, x1 + 6, y1 - 2);
|
|
|
|
fl_polygon(x1, y1 + 2, x1 + 3, y1 + 5, x1 + 6, y1 + 2);
|
|
|
|
|
|
|
|
fl_color(fl_darker(color()));
|
|
|
|
fl_yxline(x1 - 7, y1 - 8, y1 + 8);
|
|
|
|
|
|
|
|
fl_color(fl_lighter(color()));
|
|
|
|
fl_yxline(x1 - 6, y1 - 8, y1 + 8);
|
|
|
|
}
|
2002-03-26 03:50:16 +03:00
|
|
|
} else {
|
2016-08-30 22:16:50 +03:00
|
|
|
// DEFAULT SCHEME
|
|
|
|
|
|
|
|
// Draw widget box
|
2009-09-16 11:06:41 +04:00
|
|
|
if (fl_contrast(textcolor(), FL_BACKGROUND2_COLOR) == textcolor()) {
|
2016-08-30 22:16:50 +03:00
|
|
|
draw_box(btype, FL_BACKGROUND2_COLOR);
|
2009-09-16 11:06:41 +04:00
|
|
|
} else {
|
2016-08-30 22:16:50 +03:00
|
|
|
draw_box(btype, fl_lighter(color()));
|
2009-09-16 11:06:41 +04:00
|
|
|
}
|
2002-03-26 03:50:16 +03:00
|
|
|
|
2016-08-30 22:16:50 +03:00
|
|
|
// Draw arrow area
|
|
|
|
draw_box(FL_UP_BOX,X,Y,W,H,color());
|
2002-03-26 03:50:16 +03:00
|
|
|
fl_color(active_r() ? labelcolor() : fl_inactive(labelcolor()));
|
|
|
|
fl_polygon(x1, y1, x1 + w1, y1 + w1, x1 + 2 * w1, y1);
|
|
|
|
}
|
2001-08-05 00:17:10 +04:00
|
|
|
|
2002-03-26 01:11:52 +03:00
|
|
|
W += 2 * dx;
|
|
|
|
|
2016-08-30 22:16:50 +03:00
|
|
|
// Draw menu item's label
|
1999-03-07 11:51:44 +03:00
|
|
|
if (mvalue()) {
|
|
|
|
Fl_Menu_Item m = *mvalue();
|
|
|
|
if (active_r()) m.activate(); else m.deactivate();
|
2003-08-24 17:09:06 +04:00
|
|
|
|
2016-08-30 22:16:50 +03:00
|
|
|
// Clip
|
2003-08-24 17:09:06 +04:00
|
|
|
int xx = x() + dx, yy = y() + dy + 1, ww = w() - W, hh = H - 2;
|
2008-12-06 17:59:52 +03:00
|
|
|
fl_push_clip(xx, yy, ww, hh);
|
2004-02-26 06:06:41 +03:00
|
|
|
|
|
|
|
if ( Fl::scheme()) {
|
|
|
|
Fl_Label l;
|
|
|
|
l.value = m.text;
|
|
|
|
l.image = 0;
|
|
|
|
l.deimage = 0;
|
|
|
|
l.type = m.labeltype_;
|
2008-04-23 23:09:28 +04:00
|
|
|
l.font = m.labelsize_ || m.labelfont_ ? m.labelfont_ : textfont();
|
2004-02-26 06:06:41 +03:00
|
|
|
l.size = m.labelsize_ ? m.labelsize_ : textsize();
|
|
|
|
l.color= m.labelcolor_ ? m.labelcolor_ : textcolor();
|
|
|
|
if (!m.active()) l.color = fl_inactive((Fl_Color)l.color);
|
|
|
|
fl_draw_shortcut = 2; // hack value to make '&' disappear
|
|
|
|
l.draw(xx+3, yy, ww>6 ? ww-6 : 0, hh, FL_ALIGN_LEFT);
|
|
|
|
fl_draw_shortcut = 0;
|
|
|
|
if ( Fl::focus() == this ) draw_focus(box(), xx, yy, ww, hh);
|
2003-08-24 17:09:06 +04:00
|
|
|
}
|
2004-02-26 06:06:41 +03:00
|
|
|
else {
|
|
|
|
fl_draw_shortcut = 2; // hack value to make '&' disappear
|
|
|
|
m.draw(xx, yy, ww, hh, this, Fl::focus() == this);
|
|
|
|
fl_draw_shortcut = 0;
|
2003-08-24 17:09:06 +04:00
|
|
|
}
|
2004-02-26 06:06:41 +03:00
|
|
|
|
|
|
|
fl_pop_clip();
|
1999-03-07 11:51:44 +03:00
|
|
|
}
|
2002-03-26 03:50:16 +03:00
|
|
|
|
2016-08-30 22:16:50 +03:00
|
|
|
// Widget's label
|
1998-10-06 22:21:25 +04:00
|
|
|
draw_label();
|
|
|
|
}
|
|
|
|
|
2008-09-16 10:49:08 +04:00
|
|
|
/**
|
|
|
|
Create a new Fl_Choice widget using the given position, size and label string.
|
|
|
|
The default boxtype is \c FL_UP_BOX.
|
|
|
|
|
|
|
|
The constructor sets menu() to NULL.
|
|
|
|
See Fl_Menu_ for the methods to set or change the menu.
|
|
|
|
|
|
|
|
\param[in] X, Y, W, H position and size of the widget
|
|
|
|
\param[in] L widget label, default is no label
|
|
|
|
*/
|
|
|
|
Fl_Choice::Fl_Choice(int X, int Y, int W, int H, const char *L)
|
|
|
|
: Fl_Menu_(X,Y,W,H,L) {
|
1998-10-06 22:21:25 +04:00
|
|
|
align(FL_ALIGN_LEFT);
|
|
|
|
when(FL_WHEN_RELEASE);
|
|
|
|
textfont(FL_HELVETICA);
|
2001-08-05 00:17:10 +04:00
|
|
|
box(FL_FLAT_BOX);
|
|
|
|
down_box(FL_BORDER_BOX);
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
|
|
|
|
2008-09-16 10:49:08 +04:00
|
|
|
/**
|
|
|
|
Sets the currently selected value using a pointer to menu item.
|
|
|
|
Changing the selected value causes a redraw().
|
|
|
|
\param[in] v pointer to menu item in the menu item array.
|
|
|
|
\returns non-zero if the new value is different to the old one.
|
|
|
|
*/
|
2004-04-29 06:47:09 +04:00
|
|
|
int Fl_Choice::value(const Fl_Menu_Item *v) {
|
|
|
|
if (!Fl_Menu_::value(v)) return 0;
|
|
|
|
redraw();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2008-09-16 10:49:08 +04:00
|
|
|
/**
|
|
|
|
Sets the currently selected value using the index into the menu item array.
|
|
|
|
Changing the selected value causes a redraw().
|
|
|
|
\param[in] v index of value in the menu item array.
|
|
|
|
\returns non-zero if the new value is different to the old one.
|
|
|
|
*/
|
1998-10-06 22:21:25 +04:00
|
|
|
int Fl_Choice::value(int v) {
|
2004-12-03 05:48:22 +03:00
|
|
|
if (v == -1) return value((const Fl_Menu_Item *)0);
|
2002-07-09 21:18:45 +04:00
|
|
|
if (v < 0 || v >= (size() - 1)) return 0;
|
1998-10-06 22:21:25 +04:00
|
|
|
if (!Fl_Menu_::value(v)) return 0;
|
|
|
|
redraw();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Fl_Choice::handle(int e) {
|
|
|
|
if (!menu() || !menu()->text) return 0;
|
|
|
|
const Fl_Menu_Item* v;
|
|
|
|
switch (e) {
|
2001-09-30 02:59:45 +04:00
|
|
|
case FL_ENTER:
|
|
|
|
case FL_LEAVE:
|
|
|
|
return 1;
|
|
|
|
|
2001-08-05 00:17:10 +04:00
|
|
|
case FL_KEYBOARD:
|
2002-11-11 23:22:21 +03:00
|
|
|
if (Fl::event_key() != ' ' ||
|
|
|
|
(Fl::event_state() & (FL_SHIFT | FL_CTRL | FL_ALT | FL_META))) return 0;
|
1998-10-06 22:21:25 +04:00
|
|
|
case FL_PUSH:
|
2001-12-16 19:41:48 +03:00
|
|
|
if (Fl::visible_focus()) Fl::focus(this);
|
1998-10-06 22:21:25 +04:00
|
|
|
J1:
|
2009-09-16 11:06:41 +04:00
|
|
|
if (Fl::scheme()
|
|
|
|
|| fl_contrast(textcolor(), FL_BACKGROUND2_COLOR) != textcolor()) {
|
2006-09-24 17:35:23 +04:00
|
|
|
v = menu()->pulldown(x(), y(), w(), h(), mvalue(), this);
|
|
|
|
} else {
|
|
|
|
// In order to preserve the old look-n-feel of "white" menus,
|
|
|
|
// temporarily override the color() of this widget...
|
|
|
|
Fl_Color c = color();
|
|
|
|
color(FL_BACKGROUND2_COLOR);
|
|
|
|
v = menu()->pulldown(x(), y(), w(), h(), mvalue(), this);
|
|
|
|
color(c);
|
|
|
|
}
|
1998-10-06 22:21:25 +04:00
|
|
|
if (!v || v->submenu()) return 1;
|
|
|
|
if (v != mvalue()) redraw();
|
|
|
|
picked(v);
|
|
|
|
return 1;
|
|
|
|
case FL_SHORTCUT:
|
|
|
|
if (Fl_Widget::test_shortcut()) goto J1;
|
|
|
|
v = menu()->test_shortcut();
|
|
|
|
if (!v) return 0;
|
|
|
|
if (v != mvalue()) redraw();
|
|
|
|
picked(v);
|
|
|
|
return 1;
|
2001-08-05 00:17:10 +04:00
|
|
|
case FL_FOCUS:
|
|
|
|
case FL_UNFOCUS:
|
2001-11-03 22:24:22 +03:00
|
|
|
if (Fl::visible_focus()) {
|
|
|
|
redraw();
|
|
|
|
return 1;
|
|
|
|
} else return 0;
|
1998-10-06 22:21:25 +04:00
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-10-20 00:46:58 +04:00
|
|
|
//
|
2005-02-04 17:11:15 +03:00
|
|
|
// End of "$Id$".
|
1998-10-20 00:46:58 +04:00
|
|
|
//
|