Antares Simulator
Power System Simulator
button.h
1 /*
2  * Copyright 2007-2025, RTE (https://www.rte-france.com)
3  * See AUTHORS.txt
4  * SPDX-License-Identifier: MPL-2.0
5  * This file is part of Antares-Simulator,
6  * Adequacy and Performance assessment for interconnected energy networks.
7  *
8  * Antares_Simulator is free software: you can redistribute it and/or modify
9  * it under the terms of the Mozilla Public Licence 2.0 as published by
10  * the Mozilla Foundation, either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * Antares_Simulator is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * Mozilla Public Licence 2.0 for more details.
17  *
18  * You should have received a copy of the Mozilla Public Licence 2.0
19  * along with Antares_Simulator. If not, see <https://opensource.org/license/mpl-2-0/>.
20  */
21 #ifndef __ANTARES_TOOLBOX_COMPONENTS_BUTTON_BUTTON_H__
22 #define __ANTARES_TOOLBOX_COMPONENTS_BUTTON_BUTTON_H__
23 
24 #include <yuni/yuni.h>
25 #include <wx/bitmap.h>
26 #include <ui/common/component/panel.h>
27 #include <vector>
28 
29 namespace Antares::Component
30 {
31 class Button final: public Panel
32 {
33 public:
35  using OnClick = Yuni::Bind<void(void*)>;
37  using OnPopupMenu = Yuni::Bind<void(Button&, wxMenu&, void*)>;
39  using Vector = std::vector<Button*>;
40 
41  enum ClickBehavior
42  {
43  clkDefault,
44  clkDropdown,
45  clkMenu,
46  };
47 
48 public:
50 
51 
54  Button(wxWindow* parent, const wxString& caption);
58  Button(wxWindow* parent, const wxString& caption, const char* bitmap);
62  Button(wxWindow* parent, const wxString& caption, const char* bitmap, const OnClick& onclick);
63 
67  template<class T>
68  Button(wxWindow* parent,
69  const wxString& caption,
70  const char* bitmap,
71  T* object,
72  void (T::*method)(void*));
73 
75  virtual ~Button();
77 
79 
80  const wxString& caption() const;
83  void caption(const wxString& rhs);
84  void caption(const wxChar* rhs);
86 
88 
89  void image(const char* filename);
92 
94 
95  bool enabled() const;
98  void enabled(bool value);
100 
102 
103  void* userdata() const;
106  void userdata(void* v);
108 
110 
111  bool dropDown() const;
114  void dropDown(bool rhs);
116 
118 
119  bool menu() const;
122  void menu(bool rhs);
124 
126 
127  bool pushed() const;
130  void pushed(bool v);
131 
133  bool autoToggle() const;
135  void autoToggle(bool v);
136 
138  void pushedColor(uint r, uint g, uint b);
139  void pushedColor(const wxColour& color);
140  void pushedColor(const wxWindow* wnd);
142 
144 
145  bool bold() const;
148  void bold(bool value);
150 
152 
153  void hover(bool value);
155  bool hover() const;
157 
161  template<class T>
162  void onPopupMenu(const T* object, void (T::*method)(Button&, wxMenu&, void*));
166  void onPopupMenu(const OnPopupMenu& popup);
167 
171  template<class T>
172  void onClick(const T* object, void (T::*method)(void*));
173 
175  void disconnectClickEvent();
176 
178  virtual void onMouseClick() override;
179 
181  virtual void onMouseEnter() override;
183  virtual void onMouseLeave() override;
184 
186  virtual void onMouseUp(wxMouseEvent& evt) override;
187 
188 protected:
190  void loadIconFromResource(const char* filename);
194  void internalClick();
195 
197  void onDraw(wxPaintEvent& evt);
198 
200  void onEraseBackground(wxEraseEvent&)
201  {
202  }
203 
205  // (may lead to SegV otherwise)
206  virtual bool triggerMouseClickEvent() const override
207  {
208  return false;
209  }
210 
211 private:
213  wxString pCaption;
215  OnClick pOnClick;
217  OnPopupMenu pOnPopup;
219  void* pUserData;
221  wxBitmap* pIcon;
223  ClickBehavior pClickBehavior;
224 
226  int pRecommendedWidth;
227  int pMiddleWidth;
228  bool pSelected;
229  bool pPushed;
230  bool pAutoToggle;
231  bool pBold;
232  bool pHover;
233 
235  wxColour pColorOverridePushed;
236 
238  wxSize pCaptionExtent;
239  // Event table
240  DECLARE_EVENT_TABLE()
241 
242 }; // class Button
243 
244 } // namespace Antares::Component
245 
246 #include "button.hxx"
247 
248 #endif // __ANTARES_TOOLBOX_COMPONENTS_BUTTON_BUTTON_H__
Definition: button.h:32
bool pushed() const
Get if the button is pushed.
Definition: button.hxx:135
virtual bool triggerMouseClickEvent() const override
Tells to Component::Panel to not trigger its own mouse click event.
Definition: button.h:206
void disconnectClickEvent()
Disconnect the onClick event.
Definition: button.hxx:91
bool menu() const
Get if a click on the button will produce a menu.
Definition: button.hxx:123
bool bold() const
Get if the font should be bold.
Definition: button.hxx:184
void onDraw(wxPaintEvent &evt)
Event: draw the panel.
Definition: button.cpp:232
virtual void onMouseEnter() override
The mouse has entered.
Definition: button.cpp:399
bool autoToggle() const
Get if the button is in auto-toggle mode.
Definition: button.hxx:149
Button(wxWindow *parent, const wxString &caption)
Constructor.
Definition: button.cpp:64
virtual void onMouseClick() override
The panel has been clicked (delayed)
Definition: button.cpp:394
void onClick(const T *object, void(T::*method)(void *))
Set the onClick event.
Definition: button.hxx:78
void precalculateCoordinates()
Pre-calculate all coordinates.
Definition: button.cpp:165
void onEraseBackground(wxEraseEvent &)
UI: Erase background, empty to avoid flickering.
Definition: button.h:200
bool dropDown() const
Get if the button has a drop down menu.
Definition: button.hxx:111
bool enabled() const
Get if the button is enabled.
Definition: button.hxx:194
void internalClick()
internal click
Definition: button.cpp:418
Yuni::Bind< void(void *)> OnClick
Event: User click.
Definition: button.h:35
void onPopupMenu(const T *object, void(T::*method)(Button &, wxMenu &, void *))
Set the handler for creating the popup menu.
Definition: button.hxx:59
const wxString & caption() const
Get the caption of the button.
Definition: button.hxx:96
Yuni::Bind< void(Button &, wxMenu &, void *)> OnPopupMenu
Event: Popup menu.
Definition: button.h:37
void loadIconFromResource(const char *filename)
Preload the Icon.
Definition: button.cpp:147
std::vector< Button * > Vector
Vector.
Definition: button.h:39
virtual ~Button()
Destructor.
Definition: button.cpp:136
virtual void onMouseLeave() override
The mouse has leaved.
Definition: button.cpp:409
virtual void onMouseUp(wxMouseEvent &evt) override
Click up.
Definition: button.cpp:426
void image(const char *filename)
Set the image filename.
Definition: button.hxx:159
void pushedColor(uint r, uint g, uint b)
Set the pushed color.
Definition: button.hxx:165
void * userdata() const
Get the user-data.
Definition: button.hxx:101
Panel implementation.
Definition: panel.h:34