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