]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/wx-console/wxbhistorytextctrl.cpp
kes Simplify locking in the reservations system.
[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    Bacula® - The Network Backup Solution
11
12    Copyright (C) 2004-2006 Free Software Foundation Europe e.V.
13
14    The main author of Bacula is Kern Sibbald, with contributions from
15    many others, a complete list can be found in the file AUTHORS.
16    This program is Free Software; you can redistribute it and/or
17    modify it under the terms of version two of the GNU General Public
18    License as published by the Free Software Foundation plus additions
19    that are listed in the file LICENSE.
20
21    This program is distributed in the hope that it will be useful, but
22    WITHOUT ANY WARRANTY; without even the implied warranty of
23    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24    General Public License for more details.
25
26    You should have received a copy of the GNU General Public License
27    along with this program; if not, write to the Free Software
28    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
29    02110-1301, USA.
30
31    Bacula® is a registered trademark of John Walker.
32    The licensor of Bacula is the Free Software Foundation Europe
33    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
34    Switzerland, email:ftf@fsfeurope.org.
35 */
36
37 /*  Windows debug builds set _DEBUG which is used by wxWidgets to select their
38  *  debug memory allocator.  Unfortunately it conflicts with Bacula's SmartAlloc.
39  * So we turn _DEBUG off since we aren't interested in things it enables.
40  */
41
42 #undef _DEBUG
43
44 #include "bacula.h"
45 #include "wxbhistorytextctrl.h"
46
47 BEGIN_EVENT_TABLE(wxbHistoryTextCtrl, wxTextCtrl)
48    EVT_KEY_DOWN(wxbHistoryTextCtrl::OnKeyDown)
49    EVT_KEY_UP(wxbHistoryTextCtrl::OnKeyUp)
50 END_EVENT_TABLE()
51
52 wxbHistoryTextCtrl::wxbHistoryTextCtrl(wxStaticText* help, 
53       wxWindow* parent, wxWindowID id, 
54       const wxString& value, const wxPoint& pos, 
55       const wxSize& size , 
56       const wxValidator& validator, 
57       const wxString& name): 
58          wxTextCtrl(parent, id, value, pos, size, 
59             wxTE_PROCESS_ENTER, validator, name) {
60    this->help = help;
61    index = 0;
62    history.Add(wxT(""));
63 }
64
65 void wxbHistoryTextCtrl::AddCommand(wxString cmd, wxString description) {
66    commands[cmd] = description;
67 }
68
69 void wxbHistoryTextCtrl::ClearCommandList() {
70    commands.clear();
71 }
72
73 void wxbHistoryTextCtrl::HistoryAdd(wxString cmd) {
74    if (cmd == wxT("")) return;
75    index = history.Count();
76    history[index-1] = cmd;
77    history.Add(wxT(""));
78 }
79
80 void wxbHistoryTextCtrl::SetValue(const wxString& value) {
81    if (value == wxT("")) {
82       help->SetLabel(_("Type your command below:"));
83    }
84    wxTextCtrl::SetValue(value);
85 }
86
87 void wxbHistoryTextCtrl::OnKeyDown(wxKeyEvent& event) {
88    if (event.m_keyCode == WXK_TAB) {
89
90    }
91    else {
92       event.Skip();
93    }
94 }
95
96 void wxbHistoryTextCtrl::OnKeyUp(wxKeyEvent& event) {
97    if (event.m_keyCode == WXK_UP) {
98       if (index > 0) {
99          if (index == ((int)history.Count()-1)) {
100             history[index] = GetValue();
101          }
102          index--;
103          SetValue(history[index]);
104          SetInsertionPointEnd();
105       }
106    }
107    else if (event.m_keyCode == WXK_DOWN) {
108       if (index < ((int)history.Count()-1)) {
109          index++;
110          SetValue(history[index]);
111          SetInsertionPointEnd();
112       }      
113    }
114    else if (GetValue() != wxT("")) {
115       wxbCommands::iterator it;
116       wxString key;
117       wxString helptext = _("Unknown command.");
118       int found = 0;      
119       for( it = commands.begin(); it != commands.end(); ++it ) {         
120          if (it->first.Find(GetValue()) == 0) {
121             found++;
122             if (found > 2) {
123                helptext += wxT(" ") + it->first;
124             }
125             else if (found > 1) {
126                helptext = _("Possible completions: ") + key + wxT(" ") + it->first;
127             }
128             else { // (found == 1)
129                helptext = it->first + wxT(": ") + it->second;
130                key = it->first;
131             }
132          }
133          else if (GetValue().Find(it->first) == 0) {
134             helptext = it->first + wxT(": ") + it->second;
135             found = 0;
136             break;
137          }
138       }
139       
140       help->SetLabel(helptext);
141             
142       if (event.m_keyCode == WXK_TAB) {
143          if (found == 1) {
144             SetValue(key);
145             SetInsertionPointEnd();
146          }
147       }
148       else {
149          event.Skip();
150       }
151    }
152    else {
153       help->SetLabel(_("Type your command below:"));
154       event.Skip();
155    }
156 }