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