]> git.saurik.com Git - wxWidgets.git/blob - samples/htlbox/htlbox.cpp
Don't generate any events from wxSpinCtrl and wxSpinCtrlDouble methods.
[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 // Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwidgets.org>
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
26 // for all others, include the necessary headers
27 #ifndef WX_PRECOMP
28 #include "wx/app.h"
29 #include "wx/frame.h"
30 #include "wx/log.h"
31 #include "wx/textdlg.h"
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"
39 #include "wx/icon.h"
40 #endif
41
42 #include "wx/colordlg.h"
43 #include "wx/numdlg.h"
44
45 #include "wx/htmllbox.h"
46
47 // you can also have a file containing HTML strings for testing, enable this if
48 // you want to use it
49 //#define USE_HTML_FILE
50 #ifdef USE_HTML_FILE
51 #include "wx/textfile.h"
52 #endif
53
54 #include "../sample.xpm"
55
56 // ----------------------------------------------------------------------------
57 // private classes
58 // ----------------------------------------------------------------------------
59
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:
65 MyHtmlListBox() { }
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 // flag telling us whether we should use fg colour even for the selected
81 // item
82 bool m_change;
83
84 // flag which we toggle to update the first items text in OnGetItem()
85 bool m_firstItemUpdated;
86
87 public:
88
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;
92
93 #ifdef USE_HTML_FILE
94 wxTextFile m_file;
95 #endif
96
97 wxDECLARE_NO_COPY_CLASS(MyHtmlListBox);
98 DECLARE_DYNAMIC_CLASS(MyHtmlListBox)
99 };
100
101
102 class MyFrame : public wxFrame
103 {
104 public:
105 MyFrame();
106 virtual ~MyFrame();
107
108 // event handlers
109 void OnSimpleOrCustomBox(wxCommandEvent& event);
110 void OnQuit(wxCommandEvent& event);
111 void OnAbout(wxCommandEvent& event);
112
113 void OnSetMargins(wxCommandEvent& event);
114 void OnDrawSeparator(wxCommandEvent&) { m_hlbox->RefreshAll(); }
115 void OnToggleMulti(wxCommandEvent& event);
116 void OnSelectAll(wxCommandEvent& event);
117 void OnUpdateItem(wxCommandEvent& event);
118 void OnGetItemRect(wxCommandEvent& event);
119
120 void OnSetBgCol(wxCommandEvent& event);
121 void OnSetSelBgCol(wxCommandEvent& event);
122 void OnSetSelFgCol(wxCommandEvent& event);
123
124 void OnClear(wxCommandEvent& event);
125
126 void OnUpdateUISelectAll(wxUpdateUIEvent& event);
127
128 void OnLboxSelect(wxCommandEvent& event);
129 void OnLboxDClick(wxCommandEvent& event)
130 {
131 wxLogMessage(wxT("Listbox item %d double clicked."), event.GetInt());
132 }
133
134 void OnHtmlLinkClicked(wxHtmlLinkEvent& event);
135 void OnHtmlCellHover(wxHtmlCellEvent &event);
136 void OnHtmlCellClicked(wxHtmlCellEvent &event);
137
138 wxSimpleHtmlListBox *GetSimpleBox()
139 { return wxDynamicCast(m_hlbox, wxSimpleHtmlListBox); }
140 MyHtmlListBox *GetMyBox()
141 { return wxDynamicCast(m_hlbox, MyHtmlListBox); }
142
143 void CreateBox();
144
145 private:
146 wxHtmlListBox *m_hlbox;
147
148 // any class wishing to process wxWidgets events must use this macro
149 DECLARE_EVENT_TABLE()
150 };
151
152 class MyApp : public wxApp
153 {
154 public:
155 virtual bool OnInit() { (new MyFrame())->Show(); return true; }
156 };
157
158 // ----------------------------------------------------------------------------
159 // constants
160 // ----------------------------------------------------------------------------
161
162 // IDs for the controls and the menu commands
163 enum
164 {
165 // menu items
166 HtmlLbox_CustomBox = 1,
167 HtmlLbox_SimpleBox,
168 HtmlLbox_Quit,
169
170 HtmlLbox_SetMargins,
171 HtmlLbox_DrawSeparator,
172 HtmlLbox_ToggleMulti,
173 HtmlLbox_SelectAll,
174 HtmlLbox_UpdateItem,
175 HtmlLbox_GetItemRect,
176
177 HtmlLbox_SetBgCol,
178 HtmlLbox_SetSelBgCol,
179 HtmlLbox_SetSelFgCol,
180
181 HtmlLbox_Clear,
182
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 // ----------------------------------------------------------------------------
190 // event tables and other macros for wxWidgets
191 // ----------------------------------------------------------------------------
192
193 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
194 EVT_MENU(HtmlLbox_CustomBox, MyFrame::OnSimpleOrCustomBox)
195 EVT_MENU(HtmlLbox_SimpleBox, MyFrame::OnSimpleOrCustomBox)
196 EVT_MENU(HtmlLbox_Quit, MyFrame::OnQuit)
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)
202 EVT_MENU(HtmlLbox_UpdateItem, MyFrame::OnUpdateItem)
203 EVT_MENU(HtmlLbox_GetItemRect, MyFrame::OnGetItemRect)
204
205 EVT_MENU(HtmlLbox_About, MyFrame::OnAbout)
206
207 EVT_MENU(HtmlLbox_SetBgCol, MyFrame::OnSetBgCol)
208 EVT_MENU(HtmlLbox_SetSelBgCol, MyFrame::OnSetSelBgCol)
209 EVT_MENU(HtmlLbox_SetSelFgCol, MyFrame::OnSetSelFgCol)
210
211 EVT_MENU(HtmlLbox_Clear, MyFrame::OnClear)
212
213 EVT_UPDATE_UI(HtmlLbox_SelectAll, MyFrame::OnUpdateUISelectAll)
214
215
216 EVT_LISTBOX(wxID_ANY, MyFrame::OnLboxSelect)
217 EVT_LISTBOX_DCLICK(wxID_ANY, MyFrame::OnLboxDClick)
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
225 END_EVENT_TABLE()
226
227 IMPLEMENT_APP(MyApp)
228
229 // ============================================================================
230 // MyFrame
231 // ============================================================================
232
233 // ----------------------------------------------------------------------------
234 // MyFrame ctor/dtor
235 // ----------------------------------------------------------------------------
236
237 // frame constructor
238 MyFrame::MyFrame()
239 : wxFrame(NULL, wxID_ANY, wxT("HtmlLbox wxWidgets Sample"),
240 wxDefaultPosition, wxSize(500, 500))
241 {
242 // set the frame icon
243 SetIcon(wxICON(sample));
244
245 #if wxUSE_MENUS
246 // create a menu bar
247 wxMenu *menuFile = new wxMenu;
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"));
252 menuFile->AppendSeparator();
253 menuFile->Append(HtmlLbox_Quit, wxT("E&xit\tAlt-X"), wxT("Quit this program"));
254
255 // create our specific menu
256 wxMenu *menuHLbox = new wxMenu;
257 menuHLbox->Append(HtmlLbox_SetMargins,
258 wxT("Set &margins...\tCtrl-G"),
259 wxT("Change the margins around the items"));
260 menuHLbox->AppendCheckItem(HtmlLbox_DrawSeparator,
261 wxT("&Draw separators\tCtrl-D"),
262 wxT("Toggle drawing separators between cells"));
263 menuHLbox->AppendSeparator();
264 menuHLbox->AppendCheckItem(HtmlLbox_ToggleMulti,
265 wxT("&Multiple selection\tCtrl-M"),
266 wxT("Toggle multiple selection on/off"));
267 menuHLbox->AppendSeparator();
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"));
271 menuHLbox->AppendSeparator();
272 menuHLbox->Append(HtmlLbox_SetBgCol, wxT("Set &background...\tCtrl-B"));
273 menuHLbox->Append(HtmlLbox_SetSelBgCol,
274 wxT("Set &selection background...\tCtrl-S"));
275 menuHLbox->AppendCheckItem(HtmlLbox_SetSelFgCol,
276 wxT("Keep &foreground in selection\tCtrl-F"));
277
278 menuHLbox->AppendSeparator();
279 menuHLbox->Append(HtmlLbox_Clear, wxT("&Clear\tCtrl-L"));
280
281 // the "About" item should be in the help menu
282 wxMenu *helpMenu = new wxMenu;
283 helpMenu->Append(HtmlLbox_About, wxT("&About\tF1"), wxT("Show about dialog"));
284
285 // now append the freshly created menu to the menu bar...
286 wxMenuBar *menuBar = new wxMenuBar();
287 menuBar->Append(menuFile, wxT("&File"));
288 menuBar->Append(menuHLbox, wxT("&Listbox"));
289 menuBar->Append(helpMenu, wxT("&Help"));
290
291 menuBar->Check(HtmlLbox_DrawSeparator, true);
292
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);
300 SetStatusText(wxT("Welcome to wxWidgets!"));
301 #endif // wxUSE_STATUSBAR
302
303 // create the child controls
304 CreateBox();
305 wxTextCtrl *text = new wxTextCtrl(this, wxID_ANY, wxT(""),
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);
312 sizer->Add(m_hlbox, 2, wxGROW);
313 sizer->Add(text, 3, wxGROW);
314
315 SetSizer(sizer);
316 }
317
318 MyFrame::~MyFrame()
319 {
320 delete wxLog::SetActiveTarget(NULL);
321 }
322
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);
335
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
347 wxString label = wxString::Format(wxT("<h%d><font color=%s>")
348 wxT("Item</font> <b>%lu</b>")
349 wxT("</h%d>"),
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
361 // ----------------------------------------------------------------------------
362 // menu event handlers
363 // ----------------------------------------------------------------------------
364
365 void MyFrame::OnSimpleOrCustomBox(wxCommandEvent& WXUNUSED(event))
366 {
367 wxWindow *old = m_hlbox;
368
369 // we need to recreate the listbox
370 CreateBox();
371 GetSizer()->Replace(old, m_hlbox);
372 delete old;
373
374 GetSizer()->Layout();
375 Refresh();
376 }
377
378 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
379 {
380 // true is to force the frame to close
381 Close(true);
382 }
383
384 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
385 {
386 wxMessageBox(wxT("This sample shows wxHtmlListBox class.\n")
387 wxT("\n")
388 wxT("(c) 2003 Vadim Zeitlin"),
389 wxT("About HtmlLbox"),
390 wxOK | wxICON_INFORMATION,
391 this);
392 }
393
394 void MyFrame::OnSetMargins(wxCommandEvent& WXUNUSED(event))
395 {
396 long margin = wxGetNumberFromUser
397 (
398 wxT("Enter the margins to use for the listbox items."),
399 wxT("Margin: "),
400 wxT("HtmlLbox: Set the margins"),
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
412 void MyFrame::OnToggleMulti(wxCommandEvent& WXUNUSED(event))
413 {
414 wxWindow *old = m_hlbox;
415
416 // we need to recreate the listbox
417 CreateBox();
418 GetSizer()->Replace(old, m_hlbox);
419 delete old;
420
421 GetSizer()->Layout();
422 }
423
424 void MyFrame::OnSelectAll(wxCommandEvent& WXUNUSED(event))
425 {
426 m_hlbox->SelectAll();
427 }
428
429 void MyFrame::OnUpdateUISelectAll(wxUpdateUIEvent& event)
430 {
431 event.Enable( m_hlbox && m_hlbox->HasMultipleSelection() );
432 }
433
434 void MyFrame::OnUpdateItem(wxCommandEvent& WXUNUSED(event))
435 {
436 if (GetMyBox())
437 GetMyBox()->UpdateFirstItem();
438 }
439
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
448 void MyFrame::OnSetBgCol(wxCommandEvent& WXUNUSED(event))
449 {
450 wxColour col = wxGetColourFromUser(this, m_hlbox->GetBackgroundColour());
451 if ( col.IsOk() )
452 {
453 m_hlbox->SetBackgroundColour(col);
454 m_hlbox->Refresh();
455
456 #if wxUSE_STATUSBAR
457 SetStatusText(wxT("Background colour changed."));
458 #endif // wxUSE_STATUSBAR
459 }
460 }
461
462 void MyFrame::OnSetSelBgCol(wxCommandEvent& WXUNUSED(event))
463 {
464 wxColour col = wxGetColourFromUser(this, m_hlbox->GetSelectionBackground());
465 if ( col.IsOk() )
466 {
467 m_hlbox->SetSelectionBackground(col);
468 m_hlbox->Refresh();
469
470 #if wxUSE_STATUSBAR
471 SetStatusText(wxT("Selection background colour changed."));
472 #endif // wxUSE_STATUSBAR
473 }
474 }
475
476 void MyFrame::OnSetSelFgCol(wxCommandEvent& event)
477 {
478 if (GetMyBox())
479 {
480 GetMyBox()->SetChangeSelFg(!event.IsChecked());
481 GetMyBox()->Refresh();
482 }
483 }
484
485 void MyFrame::OnClear(wxCommandEvent& WXUNUSED(event))
486 {
487 m_hlbox->Clear();
488 }
489
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;
497 GetMyBox()->RefreshRow(1);
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
516 // ----------------------------------------------------------------------------
517 // listbox event handlers
518 // ----------------------------------------------------------------------------
519
520 void MyFrame::OnLboxSelect(wxCommandEvent& event)
521 {
522 wxLogMessage(wxT("Listbox selection is now %d."), event.GetInt());
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
537 s << wxT(", ");
538
539 s << item;
540 }
541
542 if ( !s.empty() )
543 {
544 wxLogMessage(wxT("Selected items: %s"), s.c_str());
545 }
546 }
547
548 #if wxUSE_STATUSBAR
549 SetStatusText(wxString::Format(
550 wxT("# items selected = %lu"),
551 (unsigned long)m_hlbox->GetSelectedCount()
552 ));
553 #endif // wxUSE_STATUSBAR
554 }
555
556 // ============================================================================
557 // MyHtmlListBox
558 // ============================================================================
559
560 IMPLEMENT_DYNAMIC_CLASS(MyHtmlListBox, wxHtmlListBox)
561
562 MyHtmlListBox::MyHtmlListBox(wxWindow *parent, bool multi)
563 : wxHtmlListBox(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize,
564 multi ? wxLB_MULTIPLE : 0)
565 {
566 m_change = true;
567 m_firstItemUpdated = false;
568 m_linkClicked = false;
569
570
571 SetMargins(5, 5);
572
573 #ifdef USE_HTML_FILE
574 if ( !m_file.Open(wxT("results")) )
575 {
576 wxLogError(wxT("Failed to open results file"));
577 }
578 else
579 {
580 SetItemCount(m_file.GetLineCount());
581 }
582 #else
583 SetItemCount(1000);
584 #endif
585
586 SetSelection(3);
587 }
588
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
600 wxString MyHtmlListBox::OnGetItem(size_t n) const
601 {
602 if ( !n && m_firstItemUpdated )
603 {
604 return wxT("<h1><b>Just updated</b></h1>");
605 }
606
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;
615
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));
619
620 wxString label = wxString::Format(wxT("<h%d><font color=%s>")
621 wxT("Item</font> <b>%lu</b>")
622 wxT("</h%d>"),
623 level,
624 clr.GetAsString(wxC2S_HTML_SYNTAX).c_str(),
625 (unsigned long)n, level);
626 if ( n == 1 )
627 {
628 if ( !m_linkClicked )
629 label += wxT("<a href='1'>Click here...</a>");
630 else
631 label += wxT("<font color='#9999ff'>Clicked here...</font>");
632 }
633
634 return label;
635 #endif
636 }
637
638 wxColour MyHtmlListBox::GetSelectedTextColour(const wxColour& colFg) const
639 {
640 return m_change ? wxHtmlListBox::GetSelectedTextColour(colFg) : colFg;
641 }
642
643 void MyHtmlListBox::UpdateFirstItem()
644 {
645 m_firstItemUpdated = !m_firstItemUpdated;
646
647 RefreshRow(0);
648 }