Antares Simulator
Power System Simulator
button.hxx
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_HXX__
22 #define __ANTARES_TOOLBOX_COMPONENTS_BUTTON_BUTTON_HXX__
23 
24 namespace Antares::Component
25 {
26 template<class T>
27 Button::Button(wxWindow* parent,
28  const wxString& caption,
29  const char* bitmap,
30  T* object,
31  void (T::*method)(void*)):
32  Panel(parent),
33  pCaption(caption),
34  pUserData(NULL),
35  pIcon(NULL),
36  pClickBehavior(clkDefault),
37  pRecommendedWidth(0),
38  pMiddleWidth(0),
39  pSelected(false),
40  pPushed(false),
41  pAutoToggle(false),
42  pBold(false),
43  pHover(true)
44 {
45  // callback
46  using MethodType = void (T::*)(void*);
47  if (object)
48  {
49  pOnClick.bind(const_cast<T*>(object), reinterpret_cast<MethodType>(method));
50  }
51 
52  SetBackgroundStyle(wxBG_STYLE_CUSTOM); // Required by both GTK and Windows
53  SetBackgroundColour(parent->GetBackgroundColour());
54  loadIconFromResource(bitmap);
56 }
57 
58 template<class T>
59 inline void Button::onPopupMenu(const T* object, void (T::*method)(Button&, wxMenu&, void*))
60 {
61  if (object)
62  {
63  using MethodType = void (T::*)(Button&, wxMenu&, void*);
64  pOnPopup.bind(const_cast<T*>(object), reinterpret_cast<MethodType>(method));
65  }
66  else
67  {
68  pOnPopup.clear();
69  }
70 }
71 
72 inline void Button::onPopupMenu(const OnPopupMenu& popup)
73 {
74  pOnPopup = popup;
75 }
76 
77 template<class T>
78 inline void Button::onClick(const T* object, void (T::*method)(void*))
79 {
80  if (object)
81  {
82  using MethodType = void (T::*)(void*);
83  pOnClick.bind(const_cast<T*>(object), reinterpret_cast<MethodType>(method));
84  }
85  else
86  {
87  pOnClick.clear();
88  }
89 }
90 
92 {
93  pOnClick.clear();
94 }
95 
96 inline const wxString& Button::caption() const
97 {
98  return pCaption;
99 }
100 
101 inline void* Button::userdata() const
102 {
103  return pUserData;
104 }
105 
106 inline void Button::userdata(void* v)
107 {
108  pUserData = v;
109 }
110 
111 inline bool Button::dropDown() const
112 {
113  return (pClickBehavior == clkDropdown);
114 }
115 
116 inline void Button::dropDown(bool rhs)
117 {
118  pClickBehavior = (rhs) ? clkDropdown : clkDefault;
120  Refresh();
121 }
122 
123 inline bool Button::menu() const
124 {
125  return (pClickBehavior == clkDropdown);
126 }
127 
128 inline void Button::menu(bool rhs)
129 {
130  pClickBehavior = (rhs) ? clkMenu : clkDefault;
132  Refresh();
133 }
134 
135 inline bool Button::pushed() const
136 {
137  return pPushed;
138 }
139 
140 inline void Button::pushed(bool v)
141 {
142  if (v != pPushed)
143  {
144  pPushed = v;
145  Refresh();
146  }
147 }
148 
149 inline bool Button::autoToggle() const
150 {
151  return pAutoToggle;
152 }
153 
154 inline void Button::autoToggle(bool v)
155 {
156  pAutoToggle = v;
157 }
158 
159 inline void Button::image(const char* filename)
160 {
161  loadIconFromResource(filename);
163 }
164 
165 inline void Button::pushedColor(uint r, uint g, uint b)
166 {
167  pColorOverridePushed.Set((unsigned char)r, (unsigned char)g, (unsigned char)b);
168 }
169 
170 inline void Button::pushedColor(const wxColour& color)
171 {
172  pColorOverridePushed = color;
173 }
174 
175 inline void Button::pushedColor(const wxWindow* wnd)
176 {
177  assert(wnd && "invalid window");
178  if (wnd)
179  {
180  pColorOverridePushed = wnd->GetBackgroundColour();
181  }
182 }
183 
184 inline bool Button::bold() const
185 {
186  return pBold;
187 }
188 
189 inline void Button::bold(bool value)
190 {
191  pBold = value;
192 }
193 
194 inline bool Button::enabled() const
195 {
196  return IsEnabled();
197 }
198 
199 inline void Button::enabled(bool value)
200 {
201  Enable(value);
202 }
203 
204 inline bool Button::hover() const
205 {
206  return pHover;
207 }
208 
209 inline void Button::hover(bool value)
210 {
211  pHover = value;
212 }
213 
214 } // namespace Antares::Component
215 
216 #endif // __ANTARES_TOOLBOX_COMPONENTS_BUTTON_BUTTON_HXX__
Definition: button.h:32
bool pushed() const
Get if the button is pushed.
Definition: button.hxx:135
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
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
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
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 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
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