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