]>
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"
28 #include "wx/propform.h"
40 IMPLEMENT_DYNAMIC_CLASS(wxPropertyFormView
, wxPropertyView
)
42 BEGIN_EVENT_TABLE(wxPropertyFormView
, wxPropertyView
)
43 EVT_BUTTON(wxID_OK
, wxPropertyFormView::OnOk
)
44 EVT_BUTTON(wxID_CANCEL
, wxPropertyFormView::OnCancel
)
45 EVT_BUTTON(wxID_HELP
, wxPropertyFormView::OnHelp
)
46 EVT_BUTTON(wxID_PROP_REVERT
, wxPropertyFormView::OnRevert
)
47 EVT_BUTTON(wxID_PROP_UPDATE
, wxPropertyFormView::OnUpdate
)
50 bool wxPropertyFormView::sm_dialogCancelled
= FALSE
;
52 wxPropertyFormView::wxPropertyFormView(wxWindow
*propPanel
, long flags
):wxPropertyView(flags
)
54 m_propertyWindow
= propPanel
;
55 m_managedWindow
= NULL
;
57 m_windowCloseButton
= NULL
;
58 m_windowCancelButton
= NULL
;
59 m_windowHelpButton
= NULL
;
61 m_detailedEditing
= FALSE
;
64 wxPropertyFormView::~wxPropertyFormView(void)
68 void wxPropertyFormView::ShowView(wxPropertySheet
*ps
, wxWindow
*panel
)
72 AssociatePanel(panel
);
74 // UpdatePropertyList();
77 // Update this view of the viewed object, called e.g. by
79 bool wxPropertyFormView::OnUpdateView(void)
84 bool wxPropertyFormView::Check(void)
89 wxNode
*node
= m_propertySheet
->GetProperties().First();
92 wxProperty
*prop
= (wxProperty
*)node
->Data();
93 wxPropertyValidator
*validator
= FindPropertyValidator(prop
);
94 if (validator
&& validator
->IsKindOf(CLASSINFO(wxPropertyFormValidator
)))
96 wxPropertyFormValidator
*formValidator
= (wxPropertyFormValidator
*)validator
;
97 if (!formValidator
->OnCheckValue(prop
, this, m_propertyWindow
))
105 bool wxPropertyFormView::TransferToPropertySheet(void)
107 if (!m_propertySheet
)
110 wxNode
*node
= m_propertySheet
->GetProperties().First();
113 wxProperty
*prop
= (wxProperty
*)node
->Data();
114 wxPropertyValidator
*validator
= FindPropertyValidator(prop
);
115 if (validator
&& validator
->IsKindOf(CLASSINFO(wxPropertyFormValidator
)))
117 wxPropertyFormValidator
*formValidator
= (wxPropertyFormValidator
*)validator
;
118 formValidator
->OnRetrieveValue(prop
, this, m_propertyWindow
);
125 bool wxPropertyFormView::TransferToDialog(void)
127 if (!m_propertySheet
)
130 wxNode
*node
= m_propertySheet
->GetProperties().First();
133 wxProperty
*prop
= (wxProperty
*)node
->Data();
134 wxPropertyValidator
*validator
= FindPropertyValidator(prop
);
135 if (validator
&& validator
->IsKindOf(CLASSINFO(wxPropertyFormValidator
)))
137 wxPropertyFormValidator
*formValidator
= (wxPropertyFormValidator
*)validator
;
138 formValidator
->OnDisplayValue(prop
, this, m_propertyWindow
);
145 bool wxPropertyFormView::AssociateNames(void)
147 if (!m_propertySheet
|| !m_propertyWindow
)
150 wxNode
*node
= m_propertyWindow
->GetChildren().First();
153 wxWindow
*win
= (wxWindow
*)node
->Data();
154 if (win
->GetName() != wxT(""))
156 wxProperty
*prop
= m_propertySheet
->GetProperty(win
->GetName());
158 prop
->SetWindow(win
);
166 bool wxPropertyFormView::OnClose(void)
168 if (m_propertyWindow
->IsKindOf(CLASSINFO(wxPropertyFormPanel
)))
170 ((wxPropertyFormPanel
*)m_propertyWindow
)->SetView(NULL
);
176 void wxPropertyFormView::OnOk(wxCommandEvent
& WXUNUSED(event
))
178 // Retrieve the value if any
182 sm_dialogCancelled
= FALSE
;
183 TransferToPropertySheet();
185 m_managedWindow
->Close(TRUE
);
188 void wxPropertyFormView::OnCancel(wxCommandEvent
& WXUNUSED(event
))
190 sm_dialogCancelled
= TRUE
;
192 m_managedWindow
->Close(TRUE
);
195 void wxPropertyFormView::OnHelp(wxCommandEvent
& WXUNUSED(event
))
199 void wxPropertyFormView::OnUpdate(wxCommandEvent
& WXUNUSED(event
))
202 TransferToPropertySheet();
205 void wxPropertyFormView::OnRevert(wxCommandEvent
& WXUNUSED(event
))
210 void wxPropertyFormView::OnCommand(wxWindow
& win
, wxCommandEvent
& event
)
212 if (!m_propertySheet
)
215 if (win
.GetName() == wxT(""))
218 if (wxStrcmp(win
.GetName(), wxT("ok")) == 0)
220 else if (wxStrcmp(win
.GetName(), wxT("cancel")) == 0)
222 else if (wxStrcmp(win
.GetName(), wxT("help")) == 0)
224 else if (wxStrcmp(win
.GetName(), wxT("update")) == 0)
226 else if (wxStrcmp(win
.GetName(), wxT("revert")) == 0)
230 // Find a validator to route the command to.
231 wxNode
*node
= m_propertySheet
->GetProperties().First();
234 wxProperty
*prop
= (wxProperty
*)node
->Data();
235 if (prop
->GetWindow() && (prop
->GetWindow() == &win
))
237 wxPropertyValidator
*validator
= FindPropertyValidator(prop
);
238 if (validator
&& validator
->IsKindOf(CLASSINFO(wxPropertyFormValidator
)))
240 wxPropertyFormValidator
*formValidator
= (wxPropertyFormValidator
*)validator
;
241 formValidator
->OnCommand(prop
, this, m_propertyWindow
, event
);
250 // Extend event processing to call OnCommand
251 bool wxPropertyFormView::ProcessEvent(wxEvent
& event
)
253 if (wxEvtHandler::ProcessEvent(event
))
255 else if (event
.IsCommandEvent() && !event
.IsKindOf(CLASSINFO(wxUpdateUIEvent
)) && event
.GetEventObject())
257 OnCommand(* ((wxWindow
*) event
.GetEventObject()), (wxCommandEvent
&) event
);
264 void wxPropertyFormView::OnDoubleClick(wxControl
*item
)
266 if (!m_propertySheet
)
269 // Find a validator to route the command to.
270 wxNode
*node
= m_propertySheet
->GetProperties().First();
273 wxProperty
*prop
= (wxProperty
*)node
->Data();
274 if (prop
->GetWindow() && ((wxControl
*)prop
->GetWindow() == item
))
276 wxPropertyValidator
*validator
= FindPropertyValidator(prop
);
277 if (validator
&& validator
->IsKindOf(CLASSINFO(wxPropertyFormValidator
)))
279 wxPropertyFormValidator
*formValidator
= (wxPropertyFormValidator
*)validator
;
280 formValidator
->OnDoubleClick(prop
, this, m_propertyWindow
);
289 * Property form dialog box
292 IMPLEMENT_DYNAMIC_CLASS(wxPropertyFormDialog
, wxDialog
)
294 BEGIN_EVENT_TABLE(wxPropertyFormDialog
, wxDialog
)
295 EVT_CLOSE(wxPropertyFormDialog::OnCloseWindow
)
298 wxPropertyFormDialog::wxPropertyFormDialog(wxPropertyFormView
*v
, wxWindow
*parent
, const wxString
& title
,
299 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
):
300 wxDialog(parent
, -1, title
, pos
, size
, style
, name
)
303 m_view
->AssociatePanel(this);
304 m_view
->SetManagedWindow(this);
305 // SetAutoLayout(TRUE);
308 void wxPropertyFormDialog::OnCloseWindow(wxCloseEvent
& event
)
320 void wxPropertyFormDialog::OnDefaultAction(wxControl
*item
)
322 m_view
->OnDoubleClick(item
);
325 void wxPropertyFormDialog::OnCommand(wxWindow
& win
, wxCommandEvent
& event
)
328 m_view
->OnCommand(win
, event
);
331 // Extend event processing to search the view's event table
332 bool wxPropertyFormDialog::ProcessEvent(wxEvent
& event
)
334 if ( !m_view
|| ! m_view
->ProcessEvent(event
) )
335 return wxEvtHandler::ProcessEvent(event
);
342 * Property form panel
345 IMPLEMENT_DYNAMIC_CLASS(wxPropertyFormPanel
, wxPanel
)
347 void wxPropertyFormPanel::OnDefaultAction(wxControl
*item
)
349 m_view
->OnDoubleClick(item
);
352 void wxPropertyFormPanel::OnCommand(wxWindow
& win
, wxCommandEvent
& event
)
354 m_view
->OnCommand(win
, event
);
357 // Extend event processing to search the view's event table
358 bool wxPropertyFormPanel::ProcessEvent(wxEvent
& event
)
360 if ( !m_view
|| ! m_view
->ProcessEvent(event
) )
361 return wxEvtHandler::ProcessEvent(event
);
370 IMPLEMENT_DYNAMIC_CLASS(wxPropertyFormFrame
, wxFrame
)
372 BEGIN_EVENT_TABLE(wxPropertyFormFrame
, wxFrame
)
373 EVT_CLOSE(wxPropertyFormFrame::OnCloseWindow
)
376 void wxPropertyFormFrame::OnCloseWindow(wxCloseEvent
& event
)
378 if (m_view
&& m_view
->OnClose())
384 wxPanel
*wxPropertyFormFrame::OnCreatePanel(wxFrame
*parent
, wxPropertyFormView
*v
)
386 return new wxPropertyFormPanel(v
, parent
);
389 bool wxPropertyFormFrame::Initialize(void)
391 m_propertyPanel
= OnCreatePanel(this, m_view
);
394 m_view
->AssociatePanel(m_propertyPanel
);
395 m_view
->SetManagedWindow(this);
403 * Property form specific validator
406 IMPLEMENT_ABSTRACT_CLASS(wxPropertyFormValidator
, wxPropertyValidator
)
413 IMPLEMENT_DYNAMIC_CLASS(wxRealFormValidator
, wxPropertyFormValidator
)
416 /// Real number form validator
418 bool wxRealFormValidator::OnCheckValue( wxProperty
*property
, wxPropertyFormView
*WXUNUSED(view
),
419 wxWindow
*parentWindow
)
421 if (m_realMin
== 0.0 && m_realMax
== 0.0)
424 // The item used for viewing the real number: should be a text item.
425 wxWindow
*m_propertyWindow
= property
->GetWindow();
426 if (!m_propertyWindow
|| !m_propertyWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)))
429 wxString
value(((wxTextCtrl
*)m_propertyWindow
)->GetValue());
432 if (!StringToFloat(WXSTRINGCAST value
, &val
))
435 wxSprintf(buf
, wxT("Value %s is not a valid real number!"), (const wxChar
*)value
);
436 wxMessageBox(buf
, wxT("Property value error"), wxOK
| wxICON_EXCLAMATION
, parentWindow
);
440 if (val
< m_realMin
|| val
> m_realMax
)
443 wxSprintf(buf
, wxT("Value must be a real number between %.2f and %.2f!"), m_realMin
, m_realMax
);
444 wxMessageBox(buf
, wxT("Property value error"), wxOK
| wxICON_EXCLAMATION
, parentWindow
);
450 bool wxRealFormValidator::OnRetrieveValue(wxProperty
*property
, wxPropertyFormView
*WXUNUSED(view
),
451 wxWindow
*WXUNUSED(parentWindow
) )
453 // The item used for viewing the real number: should be a text item.
454 wxWindow
*m_propertyWindow
= property
->GetWindow();
455 if (!m_propertyWindow
|| !m_propertyWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)))
458 wxString
value(((wxTextCtrl
*)m_propertyWindow
)->GetValue());
460 if (value
.Length() == 0)
463 float f
= (float)wxAtof((const wxChar
*)value
);
464 property
->GetValue() = f
;
468 bool wxRealFormValidator::OnDisplayValue(wxProperty
*property
, wxPropertyFormView
*WXUNUSED(view
),
469 wxWindow
*WXUNUSED(parentWindow
) )
471 // The item used for viewing the real number: should be a text item.
472 wxWindow
*m_propertyWindow
= property
->GetWindow();
473 if (!m_propertyWindow
|| !m_propertyWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)))
476 wxTextCtrl
*textItem
= (wxTextCtrl
*)m_propertyWindow
;
477 textItem
->SetValue(FloatToString(property
->GetValue().RealValue()));
482 /// Integer validator
484 IMPLEMENT_DYNAMIC_CLASS(wxIntegerFormValidator
, wxPropertyFormValidator
)
486 bool wxIntegerFormValidator::OnCheckValue(wxProperty
*property
, wxPropertyFormView
*WXUNUSED(view
),
487 wxWindow
*parentWindow
)
489 if (m_integerMin
== 0.0 && m_integerMax
== 0.0)
492 // The item used for viewing the real number: should be a text item or a slider
493 wxWindow
*m_propertyWindow
= property
->GetWindow();
494 if (!m_propertyWindow
)
499 if (m_propertyWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)))
501 wxString
value(((wxTextCtrl
*)m_propertyWindow
)->GetValue());
503 if (!StringToLong(WXSTRINGCAST value
, &val
))
506 wxSprintf(buf
, wxT("Value %s is not a valid integer!"), (const wxChar
*)value
);
507 wxMessageBox(buf
, wxT("Property value error"), wxOK
| wxICON_EXCLAMATION
, parentWindow
);
511 else if (m_propertyWindow
->IsKindOf(CLASSINFO(wxSlider
)))
513 val
= (long)((wxSlider
*)m_propertyWindow
)->GetValue();
518 if (val
< m_integerMin
|| val
> m_integerMax
)
521 sprintf(buf
, "Value must be an integer between %ld and %ld!", m_integerMin
, m_integerMax
);
522 wxMessageBox(buf
, "Property value error", wxOK
| wxICON_EXCLAMATION
, parentWindow
);
528 bool wxIntegerFormValidator::OnRetrieveValue(wxProperty
*property
, wxPropertyFormView
*WXUNUSED(view
),
529 wxWindow
*WXUNUSED(parentWindow
))
531 // The item used for viewing the real number: should be a text item or a slider
532 wxWindow
*m_propertyWindow
= property
->GetWindow();
533 if (!m_propertyWindow
)
536 if (m_propertyWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)))
538 wxString
value(((wxTextCtrl
*)m_propertyWindow
)->GetValue());
540 if (value
.Length() == 0)
543 long i
= wxAtol((const wxChar
*)value
);
544 property
->GetValue() = i
;
546 else if (m_propertyWindow
->IsKindOf(CLASSINFO(wxSlider
)))
548 property
->GetValue() = (long)((wxSlider
*)m_propertyWindow
)->GetValue();
556 bool wxIntegerFormValidator::OnDisplayValue( wxProperty
*property
, wxPropertyFormView
*WXUNUSED(view
),
557 wxWindow
*WXUNUSED(parentWindow
))
559 // The item used for viewing the real number: should be a text item or a slider
560 wxWindow
*m_propertyWindow
= property
->GetWindow();
561 if (!m_propertyWindow
)
564 if (m_propertyWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)))
566 wxTextCtrl
*textItem
= (wxTextCtrl
*)m_propertyWindow
;
567 textItem
->SetValue(LongToString(property
->GetValue().IntegerValue()));
569 else if (m_propertyWindow
->IsKindOf(CLASSINFO(wxSlider
)))
571 ((wxSlider
*)m_propertyWindow
)->SetValue((int)property
->GetValue().IntegerValue());
579 /// Boolean validator
581 IMPLEMENT_DYNAMIC_CLASS(wxBoolFormValidator
, wxPropertyFormValidator
)
583 bool wxBoolFormValidator::OnCheckValue(wxProperty
*property
, wxPropertyFormView
*WXUNUSED(view
),
584 wxWindow
*WXUNUSED(parentWindow
))
586 // The item used for viewing the boolean: should be a checkbox
587 wxWindow
*m_propertyWindow
= property
->GetWindow();
588 if (!m_propertyWindow
|| !m_propertyWindow
->IsKindOf(CLASSINFO(wxCheckBox
)))
594 bool wxBoolFormValidator::OnRetrieveValue(wxProperty
*property
, wxPropertyFormView
*WXUNUSED(view
),
595 wxWindow
*WXUNUSED(parentWindow
) )
597 // The item used for viewing the boolean: should be a checkbox.
598 wxWindow
*m_propertyWindow
= property
->GetWindow();
599 if (!m_propertyWindow
|| !m_propertyWindow
->IsKindOf(CLASSINFO(wxCheckBox
)))
602 wxCheckBox
*checkBox
= (wxCheckBox
*)m_propertyWindow
;
604 property
->GetValue() = (bool)checkBox
->GetValue();
608 bool wxBoolFormValidator::OnDisplayValue(wxProperty
*property
, wxPropertyFormView
*WXUNUSED(view
),
609 wxWindow
*WXUNUSED(parentWindow
))
611 // The item used for viewing the boolean: should be a checkbox.
612 wxWindow
*m_propertyWindow
= property
->GetWindow();
613 if (!m_propertyWindow
|| !m_propertyWindow
->IsKindOf(CLASSINFO(wxCheckBox
)))
616 wxCheckBox
*checkBox
= (wxCheckBox
*)m_propertyWindow
;
617 checkBox
->SetValue((bool)property
->GetValue().BoolValue());
624 IMPLEMENT_DYNAMIC_CLASS(wxStringFormValidator
, wxPropertyFormValidator
)
626 wxStringFormValidator::wxStringFormValidator(wxStringList
*list
, long flags
):
627 wxPropertyFormValidator(flags
)
632 bool wxStringFormValidator::OnCheckValue(wxProperty
*property
, wxPropertyFormView
*WXUNUSED(view
),
633 wxWindow
*parentWindow
)
638 // The item used for viewing the string: should be a text item, choice item or listbox.
639 wxWindow
*m_propertyWindow
= property
->GetWindow();
640 if (!m_propertyWindow
)
642 if (m_propertyWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)))
644 wxTextCtrl
*text
= (wxTextCtrl
*)m_propertyWindow
;
645 if (!m_strings
->Member(text
->GetValue()))
647 wxString
s("Value ");
648 s
+= text
->GetValue();
649 s
+= " is not valid.";
650 wxMessageBox(s
, "Property value error", wxOK
| wxICON_EXCLAMATION
, parentWindow
);
656 // Any other item constrains the string value,
657 // so we don't have to check it.
662 bool wxStringFormValidator::OnRetrieveValue(wxProperty
*property
, wxPropertyFormView
*WXUNUSED(view
),
663 wxWindow
*WXUNUSED(parentWindow
) )
665 // The item used for viewing the string: should be a text item, choice item or listbox.
666 wxWindow
*m_propertyWindow
= property
->GetWindow();
667 if (!m_propertyWindow
)
669 if (m_propertyWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)))
671 wxTextCtrl
*text
= (wxTextCtrl
*)m_propertyWindow
;
672 property
->GetValue() = text
->GetValue();
674 else if (m_propertyWindow
->IsKindOf(CLASSINFO(wxListBox
)))
676 wxListBox
*lbox
= (wxListBox
*)m_propertyWindow
;
677 if (lbox
->GetSelection() > -1)
678 property
->GetValue() = lbox
->GetStringSelection();
681 else if (m_propertyWindow->IsKindOf(CLASSINFO(wxRadioBox)))
683 wxRadioBox *rbox = (wxRadioBox *)m_propertyWindow;
685 if ((n = rbox->GetSelection()) > -1)
686 property->GetValue() = rbox->GetString(n);
689 else if (m_propertyWindow
->IsKindOf(CLASSINFO(wxChoice
)))
691 wxChoice
*choice
= (wxChoice
*)m_propertyWindow
;
692 if (choice
->GetSelection() > -1)
693 property
->GetValue() = choice
->GetStringSelection();
700 bool wxStringFormValidator::OnDisplayValue(wxProperty
*property
, wxPropertyFormView
*WXUNUSED(view
),
701 wxWindow
*WXUNUSED(parentWindow
) )
703 // The item used for viewing the string: should be a text item, choice item or listbox.
704 wxWindow
*m_propertyWindow
= property
->GetWindow();
705 if (!m_propertyWindow
)
707 if (m_propertyWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)))
709 wxTextCtrl
*text
= (wxTextCtrl
*)m_propertyWindow
;
710 text
->SetValue(property
->GetValue().StringValue());
712 else if (m_propertyWindow
->IsKindOf(CLASSINFO(wxListBox
)))
714 wxListBox
*lbox
= (wxListBox
*)m_propertyWindow
;
715 if (lbox
->GetCount() == 0 && m_strings
)
717 // Try to initialize the listbox from 'strings'
718 wxNode
*node
= m_strings
->First();
721 char *s
= (char *)node
->Data();
726 lbox
->SetStringSelection(property
->GetValue().StringValue());
729 else if (m_propertyWindow->IsKindOf(CLASSINFO(wxRadioBox)))
731 wxRadioBox *rbox = (wxRadioBox *)m_propertyWindow;
732 rbox->SetStringSelection(property->GetValue().StringValue());
735 else if (m_propertyWindow
->IsKindOf(CLASSINFO(wxChoice
)))
737 wxChoice
*choice
= (wxChoice
*)m_propertyWindow
;
739 if (choice
->GetCount() == 0 && m_strings
)
741 // Try to initialize the choice item from 'strings'
742 // XView doesn't allow this kind of thing.
743 wxNode
*node
= m_strings
->First();
746 char *s
= (char *)node
->Data();
752 choice
->SetStringSelection(property
->GetValue().StringValue());
759 #endif // wxUSE_PROPSHEET