CLI  2.9
ui_choice.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 "cli/ui_choice.h"
31 #include "cli/shell.h"
32 #include "command_line_edition.h"
33 
34 
36 
37  CLI_NS_BEGIN(ui)
38 
39 
40  static const ResourceString Choice2Str(
42  const int I_Choice,
43  const tk::Queue<ResourceString>& TK_Choices
44  )
45  {
46  // Loop over the choice list until the number of elements to skip has been reached.
47  tk::Queue<ResourceString>::Iterator it = TK_Choices.GetIterator();
48  for ( int i_ElementsToSkip = I_Choice;
49  (i_ElementsToSkip >= 0) && TK_Choices.IsValid(it);
50  i_ElementsToSkip --)
51  {
52  if (i_ElementsToSkip == 0)
53  {
54  return TK_Choices.GetAt(it);
55  }
56  else
57  {
58  TK_Choices.MoveNext(it);
59  }
60  }
61 
62  // Default to an empty string.
63  return ResourceString();
64  }
65 
66  // Unlike ui::Int and ui::Float implementations, ui::Line default value is not used for ui::Choice.
67  // Default value management will rather rely on ui::Line::ResetToDefault() overriding.
68 
69  Choice::Choice(const int I_DefaultChoice, const tk::Queue<ResourceString>& TK_Choices)
70  : Line(tk::String(10), -1, -1),
71  m_iDefaultChoice(I_DefaultChoice), m_tkChoices(TK_Choices), m_eLang(ResourceString::LANG_EN)
72  {
73  }
74 
75  Choice::Choice(ExecutionContext& CLI_ParentContext, const int I_DefaultChoice, const tk::Queue<ResourceString>& TK_Choices)
76  : Line(CLI_ParentContext, tk::String(10), -1, -1),
77  m_iDefaultChoice(I_DefaultChoice), m_tkChoices(TK_Choices), m_eLang(ResourceString::LANG_EN)
78  {
79  }
80 
82  {
83  }
84 
85  const int Choice::GetChoice(void) const
86  {
87  const tk::String str_Line = Line::GetLine();
88  int i_Choice = 0;
89  tk::Queue<int> tk_Near(10);
90  for ( tk::Queue<ResourceString>::Iterator it = m_tkChoices.GetIterator();
91  m_tkChoices.IsValid(it);
92  m_tkChoices.MoveNext(it), i_Choice++)
93  {
94  const tk::String str_Choice = m_tkChoices.GetAt(it).GetString(m_eLang);
95 
96  // Check whether the line matches the choice exactly.
97  if (str_Line == str_Choice)
98  {
99  return i_Choice;
100  }
101 
102  // Check whether the line starts like the choice (case insensitive).
103  if (str_Line.ToUpper() == str_Choice.SubString(0, str_Line.GetLength()).ToUpper())
104  {
105  tk_Near.AddTail(i_Choice);
106  }
107  }
108 
109  // If only one near result, then this one is the choice.
110  if (tk_Near.GetCount() == 1)
111  {
112  return tk_Near.GetHead();
113  }
114 
115  // No matching choice.
116  return -1;
117  }
118 
120  {
121  // First retrieve the choice enterd in its index form.
122  int i_Choice = GetChoice();
123  if (i_Choice >= 0)
124  {
125  return Choice2Str(i_Choice, m_tkChoices);
126  }
127  else
128  {
129  return ResourceString();
130  }
131  }
132 
134  {
135  // Remember the shell language setting
136  // so that post-execution calls to GetChoice() do not need a shell instance reference anymore.
137  m_eLang = GetLang();
138 
139  Line::SetLine(Choice2Str(m_iDefaultChoice, m_tkChoices).GetString(m_eLang), false, true);
140  }
141 
142  void Choice::OnKey(const KEY E_KeyCode)
143  {
144  switch (E_KeyCode)
145  {
146  case NULL_KEY:
147  EndControl(false);
148  break;
149  case KEY_UP:
150  MoveChoice(-1, 1);
151  break;
152  case KEY_DOWN:
153  MoveChoice(1, 1);
154  break;
155  case PAGE_UP:
156  MoveChoice(-1, m_tkChoices.GetCount() / 10);
157  break;
158  case PAGE_DOWN:
159  MoveChoice(1, m_tkChoices.GetCount() / 10);
160  break;
161  case ENTER:
162  if (GetChoice() >= 0)
163  {
164  Line::SetLine(GetstrChoice().GetString(m_eLang), true, true);
165  EndControl(true);
166  }
167  else
168  {
169  Beep();
170  }
171  break;
172  default:
173  Line::OnKey(E_KeyCode);
174  break;
175  }
176  }
177 
178  void Choice::MoveChoice(const int I_Way, const unsigned int UI_Increment)
179  {
180  const int i_Increment = I_Way * (int) ((UI_Increment == 0) ? 1 : UI_Increment);
181  const int i_Choice = GetChoice();
182 
183  if (i_Choice < 0)
184  {
185  // Undefined choice: restore default.
186  Beep();
187  ResetToDefault();
188  }
189  else if (i_Choice + i_Increment < 0)
190  {
191  // Top of choice list.
192  // Possibly beep.
193  if (i_Choice <= 0)
194  {
195  Beep();
196  }
197  // Reprint.
198  Line::SetLine(Choice2Str(0, m_tkChoices).GetString(m_eLang), false, true);
199  }
200  else if (i_Choice + i_Increment >= (int) m_tkChoices.GetCount())
201  {
202  // Bottom of choice list.
203  // Possibly beep.
204  if (i_Choice >= ((int) m_tkChoices.GetCount()) - 1)
205  {
206  Beep();
207  }
208  // Reprint.
209  Line::SetLine(Choice2Str(m_tkChoices.GetCount() - 1, m_tkChoices).GetString(m_eLang), false, true);
210  }
211  else
212  {
213  // Move to next element.
214  Line::SetLine(Choice2Str(GetChoice() + i_Increment, m_tkChoices).GetString(m_eLang), false, true);
215  }
216  }
217 
218  CLI_NS_END(ui)
219 
221 
Main namespace of the CLI library.
const tk::String GetLine(void) const
Line retrieval.
Definition: ui_line.cpp:58
Shell class definition.
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
const ResourceString::LANG GetLang(void) const
Language access.
Choice class 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
CLI classes toolkit.
Definition: tk_stl.h:51
Page up arrow key.
Definition: io_device.h:195
ResourceString class.
const ResourceString GetstrChoice(void) const
Choice retrieval in its string form.
Definition: ui_choice.cpp:119
Page down arrow key.
Definition: io_device.h:196
Execution context.
Definition: exec_context.h:149
CmdLineEdition class definition.
void Beep(void)
Sends a beep signal.
CLI library default pre-compiled header.
enum _KEY KEY
Input keys.
#define CLI_NS_BEGIN(__ns)
Begin a namespace definition.
Definition: namespace.h:38
Choice(const int I_DefaultChoice, const tk::Queue< ResourceString > &TK_Choices)
Top execution context constructor.
Definition: ui_choice.cpp:69
Enter.
Definition: io_device.h:55
Down arrow key.
Definition: io_device.h:192
virtual void OnKey(const KEY E_KeyCode)
Handler called on character input.
Definition: ui_choice.cpp:142
const int GetChoice(void) const
Choice retrieval.
Definition: ui_choice.cpp:85
virtual void OnKey(const KEY E_KeyCode)
Handler called on character input.
Definition: ui_line.cpp:86
virtual ~Choice(void)
Destructor.
Definition: ui_choice.cpp:81
virtual void ResetToDefault(void)
Handler called when default value is required to be restored.
Definition: ui_choice.cpp:133
Null key.
Definition: io_device.h:51
Up arrow key.
Definition: io_device.h:191