📄 props.cpp
字号:
/////////////////////////////////////////////////////////////////////////////
// Name: props.cpp
// Purpose: Basic Property Classes
// Author: Jaakko Salli
// Modified by:
// Created: May-14-2004
// RCS-ID: $Id:
// Copyright: (c) Jaakko Salli
// Licence: wxWindows license
/////////////////////////////////////////////////////////////////////////////
// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#ifndef WX_PRECOMP
#include "wx/defs.h"
#include "wx/object.h"
#include "wx/hash.h"
#include "wx/string.h"
#include "wx/log.h"
#include "wx/event.h"
#include "wx/window.h"
#include "wx/panel.h"
#include "wx/dc.h"
#include "wx/dcclient.h"
#include "wx/dcmemory.h"
#include "wx/button.h"
#include "wx/pen.h"
#include "wx/brush.h"
#include "wx/cursor.h"
#include "wx/dialog.h"
#include "wx/settings.h"
#include "wx/msgdlg.h"
#include "wx/choice.h"
#include "wx/stattext.h"
#include "wx/scrolwin.h"
#include "wx/dirdlg.h"
#include "wx/combobox.h"
#include "wx/layout.h"
#include "wx/sizer.h"
#include "wx/textdlg.h"
#include "wx/filedlg.h"
#include "wx/statusbr.h"
#include "wx/intl.h"
#endif
#include <wx/filename.h>
#include <wx/propgrid/propgrid.h>
#include <wx/propgrid/propdev.h>
#define wxPG_CUSTOM_IMAGE_WIDTH 20 // for wxColourProperty etc.
// -----------------------------------------------------------------------
// wxStringProperty
// -----------------------------------------------------------------------
WX_PG_IMPLEMENT_PROPERTY_CLASS(wxStringProperty,wxBaseProperty,
wxString,const wxString&,TextCtrl)
wxStringPropertyClass::wxStringPropertyClass( const wxString& label,
const wxString& name,
const wxString& value )
: wxPGProperty(label,name)
{
DoSetValue(value);
}
wxStringPropertyClass::~wxStringPropertyClass() { }
void wxStringPropertyClass::DoSetValue( wxPGVariant value )
{
m_value = wxPGVariantToString(value);
}
wxPGVariant wxStringPropertyClass::DoGetValue() const
{
return wxPGVariant(m_value);
}
wxString wxStringPropertyClass::GetValueAsString( int argFlags ) const
{
// If string is password and value is for visual purposes,
// then return asterisks instead the actual string.
if ( (m_flags & wxPG_PROP_PASSWORD) && !(argFlags & wxPG_FULL_VALUE) )
return wxString(wxChar('*'), m_value.Length());
return m_value;
}
bool wxStringPropertyClass::SetValueFromString( const wxString& text, int )
{
if ( m_value != text )
return StdValidationProcedure(text);
return false;
}
void wxStringPropertyClass::SetAttribute( int id, wxVariant& value )
{
if ( id == wxPG_STRING_PASSWORD )
{
m_flags &= ~(wxPG_PROP_PASSWORD);
if ( value.GetLong() ) m_flags |= wxPG_PROP_PASSWORD;
RecreateEditor();
}
}
// -----------------------------------------------------------------------
// wxIntProperty
// -----------------------------------------------------------------------
wxPG_BEGIN_PROPERTY_CLASS_BODY(wxIntProperty,wxPGProperty,long,long)
WX_PG_DECLARE_BASIC_TYPE_METHODS()
virtual bool SetValueFromInt( long value, int flags );
#if wxUSE_VALIDATORS
static wxValidator* GetClassValidator();
virtual wxValidator* DoGetValidator() const;
#endif
wxPG_END_PROPERTY_CLASS_BODY()
WX_PG_IMPLEMENT_PROPERTY_CLASS(wxIntProperty,wxBaseProperty,
long,long,TextCtrl)
wxIntPropertyClass::wxIntPropertyClass( const wxString& label, const wxString& name,
long value ) : wxPGProperty(label,name)
{
DoSetValue(value);
}
wxIntPropertyClass::~wxIntPropertyClass() { }
void wxIntPropertyClass::DoSetValue( wxPGVariant value )
{
m_value = wxPGVariantToLong(value);
}
wxPGVariant wxIntPropertyClass::DoGetValue() const
{
return wxPGVariant(m_value);
}
wxString wxIntPropertyClass::GetValueAsString( int ) const
{
return wxString::Format(wxT("%li"),m_value);
}
bool wxIntPropertyClass::SetValueFromString( const wxString& text, int argFlags )
{
wxString s;
long value;
if ( text.length() == 0 )
{
SetValueToUnspecified();
return true;
}
// We know it is a number, but let's still check
// the return value.
if ( text.IsNumber() && text.ToLong( &value, 0 ) )
{
if ( m_value != value )
{
return StdValidationProcedure(value);
}
}
else if ( argFlags & wxPG_REPORT_ERROR )
{
s.Printf( wxT("! %s: \"%s\" is not a number."), m_label.c_str(), text.c_str() );
ShowError(s);
}
return false;
}
bool wxIntPropertyClass::SetValueFromInt( long value, int WXUNUSED(flags) )
{
if ( m_value != value )
{
m_value = value;
return true;
}
return false;
}
#if wxUSE_VALIDATORS
wxValidator* wxIntPropertyClass::GetClassValidator()
{
WX_PG_DOGETVALIDATOR_ENTRY()
// Atleast wxPython 2.6.2.1 required that the string argument is given
static wxString v;
wxTextValidator* validator = new wxTextValidator(wxFILTER_NUMERIC,&v);
WX_PG_DOGETVALIDATOR_EXIT(validator)
}
wxValidator* wxIntPropertyClass::DoGetValidator() const
{
return GetClassValidator();
}
#endif
// -----------------------------------------------------------------------
// wxUIntProperty
// -----------------------------------------------------------------------
#define wxPG_UINT_TEMPLATE_MAX 8
static const wxChar* gs_uintTemplates[wxPG_UINT_TEMPLATE_MAX] = {
wxT("%x"),wxT("0x%x"),wxT("$%x"),
wxT("%X"),wxT("0x%X"),wxT("$%X"),
wxT("%u"),wxT("%o")
};
wxPG_BEGIN_PROPERTY_CLASS_BODY(wxUIntProperty,wxBasePropertyClass,long,unsigned long)
WX_PG_DECLARE_BASIC_TYPE_METHODS()
WX_PG_DECLARE_ATTRIBUTE_METHODS()
virtual bool SetValueFromInt ( long value, int flags );
protected:
wxByte m_base;
wxByte m_realBase; // translated to 8,16,etc.
wxByte m_prefix;
wxPG_END_PROPERTY_CLASS_BODY()
WX_PG_IMPLEMENT_PROPERTY_CLASS(wxUIntProperty,wxBaseProperty,
long,unsigned long,TextCtrl)
wxUIntPropertyClass::wxUIntPropertyClass( const wxString& label, const wxString& name,
unsigned long value ) : wxBasePropertyClass(label,name)
{
m_base = 6; // This is magic number for dec base (must be same as in setattribute)
m_realBase = 10;
m_prefix = wxPG_PREFIX_NONE;
DoSetValue((long)value);
}
wxUIntPropertyClass::~wxUIntPropertyClass() { }
void wxUIntPropertyClass::DoSetValue( wxPGVariant value )
{
m_value = wxPGVariantToLong(value);
}
wxPGVariant wxUIntPropertyClass::DoGetValue() const
{
return wxPGVariant(m_value);
}
wxString wxUIntPropertyClass::GetValueAsString( int ) const
{
//return wxString::Format(wxPGGlobalVars->m_uintTemplate.c_str(),m_value);
size_t index = m_base + m_prefix;
if ( index >= wxPG_UINT_TEMPLATE_MAX )
index = wxPG_BASE_DEC;
return wxString::Format(gs_uintTemplates[index],m_value);
}
bool wxUIntPropertyClass::SetValueFromString( const wxString& text, int WXUNUSED(argFlags) )
{
//wxString s;
long unsigned value = 0;
if ( text.length() == 0 )
{
SetValueToUnspecified();
return true;
}
size_t start = 0;
if ( text.length() > 0 && !wxIsalnum(text[0]) )
start++;
wxString s = text.substr(start, text.length() - start);
bool res = s.ToULong(&value, (unsigned int)m_realBase);
//wxChar *end;
//value = wxStrtoul(text.c_str() + ((size_t)start), &end, (unsigned int)m_realBase);
if ( res && m_value != (long)value )
{
return StdValidationProcedure((long)value);
}
/*}
else if ( argFlags & wxPG_REPORT_ERROR )
{
s.Printf ( wxT("! %s: \"%s\" is not a number."), m_label.c_str(), text.c_str() );
ShowError(s);
}*/
return false;
}
bool wxUIntPropertyClass::SetValueFromInt( long value, int WXUNUSED(flags) )
{
if ( m_value != value )
{
m_value = value;
return true;
}
return false;
}
void wxUIntPropertyClass::SetAttribute( int id, wxVariant& value )
{
if ( id == wxPG_UINT_BASE )
{
int val = value.GetLong();
m_realBase = (wxByte) val;
if ( m_realBase > 16 )
m_realBase = 16;
//
// Translate logical base to a template array index
m_base = 7; // oct
if ( val == wxPG_BASE_HEX )
m_base = 3;
else if ( val == wxPG_BASE_DEC )
m_base = 6;
else if ( val == wxPG_BASE_HEXL )
m_base = 0;
}
else if ( id == wxPG_UINT_PREFIX )
m_prefix = (wxByte) value.GetLong();
}
// -----------------------------------------------------------------------
// wxFloatProperty
// -----------------------------------------------------------------------
wxPG_BEGIN_PROPERTY_CLASS_BODY(wxFloatProperty,wxPGProperty,double,double)
WX_PG_DECLARE_BASIC_TYPE_METHODS()
WX_PG_DECLARE_ATTRIBUTE_METHODS()
protected:
int m_precision;
#if wxUSE_VALIDATORS
//static wxValidator* GetClassValidator ();
virtual wxValidator* DoGetValidator () const;
#endif
wxPG_END_PROPERTY_CLASS_BODY()
WX_PG_IMPLEMENT_PROPERTY_CLASS(wxFloatProperty,wxBaseProperty,
double,double,TextCtrl)
wxFloatPropertyClass::wxFloatPropertyClass( const wxString& label,
const wxString& name,
double value )
: wxPGProperty(label,name)
{
m_precision = -1;
DoSetValue(value);
}
wxFloatPropertyClass::~wxFloatPropertyClass() { }
void wxFloatPropertyClass::DoSetValue( wxPGVariant value )
{
m_value = wxPGVariantToDouble(value);
}
wxPGVariant wxFloatPropertyClass::DoGetValue() const
{
return wxPGVariant(m_value);
}
// This helper method provides standard way for floating point-using
// properties to convert values to string.
void wxPropertyGrid::DoubleToString(wxString& target,
double value,
int precision,
bool removeZeroes,
wxString* precTemplate)
{
if ( precision >= 0 )
{
wxString text1;
if (!precTemplate)
precTemplate = &text1;
if ( !precTemplate->length() )
{
*precTemplate = wxT("%.");
*precTemplate << wxString::Format( wxT("%i"), precision );
*precTemplate << wxT('f');
}
target.Printf( precTemplate->c_str(), value );
}
else
{
target.Printf( wxT("%f"), value );
}
if ( removeZeroes && precision != 0 && target.length() )
{
// Remove excess zeroes (do not remove this code just yet,
// since sprintf can't do the same consistently across platforms).
wxString::const_iterator i = target.end() - 1;
size_t new_len = target.length() - 1;
for ( ; i != target.begin(); i-- )
{
if ( wxPGGetIterChar(target, i) != wxT('0') )
break;
new_len--;
}
wxChar cur_char = wxPGGetIterChar(target, i);
if ( cur_char != wxT('.') && cur_char != wxT(',') )
new_len++;
if ( new_len != target.length() )
target.resize(new_len);
/*
unsigned int cur_pos = target.length() - 1;
wxChar a;
a = target.GetChar( cur_pos );
while ( a == wxT('0') && cur_pos > 0 )
{
cur_pos--;
a = target.GetChar( cur_pos );
}
wxChar cur_char = target.GetChar( cur_pos );
if ( cur_char != wxT('.') && cur_char != wxT(',') )
cur_pos += 1;
if ( cur_pos < target.length() )
target.Truncate( cur_pos );
*/
}
}
wxString wxFloatPropertyClass::GetValueAsString( int argFlags ) const
{
wxString text;
wxPropertyGrid::DoubleToString(text,m_value,
m_precision,
!(argFlags & wxPG_FULL_VALUE),
(wxString*) NULL);
return text;
}
bool wxFloatPropertyClass::SetValueFromString( const wxString& text, int argFlags )
{
wxString s;
double value;
if ( text.length() == 0 )
{
SetValueToUnspecified();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -