3 * Text control with an history of commands typed
5 * Nicolas Boichat, July 2004
10 Copyright (C) 2004-2006 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.
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 == wxT("")) return;
57 index = history.Count();
58 history[index-1] = cmd;
62 void wxbHistoryTextCtrl::SetValue(const wxString& value) {
63 if (value == wxT("")) {
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() != wxT("")) {
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 += wxT(" ") + it->first;
107 else if (found > 1) {
108 helptext = _("Possible completions: ") + key + wxT(" ") + it->first;
110 else { // (found == 1)
111 helptext = it->first + wxT(": ") + it->second;
115 else if (GetValue().Find(it->first) == 0) {
116 helptext = it->first + wxT(": ") + it->second;
122 help->SetLabel(helptext);
124 if (event.m_keyCode == WXK_TAB) {
127 SetInsertionPointEnd();
135 help->SetLabel(_("Type your command below:"));