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.
26 #include "wxbhistorytextctrl.h"
28 BEGIN_EVENT_TABLE(wxbHistoryTextCtrl, wxTextCtrl)
29 EVT_KEY_DOWN(wxbHistoryTextCtrl::OnKeyDown)
30 EVT_KEY_UP(wxbHistoryTextCtrl::OnKeyUp)
33 wxbHistoryTextCtrl::wxbHistoryTextCtrl(wxStaticText* help,
34 wxWindow* parent, wxWindowID id,
35 const wxString& value, const wxPoint& pos,
37 const wxValidator& validator,
38 const wxString& name):
39 wxTextCtrl(parent, id, value, pos, size,
40 wxTE_PROCESS_ENTER, validator, name) {
46 void wxbHistoryTextCtrl::AddCommand(wxString cmd, wxString description) {
47 commands[cmd] = description;
50 void wxbHistoryTextCtrl::ClearCommandList() {
54 void wxbHistoryTextCtrl::HistoryAdd(wxString cmd) {
55 if (cmd == wxT("")) return;
56 index = history.Count();
57 history[index-1] = cmd;
61 void wxbHistoryTextCtrl::SetValue(const wxString& value) {
62 if (value == wxT("")) {
63 help->SetLabel(_("Type your command below:"));
65 wxTextCtrl::SetValue(value);
68 void wxbHistoryTextCtrl::OnKeyDown(wxKeyEvent& event) {
69 if (event.m_keyCode == WXK_TAB) {
77 void wxbHistoryTextCtrl::OnKeyUp(wxKeyEvent& event) {
78 if (event.m_keyCode == WXK_UP) {
80 if (index == ((int)history.Count()-1)) {
81 history[index] = GetValue();
84 SetValue(history[index]);
85 SetInsertionPointEnd();
88 else if (event.m_keyCode == WXK_DOWN) {
89 if (index < ((int)history.Count()-1)) {
91 SetValue(history[index]);
92 SetInsertionPointEnd();
95 else if (GetValue() != wxT("")) {
96 wxbCommands::iterator it;
98 wxString helptext = _("Unknown command.");
100 for( it = commands.begin(); it != commands.end(); ++it ) {
101 if (it->first.Find(GetValue()) == 0) {
104 helptext += wxT(" ") + it->first;
106 else if (found > 1) {
107 helptext = _("Possible completions: ") + key + wxT(" ") + it->first;
109 else { // (found == 1)
110 helptext = it->first + wxT(": ") + it->second;
114 else if (GetValue().Find(it->first) == 0) {
115 helptext = it->first + wxT(": ") + it->second;
121 help->SetLabel(helptext);
123 if (event.m_keyCode == WXK_TAB) {
126 SetInsertionPointEnd();
134 help->SetLabel(_("Type your command below:"));