Mercurial > projects > mde
annotate mde/gui/widget/PopupMenu.d @ 133:9fd705793568
Fixed menu popup bug, improved recursion detection.
Menu popups can now determine whether or not they are sub-menus.
Recursion detection can now also check content (if not the same, there's not a risk of infinite recursion).
author | Diggory Hardy <diggory.hardy@gmail.com> |
---|---|
date | Fri, 23 Jan 2009 16:05:05 +0000 |
parents | 9cff74f68b84 |
children | 9f035cd139c6 |
rev | line source |
---|---|
108
c9fc2d303178
Added capability for border-less pop-up widgets. Simple pop-up menu.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
1 /* LICENSE BLOCK |
c9fc2d303178
Added capability for border-less pop-up widgets. Simple pop-up menu.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
2 Part of mde: a Modular D game-oriented Engine |
c9fc2d303178
Added capability for border-less pop-up widgets. Simple pop-up menu.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
3 Copyright © 2007-2008 Diggory Hardy |
c9fc2d303178
Added capability for border-less pop-up widgets. Simple pop-up menu.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
4 |
c9fc2d303178
Added capability for border-less pop-up widgets. Simple pop-up menu.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
5 This program is free software: you can redistribute it and/or modify it under the terms |
c9fc2d303178
Added capability for border-less pop-up widgets. Simple pop-up menu.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
6 of the GNU General Public License as published by the Free Software Foundation, either |
c9fc2d303178
Added capability for border-less pop-up widgets. Simple pop-up menu.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
7 version 2 of the License, or (at your option) any later version. |
c9fc2d303178
Added capability for border-less pop-up widgets. Simple pop-up menu.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
8 |
c9fc2d303178
Added capability for border-less pop-up widgets. Simple pop-up menu.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
9 This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
c9fc2d303178
Added capability for border-less pop-up widgets. Simple pop-up menu.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
10 without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
c9fc2d303178
Added capability for border-less pop-up widgets. Simple pop-up menu.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
11 See the GNU General Public License for more details. |
c9fc2d303178
Added capability for border-less pop-up widgets. Simple pop-up menu.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
12 |
c9fc2d303178
Added capability for border-less pop-up widgets. Simple pop-up menu.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
13 You should have received a copy of the GNU General Public License |
c9fc2d303178
Added capability for border-less pop-up widgets. Simple pop-up menu.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
14 along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
c9fc2d303178
Added capability for border-less pop-up widgets. Simple pop-up menu.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
15 |
131
9cff74f68b84
Major revisions to popup handling. Buttons can close menus now, plus some smaller impovements. Removed Widget module.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
121
diff
changeset
|
16 /****************************************************************************** |
113
9824bee909fd
Popup menu; works for simple menus except that clicking an item doesn't close it.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
112
diff
changeset
|
17 * Pop-up menus based on content structures. |
131
9cff74f68b84
Major revisions to popup handling. Buttons can close menus now, plus some smaller impovements. Removed Widget module.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
121
diff
changeset
|
18 *****************************************************************************/ |
113
9824bee909fd
Popup menu; works for simple menus except that clicking an item doesn't close it.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
112
diff
changeset
|
19 module mde.gui.widget.PopupMenu; |
108
c9fc2d303178
Added capability for border-less pop-up widgets. Simple pop-up menu.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
20 |
131
9cff74f68b84
Major revisions to popup handling. Buttons can close menus now, plus some smaller impovements. Removed Widget module.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
121
diff
changeset
|
21 import mde.gui.widget.AParentWidget; |
113
9824bee909fd
Popup menu; works for simple menus except that clicking an item doesn't close it.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
112
diff
changeset
|
22 |
115
1b1e2297e2fc
Enums handled more generically now via either a popup list or flat list of BoolContentWidgets.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
114
diff
changeset
|
23 import mde.content.miscContent; |
113
9824bee909fd
Popup menu; works for simple menus except that clicking an item doesn't close it.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
112
diff
changeset
|
24 import mde.gui.exception; |
108
c9fc2d303178
Added capability for border-less pop-up widgets. Simple pop-up menu.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
25 |
113
9824bee909fd
Popup menu; works for simple menus except that clicking an item doesn't close it.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
112
diff
changeset
|
26 debug { |
9824bee909fd
Popup menu; works for simple menus except that clicking an item doesn't close it.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
112
diff
changeset
|
27 import tango.util.log.Log : Log, Logger; |
9824bee909fd
Popup menu; works for simple menus except that clicking an item doesn't close it.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
112
diff
changeset
|
28 private Logger logger; |
9824bee909fd
Popup menu; works for simple menus except that clicking an item doesn't close it.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
112
diff
changeset
|
29 static this () { |
9824bee909fd
Popup menu; works for simple menus except that clicking an item doesn't close it.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
112
diff
changeset
|
30 logger = Log.getLogger ("mde.gui.widget.PopupMenu"); |
9824bee909fd
Popup menu; works for simple menus except that clicking an item doesn't close it.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
112
diff
changeset
|
31 } |
9824bee909fd
Popup menu; works for simple menus except that clicking an item doesn't close it.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
112
diff
changeset
|
32 } |
9824bee909fd
Popup menu; works for simple menus except that clicking an item doesn't close it.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
112
diff
changeset
|
33 |
131
9cff74f68b84
Major revisions to popup handling. Buttons can close menus now, plus some smaller impovements. Removed Widget module.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
121
diff
changeset
|
34 /****************************************************************************** |
113
9824bee909fd
Popup menu; works for simple menus except that clicking an item doesn't close it.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
112
diff
changeset
|
35 * Widget which pops up a menu based on a content. |
131
9cff74f68b84
Major revisions to popup handling. Buttons can close menus now, plus some smaller impovements. Removed Widget module.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
121
diff
changeset
|
36 *****************************************************************************/ |
9cff74f68b84
Major revisions to popup handling. Buttons can close menus now, plus some smaller impovements. Removed Widget module.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
121
diff
changeset
|
37 class PopupMenuWidget : APopupParentWidget |
108
c9fc2d303178
Added capability for border-less pop-up widgets. Simple pop-up menu.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff
changeset
|
38 { |
121
5b37d0400732
Widgets now receive and store their parent (IParentWidget). Infinite widget recursion checks. WidgetManager code redistributed.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
119
diff
changeset
|
39 this (IWidgetManager mgr, IParentWidget parent, widgetID id, WidgetData data, IContent c) { |
113
9824bee909fd
Popup menu; works for simple menus except that clicking an item doesn't close it.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
112
diff
changeset
|
40 content = c; |
117
aba2dd815a1f
Some tweaks to popup events and widgets.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
115
diff
changeset
|
41 WDCMinCheck (data, 1,1, content); |
121
5b37d0400732
Widgets now receive and store their parent (IParentWidget). Infinite widget recursion checks. WidgetManager code redistributed.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
119
diff
changeset
|
42 super (mgr, parent, id); |
5b37d0400732
Widgets now receive and store their parent (IParentWidget). Infinite widget recursion checks. WidgetManager code redistributed.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
119
diff
changeset
|
43 |
131
9cff74f68b84
Major revisions to popup handling. Buttons can close menus now, plus some smaller impovements. Removed Widget module.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
121
diff
changeset
|
44 popup = mgr.makeWidget (this, data.strings[0], content); |
9cff74f68b84
Major revisions to popup handling. Buttons can close menus now, plus some smaller impovements. Removed Widget module.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
121
diff
changeset
|
45 subWidgets = [popup]; |
113
9824bee909fd
Popup menu; works for simple menus except that clicking an item doesn't close it.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
112
diff
changeset
|
46 |
9824bee909fd
Popup menu; works for simple menus except that clicking an item doesn't close it.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
112
diff
changeset
|
47 adapter = mgr.renderer.getAdapter; |
9824bee909fd
Popup menu; works for simple menus except that clicking an item doesn't close it.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
112
diff
changeset
|
48 adapter.text = content.toString (1); |
9824bee909fd
Popup menu; works for simple menus except that clicking an item doesn't close it.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
112
diff
changeset
|
49 adapter.getDimensions (mw, mh); |
9824bee909fd
Popup menu; works for simple menus except that clicking an item doesn't close it.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
112
diff
changeset
|
50 w = mw; |
9824bee909fd
Popup menu; works for simple menus except that clicking an item doesn't close it.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
112
diff
changeset
|
51 h = mh; |
9824bee909fd
Popup menu; works for simple menus except that clicking an item doesn't close it.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
112
diff
changeset
|
52 } |
9824bee909fd
Popup menu; works for simple menus except that clicking an item doesn't close it.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
112
diff
changeset
|
53 |
133
9fd705793568
Fixed menu popup bug, improved recursion detection.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
131
diff
changeset
|
54 override void recursionCheck (widgetID wID, IContent c) { |
9fd705793568
Fixed menu popup bug, improved recursion detection.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
131
diff
changeset
|
55 if (wID is id && c is content) |
9fd705793568
Fixed menu popup bug, improved recursion detection.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
131
diff
changeset
|
56 throw new WidgetRecursionException (wID); |
9fd705793568
Fixed menu popup bug, improved recursion detection.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
131
diff
changeset
|
57 parent.recursionCheck (wID, c); |
9fd705793568
Fixed menu popup bug, improved recursion detection.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
131
diff
changeset
|
58 } |
9fd705793568
Fixed menu popup bug, improved recursion detection.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
131
diff
changeset
|
59 |
114
b16a534f5302
Changes for tango r4201. Added override keyword in a lot of places.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
113
diff
changeset
|
60 override int clickEvent (wdabs, wdabs, ubyte b, bool state) { |
113
9824bee909fd
Popup menu; works for simple menus except that clicking an item doesn't close it.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
112
diff
changeset
|
61 if (b == 1 && state == true) { |
9824bee909fd
Popup menu; works for simple menus except that clicking an item doesn't close it.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
112
diff
changeset
|
62 if (!pushed) { |
131
9cff74f68b84
Major revisions to popup handling. Buttons can close menus now, plus some smaller impovements. Removed Widget module.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
121
diff
changeset
|
63 parentIPPW.addChildIPPW (this); |
9cff74f68b84
Major revisions to popup handling. Buttons can close menus now, plus some smaller impovements. Removed Widget module.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
121
diff
changeset
|
64 parentIPPW.menuActive = true; |
9cff74f68b84
Major revisions to popup handling. Buttons can close menus now, plus some smaller impovements. Removed Widget module.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
121
diff
changeset
|
65 mgr.positionPopup (this, popup); |
9cff74f68b84
Major revisions to popup handling. Buttons can close menus now, plus some smaller impovements. Removed Widget module.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
121
diff
changeset
|
66 pushed = true; |
133
9fd705793568
Fixed menu popup bug, improved recursion detection.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
131
diff
changeset
|
67 } else if (!parentIPPW.parentMenuActive) { // if not a submenu |
131
9cff74f68b84
Major revisions to popup handling. Buttons can close menus now, plus some smaller impovements. Removed Widget module.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
121
diff
changeset
|
68 parentIPPW.removeChildIPPW (this); |
9cff74f68b84
Major revisions to popup handling. Buttons can close menus now, plus some smaller impovements. Removed Widget module.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
121
diff
changeset
|
69 } |
113
9824bee909fd
Popup menu; works for simple menus except that clicking an item doesn't close it.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
112
diff
changeset
|
70 } |
9824bee909fd
Popup menu; works for simple menus except that clicking an item doesn't close it.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
112
diff
changeset
|
71 return 0; |
9824bee909fd
Popup menu; works for simple menus except that clicking an item doesn't close it.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
112
diff
changeset
|
72 } |
9824bee909fd
Popup menu; works for simple menus except that clicking an item doesn't close it.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
112
diff
changeset
|
73 |
131
9cff74f68b84
Major revisions to popup handling. Buttons can close menus now, plus some smaller impovements. Removed Widget module.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
121
diff
changeset
|
74 override void removedIPPW () { |
9cff74f68b84
Major revisions to popup handling. Buttons can close menus now, plus some smaller impovements. Removed Widget module.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
121
diff
changeset
|
75 super.removedIPPW; |
113
9824bee909fd
Popup menu; works for simple menus except that clicking an item doesn't close it.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
112
diff
changeset
|
76 pushed = false; |
9824bee909fd
Popup menu; works for simple menus except that clicking an item doesn't close it.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
112
diff
changeset
|
77 } |
131
9cff74f68b84
Major revisions to popup handling. Buttons can close menus now, plus some smaller impovements. Removed Widget module.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
121
diff
changeset
|
78 |
9cff74f68b84
Major revisions to popup handling. Buttons can close menus now, plus some smaller impovements. Removed Widget module.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
121
diff
changeset
|
79 override void underMouse (bool state) { |
9cff74f68b84
Major revisions to popup handling. Buttons can close menus now, plus some smaller impovements. Removed Widget module.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
121
diff
changeset
|
80 if (state && !pushed && parentIPPW.menuActive) { |
9cff74f68b84
Major revisions to popup handling. Buttons can close menus now, plus some smaller impovements. Removed Widget module.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
121
diff
changeset
|
81 parentIPPW.addChildIPPW (this); |
9cff74f68b84
Major revisions to popup handling. Buttons can close menus now, plus some smaller impovements. Removed Widget module.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
121
diff
changeset
|
82 menuActive = true; |
133
9fd705793568
Fixed menu popup bug, improved recursion detection.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
131
diff
changeset
|
83 mgr.positionPopup (this, popup, |
9fd705793568
Fixed menu popup bug, improved recursion detection.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
131
diff
changeset
|
84 parentIPPW.parentMenuActive ? 1 : 0); |
131
9cff74f68b84
Major revisions to popup handling. Buttons can close menus now, plus some smaller impovements. Removed Widget module.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
121
diff
changeset
|
85 pushed = true; |
9cff74f68b84
Major revisions to popup handling. Buttons can close menus now, plus some smaller impovements. Removed Widget module.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
121
diff
changeset
|
86 } |
117
aba2dd815a1f
Some tweaks to popup events and widgets.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
115
diff
changeset
|
87 } |
113
9824bee909fd
Popup menu; works for simple menus except that clicking an item doesn't close it.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
112
diff
changeset
|
88 |
114
b16a534f5302
Changes for tango r4201. Added override keyword in a lot of places.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
113
diff
changeset
|
89 override void draw () { |
113
9824bee909fd
Popup menu; works for simple menus except that clicking an item doesn't close it.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
112
diff
changeset
|
90 mgr.renderer.drawButton (x,y, w,h, pushed); |
9824bee909fd
Popup menu; works for simple menus except that clicking an item doesn't close it.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
112
diff
changeset
|
91 adapter.draw (x,y); |
9824bee909fd
Popup menu; works for simple menus except that clicking an item doesn't close it.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
112
diff
changeset
|
92 } |
9824bee909fd
Popup menu; works for simple menus except that clicking an item doesn't close it.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
112
diff
changeset
|
93 |
9824bee909fd
Popup menu; works for simple menus except that clicking an item doesn't close it.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
112
diff
changeset
|
94 protected: |
9824bee909fd
Popup menu; works for simple menus except that clicking an item doesn't close it.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
112
diff
changeset
|
95 bool pushed = false; |
9824bee909fd
Popup menu; works for simple menus except that clicking an item doesn't close it.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
112
diff
changeset
|
96 IRenderer.TextAdapter adapter; |
9824bee909fd
Popup menu; works for simple menus except that clicking an item doesn't close it.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
112
diff
changeset
|
97 IContent content; |
9824bee909fd
Popup menu; works for simple menus except that clicking an item doesn't close it.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
112
diff
changeset
|
98 } |