Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

[r5105]: trunk / pwsafe / pwsafe / src / ui / wxWidgets / SafeCombinationCtrl.cpp Maximize Restore History

Download this file

SafeCombinationCtrl.cpp    153 lines (131 with data), 4.8 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
* Copyright (c) 2003-2012 Rony Shapiro <ronys@users.sourceforge.net>.
* All rights reserved. Use of the code is allowed under the
* Artistic License 2.0 terms, as specified in the LICENSE file
* distributed with this code, or available from
* http://www.opensource.org/licenses/artistic-license-2.0.php
*/
/** \file SafeCombinationCtrl.cpp
*
*/
// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"
#include "../../core/PwsPlatform.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif
#include "SafeCombinationCtrl.h"
#include "./wxutils.h"
#include "./ExternalKeyboardButton.h"
#ifdef __WXMSW__
#include <wx/msw/msvcrt.h>
#endif
/*
* This serves to transfer the data from wxTextCtrl directly into a StringX.
* Maybe it should also check if the combination is valid...
*/
class SafeCombinationValidator: public wxValidator
{
//no default ctor
SafeCombinationValidator();
// DECLARE_NO_COPY_CLASS(SafeCombinationValidator)
public:
SafeCombinationValidator(StringX* str): m_str(str), m_allowBlank(false) {}
virtual ~SafeCombinationValidator() { m_str = 0; }
virtual wxObject *Clone() const { return new SafeCombinationValidator(m_str); }
// This function can pop up an error message.
virtual bool Validate(wxWindow *WXUNUSED(parent));
// Called to transfer data to the window
virtual bool TransferToWindow();
// Called to transfer data from the window
virtual bool TransferFromWindow();
void AllowEmptyCombinationOnce() {m_allowBlank = true;}
private:
StringX* m_str;
bool m_allowBlank;
};
//
// Right now, we only validate if the user entered something in the combination box
// Maybe we could hook it up with the wxFilePickerCtrl and validate the safe
// combination itself
//
bool SafeCombinationValidator::Validate(wxWindow* parent)
{
bool retval = true;
wxTextCtrl* win = wxDynamicCast(GetWindow(), wxTextCtrl);
wxCHECK_MSG(win, false, wxT("You must associate a wxTextCtrl window with SafeCombinationValidator"));
if (!m_allowBlank && win->IsEmpty()) {
wxMessageBox(_("The combination cannot be blank."), _("Error"), wxOK | wxICON_EXCLAMATION, parent);
win->SetFocus();
retval = false;
}
m_allowBlank = false; // allowBlank is a one-shot: must be set each time!
return retval;
}
bool SafeCombinationValidator::TransferToWindow()
{
if (m_str) {
wxTextCtrl* win = wxDynamicCast(GetWindow(), wxTextCtrl);
wxCHECK_MSG(win, false, wxT("You must associate a wxTextCtrl window with SafeCombinationValidator"));
wxString tmp = towxstring(*m_str);
win->SetValue(tmp);
//clear out the memory. Is there a way to prevent this from getting optimized away?
for( size_t idx = 0; idx < tmp.Len(); ++idx)
tmp[idx] = 0;
}
return true;
}
bool SafeCombinationValidator::TransferFromWindow()
{
if (m_str) {
wxTextCtrl* win = wxDynamicCast(GetWindow(), wxTextCtrl);
wxCHECK_MSG(win, false, wxT("You must associate a wxTextCtrl window with SafeCombinationValidator"));
wxString tmp = win->GetValue();
*m_str = tostringx(tmp);
//clear out the memory. Is there a way to prevent this from getting optimized away?
for( size_t idx = 0; idx < tmp.Len(); ++idx)
tmp[idx] = 0;
}
return true;
}
void CSafeCombinationCtrl::Init(wxWindow* parent,
wxWindowID textCtrlID /*= wxID_ANY*/,
StringX* valPtr /*= 0*/,
const wxPoint& pos /* = wxDefaultPosition*/,
const wxSize& size /* = wxDefaultSize */)
{
SafeCombinationValidator scValidator(valPtr);
textCtrl = new wxTextCtrl(parent, textCtrlID, wxEmptyString, pos, size,
wxTE_PASSWORD,
scValidator);
ApplyPasswordFont(textCtrl);
Add(textCtrl, wxSizerFlags().Proportion(1).Expand());
ExternalKeyboardButton* vkbdButton = new ExternalKeyboardButton(parent);
Add(vkbdButton, wxSizerFlags().Border(wxLEFT));
}
CSafeCombinationCtrl::~CSafeCombinationCtrl()
{
}
StringX CSafeCombinationCtrl::GetCombination() const
{
return textCtrl->GetValue().c_str();
}
void CSafeCombinationCtrl::SetValidatorTarget(StringX* str)
{
SafeCombinationValidator scValidator(str);
textCtrl->SetValidator(scValidator);
}
void CSafeCombinationCtrl::SelectCombinationText() const
{
textCtrl->SetFocus();
textCtrl->SetSelection(-1,-1);
}
void CSafeCombinationCtrl::AllowEmptyCombinationOnce()
{
SafeCombinationValidator *scValidator = dynamic_cast<SafeCombinationValidator *>(textCtrl->GetValidator());
if (scValidator != NULL)
scValidator->AllowEmptyCombinationOnce();
}