]>
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::sm_dialogCancelled
= FALSE
;
59 wxPropertyFormView::wxPropertyFormView(wxWindow
*propPanel
, long flags
):wxPropertyView(flags
)
61 m_propertyWindow
= propPanel
;
62 m_managedWindow
= NULL
;
64 m_windowCloseButton
= NULL
;
65 m_windowCancelButton
= NULL
;
66 m_windowHelpButton
= NULL
;
68 m_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
= m_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, m_propertyWindow
))
112 bool wxPropertyFormView::TransferToPropertySheet(void)
114 if (!m_propertySheet
)
117 wxNode
*node
= m_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, m_propertyWindow
);
132 bool wxPropertyFormView::TransferToDialog(void)
134 if (!m_propertySheet
)
137 wxNode
*node
= m_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, m_propertyWindow
);
152 bool wxPropertyFormView::AssociateNames(void)
154 if (!m_propertySheet
|| !m_propertyWindow
)
157 wxNode
*node
= m_propertyWindow
->GetChildren()->First();
160 wxWindow
*win
= (wxWindow
*)node
->Data();
161 if (win
->GetName() != "")
163 wxProperty
*prop
= m_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 sm_dialogCancelled
= FALSE
;
187 m_managedWindow
->Close(TRUE
);
190 void wxPropertyFormView::OnCancel(wxCommandEvent
& WXUNUSED(event
))
192 sm_dialogCancelled
= TRUE
;
194 m_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
)
213 if (!m_propertySheet
)
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
= m_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, m_propertyWindow
, event
);
251 void wxPropertyFormView::OnDoubleClick(wxControl
*item
)
253 if (!m_propertySheet
)
256 // Find a validator to route the command to.
257 wxNode
*node
= m_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, m_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 m_view
->AssociatePanel(this);
287 m_view
->SetManagedWindow(this);
288 // SetAutoLayout(TRUE);
291 bool wxPropertyFormDialog::OnClose(void)
303 void wxPropertyFormDialog::OnDefaultAction(wxControl
*item
)
305 m_view
->OnDoubleClick(item
);
308 void wxPropertyFormDialog::OnCommand(wxWindow
& win
, wxCommandEvent
& event
)
311 m_view
->OnCommand(win
, event
);
314 // Extend event processing to search the view's event table
315 bool wxPropertyFormDialog::ProcessEvent(wxEvent
& event
)
317 if ( !m_view
|| ! m_view
->ProcessEvent(event
) )
318 return wxEvtHandler::ProcessEvent(event
);
325 * Property form panel
328 IMPLEMENT_CLASS(wxPropertyFormPanel
, wxPanel
)
330 void wxPropertyFormPanel::OnDefaultAction(wxControl
*item
)
332 m_view
->OnDoubleClick(item
);
335 void wxPropertyFormPanel::OnCommand(wxWindow
& win
, wxCommandEvent
& event
)
337 m_view
->OnCommand(win
, event
);
340 // Extend event processing to search the view's event table
341 bool wxPropertyFormPanel::ProcessEvent(wxEvent
& event
)
343 if ( !m_view
|| ! m_view
->ProcessEvent(event
) )
344 return wxEvtHandler::ProcessEvent(event
);
353 IMPLEMENT_CLASS(wxPropertyFormFrame
, wxFrame
)
355 bool wxPropertyFormFrame::OnClose(void)
358 return m_view
->OnClose();
363 wxPanel
*wxPropertyFormFrame::OnCreatePanel(wxFrame
*parent
, wxPropertyFormView
*v
)
365 return new wxPropertyFormPanel(v
, parent
);
368 bool wxPropertyFormFrame::Initialize(void)
370 m_propertyPanel
= OnCreatePanel(this, m_view
);
373 m_view
->AssociatePanel(m_propertyPanel
);
374 m_view
->SetManagedWindow(this);
382 * Property form specific validator
385 IMPLEMENT_ABSTRACT_CLASS(wxPropertyFormValidator
, wxPropertyValidator
)
392 IMPLEMENT_DYNAMIC_CLASS(wxRealFormValidator
, wxPropertyFormValidator
)
395 /// Real number form validator
397 bool wxRealFormValidator::OnCheckValue( wxProperty
*property
, wxPropertyFormView
*WXUNUSED(view
),
398 wxWindow
*parentWindow
)
400 if (m_realMin
== 0.0 && m_realMax
== 0.0)
403 // The item used for viewing the real number: should be a text item.
404 wxWindow
*m_propertyWindow
= property
->GetWindow();
405 if (!m_propertyWindow
|| !m_propertyWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)))
408 wxString
value(((wxTextCtrl
*)m_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
< m_realMin
|| val
> m_realMax
)
422 sprintf(buf
, "Value must be a real number between %.2f and %.2f!", m_realMin
, m_realMax
);
423 wxMessageBox(buf
, "Property value error", wxOK
| wxICON_EXCLAMATION
, parentWindow
);
429 bool wxRealFormValidator::OnRetrieveValue(wxProperty
*property
, wxPropertyFormView
*WXUNUSED(view
),
430 wxWindow
*WXUNUSED(parentWindow
) )
432 // The item used for viewing the real number: should be a text item.
433 wxWindow
*m_propertyWindow
= property
->GetWindow();
434 if (!m_propertyWindow
|| !m_propertyWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)))
437 wxString
value(((wxTextCtrl
*)m_propertyWindow
)->GetValue());
439 if (value
.Length() == 0)
442 float f
= (float)atof((const char *)value
);
443 property
->GetValue() = f
;
447 bool wxRealFormValidator::OnDisplayValue(wxProperty
*property
, wxPropertyFormView
*WXUNUSED(view
),
448 wxWindow
*WXUNUSED(parentWindow
) )
450 // The item used for viewing the real number: should be a text item.
451 wxWindow
*m_propertyWindow
= property
->GetWindow();
452 if (!m_propertyWindow
|| !m_propertyWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)))
455 wxTextCtrl
*textItem
= (wxTextCtrl
*)m_propertyWindow
;
456 textItem
->SetValue(FloatToString(property
->GetValue().RealValue()));
461 /// Integer validator
463 IMPLEMENT_DYNAMIC_CLASS(wxIntegerFormValidator
, wxPropertyFormValidator
)
465 bool wxIntegerFormValidator::OnCheckValue(wxProperty
*property
, wxPropertyFormView
*WXUNUSED(view
),
466 wxWindow
*parentWindow
)
468 if (m_integerMin
== 0.0 && m_integerMax
== 0.0)
471 // The item used for viewing the real number: should be a text item or a slider
472 wxWindow
*m_propertyWindow
= property
->GetWindow();
473 if (!m_propertyWindow
)
478 if (m_propertyWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)))
480 wxString
value(((wxTextCtrl
*)m_propertyWindow
)->GetValue());
482 if (!StringToLong(WXSTRINGCAST value
, &val
))
485 sprintf(buf
, "Value %s is not a valid integer!", (const char *)value
);
486 wxMessageBox(buf
, "Property value error", wxOK
| wxICON_EXCLAMATION
, parentWindow
);
490 else if (m_propertyWindow
->IsKindOf(CLASSINFO(wxSlider
)))
492 val
= (long)((wxSlider
*)m_propertyWindow
)->GetValue();
497 if (val
< m_integerMin
|| val
> m_integerMax
)
500 sprintf(buf
, "Value must be an integer between %ld and %ld!", m_integerMin
, m_integerMax
);
501 wxMessageBox(buf
, "Property value error", wxOK
| wxICON_EXCLAMATION
, parentWindow
);
507 bool wxIntegerFormValidator::OnRetrieveValue(wxProperty
*property
, wxPropertyFormView
*WXUNUSED(view
),
508 wxWindow
*WXUNUSED(parentWindow
))
510 // The item used for viewing the real number: should be a text item or a slider
511 wxWindow
*m_propertyWindow
= property
->GetWindow();
512 if (!m_propertyWindow
)
515 if (m_propertyWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)))
517 wxString
value(((wxTextCtrl
*)m_propertyWindow
)->GetValue());
519 if (value
.Length() == 0)
522 long i
= atol((const char *)value
);
523 property
->GetValue() = i
;
525 else if (m_propertyWindow
->IsKindOf(CLASSINFO(wxSlider
)))
527 property
->GetValue() = (long)((wxSlider
*)m_propertyWindow
)->GetValue();
535 bool wxIntegerFormValidator::OnDisplayValue( wxProperty
*property
, wxPropertyFormView
*WXUNUSED(view
),
536 wxWindow
*WXUNUSED(parentWindow
))
538 // The item used for viewing the real number: should be a text item or a slider
539 wxWindow
*m_propertyWindow
= property
->GetWindow();
540 if (!m_propertyWindow
)
543 if (m_propertyWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)))
545 wxTextCtrl
*textItem
= (wxTextCtrl
*)m_propertyWindow
;
546 textItem
->SetValue(LongToString(property
->GetValue().IntegerValue()));
548 else if (m_propertyWindow
->IsKindOf(CLASSINFO(wxSlider
)))
550 ((wxSlider
*)m_propertyWindow
)->SetValue((int)property
->GetValue().IntegerValue());
558 /// Boolean validator
560 IMPLEMENT_DYNAMIC_CLASS(wxBoolFormValidator
, wxPropertyFormValidator
)
562 bool wxBoolFormValidator::OnCheckValue(wxProperty
*property
, wxPropertyFormView
*WXUNUSED(view
),
563 wxWindow
*WXUNUSED(parentWindow
))
565 // The item used for viewing the boolean: should be a checkbox
566 wxWindow
*m_propertyWindow
= property
->GetWindow();
567 if (!m_propertyWindow
|| !m_propertyWindow
->IsKindOf(CLASSINFO(wxCheckBox
)))
573 bool wxBoolFormValidator::OnRetrieveValue(wxProperty
*property
, wxPropertyFormView
*WXUNUSED(view
),
574 wxWindow
*WXUNUSED(parentWindow
) )
576 // The item used for viewing the boolean: should be a checkbox.
577 wxWindow
*m_propertyWindow
= property
->GetWindow();
578 if (!m_propertyWindow
|| !m_propertyWindow
->IsKindOf(CLASSINFO(wxCheckBox
)))
581 wxCheckBox
*checkBox
= (wxCheckBox
*)m_propertyWindow
;
583 property
->GetValue() = (bool)checkBox
->GetValue();
587 bool wxBoolFormValidator::OnDisplayValue(wxProperty
*property
, wxPropertyFormView
*WXUNUSED(view
),
588 wxWindow
*WXUNUSED(parentWindow
))
590 // The item used for viewing the boolean: should be a checkbox.
591 wxWindow
*m_propertyWindow
= property
->GetWindow();
592 if (!m_propertyWindow
|| !m_propertyWindow
->IsKindOf(CLASSINFO(wxCheckBox
)))
595 wxCheckBox
*checkBox
= (wxCheckBox
*)m_propertyWindow
;
596 checkBox
->SetValue((bool)property
->GetValue().BoolValue());
603 IMPLEMENT_DYNAMIC_CLASS(wxStringFormValidator
, wxPropertyFormValidator
)
605 wxStringFormValidator::wxStringFormValidator(wxStringList
*list
, long flags
):
606 wxPropertyFormValidator(flags
)
611 bool wxStringFormValidator::OnCheckValue(wxProperty
*property
, wxPropertyFormView
*WXUNUSED(view
),
612 wxWindow
*parentWindow
)
617 // The item used for viewing the string: should be a text item, choice item or listbox.
618 wxWindow
*m_propertyWindow
= property
->GetWindow();
619 if (!m_propertyWindow
)
621 if (m_propertyWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)))
623 wxTextCtrl
*text
= (wxTextCtrl
*)m_propertyWindow
;
624 if (!m_strings
->Member(text
->GetValue()))
626 wxString
s("Value ");
627 s
+= text
->GetValue();
628 s
+= " is not valid.";
629 wxMessageBox(s
, "Property value error", wxOK
| wxICON_EXCLAMATION
, parentWindow
);
635 // Any other item constrains the string value,
636 // so we don't have to check it.
641 bool wxStringFormValidator::OnRetrieveValue(wxProperty
*property
, wxPropertyFormView
*WXUNUSED(view
),
642 wxWindow
*WXUNUSED(parentWindow
) )
644 // The item used for viewing the string: should be a text item, choice item or listbox.
645 wxWindow
*m_propertyWindow
= property
->GetWindow();
646 if (!m_propertyWindow
)
648 if (m_propertyWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)))
650 wxTextCtrl
*text
= (wxTextCtrl
*)m_propertyWindow
;
651 property
->GetValue() = text
->GetValue();
653 else if (m_propertyWindow
->IsKindOf(CLASSINFO(wxListBox
)))
655 wxListBox
*lbox
= (wxListBox
*)m_propertyWindow
;
656 if (lbox
->GetSelection() > -1)
657 property
->GetValue() = lbox
->GetStringSelection();
660 else if (m_propertyWindow->IsKindOf(CLASSINFO(wxRadioBox)))
662 wxRadioBox *rbox = (wxRadioBox *)m_propertyWindow;
664 if ((n = rbox->GetSelection()) > -1)
665 property->GetValue() = rbox->GetString(n);
668 else if (m_propertyWindow
->IsKindOf(CLASSINFO(wxChoice
)))
670 wxChoice
*choice
= (wxChoice
*)m_propertyWindow
;
671 if (choice
->GetSelection() > -1)
672 property
->GetValue() = choice
->GetStringSelection();
679 bool wxStringFormValidator::OnDisplayValue(wxProperty
*property
, wxPropertyFormView
*WXUNUSED(view
),
680 wxWindow
*WXUNUSED(parentWindow
) )
682 // The item used for viewing the string: should be a text item, choice item or listbox.
683 wxWindow
*m_propertyWindow
= property
->GetWindow();
684 if (!m_propertyWindow
)
686 if (m_propertyWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)))
688 wxTextCtrl
*text
= (wxTextCtrl
*)m_propertyWindow
;
689 text
->SetValue(property
->GetValue().StringValue());
691 else if (m_propertyWindow
->IsKindOf(CLASSINFO(wxListBox
)))
693 wxListBox
*lbox
= (wxListBox
*)m_propertyWindow
;
694 if (lbox
->Number() == 0 && m_strings
)
696 // Try to initialize the listbox from 'strings'
697 wxNode
*node
= m_strings
->First();
700 char *s
= (char *)node
->Data();
705 lbox
->SetStringSelection(property
->GetValue().StringValue());
708 else if (m_propertyWindow->IsKindOf(CLASSINFO(wxRadioBox)))
710 wxRadioBox *rbox = (wxRadioBox *)m_propertyWindow;
711 rbox->SetStringSelection(property->GetValue().StringValue());
714 else if (m_propertyWindow
->IsKindOf(CLASSINFO(wxChoice
)))
716 wxChoice
*choice
= (wxChoice
*)m_propertyWindow
;
718 if (choice
->Number() == 0 && m_strings
)
720 // Try to initialize the choice item from 'strings'
721 // XView doesn't allow this kind of thing.
722 wxNode
*node
= m_strings
->First();
725 char *s
= (char *)node
->Data();
731 choice
->SetStringSelection(property
->GetValue().StringValue());