1 //
2 // Copyright (c) 2008-2017 the Urho3D project.
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to deal
6 // in the Software without restriction, including without limitation the rights
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 // copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 // THE SOFTWARE.
21 //
22 
23 #pragma once
24 
25 #include "../UI/Button.h"
26 
27 namespace Urho3D
28 {
29 
30 /// %Menu %UI element that optionally shows a popup.
31 class URHO3D_API Menu : public Button
32 {
33     URHO3D_OBJECT(Menu, Button);
34 
35     using UIElement::LoadXML;
36 
37 public:
38     /// Construct.
39     Menu(Context* context);
40     /// Destruct.
41     virtual ~Menu();
42     /// Register object factory.
43     static void RegisterObject(Context* context);
44 
45     /// Load from XML data with style. Return true if successful.
46     virtual bool LoadXML(const XMLElement& source, XMLFile* styleFile, bool setInstanceDefault = false);
47     /// Save as XML data. Return true if successful.
48     virtual bool SaveXML(XMLElement& dest) const;
49 
50     /// Perform UI element update.
51     virtual void Update(float timeStep);
52     /// React to mouse hover.
53     virtual void OnHover(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers, Cursor* cursor);
54     /// React to the popup being shown.
55     virtual void OnShowPopup();
56 
57     /// React to the popup being hidden.
OnHidePopup()58     virtual void OnHidePopup() { }
59 
60     /// Set popup element to show on selection.
61     void SetPopup(UIElement* element);
62     /// Set popup element offset.
63     void SetPopupOffset(const IntVector2& offset);
64     /// Set popup element offset.
65     void SetPopupOffset(int x, int y);
66     /// Force the popup to show or hide.
67     void ShowPopup(bool enable);
68     /// Set accelerator key (set zero key code to disable.)
69     void SetAccelerator(int key, int qualifiers);
70 
71     /// Return popup element.
GetPopup()72     UIElement* GetPopup() const { return popup_; }
73 
74     /// Return popup element offset.
GetPopupOffset()75     const IntVector2& GetPopupOffset() const { return popupOffset_; }
76 
77     /// Return whether popup is open.
GetShowPopup()78     bool GetShowPopup() const { return showPopup_; }
79 
80     /// Return accelerator key code, 0 if disabled.
GetAcceleratorKey()81     int GetAcceleratorKey() const { return acceleratorKey_; }
82 
83     /// Return accelerator qualifiers.
GetAcceleratorQualifiers()84     int GetAcceleratorQualifiers() const { return acceleratorQualifiers_; }
85 
86 protected:
87     /// Filter implicit attributes in serialization process.
88     virtual bool FilterPopupImplicitAttributes(XMLElement& dest) const;
89     /// Popup element.
90     SharedPtr<UIElement> popup_;
91     /// Popup element offset.
92     IntVector2 popupOffset_;
93     /// Show popup flag.
94     bool showPopup_;
95     /// Accelerator key code.
96     int acceleratorKey_;
97     /// Accelerator qualifiers.
98     int acceleratorQualifiers_;
99 
100 private:
101     /// Handle press and release for selection and toggling popup visibility.
102     void HandlePressedReleased(StringHash eventType, VariantMap& eventData);
103     /// Handle global focus change to check for hiding the popup.
104     void HandleFocusChanged(StringHash eventType, VariantMap& eventData);
105     /// Handle keypress for checking accelerator.
106     void HandleKeyDown(StringHash eventType, VariantMap& eventData);
107     /// Auto popup flag.
108     bool autoPopup_;
109 };
110 
111 }
112