]>
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"
33 #if defined(__WXMSW__) && !defined(__GNUWIN32__)
36 #include <strstream.h>
42 #include "wx/window.h"
45 #include "wx/propform.h"
51 IMPLEMENT_DYNAMIC_CLASS(wxPropertyFormView
, wxPropertyView
)
53 BEGIN_EVENT_TABLE(wxPropertyFormView
, wxPropertyView
)
54 EVT_BUTTON(wxID_OK
, wxPropertyFormView::OnOk
)
55 EVT_BUTTON(wxID_CANCEL
, wxPropertyFormView::OnCancel
)
56 EVT_BUTTON(wxID_HELP
, wxPropertyFormView::OnHelp
)
57 EVT_BUTTON(wxID_PROP_REVERT
, wxPropertyFormView::OnRevert
)
58 EVT_BUTTON(wxID_PROP_UPDATE
, wxPropertyFormView::OnUpdate
)
61 bool wxPropertyFormView::sm_dialogCancelled
= FALSE
;
63 wxPropertyFormView::wxPropertyFormView(wxWindow
*propPanel
, long flags
):wxPropertyView(flags
)
65 m_propertyWindow
= propPanel
;
66 m_managedWindow
= NULL
;
68 m_windowCloseButton
= NULL
;
69 m_windowCancelButton
= NULL
;
70 m_windowHelpButton
= NULL
;
72 m_detailedEditing
= FALSE
;
75 wxPropertyFormView::~wxPropertyFormView(void)
79 void wxPropertyFormView::ShowView(wxPropertySheet
*ps
, wxWindow
*panel
)
83 AssociatePanel(panel
);
85 // UpdatePropertyList();
88 // Update this view of the viewed object, called e.g. by
90 bool wxPropertyFormView::OnUpdateView(void)
95 bool wxPropertyFormView::Check(void)
100 wxNode
*node
= m_propertySheet
->GetProperties().First();
103 wxProperty
*prop
= (wxProperty
*)node
->Data();
104 wxPropertyValidator
*validator
= FindPropertyValidator(prop
);
105 if (validator
&& validator
->IsKindOf(CLASSINFO(wxPropertyFormValidator
)))
107 wxPropertyFormValidator
*formValidator
= (wxPropertyFormValidator
*)validator
;
108 if (!formValidator
->OnCheckValue(prop
, this, m_propertyWindow
))
116 bool wxPropertyFormView::TransferToPropertySheet(void)
118 if (!m_propertySheet
)
121 wxNode
*node
= m_propertySheet
->GetProperties().First();
124 wxProperty
*prop
= (wxProperty
*)node
->Data();
125 wxPropertyValidator
*validator
= FindPropertyValidator(prop
);
126 if (validator
&& validator
->IsKindOf(CLASSINFO(wxPropertyFormValidator
)))
128 wxPropertyFormValidator
*formValidator
= (wxPropertyFormValidator
*)validator
;
129 formValidator
->OnRetrieveValue(prop
, this, m_propertyWindow
);
136 bool wxPropertyFormView::TransferToDialog(void)
138 if (!m_propertySheet
)
141 wxNode
*node
= m_propertySheet
->GetProperties().First();
144 wxProperty
*prop
= (wxProperty
*)node
->Data();
145 wxPropertyValidator
*validator
= FindPropertyValidator(prop
);
146 if (validator
&& validator
->IsKindOf(CLASSINFO(wxPropertyFormValidator
)))
148 wxPropertyFormValidator
*formValidator
= (wxPropertyFormValidator
*)validator
;
149 formValidator
->OnDisplayValue(prop
, this, m_propertyWindow
);
156 bool wxPropertyFormView::AssociateNames(void)
158 if (!m_propertySheet
|| !m_propertyWindow
)
161 wxNode
*node
= m_propertyWindow
->GetChildren().First();
164 wxWindow
*win
= (wxWindow
*)node
->Data();
165 if (win
->GetName() != "")
167 wxProperty
*prop
= m_propertySheet
->GetProperty(win
->GetName());
169 prop
->SetWindow(win
);
177 bool wxPropertyFormView::OnClose(void)
183 void wxPropertyFormView::OnOk(wxCommandEvent
& WXUNUSED(event
))
185 // Retrieve the value if any
189 sm_dialogCancelled
= FALSE
;
190 TransferToPropertySheet();
192 m_managedWindow
->Close(TRUE
);
195 void wxPropertyFormView::OnCancel(wxCommandEvent
& WXUNUSED(event
))
197 sm_dialogCancelled
= TRUE
;
199 m_managedWindow
->Close(TRUE
);
202 void wxPropertyFormView::OnHelp(wxCommandEvent
& WXUNUSED(event
))
206 void wxPropertyFormView::OnUpdate(wxCommandEvent
& WXUNUSED(event
))
209 TransferToPropertySheet();
212 void wxPropertyFormView::OnRevert(wxCommandEvent
& WXUNUSED(event
))
217 void wxPropertyFormView::OnCommand(wxWindow
& win
, wxCommandEvent
& event
)
219 if (!m_propertySheet
)
222 if (win
.GetName() == "")
225 if (strcmp(win
.GetName(), "ok") == 0)
227 else if (strcmp(win
.GetName(), "cancel") == 0)
229 else if (strcmp(win
.GetName(), "help") == 0)
231 else if (strcmp(win
.GetName(), "update") == 0)
233 else if (strcmp(win
.GetName(), "revert") == 0)
237 // Find a validator to route the command to.
238 wxNode
*node
= m_propertySheet
->GetProperties().First();
241 wxProperty
*prop
= (wxProperty
*)node
->Data();
242 if (prop
->GetWindow() && (prop
->GetWindow() == &win
))
244 wxPropertyValidator
*validator
= FindPropertyValidator(prop
);
245 if (validator
&& validator
->IsKindOf(CLASSINFO(wxPropertyFormValidator
)))
247 wxPropertyFormValidator
*formValidator
= (wxPropertyFormValidator
*)validator
;
248 formValidator
->OnCommand(prop
, this, m_propertyWindow
, event
);
257 void wxPropertyFormView::OnDoubleClick(wxControl
*item
)
259 if (!m_propertySheet
)
262 // Find a validator to route the command to.
263 wxNode
*node
= m_propertySheet
->GetProperties().First();
266 wxProperty
*prop
= (wxProperty
*)node
->Data();
267 if (prop
->GetWindow() && ((wxControl
*)prop
->GetWindow() == item
))
269 wxPropertyValidator
*validator
= FindPropertyValidator(prop
);
270 if (validator
&& validator
->IsKindOf(CLASSINFO(wxPropertyFormValidator
)))
272 wxPropertyFormValidator
*formValidator
= (wxPropertyFormValidator
*)validator
;
273 formValidator
->OnDoubleClick(prop
, this, m_propertyWindow
);
282 * Property form dialog box
285 IMPLEMENT_CLASS(wxPropertyFormDialog
, wxDialog
)
287 wxPropertyFormDialog::wxPropertyFormDialog(wxPropertyFormView
*v
, wxWindow
*parent
, const wxString
& title
,
288 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
):
289 wxDialog(parent
, -1, title
, pos
, size
, style
, name
)
292 m_view
->AssociatePanel(this);
293 m_view
->SetManagedWindow(this);
294 // SetAutoLayout(TRUE);
297 bool wxPropertyFormDialog::OnClose(void)
309 void wxPropertyFormDialog::OnDefaultAction(wxControl
*item
)
311 m_view
->OnDoubleClick(item
);
314 void wxPropertyFormDialog::OnCommand(wxWindow
& win
, wxCommandEvent
& event
)
317 m_view
->OnCommand(win
, event
);
320 // Extend event processing to search the view's event table
321 bool wxPropertyFormDialog::ProcessEvent(wxEvent
& event
)
323 if ( !m_view
|| ! m_view
->ProcessEvent(event
) )
324 return wxEvtHandler::ProcessEvent(event
);
331 * Property form panel
334 IMPLEMENT_CLASS(wxPropertyFormPanel
, wxPanel
)
336 void wxPropertyFormPanel::OnDefaultAction(wxControl
*item
)
338 m_view
->OnDoubleClick(item
);
341 void wxPropertyFormPanel::OnCommand(wxWindow
& win
, wxCommandEvent
& event
)
343 m_view
->OnCommand(win
, event
);
346 // Extend event processing to search the view's event table
347 bool wxPropertyFormPanel::ProcessEvent(wxEvent
& event
)
349 if ( !m_view
|| ! m_view
->ProcessEvent(event
) )
350 return wxEvtHandler::ProcessEvent(event
);
359 IMPLEMENT_CLASS(wxPropertyFormFrame
, wxFrame
)
361 bool wxPropertyFormFrame::OnClose(void)
364 return m_view
->OnClose();
369 wxPanel
*wxPropertyFormFrame::OnCreatePanel(wxFrame
*parent
, wxPropertyFormView
*v
)
371 return new wxPropertyFormPanel(v
, parent
);
374 bool wxPropertyFormFrame::Initialize(void)
376 m_propertyPanel
= OnCreatePanel(this, m_view
);
379 m_view
->AssociatePanel(m_propertyPanel
);
380 m_view
->SetManagedWindow(this);
388 * Property form specific validator
391 IMPLEMENT_ABSTRACT_CLASS(wxPropertyFormValidator
, wxPropertyValidator
)
398 IMPLEMENT_DYNAMIC_CLASS(wxRealFormValidator
, wxPropertyFormValidator
)
401 /// Real number form validator
403 bool wxRealFormValidator::OnCheckValue( wxProperty
*property
, wxPropertyFormView
*WXUNUSED(view
),
404 wxWindow
*parentWindow
)
406 if (m_realMin
== 0.0 && m_realMax
== 0.0)
409 // The item used for viewing the real number: should be a text item.
410 wxWindow
*m_propertyWindow
= property
->GetWindow();
411 if (!m_propertyWindow
|| !m_propertyWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)))
414 wxString
value(((wxTextCtrl
*)m_propertyWindow
)->GetValue());
417 if (!StringToFloat(WXSTRINGCAST value
, &val
))
420 sprintf(buf
, "Value %s is not a valid real number!", (const char *)value
);
421 wxMessageBox(buf
, "Property value error", wxOK
| wxICON_EXCLAMATION
, parentWindow
);
425 if (val
< m_realMin
|| val
> m_realMax
)
428 sprintf(buf
, "Value must be a real number between %.2f and %.2f!", m_realMin
, m_realMax
);
429 wxMessageBox(buf
, "Property value error", wxOK
| wxICON_EXCLAMATION
, parentWindow
);
435 bool wxRealFormValidator::OnRetrieveValue(wxProperty
*property
, wxPropertyFormView
*WXUNUSED(view
),
436 wxWindow
*WXUNUSED(parentWindow
) )
438 // The item used for viewing the real number: should be a text item.
439 wxWindow
*m_propertyWindow
= property
->GetWindow();
440 if (!m_propertyWindow
|| !m_propertyWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)))
443 wxString
value(((wxTextCtrl
*)m_propertyWindow
)->GetValue());
445 if (value
.Length() == 0)
448 float f
= (float)atof((const char *)value
);
449 property
->GetValue() = f
;
453 bool wxRealFormValidator::OnDisplayValue(wxProperty
*property
, wxPropertyFormView
*WXUNUSED(view
),
454 wxWindow
*WXUNUSED(parentWindow
) )
456 // The item used for viewing the real number: should be a text item.
457 wxWindow
*m_propertyWindow
= property
->GetWindow();
458 if (!m_propertyWindow
|| !m_propertyWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)))
461 wxTextCtrl
*textItem
= (wxTextCtrl
*)m_propertyWindow
;
462 textItem
->SetValue(FloatToString(property
->GetValue().RealValue()));
467 /// Integer validator
469 IMPLEMENT_DYNAMIC_CLASS(wxIntegerFormValidator
, wxPropertyFormValidator
)
471 bool wxIntegerFormValidator::OnCheckValue(wxProperty
*property
, wxPropertyFormView
*WXUNUSED(view
),
472 wxWindow
*parentWindow
)
474 if (m_integerMin
== 0.0 && m_integerMax
== 0.0)
477 // The item used for viewing the real number: should be a text item or a slider
478 wxWindow
*m_propertyWindow
= property
->GetWindow();
479 if (!m_propertyWindow
)
484 if (m_propertyWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)))
486 wxString
value(((wxTextCtrl
*)m_propertyWindow
)->GetValue());
488 if (!StringToLong(WXSTRINGCAST value
, &val
))
491 sprintf(buf
, "Value %s is not a valid integer!", (const char *)value
);
492 wxMessageBox(buf
, "Property value error", wxOK
| wxICON_EXCLAMATION
, parentWindow
);
496 else if (m_propertyWindow
->IsKindOf(CLASSINFO(wxSlider
)))
498 val
= (long)((wxSlider
*)m_propertyWindow
)->GetValue();
503 if (val
< m_integerMin
|| val
> m_integerMax
)
506 sprintf(buf
, "Value must be an integer between %ld and %ld!", m_integerMin
, m_integerMax
);
507 wxMessageBox(buf
, "Property value error", wxOK
| wxICON_EXCLAMATION
, parentWindow
);
513 bool wxIntegerFormValidator::OnRetrieveValue(wxProperty
*property
, wxPropertyFormView
*WXUNUSED(view
),
514 wxWindow
*WXUNUSED(parentWindow
))
516 // The item used for viewing the real number: should be a text item or a slider
517 wxWindow
*m_propertyWindow
= property
->GetWindow();
518 if (!m_propertyWindow
)
521 if (m_propertyWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)))
523 wxString
value(((wxTextCtrl
*)m_propertyWindow
)->GetValue());
525 if (value
.Length() == 0)
528 long i
= atol((const char *)value
);
529 property
->GetValue() = i
;
531 else if (m_propertyWindow
->IsKindOf(CLASSINFO(wxSlider
)))
533 property
->GetValue() = (long)((wxSlider
*)m_propertyWindow
)->GetValue();
541 bool wxIntegerFormValidator::OnDisplayValue( wxProperty
*property
, wxPropertyFormView
*WXUNUSED(view
),
542 wxWindow
*WXUNUSED(parentWindow
))
544 // The item used for viewing the real number: should be a text item or a slider
545 wxWindow
*m_propertyWindow
= property
->GetWindow();
546 if (!m_propertyWindow
)
549 if (m_propertyWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)))
551 wxTextCtrl
*textItem
= (wxTextCtrl
*)m_propertyWindow
;
552 textItem
->SetValue(LongToString(property
->GetValue().IntegerValue()));
554 else if (m_propertyWindow
->IsKindOf(CLASSINFO(wxSlider
)))
556 ((wxSlider
*)m_propertyWindow
)->SetValue((int)property
->GetValue().IntegerValue());
564 /// Boolean validator
566 IMPLEMENT_DYNAMIC_CLASS(wxBoolFormValidator
, wxPropertyFormValidator
)
568 bool wxBoolFormValidator::OnCheckValue(wxProperty
*property
, wxPropertyFormView
*WXUNUSED(view
),
569 wxWindow
*WXUNUSED(parentWindow
))
571 // The item used for viewing the boolean: should be a checkbox
572 wxWindow
*m_propertyWindow
= property
->GetWindow();
573 if (!m_propertyWindow
|| !m_propertyWindow
->IsKindOf(CLASSINFO(wxCheckBox
)))
579 bool wxBoolFormValidator::OnRetrieveValue(wxProperty
*property
, wxPropertyFormView
*WXUNUSED(view
),
580 wxWindow
*WXUNUSED(parentWindow
) )
582 // The item used for viewing the boolean: should be a checkbox.
583 wxWindow
*m_propertyWindow
= property
->GetWindow();
584 if (!m_propertyWindow
|| !m_propertyWindow
->IsKindOf(CLASSINFO(wxCheckBox
)))
587 wxCheckBox
*checkBox
= (wxCheckBox
*)m_propertyWindow
;
589 property
->GetValue() = (bool)checkBox
->GetValue();
593 bool wxBoolFormValidator::OnDisplayValue(wxProperty
*property
, wxPropertyFormView
*WXUNUSED(view
),
594 wxWindow
*WXUNUSED(parentWindow
))
596 // The item used for viewing the boolean: should be a checkbox.
597 wxWindow
*m_propertyWindow
= property
->GetWindow();
598 if (!m_propertyWindow
|| !m_propertyWindow
->IsKindOf(CLASSINFO(wxCheckBox
)))
601 wxCheckBox
*checkBox
= (wxCheckBox
*)m_propertyWindow
;
602 checkBox
->SetValue((bool)property
->GetValue().BoolValue());
609 IMPLEMENT_DYNAMIC_CLASS(wxStringFormValidator
, wxPropertyFormValidator
)
611 wxStringFormValidator::wxStringFormValidator(wxStringList
*list
, long flags
):
612 wxPropertyFormValidator(flags
)
617 bool wxStringFormValidator::OnCheckValue(wxProperty
*property
, wxPropertyFormView
*WXUNUSED(view
),
618 wxWindow
*parentWindow
)
623 // The item used for viewing the string: should be a text item, choice item or listbox.
624 wxWindow
*m_propertyWindow
= property
->GetWindow();
625 if (!m_propertyWindow
)
627 if (m_propertyWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)))
629 wxTextCtrl
*text
= (wxTextCtrl
*)m_propertyWindow
;
630 if (!m_strings
->Member(text
->GetValue()))
632 wxString
s("Value ");
633 s
+= text
->GetValue();
634 s
+= " is not valid.";
635 wxMessageBox(s
, "Property value error", wxOK
| wxICON_EXCLAMATION
, parentWindow
);
641 // Any other item constrains the string value,
642 // so we don't have to check it.
647 bool wxStringFormValidator::OnRetrieveValue(wxProperty
*property
, wxPropertyFormView
*WXUNUSED(view
),
648 wxWindow
*WXUNUSED(parentWindow
) )
650 // The item used for viewing the string: should be a text item, choice item or listbox.
651 wxWindow
*m_propertyWindow
= property
->GetWindow();
652 if (!m_propertyWindow
)
654 if (m_propertyWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)))
656 wxTextCtrl
*text
= (wxTextCtrl
*)m_propertyWindow
;
657 property
->GetValue() = text
->GetValue();
659 else if (m_propertyWindow
->IsKindOf(CLASSINFO(wxListBox
)))
661 wxListBox
*lbox
= (wxListBox
*)m_propertyWindow
;
662 if (lbox
->GetSelection() > -1)
663 property
->GetValue() = lbox
->GetStringSelection();
666 else if (m_propertyWindow->IsKindOf(CLASSINFO(wxRadioBox)))
668 wxRadioBox *rbox = (wxRadioBox *)m_propertyWindow;
670 if ((n = rbox->GetSelection()) > -1)
671 property->GetValue() = rbox->GetString(n);
674 else if (m_propertyWindow
->IsKindOf(CLASSINFO(wxChoice
)))
676 wxChoice
*choice
= (wxChoice
*)m_propertyWindow
;
677 if (choice
->GetSelection() > -1)
678 property
->GetValue() = choice
->GetStringSelection();
685 bool wxStringFormValidator::OnDisplayValue(wxProperty
*property
, wxPropertyFormView
*WXUNUSED(view
),
686 wxWindow
*WXUNUSED(parentWindow
) )
688 // The item used for viewing the string: should be a text item, choice item or listbox.
689 wxWindow
*m_propertyWindow
= property
->GetWindow();
690 if (!m_propertyWindow
)
692 if (m_propertyWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)))
694 wxTextCtrl
*text
= (wxTextCtrl
*)m_propertyWindow
;
695 text
->SetValue(property
->GetValue().StringValue());
697 else if (m_propertyWindow
->IsKindOf(CLASSINFO(wxListBox
)))
699 wxListBox
*lbox
= (wxListBox
*)m_propertyWindow
;
700 if (lbox
->Number() == 0 && m_strings
)
702 // Try to initialize the listbox from 'strings'
703 wxNode
*node
= m_strings
->First();
706 char *s
= (char *)node
->Data();
711 lbox
->SetStringSelection(property
->GetValue().StringValue());
714 else if (m_propertyWindow->IsKindOf(CLASSINFO(wxRadioBox)))
716 wxRadioBox *rbox = (wxRadioBox *)m_propertyWindow;
717 rbox->SetStringSelection(property->GetValue().StringValue());
720 else if (m_propertyWindow
->IsKindOf(CLASSINFO(wxChoice
)))
722 wxChoice
*choice
= (wxChoice
*)m_propertyWindow
;
724 if (choice
->Number() == 0 && m_strings
)
726 // Try to initialize the choice item from 'strings'
727 // XView doesn't allow this kind of thing.
728 wxNode
*node
= m_strings
->First();
731 char *s
= (char *)node
->Data();
737 choice
->SetStringSelection(property
->GetValue().StringValue());