]>
git.saurik.com Git - wxWidgets.git/blob - src/generic/propform.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Property form classes
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "propform.h"
16 // For compilers that support precompilation, includes "wx/wx.h".
17 #include "wx/wxprec.h"
27 #include "wx/propform.h"
39 IMPLEMENT_DYNAMIC_CLASS(wxPropertyFormView
, wxPropertyView
)
41 BEGIN_EVENT_TABLE(wxPropertyFormView
, wxPropertyView
)
42 EVT_BUTTON(wxID_OK
, wxPropertyFormView::OnOk
)
43 EVT_BUTTON(wxID_CANCEL
, wxPropertyFormView::OnCancel
)
44 EVT_BUTTON(wxID_HELP
, wxPropertyFormView::OnHelp
)
45 EVT_BUTTON(wxID_PROP_REVERT
, wxPropertyFormView::OnRevert
)
46 EVT_BUTTON(wxID_PROP_UPDATE
, wxPropertyFormView::OnUpdate
)
49 bool wxPropertyFormView::sm_dialogCancelled
= FALSE
;
51 wxPropertyFormView::wxPropertyFormView(wxWindow
*propPanel
, long flags
):wxPropertyView(flags
)
53 m_propertyWindow
= propPanel
;
54 m_managedWindow
= NULL
;
56 m_windowCloseButton
= NULL
;
57 m_windowCancelButton
= NULL
;
58 m_windowHelpButton
= NULL
;
60 m_detailedEditing
= FALSE
;
63 wxPropertyFormView::~wxPropertyFormView(void)
67 void wxPropertyFormView::ShowView(wxPropertySheet
*ps
, wxWindow
*panel
)
71 AssociatePanel(panel
);
73 // UpdatePropertyList();
76 // Update this view of the viewed object, called e.g. by
78 bool wxPropertyFormView::OnUpdateView(void)
83 bool wxPropertyFormView::Check(void)
88 wxNode
*node
= m_propertySheet
->GetProperties().First();
91 wxProperty
*prop
= (wxProperty
*)node
->Data();
92 wxPropertyValidator
*validator
= FindPropertyValidator(prop
);
93 if (validator
&& validator
->IsKindOf(CLASSINFO(wxPropertyFormValidator
)))
95 wxPropertyFormValidator
*formValidator
= (wxPropertyFormValidator
*)validator
;
96 if (!formValidator
->OnCheckValue(prop
, this, m_propertyWindow
))
104 bool wxPropertyFormView::TransferToPropertySheet(void)
106 if (!m_propertySheet
)
109 wxNode
*node
= m_propertySheet
->GetProperties().First();
112 wxProperty
*prop
= (wxProperty
*)node
->Data();
113 wxPropertyValidator
*validator
= FindPropertyValidator(prop
);
114 if (validator
&& validator
->IsKindOf(CLASSINFO(wxPropertyFormValidator
)))
116 wxPropertyFormValidator
*formValidator
= (wxPropertyFormValidator
*)validator
;
117 formValidator
->OnRetrieveValue(prop
, this, m_propertyWindow
);
124 bool wxPropertyFormView::TransferToDialog(void)
126 if (!m_propertySheet
)
129 wxNode
*node
= m_propertySheet
->GetProperties().First();
132 wxProperty
*prop
= (wxProperty
*)node
->Data();
133 wxPropertyValidator
*validator
= FindPropertyValidator(prop
);
134 if (validator
&& validator
->IsKindOf(CLASSINFO(wxPropertyFormValidator
)))
136 wxPropertyFormValidator
*formValidator
= (wxPropertyFormValidator
*)validator
;
137 formValidator
->OnDisplayValue(prop
, this, m_propertyWindow
);
144 bool wxPropertyFormView::AssociateNames(void)
146 if (!m_propertySheet
|| !m_propertyWindow
)
149 wxNode
*node
= m_propertyWindow
->GetChildren().First();
152 wxWindow
*win
= (wxWindow
*)node
->Data();
153 if (win
->GetName() != _T(""))
155 wxProperty
*prop
= m_propertySheet
->GetProperty(win
->GetName());
157 prop
->SetWindow(win
);
165 bool wxPropertyFormView::OnClose(void)
171 void wxPropertyFormView::OnOk(wxCommandEvent
& WXUNUSED(event
))
173 // Retrieve the value if any
177 sm_dialogCancelled
= FALSE
;
178 TransferToPropertySheet();
180 m_managedWindow
->Close(TRUE
);
183 void wxPropertyFormView::OnCancel(wxCommandEvent
& WXUNUSED(event
))
185 sm_dialogCancelled
= TRUE
;
187 m_managedWindow
->Close(TRUE
);
190 void wxPropertyFormView::OnHelp(wxCommandEvent
& WXUNUSED(event
))
194 void wxPropertyFormView::OnUpdate(wxCommandEvent
& WXUNUSED(event
))
197 TransferToPropertySheet();
200 void wxPropertyFormView::OnRevert(wxCommandEvent
& WXUNUSED(event
))
205 void wxPropertyFormView::OnCommand(wxWindow
& win
, wxCommandEvent
& event
)
207 if (!m_propertySheet
)
210 if (win
.GetName() == _T(""))
213 if (wxStrcmp(win
.GetName(), _T("ok")) == 0)
215 else if (wxStrcmp(win
.GetName(), _T("cancel")) == 0)
217 else if (wxStrcmp(win
.GetName(), _T("help")) == 0)
219 else if (wxStrcmp(win
.GetName(), _T("update")) == 0)
221 else if (wxStrcmp(win
.GetName(), _T("revert")) == 0)
225 // Find a validator to route the command to.
226 wxNode
*node
= m_propertySheet
->GetProperties().First();
229 wxProperty
*prop
= (wxProperty
*)node
->Data();
230 if (prop
->GetWindow() && (prop
->GetWindow() == &win
))
232 wxPropertyValidator
*validator
= FindPropertyValidator(prop
);
233 if (validator
&& validator
->IsKindOf(CLASSINFO(wxPropertyFormValidator
)))
235 wxPropertyFormValidator
*formValidator
= (wxPropertyFormValidator
*)validator
;
236 formValidator
->OnCommand(prop
, this, m_propertyWindow
, event
);
245 void wxPropertyFormView::OnDoubleClick(wxControl
*item
)
247 if (!m_propertySheet
)
250 // Find a validator to route the command to.
251 wxNode
*node
= m_propertySheet
->GetProperties().First();
254 wxProperty
*prop
= (wxProperty
*)node
->Data();
255 if (prop
->GetWindow() && ((wxControl
*)prop
->GetWindow() == item
))
257 wxPropertyValidator
*validator
= FindPropertyValidator(prop
);
258 if (validator
&& validator
->IsKindOf(CLASSINFO(wxPropertyFormValidator
)))
260 wxPropertyFormValidator
*formValidator
= (wxPropertyFormValidator
*)validator
;
261 formValidator
->OnDoubleClick(prop
, this, m_propertyWindow
);
270 * Property form dialog box
273 IMPLEMENT_CLASS(wxPropertyFormDialog
, wxDialog
)
275 BEGIN_EVENT_TABLE(wxPropertyFormDialog
, wxDialog
)
276 EVT_CLOSE(wxPropertyFormDialog::OnCloseWindow
)
279 wxPropertyFormDialog::wxPropertyFormDialog(wxPropertyFormView
*v
, wxWindow
*parent
, const wxString
& title
,
280 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
):
281 wxDialog(parent
, -1, title
, pos
, size
, style
, name
)
284 m_view
->AssociatePanel(this);
285 m_view
->SetManagedWindow(this);
286 // SetAutoLayout(TRUE);
289 void wxPropertyFormDialog::OnCloseWindow(wxCloseEvent
& event
)
301 void wxPropertyFormDialog::OnDefaultAction(wxControl
*item
)
303 m_view
->OnDoubleClick(item
);
306 void wxPropertyFormDialog::OnCommand(wxWindow
& win
, wxCommandEvent
& event
)
309 m_view
->OnCommand(win
, event
);
312 // Extend event processing to search the view's event table
313 bool wxPropertyFormDialog::ProcessEvent(wxEvent
& event
)
315 if ( !m_view
|| ! m_view
->ProcessEvent(event
) )
316 return wxEvtHandler::ProcessEvent(event
);
323 * Property form panel
326 IMPLEMENT_CLASS(wxPropertyFormPanel
, wxPanel
)
328 void wxPropertyFormPanel::OnDefaultAction(wxControl
*item
)
330 m_view
->OnDoubleClick(item
);
333 void wxPropertyFormPanel::OnCommand(wxWindow
& win
, wxCommandEvent
& event
)
335 m_view
->OnCommand(win
, event
);
338 // Extend event processing to search the view's event table
339 bool wxPropertyFormPanel::ProcessEvent(wxEvent
& event
)
341 if ( !m_view
|| ! m_view
->ProcessEvent(event
) )
342 return wxEvtHandler::ProcessEvent(event
);
351 IMPLEMENT_CLASS(wxPropertyFormFrame
, wxFrame
)
353 BEGIN_EVENT_TABLE(wxPropertyFormFrame
, wxFrame
)
354 EVT_CLOSE(wxPropertyFormFrame::OnCloseWindow
)
357 void wxPropertyFormFrame::OnCloseWindow(wxCloseEvent
& event
)
359 if (m_view
&& m_view
->OnClose())
365 wxPanel
*wxPropertyFormFrame::OnCreatePanel(wxFrame
*parent
, wxPropertyFormView
*v
)
367 return new wxPropertyFormPanel(v
, parent
);
370 bool wxPropertyFormFrame::Initialize(void)
372 m_propertyPanel
= OnCreatePanel(this, m_view
);
375 m_view
->AssociatePanel(m_propertyPanel
);
376 m_view
->SetManagedWindow(this);
384 * Property form specific validator
387 IMPLEMENT_ABSTRACT_CLASS(wxPropertyFormValidator
, wxPropertyValidator
)
394 IMPLEMENT_DYNAMIC_CLASS(wxRealFormValidator
, wxPropertyFormValidator
)
397 /// Real number form validator
399 bool wxRealFormValidator::OnCheckValue( wxProperty
*property
, wxPropertyFormView
*WXUNUSED(view
),
400 wxWindow
*parentWindow
)
402 if (m_realMin
== 0.0 && m_realMax
== 0.0)
405 // The item used for viewing the real number: should be a text item.
406 wxWindow
*m_propertyWindow
= property
->GetWindow();
407 if (!m_propertyWindow
|| !m_propertyWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)))
410 wxString
value(((wxTextCtrl
*)m_propertyWindow
)->GetValue());
413 if (!StringToFloat(WXSTRINGCAST value
, &val
))
416 wxSprintf(buf
, _T("Value %s is not a valid real number!"), (const wxChar
*)value
);
417 wxMessageBox(buf
, _T("Property value error"), wxOK
| wxICON_EXCLAMATION
, parentWindow
);
421 if (val
< m_realMin
|| val
> m_realMax
)
424 wxSprintf(buf
, _T("Value must be a real number between %.2f and %.2f!"), m_realMin
, m_realMax
);
425 wxMessageBox(buf
, _T("Property value error"), wxOK
| wxICON_EXCLAMATION
, parentWindow
);
431 bool wxRealFormValidator::OnRetrieveValue(wxProperty
*property
, wxPropertyFormView
*WXUNUSED(view
),
432 wxWindow
*WXUNUSED(parentWindow
) )
434 // The item used for viewing the real number: should be a text item.
435 wxWindow
*m_propertyWindow
= property
->GetWindow();
436 if (!m_propertyWindow
|| !m_propertyWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)))
439 wxString
value(((wxTextCtrl
*)m_propertyWindow
)->GetValue());
441 if (value
.Length() == 0)
444 float f
= (float)wxAtof((const wxChar
*)value
);
445 property
->GetValue() = f
;
449 bool wxRealFormValidator::OnDisplayValue(wxProperty
*property
, wxPropertyFormView
*WXUNUSED(view
),
450 wxWindow
*WXUNUSED(parentWindow
) )
452 // The item used for viewing the real number: should be a text item.
453 wxWindow
*m_propertyWindow
= property
->GetWindow();
454 if (!m_propertyWindow
|| !m_propertyWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)))
457 wxTextCtrl
*textItem
= (wxTextCtrl
*)m_propertyWindow
;
458 textItem
->SetValue(FloatToString(property
->GetValue().RealValue()));
463 /// Integer validator
465 IMPLEMENT_DYNAMIC_CLASS(wxIntegerFormValidator
, wxPropertyFormValidator
)
467 bool wxIntegerFormValidator::OnCheckValue(wxProperty
*property
, wxPropertyFormView
*WXUNUSED(view
),
468 wxWindow
*parentWindow
)
470 if (m_integerMin
== 0.0 && m_integerMax
== 0.0)
473 // The item used for viewing the real number: should be a text item or a slider
474 wxWindow
*m_propertyWindow
= property
->GetWindow();
475 if (!m_propertyWindow
)
480 if (m_propertyWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)))
482 wxString
value(((wxTextCtrl
*)m_propertyWindow
)->GetValue());
484 if (!StringToLong(WXSTRINGCAST value
, &val
))
487 wxSprintf(buf
, _T("Value %s is not a valid integer!"), (const wxChar
*)value
);
488 wxMessageBox(buf
, _T("Property value error"), wxOK
| wxICON_EXCLAMATION
, parentWindow
);
492 else if (m_propertyWindow
->IsKindOf(CLASSINFO(wxSlider
)))
494 val
= (long)((wxSlider
*)m_propertyWindow
)->GetValue();
499 if (val
< m_integerMin
|| val
> m_integerMax
)
502 sprintf(buf
, "Value must be an integer between %ld and %ld!", m_integerMin
, m_integerMax
);
503 wxMessageBox(buf
, "Property value error", wxOK
| wxICON_EXCLAMATION
, parentWindow
);
509 bool wxIntegerFormValidator::OnRetrieveValue(wxProperty
*property
, wxPropertyFormView
*WXUNUSED(view
),
510 wxWindow
*WXUNUSED(parentWindow
))
512 // The item used for viewing the real number: should be a text item or a slider
513 wxWindow
*m_propertyWindow
= property
->GetWindow();
514 if (!m_propertyWindow
)
517 if (m_propertyWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)))
519 wxString
value(((wxTextCtrl
*)m_propertyWindow
)->GetValue());
521 if (value
.Length() == 0)
524 long i
= wxAtol((const wxChar
*)value
);
525 property
->GetValue() = i
;
527 else if (m_propertyWindow
->IsKindOf(CLASSINFO(wxSlider
)))
529 property
->GetValue() = (long)((wxSlider
*)m_propertyWindow
)->GetValue();
537 bool wxIntegerFormValidator::OnDisplayValue( wxProperty
*property
, wxPropertyFormView
*WXUNUSED(view
),
538 wxWindow
*WXUNUSED(parentWindow
))
540 // The item used for viewing the real number: should be a text item or a slider
541 wxWindow
*m_propertyWindow
= property
->GetWindow();
542 if (!m_propertyWindow
)
545 if (m_propertyWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)))
547 wxTextCtrl
*textItem
= (wxTextCtrl
*)m_propertyWindow
;
548 textItem
->SetValue(LongToString(property
->GetValue().IntegerValue()));
550 else if (m_propertyWindow
->IsKindOf(CLASSINFO(wxSlider
)))
552 ((wxSlider
*)m_propertyWindow
)->SetValue((int)property
->GetValue().IntegerValue());
560 /// Boolean validator
562 IMPLEMENT_DYNAMIC_CLASS(wxBoolFormValidator
, wxPropertyFormValidator
)
564 bool wxBoolFormValidator::OnCheckValue(wxProperty
*property
, wxPropertyFormView
*WXUNUSED(view
),
565 wxWindow
*WXUNUSED(parentWindow
))
567 // The item used for viewing the boolean: should be a checkbox
568 wxWindow
*m_propertyWindow
= property
->GetWindow();
569 if (!m_propertyWindow
|| !m_propertyWindow
->IsKindOf(CLASSINFO(wxCheckBox
)))
575 bool wxBoolFormValidator::OnRetrieveValue(wxProperty
*property
, wxPropertyFormView
*WXUNUSED(view
),
576 wxWindow
*WXUNUSED(parentWindow
) )
578 // The item used for viewing the boolean: should be a checkbox.
579 wxWindow
*m_propertyWindow
= property
->GetWindow();
580 if (!m_propertyWindow
|| !m_propertyWindow
->IsKindOf(CLASSINFO(wxCheckBox
)))
583 wxCheckBox
*checkBox
= (wxCheckBox
*)m_propertyWindow
;
585 property
->GetValue() = (bool)checkBox
->GetValue();
589 bool wxBoolFormValidator::OnDisplayValue(wxProperty
*property
, wxPropertyFormView
*WXUNUSED(view
),
590 wxWindow
*WXUNUSED(parentWindow
))
592 // The item used for viewing the boolean: should be a checkbox.
593 wxWindow
*m_propertyWindow
= property
->GetWindow();
594 if (!m_propertyWindow
|| !m_propertyWindow
->IsKindOf(CLASSINFO(wxCheckBox
)))
597 wxCheckBox
*checkBox
= (wxCheckBox
*)m_propertyWindow
;
598 checkBox
->SetValue((bool)property
->GetValue().BoolValue());
605 IMPLEMENT_DYNAMIC_CLASS(wxStringFormValidator
, wxPropertyFormValidator
)
607 wxStringFormValidator::wxStringFormValidator(wxStringList
*list
, long flags
):
608 wxPropertyFormValidator(flags
)
613 bool wxStringFormValidator::OnCheckValue(wxProperty
*property
, wxPropertyFormView
*WXUNUSED(view
),
614 wxWindow
*parentWindow
)
619 // The item used for viewing the string: should be a text item, choice item or listbox.
620 wxWindow
*m_propertyWindow
= property
->GetWindow();
621 if (!m_propertyWindow
)
623 if (m_propertyWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)))
625 wxTextCtrl
*text
= (wxTextCtrl
*)m_propertyWindow
;
626 if (!m_strings
->Member(text
->GetValue()))
628 wxString
s("Value ");
629 s
+= text
->GetValue();
630 s
+= " is not valid.";
631 wxMessageBox(s
, "Property value error", wxOK
| wxICON_EXCLAMATION
, parentWindow
);
637 // Any other item constrains the string value,
638 // so we don't have to check it.
643 bool wxStringFormValidator::OnRetrieveValue(wxProperty
*property
, wxPropertyFormView
*WXUNUSED(view
),
644 wxWindow
*WXUNUSED(parentWindow
) )
646 // The item used for viewing the string: should be a text item, choice item or listbox.
647 wxWindow
*m_propertyWindow
= property
->GetWindow();
648 if (!m_propertyWindow
)
650 if (m_propertyWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)))
652 wxTextCtrl
*text
= (wxTextCtrl
*)m_propertyWindow
;
653 property
->GetValue() = text
->GetValue();
655 else if (m_propertyWindow
->IsKindOf(CLASSINFO(wxListBox
)))
657 wxListBox
*lbox
= (wxListBox
*)m_propertyWindow
;
658 if (lbox
->GetSelection() > -1)
659 property
->GetValue() = lbox
->GetStringSelection();
662 else if (m_propertyWindow->IsKindOf(CLASSINFO(wxRadioBox)))
664 wxRadioBox *rbox = (wxRadioBox *)m_propertyWindow;
666 if ((n = rbox->GetSelection()) > -1)
667 property->GetValue() = rbox->GetString(n);
670 else if (m_propertyWindow
->IsKindOf(CLASSINFO(wxChoice
)))
672 wxChoice
*choice
= (wxChoice
*)m_propertyWindow
;
673 if (choice
->GetSelection() > -1)
674 property
->GetValue() = choice
->GetStringSelection();
681 bool wxStringFormValidator::OnDisplayValue(wxProperty
*property
, wxPropertyFormView
*WXUNUSED(view
),
682 wxWindow
*WXUNUSED(parentWindow
) )
684 // The item used for viewing the string: should be a text item, choice item or listbox.
685 wxWindow
*m_propertyWindow
= property
->GetWindow();
686 if (!m_propertyWindow
)
688 if (m_propertyWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)))
690 wxTextCtrl
*text
= (wxTextCtrl
*)m_propertyWindow
;
691 text
->SetValue(property
->GetValue().StringValue());
693 else if (m_propertyWindow
->IsKindOf(CLASSINFO(wxListBox
)))
695 wxListBox
*lbox
= (wxListBox
*)m_propertyWindow
;
696 if (lbox
->Number() == 0 && m_strings
)
698 // Try to initialize the listbox from 'strings'
699 wxNode
*node
= m_strings
->First();
702 char *s
= (char *)node
->Data();
707 lbox
->SetStringSelection(property
->GetValue().StringValue());
710 else if (m_propertyWindow->IsKindOf(CLASSINFO(wxRadioBox)))
712 wxRadioBox *rbox = (wxRadioBox *)m_propertyWindow;
713 rbox->SetStringSelection(property->GetValue().StringValue());
716 else if (m_propertyWindow
->IsKindOf(CLASSINFO(wxChoice
)))
718 wxChoice
*choice
= (wxChoice
*)m_propertyWindow
;
720 if (choice
->Number() == 0 && m_strings
)
722 // Try to initialize the choice item from 'strings'
723 // XView doesn't allow this kind of thing.
724 wxNode
*node
= m_strings
->First();
727 char *s
= (char *)node
->Data();
733 choice
->SetStringSelection(property
->GetValue().StringValue());