]> git.saurik.com Git - wxWidgets.git/blob - samples/htlbox/htlbox.cpp
suppress VC++ warning about long -> void * conversion too
[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 #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 // ----------------------------------------------------------------------------
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
67 // to use wxHtmlListBox you must derive a new class from it as you must
68 // implement pure virtual OnGetItem()
69 class MyHtmlListBox : public wxHtmlListBox
70 {
71 public:
72 MyHtmlListBox(wxWindow *parent, bool multi = false);
73
74 void SetChangeSelFg(bool change) { m_change = change; }
75
76 protected:
77 virtual wxString OnGetItem(size_t n) const;
78
79 // change the appearance by overriding these functions
80 virtual void OnDrawSeparator(wxDC& dc, wxRect& rect, size_t n) const;
81 virtual wxColour GetSelectedTextColour(const wxColour& colFg) const;
82
83 bool m_change;
84
85 #ifdef USE_HTML_FILE
86 wxTextFile m_file;
87 #endif
88
89 DECLARE_NO_COPY_CLASS(MyHtmlListBox)
90 };
91
92 class MyFrame : public wxFrame
93 {
94 public:
95 MyFrame();
96 virtual ~MyFrame();
97
98 // event handlers
99 void OnQuit(wxCommandEvent& event);
100 void OnAbout(wxCommandEvent& event);
101
102 void OnSetMargins(wxCommandEvent& event);
103 void OnDrawSeparator(wxCommandEvent&) { m_hlbox->RefreshAll(); }
104 void OnToggleMulti(wxCommandEvent& event);
105 void OnSelectAll(wxCommandEvent& event);
106
107 void OnSetBgCol(wxCommandEvent& event);
108 void OnSetSelBgCol(wxCommandEvent& event);
109 void OnSetSelFgCol(wxCommandEvent& event);
110
111
112 void OnUpdateUISelectAll(wxUpdateUIEvent& event);
113
114 void OnLboxSelect(wxCommandEvent& event);
115 void OnLboxDClick(wxCommandEvent& event)
116 {
117 wxLogMessage(_T("Listbox item %ld double clicked."), event.GetInt());
118 }
119
120 private:
121 MyHtmlListBox *m_hlbox;
122
123 // any class wishing to process wxWidgets events must use this macro
124 DECLARE_EVENT_TABLE()
125 };
126
127 class MyApp : public wxApp
128 {
129 public:
130 virtual bool OnInit() { (new MyFrame())->Show(); return true; }
131 };
132
133 // ----------------------------------------------------------------------------
134 // constants
135 // ----------------------------------------------------------------------------
136
137 // IDs for the controls and the menu commands
138 enum
139 {
140 // menu items
141 HtmlLbox_Quit = 1,
142
143 HtmlLbox_SetMargins,
144 HtmlLbox_DrawSeparator,
145 HtmlLbox_ToggleMulti,
146 HtmlLbox_SelectAll,
147
148 HtmlLbox_SetBgCol,
149 HtmlLbox_SetSelBgCol,
150 HtmlLbox_SetSelFgCol,
151
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 // ----------------------------------------------------------------------------
159 // event tables and other macros for wxWidgets
160 // ----------------------------------------------------------------------------
161
162 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
163 EVT_MENU(HtmlLbox_Quit, MyFrame::OnQuit)
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
170 EVT_MENU(HtmlLbox_About, MyFrame::OnAbout)
171
172 EVT_MENU(HtmlLbox_SetBgCol, MyFrame::OnSetBgCol)
173 EVT_MENU(HtmlLbox_SetSelBgCol, MyFrame::OnSetSelBgCol)
174 EVT_MENU(HtmlLbox_SetSelFgCol, MyFrame::OnSetSelFgCol)
175
176 EVT_UPDATE_UI(HtmlLbox_SelectAll, MyFrame::OnUpdateUISelectAll)
177
178
179 EVT_LISTBOX(wxID_ANY, MyFrame::OnLboxSelect)
180 EVT_LISTBOX_DCLICK(wxID_ANY, MyFrame::OnLboxDClick)
181 END_EVENT_TABLE()
182
183 IMPLEMENT_APP(MyApp)
184
185 // ============================================================================
186 // MyFrame
187 // ============================================================================
188
189 // ----------------------------------------------------------------------------
190 // MyFrame ctor/dtor
191 // ----------------------------------------------------------------------------
192
193 // frame constructor
194 MyFrame::MyFrame()
195 : wxFrame(NULL, wxID_ANY, _T("HtmlLbox wxWidgets Sample"),
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;
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,
212 _T("&Draw separators\tCtrl-D"),
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"));
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"));
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
231 // now append the freshly created menu to the menu bar...
232 wxMenuBar *menuBar = new wxMenuBar();
233 menuBar->Append(menuFile, _T("&File"));
234 menuBar->Append(menuHLbox, _T("&Listbox"));
235 menuBar->Append(helpMenu, _T("&Help"));
236
237 menuBar->Check(HtmlLbox_DrawSeparator, true);
238
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);
246 SetStatusText(_T("Welcome to wxWidgets!"));
247 #endif // wxUSE_STATUSBAR
248
249 // create the child controls
250 m_hlbox = new MyHtmlListBox(this);
251 wxTextCtrl *text = new wxTextCtrl(this, wxID_ANY, _T(""),
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);
258 sizer->Add(m_hlbox, 1, wxGROW);
259 sizer->Add(text, 1, wxGROW);
260
261 SetSizer(sizer);
262 }
263
264 MyFrame::~MyFrame()
265 {
266 delete wxLog::SetActiveTarget(NULL);
267 }
268
269 // ----------------------------------------------------------------------------
270 // menu event handlers
271 // ----------------------------------------------------------------------------
272
273 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
274 {
275 // true is to force the frame to close
276 Close(true);
277 }
278
279 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
280 {
281 wxMessageBox(_T("This sample shows wxHtmlListBox class.\n")
282 _T("\n")
283 _T("(c) 2003 Vadim Zeitlin"),
284 _T("About HtmlLbox"),
285 wxOK | wxICON_INFORMATION,
286 this);
287 }
288
289 void MyFrame::OnSetMargins(wxCommandEvent& WXUNUSED(event))
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
307 void 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
320 void MyFrame::OnSelectAll(wxCommandEvent& WXUNUSED(event))
321 {
322 m_hlbox->SelectAll();
323 }
324
325 void MyFrame::OnUpdateUISelectAll(wxUpdateUIEvent& event)
326 {
327 event.Enable( m_hlbox && m_hlbox->HasMultipleSelection() );
328 }
329
330 void 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
338 #if wxUSE_STATUSBAR
339 SetStatusText(_T("Background colour changed."));
340 #endif // wxUSE_STATUSBAR
341 }
342 }
343
344 void 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
352 #if wxUSE_STATUSBAR
353 SetStatusText(_T("Selection background colour changed."));
354 #endif // wxUSE_STATUSBAR
355 }
356 }
357
358 void MyFrame::OnSetSelFgCol(wxCommandEvent& event)
359 {
360 m_hlbox->SetChangeSelFg(!event.IsChecked());
361 m_hlbox->Refresh();
362 }
363
364 // ----------------------------------------------------------------------------
365 // listbox event handlers
366 // ----------------------------------------------------------------------------
367
368 void 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
394 #if wxUSE_STATUSBAR
395 SetStatusText(wxString::Format(
396 _T("# items selected = %lu"),
397 (unsigned long)m_hlbox->GetSelectedCount()
398 ));
399 #endif // wxUSE_STATUSBAR
400 }
401
402 // ============================================================================
403 // MyHtmlListBox
404 // ============================================================================
405
406 MyHtmlListBox::MyHtmlListBox(wxWindow *parent, bool multi)
407 : wxHtmlListBox(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize,
408 multi ? wxLB_MULTIPLE : 0)
409 {
410 m_change = true;
411
412 SetMargins(5, 5);
413
414 #ifdef USE_HTML_FILE
415 if ( !m_file.Open(_T("results")) )
416 {
417 wxLogError(_T("Failed to open results file"));
418 }
419 else
420 {
421 SetItemCount(m_file.GetLineCount());
422 }
423 #else
424 SetItemCount(1000);
425 #endif
426
427 SetSelection(3);
428 }
429
430 void 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
441 wxString 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
462 wxColour MyHtmlListBox::GetSelectedTextColour(const wxColour& colFg) const
463 {
464 return m_change ? wxHtmlListBox::GetSelectedTextColour(colFg) : colFg;
465 }
466