PasswordTextCtrl.cc

Go to the documentation of this file.
00001 /*
00002  * Metroid Password Generator
00003  * Copyright (C) 2005,2007-2008 emuWorks
00004  * http://games.technoplaza.net/
00005  *
00006  * This file is part of Metroid Password Generator.
00007  *
00008  * Metroid Password Generator is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * Metroid Password Generator is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with Metroid Password Generator; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00021  */
00022  
00023 // $Id: PasswordTextCtrl.cc,v 1.9 2008/12/17 00:23:12 jdratlif Exp $
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 wxString PasswordTextCtrl::GetValue() const {
00043     wxString temp = wxTextCtrl::GetValue();
00044     
00045     return wxString(temp.Mid(0, 6) + temp.Mid(7, 6) +
00046                      temp.Mid(14, 6) + temp.Mid(21));
00047 }
00048 
00049 void PasswordTextCtrl::SetValue(const wxString &value) {
00050     wxString text;
00051     
00052     // separate the password segments with spaces
00053     for (unsigned int i = 0; i < PASSWORD_LENGTH; ++i) {
00054         if ((i > 0) && ((i % 6) == 0)) {
00055             text += wxT(' ');
00056         }
00057         
00058         text += value[i];
00059     }
00060     
00061     wxTextCtrl::SetValue(text);
00062 }
00063 
00064 void PasswordTextCtrl::Paste() {
00065     if (wxTheClipboard->Open()) {
00066         if (wxTheClipboard->IsSupported(wxDF_TEXT)) {
00067             wxTextDataObject data;
00068             wxTheClipboard->GetData(data);
00069             
00070             wxString text = data.GetText();
00071             text.Replace(wxT(" "), wxT(""));
00072             
00073             int pos = GetInsertionPoint();
00074             int retPos = pos;
00075             text = text.Truncate(27 - pos);
00076             
00077             if (pos >= 21) {
00078                 pos -= 3;
00079             } else if (pos >= 14) {
00080                 pos -= 2;
00081             } else if (pos >= 7) {
00082                 --pos;
00083             }
00084             
00085             int len = text.Length();            
00086             wxString value = GetValue();
00087             wxString temp;
00088             
00089             if (pos == 0) {
00090                 temp += text;
00091                 temp += value.Mid(len);
00092             } else {
00093                 temp += value.Mid(0, pos);
00094                 temp += text;
00095                 temp += value.Mid(pos + len);
00096             }
00097             
00098             SetValue(temp);
00099             SetInsertionPoint(retPos);
00100         }
00101         
00102         wxTheClipboard->Close();
00103     }
00104 }
00105 
00106 void PasswordTextCtrl::Replace(long from, long to, const wxString &value) {
00107     #ifdef __WXMAC__
00108         wxString text = wxTextCtrl::GetValue();
00109         
00110         for (int i = from, count = 0; i < to; i++, count++) {
00111             text[i] = value[count];
00112         }
00113         
00114         wxTextCtrl::SetValue(text);
00115     #else
00116         wxTextCtrl::Replace(from, to, value);
00117     #endif
00118 }
00119 
00120 void PasswordTextCtrl::onChar(wxKeyEvent &event) {
00121     long key = event.m_keyCode;
00122     int pos = GetInsertionPoint();
00123     
00124     if (key == WXK_LEFT) {
00125         if ((pos == 7) || (pos == 14) || (pos == 21)) {
00126             pos -= 2;
00127         } else if (pos != 0) {
00128             --pos;
00129         }
00130         
00131         SetInsertionPoint(pos);
00132     } else if (key == WXK_RIGHT) {
00133         if ((pos == 5) || (pos == 12) || (pos == 19)) {
00134             pos += 2;
00135         } else if (pos != 27) {
00136             ++pos;
00137         }
00138         
00139         SetInsertionPoint(pos);
00140     } else if (key == WXK_BACK) {
00141         if ((pos == 7) || (pos == 14) || (pos == 21)) {
00142             --pos;
00143         }
00144         
00145         if (pos > 0) {
00146             Replace(pos - 1, pos, wxT(' '));
00147             SetInsertionPoint(pos - 1);
00148         }
00149     } else if (key == WXK_DELETE) {
00150         if (pos < 27) {
00151             Replace(pos, pos + 1, wxT(' '));
00152             SetInsertionPoint(pos);
00153         }
00154     } else if ( ((key >= '0') && (key <= '9')) ||
00155                 ((key >= 'A') && (key <= 'Z')) ||
00156                 ((key >= 'a') && (key <= 'z')) ||
00157                 (key == '?') || (key == '-') || (key == WXK_SPACE)
00158               ) {
00159         // The legitimite Metroid alphabet
00160         if (event.AltDown()) {
00161             // skip menu accelerators
00162             event.Skip();
00163         } else if (pos < 27) {
00164             wxString text = wxTextCtrl::GetValue();
00165             wxChar ch = static_cast<wxChar>(key);
00166             
00167             if ((pos == 6) || (pos == 13) || (pos == 20)) {
00168                 ++pos;
00169             }
00170             
00171             if (text[pos] != ch) {
00172                 Replace(pos, pos + 1, ch);
00173             }
00174             
00175             if ((pos == 5) || (pos == 12) || (pos == 19)) {
00176                 ++pos;
00177             }
00178             
00179             SetInsertionPoint(pos + 1);
00180         }
00181     } else if ((key < WXK_SPACE) || (key > WXK_START)) {
00182         // don't filter special keys
00183         event.Skip();
00184     }
00185 }
00186 
00187 void PasswordTextCtrl::onKeyDown(wxKeyEvent &event) {
00188     if (event.m_keyCode == WXK_DELETE) {
00189         long from, to;
00190         GetSelection(&from, &to);
00191         
00192         if (from != to) {
00193             Replace(from, to, wxString(wxT(' '), (to - from)));
00194             SetInsertionPoint(from);
00195         } else {
00196             event.Skip();
00197         }
00198     } else if ((event.m_keyCode == 'V') && (event.CmdDown())) {
00199         Paste();
00200     } else {
00201         event.Skip();
00202     }
00203 }
00204 
00205 IMPLEMENT_DYNAMIC_CLASS(PasswordTextCtrl, wxTextCtrl)
00206 
00207 BEGIN_EVENT_TABLE(PasswordTextCtrl, wxTextCtrl)
00208     EVT_CHAR(PasswordTextCtrl::onChar)
00209     EVT_KEY_DOWN(PasswordTextCtrl::onKeyDown)
00210 END_EVENT_TABLE()
00211 

Generated on Tue Dec 16 20:16:57 2008 for Metroid Password Generator by  doxygen 1.5.4