1 /*
2  *  The ManaPlus Client
3  *  Copyright (C) 2006-2009  The Mana World Development Team
4  *  Copyright (C) 2009-2010  The Mana Developers
5  *  Copyright (C) 2011-2019  The ManaPlus Developers
6  *  Copyright (C) 2019-2021  Andrei Karas
7  *
8  *  This file is part of The ManaPlus Client.
9  *
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 2 of the License, or
13  *  any later version.
14  *
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License
21  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  */
23 
24 #ifndef GUI_WIDGETS_DROPDOWN_H
25 #define GUI_WIDGETS_DROPDOWN_H
26 
27 #include "enums/simpletypes/modal.h"
28 
29 #include "gui/widgets/basiccontainer.h"
30 
31 #include "listeners/actionlistener.h"
32 #include "listeners/focuslistener.h"
33 #include "listeners/keylistener.h"
34 #include "listeners/mouselistener.h"
35 #include "listeners/selectionlistener.h"
36 
37 #include "localconsts.h"
38 
39 class Image;
40 class ListModel;
41 class PopupList;
42 class Skin;
43 
44 /**
45  * A drop down box from which you can select different values.
46  *
47  * A ListModel provides the contents of the drop down. To be able to use
48  * DropDown you must give DropDown an implemented ListModel which represents
49  * your list.
50  */
51 class DropDown final : public ActionListener,
52                        public BasicContainer,
53                        public KeyListener,
54                        public MouseListener,
55                        public FocusListener,
56                        public SelectionListener
57 {
58     public:
59         DropDown(const Widget2 *const widget,
60                  ListModel *const listModel,
61                  const bool extended,
62                  const Modal modal,
63                  ActionListener *const listener,
64                  const std::string &eventId);
65 
66         A_DELETE_COPY(DropDown)
67 
68         ~DropDown() override final;
69 
70         /**
71          * Update the alpha value to the graphic components.
72          */
73         void updateAlpha();
74 
75         void draw(Graphics *const graphics) override final A_NONNULL(2);
76 
77         void safeDraw(Graphics *const graphics) override final A_NONNULL(2);
78 
79         void drawFrame(Graphics *const graphics) override final A_NONNULL(2);
80 
81         void safeDrawFrame(Graphics *const graphics) override final
82                            A_NONNULL(2);
83 
84         // Inherited from KeyListener
85 
86         void keyPressed(KeyEvent& event) override final;
87 
88         // Inherited from MouseListener
89 
90         void mousePressed(MouseEvent& event) override final;
91 
92         void mouseReleased(MouseEvent& event) override final;
93 
94         void mouseDragged(MouseEvent& event) override final;
95 
96         void mouseWheelMovedUp(MouseEvent& event) override final;
97 
98         void mouseWheelMovedDown(MouseEvent& event) override final;
99 
100         void setSelectedString(const std::string &str);
101 
102         std::string getSelectedString() const A_WARN_UNUSED;
103 
104         void valueChanged(const SelectionEvent& event) override final;
105 
106         void updateSelection();
107 
108         void adjustHeight();
109 
110         void dropDown();
111 
112         void foldUp();
113 
114         void hideDrop(bool event);
115 
116         int getSelected() const;
117 
118         void setSelected(int selected);
119 
120         void setListModel(ListModel *const listModel);
121 
122         ListModel *getListModel();
123 
124         void addSelectionListener(SelectionListener* listener);
125 
126         void removeSelectionListener(SelectionListener* selectionListener);
127 
128         Rect getChildrenArea() override;
129 
130         void action(const ActionEvent &actionEvent) override;
131 
132         void distributeValueChangedEvent();
133 
134     protected:
135         /**
136          * Draws the button with the little down arrow.
137          *
138          * @param graphics a Graphics object to draw with.
139          */
140         void drawButton(Graphics *const graphics) A_NONNULL(2);
141 
142         PopupList *mPopup A_NONNULLPOINTER;
143         Color mShadowColor;
144         Color mHighlightColor;
145         int mPadding;
146         int mImagePadding;
147         int mSpacing;
148         int mFoldedUpHeight;
149 
150         typedef std::list<SelectionListener*> SelectionListenerList;
151         SelectionListenerList mSelectionListeners;
152         typedef SelectionListenerList::iterator SelectionListenerIterator;
153 
154         bool mExtended;
155         bool mDroppedDown;
156         bool mPushed;
157         bool mIsDragged;
158 
159         // Add own Images.
160         static int instances;
161         static Image *buttons[2][2];
162         static ImageRect skinRect;
163         static float mAlpha;
164         static Skin *mSkin;
165 };
166 
167 #endif  // GUI_WIDGETS_DROPDOWN_H
168