Planeshift
|
00001 /* 00002 * pawsmenu.h - Author: Ondrej Hurt 00003 * 00004 * Copyright (C) 2003 Atomic Blue ([email protected], http://www.atomicblue.org) 00005 * 00006 * 00007 * This program is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU General Public License 00009 * as published by the Free Software Foundation (version 2 of the License) 00010 * This program is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 * GNU General Public License for more details. 00014 * You should have received a copy of the GNU General Public License 00015 * along with this program; if not, write to the Free Software 00016 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00017 * 00018 */ 00019 00020 #ifndef PAWS_MENU_HEADER 00021 #define PAWS_MENU_HEADER 00022 00023 #include <csutil/parray.h> 00024 #include <iutil/document.h> 00025 #include "pawswidget.h" 00026 #include "pawstextbox.h" 00027 #include "pawsbutton.h" 00028 00029 00030 class pawsIMenu; 00031 class pawsMenu; 00032 class pawsIMenuItem; 00033 class pawsMenuItem; 00034 00096 class pawsMenuAction 00097 { 00098 public: 00099 csString name; 00100 csArray<csString> params; 00101 }; 00102 00106 #define MENU_DESTROY_ACTION_NAME "MenuWantsDestroy" 00107 00108 //----------------------------------------------------------------------------- 00109 // interface pawsIMenu 00110 //----------------------------------------------------------------------------- 00111 00115 enum pawsMenuClose 00116 { 00117 closeAction, 00118 closeSiblingOpened, 00119 closeCloseClicked, 00120 closeParentClosed, 00121 closeChildClosed 00122 }; 00123 00127 class pawsIMenu : public pawsWidget 00128 { 00129 public: 00133 pawsIMenu() { }; 00134 00138 pawsIMenu(const pawsIMenu &origin): pawsWidget(origin) 00139 { 00140 00141 } 00142 00146 virtual void SetParentMenu(pawsIMenu* parentMenu) = 0; 00147 00151 virtual void OnParentMenuDestroyed(pawsMenuClose reason) = 0; 00152 00156 virtual void OnChildMenuDestroyed(pawsIMenu* child, pawsMenuClose reason) = 0; 00157 00161 virtual void OnSiblingOpened() = 0; 00162 00169 virtual void DoAction(pawsIMenuItem* item) = 0; 00170 00174 virtual void SetNotify(pawsWidget* notifyTarget) = 0; 00175 00176 }; 00177 00178 00179 //----------------------------------------------------------------------------- 00180 // class pawsMenu 00181 //----------------------------------------------------------------------------- 00182 00186 enum pawsMenuAlign {alignLeft, alignCenter}; 00187 00188 00192 class pawsMenu : public pawsIMenu 00193 { 00194 public: 00195 pawsMenu(); 00196 ~pawsMenu(); 00197 pawsMenu(const pawsMenu &origin); 00198 00199 //from pawsWidget: 00200 virtual bool OnButtonPressed(int button, int keyModifier, pawsWidget* widget); 00201 virtual bool OnMouseDown(int button, int modifiers, int x, int y); 00202 virtual bool Setup(iDocumentNode* node); 00203 virtual bool PostSetup(); 00204 virtual void Draw(); 00205 00206 //from pawsIMenu: 00207 virtual void SetParentMenu(pawsIMenu* parentMenu); 00208 virtual void OnParentMenuDestroyed(pawsMenuClose reason); 00209 virtual void OnChildMenuDestroyed(pawsIMenu* child, pawsMenuClose reason); 00210 virtual void OnSiblingOpened(); 00211 virtual void DoAction(pawsIMenuItem* item); 00212 virtual void SetNotify(pawsWidget* notifyTarget); 00213 00214 00215 /* Adds 'item' to the menu, before the 'nextItem' item. 00216 * If 'nextItem' is NULL, item is added to the end. 00217 * OWNERSHIP of 'item' goes to pawsMenu 00218 */ 00219 void AddItem(pawsIMenuItem* item, pawsIMenuItem* nextItem=NULL); 00220 00224 void DeleteItem(pawsIMenuItem* item); 00225 00229 void SetItemAction(pawsIMenuItem* item, const pawsMenuAction &action); 00230 00234 bool HasSubmenus(); 00235 00236 protected: 00240 void SetPositionsOfItems(); 00241 00245 void SendOnMenuAction(const pawsMenuAction &action); 00246 00250 void SendDestroyAction(); 00251 00257 void DestroyMenu(pawsMenuClose reason); 00258 00262 int GetContentWidth(); 00263 int GetContentHeight(); 00264 00268 void Autosize(); 00269 00273 void SetSubmenuPos(pawsMenu* submenu, int recommY); 00274 00279 csPtr<iDocumentNode> FindSubmenuNode(iDocumentNode* node, const csString &name); 00280 00284 void SetButtonPositions(); 00285 00286 00287 pawsIMenu* parentMenu; 00288 00292 csArray<pawsIMenuItem*> items; 00293 00297 csArray<pawsIMenu*> submenus; 00298 00302 pawsWidget* notifyTarget; 00303 00304 pawsButton* stickyButton, * closeButton; 00305 pawsTextBox* label; 00306 00307 csRef<iPawsImage> arrowImage; // image of right arrow indicating that menu item invokes a submenu 00308 00309 pawsMenuAlign align; 00310 00314 bool autosize; 00315 00320 bool sticky; 00321 00322 iGraphics2D* graphics2d; 00323 }; 00324 00325 CREATE_PAWS_FACTORY(pawsMenu); 00326 00327 //----------------------------------------------------------------------------- 00328 // class pawsMenuItem 00329 //----------------------------------------------------------------------------- 00330 00331 class pawsIMenuItem : public pawsWidget 00332 { 00333 public: 00334 pawsIMenuItem() {} 00335 pawsIMenuItem(const pawsIMenuItem &origin):pawsWidget(origin) 00336 { 00337 00338 } 00342 virtual void Invoke() 00343 { 00344 } 00345 00349 virtual void SetAction(const pawsMenuAction & /*action*/) 00350 { 00351 } 00352 virtual pawsMenuAction GetAction() 00353 { 00354 pawsMenuAction action; 00355 return action; 00356 } 00357 }; 00358 00362 class pawsMenuItem : public pawsIMenuItem 00363 { 00364 public: 00365 pawsMenuItem(); 00366 pawsMenuItem(const pawsMenuItem &origin); 00367 //from pawsWidget: 00368 virtual bool Load(iDocumentNode* node); 00369 virtual bool Setup(iDocumentNode* node); 00370 virtual void Draw(); 00371 00372 //from pawsIMenuItem: 00373 virtual void Invoke(); 00374 void SetAction(const pawsMenuAction &action); 00375 pawsMenuAction GetAction() 00376 { 00377 return action; 00378 } 00379 00383 void EnableCheckbox(bool enable); 00384 void EnableImage(bool enable); 00385 00386 void SetImage(const csString &newImage); 00387 void SetLabel(const csString &newLabel); 00388 00392 void SetCheckboxImages(const csString &on, const csString &off); 00393 00397 void SetCheckboxState(bool checked); 00398 00403 void SetSizes(int labelWidth, int spacing, int border); 00404 00405 protected: 00410 void SetLayout(); 00411 00415 virtual void LoadAction(iDocumentNode* node); 00416 00417 00418 pawsWidget* image; 00419 pawsTextBox* label; 00420 pawsButton* checkbox; 00421 00422 pawsMenuAction action; 00423 00427 bool imageEnabled, checkboxEnabled; 00428 00432 int spacing; 00433 00437 int border; 00438 00439 iGraphics2D* graphics2d; 00440 }; 00441 00442 CREATE_PAWS_FACTORY(pawsMenuItem); 00443 00444 00445 //----------------------------------------------------------------------------- 00446 // menu separator 00447 //----------------------------------------------------------------------------- 00448 00454 class pawsMenuSeparator : public pawsIMenuItem 00455 { 00456 public: 00457 pawsMenuSeparator(); 00458 pawsMenuSeparator(const pawsMenuSeparator &origin); 00459 // from pawsWidget: 00460 virtual void Draw(); 00461 00462 protected: 00463 iGraphics2D* graphics2d; 00464 }; 00465 00466 CREATE_PAWS_FACTORY(pawsMenuSeparator); 00467 00468 00471 #endif