1 //-----------------------------------------------------------------------------
3 // Purpose: XML resources sample: Object references and ID ranges dialog
4 // Author: David Hart, Vaclav Slavik
6 // Copyright: (c) Vaclav Slavik
7 // Licence: wxWindows licence
8 //-----------------------------------------------------------------------------
10 //-----------------------------------------------------------------------------
11 // Standard wxWidgets headers
12 //-----------------------------------------------------------------------------
14 // For compilers that support precompilation, includes "wx/wx.h".
15 #include "wx/wxprec.h"
21 // For all others, include the necessary headers (this file is usually all you
22 // need because it includes almost all "standard" wxWidgets headers)
27 //-----------------------------------------------------------------------------
28 // Header of this .cpp file
29 //-----------------------------------------------------------------------------
31 #include "objrefdlg.h"
33 //-----------------------------------------------------------------------------
35 //-----------------------------------------------------------------------------
37 #include "wx/xrc/xmlres.h" // XRC XML resouces
41 //-----------------------------------------------------------------------------
43 //-----------------------------------------------------------------------------
44 ObjrefDialog::ObjrefDialog(wxWindow
* parent
)
46 wxXmlResource::Get()->LoadDialog(this, parent
, wxT("objref_dialog"));
48 nb
= XRCCTRL(*this, "objref_notebook", wxNotebook
);
49 wxCHECK_RET(nb
, "failed to find objref_notebook");
51 // Connect different event handlers.
52 nb
->Connect(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
,
53 wxNotebookEventHandler(ObjrefDialog::OnNotebookPageChanged
),
56 // We want to direct UpdateUI events for the ID range 'first_row' to
57 // OnUpdateUIFirst(). We could achieve this using first_row[0] and
58 // first_row[2], but what if a fourth column were added? It's safer to use
59 // the 'typedefs' for the two ends of the range:
60 wxNotebookPage
*page
= nb
->GetPage(icons_page
);
61 page
->Connect(XRCID("first_row[start]"), XRCID("first_row[end]"),
63 wxUpdateUIEventHandler(ObjrefDialog::OnUpdateUIFirst
),
65 page
->Connect(XRCID("second_row[start]"), XRCID("second_row[end]"),
67 wxUpdateUIEventHandler(ObjrefDialog::OnUpdateUISecond
),
69 page
->Connect(XRCID("third_row[start]"), XRCID("third_row[end]"),
71 wxUpdateUIEventHandler(ObjrefDialog::OnUpdateUIThird
),
74 // Connect the id ranges, using the [start] and [end] 'typedefs'
75 page
= nb
->GetPage(calc_page
);
76 page
->Connect(XRCID("digits[start]"), XRCID("digits[end]"),
77 wxEVT_COMMAND_BUTTON_CLICKED
,
78 wxCommandEventHandler(ObjrefDialog::OnNumeralClick
),
80 page
->Connect(XRCID("operators[start]"), XRCID("operators[end]"),
81 wxEVT_COMMAND_BUTTON_CLICKED
,
82 wxCommandEventHandler(ObjrefDialog::OnOperatorClick
),
87 ObjrefDialog::~ObjrefDialog()
89 // Select page 0. Otherwise if the Calc page were selected, when it's
90 // removed the Icons page is selected and sets the log target again in idle
91 // time, *after* myframe restores the old one!
92 nb
->ChangeSelection(0);
95 //-----------------------------------------------------------------------------
96 // Private members (including the event handlers)
97 //-----------------------------------------------------------------------------
98 void ObjrefDialog::OnNotebookPageChanged( wxNotebookEvent
&event
)
100 switch(event
.GetSelection())
104 // This is a straight object reference to the first page
105 // so change the text programmatically
106 nb
->SetPageText(copy_page
, "Page 1 copy");
108 wxNotebookPage
*page
= nb
->GetPage(copy_page
);
110 text
= XRCCTRL(*page
, "description_text", wxTextCtrl
);
112 "This is a duplicate of page 1, using an object reference. "
113 "It was created by this very simple xml:\n\n"
114 "<object class=\"notebookpage\">\n\t<object_ref ref=\"page1\"/>\n"
115 "\t<label>Page 1 copy</label>\n</object>"
116 "\n\n(Then I'm cheating by inserting this text programmatically.)"
123 wxNotebookPage
*page
= nb
->GetPage(icons_page
);
124 text
= XRCCTRL(*page
, "log_text", wxTextCtrl
);
126 delete wxLog::SetActiveTarget(new wxLogTextCtrl(text
));
132 wxNotebookPage
*page
= nb
->GetPage(calc_page
);
133 result_txt
= XRCCTRL(*page
, "result", wxTextCtrl
);
134 text
= XRCCTRL(*page
, "log_text", wxTextCtrl
);
136 delete wxLog::SetActiveTarget(new wxLogTextCtrl(text
));
144 // There are undoubtedly simpler ways of doing all this, but we're
145 // demonstrating the use of ID ranges
146 void ObjrefDialog::OnUpdateUIFirst(wxUpdateUIEvent
& event
)
148 // The checkbox with the XRCID 'check[0]' controls this row of icons
150 chk
= XRCCTRL(*(nb
->GetPage(icons_page
)), "check[0]", wxCheckBox
);
152 event
.Enable(chk
->IsChecked());
154 // Let's create a log-window entry
155 static bool checked
= true;
156 if (chk
->IsChecked() != checked
)
158 checked
= chk
->IsChecked();
159 wxLogMessage("Row one has been %s by check[0], XRCID = %i",
160 checked
? "enabled" : "disabled", XRCID("check[0]"));
161 wxLogMessage("XRCIDs: first_row[start] = %i, first_row[0] = %i, "
162 "first_row[1] = %i, first_row[2] = %i, "
163 "first_row[end] = %i",
164 XRCID("first_row[start]"), XRCID("first_row[0]"),
165 XRCID("first_row[1]"), XRCID("first_row[2]"),
166 XRCID("first_row[end]"));
170 void ObjrefDialog::OnUpdateUISecond(wxUpdateUIEvent
& event
)
172 // The checkbox with the XRCID 'check[1]' controls this row of icons
174 chk
= XRCCTRL(*(nb
->GetPage(icons_page
)), "check[1]", wxCheckBox
);
176 event
.Enable(chk
->IsChecked());
178 // Let's create a log-window entry
179 static bool checked
= true;
180 if (chk
->IsChecked() != checked
)
182 checked
= chk
->IsChecked();
183 wxLogMessage("Row two has been %s by check[1], XRCID = %i",
184 checked
? "enabled" : "disabled", XRCID("check[1]"));
185 wxLogMessage("XRCIDs: second_row[start] = %i, second_row[0] = %i, "
186 "second_row[1] = %i, second_row[2] = %i, "
187 "second_row[end] = %i",
188 XRCID("second_row[start]"), XRCID("second_row[0]"),
189 XRCID("second_row[1]"), XRCID("second_row[2]"),
190 XRCID("second_row[end]"));
194 void ObjrefDialog::OnUpdateUIThird(wxUpdateUIEvent
& event
)
196 // The checkbox with the XRCID 'check[2]' controls this row of icons
198 chk
= XRCCTRL(*(nb
->GetPage(icons_page
)), "check[2]", wxCheckBox
);
200 event
.Enable(chk
->IsChecked());
202 // Let's create a log-window entry
203 static bool checked
= true;
204 if (chk
->IsChecked() != checked
)
206 checked
= chk
->IsChecked();
207 wxLogMessage("Row three has been %s by check[2], XRCID = %i",
208 checked
? "enabled" : "disabled", XRCID("check[2]"));
209 wxLogMessage("XRCIDs: third_row[start] = %i, third_row[0] = %i, "
210 "third_row[1] = %i, third_row[2] = %i, "
211 "third_row[end] = %i",
212 XRCID("third_row[start]"), XRCID("third_row[0]"),
213 XRCID("third_row[1]"), XRCID("third_row[2]"),
214 XRCID("third_row[end]"));
218 void ObjrefDialog::OnNumeralClick(wxCommandEvent
& event
)
220 // See how the id range simplifies determining which numeral was clicked
221 int digit
= event
.GetId() - XRCID("digits[start]");
223 char c
= '0' + digit
;
224 if (current
==0 && previous
==0)
226 // We're just starting a calculation, so get rid of the placeholder '0'
229 else if (operator_expected
== true)
231 // If we've just finished one calculation, and now a digit is entered,
239 current
= current
*10 + digit
;
241 wxLogMessage("You clicked digits[%c], XRCID %i", c
, event
.GetId());
244 void ObjrefDialog::OnOperatorClick(wxCommandEvent
& event
)
246 static const char symbols
[] = "+-*/=";
248 operator_expected
= false;
249 int ID
= event
.GetId() - XRCID("operators[start]");
251 // We carefully used "operators[end]" as the name of the Clear button
252 if (event
.GetId() == XRCID("operators[end]"))
254 wxLogMessage("You clicked operators[%i], XRCID %d, 'Clear'",
264 case operator_multiply
:
265 case operator_divide
:
266 if (current
!=0 || previous
!=0)
268 // We're in the middle of a complex calculation, so do the
272 curr_operator
= (CalcOperator
)ID
;
275 case operator_equals
:
277 wxLogMessage("You clicked operators[%i], XRCID %i, giving a '%c'",
278 ID
, event
.GetId(), symbols
[ID
]);
279 curr_operator
= operator_equals
;
280 // Flag that the next entry should be an operator, not a digit
281 operator_expected
= true;
285 (*result_txt
) << ' ' << symbols
[ID
] << ' ';
287 wxLogMessage("You clicked operators[%i], XRCID %i, giving a '%c'",
288 ID
, event
.GetId(), symbols
[ID
]);
291 void ObjrefDialog::Calculate()
293 switch(curr_operator
)
296 previous
+= current
; break;
298 previous
-= current
; break;
299 case operator_multiply
:
300 previous
*= current
; break;
301 case operator_divide
:
308 curr_operator
= operator_plus
;
312 (*result_txt
) << previous
;
315 void ObjrefDialog::ClearCalculator()
317 current
= previous
= 0;
318 curr_operator
= operator_plus
;
319 operator_expected
= false;
320 result_txt
->ChangeValue("0");