3 * Text control with an history of commands typed
5 * Nicolas Boichat, July 2004
10 Copyright (C) 2004-2005 Kern Sibbald
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 version 2 as amended with additional clauses defined in the
15 file LICENSE in the main source directory.
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 the file LICENSE for additional details.
24 #include "wxbhistorytextctrl.h"
26 BEGIN_EVENT_TABLE(wxbHistoryTextCtrl, wxTextCtrl)
27 EVT_KEY_DOWN(wxbHistoryTextCtrl::OnKeyDown)
28 EVT_KEY_UP(wxbHistoryTextCtrl::OnKeyUp)
31 wxbHistoryTextCtrl::wxbHistoryTextCtrl(wxStaticText* help,
32 wxWindow* parent, wxWindowID id,
33 const wxString& value, const wxPoint& pos,
35 const wxValidator& validator,
36 const wxString& name):
37 wxTextCtrl(parent, id, value, pos, size,
38 wxTE_PROCESS_ENTER, validator, name) {
44 void wxbHistoryTextCtrl::AddCommand(wxString cmd, wxString description) {
45 commands[cmd] = description;
48 void wxbHistoryTextCtrl::ClearCommandList() {
52 void wxbHistoryTextCtrl::HistoryAdd(wxString cmd) {
53 if (cmd == wxT("")) return;
54 index = history.Count();
55 history[index-1] = cmd;
59 void wxbHistoryTextCtrl::SetValue(const wxString& value) {
60 if (value == wxT("")) {
61 help->SetLabel(_("Type your command below:"));
63 wxTextCtrl::SetValue(value);
66 void wxbHistoryTextCtrl::OnKeyDown(wxKeyEvent& event) {
67 if (event.m_keyCode == WXK_TAB) {
75 void wxbHistoryTextCtrl::OnKeyUp(wxKeyEvent& event) {
76 if (event.m_keyCode == WXK_UP) {
78 if (index == ((int)history.Count()-1)) {
79 history[index] = GetValue();
82 SetValue(history[index]);
83 SetInsertionPointEnd();
86 else if (event.m_keyCode == WXK_DOWN) {
87 if (index < ((int)history.Count()-1)) {
89 SetValue(history[index]);
90 SetInsertionPointEnd();
93 else if (GetValue() != wxT("")) {
94 wxbCommands::iterator it;
96 wxString helptext = _("Unknown command.");
98 for( it = commands.begin(); it != commands.end(); ++it ) {
99 if (it->first.Find(GetValue()) == 0) {
102 helptext += wxT(" ") + it->first;
104 else if (found > 1) {
105 helptext = _("Possible completions: ") + key + wxT(" ") + it->first;
107 else { // (found == 1)
108 helptext = it->first + wxT(": ") + it->second;
112 else if (GetValue().Find(it->first) == 0) {
113 helptext = it->first + wxT(": ") + it->second;
119 help->SetLabel(helptext);
121 if (event.m_keyCode == WXK_TAB) {
124 SetInsertionPointEnd();
132 help->SetLabel(_("Type your command below:"));