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