📄 proplist.cpp
字号:
IMPLEMENT_DYNAMIC_CLASS(wxColourListValidator, wxPropertyListValidator)
wxColourListValidator::wxColourListValidator(long flags):
wxPropertyListValidator(flags)
{
}
wxColourListValidator::~wxColourListValidator()
{
}
bool wxColourListValidator::OnCheckValue(wxProperty *WXUNUSED(property), wxPropertyListView *WXUNUSED(view), wxWindow *WXUNUSED(parentWindow))
{
return true;
}
// Called when TICK is pressed or focus is lost or view wants to update
// the property list.
// Does the transferance from the property editing area to the property itself
bool wxColourListValidator::OnRetrieveValue(wxProperty *property, wxPropertyListView *view, wxWindow *WXUNUSED(parentWindow))
{
if (!view->GetValueText())
return false;
wxString value(view->GetValueText()->GetValue());
property->GetValue() = value ;
return true;
}
// Called when TICK is pressed or focus is lost or view wants to update
// the property list.
// Does the transferance from the property editing area to the property itself
bool wxColourListValidator::OnDisplayValue(wxProperty *property, wxPropertyListView *view, wxWindow *WXUNUSED(parentWindow))
{
if (!view->GetValueText())
return false;
wxString str(property->GetValue().GetStringRepresentation());
view->GetValueText()->SetValue(str);
return true;
}
// Called when the property is double clicked. Extra functionality can be provided,
// cycling through possible values.
bool wxColourListValidator::OnDoubleClick(wxProperty *property, wxPropertyListView *view, wxWindow *parentWindow)
{
if (!view->GetValueText())
return false;
OnEdit(property, view, parentWindow);
return true;
}
bool wxColourListValidator::OnPrepareControls(wxProperty *WXUNUSED(property), wxPropertyListView *view, wxWindow *WXUNUSED(parentWindow))
{
if (view->GetConfirmButton())
view->GetConfirmButton()->Enable();
if (view->GetCancelButton())
view->GetCancelButton()->Enable();
if (view->GetEditButton())
view->GetEditButton()->Enable();
if (view->GetValueText())
view->GetValueText()->Enable((GetFlags() & wxPROP_ALLOW_TEXT_EDITING) == wxPROP_ALLOW_TEXT_EDITING);
return true;
}
void wxColourListValidator::OnEdit(wxProperty *property, wxPropertyListView *view, wxWindow *parentWindow)
{
if (!view->GetValueText())
return;
wxChar *s = property->GetValue().StringValue();
unsigned char r = 0;
unsigned char g = 0;
unsigned char b = 0;
if (s)
{
r = (unsigned char)wxHexToDec(s);
g = (unsigned char)wxHexToDec(s+2);
b = (unsigned char)wxHexToDec(s+4);
}
wxColour col(r,g,b);
wxColourData data;
data.SetChooseFull(true);
data.SetColour(col);
for (int i = 0; i < 16; i++)
{
wxColour colour((unsigned char)(i*16),
(unsigned char)(i*16),
(unsigned char)(i*16));
data.SetCustomColour(i, colour);
}
wxColourDialog dialog(parentWindow, &data);
if (dialog.ShowModal() != wxID_CANCEL)
{
wxColourData retData = dialog.GetColourData();
col = retData.GetColour();
wxChar buf[7];
wxDecToHex(col.Red(), buf);
wxDecToHex(col.Green(), buf+2);
wxDecToHex(col.Blue(), buf+4);
property->GetValue() = wxString(buf);
view->DisplayProperty(property);
view->UpdatePropertyDisplayInList(property);
view->OnPropertyChanged(property);
}
}
///
/// List of strings validator. For this we need more user interface than
/// we get with a property list; so create a new dialog for editing the list.
///
IMPLEMENT_DYNAMIC_CLASS(wxListOfStringsListValidator, wxPropertyListValidator)
wxListOfStringsListValidator::wxListOfStringsListValidator(long flags):
wxPropertyListValidator(flags)
{
}
bool wxListOfStringsListValidator::OnCheckValue(wxProperty *WXUNUSED(property), wxPropertyListView *WXUNUSED(view), wxWindow *WXUNUSED(parentWindow))
{
// No constraints for an arbitrary, user-editable list of strings.
return true;
}
// Called when TICK is pressed or focus is lost or view wants to update
// the property list.
// Does the transferance from the property editing area to the property itself.
// In this case, the user cannot directly edit the string list.
bool wxListOfStringsListValidator::OnRetrieveValue(wxProperty *WXUNUSED(property), wxPropertyListView *WXUNUSED(view), wxWindow *WXUNUSED(parentWindow))
{
return true;
}
bool wxListOfStringsListValidator::OnDisplayValue(wxProperty *property, wxPropertyListView *view, wxWindow *WXUNUSED(parentWindow))
{
if (!view->GetValueText())
return false;
wxString str(property->GetValue().GetStringRepresentation());
view->GetValueText()->SetValue(str);
return true;
}
bool wxListOfStringsListValidator::OnPrepareControls(wxProperty *WXUNUSED(property), wxPropertyListView *view, wxWindow *WXUNUSED(parentWindow))
{
if (view->GetEditButton())
view->GetEditButton()->Enable();
if (view->GetValueText())
view->GetValueText()->Disable();
if (view->GetConfirmButton())
view->GetConfirmButton()->Disable();
if (view->GetCancelButton())
view->GetCancelButton()->Disable();
return true;
}
// Called when the property is double clicked. Extra functionality can be provided,
// cycling through possible values.
bool wxListOfStringsListValidator::OnDoubleClick(wxProperty *property, wxPropertyListView *view, wxWindow *parentWindow)
{
OnEdit(property, view, parentWindow);
return true;
}
void wxListOfStringsListValidator::OnEdit( wxProperty *property,
wxPropertyListView *view,
wxWindow *parentWindow )
{
// Convert property value to a list of strings for editing
wxStringList *stringList = new wxStringList;
wxPropertyValue *expr = property->GetValue().GetFirst();
while (expr)
{
wxChar *s = expr->StringValue();
if (s)
stringList->Add(s);
expr = expr->GetNext();
}
wxString title(wxT("Editing "));
title += property->GetName();
if (EditStringList(parentWindow, stringList, title.GetData()))
{
wxPropertyValue& oldValue = property->GetValue();
oldValue.ClearList();
wxStringList::Node *node = stringList->GetFirst();
while (node)
{
wxChar *s = node->GetData();
oldValue.Append(new wxPropertyValue(s));
node = node->GetNext();
}
view->DisplayProperty(property);
view->UpdatePropertyDisplayInList(property);
view->OnPropertyChanged(property);
}
delete stringList;
}
class wxPropertyStringListEditorDialog: public wxDialog
{
public:
wxPropertyStringListEditorDialog(wxWindow *parent, const wxString& title,
const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize,
long windowStyle = wxDEFAULT_DIALOG_STYLE, const wxString& name = wxT("stringEditorDialogBox")):
wxDialog(parent, wxID_ANY, title, pos, size, windowStyle, name)
{
m_stringList = NULL;
m_stringText = NULL;
m_listBox = NULL;
sm_dialogCancelled = false;
m_currentSelection = -1;
}
~wxPropertyStringListEditorDialog(void) {}
void OnCloseWindow(wxCloseEvent& event);
void SaveCurrentSelection(void);
void ShowCurrentSelection(void);
void OnOK(wxCommandEvent& event);
void OnCancel(wxCommandEvent& event);
void OnAdd(wxCommandEvent& event);
void OnDelete(wxCommandEvent& event);
void OnStrings(wxCommandEvent& event);
void OnText(wxCommandEvent& event);
public:
wxStringList* m_stringList;
wxListBox* m_listBox;
wxTextCtrl* m_stringText;
static bool sm_dialogCancelled;
int m_currentSelection;
DECLARE_EVENT_TABLE()
};
#define wxID_PROP_SL_ADD 3000
#define wxID_PROP_SL_DELETE 3001
#define wxID_PROP_SL_STRINGS 3002
#define wxID_PROP_SL_TEXT 3003
BEGIN_EVENT_TABLE(wxPropertyStringListEditorDialog, wxDialog)
EVT_BUTTON(wxID_OK, wxPropertyStringListEditorDialog::OnOK)
EVT_BUTTON(wxID_CANCEL, wxPropertyStringListEditorDialog::OnCancel)
EVT_BUTTON(wxID_PROP_SL_ADD, wxPropertyStringListEditorDialog::OnAdd)
EVT_BUTTON(wxID_PROP_SL_DELETE, wxPropertyStringListEditorDialog::OnDelete)
EVT_LISTBOX(wxID_PROP_SL_STRINGS, wxPropertyStringListEditorDialog::OnStrings)
EVT_TEXT_ENTER(wxID_PROP_SL_TEXT, wxPropertyStringListEditorDialog::OnText)
EVT_CLOSE(wxPropertyStringListEditorDialog::OnCloseWindow)
END_EVENT_TABLE()
class wxPropertyStringListEditorText: public wxTextCtrl
{
public:
wxPropertyStringListEditorText(wxWindow *parent, wxWindowID id, const wxString& val,
const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize,
long windowStyle = 0, const wxString& name = wxT("text")):
wxTextCtrl(parent, id, val, pos, size, windowStyle, wxDefaultValidator, name)
{
}
void OnKillFocus()
{
wxPropertyStringListEditorDialog *dialog = (wxPropertyStringListEditorDialog *)GetParent();
dialog->SaveCurrentSelection();
}
};
bool wxPropertyStringListEditorDialog::sm_dialogCancelled = false;
// Edit the string list.
bool wxListOfStringsListValidator::EditStringList(wxWindow *parent, wxStringList *stringList, const wxChar *title)
{
int largeButtonWidth = 60;
int largeButtonHeight = 25;
wxBeginBusyCursor();
wxPropertyStringListEditorDialog *dialog = new wxPropertyStringListEditorDialog(parent,
title, wxPoint(10, 10), wxSize(400, 400));
dialog->m_stringList = stringList;
dialog->m_listBox = new wxListBox(dialog, wxID_PROP_SL_STRINGS,
wxDefaultPosition, wxDefaultSize, 0, NULL, wxLB_SINGLE);
dialog->m_stringText = new wxPropertyStringListEditorText(dialog,
wxID_PROP_SL_TEXT, wxEmptyString, wxPoint(5, 240),
wxSize(300, wxDefaultCoord), wxTE_PROCESS_ENTER);
dialog->m_stringText->Disable();
wxButton *addButton = new wxButton(dialog, wxID_PROP_SL_ADD, wxT("Add"), wxDefaultPosition, wxSize(largeButtonWidth, largeButtonHeight));
wxButton *deleteButton = new wxButton(dialog, wxID_PROP_SL_DELETE, wxT("Delete"), wxDefaultPosition, wxSize(largeButtonWidth, largeButtonHeight));
wxButton *cancelButton = new wxButton(dialog, wxID_CANCEL, wxT("Cancel"), wxDefaultPosition, wxSize(largeButtonWidth, largeButtonHeight));
wxButton *okButton = new wxButton(dialog, wxID_OK, wxT("OK"), wxDefaultPosition, wxSize(largeButtonWidth, largeButtonHeight));
#ifndef __WXGTK__
okButton->SetDefault();
#endif
wxBoxSizer *m_bottom_sizer = new wxBoxSizer( wxHORIZONTAL );
m_bottom_sizer->Add(addButton, 0, wxALL | wxALIGN_LEFT, 2 );
m_bottom_sizer->Add(deleteButton, 0, wxALL | wxALIGN_LEFT, 2 );
m_bottom_sizer->Add(1, 1, 1, wxEXPAND | wxALL);
m_bottom_sizer->Add(cancelButton, 0, wxALL | wxALIGN_RIGHT, 2 );
m_bottom_sizer->Add(okButton, 0, wxALL | wxALIGN_RIGHT, 2 );
wxBoxSizer *m_sizer = new wxBoxSizer( wxVERTICAL );
m_sizer->Add(dialog->m_listBox, 1, wxEXPAND | wxALL, 2 );
m_sizer->Add(dialog->m_stringText, 0, wxEXPAND | wxALL, 2 );
m_sizer->Add(m_bottom_sizer, 0, wxEXPAND | wxALL , 0 );
dialog->SetSizer( m_sizer );
m_sizer->SetSizeHints( dialog );
wxStringList::Node *node = stringList->GetFirst();
while (node)
{
wxChar *str = node->GetData();
// Save node as client data for each listbox item
dialog->m_listBox->Append(str, (wxChar *)node);
node = node->GetNext();
}
dialog->SetClientSize(310, 305);
dialog->Layout();
dialog->Centre(wxBOTH);
wxEndBusyCursor();
if (dialog->ShowModal() == wxID_CANCEL)
return false;
else
return true;
}
/*
* String list editor callbacks
*
*/
void wxPropertyStringListEditorDialog::OnStrings(wxCommandEvent& WXUNUSED(event))
{
int sel = m_listBox->GetSelection();
if (sel != wxNOT_FOUND)
{
m_currentSelection = sel;
ShowCurrentSelection();
}
}
void wxPropertyStringListEditorDialog::OnDelete(wxCommandEvent& WXUNUSED(event))
{
int sel = m_listBox->GetSelection();
if (sel == wxNOT_FOUND)
return;
wxNode *node = (wxNode *)m_listBox->wxListBox::GetClientData(sel);
if (!node)
return;
m_listBox->Delete(sel);
delete[] (wxChar *)node->GetData();
delete node;
m_currentSelection = -1;
m_stringText->SetValue(wxEmptyString);
}
void wxPropertyStringListEditorDialog::OnAdd(wxCommandEvent& WXUNUSED(event))
{
SaveCurrentSelection();
wxString initialText;
wxNode *node = m_stringList->Add(initialText);
m_listBox->Append(initialText, (void *)node);
m_currentSelection = m_stringList->GetCount() - 1;
m_listBox->SetSelection(m_currentSelection);
ShowCurrentSelection();
m_stringText->SetFocus();
}
void wxPropertyStringListEditorDialog::OnOK(wxCommandEvent& WXUNUSED(event))
{
SaveCurrentSelection();
EndModal(wxID_OK);
// Close(true);
this->Destroy();
}
void wxPropertyStringListEditorDialog::OnCancel(wxCommandEvent& WXUNUSED(event))
{
sm_dialogCancelled = true;
EndModal(wxID_CANCEL);
// Close(true);
this->Destroy();
}
void wxPropertyStringListEditorDialog::OnText(wxCommandEvent& event)
{
if (event.GetEventType() == wxEVT_COMMAND_TEXT_ENTER)
{
SaveCurrentSelection();
}
}
void
wxPropertyStringListEditorDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
{
SaveCurrentSelection();
Destroy();
}
void wxPropertyStringListEditorDialog::SaveCurrentSelection()
{
if (m_currentSelection == -1)
return;
wxNode *node = (wxNode *)m_listBox->wxListBox::GetClientData(m_currentSelection);
if (!node)
return;
wxString txt(m_stringText->GetValue());
if (node->GetData())
delete[] (wxChar *)node->GetData();
node->SetData((wxObject *)wxStrdup(txt));
m_listBox->SetString(m_currentSelection, (wxChar *)node->GetData());
}
void wxPropertyStringListEditorDialog::ShowCurrentSelection()
{
if (m_currentSelection == -1)
{
m_stringText->SetValue(wxEmptyString);
return;
}
wxNode *node = (wxNode *)m_listBox->wxListBox::GetClientData(m_currentSelection);
wxChar *txt = (wxChar *)node->GetData();
m_stringText->SetValue(txt);
m_stringText->Enable();
}
#endif // wxUSE_PROPSHEET
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -