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