]> git.saurik.com Git - wxWidgets.git/blame - samples/htlbox/htlbox.cpp
Compile fix for VC
[wxWidgets.git] / samples / htlbox / htlbox.cpp
CommitLineData
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
7// RCS-ID: $Id$
be5a51fb 8// Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwidgets.org>
201ca879
VZ
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
5f732810 27// for all others, include the necessary headers
201ca879
VZ
28#ifndef WX_PRECOMP
29 #include "wx/app.h"
30 #include "wx/frame.h"
31 #include "wx/log.h"
5f732810 32 #include "wx/textdlg.h"
6170c108
VZ
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"
ed269e7b 40 #include "wx/icon.h"
201ca879
VZ
41#endif
42
9a9b4940 43#include "wx/colordlg.h"
e4f3eb42 44#include "wx/numdlg.h"
9a9b4940 45
201ca879
VZ
46#include "wx/htmllbox.h"
47
5f732810
VZ
48// you can also have a file containing HTML strings for testing, enable this if
49// you want to use it
c2a331e0 50//#define USE_HTML_FILE
5f732810
VZ
51#ifdef USE_HTML_FILE
52 #include "wx/textfile.h"
53#endif
54
b6cf9ad0 55#include "../sample.xpm"
201ca879
VZ
56
57// ----------------------------------------------------------------------------
58// private classes
59// ----------------------------------------------------------------------------
60
201ca879
VZ
61// to use wxHtmlListBox you must derive a new class from it as you must
62// implement pure virtual OnGetItem()
63class MyHtmlListBox : public wxHtmlListBox
64{
65public:
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
71protected:
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
bc55e31b
VS
80 // override this method to handle mouse clicks
81 virtual void OnLinkClicked(size_t n, const wxHtmlLinkInfo& link);
1d41ed0a
VZ
82
83 // flag telling us whether we should use fg colour even for the selected
84 // item
9a9b4940
VZ
85 bool m_change;
86
1d41ed0a
VZ
87 // flag which we toggle to update the first items text in OnGetItem()
88 bool m_firstItemUpdated;
89
bc55e31b
VS
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;
1d41ed0a
VZ
93
94
9a9b4940 95#ifdef USE_HTML_FILE
5f732810 96 wxTextFile m_file;
9a9b4940 97#endif
27d0dcd0
VZ
98
99 DECLARE_NO_COPY_CLASS(MyHtmlListBox)
201ca879
VZ
100};
101
8613d47b
VZ
102class MyFrame : public wxFrame
103{
104public:
105 MyFrame();
106 virtual ~MyFrame();
107
108 // event handlers
109 void OnQuit(wxCommandEvent& event);
110 void OnAbout(wxCommandEvent& event);
111
5f732810
VZ
112 void OnSetMargins(wxCommandEvent& event);
113 void OnDrawSeparator(wxCommandEvent&) { m_hlbox->RefreshAll(); }
114 void OnToggleMulti(wxCommandEvent& event);
115 void OnSelectAll(wxCommandEvent& event);
1d41ed0a 116 void OnUpdateItem(wxCommandEvent& event);
8613d47b 117
9a9b4940
VZ
118 void OnSetBgCol(wxCommandEvent& event);
119 void OnSetSelBgCol(wxCommandEvent& event);
120 void OnSetSelFgCol(wxCommandEvent& event);
121
122
5f732810 123 void OnUpdateUISelectAll(wxUpdateUIEvent& event);
8613d47b 124
5f732810 125 void OnLboxSelect(wxCommandEvent& event);
8613d47b
VZ
126 void OnLboxDClick(wxCommandEvent& event)
127 {
128 wxLogMessage(_T("Listbox item %ld double clicked."), event.GetInt());
129 }
130
131private:
132 MyHtmlListBox *m_hlbox;
133
be5a51fb 134 // any class wishing to process wxWidgets events must use this macro
8613d47b
VZ
135 DECLARE_EVENT_TABLE()
136};
137
5f732810
VZ
138class MyApp : public wxApp
139{
140public:
07850a49 141 virtual bool OnInit() { (new MyFrame())->Show(); return true; }
5f732810
VZ
142};
143
201ca879
VZ
144// ----------------------------------------------------------------------------
145// constants
146// ----------------------------------------------------------------------------
147
148// IDs for the controls and the menu commands
149enum
150{
151 // menu items
152 HtmlLbox_Quit = 1,
153
5f732810
VZ
154 HtmlLbox_SetMargins,
155 HtmlLbox_DrawSeparator,
156 HtmlLbox_ToggleMulti,
157 HtmlLbox_SelectAll,
1d41ed0a 158 HtmlLbox_UpdateItem,
5f732810 159
9a9b4940
VZ
160 HtmlLbox_SetBgCol,
161 HtmlLbox_SetSelBgCol,
162 HtmlLbox_SetSelFgCol,
163
201ca879
VZ
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// ----------------------------------------------------------------------------
be5a51fb 171// event tables and other macros for wxWidgets
201ca879
VZ
172// ----------------------------------------------------------------------------
173
201ca879
VZ
174BEGIN_EVENT_TABLE(MyFrame, wxFrame)
175 EVT_MENU(HtmlLbox_Quit, MyFrame::OnQuit)
5f732810
VZ
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)
1d41ed0a 181 EVT_MENU(HtmlLbox_UpdateItem, MyFrame::OnUpdateItem)
5f732810 182
201ca879
VZ
183 EVT_MENU(HtmlLbox_About, MyFrame::OnAbout)
184
9a9b4940
VZ
185 EVT_MENU(HtmlLbox_SetBgCol, MyFrame::OnSetBgCol)
186 EVT_MENU(HtmlLbox_SetSelBgCol, MyFrame::OnSetSelBgCol)
187 EVT_MENU(HtmlLbox_SetSelFgCol, MyFrame::OnSetSelFgCol)
5f732810
VZ
188
189 EVT_UPDATE_UI(HtmlLbox_SelectAll, MyFrame::OnUpdateUISelectAll)
190
191
201ca879
VZ
192 EVT_LISTBOX(wxID_ANY, MyFrame::OnLboxSelect)
193 EVT_LISTBOX_DCLICK(wxID_ANY, MyFrame::OnLboxDClick)
194END_EVENT_TABLE()
195
201ca879
VZ
196IMPLEMENT_APP(MyApp)
197
198// ============================================================================
5f732810 199// MyFrame
201ca879
VZ
200// ============================================================================
201
202// ----------------------------------------------------------------------------
5f732810 203// MyFrame ctor/dtor
201ca879
VZ
204// ----------------------------------------------------------------------------
205
206// frame constructor
207MyFrame::MyFrame()
07850a49 208 : wxFrame(NULL, wxID_ANY, _T("HtmlLbox wxWidgets Sample"),
201ca879
VZ
209 wxDefaultPosition, wxSize(400, 500))
210{
211 // set the frame icon
b6cf9ad0 212 SetIcon(wxIcon(sample_xpm));
201ca879
VZ
213
214#if wxUSE_MENUS
215 // create a menu bar
216 wxMenu *menuFile = new wxMenu;
5f732810
VZ
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,
9a9b4940 225 _T("&Draw separators\tCtrl-D"),
5f732810
VZ
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"));
1d41ed0a 233 menuHLbox->Append(HtmlLbox_UpdateItem, _T("Update &first item\tCtrl-U"));
9a9b4940
VZ
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"));
201ca879
VZ
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
201ca879
VZ
245 // now append the freshly created menu to the menu bar...
246 wxMenuBar *menuBar = new wxMenuBar();
247 menuBar->Append(menuFile, _T("&File"));
5f732810 248 menuBar->Append(menuHLbox, _T("&Listbox"));
201ca879
VZ
249 menuBar->Append(helpMenu, _T("&Help"));
250
5f732810
VZ
251 menuBar->Check(HtmlLbox_DrawSeparator, true);
252
201ca879
VZ
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);
be5a51fb 260 SetStatusText(_T("Welcome to wxWidgets!"));
201ca879
VZ
261#endif // wxUSE_STATUSBAR
262
263 // create the child controls
5f732810 264 m_hlbox = new MyHtmlListBox(this);
07850a49 265 wxTextCtrl *text = new wxTextCtrl(this, wxID_ANY, _T(""),
201ca879
VZ
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);
8613d47b 272 sizer->Add(m_hlbox, 1, wxGROW);
201ca879
VZ
273 sizer->Add(text, 1, wxGROW);
274
275 SetSizer(sizer);
276}
277
278MyFrame::~MyFrame()
279{
280 delete wxLog::SetActiveTarget(NULL);
281}
282
5f732810
VZ
283// ----------------------------------------------------------------------------
284// menu event handlers
285// ----------------------------------------------------------------------------
286
201ca879
VZ
287void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
288{
07850a49
WS
289 // true is to force the frame to close
290 Close(true);
201ca879
VZ
291}
292
293void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
294{
295 wxMessageBox(_T("This sample shows wxHtmlListBox class.\n")
296 _T("\n")
62b45e06 297 _T("(c) 2003 Vadim Zeitlin"),
201ca879
VZ
298 _T("About HtmlLbox"),
299 wxOK | wxICON_INFORMATION,
300 this);
301}
302
9a9b4940 303void MyFrame::OnSetMargins(wxCommandEvent& WXUNUSED(event))
5f732810
VZ
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
321void 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
9a9b4940 334void MyFrame::OnSelectAll(wxCommandEvent& WXUNUSED(event))
5f732810 335{
4636a1e8 336 m_hlbox->SelectAll();
5f732810
VZ
337}
338
339void MyFrame::OnUpdateUISelectAll(wxUpdateUIEvent& event)
340{
341 event.Enable( m_hlbox && m_hlbox->HasMultipleSelection() );
342}
343
1d41ed0a
VZ
344void MyFrame::OnUpdateItem(wxCommandEvent& WXUNUSED(event))
345{
346 m_hlbox->UpdateFirstItem();
347}
348
9a9b4940
VZ
349void 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
8520f137 357#if wxUSE_STATUSBAR
9a9b4940 358 SetStatusText(_T("Background colour changed."));
8520f137 359#endif // wxUSE_STATUSBAR
9a9b4940
VZ
360 }
361}
362
363void 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
8520f137 371#if wxUSE_STATUSBAR
9a9b4940 372 SetStatusText(_T("Selection background colour changed."));
8520f137 373#endif // wxUSE_STATUSBAR
9a9b4940
VZ
374 }
375}
376
377void MyFrame::OnSetSelFgCol(wxCommandEvent& event)
378{
379 m_hlbox->SetChangeSelFg(!event.IsChecked());
380 m_hlbox->Refresh();
381}
382
5f732810
VZ
383// ----------------------------------------------------------------------------
384// listbox event handlers
385// ----------------------------------------------------------------------------
386
387void 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
8520f137 413#if wxUSE_STATUSBAR
5f732810
VZ
414 SetStatusText(wxString::Format(
415 _T("# items selected = %lu"),
416 (unsigned long)m_hlbox->GetSelectedCount()
417 ));
8520f137 418#endif // wxUSE_STATUSBAR
5f732810
VZ
419}
420
421// ============================================================================
422// MyHtmlListBox
423// ============================================================================
424
9a9b4940 425MyHtmlListBox::MyHtmlListBox(wxWindow *parent, bool multi)
07850a49 426 : wxHtmlListBox(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize,
9a9b4940
VZ
427 multi ? wxLB_MULTIPLE : 0)
428{
429 m_change = true;
1d41ed0a 430 m_firstItemUpdated = false;
bc55e31b 431 m_linkClicked = false;
1d41ed0a 432
9a9b4940
VZ
433
434 SetMargins(5, 5);
435
436#ifdef USE_HTML_FILE
c2a331e0 437 if ( !m_file.Open(_T("results")) )
9a9b4940 438 {
c2a331e0 439 wxLogError(_T("Failed to open results file"));
9a9b4940
VZ
440 }
441 else
442 {
443 SetItemCount(m_file.GetLineCount());
444 }
445#else
4636a1e8 446 SetItemCount(1000);
9a9b4940
VZ
447#endif
448
4636a1e8 449 SetSelection(3);
9a9b4940
VZ
450}
451
5f732810
VZ
452void 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
9a9b4940
VZ
463wxString MyHtmlListBox::OnGetItem(size_t n) const
464{
1d41ed0a
VZ
465 if ( !n && m_firstItemUpdated )
466 {
467 return wxString::Format(_T("<h1><b>Just updated</b></h1>"));
468 }
469
9a9b4940
VZ
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;
bc55e31b
VS
478 wxString label = wxString::Format(_T("<h%d><font color=#%2x%2x%2x>")
479 _T("Item</font> <b>%lu</b>")
480 _T("</h%d>"),
481 level,
482 abs((int)n - 192) % 256,
483 abs((int)n - 256) % 256,
484 abs((int)n - 128) % 256,
485 (unsigned long)n, level);
486 if ( n == 1 )
487 {
488 if ( !m_linkClicked )
489 label += _T("<a href='1'>Click here...</a>");
490 else
491 label += _T("<font color='#9999ff'>Clicked here...</font>");
492 }
493
494 return label;
9a9b4940
VZ
495#endif
496}
497
498wxColour MyHtmlListBox::GetSelectedTextColour(const wxColour& colFg) const
499{
500 return m_change ? wxHtmlListBox::GetSelectedTextColour(colFg) : colFg;
501}
502
1d41ed0a
VZ
503void MyHtmlListBox::UpdateFirstItem()
504{
505 m_firstItemUpdated = !m_firstItemUpdated;
506
507 RefreshLine(0);
508}
509
bc55e31b
VS
510void MyHtmlListBox::OnLinkClicked(size_t WXUNUSED(n),
511 const wxHtmlLinkInfo& WXUNUSED(link))
512{
513 m_linkClicked = true;
514
515 RefreshLine(1);
516}