]>
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"
33 #if defined(__WXMSW__) && !defined(__GNUWIN32__)
36 #include <strstream.h>
42 #include "wx/window.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
;
191 m_managedWindow
->Close(TRUE
);
194 void wxPropertyFormView::OnCancel(wxCommandEvent
& WXUNUSED(event
))
196 sm_dialogCancelled
= TRUE
;
198 m_managedWindow
->Close(TRUE
);
201 void wxPropertyFormView::OnHelp(wxCommandEvent
& WXUNUSED(event
))
205 void wxPropertyFormView::OnUpdate(wxCommandEvent
& WXUNUSED(event
))
207 TransferToPropertySheet();
210 void wxPropertyFormView::OnRevert(wxCommandEvent
& WXUNUSED(event
))
215 void wxPropertyFormView::OnCommand(wxWindow
& win
, wxCommandEvent
& event
)
217 if (!m_propertySheet
)
220 if (win
.GetName() == "")
223 if (strcmp(win
.GetName(), "ok") == 0)
225 else if (strcmp(win
.GetName(), "cancel") == 0)
227 else if (strcmp(win
.GetName(), "help") == 0)
229 else if (strcmp(win
.GetName(), "update") == 0)
231 else if (strcmp(win
.GetName(), "revert") == 0)
235 // Find a validator to route the command to.
236 wxNode
*node
= m_propertySheet
->GetProperties().First();
239 wxProperty
*prop
= (wxProperty
*)node
->Data();
240 if (prop
->GetWindow() && (prop
->GetWindow() == &win
))
242 wxPropertyValidator
*validator
= FindPropertyValidator(prop
);
243 if (validator
&& validator
->IsKindOf(CLASSINFO(wxPropertyFormValidator
)))
245 wxPropertyFormValidator
*formValidator
= (wxPropertyFormValidator
*)validator
;
246 formValidator
->OnCommand(prop
, this, m_propertyWindow
, event
);
255 void wxPropertyFormView::OnDoubleClick(wxControl
*item
)
257 if (!m_propertySheet
)
260 // Find a validator to route the command to.
261 wxNode
*node
= m_propertySheet
->GetProperties().First();
264 wxProperty
*prop
= (wxProperty
*)node
->Data();
265 if (prop
->GetWindow() && ((wxControl
*)prop
->GetWindow() == item
))
267 wxPropertyValidator
*validator
= FindPropertyValidator(prop
);
268 if (validator
&& validator
->IsKindOf(CLASSINFO(wxPropertyFormValidator
)))
270 wxPropertyFormValidator
*formValidator
= (wxPropertyFormValidator
*)validator
;
271 formValidator
->OnDoubleClick(prop
, this, m_propertyWindow
);
280 * Property form dialog box
283 IMPLEMENT_CLASS(wxPropertyFormDialog
, wxDialog
)
285 wxPropertyFormDialog::wxPropertyFormDialog(wxPropertyFormView
*v
, wxWindow
*parent
, const wxString
& title
,
286 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
):
287 wxDialog(parent
, -1, title
, pos
, size
, style
, name
)
290 m_view
->AssociatePanel(this);
291 m_view
->SetManagedWindow(this);
292 // SetAutoLayout(TRUE);
295 bool wxPropertyFormDialog::OnClose(void)
307 void wxPropertyFormDialog::OnDefaultAction(wxControl
*item
)
309 m_view
->OnDoubleClick(item
);
312 void wxPropertyFormDialog::OnCommand(wxWindow
& win
, wxCommandEvent
& event
)
315 m_view
->OnCommand(win
, event
);
318 // Extend event processing to search the view's event table
319 bool wxPropertyFormDialog::ProcessEvent(wxEvent
& event
)
321 if ( !m_view
|| ! m_view
->ProcessEvent(event
) )
322 return wxEvtHandler::ProcessEvent(event
);
329 * Property form panel
332 IMPLEMENT_CLASS(wxPropertyFormPanel
, wxPanel
)
334 void wxPropertyFormPanel::OnDefaultAction(wxControl
*item
)
336 m_view
->OnDoubleClick(item
);
339 void wxPropertyFormPanel::OnCommand(wxWindow
& win
, wxCommandEvent
& event
)
341 m_view
->OnCommand(win
, event
);
344 // Extend event processing to search the view's event table
345 bool wxPropertyFormPanel::ProcessEvent(wxEvent
& event
)
347 if ( !m_view
|| ! m_view
->ProcessEvent(event
) )
348 return wxEvtHandler::ProcessEvent(event
);
357 IMPLEMENT_CLASS(wxPropertyFormFrame
, wxFrame
)
359 bool wxPropertyFormFrame::OnClose(void)
362 return m_view
->OnClose();
367 wxPanel
*wxPropertyFormFrame::OnCreatePanel(wxFrame
*parent
, wxPropertyFormView
*v
)
369 return new wxPropertyFormPanel(v
, parent
);
372 bool wxPropertyFormFrame::Initialize(void)
374 m_propertyPanel
= OnCreatePanel(this, m_view
);
377 m_view
->AssociatePanel(m_propertyPanel
);
378 m_view
->SetManagedWindow(this);
386 * Property form specific validator
389 IMPLEMENT_ABSTRACT_CLASS(wxPropertyFormValidator
, wxPropertyValidator
)
396 IMPLEMENT_DYNAMIC_CLASS(wxRealFormValidator
, wxPropertyFormValidator
)
399 /// Real number form validator
401 bool wxRealFormValidator::OnCheckValue( wxProperty
*property
, wxPropertyFormView
*WXUNUSED(view
),
402 wxWindow
*parentWindow
)
404 if (m_realMin
== 0.0 && m_realMax
== 0.0)
407 // The item used for viewing the real number: should be a text item.
408 wxWindow
*m_propertyWindow
= property
->GetWindow();
409 if (!m_propertyWindow
|| !m_propertyWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)))
412 wxString
value(((wxTextCtrl
*)m_propertyWindow
)->GetValue());
415 if (!StringToFloat(WXSTRINGCAST value
, &val
))
418 sprintf(buf
, "Value %s is not a valid real number!", (const char *)value
);
419 wxMessageBox(buf
, "Property value error", wxOK
| wxICON_EXCLAMATION
, parentWindow
);
423 if (val
< m_realMin
|| val
> m_realMax
)
426 sprintf(buf
, "Value must be a real number between %.2f and %.2f!", m_realMin
, m_realMax
);
427 wxMessageBox(buf
, "Property value error", wxOK
| wxICON_EXCLAMATION
, parentWindow
);
433 bool wxRealFormValidator::OnRetrieveValue(wxProperty
*property
, wxPropertyFormView
*WXUNUSED(view
),
434 wxWindow
*WXUNUSED(parentWindow
) )
436 // The item used for viewing the real number: should be a text item.
437 wxWindow
*m_propertyWindow
= property
->GetWindow();
438 if (!m_propertyWindow
|| !m_propertyWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)))
441 wxString
value(((wxTextCtrl
*)m_propertyWindow
)->GetValue());
443 if (value
.Length() == 0)
446 float f
= (float)atof((const char *)value
);
447 property
->GetValue() = f
;
451 bool wxRealFormValidator::OnDisplayValue(wxProperty
*property
, wxPropertyFormView
*WXUNUSED(view
),
452 wxWindow
*WXUNUSED(parentWindow
) )
454 // The item used for viewing the real number: should be a text item.
455 wxWindow
*m_propertyWindow
= property
->GetWindow();
456 if (!m_propertyWindow
|| !m_propertyWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)))
459 wxTextCtrl
*textItem
= (wxTextCtrl
*)m_propertyWindow
;
460 textItem
->SetValue(FloatToString(property
->GetValue().RealValue()));
465 /// Integer validator
467 IMPLEMENT_DYNAMIC_CLASS(wxIntegerFormValidator
, wxPropertyFormValidator
)
469 bool wxIntegerFormValidator::OnCheckValue(wxProperty
*property
, wxPropertyFormView
*WXUNUSED(view
),
470 wxWindow
*parentWindow
)
472 if (m_integerMin
== 0.0 && m_integerMax
== 0.0)
475 // The item used for viewing the real number: should be a text item or a slider
476 wxWindow
*m_propertyWindow
= property
->GetWindow();
477 if (!m_propertyWindow
)
482 if (m_propertyWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)))
484 wxString
value(((wxTextCtrl
*)m_propertyWindow
)->GetValue());
486 if (!StringToLong(WXSTRINGCAST value
, &val
))
489 sprintf(buf
, "Value %s is not a valid integer!", (const char *)value
);
490 wxMessageBox(buf
, "Property value error", wxOK
| wxICON_EXCLAMATION
, parentWindow
);
494 else if (m_propertyWindow
->IsKindOf(CLASSINFO(wxSlider
)))
496 val
= (long)((wxSlider
*)m_propertyWindow
)->GetValue();
501 if (val
< m_integerMin
|| val
> m_integerMax
)
504 sprintf(buf
, "Value must be an integer between %ld and %ld!", m_integerMin
, m_integerMax
);
505 wxMessageBox(buf
, "Property value error", wxOK
| wxICON_EXCLAMATION
, parentWindow
);
511 bool wxIntegerFormValidator::OnRetrieveValue(wxProperty
*property
, wxPropertyFormView
*WXUNUSED(view
),
512 wxWindow
*WXUNUSED(parentWindow
))
514 // The item used for viewing the real number: should be a text item or a slider
515 wxWindow
*m_propertyWindow
= property
->GetWindow();
516 if (!m_propertyWindow
)
519 if (m_propertyWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)))
521 wxString
value(((wxTextCtrl
*)m_propertyWindow
)->GetValue());
523 if (value
.Length() == 0)
526 long i
= atol((const char *)value
);
527 property
->GetValue() = i
;
529 else if (m_propertyWindow
->IsKindOf(CLASSINFO(wxSlider
)))
531 property
->GetValue() = (long)((wxSlider
*)m_propertyWindow
)->GetValue();
539 bool wxIntegerFormValidator::OnDisplayValue( wxProperty
*property
, wxPropertyFormView
*WXUNUSED(view
),
540 wxWindow
*WXUNUSED(parentWindow
))
542 // The item used for viewing the real number: should be a text item or a slider
543 wxWindow
*m_propertyWindow
= property
->GetWindow();
544 if (!m_propertyWindow
)
547 if (m_propertyWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)))
549 wxTextCtrl
*textItem
= (wxTextCtrl
*)m_propertyWindow
;
550 textItem
->SetValue(LongToString(property
->GetValue().IntegerValue()));
552 else if (m_propertyWindow
->IsKindOf(CLASSINFO(wxSlider
)))
554 ((wxSlider
*)m_propertyWindow
)->SetValue((int)property
->GetValue().IntegerValue());
562 /// Boolean validator
564 IMPLEMENT_DYNAMIC_CLASS(wxBoolFormValidator
, wxPropertyFormValidator
)
566 bool wxBoolFormValidator::OnCheckValue(wxProperty
*property
, wxPropertyFormView
*WXUNUSED(view
),
567 wxWindow
*WXUNUSED(parentWindow
))
569 // The item used for viewing the boolean: should be a checkbox
570 wxWindow
*m_propertyWindow
= property
->GetWindow();
571 if (!m_propertyWindow
|| !m_propertyWindow
->IsKindOf(CLASSINFO(wxCheckBox
)))
577 bool wxBoolFormValidator::OnRetrieveValue(wxProperty
*property
, wxPropertyFormView
*WXUNUSED(view
),
578 wxWindow
*WXUNUSED(parentWindow
) )
580 // The item used for viewing the boolean: should be a checkbox.
581 wxWindow
*m_propertyWindow
= property
->GetWindow();
582 if (!m_propertyWindow
|| !m_propertyWindow
->IsKindOf(CLASSINFO(wxCheckBox
)))
585 wxCheckBox
*checkBox
= (wxCheckBox
*)m_propertyWindow
;
587 property
->GetValue() = (bool)checkBox
->GetValue();
591 bool wxBoolFormValidator::OnDisplayValue(wxProperty
*property
, wxPropertyFormView
*WXUNUSED(view
),
592 wxWindow
*WXUNUSED(parentWindow
))
594 // The item used for viewing the boolean: should be a checkbox.
595 wxWindow
*m_propertyWindow
= property
->GetWindow();
596 if (!m_propertyWindow
|| !m_propertyWindow
->IsKindOf(CLASSINFO(wxCheckBox
)))
599 wxCheckBox
*checkBox
= (wxCheckBox
*)m_propertyWindow
;
600 checkBox
->SetValue((bool)property
->GetValue().BoolValue());
607 IMPLEMENT_DYNAMIC_CLASS(wxStringFormValidator
, wxPropertyFormValidator
)
609 wxStringFormValidator::wxStringFormValidator(wxStringList
*list
, long flags
):
610 wxPropertyFormValidator(flags
)
615 bool wxStringFormValidator::OnCheckValue(wxProperty
*property
, wxPropertyFormView
*WXUNUSED(view
),
616 wxWindow
*parentWindow
)
621 // The item used for viewing the string: should be a text item, choice item or listbox.
622 wxWindow
*m_propertyWindow
= property
->GetWindow();
623 if (!m_propertyWindow
)
625 if (m_propertyWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)))
627 wxTextCtrl
*text
= (wxTextCtrl
*)m_propertyWindow
;
628 if (!m_strings
->Member(text
->GetValue()))
630 wxString
s("Value ");
631 s
+= text
->GetValue();
632 s
+= " is not valid.";
633 wxMessageBox(s
, "Property value error", wxOK
| wxICON_EXCLAMATION
, parentWindow
);
639 // Any other item constrains the string value,
640 // so we don't have to check it.
645 bool wxStringFormValidator::OnRetrieveValue(wxProperty
*property
, wxPropertyFormView
*WXUNUSED(view
),
646 wxWindow
*WXUNUSED(parentWindow
) )
648 // The item used for viewing the string: should be a text item, choice item or listbox.
649 wxWindow
*m_propertyWindow
= property
->GetWindow();
650 if (!m_propertyWindow
)
652 if (m_propertyWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)))
654 wxTextCtrl
*text
= (wxTextCtrl
*)m_propertyWindow
;
655 property
->GetValue() = text
->GetValue();
657 else if (m_propertyWindow
->IsKindOf(CLASSINFO(wxListBox
)))
659 wxListBox
*lbox
= (wxListBox
*)m_propertyWindow
;
660 if (lbox
->GetSelection() > -1)
661 property
->GetValue() = lbox
->GetStringSelection();
664 else if (m_propertyWindow->IsKindOf(CLASSINFO(wxRadioBox)))
666 wxRadioBox *rbox = (wxRadioBox *)m_propertyWindow;
668 if ((n = rbox->GetSelection()) > -1)
669 property->GetValue() = rbox->GetString(n);
672 else if (m_propertyWindow
->IsKindOf(CLASSINFO(wxChoice
)))
674 wxChoice
*choice
= (wxChoice
*)m_propertyWindow
;
675 if (choice
->GetSelection() > -1)
676 property
->GetValue() = choice
->GetStringSelection();
683 bool wxStringFormValidator::OnDisplayValue(wxProperty
*property
, wxPropertyFormView
*WXUNUSED(view
),
684 wxWindow
*WXUNUSED(parentWindow
) )
686 // The item used for viewing the string: should be a text item, choice item or listbox.
687 wxWindow
*m_propertyWindow
= property
->GetWindow();
688 if (!m_propertyWindow
)
690 if (m_propertyWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)))
692 wxTextCtrl
*text
= (wxTextCtrl
*)m_propertyWindow
;
693 text
->SetValue(property
->GetValue().StringValue());
695 else if (m_propertyWindow
->IsKindOf(CLASSINFO(wxListBox
)))
697 wxListBox
*lbox
= (wxListBox
*)m_propertyWindow
;
698 if (lbox
->Number() == 0 && m_strings
)
700 // Try to initialize the listbox from 'strings'
701 wxNode
*node
= m_strings
->First();
704 char *s
= (char *)node
->Data();
709 lbox
->SetStringSelection(property
->GetValue().StringValue());
712 else if (m_propertyWindow->IsKindOf(CLASSINFO(wxRadioBox)))
714 wxRadioBox *rbox = (wxRadioBox *)m_propertyWindow;
715 rbox->SetStringSelection(property->GetValue().StringValue());
718 else if (m_propertyWindow
->IsKindOf(CLASSINFO(wxChoice
)))
720 wxChoice
*choice
= (wxChoice
*)m_propertyWindow
;
722 if (choice
->Number() == 0 && m_strings
)
724 // Try to initialize the choice item from 'strings'
725 // XView doesn't allow this kind of thing.
726 wxNode
*node
= m_strings
->First();
729 char *s
= (char *)node
->Data();
735 choice
->SetStringSelection(property
->GetValue().StringValue());