CLI  2.9
ui_float.cpp
1 /*
2  Copyright (c) 2006-2018, Alexis Royer, http://alexis.royer.free.fr/CLI
3 
4  All rights reserved.
5 
6  Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
7 
8  * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
9  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation
10  and/or other materials provided with the distribution.
11  * Neither the name of the CLI library project nor the names of its contributors may be used to endorse or promote products derived from this software
12  without specific prior written permission.
13 
14  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
18  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
22  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
23  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26 
27 
28 #include "cli/pch.h"
29 
30 #include "constraints.h"
31 #include "cli/ui_float.h"
32 #include "cli/param_float.h"
33 #include "cli/string_device.h"
34 #include "cli/shell.h"
35 #include "command_line_edition.h"
36 
37 
39 
40  CLI_NS_BEGIN(ui)
41 
42 
43  static const tk::String Float2Str(
45  const double D_Float
46  )
47  {
48  StringDevice cli_Str(MAX_WORD_LENGTH, false);
49  cli_Str << D_Float;
50  return cli_Str.GetString();
51  }
52 
53 
54  Float::Float(const double D_DefaultValue, const double D_MinValue, const double D_MaxValue)
55  : Line(Float2Str(D_DefaultValue), -1, -1),
56  m_dDefaultValue(D_DefaultValue), m_dMinValue(D_MinValue), m_dMaxValue(D_MaxValue)
57  {
58  }
59 
60  Float::Float(ExecutionContext& CLI_ParentContext, const double D_DefaultValue, const double D_MinValue, const double D_MaxValue)
61  : Line(CLI_ParentContext, Float2Str(D_DefaultValue), -1, -1),
62  m_dDefaultValue(D_DefaultValue), m_dMinValue(D_MinValue), m_dMaxValue(D_MaxValue)
63  {
64  }
65 
67  {
68  }
69 
70  const double Float::GetFloat(void) const
71  {
72  Help cli_Help; ParamFloat cli_Float(cli_Help);
73  if (cli_Float.SetstrValue(Line::GetLine()))
74  {
75  return (double) cli_Float;
76  }
77 
78  return m_dDefaultValue;
79  }
80 
82  {
84  }
85 
86  void Float::OnKey(const KEY E_KeyCode)
87  {
88  switch (E_KeyCode)
89  {
90  case NULL_KEY:
91  EndControl(false);
92  break;
93  //case KEY_UP:
94  //case KEY_DOWN:
95  case PAGE_UP:
96  if (GetFloat() >= m_dMaxValue)
97  {
98  // Upper bound already reached
99  Beep();
100  }
101  // Ensure max value.
102  Line::SetLine(Float2Str(m_dMaxValue), false, false);
103  break;
104  case PAGE_DOWN:
105  if (GetFloat() <= m_dMinValue)
106  {
107  // Lower bound already reached
108  Beep();
109  }
110  // Ensure min value.
111  Line::SetLine(Float2Str(m_dMinValue), false, false);
112  break;
113  case ENTER:
114  if ((GetFloat() >= m_dMinValue) && (GetFloat() <= m_dMaxValue))
115  {
116  // Reprint understood value to avoid confusions from the user.
117  double d_Value = GetFloat();
118  Line::SetLine(Float2Str(d_Value), true, false);
119  EndControl(true);
120  }
121  else
122  {
123  Beep();
124  }
125  break;
126  default:
127  Line::OnKey(E_KeyCode);
128  break;
129  }
130  }
131 
132  CLI_NS_END(ui)
133 
135 
Main namespace of the CLI library.
const tk::String GetLine(void) const
Line retrieval.
Definition: ui_line.cpp:58
Shell class definition.
Help container class.
Definition: help.h:46
Simple line user interface object.
Definition: ui_line.h:47
void SetLine(const tk::String &TK_Line, const bool B_NewLine, const bool B_CleanOnTyping)
Protected line setter for child classes.
Definition: ui_line.cpp:63
String device definition.
#define CLI_NS_END(__ns)
End a namespace definition.
Definition: namespace.h:45
void EndControl(const bool B_ExecResult)
Method to call by child classes in order to end the control execution.
Definition: ui.cpp:79
Page up arrow key.
Definition: io_device.h:195
Page down arrow key.
Definition: io_device.h:196
virtual void ResetToDefault(void)
Handler called when default value is required to be restored.
Definition: ui_float.cpp:81
String device.
Definition: string_device.h:46
Execution context.
Definition: exec_context.h:149
const double GetFloat(void) const
Float retrieval.
Definition: ui_float.cpp:70
CmdLineEdition class definition.
void Beep(void)
Sends a beep signal.
Float parameter element class.
Definition: param_float.h:47
CLI library default pre-compiled header.
Float class definition.
Float(const double D_DefaultValue, const double D_MinValue, const double D_MaxValue)
Top execution context constructor.
Definition: ui_float.cpp:54
enum _KEY KEY
Input keys.
virtual void OnKey(const KEY E_KeyCode)
Handler called on character input.
Definition: ui_float.cpp:86
#define CLI_NS_BEGIN(__ns)
Begin a namespace definition.
Definition: namespace.h:38
ParamFloat class definition.
Enter.
Definition: io_device.h:55
virtual ~Float(void)
Destructor.
Definition: ui_float.cpp:66
Object consistency insurance.
virtual void OnKey(const KEY E_KeyCode)
Handler called on character input.
Definition: ui_line.cpp:86
Null key.
Definition: io_device.h:51
virtual void ResetToDefault(void)
Handler called when default value is required to be restored.
Definition: ui_line.cpp:81
virtual const bool SetstrValue(const char *const STR_Value) const
Value setting.