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