Planeshift
pawsmenu.h
Go to the documentation of this file.
1 /*
2  * pawsmenu.h - Author: Ondrej Hurt
3  *
4  * Copyright (C) 2003 Atomic Blue (info@planeshift.it, http://www.atomicblue.org)
5  *
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation (version 2 of the License)
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  *
18  */
19 
20 #ifndef PAWS_MENU_HEADER
21 #define PAWS_MENU_HEADER
22 
23 #include <csutil/parray.h>
24 #include <iutil/document.h>
25 #include "pawswidget.h"
26 #include "pawstextbox.h"
27 #include "pawsbutton.h"
28 
29 
30 class pawsIMenu;
31 class pawsMenu;
32 class pawsIMenuItem;
33 class pawsMenuItem;
34 
97 {
98 public:
99  csString name;
100  csArray<csString> params;
101 };
102 
106 #define MENU_DESTROY_ACTION_NAME "MenuWantsDestroy"
107 
108 //-----------------------------------------------------------------------------
109 // interface pawsIMenu
110 //-----------------------------------------------------------------------------
111 
116 {
122 };
123 
127 class pawsIMenu : public pawsWidget
128 {
129 public:
133  pawsIMenu() { };
134 
138  pawsIMenu(const pawsIMenu &origin): pawsWidget(origin)
139  {
140 
141  }
142 
146  virtual void SetParentMenu(pawsIMenu* parentMenu) = 0;
147 
151  virtual void OnParentMenuDestroyed(pawsMenuClose reason) = 0;
152 
156  virtual void OnChildMenuDestroyed(pawsIMenu* child, pawsMenuClose reason) = 0;
157 
161  virtual void OnSiblingOpened() = 0;
162 
169  virtual void DoAction(pawsIMenuItem* item) = 0;
170 
174  virtual void SetNotify(pawsWidget* notifyTarget) = 0;
175 
176 };
177 
178 
179 //-----------------------------------------------------------------------------
180 // class pawsMenu
181 //-----------------------------------------------------------------------------
182 
187 
188 
192 class pawsMenu : public pawsIMenu
193 {
194 public:
195  pawsMenu();
196  ~pawsMenu();
197  pawsMenu(const pawsMenu &origin);
198 
199  //from pawsWidget:
200  virtual bool OnButtonPressed(int button, int keyModifier, pawsWidget* widget);
201  virtual bool OnMouseDown(int button, int modifiers, int x, int y);
202  virtual bool Setup(iDocumentNode* node);
203  virtual bool PostSetup();
204  virtual void Draw();
205 
206  //from pawsIMenu:
207  virtual void SetParentMenu(pawsIMenu* parentMenu);
208  virtual void OnParentMenuDestroyed(pawsMenuClose reason);
209  virtual void OnChildMenuDestroyed(pawsIMenu* child, pawsMenuClose reason);
210  virtual void OnSiblingOpened();
211  virtual void DoAction(pawsIMenuItem* item);
212  virtual void SetNotify(pawsWidget* notifyTarget);
213 
214 
215  /* Adds 'item' to the menu, before the 'nextItem' item.
216  * If 'nextItem' is NULL, item is added to the end.
217  * OWNERSHIP of 'item' goes to pawsMenu
218  */
219  void AddItem(pawsIMenuItem* item, pawsIMenuItem* nextItem=NULL);
220 
224  void DeleteItem(pawsIMenuItem* item);
225 
229  void SetItemAction(pawsIMenuItem* item, const pawsMenuAction &action);
230 
234  bool HasSubmenus();
235 
236 protected:
240  void SetPositionsOfItems();
241 
245  void SendOnMenuAction(const pawsMenuAction &action);
246 
250  void SendDestroyAction();
251 
257  void DestroyMenu(pawsMenuClose reason);
258 
262  int GetContentWidth();
263  int GetContentHeight();
264 
268  void Autosize();
269 
273  void SetSubmenuPos(pawsMenu* submenu, int recommY);
274 
279  csPtr<iDocumentNode> FindSubmenuNode(iDocumentNode* node, const csString &name);
280 
284  void SetButtonPositions();
285 
286 
288 
292  csArray<pawsIMenuItem*> items;
293 
297  csArray<pawsIMenu*> submenus;
298 
303 
304  pawsButton* stickyButton, * closeButton;
306 
307  csRef<iPawsImage> arrowImage; // image of right arrow indicating that menu item invokes a submenu
308 
310 
314  bool autosize;
315 
320  bool sticky;
321 
322  iGraphics2D* graphics2d;
323 };
324 
326 
327 //-----------------------------------------------------------------------------
328 // class pawsMenuItem
329 //-----------------------------------------------------------------------------
330 
331 class pawsIMenuItem : public pawsWidget
332 {
333 public:
335  pawsIMenuItem(const pawsIMenuItem &origin):pawsWidget(origin)
336  {
337 
338  }
342  virtual void Invoke()
343  {
344  }
345 
349  virtual void SetAction(const pawsMenuAction & /*action*/)
350  {
351  }
353  {
354  pawsMenuAction action;
355  return action;
356  }
357 };
358 
363 {
364 public:
365  pawsMenuItem();
366  pawsMenuItem(const pawsMenuItem &origin);
367  //from pawsWidget:
368  virtual bool Load(iDocumentNode* node);
369  virtual bool Setup(iDocumentNode* node);
370  virtual void Draw();
371 
372  //from pawsIMenuItem:
373  virtual void Invoke();
374  void SetAction(const pawsMenuAction &action);
376  {
377  return action;
378  }
379 
383  void EnableCheckbox(bool enable);
384  void EnableImage(bool enable);
385 
386  void SetImage(const csString &newImage);
387  void SetLabel(const csString &newLabel);
388 
392  void SetCheckboxImages(const csString &on, const csString &off);
393 
397  void SetCheckboxState(bool checked);
398 
403  void SetSizes(int labelWidth, int spacing, int border);
404 
405 protected:
410  void SetLayout();
411 
415  virtual void LoadAction(iDocumentNode* node);
416 
417 
421 
423 
427  bool imageEnabled, checkboxEnabled;
428 
432  int spacing;
433 
437  int border;
438 
439  iGraphics2D* graphics2d;
440 };
441 
443 
444 
445 //-----------------------------------------------------------------------------
446 // menu separator
447 //-----------------------------------------------------------------------------
448 
455 {
456 public:
458  pawsMenuSeparator(const pawsMenuSeparator &origin);
459  // from pawsWidget:
460  virtual void Draw();
461 
462 protected:
463  iGraphics2D* graphics2d;
464 };
465 
467 
468 
471 #endif
iGraphics2D * graphics2d
Definition: pawsmenu.h:322
pawsIMenuItem(const pawsIMenuItem &origin)
Definition: pawsmenu.h:335
The main base widget that all other widgets should inherit from.
Definition: pawswidget.h:116
Parent closed.
Definition: pawsmenu.h:120
pawsButton * stickyButton
Definition: pawsmenu.h:304
Sibling was opened.
Definition: pawsmenu.h:118
bool autosize
should Autosize() method be used ?
Definition: pawsmenu.h:314
When pawsMenu is created, target of notification messages (OnMenuAction) must be set by SetNotify() m...
Definition: pawsmenu.h:96
iGraphics2D * graphics2d
Definition: pawsmenu.h:463
csRef< iPawsImage > arrowImage
Definition: pawsmenu.h:307
Child closed.
Definition: pawsmenu.h:121
CREATE_PAWS_FACTORY(pawsMenu)
pawsMenuAlign
Possible vertical alignments of menu items:
Definition: pawsmenu.h:186
pawsWidget * notifyTarget
target of OnMenuAction events
Definition: pawsmenu.h:302
pawsIMenu(const pawsIMenu &origin)
Constructor.
Definition: pawsmenu.h:138
bool sticky
Is the menu "stickied" on screen ? Stickied menu can only be closed by clicking on the closing button...
Definition: pawsmenu.h:320
csArray< pawsIMenuItem * > items
list of menu items
Definition: pawsmenu.h:292
virtual void Invoke()
Makes the menu item send its menu event,.
Definition: pawsmenu.h:342
iGraphics2D * graphics2d
Definition: pawsmenu.h:439
pawsMenu is standard PAWS menu widget.
Definition: pawsmenu.h:192
int spacing
Horizontal distance between parts of the menu item (image, label, checkbox)
Definition: pawsmenu.h:432
csArray< pawsIMenu * > submenus
list of open submenus
Definition: pawsmenu.h:297
int border
Space between content of the menu item (image, label, checkbox) and border of the item...
Definition: pawsmenu.h:437
pawsTextBox * label
Definition: pawsmenu.h:419
pawsMenuAction GetAction()
Definition: pawsmenu.h:375
csString name
Definition: pawsmenu.h:99
bool imageEnabled
Is image/checkbox visible ?
Definition: pawsmenu.h:427
pawsIMenu * parentMenu
Definition: pawsmenu.h:287
A simple button widget.
Definition: pawsbutton.h:43
csArray< csString > params
Definition: pawsmenu.h:100
pawsTextBox * label
Definition: pawsmenu.h:305
pawsIMenu is common interface to menus pawsMenu.
Definition: pawsmenu.h:127
pawsMenuClose
Possible reasons of closing of menu.
Definition: pawsmenu.h:115
Action to close the menu.
Definition: pawsmenu.h:117
Close was cliked.
Definition: pawsmenu.h:119
pawsMenuAction action
Definition: pawsmenu.h:422
pawsMenuAlign align
Definition: pawsmenu.h:309
class pawsMenuItem - standard menu item with label, checkbox (optional) and image (optional)...
Definition: pawsmenu.h:362
virtual void SetAction(const pawsMenuAction &)
Sets/gets content of event that will be sent when the menu item is invoked.
Definition: pawsmenu.h:349
pawsMenuSeparator - special menu item that visually splits the menu to more parts.
Definition: pawsmenu.h:454
pawsIMenu()
Constructor.
Definition: pawsmenu.h:133
A basic text box widget.
Definition: pawstextbox.h:42
virtual pawsMenuAction GetAction()
Definition: pawsmenu.h:352
pawsButton * checkbox
Definition: pawsmenu.h:420
pawsWidget * image
Definition: pawsmenu.h:418