1998-10-19 20:46:58 +00:00
|
|
|
//
|
|
|
|
|
// Menu button widget for the Fast Light Tool Kit (FLTK).
|
|
|
|
|
//
|
2020-11-22 18:19:19 +00:00
|
|
|
// Copyright 1998-2022 by Bill Spitzak and others.
|
1998-10-19 20:46:58 +00:00
|
|
|
//
|
2011-07-19 04:49:30 +00: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:
|
|
|
|
|
//
|
2020-07-01 16:03:10 +00:00
|
|
|
// https://www.fltk.org/COPYING.php
|
1998-10-19 20:46:58 +00:00
|
|
|
//
|
2020-07-01 16:03:10 +00:00
|
|
|
// Please see the following page on how to report bugs and issues:
|
2005-04-16 00:13:17 +00:00
|
|
|
//
|
2020-07-01 16:03:10 +00:00
|
|
|
// https://www.fltk.org/bugs.php
|
1998-10-19 20:46:58 +00:00
|
|
|
//
|
1998-10-06 18:21:25 +00:00
|
|
|
|
|
|
|
|
#include <FL/Fl.H>
|
|
|
|
|
#include <FL/Fl_Menu_Button.H>
|
2022-11-23 14:18:19 +00:00
|
|
|
#include <FL/Fl_Rect.H>
|
1998-10-06 18:21:25 +00:00
|
|
|
#include <FL/fl_draw.H>
|
|
|
|
|
|
2006-01-15 02:21:19 +00:00
|
|
|
|
2020-07-01 16:03:10 +00:00
|
|
|
static Fl_Menu_Button *pressed_menu_button_ = 0;
|
2006-01-15 02:21:19 +00:00
|
|
|
|
2012-08-16 21:35:14 +00:00
|
|
|
|
1998-10-06 18:21:25 +00:00
|
|
|
void Fl_Menu_Button::draw() {
|
|
|
|
|
if (!box() || type()) return;
|
2022-11-23 14:18:19 +00:00
|
|
|
|
2022-11-24 18:00:00 +00:00
|
|
|
// calculate position and size of virtual "arrow box" (choice button)
|
2022-11-23 14:18:19 +00:00
|
|
|
|
2022-11-24 18:00:00 +00:00
|
|
|
int ah = h() - Fl::box_dh(box());
|
|
|
|
|
int aw = ah > 20 ? 20 : ah; // limit width: don't waste space for button
|
|
|
|
|
int ax = x() + w() - Fl::box_dx(box()) - aw;
|
|
|
|
|
int ay = y() + (h() - ah) / 2;
|
|
|
|
|
|
|
|
|
|
// the remaining space is used to draw the label
|
2022-11-23 14:18:19 +00:00
|
|
|
|
2011-09-28 02:44:56 +00:00
|
|
|
draw_box(pressed_menu_button_ == this ? fl_down(box()) : box(), color());
|
2022-11-24 18:00:00 +00:00
|
|
|
draw_label(x() + Fl::box_dx(box()), y(), w() - Fl::box_dw(box()) - aw, h());
|
2011-09-28 02:44:56 +00:00
|
|
|
if (Fl::focus() == this) draw_focus();
|
2022-11-23 14:18:19 +00:00
|
|
|
|
2022-11-24 18:00:00 +00:00
|
|
|
// draw the arrow (choice button)
|
2022-11-23 14:18:19 +00:00
|
|
|
|
|
|
|
|
Fl_Color arrow_color = active_r() ? FL_DARK3 : fl_inactive(FL_DARK3);
|
2022-11-24 18:00:00 +00:00
|
|
|
fl_draw_arrow(Fl_Rect(ax, ay, aw, ah), FL_ARROW_SINGLE, FL_ORIENT_DOWN, arrow_color);
|
1998-10-06 18:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2012-08-16 21:35:14 +00:00
|
|
|
|
2008-09-15 00:27:28 +00:00
|
|
|
/**
|
|
|
|
|
Act exactly as though the user clicked the button or typed the
|
|
|
|
|
shortcut key. The menu appears, it waits for the user to pick an item,
|
|
|
|
|
and if they pick one it sets value() and does the callback or
|
|
|
|
|
sets changed() as described above. The menu item is returned
|
|
|
|
|
or NULL if the user dismisses the menu.
|
2019-06-27 14:04:30 +00:00
|
|
|
|
|
|
|
|
\note Since FLTK 1.4.0 Fl_Menu_::menu_end() is called before the menu
|
|
|
|
|
pops up to make sure the menu array is located in private storage.
|
|
|
|
|
|
|
|
|
|
\see Fl_Menu_::menu_end()
|
2008-09-15 00:27:28 +00:00
|
|
|
*/
|
1998-10-06 18:21:25 +00:00
|
|
|
const Fl_Menu_Item* Fl_Menu_Button::popup() {
|
2019-06-27 14:04:30 +00:00
|
|
|
menu_end();
|
1998-10-06 18:21:25 +00:00
|
|
|
const Fl_Menu_Item* m;
|
2006-01-15 02:21:19 +00:00
|
|
|
pressed_menu_button_ = this;
|
|
|
|
|
redraw();
|
2009-02-15 13:49:34 +00:00
|
|
|
Fl_Widget_Tracker mb(this);
|
1998-10-06 18:21:25 +00:00
|
|
|
if (!box() || type()) {
|
|
|
|
|
m = menu()->popup(Fl::event_x(), Fl::event_y(), label(), mvalue(), this);
|
|
|
|
|
} else {
|
|
|
|
|
m = menu()->pulldown(x(), y(), w(), h(), 0, this);
|
|
|
|
|
}
|
|
|
|
|
picked(m);
|
2006-01-15 02:21:19 +00:00
|
|
|
pressed_menu_button_ = 0;
|
2009-02-08 14:44:15 +00:00
|
|
|
if (mb.exists()) redraw();
|
1998-10-06 18:21:25 +00:00
|
|
|
return m;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int Fl_Menu_Button::handle(int e) {
|
|
|
|
|
if (!menu() || !menu()->text) return 0;
|
|
|
|
|
switch (e) {
|
2010-02-26 21:10:46 +00:00
|
|
|
case FL_ENTER: /* FALLTHROUGH */
|
1998-10-06 18:21:25 +00:00
|
|
|
case FL_LEAVE:
|
|
|
|
|
return (box() && !type()) ? 1 : 0;
|
|
|
|
|
case FL_PUSH:
|
|
|
|
|
if (!box()) {
|
|
|
|
|
if (Fl::event_button() != 3) return 0;
|
|
|
|
|
} else if (type()) {
|
|
|
|
|
if (!(type() & (1 << (Fl::event_button()-1)))) return 0;
|
|
|
|
|
}
|
2001-12-16 16:41:48 +00:00
|
|
|
if (Fl::visible_focus()) Fl::focus(this);
|
1998-10-06 18:21:25 +00:00
|
|
|
popup();
|
|
|
|
|
return 1;
|
2001-08-04 20:17:10 +00:00
|
|
|
case FL_KEYBOARD:
|
2002-09-01 22:39:33 +00:00
|
|
|
if (!box()) return 0;
|
2002-11-11 20:22:21 +00:00
|
|
|
if (Fl::event_key() == ' ' &&
|
|
|
|
|
!(Fl::event_state() & (FL_SHIFT | FL_CTRL | FL_ALT | FL_META))) {
|
2001-08-04 20:17:10 +00:00
|
|
|
popup();
|
|
|
|
|
return 1;
|
|
|
|
|
} else return 0;
|
1998-10-06 18:21:25 +00:00
|
|
|
case FL_SHORTCUT:
|
|
|
|
|
if (Fl_Widget::test_shortcut()) {popup(); return 1;}
|
|
|
|
|
return test_shortcut() != 0;
|
2010-02-26 21:10:46 +00:00
|
|
|
case FL_FOCUS: /* FALLTHROUGH */
|
2001-08-04 20:17:10 +00:00
|
|
|
case FL_UNFOCUS:
|
2001-11-03 19:24:22 +00:00
|
|
|
if (box() && Fl::visible_focus()) {
|
2001-08-04 20:17:10 +00:00
|
|
|
redraw();
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
1998-10-06 18:21:25 +00:00
|
|
|
default:
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2008-09-15 00:27:28 +00:00
|
|
|
/**
|
|
|
|
|
Creates a new Fl_Menu_Button widget using the given position,
|
|
|
|
|
size, and label string. The default boxtype is FL_UP_BOX.
|
2020-07-01 16:03:10 +00:00
|
|
|
<P>The constructor sets menu() to NULL. See
|
2008-09-15 00:27:28 +00:00
|
|
|
Fl_Menu_ for the methods to set or change the menu.
|
|
|
|
|
*/
|
1998-10-06 18:21:25 +00:00
|
|
|
Fl_Menu_Button::Fl_Menu_Button(int X,int Y,int W,int H,const char *l)
|
|
|
|
|
: Fl_Menu_(X,Y,W,H,l) {
|
|
|
|
|
down_box(FL_NO_BOX);
|
|
|
|
|
}
|