- propgrid.png (11.05 KiB) Viewed 1493 times
Thank you.
Code: Select all
// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
// for all others, include the necessary headers (this file is usually all you
// need because it includes almost all "standard" wxWidgets headers)
#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif
#include <wx/dcmemory.h>
#include <wx/renderer.h>
#include <wx/propgrid/propgrid.h>
#include <wx/propgrid/advprops.h>
///////////// Declarations
class MyRenderer:public wxPGDefaultRenderer
{
public:
virtual bool Render(wxDC&, const wxRect&, const wxPropertyGrid*,
wxPGProperty*, int column, int item, int flags )
const wxOVERRIDE;
};
class MyStringProperty:public wxStringProperty
{
public:
MyStringProperty(const wxString &label=wxPG_LABEL,
const wxString &name=wxPG_LABEL,
const wxString &value=wxEmptyString,
bool isChecked = false);
virtual wxPGCellRenderer* GetCellRenderer (int column) const wxOVERRIDE;
bool IsChecked() const;
void ChangeStatus();
int GetCheckMinX() const;
int GetCheckMaxX() const;
void SetCheckMinX(int x);
void SetCheckMaxX(int x);
private:
bool m_checked;
int m_checkMinX;
int m_checkMaxX;
wxDECLARE_ABSTRACT_CLASS(MyStringProperty);
};
class MyFrame : public wxFrame
{
public:
MyFrame( wxWindow* parent, int id = wxID_ANY, wxString title = "Demo",
wxPoint pos = wxDefaultPosition, wxSize size = wxSize(481,466),
int style = wxDEFAULT_FRAME_STYLE|wxTAB_TRAVERSAL );
private:
void OnLeftDown(wxMouseEvent& event);
wxPropertyGrid* m_propGrid;
};
class MyApp : public wxApp
{
public:
virtual bool OnInit() wxOVERRIDE;
virtual int OnExit () wxOVERRIDE;
MyRenderer* m_renderer;
};
wxDECLARE_APP(MyApp);
///////////// Implementation
bool MyRenderer::Render( wxDC& dc,
const wxRect& rect,
const wxPropertyGrid* propertyGrid,
wxPGProperty* property,
int column,
int item,
int flags ) const
{
if ( column == 0 && item == -1 )
{
MyStringProperty* msp = wxDynamicCast(property, MyStringProperty);
if ( msp )
{
// Make a bitmap of a checkbox
wxRendererNative& renderer = wxRendererNative::Get();
wxSize sz = renderer.GetCheckBoxSize(
const_cast<wxPropertyGrid*>(propertyGrid));
wxBitmap bmp(sz.GetWidth(), rect.GetHeight()-1);
wxMemoryDC mem(bmp);
mem.SetBackground(wxBrush(*wxGREEN));
mem.Clear();
wxRect r(0, 0, sz.GetWidth(), rect.GetHeight()-1);
int cbFlags = msp->IsChecked() ? wxCONTROL_CHECKED : 0;
renderer.DrawCheckBox(const_cast<wxPropertyGrid*>(propertyGrid),
mem, r, cbFlags) ;
mem.SelectObject(wxNullBitmap);
bmp.SetMask(new wxMask(bmp, *wxGREEN));
// Set the bitmap that was made to be the cell's bitmap
property->GetCell(0).SetBitmap(bmp);
// Record the location at which the checkbox was drawn, so that
// we can tell if mouse clicks occur over it
int checkMinX =
rect.GetLeft() + wxPG_CONTROL_MARGIN + wxCC_CUSTOM_IMAGE_MARGIN1;
msp->SetCheckMinX(checkMinX);
msp->SetCheckMaxX(checkMinX+sz.GetWidth());
}
}
return wxPGDefaultRenderer::Render(dc, rect, propertyGrid, property,
column,item,flags );
}
MyStringProperty::MyStringProperty(const wxString &label, const wxString &name,
const wxString &value, bool isChecked)
:wxStringProperty (label, name, value)
{
m_checked = isChecked;
}
wxPGCellRenderer* MyStringProperty::GetCellRenderer(int column) const
{
if ( column == 0 )
return wxGetApp().m_renderer;
else
return wxStringProperty::GetCellRenderer(column);
}
bool MyStringProperty::IsChecked() const
{
return m_checked;
}
void MyStringProperty::ChangeStatus()
{
m_checked=!m_checked;
}
int MyStringProperty::GetCheckMinX() const
{
return m_checkMinX;
}
int MyStringProperty::GetCheckMaxX() const
{
return m_checkMaxX;
}
void MyStringProperty::SetCheckMinX(int x)
{
m_checkMinX=x;
}
void MyStringProperty::SetCheckMaxX(int x)
{
m_checkMaxX=x;
}
MyFrame::MyFrame( wxWindow* parent, int id, wxString title, wxPoint pos,
wxSize size, int style )
:wxFrame( parent, id, title, pos, size, style )
{
wxPanel* panel = new wxPanel( this, wxID_ANY );
m_propGrid = new wxPropertyGrid(panel, wxID_ANY, wxDefaultPosition,
wxDefaultSize,
wxPG_DEFAULT_STYLE|wxPG_SPLITTER_AUTO_CENTER
);
m_propGrid->Append( new wxStringProperty("String without checkbox") );
m_propGrid->Append( new MyStringProperty("String with checkbox") );
m_propGrid->Bind(wxEVT_LEFT_DOWN,&MyFrame::OnLeftDown,this);
wxBoxSizer* bSizer = new wxBoxSizer( wxVERTICAL );
bSizer->Add( m_propGrid, 1, wxALL|wxEXPAND, 5 );
panel->SetSizer( bSizer );
Layout();
}
void MyFrame::OnLeftDown(wxMouseEvent& event)
{
wxPropertyGridHitTestResult htr = m_propGrid->HitTest(event.GetPosition());
wxPGProperty* prop = htr.GetProperty();
MyStringProperty* msp = wxDynamicCast(prop, MyStringProperty);
if ( msp && htr.GetColumn()==0 )
{
int minx = msp->GetCheckMinX();
int maxx = msp->GetCheckMaxX();
int evx = event.GetPosition().x;
if ( minx <= evx && evx <= maxx )
msp->ChangeStatus();
}
event.Skip();
}
bool MyApp::OnInit()
{
m_renderer = new MyRenderer();
MyFrame* frame = new MyFrame(NULL);
frame->Show();
return true;
}
int MyApp::OnExit()
{
if(m_renderer)
delete m_renderer;
return wxApp::OnExit();
}
wxIMPLEMENT_ABSTRACT_CLASS(MyStringProperty,wxStringProperty);
wxIMPLEMENT_APP(MyApp);