]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/wx-console/wxbhistorytextctrl.cpp
- Put Dmsg() on inside if() to avoid calling subroutine.
[bacula/bacula] / bacula / src / wx-console / wxbhistorytextctrl.cpp
1 /*
2  *
3  *   Text control with an history of commands typed
4  *
5  *    Nicolas Boichat, July 2004
6  *
7  *    Version $Id$
8  */
9 /*
10    Copyright (C) 2004 Kern Sibbald and John Walker
11
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.
16
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.
21
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.
25  */
26  
27 #include "wxbhistorytextctrl.h"
28
29 BEGIN_EVENT_TABLE(wxbHistoryTextCtrl, wxTextCtrl)
30    EVT_KEY_DOWN(wxbHistoryTextCtrl::OnKeyDown)
31    EVT_KEY_UP(wxbHistoryTextCtrl::OnKeyUp)
32 END_EVENT_TABLE()
33
34 wxbHistoryTextCtrl::wxbHistoryTextCtrl(wxStaticText* help, 
35       wxWindow* parent, wxWindowID id, 
36       const wxString& value, const wxPoint& pos, 
37       const wxSize& size , 
38       const wxValidator& validator, 
39       const wxString& name): 
40          wxTextCtrl(parent, id, value, pos, size, 
41             wxTE_PROCESS_ENTER, validator, name) {
42    this->help = help;
43    index = 0;
44    history.Add(wxT(""));
45 }
46
47 void wxbHistoryTextCtrl::AddCommand(wxString cmd, wxString description) {
48    commands[cmd] = description;
49 }
50
51 void wxbHistoryTextCtrl::ClearCommandList() {
52    commands.clear();
53 }
54
55 void wxbHistoryTextCtrl::HistoryAdd(wxString cmd) {
56    if (cmd == wxT("")) return;
57    index = history.Count();
58    history[index-1] = cmd;
59    history.Add(wxT(""));
60 }
61
62 void wxbHistoryTextCtrl::SetValue(const wxString& value) {
63    if (value == wxT("")) {
64       help->SetLabel(wxT("Type your command below:"));
65    }
66    wxTextCtrl::SetValue(value);
67 }
68
69 void wxbHistoryTextCtrl::OnKeyDown(wxKeyEvent& event) {
70    if (event.m_keyCode == WXK_TAB) {
71
72    }
73    else {
74       event.Skip();
75    }
76 }
77
78 void wxbHistoryTextCtrl::OnKeyUp(wxKeyEvent& event) {
79    if (event.m_keyCode == WXK_UP) {
80       if (index > 0) {
81          if (index == ((int)history.Count()-1)) {
82             history[index] = GetValue();
83          }
84          index--;
85          SetValue(history[index]);
86          SetInsertionPointEnd();
87       }
88    }
89    else if (event.m_keyCode == WXK_DOWN) {
90       if (index < ((int)history.Count()-1)) {
91          index++;
92          SetValue(history[index]);
93          SetInsertionPointEnd();
94       }      
95    }
96    else if (GetValue() != wxT("")) {
97       wxbCommands::iterator it;
98       wxString key;
99       wxString helptext = wxT("Unknown command.");
100       int found = 0;      
101       for( it = commands.begin(); it != commands.end(); ++it ) {         
102          if (it->first.Find(GetValue()) == 0) {
103             found++;
104             if (found > 2) {
105                helptext += wxT(" ") + it->first;
106             }
107             else if (found > 1) {
108                helptext = wxT("Possible completions: ") + key + wxT(" ") + it->first;
109             }
110             else { // (found == 1)
111                helptext = it->first + wxT(": ") + it->second;
112                key = it->first;
113             }
114          }
115          else if (GetValue().Find(it->first) == 0) {
116             helptext = it->first + wxT(": ") + it->second;
117             found = 0;
118             break;
119          }
120       }
121       
122       help->SetLabel(helptext);
123             
124       if (event.m_keyCode == WXK_TAB) {
125          if (found == 1) {
126             SetValue(key);
127             SetInsertionPointEnd();
128          }
129       }
130       else {
131          event.Skip();
132       }
133    }
134    else {
135       help->SetLabel(wxT("Type your command below:"));
136       event.Skip();
137    }
138 }