Final fix from '[ 1473731 ] wxColourBase and wxString <-> wxColour implementation'.
[wxWidgets.git] / samples / htlbox / htlbox.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: htmllbox.cpp
3 // Purpose: HtmlLbox wxWidgets sample
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 31.05.03
7 // RCS-ID: $Id$
8 // Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwidgets.org>
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
27 // for all others, include the necessary headers
28 #ifndef WX_PRECOMP
29 #include "wx/app.h"
30 #include "wx/frame.h"
31 #include "wx/log.h"
32 #include "wx/textdlg.h"
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"
40 #include "wx/icon.h"
41 #endif
42
43 #include "wx/colordlg.h"
44 #include "wx/numdlg.h"
45
46 #include "wx/htmllbox.h"
47
48 // you can also have a file containing HTML strings for testing, enable this if
49 // you want to use it
50 //#define USE_HTML_FILE
51 #ifdef USE_HTML_FILE
52 #include "wx/textfile.h"
53 #endif
54
55 #include "../sample.xpm"
56
57 // ----------------------------------------------------------------------------
58 // private classes
59 // ----------------------------------------------------------------------------
60
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:
66 MyHtmlListBox(wxWindow *parent, bool multi = false);
67
68 void SetChangeSelFg(bool change) { m_change = change; }
69 void UpdateFirstItem();
70
71 protected:
72 // override this method to return data to be shown in the listbox (this is
73 // mandatory)
74 virtual wxString OnGetItem(size_t n) const;
75
76 // change the appearance by overriding these functions (this is optional)
77 virtual void OnDrawSeparator(wxDC& dc, wxRect& rect, size_t n) const;
78 virtual wxColour GetSelectedTextColour(const wxColour& colFg) const;
79
80 // override this method to handle mouse clicks
81 virtual void OnLinkClicked(size_t n, const wxHtmlLinkInfo& link);
82
83 // flag telling us whether we should use fg colour even for the selected
84 // item
85 bool m_change;
86
87 // flag which we toggle to update the first items text in OnGetItem()
88 bool m_firstItemUpdated;
89
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;
93
94
95 #ifdef USE_HTML_FILE
96 wxTextFile m_file;
97 #endif
98
99 DECLARE_NO_COPY_CLASS(MyHtmlListBox)
100 };
101
102 class MyFrame : public wxFrame
103 {
104 public:
105 MyFrame();
106 virtual ~MyFrame();
107
108 // event handlers
109 void OnQuit(wxCommandEvent& event);
110 void OnAbout(wxCommandEvent& event);
111
112 void OnSetMargins(wxCommandEvent& event);
113 void OnDrawSeparator(wxCommandEvent&) { m_hlbox->RefreshAll(); }
114 void OnToggleMulti(wxCommandEvent& event);
115 void OnSelectAll(wxCommandEvent& event);
116 void OnUpdateItem(wxCommandEvent& event);
117
118 void OnSetBgCol(wxCommandEvent& event);
119 void OnSetSelBgCol(wxCommandEvent& event);
120 void OnSetSelFgCol(wxCommandEvent& event);
121
122
123 void OnUpdateUISelectAll(wxUpdateUIEvent& event);
124
125 void OnLboxSelect(wxCommandEvent& event);
126 void OnLboxDClick(wxCommandEvent& event)
127 {
128 wxLogMessage(_T("Listbox item %ld double clicked."), event.GetInt());
129 }
130
131 private:
132 MyHtmlListBox *m_hlbox;
133
134 // any class wishing to process wxWidgets events must use this macro
135 DECLARE_EVENT_TABLE()
136 };
137
138 class MyApp : public wxApp
139 {
140 public:
141 virtual bool OnInit() { (new MyFrame())->Show(); return true; }
142 };
143
144 // ----------------------------------------------------------------------------
145 // constants
146 // ----------------------------------------------------------------------------
147
148 // IDs for the controls and the menu commands
149 enum
150 {
151 // menu items
152 HtmlLbox_Quit = 1,
153
154 HtmlLbox_SetMargins,
155 HtmlLbox_DrawSeparator,
156 HtmlLbox_ToggleMulti,
157 HtmlLbox_SelectAll,
158 HtmlLbox_UpdateItem,
159
160 HtmlLbox_SetBgCol,
161 HtmlLbox_SetSelBgCol,
162 HtmlLbox_SetSelFgCol,
163
164 // it is important for the id corresponding to the "About" command to have
165 // this standard value as otherwise it won't be handled properly under Mac
166 // (where it is special and put into the "Apple" menu)
167 HtmlLbox_About = wxID_ABOUT
168 };
169
170 // ----------------------------------------------------------------------------
171 // event tables and other macros for wxWidgets
172 // ----------------------------------------------------------------------------
173
174 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
175 EVT_MENU(HtmlLbox_Quit, MyFrame::OnQuit)
176
177 EVT_MENU(HtmlLbox_SetMargins, MyFrame::OnSetMargins)
178 EVT_MENU(HtmlLbox_DrawSeparator, MyFrame::OnDrawSeparator)
179 EVT_MENU(HtmlLbox_ToggleMulti, MyFrame::OnToggleMulti)
180 EVT_MENU(HtmlLbox_SelectAll, MyFrame::OnSelectAll)
181 EVT_MENU(HtmlLbox_UpdateItem, MyFrame::OnUpdateItem)
182
183 EVT_MENU(HtmlLbox_About, MyFrame::OnAbout)
184
185 EVT_MENU(HtmlLbox_SetBgCol, MyFrame::OnSetBgCol)
186 EVT_MENU(HtmlLbox_SetSelBgCol, MyFrame::OnSetSelBgCol)
187 EVT_MENU(HtmlLbox_SetSelFgCol, MyFrame::OnSetSelFgCol)
188
189 EVT_UPDATE_UI(HtmlLbox_SelectAll, MyFrame::OnUpdateUISelectAll)
190
191
192 EVT_LISTBOX(wxID_ANY, MyFrame::OnLboxSelect)
193 EVT_LISTBOX_DCLICK(wxID_ANY, MyFrame::OnLboxDClick)
194 END_EVENT_TABLE()
195
196 IMPLEMENT_APP(MyApp)
197
198 // ============================================================================
199 // MyFrame
200 // ============================================================================
201
202 // ----------------------------------------------------------------------------
203 // MyFrame ctor/dtor
204 // ----------------------------------------------------------------------------
205
206 // frame constructor
207 MyFrame::MyFrame()
208 : wxFrame(NULL, wxID_ANY, _T("HtmlLbox wxWidgets Sample"),
209 wxDefaultPosition, wxSize(400, 500))
210 {
211 // set the frame icon
212 SetIcon(wxIcon(sample_xpm));
213
214 #if wxUSE_MENUS
215 // create a menu bar
216 wxMenu *menuFile = new wxMenu;
217 menuFile->Append(HtmlLbox_Quit, _T("E&xit\tAlt-X"), _T("Quit this program"));
218
219 // create our specific menu
220 wxMenu *menuHLbox = new wxMenu;
221 menuHLbox->Append(HtmlLbox_SetMargins,
222 _T("Set &margins...\tCtrl-G"),
223 _T("Change the margins around the items"));
224 menuHLbox->AppendCheckItem(HtmlLbox_DrawSeparator,
225 _T("&Draw separators\tCtrl-D"),
226 _T("Toggle drawing separators between cells"));
227 menuHLbox->AppendSeparator();
228 menuHLbox->AppendCheckItem(HtmlLbox_ToggleMulti,
229 _T("&Multiple selection\tCtrl-M"),
230 _T("Toggle multiple selection on/off"));
231 menuHLbox->AppendSeparator();
232 menuHLbox->Append(HtmlLbox_SelectAll, _T("Select &all items\tCtrl-A"));
233 menuHLbox->Append(HtmlLbox_UpdateItem, _T("Update &first item\tCtrl-U"));
234 menuHLbox->AppendSeparator();
235 menuHLbox->Append(HtmlLbox_SetBgCol, _T("Set &background...\tCtrl-B"));
236 menuHLbox->Append(HtmlLbox_SetSelBgCol,
237 _T("Set &selection background...\tCtrl-S"));
238 menuHLbox->AppendCheckItem(HtmlLbox_SetSelFgCol,
239 _T("Keep &foreground in selection\tCtrl-F"));
240
241 // the "About" item should be in the help menu
242 wxMenu *helpMenu = new wxMenu;
243 helpMenu->Append(HtmlLbox_About, _T("&About...\tF1"), _T("Show about dialog"));
244
245 // now append the freshly created menu to the menu bar...
246 wxMenuBar *menuBar = new wxMenuBar();
247 menuBar->Append(menuFile, _T("&File"));
248 menuBar->Append(menuHLbox, _T("&Listbox"));
249 menuBar->Append(helpMenu, _T("&Help"));
250
251 menuBar->Check(HtmlLbox_DrawSeparator, true);
252
253 // ... and attach this menu bar to the frame
254 SetMenuBar(menuBar);
255 #endif // wxUSE_MENUS
256
257 #if wxUSE_STATUSBAR
258 // create a status bar just for fun (by default with 1 pane only)
259 CreateStatusBar(2);
260 SetStatusText(_T("Welcome to wxWidgets!"));
261 #endif // wxUSE_STATUSBAR
262
263 // create the child controls
264 m_hlbox = new MyHtmlListBox(this);
265 wxTextCtrl *text = new wxTextCtrl(this, wxID_ANY, _T(""),
266 wxDefaultPosition, wxDefaultSize,
267 wxTE_MULTILINE);
268 delete wxLog::SetActiveTarget(new wxLogTextCtrl(text));
269
270 // and lay them out
271 wxSizer *sizer = new wxBoxSizer(wxHORIZONTAL);
272 sizer->Add(m_hlbox, 1, wxGROW);
273 sizer->Add(text, 1, wxGROW);
274
275 SetSizer(sizer);
276 }
277
278 MyFrame::~MyFrame()
279 {
280 delete wxLog::SetActiveTarget(NULL);
281 }
282
283 // ----------------------------------------------------------------------------
284 // menu event handlers
285 // ----------------------------------------------------------------------------
286
287 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
288 {
289 // true is to force the frame to close
290 Close(true);
291 }
292
293 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
294 {
295 wxMessageBox(_T("This sample shows wxHtmlListBox class.\n")
296 _T("\n")
297 _T("(c) 2003 Vadim Zeitlin"),
298 _T("About HtmlLbox"),
299 wxOK | wxICON_INFORMATION,
300 this);
301 }
302
303 void MyFrame::OnSetMargins(wxCommandEvent& WXUNUSED(event))
304 {
305 long margin = wxGetNumberFromUser
306 (
307 _T("Enter the margins to use for the listbox items."),
308 _T("Margin: "),
309 _T("HtmlLbox: Set the margins"),
310 0, 0, 20,
311 this
312 );
313
314 if ( margin != -1 )
315 {
316 m_hlbox->SetMargins(margin, margin);
317 m_hlbox->RefreshAll();
318 }
319 }
320
321 void MyFrame::OnToggleMulti(wxCommandEvent& event)
322 {
323 // we need to recreate the listbox
324 wxSizer *sizer = GetSizer();
325 sizer->Detach(m_hlbox);
326 delete m_hlbox;
327
328 m_hlbox = new MyHtmlListBox(this, event.IsChecked());
329 sizer->Prepend(m_hlbox, 1, wxGROW);
330
331 sizer->Layout();
332 }
333
334 void MyFrame::OnSelectAll(wxCommandEvent& WXUNUSED(event))
335 {
336 m_hlbox->SelectAll();
337 }
338
339 void MyFrame::OnUpdateUISelectAll(wxUpdateUIEvent& event)
340 {
341 event.Enable( m_hlbox && m_hlbox->HasMultipleSelection() );
342 }
343
344 void MyFrame::OnUpdateItem(wxCommandEvent& WXUNUSED(event))
345 {
346 m_hlbox->UpdateFirstItem();
347 }
348
349 void MyFrame::OnSetBgCol(wxCommandEvent& WXUNUSED(event))
350 {
351 wxColour col = wxGetColourFromUser(this, m_hlbox->GetBackgroundColour());
352 if ( col.Ok() )
353 {
354 m_hlbox->SetBackgroundColour(col);
355 m_hlbox->Refresh();
356
357 #if wxUSE_STATUSBAR
358 SetStatusText(_T("Background colour changed."));
359 #endif // wxUSE_STATUSBAR
360 }
361 }
362
363 void MyFrame::OnSetSelBgCol(wxCommandEvent& WXUNUSED(event))
364 {
365 wxColour col = wxGetColourFromUser(this, m_hlbox->GetSelectionBackground());
366 if ( col.Ok() )
367 {
368 m_hlbox->SetSelectionBackground(col);
369 m_hlbox->Refresh();
370
371 #if wxUSE_STATUSBAR
372 SetStatusText(_T("Selection background colour changed."));
373 #endif // wxUSE_STATUSBAR
374 }
375 }
376
377 void MyFrame::OnSetSelFgCol(wxCommandEvent& event)
378 {
379 m_hlbox->SetChangeSelFg(!event.IsChecked());
380 m_hlbox->Refresh();
381 }
382
383 // ----------------------------------------------------------------------------
384 // listbox event handlers
385 // ----------------------------------------------------------------------------
386
387 void MyFrame::OnLboxSelect(wxCommandEvent& event)
388 {
389 wxLogMessage(_T("Listbox selection is now %ld."), event.GetInt());
390
391 if ( m_hlbox->HasMultipleSelection() )
392 {
393 wxString s;
394
395 bool first = true;
396 unsigned long cookie;
397 for ( int item = m_hlbox->GetFirstSelected(cookie);
398 item != wxNOT_FOUND;
399 item = m_hlbox->GetNextSelected(cookie) )
400 {
401 if ( first )
402 first = false;
403 else
404 s << _T(", ");
405
406 s << item;
407 }
408
409 if ( !s.empty() )
410 wxLogMessage(_T("Selected items: %s"), s.c_str());
411 }
412
413 #if wxUSE_STATUSBAR
414 SetStatusText(wxString::Format(
415 _T("# items selected = %lu"),
416 (unsigned long)m_hlbox->GetSelectedCount()
417 ));
418 #endif // wxUSE_STATUSBAR
419 }
420
421 // ============================================================================
422 // MyHtmlListBox
423 // ============================================================================
424
425 MyHtmlListBox::MyHtmlListBox(wxWindow *parent, bool multi)
426 : wxHtmlListBox(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize,
427 multi ? wxLB_MULTIPLE : 0)
428 {
429 m_change = true;
430 m_firstItemUpdated = false;
431 m_linkClicked = false;
432
433
434 SetMargins(5, 5);
435
436 #ifdef USE_HTML_FILE
437 if ( !m_file.Open(_T("results")) )
438 {
439 wxLogError(_T("Failed to open results file"));
440 }
441 else
442 {
443 SetItemCount(m_file.GetLineCount());
444 }
445 #else
446 SetItemCount(1000);
447 #endif
448
449 SetSelection(3);
450 }
451
452 void MyHtmlListBox::OnDrawSeparator(wxDC& dc, wxRect& rect, size_t) const
453 {
454 if ( ((MyFrame *)GetParent())->
455 GetMenuBar()->IsChecked(HtmlLbox_DrawSeparator) )
456 {
457 dc.SetPen(*wxBLACK_DASHED_PEN);
458 dc.DrawLine(rect.x, rect.y, rect.GetRight(), rect.y);
459 dc.DrawLine(rect.x, rect.GetBottom(), rect.GetRight(), rect.GetBottom());
460 }
461 }
462
463 wxString MyHtmlListBox::OnGetItem(size_t n) const
464 {
465 if ( !n && m_firstItemUpdated )
466 {
467 return wxString::Format(_T("<h1><b>Just updated</b></h1>"));
468 }
469
470 #ifdef USE_HTML_FILE
471 wxString s;
472 if ( m_file.IsOpened() )
473 s = m_file[n];
474
475 return s;
476 #else
477 int level = n % 6 + 1;
478
479 wxColour clr(abs((int)n - 192) % 256,
480 abs((int)n - 256) % 256,
481 abs((int)n - 128) % 256);
482
483 wxString label = wxString::Format(_T("<h%d><font color=%s>")
484 _T("Item</font> <b>%lu</b>")
485 _T("</h%d>"),
486 level,
487 clr.GetAsString(wxC2S_HTML_SYNTAX).c_str(),
488 (unsigned long)n, level);
489 if ( n == 1 )
490 {
491 if ( !m_linkClicked )
492 label += _T("<a href='1'>Click here...</a>");
493 else
494 label += _T("<font color='#9999ff'>Clicked here...</font>");
495 }
496
497 return label;
498 #endif
499 }
500
501 wxColour MyHtmlListBox::GetSelectedTextColour(const wxColour& colFg) const
502 {
503 return m_change ? wxHtmlListBox::GetSelectedTextColour(colFg) : colFg;
504 }
505
506 void MyHtmlListBox::UpdateFirstItem()
507 {
508 m_firstItemUpdated = !m_firstItemUpdated;
509
510 RefreshLine(0);
511 }
512
513 void MyHtmlListBox::OnLinkClicked(size_t WXUNUSED(n),
514 const wxHtmlLinkInfo& WXUNUSED(link))
515 {
516 m_linkClicked = true;
517
518 RefreshLine(1);
519 }