]> git.saurik.com Git - wxWidgets.git/blob - samples/htlbox/htlbox.cpp
Tinderbox build fix (wxCococa).
[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 #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(wxWindow *parent, bool multi = false);
66
67 void SetChangeSelFg(bool change) { m_change = change; }
68 void UpdateFirstItem();
69
70 protected:
71 // override this method to return data to be shown in the listbox (this is
72 // mandatory)
73 virtual wxString OnGetItem(size_t n) const;
74
75 // change the appearance by overriding these functions (this is optional)
76 virtual void OnDrawSeparator(wxDC& dc, wxRect& rect, size_t n) const;
77 virtual wxColour GetSelectedTextColour(const wxColour& colFg) const;
78
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
88
89 #ifdef USE_HTML_FILE
90 wxTextFile m_file;
91 #endif
92
93 DECLARE_NO_COPY_CLASS(MyHtmlListBox)
94 };
95
96 class MyFrame : public wxFrame
97 {
98 public:
99 MyFrame();
100 virtual ~MyFrame();
101
102 // event handlers
103 void OnQuit(wxCommandEvent& event);
104 void OnAbout(wxCommandEvent& event);
105
106 void OnSetMargins(wxCommandEvent& event);
107 void OnDrawSeparator(wxCommandEvent&) { m_hlbox->RefreshAll(); }
108 void OnToggleMulti(wxCommandEvent& event);
109 void OnSelectAll(wxCommandEvent& event);
110 void OnUpdateItem(wxCommandEvent& event);
111
112 void OnSetBgCol(wxCommandEvent& event);
113 void OnSetSelBgCol(wxCommandEvent& event);
114 void OnSetSelFgCol(wxCommandEvent& event);
115
116
117 void OnUpdateUISelectAll(wxUpdateUIEvent& event);
118
119 void OnLboxSelect(wxCommandEvent& event);
120 void OnLboxDClick(wxCommandEvent& event)
121 {
122 wxLogMessage(_T("Listbox item %ld double clicked."), event.GetInt());
123 }
124
125 private:
126 MyHtmlListBox *m_hlbox;
127
128 // any class wishing to process wxWidgets events must use this macro
129 DECLARE_EVENT_TABLE()
130 };
131
132 class MyApp : public wxApp
133 {
134 public:
135 virtual bool OnInit() { (new MyFrame())->Show(); return true; }
136 };
137
138 // ----------------------------------------------------------------------------
139 // constants
140 // ----------------------------------------------------------------------------
141
142 // IDs for the controls and the menu commands
143 enum
144 {
145 // menu items
146 HtmlLbox_Quit = 1,
147
148 HtmlLbox_SetMargins,
149 HtmlLbox_DrawSeparator,
150 HtmlLbox_ToggleMulti,
151 HtmlLbox_SelectAll,
152 HtmlLbox_UpdateItem,
153
154 HtmlLbox_SetBgCol,
155 HtmlLbox_SetSelBgCol,
156 HtmlLbox_SetSelFgCol,
157
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 // ----------------------------------------------------------------------------
165 // event tables and other macros for wxWidgets
166 // ----------------------------------------------------------------------------
167
168 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
169 EVT_MENU(HtmlLbox_Quit, MyFrame::OnQuit)
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)
175 EVT_MENU(HtmlLbox_UpdateItem, MyFrame::OnUpdateItem)
176
177 EVT_MENU(HtmlLbox_About, MyFrame::OnAbout)
178
179 EVT_MENU(HtmlLbox_SetBgCol, MyFrame::OnSetBgCol)
180 EVT_MENU(HtmlLbox_SetSelBgCol, MyFrame::OnSetSelBgCol)
181 EVT_MENU(HtmlLbox_SetSelFgCol, MyFrame::OnSetSelFgCol)
182
183 EVT_UPDATE_UI(HtmlLbox_SelectAll, MyFrame::OnUpdateUISelectAll)
184
185
186 EVT_LISTBOX(wxID_ANY, MyFrame::OnLboxSelect)
187 EVT_LISTBOX_DCLICK(wxID_ANY, MyFrame::OnLboxDClick)
188 END_EVENT_TABLE()
189
190 IMPLEMENT_APP(MyApp)
191
192 // ============================================================================
193 // MyFrame
194 // ============================================================================
195
196 // ----------------------------------------------------------------------------
197 // MyFrame ctor/dtor
198 // ----------------------------------------------------------------------------
199
200 // frame constructor
201 MyFrame::MyFrame()
202 : wxFrame(NULL, wxID_ANY, _T("HtmlLbox wxWidgets Sample"),
203 wxDefaultPosition, wxSize(400, 500))
204 {
205 // set the frame icon
206 SetIcon(wxIcon(sample_xpm));
207
208 #if wxUSE_MENUS
209 // create a menu bar
210 wxMenu *menuFile = new wxMenu;
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,
219 _T("&Draw separators\tCtrl-D"),
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"));
227 menuHLbox->Append(HtmlLbox_UpdateItem, _T("Update &first item\tCtrl-U"));
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"));
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
239 // now append the freshly created menu to the menu bar...
240 wxMenuBar *menuBar = new wxMenuBar();
241 menuBar->Append(menuFile, _T("&File"));
242 menuBar->Append(menuHLbox, _T("&Listbox"));
243 menuBar->Append(helpMenu, _T("&Help"));
244
245 menuBar->Check(HtmlLbox_DrawSeparator, true);
246
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);
254 SetStatusText(_T("Welcome to wxWidgets!"));
255 #endif // wxUSE_STATUSBAR
256
257 // create the child controls
258 m_hlbox = new MyHtmlListBox(this);
259 wxTextCtrl *text = new wxTextCtrl(this, wxID_ANY, _T(""),
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);
266 sizer->Add(m_hlbox, 1, wxGROW);
267 sizer->Add(text, 1, wxGROW);
268
269 SetSizer(sizer);
270 }
271
272 MyFrame::~MyFrame()
273 {
274 delete wxLog::SetActiveTarget(NULL);
275 }
276
277 // ----------------------------------------------------------------------------
278 // menu event handlers
279 // ----------------------------------------------------------------------------
280
281 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
282 {
283 // true is to force the frame to close
284 Close(true);
285 }
286
287 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
288 {
289 wxMessageBox(_T("This sample shows wxHtmlListBox class.\n")
290 _T("\n")
291 _T("(c) 2003 Vadim Zeitlin"),
292 _T("About HtmlLbox"),
293 wxOK | wxICON_INFORMATION,
294 this);
295 }
296
297 void MyFrame::OnSetMargins(wxCommandEvent& WXUNUSED(event))
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
315 void 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
328 void MyFrame::OnSelectAll(wxCommandEvent& WXUNUSED(event))
329 {
330 m_hlbox->SelectAll();
331 }
332
333 void MyFrame::OnUpdateUISelectAll(wxUpdateUIEvent& event)
334 {
335 event.Enable( m_hlbox && m_hlbox->HasMultipleSelection() );
336 }
337
338 void MyFrame::OnUpdateItem(wxCommandEvent& WXUNUSED(event))
339 {
340 m_hlbox->UpdateFirstItem();
341 }
342
343 void 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
351 #if wxUSE_STATUSBAR
352 SetStatusText(_T("Background colour changed."));
353 #endif // wxUSE_STATUSBAR
354 }
355 }
356
357 void 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
365 #if wxUSE_STATUSBAR
366 SetStatusText(_T("Selection background colour changed."));
367 #endif // wxUSE_STATUSBAR
368 }
369 }
370
371 void MyFrame::OnSetSelFgCol(wxCommandEvent& event)
372 {
373 m_hlbox->SetChangeSelFg(!event.IsChecked());
374 m_hlbox->Refresh();
375 }
376
377 // ----------------------------------------------------------------------------
378 // listbox event handlers
379 // ----------------------------------------------------------------------------
380
381 void 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
407 #if wxUSE_STATUSBAR
408 SetStatusText(wxString::Format(
409 _T("# items selected = %lu"),
410 (unsigned long)m_hlbox->GetSelectedCount()
411 ));
412 #endif // wxUSE_STATUSBAR
413 }
414
415 // ============================================================================
416 // MyHtmlListBox
417 // ============================================================================
418
419 MyHtmlListBox::MyHtmlListBox(wxWindow *parent, bool multi)
420 : wxHtmlListBox(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize,
421 multi ? wxLB_MULTIPLE : 0)
422 {
423 m_change = true;
424 m_firstItemUpdated = false;
425
426
427 SetMargins(5, 5);
428
429 #ifdef USE_HTML_FILE
430 if ( !m_file.Open(_T("results")) )
431 {
432 wxLogError(_T("Failed to open results file"));
433 }
434 else
435 {
436 SetItemCount(m_file.GetLineCount());
437 }
438 #else
439 SetItemCount(1000);
440 #endif
441
442 SetSelection(3);
443 }
444
445 void 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
456 wxString MyHtmlListBox::OnGetItem(size_t n) const
457 {
458 if ( !n && m_firstItemUpdated )
459 {
460 return wxString::Format(_T("<h1><b>Just updated</b></h1>"));
461 }
462
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,
475 abs((int)n - 192) % 256,
476 abs((int)n - 256) % 256,
477 abs((int)n - 128) % 256,
478 (unsigned long)n, level);
479 #endif
480 }
481
482 wxColour MyHtmlListBox::GetSelectedTextColour(const wxColour& colFg) const
483 {
484 return m_change ? wxHtmlListBox::GetSelectedTextColour(colFg) : colFg;
485 }
486
487 void MyHtmlListBox::UpdateFirstItem()
488 {
489 m_firstItemUpdated = !m_firstItemUpdated;
490
491 RefreshLine(0);
492 }
493