]>
git.saurik.com Git - wxWidgets.git/blob - utils/wxprop/src/propform.cpp
ac5aed868fe0f4a304debe23087b1e13928a6d6e
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
& event
)
181 // Retrieve the value if any
185 dialogCancelled
= FALSE
;
187 managedWindow
->Close(TRUE
);
190 void wxPropertyFormView::OnCancel(wxCommandEvent
& event
)
192 dialogCancelled
= TRUE
;
194 managedWindow
->Close(TRUE
);
197 void wxPropertyFormView::OnHelp(wxCommandEvent
& event
)
201 void wxPropertyFormView::OnUpdate(wxCommandEvent
& event
)
203 TransferToPropertySheet();
206 void wxPropertyFormView::OnRevert(wxCommandEvent
& 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
*view
, wxWindow
*parentWindow
)
400 if (realMin
== 0.0 && realMax
== 0.0)
403 // The item used for viewing the real number: should be a text item.
404 wxWindow
*propertyWindow
= property
->GetWindow();
405 if (!propertyWindow
|| !propertyWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)))
408 wxString
value(((wxTextCtrl
*)propertyWindow
)->GetValue());
411 if (!StringToFloat(WXSTRINGCAST value
, &val
))
414 sprintf(buf
, "Value %s is not a valid real number!", (const char *)value
);
415 wxMessageBox(buf
, "Property value error", wxOK
| wxICON_EXCLAMATION
, parentWindow
);
419 if (val
< realMin
|| val
> realMax
)
422 sprintf(buf
, "Value must be a real number between %.2f and %.2f!", realMin
, realMax
);
423 wxMessageBox(buf
, "Property value error", wxOK
| wxICON_EXCLAMATION
, parentWindow
);
429 bool wxRealFormValidator::OnRetrieveValue(wxProperty
*property
, wxPropertyFormView
*view
, wxWindow
*parentWindow
)
431 // The item used for viewing the real number: should be a text item.
432 wxWindow
*propertyWindow
= property
->GetWindow();
433 if (!propertyWindow
|| !propertyWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)))
436 wxString
value(((wxTextCtrl
*)propertyWindow
)->GetValue());
438 if (value
.Length() == 0)
441 float f
= (float)atof((const char *)value
);
442 property
->GetValue() = f
;
446 bool wxRealFormValidator::OnDisplayValue(wxProperty
*property
, wxPropertyFormView
*view
, wxWindow
*parentWindow
)
448 // The item used for viewing the real number: should be a text item.
449 wxWindow
*propertyWindow
= property
->GetWindow();
450 if (!propertyWindow
|| !propertyWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)))
453 wxTextCtrl
*textItem
= (wxTextCtrl
*)propertyWindow
;
454 textItem
->SetValue(FloatToString(property
->GetValue().RealValue()));
459 /// Integer validator
461 IMPLEMENT_DYNAMIC_CLASS(wxIntegerFormValidator
, wxPropertyFormValidator
)
463 bool wxIntegerFormValidator::OnCheckValue(wxProperty
*property
, wxPropertyFormView
*view
, wxWindow
*parentWindow
)
465 if (integerMin
== 0.0 && integerMax
== 0.0)
468 // The item used for viewing the real number: should be a text item or a slider
469 wxWindow
*propertyWindow
= property
->GetWindow();
475 if (propertyWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)))
477 wxString
value(((wxTextCtrl
*)propertyWindow
)->GetValue());
479 if (!StringToLong(WXSTRINGCAST value
, &val
))
482 sprintf(buf
, "Value %s is not a valid integer!", (const char *)value
);
483 wxMessageBox(buf
, "Property value error", wxOK
| wxICON_EXCLAMATION
, parentWindow
);
487 else if (propertyWindow
->IsKindOf(CLASSINFO(wxSlider
)))
489 val
= (long)((wxSlider
*)propertyWindow
)->GetValue();
494 if (val
< integerMin
|| val
> integerMax
)
497 sprintf(buf
, "Value must be an integer between %ld and %ld!", integerMin
, integerMax
);
498 wxMessageBox(buf
, "Property value error", wxOK
| wxICON_EXCLAMATION
, parentWindow
);
504 bool wxIntegerFormValidator::OnRetrieveValue(wxProperty
*property
, wxPropertyFormView
*view
, wxWindow
*parentWindow
)
506 // The item used for viewing the real number: should be a text item or a slider
507 wxWindow
*propertyWindow
= property
->GetWindow();
511 if (propertyWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)))
513 wxString
value(((wxTextCtrl
*)propertyWindow
)->GetValue());
515 if (value
.Length() == 0)
518 long i
= atol((const char *)value
);
519 property
->GetValue() = i
;
521 else if (propertyWindow
->IsKindOf(CLASSINFO(wxSlider
)))
523 property
->GetValue() = (long)((wxSlider
*)propertyWindow
)->GetValue();
531 bool wxIntegerFormValidator::OnDisplayValue(wxProperty
*property
, wxPropertyFormView
*view
, wxWindow
*parentWindow
)
533 // The item used for viewing the real number: should be a text item or a slider
534 wxWindow
*propertyWindow
= property
->GetWindow();
538 if (propertyWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)))
540 wxTextCtrl
*textItem
= (wxTextCtrl
*)propertyWindow
;
541 textItem
->SetValue(LongToString(property
->GetValue().IntegerValue()));
543 else if (propertyWindow
->IsKindOf(CLASSINFO(wxSlider
)))
545 ((wxSlider
*)propertyWindow
)->SetValue((int)property
->GetValue().IntegerValue());
553 /// Boolean validator
555 IMPLEMENT_DYNAMIC_CLASS(wxBoolFormValidator
, wxPropertyFormValidator
)
557 bool wxBoolFormValidator::OnCheckValue(wxProperty
*property
, wxPropertyFormView
*view
, wxWindow
*parentWindow
)
559 // The item used for viewing the boolean: should be a checkbox
560 wxWindow
*propertyWindow
= property
->GetWindow();
561 if (!propertyWindow
|| !propertyWindow
->IsKindOf(CLASSINFO(wxCheckBox
)))
567 bool wxBoolFormValidator::OnRetrieveValue(wxProperty
*property
, wxPropertyFormView
*view
, wxWindow
*parentWindow
)
569 // The item used for viewing the boolean: should be a checkbox.
570 wxWindow
*propertyWindow
= property
->GetWindow();
571 if (!propertyWindow
|| !propertyWindow
->IsKindOf(CLASSINFO(wxCheckBox
)))
574 wxCheckBox
*checkBox
= (wxCheckBox
*)propertyWindow
;
576 property
->GetValue() = (bool)checkBox
->GetValue();
580 bool wxBoolFormValidator::OnDisplayValue(wxProperty
*property
, wxPropertyFormView
*view
, wxWindow
*parentWindow
)
582 // The item used for viewing the boolean: should be a checkbox.
583 wxWindow
*propertyWindow
= property
->GetWindow();
584 if (!propertyWindow
|| !propertyWindow
->IsKindOf(CLASSINFO(wxCheckBox
)))
587 wxCheckBox
*checkBox
= (wxCheckBox
*)propertyWindow
;
588 checkBox
->SetValue((bool)property
->GetValue().BoolValue());
595 IMPLEMENT_DYNAMIC_CLASS(wxStringFormValidator
, wxPropertyFormValidator
)
597 wxStringFormValidator::wxStringFormValidator(wxStringList
*list
, long flags
):
598 wxPropertyFormValidator(flags
)
603 bool wxStringFormValidator::OnCheckValue(wxProperty
*property
, wxPropertyFormView
*view
, wxWindow
*parentWindow
)
608 // The item used for viewing the string: should be a text item, choice item or listbox.
609 wxWindow
*propertyWindow
= property
->GetWindow();
612 if (propertyWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)))
614 wxTextCtrl
*text
= (wxTextCtrl
*)propertyWindow
;
615 if (!strings
->Member(text
->GetValue()))
617 wxString
s("Value ");
618 s
+= text
->GetValue();
619 s
+= " is not valid.";
620 wxMessageBox(s
, "Property value error", wxOK
| wxICON_EXCLAMATION
, parentWindow
);
626 // Any other item constrains the string value,
627 // so we don't have to check it.
632 bool wxStringFormValidator::OnRetrieveValue(wxProperty
*property
, wxPropertyFormView
*view
, wxWindow
*parentWindow
)
634 // The item used for viewing the string: should be a text item, choice item or listbox.
635 wxWindow
*propertyWindow
= property
->GetWindow();
638 if (propertyWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)))
640 wxTextCtrl
*text
= (wxTextCtrl
*)propertyWindow
;
641 property
->GetValue() = text
->GetValue();
643 else if (propertyWindow
->IsKindOf(CLASSINFO(wxListBox
)))
645 wxListBox
*lbox
= (wxListBox
*)propertyWindow
;
646 if (lbox
->GetSelection() > -1)
647 property
->GetValue() = lbox
->GetStringSelection();
650 else if (propertyWindow->IsKindOf(CLASSINFO(wxRadioBox)))
652 wxRadioBox *rbox = (wxRadioBox *)propertyWindow;
654 if ((n = rbox->GetSelection()) > -1)
655 property->GetValue() = rbox->GetString(n);
658 else if (propertyWindow
->IsKindOf(CLASSINFO(wxChoice
)))
660 wxChoice
*choice
= (wxChoice
*)propertyWindow
;
661 if (choice
->GetSelection() > -1)
662 property
->GetValue() = choice
->GetStringSelection();
669 bool wxStringFormValidator::OnDisplayValue(wxProperty
*property
, wxPropertyFormView
*view
, wxWindow
*parentWindow
)
671 // The item used for viewing the string: should be a text item, choice item or listbox.
672 wxWindow
*propertyWindow
= property
->GetWindow();
675 if (propertyWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)))
677 wxTextCtrl
*text
= (wxTextCtrl
*)propertyWindow
;
678 text
->SetValue(property
->GetValue().StringValue());
680 else if (propertyWindow
->IsKindOf(CLASSINFO(wxListBox
)))
682 wxListBox
*lbox
= (wxListBox
*)propertyWindow
;
683 if (lbox
->Number() == 0 && strings
)
685 // Try to initialize the listbox from 'strings'
686 wxNode
*node
= strings
->First();
689 char *s
= (char *)node
->Data();
694 lbox
->SetStringSelection(property
->GetValue().StringValue());
697 else if (propertyWindow->IsKindOf(CLASSINFO(wxRadioBox)))
699 wxRadioBox *rbox = (wxRadioBox *)propertyWindow;
700 rbox->SetStringSelection(property->GetValue().StringValue());
703 else if (propertyWindow
->IsKindOf(CLASSINFO(wxChoice
)))
705 wxChoice
*choice
= (wxChoice
*)propertyWindow
;
707 if (choice
->Number() == 0 && strings
)
709 // Try to initialize the choice item from 'strings'
710 // XView doesn't allow this kind of thing.
711 wxNode
*node
= strings
->First();
714 char *s
= (char *)node
->Data();
720 choice
->SetStringSelection(property
->GetValue().StringValue());