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