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