00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifdef HAVE_CONFIG_H
00026 #include <config.h>
00027 #endif
00028
00029 #include <wx/wxprec.h>
00030
00031 #ifndef WX_PRECOMP
00032 #include <wx/wx.h>
00033 #endif
00034
00035 #include <wx/clipbrd.h>
00036
00037 #include "view/MPGFrame.hh"
00038 #include "view/PasswordTextCtrl.hh"
00039
00040 using namespace mpg;
00041
00042 BEGIN_EVENT_TABLE(PasswordTextCtrl, wxTextCtrl)
00043 EVT_CHAR(PasswordTextCtrl::onChar)
00044 EVT_KEY_DOWN(PasswordTextCtrl::onKeyDown)
00045 END_EVENT_TABLE()
00046
00047 wxString PasswordTextCtrl::GetValue() const {
00048 wxString temp = wxTextCtrl::GetValue();
00049
00050 return wxString(temp.Mid(0, 6) + temp.Mid(7, 6) +
00051 temp.Mid(14, 6) + temp.Mid(21));
00052 }
00053
00054 void PasswordTextCtrl::SetValue(const wxString &value) {
00055 wxString text;
00056
00057
00058 for (unsigned int i = 0; i < PASSWORD_LENGTH; ++i) {
00059 if ((i > 0) && ((i % 6) == 0)) {
00060 text += wxT(' ');
00061 }
00062
00063 text += value[i];
00064 }
00065
00066 wxTextCtrl::SetValue(text);
00067 }
00068
00069 void PasswordTextCtrl::Paste() {
00070 if (wxTheClipboard->Open()) {
00071 if (wxTheClipboard->IsSupported(wxDF_TEXT)) {
00072 wxTextDataObject data;
00073 wxTheClipboard->GetData(data);
00074
00075 wxString text = data.GetText();
00076 text.Replace(wxT(" "), wxT(""));
00077
00078 int pos = GetInsertionPoint();
00079 int retPos = pos;
00080 text = text.Truncate(27 - pos);
00081
00082 if (pos >= 21) {
00083 pos -= 3;
00084 } else if (pos >= 14) {
00085 pos -= 2;
00086 } else if (pos >= 7) {
00087 --pos;
00088 }
00089
00090 int len = text.Length();
00091 wxString value = GetValue();
00092 wxString temp;
00093
00094 if (pos == 0) {
00095 temp += text;
00096 temp += value.Mid(len);
00097 } else {
00098 temp += value.Mid(0, pos);
00099 temp += text;
00100 temp += value.Mid(pos + len);
00101 }
00102
00103 SetValue(temp);
00104 SetInsertionPoint(retPos);
00105 }
00106
00107 wxTheClipboard->Close();
00108 }
00109 }
00110
00111 void PasswordTextCtrl::Replace(long from, long to, const wxString &value) {
00112 #ifdef __WXMAC__
00113 wxString text = wxTextCtrl::GetValue();
00114
00115 for (int i = from, count = 0; i < to; i++, count++) {
00116 text[i] = value[count];
00117 }
00118
00119 wxTextCtrl::SetValue(text);
00120 #else
00121 wxTextCtrl::Replace(from, to, value);
00122 #endif
00123 }
00124
00125 void PasswordTextCtrl::onChar(wxKeyEvent &event) {
00126 long key = event.m_keyCode;
00127 int pos = GetInsertionPoint();
00128
00129 if (key == WXK_LEFT) {
00130 if ((pos == 7) || (pos == 14) || (pos == 21)) {
00131 pos -= 2;
00132 } else if (pos != 0) {
00133 --pos;
00134 }
00135
00136 SetInsertionPoint(pos);
00137 } else if (key == WXK_RIGHT) {
00138 if ((pos == 5) || (pos == 12) || (pos == 19)) {
00139 pos += 2;
00140 } else if (pos != 27) {
00141 ++pos;
00142 }
00143
00144 SetInsertionPoint(pos);
00145 } else if (key == WXK_BACK) {
00146 if ((pos == 7) || (pos == 14) || (pos == 21)) {
00147 --pos;
00148 }
00149
00150 if (pos > 0) {
00151 Replace(pos - 1, pos, wxT(' '));
00152 SetInsertionPoint(pos - 1);
00153 }
00154 } else if (key == WXK_DELETE) {
00155 if (pos < 27) {
00156 Replace(pos, pos + 1, wxT(' '));
00157 SetInsertionPoint(pos);
00158 }
00159 } else if ( ((key >= '0') && (key <= '9')) ||
00160 ((key >= 'A') && (key <= 'Z')) ||
00161 ((key >= 'a') && (key <= 'z')) ||
00162 (key == '?') || (key == '-') || (key == WXK_SPACE)
00163 ) {
00164
00165 if (event.AltDown()) {
00166
00167 event.Skip();
00168 } else if (pos < 27) {
00169 wxString text = wxTextCtrl::GetValue();
00170 wxChar ch = static_cast<wxChar>(key);
00171
00172 if ((pos == 6) || (pos == 13) || (pos == 20)) {
00173 ++pos;
00174 }
00175
00176 if (text[pos] != ch) {
00177 Replace(pos, pos + 1, ch);
00178 }
00179
00180 if ((pos == 5) || (pos == 12) || (pos == 19)) {
00181 ++pos;
00182 }
00183
00184 SetInsertionPoint(pos + 1);
00185 }
00186 } else if ((key < WXK_SPACE) || (key > WXK_START)) {
00187
00188 event.Skip();
00189 }
00190 }
00191
00192 void PasswordTextCtrl::onKeyDown(wxKeyEvent &event) {
00193 if (event.m_keyCode == WXK_DELETE) {
00194 long from, to;
00195 GetSelection(&from, &to);
00196
00197 if (from != to) {
00198 Replace(from, to, wxString(wxT(' '), (to - from)));
00199 SetInsertionPoint(from);
00200 } else {
00201 event.Skip();
00202 }
00203 } else if ((event.m_keyCode == 'V') && (event.CmdDown())) {
00204 Paste();
00205 } else {
00206 event.Skip();
00207 }
00208 }
00209
00210 IMPLEMENT_DYNAMIC_CLASS(PasswordTextCtrl, wxTextCtrl)
00211