1998-10-19 20:46:58 +00:00
|
|
|
//
|
|
|
|
|
// Menu button widget for the Fast Light Tool Kit (FLTK).
|
|
|
|
|
//
|
2019-06-27 14:04:30 +00:00
|
|
|
// Copyright 1998-2019 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>
|
|
|
|
|
#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;
|
2012-08-16 21:35:14 +00:00
|
|
|
int H = (labelsize()-3)&-2;
|
|
|
|
|
int X = x()+w()-H-Fl::box_dx(box())-Fl::box_dw(box())-1;
|
|
|
|
|
int Y = y()+(h()-H)/2;
|
2011-09-28 02:44:56 +00:00
|
|
|
draw_box(pressed_menu_button_ == this ? fl_down(box()) : box(), color());
|
2012-08-16 21:35:14 +00:00
|
|
|
draw_label(x()+Fl::box_dx(box()), y(), X-x()+2, h());
|
2011-09-28 02:44:56 +00:00
|
|
|
if (Fl::focus() == this) draw_focus();
|
|
|
|
|
// ** if (box() == FL_FLAT_BOX) return; // for XForms compatibility
|
2001-08-04 20:17:10 +00:00
|
|
|
fl_color(active_r() ? FL_DARK3 : fl_inactive(FL_DARK3));
|
|
|
|
|
fl_line(X+H/2, Y+H, X, Y, X+H, Y);
|
|
|
|
|
fl_color(active_r() ? FL_LIGHT3 : fl_inactive(FL_LIGHT3));
|
|
|
|
|
fl_line(X+H, Y, X+H/2, Y+H);
|
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);
|
|
|
|
|
}
|