00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef _WG_MENU_H_
00026 #define _WG_MENU_H_
00027
00028 #include "wg_window.h"
00029 #include "wg_painter.h"
00030 #include "wg_renderedstring.h"
00031 #include "wg_resources.h"
00032 #include "wg_timer.h"
00033 #include <vector>
00034 #include <string>
00035
00036
00037 namespace wGui
00038 {
00039
00040 class CPopupMenu;
00041
00042
00044
00045 struct SMenuItem
00046 {
00047 public:
00052 SMenuItem(std::wstring sItemText, long int iItemId = 0, CPopupMenu* pPopup = 0) :
00053 sItemText(sItemText), iItemId(iItemId), pPopup(pPopup), bSpacer(false) { }
00054
00056 SMenuItem(void) : sItemText(L""), iItemId(0), pPopup(0), bSpacer(true) { }
00057
00058 std::wstring sItemText;
00059 long int iItemId;
00060 CPopupMenu* pPopup;
00061 bool bSpacer;
00062 };
00063
00064
00066
00069
00070 class CMenuBase : public CWindow
00071 {
00072 public:
00078 CMenuBase(const CRect& WindowRect, CWindow* pParent, CFontEngine* pFontEngine = 0);
00079
00081 virtual ~CMenuBase(void);
00082
00086 virtual void InsertMenuItem(const SMenuItem& MenuItem, int iPosition = -1);
00087
00090 void RemoveMenuItem(int iPosition);
00091
00094 int GetMenuItemCount(void) const { return stdex::safe_static_cast<int>(m_MenuItems.size()); }
00095
00097 void HideActivePopup(void);
00098
00101 void SetHighlightColor(const CRGBColor& HighlightColor) { m_HighlightColor = HighlightColor; }
00102
00103
00106 virtual void Draw(void) const = 0;
00107
00113 virtual bool OnMouseButtonDown(CPoint Point, unsigned int Button);
00114
00115
00116
00119 virtual bool HandleMessage(CMessage* pMessage);
00120
00121
00122 protected:
00124 virtual void UpdateCachedRects(void) const = 0;
00125
00129 virtual void ShowActivePopup(const CRect& ParentRect, const CRect& BoundingRect) = 0;
00130
00131
00132 CFontEngine* m_pFontEngine;
00133
00134 struct s_MenuItemInfo
00135 {
00140 s_MenuItemInfo(const SMenuItem& MI, const CRenderedString& RS, const CRect& R)
00141 : MenuItem(MI), RenderedString(RS), Rect(R)
00142 { }
00143
00144 SMenuItem MenuItem;
00145 CRenderedString RenderedString;
00146 CRect Rect;
00147 };
00148 typedef std::vector<s_MenuItemInfo> t_MenuItemVector;
00149 mutable t_MenuItemVector m_MenuItems;
00150 SMenuItem* m_pHighlightedItem;
00151 mutable bool m_bCachedRectsValid;
00152 CPopupMenu* m_pActivePopup;
00153 CwgBitmapResourceHandle m_hRightArrowBitmap;
00154 CRGBColor m_HighlightColor;
00155 CTimer* m_pPopupTimer;
00156
00157
00158 private:
00159
00160 };
00161
00162
00164
00165 class CMenu : public CMenuBase
00166 {
00167 public:
00173 CMenu(const CRect& WindowRect, CWindow* pParent, CFontEngine* pFontEngine = 0);
00174
00176 virtual ~CMenu(void);
00177
00181 virtual void InsertMenuItem(const SMenuItem& MenuItem, int iPosition = -1);
00182
00183
00186 virtual void Draw(void) const;
00187
00193 virtual bool OnMouseButtonDown(CPoint Point, unsigned int Button);
00194
00195
00196
00199 virtual bool HandleMessage(CMessage* pMessage);
00200
00201
00202 protected:
00204 virtual void UpdateCachedRects(void) const;
00205
00209 virtual void ShowActivePopup(const CRect& ParentRect, const CRect& BoundingRect);
00210
00211
00212 private:
00213 void operator=(CMenu) { }
00214 };
00215
00216
00218
00219 class CPopupMenu : public CMenuBase
00220 {
00221 public:
00227 CPopupMenu(const CRect& WindowRect, CWindow* pParent, CFontEngine* pFontEngine = 0);
00228
00230 virtual ~CPopupMenu(void);
00231
00234 void Show(CPoint Position);
00235
00237 void Hide(void);
00238
00241 void HideAll(void);
00242
00244 bool IsInsideChild(const CPoint& Point) const;
00245
00248 bool IsRootPopup(void) { return !(dynamic_cast<CPopupMenu*>(m_pParentWindow)); }
00249
00254 void SetParentMenu(CMenu* pParentMenu) { m_pParentMenu = pParentMenu; }
00255
00256
00259 virtual void Draw(void) const;
00260
00265 virtual void PaintToSurface(SDL_Surface& ScreenSurface, SDL_Surface& FloatingSurface, const CPoint& Offset) const;
00266
00272 virtual bool OnMouseButtonDown(CPoint Point, unsigned int Button);
00273
00274
00275
00278 virtual bool HandleMessage(CMessage* pMessage);
00279
00280
00281 protected:
00283 virtual void UpdateCachedRects(void) const;
00284
00288 virtual void ShowActivePopup(const CRect& ParentRect, const CRect& BoundingRect);
00289
00292 CMenu* m_pParentMenu;
00293
00294
00295 private:
00296 void operator=(CPopupMenu) { }
00297 };
00298
00299 }
00300
00301
00302 #include "unit_tests/wg_menu_unittests.h"
00303
00304 #endif // _WG_MENU_H_
00305