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.
24 /* Windows debug builds set _DEBUG which is used by wxWidgets to select their
25 * debug memory allocator. Unfortunately it conflicts with Bacula's SmartAlloc.
26 * So we turn _DEBUG off since we aren't interested in things it enables.
32 #include "wxbhistorytextctrl.h"
34 BEGIN_EVENT_TABLE(wxbHistoryTextCtrl, wxTextCtrl)
35 EVT_KEY_DOWN(wxbHistoryTextCtrl::OnKeyDown)
36 EVT_KEY_UP(wxbHistoryTextCtrl::OnKeyUp)
39 wxbHistoryTextCtrl::wxbHistoryTextCtrl(wxStaticText* help,
40 wxWindow* parent, wxWindowID id,
41 const wxString& value, const wxPoint& pos,
43 const wxValidator& validator,
44 const wxString& name):
45 wxTextCtrl(parent, id, value, pos, size,
46 wxTE_PROCESS_ENTER, validator, name) {
52 void wxbHistoryTextCtrl::AddCommand(wxString cmd, wxString description) {
53 commands[cmd] = description;
56 void wxbHistoryTextCtrl::ClearCommandList() {
60 void wxbHistoryTextCtrl::HistoryAdd(wxString cmd) {
61 if (cmd == wxT("")) return;
62 index = history.Count();
63 history[index-1] = cmd;
67 void wxbHistoryTextCtrl::SetValue(const wxString& value) {
68 if (value == wxT("")) {
69 help->SetLabel(_("Type your command below:"));
71 wxTextCtrl::SetValue(value);
74 void wxbHistoryTextCtrl::OnKeyDown(wxKeyEvent& event) {
75 if (event.m_keyCode == WXK_TAB) {
83 void wxbHistoryTextCtrl::OnKeyUp(wxKeyEvent& event) {
84 if (event.m_keyCode == WXK_UP) {
86 if (index == ((int)history.Count()-1)) {
87 history[index] = GetValue();
90 SetValue(history[index]);
91 SetInsertionPointEnd();
94 else if (event.m_keyCode == WXK_DOWN) {
95 if (index < ((int)history.Count()-1)) {
97 SetValue(history[index]);
98 SetInsertionPointEnd();
101 else if (GetValue() != wxT("")) {
102 wxbCommands::iterator it;
104 wxString helptext = _("Unknown command.");
106 for( it = commands.begin(); it != commands.end(); ++it ) {
107 if (it->first.Find(GetValue()) == 0) {
110 helptext += wxT(" ") + it->first;
112 else if (found > 1) {
113 helptext = _("Possible completions: ") + key + wxT(" ") + it->first;
115 else { // (found == 1)
116 helptext = it->first + wxT(": ") + it->second;
120 else if (GetValue().Find(it->first) == 0) {
121 helptext = it->first + wxT(": ") + it->second;
127 help->SetLabel(helptext);
129 if (event.m_keyCode == WXK_TAB) {
132 SetInsertionPointEnd();
140 help->SetLabel(_("Type your command below:"));