📄 fl_menu.cxx
字号:
int menuwindow::find_selected(int mx, int my) {
if (!menu || !menu->text) return -1;
mx -= x();
my -= y();
if (my < 0 || my >= h()) return -1;
if (!itemheight) { // menubar
int xx = 3; int n = 0;
const Fl_Menu_Item* m = menu;
for (; ; m = m->next(), n++) {
if (!m->text) return -1;
xx += m->measure(0, button) + 16;
if (xx > mx) break;
}
return n;
}
if (mx < Fl::box_dx(box()) || mx >= w()) return -1;
int n = (my-Fl::box_dx(box())-1)/itemheight;
if (n < 0 || n>=numitems) return -1;
return n;
}
// return horizontal position for item n in a menubar:
int menuwindow::titlex(int n) {
const Fl_Menu_Item* m;
int xx = 3;
for (m=menu; n--; m = m->next()) xx += m->measure(0, button) + 16;
return xx;
}
////////////////////////////////////////////////////////////////
// Fl_Menu_Item::popup(...)
// Because Fl::grab() is done, all events go to one of the menu windows.
// But the handle method needs to look at all of them to find out
// what item the user is pointing at. And it needs a whole lot
// of other state variables to determine what is going on with
// the currently displayed menus.
// So the main loop (handlemenu()) puts all the state in a structure
// and puts a pointer to it in a static location, so the handle()
// on menus can refer to it and alter it. The handle() method
// changes variables in this state to indicate what item is
// picked, but does not actually alter the display, instead the
// main loop does that. This is because the X mapping and unmapping
// of windows is slow, and we don't want to fall behind the events.
// values for menustate.state:
#define INITIAL_STATE 0 // no mouse up or down since popup() called
#define PUSH_STATE 1 // mouse has been pushed on a normal item
#define DONE_STATE 2 // exit the popup, the current item was picked
#define MENU_PUSH_STATE 3 // mouse has been pushed on a menu title
struct menustate {
const Fl_Menu_Item* current_item; // what mouse is pointing at
int menu_number; // which menu it is in
int item_number; // which item in that menu, -1 if none
menuwindow* p[20]; // pointers to menus
int nummenus;
int menubar; // if true p[0] is a menubar
int state;
};
static menustate* p;
static inline void setitem(const Fl_Menu_Item* i, int m, int n) {
p->current_item = i;
p->menu_number = m;
p->item_number = n;
}
static void setitem(int m, int n) {
menustate &pp = *p;
pp.current_item = (n >= 0) ? pp.p[m]->menu->next(n) : 0;
pp.menu_number = m;
pp.item_number = n;
}
static int forward(int menu) { // go to next item in menu menu if possible
menustate &pp = *p;
menuwindow &m = *(pp.p[menu]);
int item = (menu == pp.menu_number) ? pp.item_number : m.selected;
while (++item < m.numitems) {
const Fl_Menu_Item* m1 = m.menu->next(item);
if (m1->activevisible()) {setitem(m1, menu, item); return 1;}
}
return 0;
}
static int backward(int menu) { // previous item in menu menu if possible
menustate &pp = *p;
menuwindow &m = *(pp.p[menu]);
int item = (menu == pp.menu_number) ? pp.item_number : m.selected;
if (item < 0) item = m.numitems;
while (--item >= 0) {
const Fl_Menu_Item* m1 = m.menu->next(item);
if (m1->activevisible()) {setitem(m1, menu, item); return 1;}
}
return 0;
}
int menuwindow::handle(int e) {
menustate &pp = *p;
switch (e) {
case FL_KEYBOARD:
switch (Fl::event_key()) {
case FL_BackSpace:
case 0xFE20: // backtab
BACKTAB:
if (!backward(pp.menu_number)) {pp.item_number = -1;backward(pp.menu_number);}
return 1;
case FL_Up:
if (pp.menubar && pp.menu_number == 0) {
// Do nothing...
} else if (backward(pp.menu_number)) {
// Do nothing...
} else if (pp.menubar && pp.menu_number==1) {
setitem(0, pp.p[0]->selected);
}
return 1;
case FL_Tab:
if (Fl::event_shift()) goto BACKTAB;
case FL_Down:
if (pp.menu_number || !pp.menubar) forward(pp.menu_number);
else if (pp.menu_number < pp.nummenus-1) forward(pp.menu_number+1);
return 1;
case FL_Right:
if (pp.menubar && (pp.menu_number<=0 || pp.menu_number==1 && pp.nummenus==2))
forward(0);
else if (pp.menu_number < pp.nummenus-1) forward(pp.menu_number+1);
return 1;
case FL_Left:
if (pp.menubar && pp.menu_number<=1) backward(0);
else if (pp.menu_number>0)
setitem(pp.menu_number-1, pp.p[pp.menu_number-1]->selected);
return 1;
case FL_Enter:
case ' ':
pp.state = DONE_STATE;
return 1;
case FL_Escape:
setitem(0, -1, 0);
pp.state = DONE_STATE;
return 1;
}
break;
case FL_SHORTCUT: {
for (int mymenu = pp.nummenus; mymenu--;) {
menuwindow &mw = *(pp.p[mymenu]);
int item; const Fl_Menu_Item* m = mw.menu->find_shortcut(&item);
if (m) {
setitem(m, mymenu, item);
if (!m->submenu()) pp.state = DONE_STATE;
return 1;
}
}} break;
case FL_ENTER:
case FL_MOVE:
case FL_PUSH:
case FL_DRAG: {
int mx = Fl::event_x_root();
int my = Fl::event_y_root();
int item=0; int mymenu;
for (mymenu = pp.nummenus-1; ; mymenu--) {
item = pp.p[mymenu]->find_selected(mx, my);
if (item >= 0) break;
if (mymenu <= 0) break;
}
setitem(mymenu, item);
if (e == FL_PUSH) {
if (pp.current_item && pp.current_item->submenu() // this is a menu title
&& item != pp.p[mymenu]->selected // and it is not already on
&& !pp.current_item->callback_) // and it does not have a callback
pp.state = MENU_PUSH_STATE;
else
pp.state = PUSH_STATE;
}} return 1;
case FL_RELEASE:
// do nothing if they try to pick inactive items
if (pp.current_item && !pp.current_item->activevisible()) return 1;
// Mouse must either be held down/dragged some, or this must be
// the second click (not the one that popped up the menu):
if (!Fl::event_is_click() || pp.state == PUSH_STATE ||
pp.menubar && pp.current_item && !pp.current_item->submenu() // button
) {
#if 0 // makes the check/radio items leave the menu up
const Fl_Menu_Item* m = pp.current_item;
if (m && button && (m->flags & (FL_MENU_TOGGLE|FL_MENU_RADIO))) {
((Fl_Menu_*)button)->picked(m);
pp.p[pp.menu_number]->redraw();
} else
#endif
pp.state = DONE_STATE;
}
return 1;
}
return Fl_Window::handle(e);
}
const Fl_Menu_Item* Fl_Menu_Item::pulldown(
int X, int Y, int W, int H,
const Fl_Menu_Item* initial_item,
const Fl_Menu_* pbutton,
const Fl_Menu_Item* t,
int menubar) const
{
Fl_Group::current(0); // fix possible user error...
button = pbutton;
if (pbutton) {
for (Fl_Window* w = pbutton->window(); w; w = w->window()) {
X += w->x();
Y += w->y();
}
} else {
X += Fl::event_x_root()-Fl::event_x();
Y += Fl::event_y_root()-Fl::event_y();
}
menuwindow mw(this, X, Y, W, H, initial_item, t, menubar);
Fl::grab(mw);
menustate pp; p = &pp;
pp.p[0] = &mw;
pp.nummenus = 1;
pp.menubar = menubar;
pp.state = INITIAL_STATE;
menuwindow* fakemenu = 0; // kludge for buttons in menubar
// preselected item, pop up submenus if necessary:
if (initial_item && mw.selected >= 0) {
setitem(0, mw.selected);
goto STARTUP;
}
pp.current_item = 0; pp.menu_number = 0; pp.item_number = -1;
if (menubar) mw.handle(FL_DRAG); // find the initial menu
initial_item = pp.current_item;
if (initial_item) goto STARTUP;
// the main loop, runs until p.state goes to DONE_STATE:
for (;;) {
// make sure all the menus are shown:
{for (int k = menubar; k < pp.nummenus; k++)
if (!pp.p[k]->shown()) {
if (pp.p[k]->title) pp.p[k]->title->show();
pp.p[k]->show();
}
}
// get events:
{const Fl_Menu_Item* oldi = pp.current_item;
Fl::wait();
if (pp.state == DONE_STATE) break; // done.
if (pp.current_item == oldi) continue;}
// only do rest if item changes:
delete fakemenu; fakemenu = 0; // turn off "menubar button"
if (!pp.current_item) { // pointing at nothing
// turn off selection in deepest menu, but don't erase other menus:
pp.p[pp.nummenus-1]->set_selected(-1);
continue;
}
delete fakemenu; fakemenu = 0;
initial_item = 0; // stop the startup code
pp.p[pp.menu_number]->autoscroll(pp.item_number);
STARTUP:
menuwindow& cw = *pp.p[pp.menu_number];
const Fl_Menu_Item* m = pp.current_item;
if (!m->activevisible()) { // pointing at inactive item
cw.set_selected(-1);
initial_item = 0; // turn off startup code
continue;
}
cw.set_selected(pp.item_number);
if (m==initial_item) initial_item=0; // stop the startup code if item found
if (m->submenu()) {
const Fl_Menu_Item* title = m;
const Fl_Menu_Item* menutable;
if (m->flags&FL_SUBMENU) menutable = m+1;
else menutable = (Fl_Menu_Item*)(m)->user_data_;
// figure out where new menu goes:
int nX, nY;
if (!pp.menu_number && pp.menubar) { // menu off a menubar:
nX = cw.x() + cw.titlex(pp.item_number);
nY = cw.y() + cw.h();
initial_item = 0;
} else {
nX = cw.x() + cw.w();
nY = cw.y() + pp.item_number * cw.itemheight;
title = 0;
}
if (initial_item) { // bring up submenu containing initial item:
menuwindow* n = new menuwindow(menutable,X,Y,W,H,initial_item,title,0,0,cw.x());
pp.p[pp.nummenus++] = n;
// move all earlier menus to line up with this new one:
if (n->selected>=0) {
int dy = n->y()-nY;
int dx = n->x()-nX;
for (int menu = 0; menu <= pp.menu_number; menu++) {
menuwindow* tt = pp.p[menu];
int nx = tt->x()+dx; if (nx < 0) {nx = 0; dx = -tt->x();}
int ny = tt->y()+dy; if (ny < 0) {ny = 0; dy = -tt->y();}
tt->position(nx, ny);
}
setitem(pp.nummenus-1, n->selected);
goto STARTUP;
}
} else if (pp.nummenus > pp.menu_number+1 &&
pp.p[pp.menu_number+1]->menu == menutable) {
// the menu is already up:
while (pp.nummenus > pp.menu_number+2) delete pp.p[--pp.nummenus];
pp.p[pp.nummenus-1]->set_selected(-1);
} else {
// delete all the old menus and create new one:
while (pp.nummenus > pp.menu_number+1) delete pp.p[--pp.nummenus];
pp.p[pp.nummenus++]= new menuwindow(menutable, nX, nY,
title?1:0, 0, 0, title, 0, menubar, cw.x());
}
} else { // !m->submenu():
while (pp.nummenus > pp.menu_number+1) delete pp.p[--pp.nummenus];
if (!pp.menu_number && pp.menubar) {
// kludge so "menubar buttons" turn "on" by using menu title:
fakemenu = new menuwindow(0,
cw.x()+cw.titlex(pp.item_number),
cw.y()+cw.h(), 0, 0,
0, m, 0, 1);
fakemenu->title->show();
}
}
}
const Fl_Menu_Item* m = pp.current_item;
delete fakemenu;
while (pp.nummenus>1) delete pp.p[--pp.nummenus];
mw.hide();
Fl::release();
return m;
}
const Fl_Menu_Item*
Fl_Menu_Item::popup(
int X, int Y,
const char* title,
const Fl_Menu_Item* picked,
const Fl_Menu_* but
) const
{
static Fl_Menu_Item dummy; // static so it is all zeros
dummy.text = title;
return pulldown(X, Y, 0, 0, picked, but, title ? &dummy : 0);
}
// Search only the top level menu for a shortcut. Either &x in the
// label or the shortcut fields are used:
const Fl_Menu_Item* Fl_Menu_Item::find_shortcut(int* ip) const {
const Fl_Menu_Item* m = this;
if (m) for (int ii = 0; m->text; m = m->next(), ii++) {
if (m->activevisible()) {
if (Fl::test_shortcut(m->shortcut_)
|| Fl_Widget::test_shortcut(m->text)) {
if (ip) *ip=ii;
return m;
}
}
}
return 0;
}
// Recursive search of all submenus for anything with this key as a
// shortcut. Only uses the shortcut field, ignores &x in the labels:
const Fl_Menu_Item* Fl_Menu_Item::test_shortcut() const {
const Fl_Menu_Item* m = this;
const Fl_Menu_Item* ret = 0;
if (m) for (; m->text; m = m->next()) {
if (m->activevisible()) {
// return immediately any match of an item in top level menu:
if (Fl::test_shortcut(m->shortcut_)) return m;
// if (Fl_Widget::test_shortcut(m->text)) return m;
// only return matches from lower menu if nothing found in top menu:
if (!ret && m->submenu()) {
const Fl_Menu_Item* s =
(m->flags&FL_SUBMENU) ? m+1:(const Fl_Menu_Item*)m->user_data_;
ret = s->test_shortcut();
}
}
}
return ret;
}
//
// End of "$Id: Fl_Menu.cxx,v 1.1.1.1 2003/06/03 22:25:43 agno Exp $".
//
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -