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