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