File: spinctrlfloat.cpp

package info (click to toggle)
mcrl2 201409.0-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd
  • size: 46,348 kB
  • ctags: 29,960
  • sloc: cpp: 213,160; ansic: 16,219; python: 13,238; yacc: 309; lex: 214; xml: 197; makefile: 83; sh: 82; pascal: 17
file content (128 lines) | stat: -rwxr-xr-x 3,355 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Author(s): S.W.C. Ploeger, D. Reniers
// Copyright: see the accompanying file COPYING or copy at
// https://svn.win.tue.nl/trac/MCRL2/browser/trunk/COPYING
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

#include "wx.hpp" // precompiled headers

#include <cmath>
#include <string>

#include "mcrl2/utilities/spinctrlfloat.h"

using namespace std;

/**
 * \brief Rounds a double to an integer value
 **/
static int roundToInt(double f)
{
  double intpart;
  modf(f + 0.5, &intpart);
  return static_cast< int >(intpart);
}

namespace mcrl2
{
namespace utilities
{
namespace wx
{
/**
 * \param[in] parent the wxWidgets window object of which this object will be a child
 * \param[in] id a wxWidgets identifier for this widget
 * \param[in] p_Min the minimum value
 * \param[in] p_Max the maximum value
 * \param[in] p_Rate the resolution of the spin control
 * \param[in] p_Init the initial value
 * \param[in] pos the position for this widget
 * \param[in] size the size of this widget
 **/
wxSpinCtrlFloat::wxSpinCtrlFloat(
  wxWindow* parent,
  wxWindowID /*id*/,
  double p_Min,
  double p_Max,
  double p_Rate,
  double p_Init,
  const wxPoint& pos,
  const wxSize&  size)
  : wxPanel(
    parent,
    wxID_ANY,
    pos,
    size,
    wxNO_BORDER),
  m_Rate(p_Rate)
{
  wxBoxSizer* Sizer = new wxBoxSizer(wxHORIZONTAL);
  m_TextCtrl = new wxTextCtrl(this, 0, wxEmptyString, wxDefaultPosition,
                              wxDefaultSize, wxTE_PROCESS_ENTER);
  m_SpinButton = new wxSpinButton(this, 1, wxDefaultPosition, wxDefaultSize,
                                  wxSP_VERTICAL);

  // spin button should be at most as high as the text control
  int w;
  int h;
  m_TextCtrl->GetSize(&w, &h);
  m_SpinButton->SetSizeHints(-1, h, -1, h);

  Sizer->Add(m_TextCtrl, 1, wxEXPAND, 0);
  Sizer->Add(m_SpinButton , 0, wxEXPAND, 0);

  m_SpinButton->SetRange(roundToInt(p_Min/p_Rate), roundToInt(p_Max/p_Rate));
  SetValue(p_Init);

  Sizer->SetMinSize(size);
  this->SetSizer(Sizer);
  Sizer->SetSizeHints(this);
}

BEGIN_EVENT_TABLE(wxSpinCtrlFloat, wxPanel)
  EVT_TEXT_ENTER(0, wxSpinCtrlFloat::OnEnter)
  EVT_SPIN(1, wxSpinCtrlFloat::OnSpin)
END_EVENT_TABLE()

void wxSpinCtrlFloat::OnEnter(wxCommandEvent& event)
{
  string str(event.GetString().mb_str(wxConvUTF8));

  float f;
  sscanf(str.c_str(), "%f", &f);

  f = (std::min)(m_SpinButton->GetMax() * m_Rate, (std::max)(static_cast < double >(f), m_SpinButton->GetMin() * m_Rate));
  SetValue(f);

  wxCommandEvent NewEvent(wxEVT_COMMAND_SPINCTRL_UPDATED, GetId());
  NewEvent.SetEventObject(this);
  GetParent()->GetEventHandler()->ProcessEvent(NewEvent);
}

void wxSpinCtrlFloat::OnSpin(wxSpinEvent& event)
{
  int i = event.GetInt();
  m_TextCtrl->SetValue(wxString::Format(wxT("%2.1f"), i * m_Rate));

  wxCommandEvent NewEvent(wxEVT_COMMAND_SPINCTRL_UPDATED, GetId());
  NewEvent.SetEventObject(this);
  GetParent()->GetEventHandler()->ProcessEvent(NewEvent);
}

double wxSpinCtrlFloat::GetValue()
{
  return m_Rate * m_SpinButton->GetValue();
}

void wxSpinCtrlFloat::SetValue(float v)
{
  m_TextCtrl->SetValue(wxString::Format(wxT("%2.1f"), v));
  m_SpinButton->SetValue(roundToInt(v/m_Rate));
}
}
}
}

// -- end -----------------------------------------------------------