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