#include <FL/Fl.H>
#include <FL/Fl_Menu_Button.H>
#include <FL/Fl_Rect.H>
#include <FL/fl_draw.H>
#include "Fl_Window_Driver.H"
Fl_Menu_Button* Fl_Menu_Button::pressed_menu_button_ = NULL;
void Fl_Menu_Button::draw() {
if (!box() || type()) return;
int ah = h() - Fl::box_dh(box());
int aw = ah > 20 ? 20 : ah; int ax = x() + w() - Fl::box_dx(box()) - aw;
int ay = y() + (h() - ah) / 2;
draw_box(pressed_menu_button_ == this ? fl_down(box()) : box(), color());
draw_label(x() + Fl::box_dx(box()), y(), w() - Fl::box_dw(box()) - aw, h());
if (Fl::focus() == this) draw_focus();
Fl_Color arrow_color = active_r() ? labelcolor() : fl_inactive(labelcolor());
fl_draw_arrow(Fl_Rect(ax, ay, aw, ah), FL_ARROW_SINGLE, FL_ORIENT_DOWN, arrow_color);
}
const Fl_Menu_Item* Fl_Menu_Button::popup() {
menu_end();
const Fl_Menu_Item* m;
pressed_menu_button_ = this;
redraw();
Fl_Widget_Tracker mb(this);
if (!box() || type()) {
m = menu()->popup(Fl::event_x(), Fl::event_y(), label(), mvalue(), this);
} else {
Fl_Window_Driver::current_menu_button = this;
m = menu()->pulldown(x(), y(), w(), h(), 0, this);
Fl_Window_Driver::current_menu_button = NULL;
}
picked(m);
pressed_menu_button_ = 0;
if (mb.exists()) redraw();
return m;
}
int Fl_Menu_Button::handle(int e) {
if (!menu() || !menu()->text) return 0;
switch (e) {
case FL_ENTER:
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;
}
if (Fl::visible_focus()) Fl::focus(this);
popup();
return 1;
case FL_KEYBOARD:
if (!box()) return 0;
if (Fl::event_key() == ' ' &&
!(Fl::event_state() & (FL_SHIFT | FL_CTRL | FL_ALT | FL_META))) {
popup();
return 1;
} else return 0;
case FL_SHORTCUT:
if (Fl_Widget::test_shortcut()) {popup(); return 1;}
return test_shortcut() != 0;
case FL_FOCUS:
case FL_UNFOCUS:
if (box() && Fl::visible_focus()) {
redraw();
return 1;
}
default:
return 0;
}
}
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);
}