NCBI C++ ToolKit
wx_tools.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: wx_tools.cpp 99190 2023-02-24 16:10:10Z dzhang $
2 * ===========================================================================
3 *
4 * PUBLIC DOMAIN NOTICE
5 * National Center for Biotechnology Information
6 *
7 * This software/database is a "United States Government Work" under the
8 * terms of the United States Copyright Act. It was written as part of
9 * the author's official duties as a United States Government employee and
10 * thus cannot be copyrighted. This software/database is freely available
11 * to the public for use. The National Library of Medicine and the U.S.
12 * Government have not placed any restriction on its use or reproduction.
13 *
14 * Although all reasonable efforts have been taken to ensure the accuracy
15 * and reliability of the software and data, the NLM and the U.S.
16 * Government do not and cannot warrant the performance or results that
17 * may be obtained by using this software or data. The NLM and the U.S.
18 * Government disclaim all warranties, express or implied, including
19 * warranties of performance, merchantability or fitness for any particular
20 * purpose.
21 *
22 * Please cite the author in any work or product based on this material.
23 *
24 * ===========================================================================
25 *
26 * Authors: Paul Thiessen
27 *
28 * File Description:
29 * custom wx GUI controls
30 *
31 * ===========================================================================
32 */
33 
34 #include <ncbi_pch.hpp>
35 #include <corelib/ncbistd.hpp>
36 #include <corelib/ncbi_limits.h>
37 
39 
40 
42 
43 const int WX_TOOLS_NOTIFY_CHANGED = wxNewEventType();
44 
45 #define SEND_CHANGED_EVENT do { \
46  wxCommandEvent notify(WX_TOOLS_NOTIFY_CHANGED); \
47  AddPendingEvent(notify); } while (0)
48 
49 
50 /////////////////////////////////////////////////////////////////////////////////
51 // NotifyingSpinButton implementation
52 /////////////////////////////////////////////////////////////////////////////////
53 
54 BEGIN_EVENT_TABLE(NotifyingSpinButton, wxSpinButton)
55  EVT_SPIN_UP (-1, NotifyingSpinButton::OnSpinButtonUp)
56  EVT_SPIN_DOWN(-1, NotifyingSpinButton::OnSpinButtonDown)
58 
59 void NotifyingSpinButton::OnSpinButtonUp(wxSpinEvent& event)
60 {
61  notify->OnSpinButtonUp(event);
63 }
64 
65 void NotifyingSpinButton::OnSpinButtonDown(wxSpinEvent& event)
66 {
67  notify->OnSpinButtonDown(event);
69 }
70 
71 
72 /////////////////////////////////////////////////////////////////////////////////
73 // IntegerTextCtrl implementation
74 /////////////////////////////////////////////////////////////////////////////////
75 
76 BEGIN_EVENT_TABLE(IntegerTextCtrl, wxTextCtrl)
77  EVT_TEXT (-1, IntegerTextCtrl::DoValidate)
78  EVT_TEXT_ENTER (-1, IntegerTextCtrl::OnChange)
80 
81 IntegerTextCtrl::IntegerTextCtrl(wxWindow* parent, wxWindowID id, const wxString& value,
82  const wxPoint& pos, const wxSize& size, long style,
83  const wxValidator& validator, const wxString& name) :
84  wxTextCtrl(parent, id, value, pos, size, style | wxTE_PROCESS_ENTER, validator, name),
85  minVal(kMin_Int), maxVal(kMax_Int)
86 {
87 }
88 
89 void IntegerTextCtrl::DoValidate(wxCommandEvent&)
90 {
91  if (IsValidInteger())
92  SetBackgroundColour(*wxWHITE);
93  else
94  SetBackgroundColour(*wxRED);
95  Refresh();
96 }
97 
98 void IntegerTextCtrl::OnChange(wxCommandEvent& event)
99 {
101 }
102 
103 void IntegerTextCtrl::SetAllowedRange(int min, int max, int incr)
104 {
105  minVal = min;
106  maxVal = max;
107  incrVal = incr;
108 }
109 
111 {
112  long longVal;
113  int intVal;
114  if (!GetValue().ToLong(&longVal)) return false;
115  intVal = (int) longVal;
116  return (intVal >= minVal && intVal <= maxVal);
117 }
118 
119 
120 /////////////////////////////////////////////////////////////////////////////////
121 // IntegerSpinCtrl implementation
122 /////////////////////////////////////////////////////////////////////////////////
123 
125  int min, int max, int increment, int initial,
126  const wxPoint& textCtrlPos, const wxSize& textCtrlSize, long textCtrlStyle,
127  const wxPoint& spinCtrlPos, const wxSize& spinCtrlSize) :
128  minVal(min), maxVal(max), incrVal(increment)
129 {
130  iTextCtrl = new IntegerTextCtrl(parent, -1, wxT(""), textCtrlPos, textCtrlSize, textCtrlStyle);
131  iTextCtrl->SetAllowedRange(min, max, increment);
132 
133  //spinButton = new NotifyingSpinButton(this,
134  // parent, -1, spinCtrlPos, spinCtrlSize, wxSP_VERTICAL | wxSP_ARROW_KEYS);
135  //spinButton->SetRange(-1, 1); // position irrelevant; just need the button GUI
136 
137  // clamp and set initial value
138  if (initial < min) initial = min;
139  if (initial > max) initial = max;
140  SetInteger(initial);
141 }
142 
144 {
145  // check for allowed value
146  if (value < minVal || value > maxVal) return false;
147 
148  wxString strVal;
149  strVal.Printf(wxT("%i"), value);
150  iTextCtrl->SetValue(strVal);
151  //spinButton->SetValue(0);
152  return true;
153 }
154 
155 void IntegerSpinCtrl::OnSpinButtonUp(wxSpinEvent& event)
156 {
157  int value;
158  if (!GetInteger(&value)) return;
159  value += incrVal;
160  if (value > maxVal) value = maxVal;
161  SetInteger(value);
162 }
163 
164 void IntegerSpinCtrl::OnSpinButtonDown(wxSpinEvent& event)
165 {
166  int value;
167  if (!GetInteger(&value)) return;
168  value -= incrVal;
169  if (value < minVal) value = minVal;
170  SetInteger(value);
171 }
172 
174 {
175  long longValue;
176  if (!iTextCtrl->GetValue().ToLong(&longValue)) return false;
177  *value = (int) longValue;
178  return (iTextCtrl->IsValidInteger());
179 }
180 
182 {
183  long longValue;
184  if (!iTextCtrl->GetValue().ToLong(&longValue)) return false;
185  if (minVal < 0 || maxVal < 0 || longValue < 0)
186  ERR_POST(Warning << "IntegerSpinCtrl::GetUnsignedInteger() - possible signed/unsigned mismatch");
187  *value = (unsigned int) longValue;
188  return (iTextCtrl->IsValidInteger());
189 }
190 
191 
192 /////////////////////////////////////////////////////////////////////////////////
193 // FloatingPointTextCtrl implementation
194 /////////////////////////////////////////////////////////////////////////////////
195 
196 BEGIN_EVENT_TABLE(FloatingPointTextCtrl, wxTextCtrl)
197  EVT_TEXT (-1, FloatingPointTextCtrl::DoValidate)
198  EVT_TEXT_ENTER (-1, FloatingPointTextCtrl::OnChange)
200 
201 FloatingPointTextCtrl::FloatingPointTextCtrl(wxWindow* parent, wxWindowID id, const wxString& value,
202  const wxPoint& pos, const wxSize& size, long style,
203  const wxValidator& validator, const wxString& name) :
204  wxTextCtrl(parent, id, value, pos, size, style | wxTE_PROCESS_ENTER, validator, name),
205  minVal(kMin_Double), maxVal(kMax_Double)
206 {
207 }
208 
209 void FloatingPointTextCtrl::DoValidate(wxCommandEvent& event)
210 {
211  if (IsValidDouble())
212  SetBackgroundColour(*wxWHITE);
213  else
214  SetBackgroundColour(*wxRED);
215  Refresh();
216 }
217 
218 void FloatingPointTextCtrl::OnChange(wxCommandEvent& event)
219 {
221 }
222 
224 {
225  minVal = min;
226  maxVal = max;
227 }
228 
230 {
231  double doubleVal;
232  return (GetValue().ToDouble(&doubleVal) && doubleVal >= minVal && doubleVal <= maxVal);
233 }
234 
235 
236 /////////////////////////////////////////////////////////////////////////////////
237 // FloatingPointSpinCtrl implementation
238 /////////////////////////////////////////////////////////////////////////////////
239 
241  double min, double max, double increment, double initial,
242  const wxPoint& textCtrlPos, const wxSize& textCtrlSize, long textCtrlStyle,
243  const wxPoint& spinCtrlPos, const wxSize& spinCtrlSize) :
244  minVal(min), maxVal(max), incrVal(increment)
245 {
246  fpTextCtrl = new FloatingPointTextCtrl(parent, -1, wxT(""), textCtrlPos, textCtrlSize, textCtrlStyle);
248 
249 // spinButton = new NotifyingSpinButton(this,
250 // parent, -1, spinCtrlPos, spinCtrlSize, wxSP_VERTICAL | wxSP_ARROW_KEYS);
251 // spinButton->SetRange(-1, 1); // position irrelevant; just need the button GUI
252 
253  // clamp and set initial value
254  if (initial < min) initial = min;
255  if (initial > max) initial = max;
256  SetDouble(initial);
257 }
258 
260 {
261  // check allowed range
262  if (value < minVal || value > maxVal) return false;
263 
264  wxString strVal;
265  strVal.Printf(wxT("%g"), value);
266  fpTextCtrl->SetValue(strVal);
267 // spinButton->SetValue(0);
268  return true;
269 }
270 
272 {
273  double value;
274  if (!GetDouble(&value)) return;
275  value += incrVal;
276  if (value > maxVal) value = maxVal;
277  SetDouble(value);
278 }
279 
281 {
282  double value;
283  if (!GetDouble(&value)) return;
284  value -= incrVal;
285  if (value < minVal) value = minVal;
286  SetDouble(value);
287 }
288 
290 {
291  return (fpTextCtrl->GetValue().ToDouble(value) && fpTextCtrl->IsValidDouble());
292 }
293 
294 
295 /////////////////////////////////////////////////////////////////////////////////
296 // GetFloatingPointDialog implementation
297 /////////////////////////////////////////////////////////////////////////////////
298 
299 BEGIN_EVENT_TABLE(GetFloatingPointDialog, wxDialog)
300  EVT_BUTTON(-1, GetFloatingPointDialog::OnButton)
303 
305  const wxString& message, const wxString& title,
306  double min, double max, double increment, double initial) :
307  wxDialog(parent, -1, title, wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE)
308 {
309  // code modified (heavily) from wxDesigner C++ output of fp_dialog.wdr
310  wxPanel *panel = new wxPanel(this, -1);
311  wxBoxSizer *item0 = new wxBoxSizer(wxVERTICAL);
312  wxStaticText *item1 = new wxStaticText(panel, -1, message, wxDefaultPosition, wxDefaultSize, 0);
313  item0->Add(item1, 0, wxALIGN_CENTRE|wxALL, 5);
314  wxFlexGridSizer *grid = new wxFlexGridSizer(1, 0, 0, 0);
315  grid->AddGrowableCol(1);
316 
317  buttonOK = new wxButton(panel, -1, wxT("OK"), wxDefaultPosition, wxDefaultSize, 0);
318  grid->Add(buttonOK, 0, wxGROW|wxALIGN_CENTER_HORIZONTAL|wxRIGHT, 5);
319 
320  fpSpinCtrl = new FloatingPointSpinCtrl(panel,
321  min, max, increment, initial,
322  wxDefaultPosition, wxSize(80, SPIN_CTRL_HEIGHT), 0,
323  wxDefaultPosition, wxSize(-1, SPIN_CTRL_HEIGHT));
324  grid->Add(fpSpinCtrl->GetTextCtrl(), 0, wxGROW|wxALIGN_CENTER_VERTICAL, 5);
325  //grid->Add(fpSpinCtrl->GetSpinButton(), 0, wxALIGN_CENTRE, 5);
326 
327  item0->Add(grid, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5);
328  panel->SetAutoLayout(true);
329  panel->SetSizer(item0);
330  item0->Fit(this);
331  item0->Fit(panel);
332  item0->SetSizeHints(this);
333 }
334 
336 {
337  delete fpSpinCtrl;
338 }
339 
341 {
342  double returnValue;
343  fpSpinCtrl->GetDouble(&returnValue);
344  return returnValue;
345 }
346 
347 void GetFloatingPointDialog::OnButton(wxCommandEvent& event)
348 {
349  if (event.GetEventObject() == buttonOK) {
350  double test;
351  if (fpSpinCtrl->GetDouble(&test))
352  EndModal(wxOK);
353  else
354  wxBell();
355  } else {
356  event.Skip();
357  }
358 }
359 
360 void GetFloatingPointDialog::OnCloseWindow(wxCloseEvent& event)
361 {
362  EndModal(wxCANCEL);
363 }
364 
virtual void OnSpinButtonDown(wxSpinEvent &event)=0
bool GetDouble(double *value) const
Definition: wx_tools.cpp:289
void OnSpinButtonDown(wxSpinEvent &event)
Definition: wx_tools.cpp:280
FloatingPointSpinCtrl(wxWindow *parent, double min, double max, double increment, double initial, const wxPoint &textCtrlPos, const wxSize &textCtrlSize, long textCtrlStyle, const wxPoint &spinCtrlPos, const wxSize &spinCtrlSize)
Definition: wx_tools.cpp:240
void OnSpinButtonUp(wxSpinEvent &event)
Definition: wx_tools.cpp:271
FloatingPointTextCtrl * fpTextCtrl
Definition: wx_tools.hpp:198
bool SetDouble(double value)
Definition: wx_tools.cpp:259
void SetAllowedRange(double min, double max)
Definition: wx_tools.cpp:223
bool IsValidDouble(void) const
Definition: wx_tools.cpp:229
void OnChange(wxCommandEvent &event)
Definition: wx_tools.cpp:218
void DoValidate(wxCommandEvent &event)
Definition: wx_tools.cpp:209
FloatingPointSpinCtrl * fpSpinCtrl
Definition: wx_tools.hpp:226
void OnButton(wxCommandEvent &event)
Definition: wx_tools.cpp:347
double GetValue(void)
Definition: wx_tools.cpp:340
void OnCloseWindow(wxCloseEvent &event)
Definition: wx_tools.cpp:360
IntegerTextCtrl * iTextCtrl
Definition: wx_tools.hpp:144
IntegerSpinCtrl(wxWindow *parent, int min, int max, int increment, int initial, const wxPoint &textCtrlPos, const wxSize &textCtrlSize, long textCtrlStyle, const wxPoint &spinCtrlPos, const wxSize &spinCtrlSize)
Definition: wx_tools.cpp:124
bool GetUnsignedInteger(unsigned int *value) const
Definition: wx_tools.cpp:181
bool GetInteger(int *value) const
Definition: wx_tools.cpp:173
void OnSpinButtonUp(wxSpinEvent &event)
Definition: wx_tools.cpp:155
bool SetInteger(int value)
Definition: wx_tools.cpp:143
void OnSpinButtonDown(wxSpinEvent &event)
Definition: wx_tools.cpp:164
void SetAllowedRange(int min, int max, int incr)
Definition: wx_tools.cpp:103
bool IsValidInteger(void) const
Definition: wx_tools.cpp:110
void DoValidate(wxCommandEvent &event)
Definition: wx_tools.cpp:89
void OnChange(wxCommandEvent &event)
Definition: wx_tools.cpp:98
void OnSpinButtonUp(wxSpinEvent &event)
Definition: wx_tools.cpp:59
void OnSpinButtonDown(wxSpinEvent &event)
Definition: wx_tools.cpp:65
CustomSpinCtrl * notify
Definition: wx_tools.hpp:88
Include a standard set of the NCBI C++ Toolkit most basic headers.
#define test(a, b, c, d, e)
Definition: numeric.c:170
#define ERR_POST(message)
Error posting with file, line number information but without error codes.
Definition: ncbidiag.hpp:186
void Warning(CExceptionArgs_Base &args)
Definition: ncbiexpt.hpp:1191
#define kMin_Int
Definition: ncbi_limits.h:183
#define kMin_Double
Definition: ncbi_limits.h:207
#define kMax_Double
Definition: ncbi_limits.h:208
#define kMax_Int
Definition: ncbi_limits.h:184
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
unsigned int
A callback function used to compare two keys in a database.
Definition: types.hpp:1210
END_EVENT_TABLE()
#define wxT(x)
Definition: muParser.cpp:41
const struct ncbi::grid::netcache::search::fields::SIZE size
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1227
T max(T x_, T y_)
T min(T x_, T y_)
#define SEND_CHANGED_EVENT
Definition: wx_tools.cpp:45
const int WX_TOOLS_NOTIFY_CHANGED
Definition: wx_tools.cpp:43
#define const
Definition: zconf.h:232
Modified on Sun Apr 21 03:39:32 2024 by modify_doxy.py rev. 669887