]>
Commit | Line | Data |
---|---|---|
201ca879 VZ |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: htmllbox.cpp | |
be5a51fb | 3 | // Purpose: HtmlLbox wxWidgets sample |
201ca879 VZ |
4 | // Author: Vadim Zeitlin |
5 | // Modified by: | |
6 | // Created: 31.05.03 | |
7 | // RCS-ID: $Id$ | |
be5a51fb | 8 | // Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwidgets.org> |
201ca879 VZ |
9 | // Licence: wxWindows licence |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
20 | // For compilers that support precompilation, includes "wx/wx.h". | |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
24 | #pragma hdrstop | |
25 | #endif | |
26 | ||
5f732810 | 27 | // for all others, include the necessary headers |
201ca879 VZ |
28 | #ifndef WX_PRECOMP |
29 | #include "wx/app.h" | |
30 | #include "wx/frame.h" | |
31 | #include "wx/log.h" | |
5f732810 | 32 | #include "wx/textdlg.h" |
6170c108 VZ |
33 | #include "wx/sizer.h" |
34 | ||
35 | #include "wx/menu.h" | |
36 | #include "wx/msgdlg.h" | |
37 | #include "wx/textctrl.h" | |
38 | ||
39 | #include "wx/dc.h" | |
ed269e7b | 40 | #include "wx/icon.h" |
201ca879 VZ |
41 | #endif |
42 | ||
9a9b4940 | 43 | #include "wx/colordlg.h" |
e4f3eb42 | 44 | #include "wx/numdlg.h" |
9a9b4940 | 45 | |
201ca879 VZ |
46 | #include "wx/htmllbox.h" |
47 | ||
5f732810 VZ |
48 | // you can also have a file containing HTML strings for testing, enable this if |
49 | // you want to use it | |
c2a331e0 | 50 | //#define USE_HTML_FILE |
5f732810 VZ |
51 | #ifdef USE_HTML_FILE |
52 | #include "wx/textfile.h" | |
53 | #endif | |
54 | ||
b6cf9ad0 | 55 | #include "../sample.xpm" |
201ca879 VZ |
56 | |
57 | // ---------------------------------------------------------------------------- | |
58 | // private classes | |
59 | // ---------------------------------------------------------------------------- | |
60 | ||
201ca879 VZ |
61 | // to use wxHtmlListBox you must derive a new class from it as you must |
62 | // implement pure virtual OnGetItem() | |
63 | class MyHtmlListBox : public wxHtmlListBox | |
64 | { | |
65 | public: | |
9ebb7cad | 66 | MyHtmlListBox() { } |
9a9b4940 | 67 | MyHtmlListBox(wxWindow *parent, bool multi = false); |
5f732810 | 68 | |
9a9b4940 | 69 | void SetChangeSelFg(bool change) { m_change = change; } |
1d41ed0a | 70 | void UpdateFirstItem(); |
201ca879 VZ |
71 | |
72 | protected: | |
1d41ed0a VZ |
73 | // override this method to return data to be shown in the listbox (this is |
74 | // mandatory) | |
9a9b4940 | 75 | virtual wxString OnGetItem(size_t n) const; |
201ca879 | 76 | |
1d41ed0a | 77 | // change the appearance by overriding these functions (this is optional) |
5f732810 | 78 | virtual void OnDrawSeparator(wxDC& dc, wxRect& rect, size_t n) const; |
9a9b4940 | 79 | virtual wxColour GetSelectedTextColour(const wxColour& colFg) const; |
201ca879 | 80 | |
1d41ed0a VZ |
81 | // flag telling us whether we should use fg colour even for the selected |
82 | // item | |
9a9b4940 VZ |
83 | bool m_change; |
84 | ||
1d41ed0a VZ |
85 | // flag which we toggle to update the first items text in OnGetItem() |
86 | bool m_firstItemUpdated; | |
87 | ||
a1c3cdc4 VS |
88 | public: |
89 | ||
bc55e31b VS |
90 | // flag which we toggle when the user clicks on the link in 2nd item |
91 | // to change 2nd item's text | |
92 | bool m_linkClicked; | |
1d41ed0a | 93 | |
9a9b4940 | 94 | #ifdef USE_HTML_FILE |
5f732810 | 95 | wxTextFile m_file; |
9a9b4940 | 96 | #endif |
27d0dcd0 | 97 | |
c0c133e1 | 98 | wxDECLARE_NO_COPY_CLASS(MyHtmlListBox); |
9ebb7cad | 99 | DECLARE_DYNAMIC_CLASS(MyHtmlListBox) |
201ca879 VZ |
100 | }; |
101 | ||
9ebb7cad | 102 | |
8613d47b VZ |
103 | class MyFrame : public wxFrame |
104 | { | |
105 | public: | |
106 | MyFrame(); | |
107 | virtual ~MyFrame(); | |
108 | ||
109 | // event handlers | |
9ebb7cad | 110 | void OnSimpleOrCustomBox(wxCommandEvent& event); |
8613d47b VZ |
111 | void OnQuit(wxCommandEvent& event); |
112 | void OnAbout(wxCommandEvent& event); | |
113 | ||
5f732810 VZ |
114 | void OnSetMargins(wxCommandEvent& event); |
115 | void OnDrawSeparator(wxCommandEvent&) { m_hlbox->RefreshAll(); } | |
116 | void OnToggleMulti(wxCommandEvent& event); | |
117 | void OnSelectAll(wxCommandEvent& event); | |
1d41ed0a | 118 | void OnUpdateItem(wxCommandEvent& event); |
293b15f7 | 119 | void OnGetItemRect(wxCommandEvent& event); |
8613d47b | 120 | |
9a9b4940 VZ |
121 | void OnSetBgCol(wxCommandEvent& event); |
122 | void OnSetSelBgCol(wxCommandEvent& event); | |
123 | void OnSetSelFgCol(wxCommandEvent& event); | |
124 | ||
60f0cf4e | 125 | void OnClear(wxCommandEvent& event); |
9a9b4940 | 126 | |
5f732810 | 127 | void OnUpdateUISelectAll(wxUpdateUIEvent& event); |
8613d47b | 128 | |
5f732810 | 129 | void OnLboxSelect(wxCommandEvent& event); |
8613d47b VZ |
130 | void OnLboxDClick(wxCommandEvent& event) |
131 | { | |
9a83f860 | 132 | wxLogMessage(wxT("Listbox item %d double clicked."), event.GetInt()); |
8613d47b | 133 | } |
f1963164 | 134 | |
a1c3cdc4 VS |
135 | void OnHtmlLinkClicked(wxHtmlLinkEvent& event); |
136 | void OnHtmlCellHover(wxHtmlCellEvent &event); | |
137 | void OnHtmlCellClicked(wxHtmlCellEvent &event); | |
138 | ||
f1963164 | 139 | wxSimpleHtmlListBox *GetSimpleBox() |
9ebb7cad VZ |
140 | { return wxDynamicCast(m_hlbox, wxSimpleHtmlListBox); } |
141 | MyHtmlListBox *GetMyBox() | |
142 | { return wxDynamicCast(m_hlbox, MyHtmlListBox); } | |
143 | ||
144 | void CreateBox(); | |
8613d47b VZ |
145 | |
146 | private: | |
9ebb7cad | 147 | wxHtmlListBox *m_hlbox; |
8613d47b | 148 | |
be5a51fb | 149 | // any class wishing to process wxWidgets events must use this macro |
8613d47b VZ |
150 | DECLARE_EVENT_TABLE() |
151 | }; | |
152 | ||
5f732810 VZ |
153 | class MyApp : public wxApp |
154 | { | |
155 | public: | |
07850a49 | 156 | virtual bool OnInit() { (new MyFrame())->Show(); return true; } |
5f732810 VZ |
157 | }; |
158 | ||
201ca879 VZ |
159 | // ---------------------------------------------------------------------------- |
160 | // constants | |
161 | // ---------------------------------------------------------------------------- | |
162 | ||
163 | // IDs for the controls and the menu commands | |
164 | enum | |
165 | { | |
166 | // menu items | |
9ebb7cad VZ |
167 | HtmlLbox_CustomBox = 1, |
168 | HtmlLbox_SimpleBox, | |
169 | HtmlLbox_Quit, | |
201ca879 | 170 | |
5f732810 VZ |
171 | HtmlLbox_SetMargins, |
172 | HtmlLbox_DrawSeparator, | |
173 | HtmlLbox_ToggleMulti, | |
174 | HtmlLbox_SelectAll, | |
1d41ed0a | 175 | HtmlLbox_UpdateItem, |
293b15f7 | 176 | HtmlLbox_GetItemRect, |
5f732810 | 177 | |
9a9b4940 VZ |
178 | HtmlLbox_SetBgCol, |
179 | HtmlLbox_SetSelBgCol, | |
180 | HtmlLbox_SetSelFgCol, | |
181 | ||
60f0cf4e VZ |
182 | HtmlLbox_Clear, |
183 | ||
201ca879 VZ |
184 | // it is important for the id corresponding to the "About" command to have |
185 | // this standard value as otherwise it won't be handled properly under Mac | |
186 | // (where it is special and put into the "Apple" menu) | |
187 | HtmlLbox_About = wxID_ABOUT | |
188 | }; | |
189 | ||
190 | // ---------------------------------------------------------------------------- | |
be5a51fb | 191 | // event tables and other macros for wxWidgets |
201ca879 VZ |
192 | // ---------------------------------------------------------------------------- |
193 | ||
201ca879 | 194 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) |
9ebb7cad VZ |
195 | EVT_MENU(HtmlLbox_CustomBox, MyFrame::OnSimpleOrCustomBox) |
196 | EVT_MENU(HtmlLbox_SimpleBox, MyFrame::OnSimpleOrCustomBox) | |
201ca879 | 197 | EVT_MENU(HtmlLbox_Quit, MyFrame::OnQuit) |
5f732810 VZ |
198 | |
199 | EVT_MENU(HtmlLbox_SetMargins, MyFrame::OnSetMargins) | |
200 | EVT_MENU(HtmlLbox_DrawSeparator, MyFrame::OnDrawSeparator) | |
201 | EVT_MENU(HtmlLbox_ToggleMulti, MyFrame::OnToggleMulti) | |
202 | EVT_MENU(HtmlLbox_SelectAll, MyFrame::OnSelectAll) | |
1d41ed0a | 203 | EVT_MENU(HtmlLbox_UpdateItem, MyFrame::OnUpdateItem) |
293b15f7 | 204 | EVT_MENU(HtmlLbox_GetItemRect, MyFrame::OnGetItemRect) |
5f732810 | 205 | |
201ca879 VZ |
206 | EVT_MENU(HtmlLbox_About, MyFrame::OnAbout) |
207 | ||
9a9b4940 VZ |
208 | EVT_MENU(HtmlLbox_SetBgCol, MyFrame::OnSetBgCol) |
209 | EVT_MENU(HtmlLbox_SetSelBgCol, MyFrame::OnSetSelBgCol) | |
210 | EVT_MENU(HtmlLbox_SetSelFgCol, MyFrame::OnSetSelFgCol) | |
5f732810 | 211 | |
60f0cf4e VZ |
212 | EVT_MENU(HtmlLbox_Clear, MyFrame::OnClear) |
213 | ||
5f732810 VZ |
214 | EVT_UPDATE_UI(HtmlLbox_SelectAll, MyFrame::OnUpdateUISelectAll) |
215 | ||
216 | ||
201ca879 VZ |
217 | EVT_LISTBOX(wxID_ANY, MyFrame::OnLboxSelect) |
218 | EVT_LISTBOX_DCLICK(wxID_ANY, MyFrame::OnLboxDClick) | |
a1c3cdc4 VS |
219 | |
220 | ||
221 | // the HTML listbox's events | |
222 | EVT_HTML_LINK_CLICKED(wxID_ANY, MyFrame::OnHtmlLinkClicked) | |
223 | EVT_HTML_CELL_HOVER(wxID_ANY, MyFrame::OnHtmlCellHover) | |
224 | EVT_HTML_CELL_CLICKED(wxID_ANY, MyFrame::OnHtmlCellClicked) | |
225 | ||
201ca879 VZ |
226 | END_EVENT_TABLE() |
227 | ||
201ca879 VZ |
228 | IMPLEMENT_APP(MyApp) |
229 | ||
230 | // ============================================================================ | |
5f732810 | 231 | // MyFrame |
201ca879 VZ |
232 | // ============================================================================ |
233 | ||
234 | // ---------------------------------------------------------------------------- | |
5f732810 | 235 | // MyFrame ctor/dtor |
201ca879 VZ |
236 | // ---------------------------------------------------------------------------- |
237 | ||
238 | // frame constructor | |
239 | MyFrame::MyFrame() | |
9a83f860 | 240 | : wxFrame(NULL, wxID_ANY, wxT("HtmlLbox wxWidgets Sample"), |
9ebb7cad | 241 | wxDefaultPosition, wxSize(500, 500)) |
201ca879 VZ |
242 | { |
243 | // set the frame icon | |
3cb332c1 | 244 | SetIcon(wxICON(sample)); |
201ca879 VZ |
245 | |
246 | #if wxUSE_MENUS | |
247 | // create a menu bar | |
248 | wxMenu *menuFile = new wxMenu; | |
9a83f860 VZ |
249 | menuFile->AppendRadioItem(HtmlLbox_CustomBox, wxT("Use custom box"), |
250 | wxT("Use a wxHtmlListBox virtual class control")); | |
251 | menuFile->AppendRadioItem(HtmlLbox_SimpleBox, wxT("Use simple box"), | |
252 | wxT("Use a wxSimpleHtmlListBox control")); | |
9ebb7cad | 253 | menuFile->AppendSeparator(); |
9a83f860 | 254 | menuFile->Append(HtmlLbox_Quit, wxT("E&xit\tAlt-X"), wxT("Quit this program")); |
5f732810 VZ |
255 | |
256 | // create our specific menu | |
257 | wxMenu *menuHLbox = new wxMenu; | |
258 | menuHLbox->Append(HtmlLbox_SetMargins, | |
9a83f860 VZ |
259 | wxT("Set &margins...\tCtrl-G"), |
260 | wxT("Change the margins around the items")); | |
5f732810 | 261 | menuHLbox->AppendCheckItem(HtmlLbox_DrawSeparator, |
9a83f860 VZ |
262 | wxT("&Draw separators\tCtrl-D"), |
263 | wxT("Toggle drawing separators between cells")); | |
5f732810 VZ |
264 | menuHLbox->AppendSeparator(); |
265 | menuHLbox->AppendCheckItem(HtmlLbox_ToggleMulti, | |
9a83f860 VZ |
266 | wxT("&Multiple selection\tCtrl-M"), |
267 | wxT("Toggle multiple selection on/off")); | |
5f732810 | 268 | menuHLbox->AppendSeparator(); |
9a83f860 VZ |
269 | menuHLbox->Append(HtmlLbox_SelectAll, wxT("Select &all items\tCtrl-A")); |
270 | menuHLbox->Append(HtmlLbox_UpdateItem, wxT("Update &first item\tCtrl-U")); | |
271 | menuHLbox->Append(HtmlLbox_GetItemRect, wxT("Show &rectangle of item #10\tCtrl-R")); | |
9a9b4940 | 272 | menuHLbox->AppendSeparator(); |
9a83f860 | 273 | menuHLbox->Append(HtmlLbox_SetBgCol, wxT("Set &background...\tCtrl-B")); |
9a9b4940 | 274 | menuHLbox->Append(HtmlLbox_SetSelBgCol, |
9a83f860 | 275 | wxT("Set &selection background...\tCtrl-S")); |
9a9b4940 | 276 | menuHLbox->AppendCheckItem(HtmlLbox_SetSelFgCol, |
9a83f860 | 277 | wxT("Keep &foreground in selection\tCtrl-F")); |
201ca879 | 278 | |
60f0cf4e | 279 | menuHLbox->AppendSeparator(); |
9a83f860 | 280 | menuHLbox->Append(HtmlLbox_Clear, wxT("&Clear\tCtrl-L")); |
60f0cf4e | 281 | |
201ca879 VZ |
282 | // the "About" item should be in the help menu |
283 | wxMenu *helpMenu = new wxMenu; | |
9a83f860 | 284 | helpMenu->Append(HtmlLbox_About, wxT("&About...\tF1"), wxT("Show about dialog")); |
201ca879 | 285 | |
201ca879 VZ |
286 | // now append the freshly created menu to the menu bar... |
287 | wxMenuBar *menuBar = new wxMenuBar(); | |
9a83f860 VZ |
288 | menuBar->Append(menuFile, wxT("&File")); |
289 | menuBar->Append(menuHLbox, wxT("&Listbox")); | |
290 | menuBar->Append(helpMenu, wxT("&Help")); | |
201ca879 | 291 | |
5f732810 VZ |
292 | menuBar->Check(HtmlLbox_DrawSeparator, true); |
293 | ||
201ca879 VZ |
294 | // ... and attach this menu bar to the frame |
295 | SetMenuBar(menuBar); | |
296 | #endif // wxUSE_MENUS | |
297 | ||
298 | #if wxUSE_STATUSBAR | |
299 | // create a status bar just for fun (by default with 1 pane only) | |
300 | CreateStatusBar(2); | |
9a83f860 | 301 | SetStatusText(wxT("Welcome to wxWidgets!")); |
201ca879 | 302 | #endif // wxUSE_STATUSBAR |
f1963164 | 303 | |
201ca879 | 304 | // create the child controls |
9ebb7cad | 305 | CreateBox(); |
9a83f860 | 306 | wxTextCtrl *text = new wxTextCtrl(this, wxID_ANY, wxT(""), |
201ca879 VZ |
307 | wxDefaultPosition, wxDefaultSize, |
308 | wxTE_MULTILINE); | |
309 | delete wxLog::SetActiveTarget(new wxLogTextCtrl(text)); | |
310 | ||
311 | // and lay them out | |
312 | wxSizer *sizer = new wxBoxSizer(wxHORIZONTAL); | |
9ebb7cad VZ |
313 | sizer->Add(m_hlbox, 2, wxGROW); |
314 | sizer->Add(text, 3, wxGROW); | |
201ca879 VZ |
315 | |
316 | SetSizer(sizer); | |
317 | } | |
318 | ||
319 | MyFrame::~MyFrame() | |
320 | { | |
321 | delete wxLog::SetActiveTarget(NULL); | |
322 | } | |
323 | ||
9ebb7cad VZ |
324 | void MyFrame::CreateBox() |
325 | { | |
326 | bool multi = GetMenuBar()->IsChecked(HtmlLbox_ToggleMulti); | |
327 | ||
328 | if ( GetMenuBar()->IsChecked(HtmlLbox_CustomBox) ) | |
329 | { | |
330 | m_hlbox = new MyHtmlListBox(this, multi); | |
331 | } | |
332 | else // simple listbox | |
333 | { | |
334 | m_hlbox = new wxSimpleHtmlListBox(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, | |
335 | 0, NULL, multi ? wxLB_MULTIPLE : 0); | |
f1963164 | 336 | |
9ebb7cad VZ |
337 | // unlike wxHtmlListBox which is abstract, wxSimpleHtmlListBox is a |
338 | // concrete control and doesn't support virtual mode, this we need | |
339 | // to add all of its items from the beginning | |
340 | wxArrayString arr; | |
341 | for (size_t n = 0; n < 1000; n++ ) | |
342 | { | |
343 | wxColour clr((unsigned char)(abs((int)n - 192) % 256), | |
344 | (unsigned char)(abs((int)n - 256) % 256), | |
345 | (unsigned char)(abs((int)n - 128) % 256)); | |
346 | int level = n % 6 + 1; | |
347 | ||
9a83f860 VZ |
348 | wxString label = wxString::Format(wxT("<h%d><font color=%s>") |
349 | wxT("Item</font> <b>%lu</b>") | |
350 | wxT("</h%d>"), | |
9ebb7cad VZ |
351 | level, |
352 | clr.GetAsString(wxC2S_HTML_SYNTAX).c_str(), | |
353 | (unsigned long)n, level); | |
354 | arr.Add(label); | |
355 | } | |
356 | ||
357 | GetSimpleBox()->Append(arr); | |
358 | } | |
359 | } | |
360 | ||
361 | ||
5f732810 VZ |
362 | // ---------------------------------------------------------------------------- |
363 | // menu event handlers | |
364 | // ---------------------------------------------------------------------------- | |
365 | ||
9ebb7cad VZ |
366 | void MyFrame::OnSimpleOrCustomBox(wxCommandEvent& WXUNUSED(event)) |
367 | { | |
368 | wxWindow *old = m_hlbox; | |
f1963164 | 369 | |
9ebb7cad VZ |
370 | // we need to recreate the listbox |
371 | CreateBox(); | |
372 | GetSizer()->Replace(old, m_hlbox); | |
373 | delete old; | |
f1963164 | 374 | |
9ebb7cad VZ |
375 | GetSizer()->Layout(); |
376 | Refresh(); | |
377 | } | |
378 | ||
201ca879 VZ |
379 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) |
380 | { | |
07850a49 WS |
381 | // true is to force the frame to close |
382 | Close(true); | |
201ca879 VZ |
383 | } |
384 | ||
385 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) | |
386 | { | |
9a83f860 VZ |
387 | wxMessageBox(wxT("This sample shows wxHtmlListBox class.\n") |
388 | wxT("\n") | |
389 | wxT("(c) 2003 Vadim Zeitlin"), | |
390 | wxT("About HtmlLbox"), | |
201ca879 VZ |
391 | wxOK | wxICON_INFORMATION, |
392 | this); | |
393 | } | |
394 | ||
9a9b4940 | 395 | void MyFrame::OnSetMargins(wxCommandEvent& WXUNUSED(event)) |
5f732810 VZ |
396 | { |
397 | long margin = wxGetNumberFromUser | |
398 | ( | |
9a83f860 VZ |
399 | wxT("Enter the margins to use for the listbox items."), |
400 | wxT("Margin: "), | |
401 | wxT("HtmlLbox: Set the margins"), | |
5f732810 VZ |
402 | 0, 0, 20, |
403 | this | |
404 | ); | |
405 | ||
406 | if ( margin != -1 ) | |
407 | { | |
408 | m_hlbox->SetMargins(margin, margin); | |
409 | m_hlbox->RefreshAll(); | |
410 | } | |
411 | } | |
412 | ||
9ebb7cad | 413 | void MyFrame::OnToggleMulti(wxCommandEvent& WXUNUSED(event)) |
5f732810 | 414 | { |
9ebb7cad | 415 | wxWindow *old = m_hlbox; |
f1963164 | 416 | |
5f732810 | 417 | // we need to recreate the listbox |
9ebb7cad VZ |
418 | CreateBox(); |
419 | GetSizer()->Replace(old, m_hlbox); | |
420 | delete old; | |
5f732810 | 421 | |
9ebb7cad | 422 | GetSizer()->Layout(); |
5f732810 VZ |
423 | } |
424 | ||
9a9b4940 | 425 | void MyFrame::OnSelectAll(wxCommandEvent& WXUNUSED(event)) |
5f732810 | 426 | { |
4636a1e8 | 427 | m_hlbox->SelectAll(); |
5f732810 VZ |
428 | } |
429 | ||
430 | void MyFrame::OnUpdateUISelectAll(wxUpdateUIEvent& event) | |
431 | { | |
432 | event.Enable( m_hlbox && m_hlbox->HasMultipleSelection() ); | |
433 | } | |
434 | ||
1d41ed0a VZ |
435 | void MyFrame::OnUpdateItem(wxCommandEvent& WXUNUSED(event)) |
436 | { | |
9ebb7cad VZ |
437 | if (GetMyBox()) |
438 | GetMyBox()->UpdateFirstItem(); | |
1d41ed0a VZ |
439 | } |
440 | ||
293b15f7 VZ |
441 | void MyFrame::OnGetItemRect(wxCommandEvent& WXUNUSED(event)) |
442 | { | |
443 | static const int ITEM = 10; | |
444 | const wxRect r = m_hlbox->GetItemRect(ITEM); | |
445 | wxLogMessage("Rect of item %d: (%d, %d)-(%d, %d)", | |
446 | ITEM, r.x, r.y, r.x + r.width, r.y + r.height); | |
447 | } | |
448 | ||
9a9b4940 VZ |
449 | void MyFrame::OnSetBgCol(wxCommandEvent& WXUNUSED(event)) |
450 | { | |
451 | wxColour col = wxGetColourFromUser(this, m_hlbox->GetBackgroundColour()); | |
a1b806b9 | 452 | if ( col.IsOk() ) |
9a9b4940 VZ |
453 | { |
454 | m_hlbox->SetBackgroundColour(col); | |
455 | m_hlbox->Refresh(); | |
456 | ||
8520f137 | 457 | #if wxUSE_STATUSBAR |
9a83f860 | 458 | SetStatusText(wxT("Background colour changed.")); |
8520f137 | 459 | #endif // wxUSE_STATUSBAR |
9a9b4940 VZ |
460 | } |
461 | } | |
462 | ||
463 | void MyFrame::OnSetSelBgCol(wxCommandEvent& WXUNUSED(event)) | |
464 | { | |
465 | wxColour col = wxGetColourFromUser(this, m_hlbox->GetSelectionBackground()); | |
a1b806b9 | 466 | if ( col.IsOk() ) |
9a9b4940 VZ |
467 | { |
468 | m_hlbox->SetSelectionBackground(col); | |
469 | m_hlbox->Refresh(); | |
470 | ||
8520f137 | 471 | #if wxUSE_STATUSBAR |
9a83f860 | 472 | SetStatusText(wxT("Selection background colour changed.")); |
8520f137 | 473 | #endif // wxUSE_STATUSBAR |
9a9b4940 VZ |
474 | } |
475 | } | |
476 | ||
477 | void MyFrame::OnSetSelFgCol(wxCommandEvent& event) | |
478 | { | |
9ebb7cad VZ |
479 | if (GetMyBox()) |
480 | { | |
481 | GetMyBox()->SetChangeSelFg(!event.IsChecked()); | |
482 | GetMyBox()->Refresh(); | |
483 | } | |
9a9b4940 VZ |
484 | } |
485 | ||
60f0cf4e VZ |
486 | void MyFrame::OnClear(wxCommandEvent& WXUNUSED(event)) |
487 | { | |
488 | m_hlbox->Clear(); | |
489 | } | |
490 | ||
a1c3cdc4 VS |
491 | void MyFrame::OnHtmlLinkClicked(wxHtmlLinkEvent &event) |
492 | { | |
493 | wxLogMessage(wxT("The url '%s' has been clicked!"), event.GetLinkInfo().GetHref().c_str()); | |
494 | ||
495 | if (GetMyBox()) | |
496 | { | |
497 | GetMyBox()->m_linkClicked = true; | |
e02c72fa | 498 | GetMyBox()->RefreshRow(1); |
a1c3cdc4 VS |
499 | } |
500 | } | |
501 | ||
502 | void MyFrame::OnHtmlCellHover(wxHtmlCellEvent &event) | |
503 | { | |
504 | wxLogMessage(wxT("Mouse moved over cell %p at %d;%d"), | |
505 | event.GetCell(), event.GetPoint().x, event.GetPoint().y); | |
506 | } | |
507 | ||
508 | void MyFrame::OnHtmlCellClicked(wxHtmlCellEvent &event) | |
509 | { | |
510 | wxLogMessage(wxT("Click over cell %p at %d;%d"), | |
511 | event.GetCell(), event.GetPoint().x, event.GetPoint().y); | |
512 | ||
513 | // if we don't skip the event, OnHtmlLinkClicked won't be called! | |
514 | event.Skip(); | |
515 | } | |
516 | ||
5f732810 VZ |
517 | // ---------------------------------------------------------------------------- |
518 | // listbox event handlers | |
519 | // ---------------------------------------------------------------------------- | |
520 | ||
521 | void MyFrame::OnLboxSelect(wxCommandEvent& event) | |
522 | { | |
9a83f860 | 523 | wxLogMessage(wxT("Listbox selection is now %d."), event.GetInt()); |
5f732810 VZ |
524 | |
525 | if ( m_hlbox->HasMultipleSelection() ) | |
526 | { | |
527 | wxString s; | |
528 | ||
529 | bool first = true; | |
530 | unsigned long cookie; | |
531 | for ( int item = m_hlbox->GetFirstSelected(cookie); | |
532 | item != wxNOT_FOUND; | |
533 | item = m_hlbox->GetNextSelected(cookie) ) | |
534 | { | |
535 | if ( first ) | |
536 | first = false; | |
537 | else | |
9a83f860 | 538 | s << wxT(", "); |
5f732810 VZ |
539 | |
540 | s << item; | |
541 | } | |
542 | ||
543 | if ( !s.empty() ) | |
43b2d5e7 | 544 | { |
9a83f860 | 545 | wxLogMessage(wxT("Selected items: %s"), s.c_str()); |
43b2d5e7 | 546 | } |
5f732810 VZ |
547 | } |
548 | ||
8520f137 | 549 | #if wxUSE_STATUSBAR |
5f732810 | 550 | SetStatusText(wxString::Format( |
9a83f860 | 551 | wxT("# items selected = %lu"), |
5f732810 VZ |
552 | (unsigned long)m_hlbox->GetSelectedCount() |
553 | )); | |
8520f137 | 554 | #endif // wxUSE_STATUSBAR |
5f732810 VZ |
555 | } |
556 | ||
557 | // ============================================================================ | |
558 | // MyHtmlListBox | |
559 | // ============================================================================ | |
560 | ||
9ebb7cad VZ |
561 | IMPLEMENT_DYNAMIC_CLASS(MyHtmlListBox, wxHtmlListBox) |
562 | ||
9a9b4940 | 563 | MyHtmlListBox::MyHtmlListBox(wxWindow *parent, bool multi) |
07850a49 | 564 | : wxHtmlListBox(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, |
9a9b4940 VZ |
565 | multi ? wxLB_MULTIPLE : 0) |
566 | { | |
567 | m_change = true; | |
1d41ed0a | 568 | m_firstItemUpdated = false; |
bc55e31b | 569 | m_linkClicked = false; |
1d41ed0a | 570 | |
9a9b4940 VZ |
571 | |
572 | SetMargins(5, 5); | |
573 | ||
574 | #ifdef USE_HTML_FILE | |
9a83f860 | 575 | if ( !m_file.Open(wxT("results")) ) |
9a9b4940 | 576 | { |
9a83f860 | 577 | wxLogError(wxT("Failed to open results file")); |
9a9b4940 VZ |
578 | } |
579 | else | |
580 | { | |
581 | SetItemCount(m_file.GetLineCount()); | |
582 | } | |
583 | #else | |
4636a1e8 | 584 | SetItemCount(1000); |
9a9b4940 VZ |
585 | #endif |
586 | ||
4636a1e8 | 587 | SetSelection(3); |
9a9b4940 VZ |
588 | } |
589 | ||
5f732810 VZ |
590 | void MyHtmlListBox::OnDrawSeparator(wxDC& dc, wxRect& rect, size_t) const |
591 | { | |
592 | if ( ((MyFrame *)GetParent())-> | |
593 | GetMenuBar()->IsChecked(HtmlLbox_DrawSeparator) ) | |
594 | { | |
595 | dc.SetPen(*wxBLACK_DASHED_PEN); | |
596 | dc.DrawLine(rect.x, rect.y, rect.GetRight(), rect.y); | |
597 | dc.DrawLine(rect.x, rect.GetBottom(), rect.GetRight(), rect.GetBottom()); | |
598 | } | |
599 | } | |
600 | ||
9a9b4940 VZ |
601 | wxString MyHtmlListBox::OnGetItem(size_t n) const |
602 | { | |
1d41ed0a VZ |
603 | if ( !n && m_firstItemUpdated ) |
604 | { | |
9a83f860 | 605 | return wxT("<h1><b>Just updated</b></h1>"); |
1d41ed0a VZ |
606 | } |
607 | ||
9a9b4940 VZ |
608 | #ifdef USE_HTML_FILE |
609 | wxString s; | |
610 | if ( m_file.IsOpened() ) | |
611 | s = m_file[n]; | |
612 | ||
613 | return s; | |
614 | #else | |
615 | int level = n % 6 + 1; | |
6bdcf0b1 | 616 | |
d924939b WS |
617 | wxColour clr((unsigned char)(abs((int)n - 192) % 256), |
618 | (unsigned char)(abs((int)n - 256) % 256), | |
619 | (unsigned char)(abs((int)n - 128) % 256)); | |
6bdcf0b1 | 620 | |
9a83f860 VZ |
621 | wxString label = wxString::Format(wxT("<h%d><font color=%s>") |
622 | wxT("Item</font> <b>%lu</b>") | |
623 | wxT("</h%d>"), | |
bc55e31b | 624 | level, |
6bdcf0b1 | 625 | clr.GetAsString(wxC2S_HTML_SYNTAX).c_str(), |
bc55e31b VS |
626 | (unsigned long)n, level); |
627 | if ( n == 1 ) | |
628 | { | |
629 | if ( !m_linkClicked ) | |
9a83f860 | 630 | label += wxT("<a href='1'>Click here...</a>"); |
bc55e31b | 631 | else |
9a83f860 | 632 | label += wxT("<font color='#9999ff'>Clicked here...</font>"); |
bc55e31b VS |
633 | } |
634 | ||
635 | return label; | |
9a9b4940 VZ |
636 | #endif |
637 | } | |
638 | ||
639 | wxColour MyHtmlListBox::GetSelectedTextColour(const wxColour& colFg) const | |
640 | { | |
641 | return m_change ? wxHtmlListBox::GetSelectedTextColour(colFg) : colFg; | |
642 | } | |
643 | ||
1d41ed0a VZ |
644 | void MyHtmlListBox::UpdateFirstItem() |
645 | { | |
646 | m_firstItemUpdated = !m_firstItemUpdated; | |
647 | ||
e02c72fa | 648 | RefreshRow(0); |
1d41ed0a | 649 | } |