]> git.saurik.com Git - wxWidgets.git/blob - samples/htlbox/htlbox.cpp
Some fixes for gcc compilation
[wxWidgets.git] / samples / htlbox / htlbox.cpp
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
27 // for all others, include the necessary headers (this file is usually all you
28 // need because it includes almost all "standard" wxWindows headers)
29 #ifndef WX_PRECOMP
30 #include "wx/app.h"
31 #include "wx/frame.h"
32 #include "wx/log.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/htmllbox.h"
43
44 // ----------------------------------------------------------------------------
45 // resources
46 // ----------------------------------------------------------------------------
47
48 // the application icon (under Windows and OS/2 it is in resources)
49 #if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__) || defined(__WXX11__)
50 #include "mondrian.xpm"
51 #endif
52
53 // ----------------------------------------------------------------------------
54 // private classes
55 // ----------------------------------------------------------------------------
56
57 class MyApp : public wxApp
58 {
59 public:
60 virtual bool OnInit();
61 };
62
63 // to use wxHtmlListBox you must derive a new class from it as you must
64 // implement pure virtual OnGetItem()
65 class MyHtmlListBox : public wxHtmlListBox
66 {
67 public:
68 MyHtmlListBox(wxWindow *parent, bool multi)
69 : wxHtmlListBox(parent, -1, wxDefaultPosition, wxDefaultSize,
70 multi ? wxLB_MULTIPLE : 0)
71 {
72 SetItemCount(1000);
73 if ( HasMultipleSelection() )
74 Select(10);
75 else
76 SetSelection(10);
77 SetMargins(5, 5);
78 }
79
80 protected:
81 virtual wxString OnGetItem(size_t n) const
82 {
83 int level = n % 6 + 1;
84 return wxString::Format(_T("<h%d><font color=#%2x%2x%2x>")
85 _T("Item</font> <b>%lu</b>")
86 _T("</h%d>"),
87 level,
88 abs(n - 192) % 256,
89 abs(n - 256) % 256,
90 abs(n - 128) % 256,
91 (unsigned long)n, level);
92 }
93
94 virtual void OnDrawSeparator(wxDC& dc,
95 wxRect& rect,
96 size_t WXUNUSED(n)) const
97 {
98 dc.SetPen(*wxBLACK_DASHED_PEN);
99 dc.DrawLine(rect.x, rect.y, rect.GetRight(), rect.y);
100 dc.DrawLine(rect.x, rect.GetBottom(), rect.GetRight(), rect.GetBottom());
101 }
102
103 };
104
105 class MyFrame : public wxFrame
106 {
107 public:
108 MyFrame();
109 virtual ~MyFrame();
110
111 // event handlers
112 void OnQuit(wxCommandEvent& event);
113 void OnAbout(wxCommandEvent& event);
114
115 void OnLboxSelect(wxCommandEvent& event)
116 {
117 wxLogMessage(_T("Listbox selection is now %ld."), event.GetInt());
118
119 if ( m_hlbox->HasMultipleSelection() )
120 {
121 wxString s;
122
123 bool first = true;
124 unsigned long cookie;
125 for ( int item = m_hlbox->GetFirstSelected(cookie);
126 item != wxNOT_FOUND;
127 item = m_hlbox->GetNextSelected(cookie) )
128 {
129 if ( first )
130 first = false;
131 else
132 s << _T(", ");
133
134 s << item;
135 }
136
137 if ( !s.empty() )
138 wxLogMessage(_T("Selected items: %s"), s.c_str());
139 }
140
141 SetStatusText(wxString::Format(
142 _T("# items selected = %lu"),
143 (unsigned long)m_hlbox->GetSelectedCount()
144 ));
145 }
146
147 void OnLboxDClick(wxCommandEvent& event)
148 {
149 wxLogMessage(_T("Listbox item %ld double clicked."), event.GetInt());
150 }
151
152 private:
153 MyHtmlListBox *m_hlbox;
154
155 // any class wishing to process wxWindows events must use this macro
156 DECLARE_EVENT_TABLE()
157 };
158
159 // ----------------------------------------------------------------------------
160 // constants
161 // ----------------------------------------------------------------------------
162
163 // IDs for the controls and the menu commands
164 enum
165 {
166 // menu items
167 HtmlLbox_Quit = 1,
168
169 // it is important for the id corresponding to the "About" command to have
170 // this standard value as otherwise it won't be handled properly under Mac
171 // (where it is special and put into the "Apple" menu)
172 HtmlLbox_About = wxID_ABOUT
173 };
174
175 // ----------------------------------------------------------------------------
176 // event tables and other macros for wxWindows
177 // ----------------------------------------------------------------------------
178
179 // the event tables connect the wxWindows events with the functions (event
180 // handlers) which process them. It can be also done at run-time, but for the
181 // simple menu events like this the static method is much simpler.
182 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
183 EVT_MENU(HtmlLbox_Quit, MyFrame::OnQuit)
184 EVT_MENU(HtmlLbox_About, MyFrame::OnAbout)
185
186 EVT_LISTBOX(wxID_ANY, MyFrame::OnLboxSelect)
187 EVT_LISTBOX_DCLICK(wxID_ANY, MyFrame::OnLboxDClick)
188 END_EVENT_TABLE()
189
190 // Create a new application object: this macro will allow wxWindows to create
191 // the application object during program execution (it's better than using a
192 // static object for many reasons) and also declares the accessor function
193 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
194 // not wxApp)
195 IMPLEMENT_APP(MyApp)
196
197 // ============================================================================
198 // implementation
199 // ============================================================================
200
201 // ----------------------------------------------------------------------------
202 // the application class
203 // ----------------------------------------------------------------------------
204
205 // 'Main program' equivalent: the program execution "starts" here
206 bool MyApp::OnInit()
207 {
208 (new MyFrame())->Show();
209
210 return TRUE;
211 }
212
213 // ----------------------------------------------------------------------------
214 // main frame
215 // ----------------------------------------------------------------------------
216
217 // frame constructor
218 MyFrame::MyFrame()
219 : wxFrame(NULL, -1, _T("HtmlLbox wxWindows Sample"),
220 wxDefaultPosition, wxSize(400, 500))
221 {
222 // set the frame icon
223 SetIcon(wxICON(mondrian));
224
225 #if wxUSE_MENUS
226 // create a menu bar
227 wxMenu *menuFile = new wxMenu;
228
229 // the "About" item should be in the help menu
230 wxMenu *helpMenu = new wxMenu;
231 helpMenu->Append(HtmlLbox_About, _T("&About...\tF1"), _T("Show about dialog"));
232
233 menuFile->Append(HtmlLbox_Quit, _T("E&xit\tAlt-X"), _T("Quit this program"));
234
235 // now append the freshly created menu to the menu bar...
236 wxMenuBar *menuBar = new wxMenuBar();
237 menuBar->Append(menuFile, _T("&File"));
238 menuBar->Append(helpMenu, _T("&Help"));
239
240 // ... and attach this menu bar to the frame
241 SetMenuBar(menuBar);
242 #endif // wxUSE_MENUS
243
244 #if wxUSE_STATUSBAR
245 // create a status bar just for fun (by default with 1 pane only)
246 CreateStatusBar(2);
247 SetStatusText(_T("Welcome to wxWindows!"));
248 #endif // wxUSE_STATUSBAR
249
250 // create the child controls
251 m_hlbox = new MyHtmlListBox(this, true);
252 wxTextCtrl *text = new wxTextCtrl(this, -1, _T(""),
253 wxDefaultPosition, wxDefaultSize,
254 wxTE_MULTILINE);
255 delete wxLog::SetActiveTarget(new wxLogTextCtrl(text));
256
257 // and lay them out
258 wxSizer *sizer = new wxBoxSizer(wxHORIZONTAL);
259 sizer->Add(m_hlbox, 1, wxGROW);
260 sizer->Add(text, 1, wxGROW);
261
262 SetSizer(sizer);
263 }
264
265 MyFrame::~MyFrame()
266 {
267 delete wxLog::SetActiveTarget(NULL);
268 }
269
270 // event handlers
271 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
272 {
273 // TRUE is to force the frame to close
274 Close(TRUE);
275 }
276
277 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
278 {
279 wxMessageBox(_T("This sample shows wxHtmlListBox class.\n")
280 _T("\n")
281 _T("© 2003 Vadim Zeitlin"),
282 _T("About HtmlLbox"),
283 wxOK | wxICON_INFORMATION,
284 this);
285 }
286