Main Page | Namespace List | Class List | Directories | File List | Namespace Members | Class Members | File Members

PasswordTextCtrl.cc

Go to the documentation of this file.
00001 /*
00002  * Metroid Password Generator
00003  * Copyright (C) 2005 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.7 2005/09/30 10:01:46 technoplaza 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 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     // separate the password segments with spaces
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         // The legitimite Metroid alphabet
00165         if (event.AltDown()) {
00166             // skip menu accelerators
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         // don't filter special keys
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 

Generated on Fri Sep 30 04:56:21 2005 for Metroid Password Generator by  doxygen 1.4.2