]>
git.saurik.com Git - wxWidgets.git/blob - utils/wxprop/src/propform.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Property form classes
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "propform.h"
16 // For compilers that support precompilation, includes "wx/wx.h".
17 #include "wx/wxprec.h"
32 #if defined(__WXMSW__) && !defined(__GNUWIN32__)
35 #include <strstream.h>
38 #include "wx/window.h"
47 IMPLEMENT_DYNAMIC_CLASS(wxPropertyFormView
, wxPropertyView
)
49 BEGIN_EVENT_TABLE(wxPropertyFormView
, wxPropertyView
)
50 EVT_BUTTON(wxID_OK
, wxPropertyFormView::OnOk
)
51 EVT_BUTTON(wxID_CANCEL
, wxPropertyFormView::OnCancel
)
52 EVT_BUTTON(wxID_HELP
, wxPropertyFormView::OnHelp
)
53 EVT_BUTTON(wxID_PROP_REVERT
, wxPropertyFormView::OnRevert
)
54 EVT_BUTTON(wxID_PROP_UPDATE
, wxPropertyFormView::OnUpdate
)
57 bool wxPropertyFormView::dialogCancelled
= FALSE
;
59 wxPropertyFormView::wxPropertyFormView(wxWindow
*propPanel
, long flags
):wxPropertyView(flags
)
61 propertyWindow
= propPanel
;
64 windowCloseButton
= NULL
;
65 windowCancelButton
= NULL
;
66 windowHelpButton
= NULL
;
68 detailedEditing
= FALSE
;
71 wxPropertyFormView::~wxPropertyFormView(void)
75 void wxPropertyFormView::ShowView(wxPropertySheet
*ps
, wxWindow
*panel
)
79 AssociatePanel(panel
);
81 // UpdatePropertyList();
84 // Update this view of the viewed object, called e.g. by
86 bool wxPropertyFormView::OnUpdateView(void)
91 bool wxPropertyFormView::Check(void)
96 wxNode
*node
= propertySheet
->GetProperties().First();
99 wxProperty
*prop
= (wxProperty
*)node
->Data();
100 wxPropertyValidator
*validator
= FindPropertyValidator(prop
);
101 if (validator
&& validator
->IsKindOf(CLASSINFO(wxPropertyFormValidator
)))
103 wxPropertyFormValidator
*formValidator
= (wxPropertyFormValidator
*)validator
;
104 if (!formValidator
->OnCheckValue(prop
, this, propertyWindow
))
112 bool wxPropertyFormView::TransferToPropertySheet(void)
117 wxNode
*node
= propertySheet
->GetProperties().First();
120 wxProperty
*prop
= (wxProperty
*)node
->Data();
121 wxPropertyValidator
*validator
= FindPropertyValidator(prop
);
122 if (validator
&& validator
->IsKindOf(CLASSINFO(wxPropertyFormValidator
)))
124 wxPropertyFormValidator
*formValidator
= (wxPropertyFormValidator
*)validator
;
125 formValidator
->OnRetrieveValue(prop
, this, propertyWindow
);
132 bool wxPropertyFormView::TransferToDialog(void)
137 wxNode
*node
= propertySheet
->GetProperties().First();
140 wxProperty
*prop
= (wxProperty
*)node
->Data();
141 wxPropertyValidator
*validator
= FindPropertyValidator(prop
);
142 if (validator
&& validator
->IsKindOf(CLASSINFO(wxPropertyFormValidator
)))
144 wxPropertyFormValidator
*formValidator
= (wxPropertyFormValidator
*)validator
;
145 formValidator
->OnDisplayValue(prop
, this, propertyWindow
);
152 bool wxPropertyFormView::AssociateNames(void)
154 if (!propertySheet
|| !propertyWindow
)
157 wxNode
*node
= propertyWindow
->GetChildren()->First();
160 wxWindow
*win
= (wxWindow
*)node
->Data();
161 if (win
->GetName() != "")
163 wxProperty
*prop
= propertySheet
->GetProperty(win
->GetName());
165 prop
->SetWindow(win
);
173 bool wxPropertyFormView::OnClose(void)
179 void wxPropertyFormView::OnOk(wxCommandEvent
& WXUNUSED(event
))
181 // Retrieve the value if any
185 dialogCancelled
= FALSE
;
187 managedWindow
->Close(TRUE
);
190 void wxPropertyFormView::OnCancel(wxCommandEvent
& WXUNUSED(event
))
192 dialogCancelled
= TRUE
;
194 managedWindow
->Close(TRUE
);
197 void wxPropertyFormView::OnHelp(wxCommandEvent
& WXUNUSED(event
))
201 void wxPropertyFormView::OnUpdate(wxCommandEvent
& WXUNUSED(event
))
203 TransferToPropertySheet();
206 void wxPropertyFormView::OnRevert(wxCommandEvent
& WXUNUSED(event
))
211 void wxPropertyFormView::OnCommand(wxWindow
& win
, wxCommandEvent
& event
)
216 if (win
.GetName() == "")
219 if (strcmp(win
.GetName(), "ok") == 0)
221 else if (strcmp(win
.GetName(), "cancel") == 0)
223 else if (strcmp(win
.GetName(), "help") == 0)
225 else if (strcmp(win
.GetName(), "update") == 0)
227 else if (strcmp(win
.GetName(), "revert") == 0)
231 // Find a validator to route the command to.
232 wxNode
*node
= propertySheet
->GetProperties().First();
235 wxProperty
*prop
= (wxProperty
*)node
->Data();
236 if (prop
->GetWindow() && (prop
->GetWindow() == &win
))
238 wxPropertyValidator
*validator
= FindPropertyValidator(prop
);
239 if (validator
&& validator
->IsKindOf(CLASSINFO(wxPropertyFormValidator
)))
241 wxPropertyFormValidator
*formValidator
= (wxPropertyFormValidator
*)validator
;
242 formValidator
->OnCommand(prop
, this, propertyWindow
, event
);
251 void wxPropertyFormView::OnDoubleClick(wxControl
*item
)
256 // Find a validator to route the command to.
257 wxNode
*node
= propertySheet
->GetProperties().First();
260 wxProperty
*prop
= (wxProperty
*)node
->Data();
261 if (prop
->GetWindow() && ((wxControl
*)prop
->GetWindow() == item
))
263 wxPropertyValidator
*validator
= FindPropertyValidator(prop
);
264 if (validator
&& validator
->IsKindOf(CLASSINFO(wxPropertyFormValidator
)))
266 wxPropertyFormValidator
*formValidator
= (wxPropertyFormValidator
*)validator
;
267 formValidator
->OnDoubleClick(prop
, this, propertyWindow
);
276 * Property form dialog box
279 IMPLEMENT_CLASS(wxPropertyFormDialog
, wxDialog
)
281 wxPropertyFormDialog::wxPropertyFormDialog(wxPropertyFormView
*v
, wxWindow
*parent
, const wxString
& title
,
282 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
):
283 wxDialog(parent
, -1, title
, pos
, size
, style
, name
)
286 view
->AssociatePanel(this);
287 view
->SetManagedWindow(this);
288 // SetAutoLayout(TRUE);
291 bool wxPropertyFormDialog::OnClose(void)
303 void wxPropertyFormDialog::OnDefaultAction(wxControl
*item
)
305 view
->OnDoubleClick(item
);
308 void wxPropertyFormDialog::OnCommand(wxWindow
& win
, wxCommandEvent
& event
)
311 view
->OnCommand(win
, event
);
314 // Extend event processing to search the view's event table
315 bool wxPropertyFormDialog::ProcessEvent(wxEvent
& event
)
317 if ( !view
|| ! view
->ProcessEvent(event
) )
318 return wxEvtHandler::ProcessEvent(event
);
325 * Property form panel
328 IMPLEMENT_CLASS(wxPropertyFormPanel
, wxPanel
)
330 void wxPropertyFormPanel::OnDefaultAction(wxControl
*item
)
332 view
->OnDoubleClick(item
);
335 void wxPropertyFormPanel::OnCommand(wxWindow
& win
, wxCommandEvent
& event
)
337 view
->OnCommand(win
, event
);
340 // Extend event processing to search the view's event table
341 bool wxPropertyFormPanel::ProcessEvent(wxEvent
& event
)
343 if ( !view
|| ! view
->ProcessEvent(event
) )
344 return wxEvtHandler::ProcessEvent(event
);
353 IMPLEMENT_CLASS(wxPropertyFormFrame
, wxFrame
)
355 bool wxPropertyFormFrame::OnClose(void)
358 return view
->OnClose();
363 wxPanel
*wxPropertyFormFrame::OnCreatePanel(wxFrame
*parent
, wxPropertyFormView
*v
)
365 return new wxPropertyFormPanel(v
, parent
);
368 bool wxPropertyFormFrame::Initialize(void)
370 propertyPanel
= OnCreatePanel(this, view
);
373 view
->AssociatePanel(propertyPanel
);
374 view
->SetManagedWindow(this);
375 // propertyPanel->SetAutoLayout(TRUE);
383 * Property form specific validator
386 IMPLEMENT_ABSTRACT_CLASS(wxPropertyFormValidator
, wxPropertyValidator
)
393 IMPLEMENT_DYNAMIC_CLASS(wxRealFormValidator
, wxPropertyFormValidator
)
396 /// Real number form validator
398 bool wxRealFormValidator::OnCheckValue( wxProperty
*property
, wxPropertyFormView
*WXUNUSED(view
),
399 wxWindow
*parentWindow
)
401 if (realMin
== 0.0 && realMax
== 0.0)
404 // The item used for viewing the real number: should be a text item.
405 wxWindow
*propertyWindow
= property
->GetWindow();
406 if (!propertyWindow
|| !propertyWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)))
409 wxString
value(((wxTextCtrl
*)propertyWindow
)->GetValue());
412 if (!StringToFloat(WXSTRINGCAST value
, &val
))
415 sprintf(buf
, "Value %s is not a valid real number!", (const char *)value
);
416 wxMessageBox(buf
, "Property value error", wxOK
| wxICON_EXCLAMATION
, parentWindow
);
420 if (val
< realMin
|| val
> realMax
)
423 sprintf(buf
, "Value must be a real number between %.2f and %.2f!", realMin
, realMax
);
424 wxMessageBox(buf
, "Property value error", wxOK
| wxICON_EXCLAMATION
, parentWindow
);
430 bool wxRealFormValidator::OnRetrieveValue(wxProperty
*property
, wxPropertyFormView
*WXUNUSED(view
),
431 wxWindow
*WXUNUSED(parentWindow
) )
433 // The item used for viewing the real number: should be a text item.
434 wxWindow
*propertyWindow
= property
->GetWindow();
435 if (!propertyWindow
|| !propertyWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)))
438 wxString
value(((wxTextCtrl
*)propertyWindow
)->GetValue());
440 if (value
.Length() == 0)
443 float f
= (float)atof((const char *)value
);
444 property
->GetValue() = f
;
448 bool wxRealFormValidator::OnDisplayValue(wxProperty
*property
, wxPropertyFormView
*WXUNUSED(view
),
449 wxWindow
*WXUNUSED(parentWindow
) )
451 // The item used for viewing the real number: should be a text item.
452 wxWindow
*propertyWindow
= property
->GetWindow();
453 if (!propertyWindow
|| !propertyWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)))
456 wxTextCtrl
*textItem
= (wxTextCtrl
*)propertyWindow
;
457 textItem
->SetValue(FloatToString(property
->GetValue().RealValue()));
462 /// Integer validator
464 IMPLEMENT_DYNAMIC_CLASS(wxIntegerFormValidator
, wxPropertyFormValidator
)
466 bool wxIntegerFormValidator::OnCheckValue(wxProperty
*property
, wxPropertyFormView
*WXUNUSED(view
),
467 wxWindow
*parentWindow
)
469 if (integerMin
== 0.0 && integerMax
== 0.0)
472 // The item used for viewing the real number: should be a text item or a slider
473 wxWindow
*propertyWindow
= property
->GetWindow();
479 if (propertyWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)))
481 wxString
value(((wxTextCtrl
*)propertyWindow
)->GetValue());
483 if (!StringToLong(WXSTRINGCAST value
, &val
))
486 sprintf(buf
, "Value %s is not a valid integer!", (const char *)value
);
487 wxMessageBox(buf
, "Property value error", wxOK
| wxICON_EXCLAMATION
, parentWindow
);
491 else if (propertyWindow
->IsKindOf(CLASSINFO(wxSlider
)))
493 val
= (long)((wxSlider
*)propertyWindow
)->GetValue();
498 if (val
< integerMin
|| val
> integerMax
)
501 sprintf(buf
, "Value must be an integer between %ld and %ld!", integerMin
, integerMax
);
502 wxMessageBox(buf
, "Property value error", wxOK
| wxICON_EXCLAMATION
, parentWindow
);
508 bool wxIntegerFormValidator::OnRetrieveValue(wxProperty
*property
, wxPropertyFormView
*WXUNUSED(view
),
509 wxWindow
*WXUNUSED(parentWindow
))
511 // The item used for viewing the real number: should be a text item or a slider
512 wxWindow
*propertyWindow
= property
->GetWindow();
516 if (propertyWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)))
518 wxString
value(((wxTextCtrl
*)propertyWindow
)->GetValue());
520 if (value
.Length() == 0)
523 long i
= atol((const char *)value
);
524 property
->GetValue() = i
;
526 else if (propertyWindow
->IsKindOf(CLASSINFO(wxSlider
)))
528 property
->GetValue() = (long)((wxSlider
*)propertyWindow
)->GetValue();
536 bool wxIntegerFormValidator::OnDisplayValue( wxProperty
*property
, wxPropertyFormView
*WXUNUSED(view
),
537 wxWindow
*WXUNUSED(parentWindow
))
539 // The item used for viewing the real number: should be a text item or a slider
540 wxWindow
*propertyWindow
= property
->GetWindow();
544 if (propertyWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)))
546 wxTextCtrl
*textItem
= (wxTextCtrl
*)propertyWindow
;
547 textItem
->SetValue(LongToString(property
->GetValue().IntegerValue()));
549 else if (propertyWindow
->IsKindOf(CLASSINFO(wxSlider
)))
551 ((wxSlider
*)propertyWindow
)->SetValue((int)property
->GetValue().IntegerValue());
559 /// Boolean validator
561 IMPLEMENT_DYNAMIC_CLASS(wxBoolFormValidator
, wxPropertyFormValidator
)
563 bool wxBoolFormValidator::OnCheckValue(wxProperty
*property
, wxPropertyFormView
*WXUNUSED(view
),
564 wxWindow
*WXUNUSED(parentWindow
))
566 // The item used for viewing the boolean: should be a checkbox
567 wxWindow
*propertyWindow
= property
->GetWindow();
568 if (!propertyWindow
|| !propertyWindow
->IsKindOf(CLASSINFO(wxCheckBox
)))
574 bool wxBoolFormValidator::OnRetrieveValue(wxProperty
*property
, wxPropertyFormView
*WXUNUSED(view
),
575 wxWindow
*WXUNUSED(parentWindow
) )
577 // The item used for viewing the boolean: should be a checkbox.
578 wxWindow
*propertyWindow
= property
->GetWindow();
579 if (!propertyWindow
|| !propertyWindow
->IsKindOf(CLASSINFO(wxCheckBox
)))
582 wxCheckBox
*checkBox
= (wxCheckBox
*)propertyWindow
;
584 property
->GetValue() = (bool)checkBox
->GetValue();
588 bool wxBoolFormValidator::OnDisplayValue(wxProperty
*property
, wxPropertyFormView
*WXUNUSED(view
),
589 wxWindow
*WXUNUSED(parentWindow
))
591 // The item used for viewing the boolean: should be a checkbox.
592 wxWindow
*propertyWindow
= property
->GetWindow();
593 if (!propertyWindow
|| !propertyWindow
->IsKindOf(CLASSINFO(wxCheckBox
)))
596 wxCheckBox
*checkBox
= (wxCheckBox
*)propertyWindow
;
597 checkBox
->SetValue((bool)property
->GetValue().BoolValue());
604 IMPLEMENT_DYNAMIC_CLASS(wxStringFormValidator
, wxPropertyFormValidator
)
606 wxStringFormValidator::wxStringFormValidator(wxStringList
*list
, long flags
):
607 wxPropertyFormValidator(flags
)
612 bool wxStringFormValidator::OnCheckValue(wxProperty
*property
, wxPropertyFormView
*WXUNUSED(view
),
613 wxWindow
*parentWindow
)
618 // The item used for viewing the string: should be a text item, choice item or listbox.
619 wxWindow
*propertyWindow
= property
->GetWindow();
622 if (propertyWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)))
624 wxTextCtrl
*text
= (wxTextCtrl
*)propertyWindow
;
625 if (!strings
->Member(text
->GetValue()))
627 wxString
s("Value ");
628 s
+= text
->GetValue();
629 s
+= " is not valid.";
630 wxMessageBox(s
, "Property value error", wxOK
| wxICON_EXCLAMATION
, parentWindow
);
636 // Any other item constrains the string value,
637 // so we don't have to check it.
642 bool wxStringFormValidator::OnRetrieveValue(wxProperty
*property
, wxPropertyFormView
*WXUNUSED(view
),
643 wxWindow
*WXUNUSED(parentWindow
) )
645 // The item used for viewing the string: should be a text item, choice item or listbox.
646 wxWindow
*propertyWindow
= property
->GetWindow();
649 if (propertyWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)))
651 wxTextCtrl
*text
= (wxTextCtrl
*)propertyWindow
;
652 property
->GetValue() = text
->GetValue();
654 else if (propertyWindow
->IsKindOf(CLASSINFO(wxListBox
)))
656 wxListBox
*lbox
= (wxListBox
*)propertyWindow
;
657 if (lbox
->GetSelection() > -1)
658 property
->GetValue() = lbox
->GetStringSelection();
661 else if (propertyWindow->IsKindOf(CLASSINFO(wxRadioBox)))
663 wxRadioBox *rbox = (wxRadioBox *)propertyWindow;
665 if ((n = rbox->GetSelection()) > -1)
666 property->GetValue() = rbox->GetString(n);
669 else if (propertyWindow
->IsKindOf(CLASSINFO(wxChoice
)))
671 wxChoice
*choice
= (wxChoice
*)propertyWindow
;
672 if (choice
->GetSelection() > -1)
673 property
->GetValue() = choice
->GetStringSelection();
680 bool wxStringFormValidator::OnDisplayValue(wxProperty
*property
, wxPropertyFormView
*WXUNUSED(view
),
681 wxWindow
*WXUNUSED(parentWindow
) )
683 // The item used for viewing the string: should be a text item, choice item or listbox.
684 wxWindow
*propertyWindow
= property
->GetWindow();
687 if (propertyWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)))
689 wxTextCtrl
*text
= (wxTextCtrl
*)propertyWindow
;
690 text
->SetValue(property
->GetValue().StringValue());
692 else if (propertyWindow
->IsKindOf(CLASSINFO(wxListBox
)))
694 wxListBox
*lbox
= (wxListBox
*)propertyWindow
;
695 if (lbox
->Number() == 0 && strings
)
697 // Try to initialize the listbox from 'strings'
698 wxNode
*node
= strings
->First();
701 char *s
= (char *)node
->Data();
706 lbox
->SetStringSelection(property
->GetValue().StringValue());
709 else if (propertyWindow->IsKindOf(CLASSINFO(wxRadioBox)))
711 wxRadioBox *rbox = (wxRadioBox *)propertyWindow;
712 rbox->SetStringSelection(property->GetValue().StringValue());
715 else if (propertyWindow
->IsKindOf(CLASSINFO(wxChoice
)))
717 wxChoice
*choice
= (wxChoice
*)propertyWindow
;
719 if (choice
->Number() == 0 && strings
)
721 // Try to initialize the choice item from 'strings'
722 // XView doesn't allow this kind of thing.
723 wxNode
*node
= strings
->First();
726 char *s
= (char *)node
->Data();
732 choice
->SetStringSelection(property
->GetValue().StringValue());