1 //-----------------------------------------------------------------------------
3 // Purpose: XML resources sample: Object references and ID ranges dialog
4 // Author: David Hart, Vaclav Slavik
5 // Copyright: (c) Vaclav Slavik
6 // Licence: wxWindows licence
7 //-----------------------------------------------------------------------------
9 //-----------------------------------------------------------------------------
10 // Standard wxWidgets headers
11 //-----------------------------------------------------------------------------
13 // For compilers that support precompilation, includes "wx/wx.h".
14 #include "wx/wxprec.h"
20 // For all others, include the necessary headers (this file is usually all you
21 // need because it includes almost all "standard" wxWidgets headers)
26 //-----------------------------------------------------------------------------
27 // Header of this .cpp file
28 //-----------------------------------------------------------------------------
30 #include "objrefdlg.h"
32 //-----------------------------------------------------------------------------
34 //-----------------------------------------------------------------------------
36 #include "wx/xrc/xmlres.h" // XRC XML resouces
40 //-----------------------------------------------------------------------------
42 //-----------------------------------------------------------------------------
43 ObjrefDialog::ObjrefDialog(wxWindow
* parent
)
45 wxXmlResource::Get()->LoadDialog(this, parent
, wxT("objref_dialog"));
47 nb
= XRCCTRL(*this, "objref_notebook", wxNotebook
);
48 wxCHECK_RET(nb
, "failed to find objref_notebook");
50 // Connect different event handlers.
51 nb
->Connect(wxEVT_NOTEBOOK_PAGE_CHANGED
,
52 wxNotebookEventHandler(ObjrefDialog::OnNotebookPageChanged
),
55 // We want to direct UpdateUI events for the ID range 'first_row' to
56 // OnUpdateUIFirst(). We could achieve this using first_row[0] and
57 // first_row[2], but what if a fourth column were added? It's safer to use
58 // the 'typedefs' for the two ends of the range:
59 wxNotebookPage
*page
= nb
->GetPage(icons_page
);
60 page
->Connect(XRCID("first_row[start]"), XRCID("first_row[end]"),
62 wxUpdateUIEventHandler(ObjrefDialog::OnUpdateUIFirst
),
64 page
->Connect(XRCID("second_row[start]"), XRCID("second_row[end]"),
66 wxUpdateUIEventHandler(ObjrefDialog::OnUpdateUISecond
),
68 page
->Connect(XRCID("third_row[start]"), XRCID("third_row[end]"),
70 wxUpdateUIEventHandler(ObjrefDialog::OnUpdateUIThird
),
73 // Connect the id ranges, using the [start] and [end] 'typedefs'
74 page
= nb
->GetPage(calc_page
);
75 page
->Connect(XRCID("digits[start]"), XRCID("digits[end]"),
77 wxCommandEventHandler(ObjrefDialog::OnNumeralClick
),
79 page
->Connect(XRCID("operators[start]"), XRCID("operators[end]"),
81 wxCommandEventHandler(ObjrefDialog::OnOperatorClick
),
86 ObjrefDialog::~ObjrefDialog()
88 // Select page 0. Otherwise if the Calc page were selected, when it's
89 // removed the Icons page is selected and sets the log target again in idle
90 // time, *after* myframe restores the old one!
91 nb
->ChangeSelection(0);
94 //-----------------------------------------------------------------------------
95 // Private members (including the event handlers)
96 //-----------------------------------------------------------------------------
97 void ObjrefDialog::OnNotebookPageChanged( wxNotebookEvent
&event
)
99 switch(event
.GetSelection())
103 // This is a straight object reference to the first page
104 // so change the text programmatically
105 nb
->SetPageText(copy_page
, "Page 1 copy");
107 wxNotebookPage
*page
= nb
->GetPage(copy_page
);
109 text
= XRCCTRL(*page
, "description_text", wxTextCtrl
);
111 "This is a duplicate of page 1, using an object reference. "
112 "It was created by this very simple xml:\n\n"
113 "<object class=\"notebookpage\">\n\t<object_ref ref=\"page1\"/>\n"
114 "\t<label>Page 1 copy</label>\n</object>"
115 "\n\n(Then I'm cheating by inserting this text programmatically.)"
122 wxNotebookPage
*page
= nb
->GetPage(icons_page
);
123 text
= XRCCTRL(*page
, "log_text", wxTextCtrl
);
125 delete wxLog::SetActiveTarget(new wxLogTextCtrl(text
));
131 wxNotebookPage
*page
= nb
->GetPage(calc_page
);
132 result_txt
= XRCCTRL(*page
, "result", wxTextCtrl
);
133 text
= XRCCTRL(*page
, "log_text", wxTextCtrl
);
135 delete wxLog::SetActiveTarget(new wxLogTextCtrl(text
));
143 // There are undoubtedly simpler ways of doing all this, but we're
144 // demonstrating the use of ID ranges
145 void ObjrefDialog::OnUpdateUIFirst(wxUpdateUIEvent
& event
)
147 // The checkbox with the XRCID 'check[0]' controls this row of icons
149 chk
= XRCCTRL(*(nb
->GetPage(icons_page
)), "check[0]", wxCheckBox
);
151 event
.Enable(chk
->IsChecked());
153 // Let's create a log-window entry
154 static bool checked
= true;
155 if (chk
->IsChecked() != checked
)
157 checked
= chk
->IsChecked();
158 wxLogMessage("Row one has been %s by check[0], XRCID = %i",
159 checked
? "enabled" : "disabled", XRCID("check[0]"));
160 wxLogMessage("XRCIDs: first_row[start] = %i, first_row[0] = %i, "
161 "first_row[1] = %i, first_row[2] = %i, "
162 "first_row[end] = %i",
163 XRCID("first_row[start]"), XRCID("first_row[0]"),
164 XRCID("first_row[1]"), XRCID("first_row[2]"),
165 XRCID("first_row[end]"));
169 void ObjrefDialog::OnUpdateUISecond(wxUpdateUIEvent
& event
)
171 // The checkbox with the XRCID 'check[1]' controls this row of icons
173 chk
= XRCCTRL(*(nb
->GetPage(icons_page
)), "check[1]", wxCheckBox
);
175 event
.Enable(chk
->IsChecked());
177 // Let's create a log-window entry
178 static bool checked
= true;
179 if (chk
->IsChecked() != checked
)
181 checked
= chk
->IsChecked();
182 wxLogMessage("Row two has been %s by check[1], XRCID = %i",
183 checked
? "enabled" : "disabled", XRCID("check[1]"));
184 wxLogMessage("XRCIDs: second_row[start] = %i, second_row[0] = %i, "
185 "second_row[1] = %i, second_row[2] = %i, "
186 "second_row[end] = %i",
187 XRCID("second_row[start]"), XRCID("second_row[0]"),
188 XRCID("second_row[1]"), XRCID("second_row[2]"),
189 XRCID("second_row[end]"));
193 void ObjrefDialog::OnUpdateUIThird(wxUpdateUIEvent
& event
)
195 // The checkbox with the XRCID 'check[2]' controls this row of icons
197 chk
= XRCCTRL(*(nb
->GetPage(icons_page
)), "check[2]", wxCheckBox
);
199 event
.Enable(chk
->IsChecked());
201 // Let's create a log-window entry
202 static bool checked
= true;
203 if (chk
->IsChecked() != checked
)
205 checked
= chk
->IsChecked();
206 wxLogMessage("Row three has been %s by check[2], XRCID = %i",
207 checked
? "enabled" : "disabled", XRCID("check[2]"));
208 wxLogMessage("XRCIDs: third_row[start] = %i, third_row[0] = %i, "
209 "third_row[1] = %i, third_row[2] = %i, "
210 "third_row[end] = %i",
211 XRCID("third_row[start]"), XRCID("third_row[0]"),
212 XRCID("third_row[1]"), XRCID("third_row[2]"),
213 XRCID("third_row[end]"));
217 void ObjrefDialog::OnNumeralClick(wxCommandEvent
& event
)
219 // See how the id range simplifies determining which numeral was clicked
220 int digit
= event
.GetId() - XRCID("digits[start]");
222 char c
= '0' + digit
;
223 if (current
==0 && previous
==0)
225 // We're just starting a calculation, so get rid of the placeholder '0'
228 else if (operator_expected
== true)
230 // If we've just finished one calculation, and now a digit is entered,
238 current
= current
*10 + digit
;
240 wxLogMessage("You clicked digits[%c], XRCID %i", c
, event
.GetId());
243 void ObjrefDialog::OnOperatorClick(wxCommandEvent
& event
)
245 static const char symbols
[] = "+-*/=";
247 operator_expected
= false;
248 int ID
= event
.GetId() - XRCID("operators[start]");
250 // We carefully used "operators[end]" as the name of the Clear button
251 if (event
.GetId() == XRCID("operators[end]"))
253 wxLogMessage("You clicked operators[%i], XRCID %d, 'Clear'",
263 case operator_multiply
:
264 case operator_divide
:
265 if (current
!=0 || previous
!=0)
267 // We're in the middle of a complex calculation, so do the
271 curr_operator
= (CalcOperator
)ID
;
274 case operator_equals
:
276 wxLogMessage("You clicked operators[%i], XRCID %i, giving a '%c'",
277 ID
, event
.GetId(), symbols
[ID
]);
278 curr_operator
= operator_equals
;
279 // Flag that the next entry should be an operator, not a digit
280 operator_expected
= true;
284 (*result_txt
) << ' ' << symbols
[ID
] << ' ';
286 wxLogMessage("You clicked operators[%i], XRCID %i, giving a '%c'",
287 ID
, event
.GetId(), symbols
[ID
]);
290 void ObjrefDialog::Calculate()
292 switch(curr_operator
)
295 previous
+= current
; break;
297 previous
-= current
; break;
298 case operator_multiply
:
299 previous
*= current
; break;
300 case operator_divide
:
307 curr_operator
= operator_plus
;
311 (*result_txt
) << previous
;
314 void ObjrefDialog::ClearCalculator()
316 current
= previous
= 0;
317 curr_operator
= operator_plus
;
318 operator_expected
= false;
319 result_txt
->ChangeValue("0");