3 * Text control with an history of commands typed
5 * Nicolas Boichat, July 2004
10 Copyright (C) 2004 Kern Sibbald and John Walker
12 This program is free software; you can redistribute it and/or
13 modify it under the terms of the GNU General Public License
14 as published by the Free Software Foundation; either version 2
15 of the License, or (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 #include "wxbhistorytextctrl.h"
29 BEGIN_EVENT_TABLE(wxbHistoryTextCtrl, wxTextCtrl)
30 EVT_KEY_DOWN(wxbHistoryTextCtrl::OnKeyDown)
31 EVT_KEY_UP(wxbHistoryTextCtrl::OnKeyUp)
34 wxbHistoryTextCtrl::wxbHistoryTextCtrl(wxStaticText* help,
35 wxWindow* parent, wxWindowID id,
36 const wxString& value, const wxPoint& pos,
38 const wxValidator& validator,
39 const wxString& name):
40 wxTextCtrl(parent, id, value, pos, size,
41 wxTE_PROCESS_ENTER, validator, name) {
47 void wxbHistoryTextCtrl::AddCommand(wxString cmd, wxString description) {
48 commands[cmd] = description;
51 void wxbHistoryTextCtrl::ClearCommandList() {
55 void wxbHistoryTextCtrl::HistoryAdd(wxString cmd) {
56 if (cmd == "") return;
57 index = history.Count();
58 history[index-1] = cmd;
62 void wxbHistoryTextCtrl::SetValue(const wxString& value) {
64 help->SetLabel("Type your command below:");
66 wxTextCtrl::SetValue(value);
69 void wxbHistoryTextCtrl::OnKeyDown(wxKeyEvent& event) {
70 if (event.m_keyCode == WXK_TAB) {
78 void wxbHistoryTextCtrl::OnKeyUp(wxKeyEvent& event) {
79 if (event.m_keyCode == WXK_UP) {
81 if (index == ((int)history.Count()-1)) {
82 history[index] = GetValue();
85 SetValue(history[index]);
86 SetInsertionPointEnd();
89 else if (event.m_keyCode == WXK_DOWN) {
90 if (index < ((int)history.Count()-1)) {
92 SetValue(history[index]);
93 SetInsertionPointEnd();
96 else if (GetValue() != "") {
97 wxbCommands::iterator it;
99 wxString helptext = "Unknown command.";
101 for( it = commands.begin(); it != commands.end(); ++it ) {
102 if (it->first.Find(GetValue()) == 0) {
105 helptext += " " + it->first;
107 else if (found > 1) {
108 helptext = "Possible completions: " + key + " " + it->first;
110 else { // (found == 1)
111 helptext = it->first + ": " + it->second;
115 else if (GetValue().Find(it->first) == 0) {
116 helptext = it->first + ": " + it->second;
122 help->SetLabel(helptext);
124 if (event.m_keyCode == WXK_TAB) {
127 SetInsertionPointEnd();
135 help->SetLabel("Type your command below:");