]> git.saurik.com Git - wxWidgets.git/blame - samples/htlbox/htlbox.cpp
updated with latest file changes and added dynamic library (dylib) targets
[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
48//#define USE_HTML_FILE
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
201ca879
VZ
87};
88
8613d47b
VZ
89class MyFrame : public wxFrame
90{
91public:
92 MyFrame();
93 virtual ~MyFrame();
94
95 // event handlers
96 void OnQuit(wxCommandEvent& event);
97 void OnAbout(wxCommandEvent& event);
98
5f732810
VZ
99 void OnSetMargins(wxCommandEvent& event);
100 void OnDrawSeparator(wxCommandEvent&) { m_hlbox->RefreshAll(); }
101 void OnToggleMulti(wxCommandEvent& event);
102 void OnSelectAll(wxCommandEvent& event);
8613d47b 103
9a9b4940
VZ
104 void OnSetBgCol(wxCommandEvent& event);
105 void OnSetSelBgCol(wxCommandEvent& event);
106 void OnSetSelFgCol(wxCommandEvent& event);
107
108
5f732810 109 void OnUpdateUISelectAll(wxUpdateUIEvent& event);
8613d47b 110
5f732810 111 void OnLboxSelect(wxCommandEvent& event);
8613d47b
VZ
112 void OnLboxDClick(wxCommandEvent& event)
113 {
114 wxLogMessage(_T("Listbox item %ld double clicked."), event.GetInt());
115 }
116
117private:
118 MyHtmlListBox *m_hlbox;
119
120 // any class wishing to process wxWindows events must use this macro
121 DECLARE_EVENT_TABLE()
122};
123
5f732810
VZ
124class MyApp : public wxApp
125{
126public:
127 virtual bool OnInit() { (new MyFrame())->Show(); return TRUE; }
128};
129
201ca879
VZ
130// ----------------------------------------------------------------------------
131// constants
132// ----------------------------------------------------------------------------
133
134// IDs for the controls and the menu commands
135enum
136{
137 // menu items
138 HtmlLbox_Quit = 1,
139
5f732810
VZ
140 HtmlLbox_SetMargins,
141 HtmlLbox_DrawSeparator,
142 HtmlLbox_ToggleMulti,
143 HtmlLbox_SelectAll,
144
9a9b4940
VZ
145 HtmlLbox_SetBgCol,
146 HtmlLbox_SetSelBgCol,
147 HtmlLbox_SetSelFgCol,
148
201ca879
VZ
149 // it is important for the id corresponding to the "About" command to have
150 // this standard value as otherwise it won't be handled properly under Mac
151 // (where it is special and put into the "Apple" menu)
152 HtmlLbox_About = wxID_ABOUT
153};
154
155// ----------------------------------------------------------------------------
156// event tables and other macros for wxWindows
157// ----------------------------------------------------------------------------
158
201ca879
VZ
159BEGIN_EVENT_TABLE(MyFrame, wxFrame)
160 EVT_MENU(HtmlLbox_Quit, MyFrame::OnQuit)
5f732810
VZ
161
162 EVT_MENU(HtmlLbox_SetMargins, MyFrame::OnSetMargins)
163 EVT_MENU(HtmlLbox_DrawSeparator, MyFrame::OnDrawSeparator)
164 EVT_MENU(HtmlLbox_ToggleMulti, MyFrame::OnToggleMulti)
165 EVT_MENU(HtmlLbox_SelectAll, MyFrame::OnSelectAll)
166
201ca879
VZ
167 EVT_MENU(HtmlLbox_About, MyFrame::OnAbout)
168
9a9b4940
VZ
169 EVT_MENU(HtmlLbox_SetBgCol, MyFrame::OnSetBgCol)
170 EVT_MENU(HtmlLbox_SetSelBgCol, MyFrame::OnSetSelBgCol)
171 EVT_MENU(HtmlLbox_SetSelFgCol, MyFrame::OnSetSelFgCol)
5f732810
VZ
172
173 EVT_UPDATE_UI(HtmlLbox_SelectAll, MyFrame::OnUpdateUISelectAll)
174
175
201ca879
VZ
176 EVT_LISTBOX(wxID_ANY, MyFrame::OnLboxSelect)
177 EVT_LISTBOX_DCLICK(wxID_ANY, MyFrame::OnLboxDClick)
178END_EVENT_TABLE()
179
201ca879
VZ
180IMPLEMENT_APP(MyApp)
181
182// ============================================================================
5f732810 183// MyFrame
201ca879
VZ
184// ============================================================================
185
186// ----------------------------------------------------------------------------
5f732810 187// MyFrame ctor/dtor
201ca879
VZ
188// ----------------------------------------------------------------------------
189
190// frame constructor
191MyFrame::MyFrame()
192 : wxFrame(NULL, -1, _T("HtmlLbox wxWindows Sample"),
193 wxDefaultPosition, wxSize(400, 500))
194{
195 // set the frame icon
196 SetIcon(wxICON(mondrian));
197
198#if wxUSE_MENUS
199 // create a menu bar
200 wxMenu *menuFile = new wxMenu;
5f732810
VZ
201 menuFile->Append(HtmlLbox_Quit, _T("E&xit\tAlt-X"), _T("Quit this program"));
202
203 // create our specific menu
204 wxMenu *menuHLbox = new wxMenu;
205 menuHLbox->Append(HtmlLbox_SetMargins,
206 _T("Set &margins...\tCtrl-G"),
207 _T("Change the margins around the items"));
208 menuHLbox->AppendCheckItem(HtmlLbox_DrawSeparator,
9a9b4940 209 _T("&Draw separators\tCtrl-D"),
5f732810
VZ
210 _T("Toggle drawing separators between cells"));
211 menuHLbox->AppendSeparator();
212 menuHLbox->AppendCheckItem(HtmlLbox_ToggleMulti,
213 _T("&Multiple selection\tCtrl-M"),
214 _T("Toggle multiple selection on/off"));
215 menuHLbox->AppendSeparator();
216 menuHLbox->Append(HtmlLbox_SelectAll, _T("Select &all items\tCtrl-A"));
9a9b4940
VZ
217 menuHLbox->AppendSeparator();
218 menuHLbox->Append(HtmlLbox_SetBgCol, _T("Set &background...\tCtrl-B"));
219 menuHLbox->Append(HtmlLbox_SetSelBgCol,
220 _T("Set &selection background...\tCtrl-S"));
221 menuHLbox->AppendCheckItem(HtmlLbox_SetSelFgCol,
222 _T("Keep &foreground in selection\tCtrl-F"));
201ca879
VZ
223
224 // the "About" item should be in the help menu
225 wxMenu *helpMenu = new wxMenu;
226 helpMenu->Append(HtmlLbox_About, _T("&About...\tF1"), _T("Show about dialog"));
227
201ca879
VZ
228 // now append the freshly created menu to the menu bar...
229 wxMenuBar *menuBar = new wxMenuBar();
230 menuBar->Append(menuFile, _T("&File"));
5f732810 231 menuBar->Append(menuHLbox, _T("&Listbox"));
201ca879
VZ
232 menuBar->Append(helpMenu, _T("&Help"));
233
5f732810
VZ
234 menuBar->Check(HtmlLbox_DrawSeparator, true);
235
201ca879
VZ
236 // ... and attach this menu bar to the frame
237 SetMenuBar(menuBar);
238#endif // wxUSE_MENUS
239
240#if wxUSE_STATUSBAR
241 // create a status bar just for fun (by default with 1 pane only)
242 CreateStatusBar(2);
243 SetStatusText(_T("Welcome to wxWindows!"));
244#endif // wxUSE_STATUSBAR
245
246 // create the child controls
5f732810 247 m_hlbox = new MyHtmlListBox(this);
201ca879
VZ
248 wxTextCtrl *text = new wxTextCtrl(this, -1, _T(""),
249 wxDefaultPosition, wxDefaultSize,
250 wxTE_MULTILINE);
251 delete wxLog::SetActiveTarget(new wxLogTextCtrl(text));
252
253 // and lay them out
254 wxSizer *sizer = new wxBoxSizer(wxHORIZONTAL);
8613d47b 255 sizer->Add(m_hlbox, 1, wxGROW);
201ca879
VZ
256 sizer->Add(text, 1, wxGROW);
257
258 SetSizer(sizer);
259}
260
261MyFrame::~MyFrame()
262{
263 delete wxLog::SetActiveTarget(NULL);
264}
265
5f732810
VZ
266// ----------------------------------------------------------------------------
267// menu event handlers
268// ----------------------------------------------------------------------------
269
201ca879
VZ
270void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
271{
272 // TRUE is to force the frame to close
273 Close(TRUE);
274}
275
276void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
277{
278 wxMessageBox(_T("This sample shows wxHtmlListBox class.\n")
279 _T("\n")
280