]> git.saurik.com Git - wxWidgets.git/blame - samples/htlbox/htlbox.cpp
Some doc corrections
[wxWidgets.git] / samples / htlbox / htlbox.cpp
CommitLineData
201ca879
VZ
1/////////////////////////////////////////////////////////////////////////////
2// Name: htmllbox.cpp
3// Purpose: HtmlLbox wxWindows sample
4// Author: Vadim Zeitlin
5// Modified by:
6// Created: 31.05.03
7// RCS-ID: $Id$
8// Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwindows.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
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
VZ
42#include "wx/colordlg.h"
43
201ca879
VZ
44#include "wx/htmllbox.h"
45
5f732810
VZ
46// you can also have a file containing HTML strings for testing, enable this if
47// you want to use it
c2a331e0 48//#define USE_HTML_FILE
5f732810
VZ
49#ifdef USE_HTML_FILE
50 #include "wx/textfile.h"
51#endif
52
201ca879
VZ
53// ----------------------------------------------------------------------------
54// resources
55// ----------------------------------------------------------------------------
56
57// the application icon (under Windows and OS/2 it is in resources)
58#if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__) || defined(__WXX11__)
59 #include "mondrian.xpm"
60#endif
61
62// ----------------------------------------------------------------------------
63// private classes
64// ----------------------------------------------------------------------------
65
201ca879
VZ
66// to use wxHtmlListBox you must derive a new class from it as you must
67// implement pure virtual OnGetItem()
68class MyHtmlListBox : public wxHtmlListBox
69{
70public:
9a9b4940 71 MyHtmlListBox(wxWindow *parent, bool multi = false);
5f732810 72
9a9b4940 73 void SetChangeSelFg(bool change) { m_change = change; }
201ca879
VZ
74
75protected:
9a9b4940 76 virtual wxString OnGetItem(size_t n) const;
201ca879 77
9a9b4940 78 // change the appearance by overriding these functions
5f732810 79 virtual void OnDrawSeparator(wxDC& dc, wxRect& rect, size_t n) const;
9a9b4940 80 virtual wxColour GetSelectedTextColour(const wxColour& colFg) const;
201ca879 81
9a9b4940
VZ
82 bool m_change;
83
84#ifdef USE_HTML_FILE
5f732810 85 wxTextFile m_file;
9a9b4940 86#endif
27d0dcd0
VZ
87
88 DECLARE_NO_COPY_CLASS(MyHtmlListBox)
201ca879
VZ
89};
90
8613d47b
VZ
91class MyFrame : public wxFrame
92{
93public:
94 MyFrame();
95 virtual ~MyFrame();
96
97 // event handlers
98 void OnQuit(wxCommandEvent& event);
99 void OnAbout(wxCommandEvent& event);
100
5f732810
VZ
101 void OnSetMargins(wxCommandEvent& event);
102 void OnDrawSeparator(wxCommandEvent&) { m_hlbox->RefreshAll(); }
103 void OnToggleMulti(wxCommandEvent& event);
104 void OnSelectAll(wxCommandEvent& event);
8613d47b 105
9a9b4940
VZ
106 void OnSetBgCol(wxCommandEvent& event);
107 void OnSetSelBgCol(wxCommandEvent& event);
108 void OnSetSelFgCol(wxCommandEvent& event);
109
110
5f732810 111 void OnUpdateUISelectAll(wxUpdateUIEvent& event);
8613d47b 112
5f732810 113 void OnLboxSelect(wxCommandEvent& event);
8613d47b
VZ
114 void OnLboxDClick(wxCommandEvent& event)
115 {
116 wxLogMessage(_T("Listbox item %ld double clicked."), event.GetInt());
117 }
118
119private:
120 MyHtmlListBox *m_hlbox;
121
122 // any class wishing to process wxWindows events must use this macro
123 DECLARE_EVENT_TABLE()
124};
125
5f732810
VZ
126class MyApp : public wxApp
127{
128public:
129 virtual bool OnInit() { (new MyFrame())->Show(); return TRUE; }
130};
131
201ca879
VZ
132// ----------------------------------------------------------------------------
133// constants
134// ----------------------------------------------------------------------------
135
136// IDs for the controls and the menu commands
137enum
138{
139 // menu items
140 HtmlLbox_Quit = 1,
141
5f732810
VZ
142 HtmlLbox_SetMargins,
143 HtmlLbox_DrawSeparator,
144 HtmlLbox_ToggleMulti,
145 HtmlLbox_SelectAll,
146
9a9b4940
VZ
147 HtmlLbox_SetBgCol,
148 HtmlLbox_SetSelBgCol,
149 HtmlLbox_SetSelFgCol,
150
201ca879
VZ
151 // it is important for the id corresponding to the "About" command to have
152 // this standard value as otherwise it won't be handled properly under Mac
153 // (where it is special and put into the "Apple" menu)
154 HtmlLbox_About = wxID_ABOUT
155};
156
157// ----------------------------------------------------------------------------
158// event tables and other macros for wxWindows
159// ----------------------------------------------------------------------------
160
201ca879
VZ
161BEGIN_EVENT_TABLE(MyFrame, wxFrame)
162 EVT_MENU(HtmlLbox_Quit, MyFrame::OnQuit)
5f732810
VZ
163
164 EVT_MENU(HtmlLbox_SetMargins, MyFrame::OnSetMargins)
165 EVT_MENU(HtmlLbox_DrawSeparator, MyFrame::OnDrawSeparator)
166 EVT_MENU(HtmlLbox_ToggleMulti, MyFrame::OnToggleMulti)
167 EVT_MENU(HtmlLbox_SelectAll, MyFrame::OnSelectAll)
168
201ca879
VZ
169 EVT_MENU(HtmlLbox_About, MyFrame::OnAbout)
170
9a9b4940
VZ
171 EVT_MENU(HtmlLbox_SetBgCol, MyFrame::OnSetBgCol)
172 EVT_MENU(HtmlLbox_SetSelBgCol, MyFrame::OnSetSelBgCol)
173 EVT_MENU(HtmlLbox_SetSelFgCol, MyFrame::OnSetSelFgCol)
5f732810
VZ
174
175 EVT_UPDATE_UI(HtmlLbox_SelectAll, MyFrame::OnUpdateUISelectAll)
176
177
201ca879
VZ
178 EVT_LISTBOX(wxID_ANY, MyFrame::OnLboxSelect)
179 EVT_LISTBOX_DCLICK(wxID_ANY, MyFrame::OnLboxDClick)
180END_EVENT_TABLE()
181
201ca879
VZ
182IMPLEMENT_APP(MyApp)
183
184// ============================================================================
5f732810 185// MyFrame
201ca879
VZ
186// ============================================================================
187
188// ----------------------------------------------------------------------------
5f732810 189// MyFrame ctor/dtor
201ca879
VZ
190// ----------------------------------------------------------------------------
191
192// frame constructor
193MyFrame::MyFrame()
194 : wxFrame(NULL, -1, _T("HtmlLbox wxWindows Sample"),
195 wxDefaultPosition, wxSize(400, 500))
196{
197 // set the frame icon
198 SetIcon(wxICON(mondrian));
199
200#if wxUSE_MENUS
201 // create a menu bar
202 wxMenu *menuFile = new wxMenu;
5f732810
VZ
203 menuFile->Append(HtmlLbox_Quit, _T("E&xit\tAlt-X"), _T("Quit this program"));
204
205 // create our specific menu
206 wxMenu *menuHLbox = new wxMenu;
207 menuHLbox->Append(HtmlLbox_SetMargins,
208 _T("Set &margins...\tCtrl-G"),
209 _T("Change the margins around the items"));
210 menuHLbox->AppendCheckItem(HtmlLbox_DrawSeparator,
9a9b4940 211 _T("&Draw separators\tCtrl-D"),
5f732810
VZ
212 _T("Toggle drawing separators between cells"));
213 menuHLbox->AppendSeparator();
214 menuHLbox->AppendCheckItem(HtmlLbox_ToggleMulti,
215 _T("&Multiple selection\tCtrl-M"),
216 _T("Toggle multiple selection on/off"));
217 menuHLbox->AppendSeparator();
218 menuHLbox->Append(HtmlLbox_SelectAll, _T("Select &all items\tCtrl-A"));
9a9b4940
VZ
219 menuHLbox->AppendSeparator();
220 menuHLbox->Append(HtmlLbox_SetBgCol, _T("Set &background...\tCtrl-B"));
221 menuHLbox->Append(HtmlLbox_SetSelBgCol,
222 _T("Set &selection background...\tCtrl-S"));
223 menuHLbox->AppendCheckItem(HtmlLbox_SetSelFgCol,
224 _T("Keep &foreground in selection\tCtrl-F"));
201ca879
VZ
225
226 // the "About" item should be in the help menu
227 wxMenu *helpMenu = new wxMenu;
228 helpMenu->Append(HtmlLbox_About, _T("&About...\tF1"), _T("Show about dialog"));
229
201ca879
VZ
230 // now append the freshly created menu to the menu bar...
231 wxMenuBar *menuBar = new wxMenuBar();
232 menuBar->Append(menuFile, _T("&File"));
5f732810 233 menuBar->Append(menuHLbox, _T("&Listbox"));
201ca879
VZ
234 menuBar->Append(helpMenu, _T("&Help"));
235
5f732810
VZ
236 menuBar->Check(HtmlLbox_DrawSeparator, true);
237
201ca879
VZ
238 // ... and attach this menu bar to the frame
239 SetMenuBar(menuBar);
240#endif // wxUSE_MENUS
241
242#if wxUSE_STATUSBAR
243 // create a status bar just for fun (by default with 1 pane only)
244 CreateStatusBar(2);
245 SetStatusText(_T("Welcome to wxWindows!"));
246#endif // wxUSE_STATUSBAR
247
248 // create the child controls
5f732810 249 m_hlbox = new MyHtmlListBox(this);
201ca879
VZ
250 wxTextCtrl *text = new wxTextCtrl(this, -1, _T(""),
251 wxDefaultPosition, wxDefaultSize,
252 wxTE_MULTILINE);
253 delete wxLog::SetActiveTarget(new wxLogTextCtrl(text));
254
255 // and lay them out
256 wxSizer *sizer = new wxBoxSizer(wxHORIZONTAL);
8613d47b 257 sizer->Add(m_hlbox, 1, wxGROW);
201ca879
VZ
258 sizer->Add(text, 1, wxGROW);
259
260 SetSizer(sizer);
261}
262
263MyFrame::~MyFrame()
264{
265 delete wxLog::SetActiveTarget(NULL);
266}
267
5f732810
VZ
268// ----------------------------------------------------------------------------
269// menu event handlers
270// ----------------------------------------------------------------------------
271
201ca879
VZ
272void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
273{
274 // TRUE is to force the frame to close
275 Close(TRUE);
276}
277
278void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
279{
280 wxMessageBox(_T("This sample shows wxHtmlListBox class.\n")
281 _T("\n")
282