CLI  2.9
ui_int.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_int.h"
32 #include "cli/param_int.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 Int2Str(
45  const int I_Int
46  )
47  {
48  StringDevice cli_Str(MAX_WORD_LENGTH, false);
49  cli_Str << I_Int;
50  return cli_Str.GetString();
51  }
52 
53 
54  Int::Int(const int I_DefaultValue, const int I_MinValue, const int I_MaxValue)
55  : Line(Int2Str(I_DefaultValue), -1, -1),
56  m_iDefaultValue(I_DefaultValue), m_iMinValue(I_MinValue), m_iMaxValue(I_MaxValue)
57  {
58  }
59 
60  Int::Int(ExecutionContext& CLI_ParentContext, const int I_DefaultValue, const int I_MinValue, const int I_MaxValue)
61  : Line(CLI_ParentContext, Int2Str(I_DefaultValue), -1, -1),
62  m_iDefaultValue(I_DefaultValue), m_iMinValue(I_MinValue), m_iMaxValue(I_MaxValue)
63  {
64  }
65 
66  Int::~Int(void)
67  {
68  }
69 
70  const int Int::GetInt(void) const
71  {
72  Help cli_Help; ParamInt cli_Int(cli_Help);
73  if (cli_Int.SetstrValue(Line::GetLine()))
74  {
75  return (int) cli_Int;
76  }
77 
78  return m_iDefaultValue;
79  }
80 
82  {
84  }
85 
86  void Int::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  if (GetInt() < m_iMinValue)
95  {
96  // Completely out of bounds: return to min value.
97  Beep();
98  Line::SetLine(Int2Str(m_iMinValue), false, false);
99  }
100  else if (GetInt() < m_iMaxValue)
101  {
102  Line::SetLine(Int2Str(GetInt() + 1), false, false);
103  }
104  else
105  {
106  // Upper bound already reached: ensure max value.
107  Beep();
108  Line::SetLine(Int2Str(m_iMaxValue), false, false);
109  }
110  break;
111  case KEY_DOWN:
112  if (GetInt() > m_iMaxValue)
113  {
114  // Completely out of bounds: return to max value.
115  Beep();
116  Line::SetLine(Int2Str(m_iMaxValue), false, false);
117  }
118  else if (GetInt() > m_iMinValue)
119  {
120  Line::SetLine(Int2Str(GetInt() - 1), false, false);
121  }
122  else
123  {
124  // Lower bound already reached: ensure min value.
125  Beep();
126  Line::SetLine(Int2Str(m_iMinValue), false, false);
127  }
128  break;
129  case PAGE_UP:
130  if (GetInt() >= m_iMaxValue)
131  {
132  // Upper bound already reached.
133  Beep();
134  }
135  // Ensure max value.
136  Line::SetLine(Int2Str(m_iMaxValue), false, false);
137  break;
138  case PAGE_DOWN:
139  if (GetInt() <= m_iMinValue)
140  {
141  // Lower bound already reached.
142  Beep();
143  }
144  // Ensure min value.
145  Line::SetLine(Int2Str(m_iMinValue), false, false);
146  break;
147  case ENTER:
148  if ((GetInt() >= m_iMinValue) && (GetInt() <= m_iMaxValue))
149  {
150  // Reprint understood value to avoid confusions from the user.
151  Line::SetLine(Int2Str(GetInt()), true, false);
152  EndControl(true);
153  }
154  else
155  {
156  Beep();
157  }
158  break;
159  default:
160  Line::OnKey(E_KeyCode);
161  break;
162  }
163  }
164 
165  CLI_NS_END(ui)
166 
168 
Main namespace of the CLI library.
Integer parameter element class.
Definition: param_int.h:47
virtual void OnKey(const KEY E_KeyCode)
Handler called on character input.
Definition: ui_int.cpp:86
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
virtual ~Int(void)
Destructor.
Definition: ui_int.cpp:66
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
const int GetInt(void) const
Integer retrieval.
Definition: ui_int.cpp:70
ParamInt class definition.
Page down arrow key.
Definition: io_device.h:196
String device.
Definition: string_device.h:46
Execution context.
Definition: exec_context.h:149
CmdLineEdition class definition.
void Beep(void)
Sends a beep signal.
virtual void ResetToDefault(void)
Handler called when default value is required to be restored.
Definition: ui_int.cpp:81
CLI library default pre-compiled header.
enum _KEY KEY
Input keys.
#define CLI_NS_BEGIN(__ns)
Begin a namespace definition.
Definition: namespace.h:38
Int(const int I_DefaultValue, const int I_MinValue, const int I_MaxValue)
Top execution context constructor.
Definition: ui_int.cpp:54
Enter.
Definition: io_device.h:55
Down arrow key.
Definition: io_device.h:192
Int class definition.
Object consistency insurance.
virtual void OnKey(const KEY E_KeyCode)
Handler called on character input.
Definition: ui_line.cpp:86
virtual const bool SetstrValue(const char *const STR_Value) const
Value setting.
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
Up arrow key.
Definition: io_device.h:191