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