]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/wx-console/wxbhistorytextctrl.cpp
Change Groupbox name to Remote Director in Configuration page if Director is not...
[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-2006 Kern Sibbald
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    version 2 as amended with additional clauses defined in the
15    file LICENSE in the main source directory.
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    the file LICENSE for additional details.
21
22  */
23
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.
27  */
28
29 #undef _DEBUG
30
31 #include "bacula.h"
32 #include "wxbhistorytextctrl.h"
33
34 BEGIN_EVENT_TABLE(wxbHistoryTextCtrl, wxTextCtrl)
35    EVT_KEY_DOWN(wxbHistoryTextCtrl::OnKeyDown)
36    EVT_KEY_UP(wxbHistoryTextCtrl::OnKeyUp)
37 END_EVENT_TABLE()
38
39 wxbHistoryTextCtrl::wxbHistoryTextCtrl(wxStaticText* help, 
40       wxWindow* parent, wxWindowID id, 
41       const wxString& value, const wxPoint& pos, 
42       const wxSize& size , 
43       const wxValidator& validator, 
44       const wxString& name): 
45          wxTextCtrl(parent, id, value, pos, size, 
46             wxTE_PROCESS_ENTER, validator, name) {
47    this->help = help;
48    index = 0;
49    history.Add(wxT(""));
50 }
51
52 void wxbHistoryTextCtrl::AddCommand(wxString cmd, wxString description) {
53    commands[cmd] = description;
54 }
55
56 void wxbHistoryTextCtrl::ClearCommandList() {
57    commands.clear();
58 }
59
60 void wxbHistoryTextCtrl::HistoryAdd(wxString cmd) {
61    if (cmd == wxT("")) return;
62    index = history.Count();
63    history[index-1] = cmd;
64    history.Add(wxT(""));
65 }
66
67 void wxbHistoryTextCtrl::SetValue(const wxString& value) {
68    if (value == wxT("")) {
69       help->SetLabel(_("Type your command below:"));
70    }
71    wxTextCtrl::SetValue(value);
72 }
73
74 void wxbHistoryTextCtrl::OnKeyDown(wxKeyEvent& event) {
75    if (event.m_keyCode == WXK_TAB) {
76
77    }
78    else {
79       event.Skip();
80    }
81 }
82
83 void wxbHistoryTextCtrl::OnKeyUp(wxKeyEvent& event) {
84    if (event.m_keyCode == WXK_UP) {
85       if (index > 0) {
86          if (index == ((int)history.Count()-1)) {
87             history[index] = GetValue();
88          }
89          index--;
90          SetValue(history[index]);
91          SetInsertionPointEnd();
92       }
93    }
94    else if (event.m_keyCode == WXK_DOWN) {
95       if (index < ((int)history.Count()-1)) {
96          index++;
97          SetValue(history[index]);
98          SetInsertionPointEnd();
99       }      
100    }
101    else if (GetValue() != wxT("")) {
102       wxbCommands::iterator it;
103       wxString key;
104       wxString helptext = _("Unknown command.");
105       int found = 0;      
106       for( it = commands.begin(); it != commands.end(); ++it ) {         
107          if (it->first.Find(GetValue()) == 0) {
108             found++;
109             if (found > 2) {
110                helptext += wxT(" ") + it->first;
111             }
112             else if (found > 1) {
113                helptext = _("Possible completions: ") + key + wxT(" ") + it->first;
114             }
115             else { // (found == 1)
116                helptext = it->first + wxT(": ") + it->second;
117                key = it->first;
118             }
119          }
120          else if (GetValue().Find(it->first) == 0) {
121             helptext = it->first + wxT(": ") + it->second;
122             found = 0;
123             break;
124          }
125       }
126       
127       help->SetLabel(helptext);
128             
129       if (event.m_keyCode == WXK_TAB) {
130          if (found == 1) {
131             SetValue(key);
132             SetInsertionPointEnd();
133          }
134       }
135       else {
136          event.Skip();
137       }
138    }
139    else {
140       help->SetLabel(_("Type your command below:"));
141       event.Skip();
142    }
143 }