]>
Commit | Line | Data |
---|---|---|
201ca879 VZ |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: htmllbox.cpp | |
be5a51fb | 3 | // Purpose: HtmlLbox wxWidgets sample |
201ca879 VZ |
4 | // Author: Vadim Zeitlin |
5 | // Modified by: | |
6 | // Created: 31.05.03 | |
7 | // RCS-ID: $Id$ | |
be5a51fb | 8 | // Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwidgets.org> |
201ca879 VZ |
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 | 42 | #include "wx/colordlg.h" |
e4f3eb42 | 43 | #include "wx/numdlg.h" |
9a9b4940 | 44 | |
201ca879 VZ |
45 | #include "wx/htmllbox.h" |
46 | ||
5f732810 VZ |
47 | // you can also have a file containing HTML strings for testing, enable this if |
48 | // you want to use it | |
c2a331e0 | 49 | //#define USE_HTML_FILE |
5f732810 VZ |
50 | #ifdef USE_HTML_FILE |
51 | #include "wx/textfile.h" | |
52 | #endif | |
53 | ||
201ca879 VZ |
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 | ||
201ca879 VZ |
67 | // to use wxHtmlListBox you must derive a new class from it as you must |
68 | // implement pure virtual OnGetItem() | |
69 | class MyHtmlListBox : public wxHtmlListBox | |
70 | { | |
71 | public: | |
9a9b4940 | 72 | MyHtmlListBox(wxWindow *parent, bool multi = false); |
5f732810 | 73 | |
9a9b4940 | 74 | void SetChangeSelFg(bool change) { m_change = change; } |
201ca879 VZ |
75 | |
76 | protected: | |
9a9b4940 | 77 | virtual wxString OnGetItem(size_t n) const; |
201ca879 | 78 | |
9a9b4940 | 79 | // change the appearance by overriding these functions |
5f732810 | 80 | virtual void OnDrawSeparator(wxDC& dc, wxRect& rect, size_t n) const; |
9a9b4940 | 81 | virtual wxColour GetSelectedTextColour(const wxColour& colFg) const; |
201ca879 | 82 | |
9a9b4940 VZ |
83 | bool m_change; |
84 | ||
85 | #ifdef USE_HTML_FILE | |
5f732810 | 86 | wxTextFile m_file; |
9a9b4940 | 87 | #endif |
27d0dcd0 VZ |
88 | |
89 | DECLARE_NO_COPY_CLASS(MyHtmlListBox) | |
201ca879 VZ |
90 | }; |
91 | ||
8613d47b VZ |
92 | class MyFrame : public wxFrame |
93 | { | |
94 | public: | |
95 | MyFrame(); | |
96 | virtual ~MyFrame(); | |
97 | ||
98 | // event handlers | |
99 | void OnQuit(wxCommandEvent& event); | |
100 | void OnAbout(wxCommandEvent& event); | |
101 | ||
5f732810 VZ |
102 | void OnSetMargins(wxCommandEvent& event); |
103 | void OnDrawSeparator(wxCommandEvent&) { m_hlbox->RefreshAll(); } | |
104 | void OnToggleMulti(wxCommandEvent& event); | |
105 | void OnSelectAll(wxCommandEvent& event); | |
8613d47b | 106 | |
9a9b4940 VZ |
107 | void OnSetBgCol(wxCommandEvent& event); |
108 | void OnSetSelBgCol(wxCommandEvent& event); | |
109 | void OnSetSelFgCol(wxCommandEvent& event); | |
110 | ||
111 | ||
5f732810 | 112 | void OnUpdateUISelectAll(wxUpdateUIEvent& event); |
8613d47b | 113 | |
5f732810 | 114 | void OnLboxSelect(wxCommandEvent& event); |
8613d47b VZ |
115 | void OnLboxDClick(wxCommandEvent& event) |
116 | { | |
117 | wxLogMessage(_T("Listbox item %ld double clicked."), event.GetInt()); | |
118 | } | |
119 | ||
120 | private: | |
121 | MyHtmlListBox *m_hlbox; | |
122 | ||
be5a51fb | 123 | // any class wishing to process wxWidgets events must use this macro |
8613d47b VZ |
124 | DECLARE_EVENT_TABLE() |
125 | }; | |
126 | ||
5f732810 VZ |
127 | class MyApp : public wxApp |
128 | { | |
129 | public: | |
07850a49 | 130 | virtual bool OnInit() { (new MyFrame())->Show(); return true; } |
5f732810 VZ |
131 | }; |
132 | ||
201ca879 VZ |
133 | // ---------------------------------------------------------------------------- |
134 | // constants | |
135 | // ---------------------------------------------------------------------------- | |
136 | ||
137 | // IDs for the controls and the menu commands | |
138 | enum | |
139 | { | |
140 | // menu items | |
141 | HtmlLbox_Quit = 1, | |
142 | ||
5f732810 VZ |
143 | HtmlLbox_SetMargins, |
144 | HtmlLbox_DrawSeparator, | |
145 | HtmlLbox_ToggleMulti, | |
146 | HtmlLbox_SelectAll, | |
147 | ||
9a9b4940 VZ |
148 | HtmlLbox_SetBgCol, |
149 | HtmlLbox_SetSelBgCol, | |
150 | HtmlLbox_SetSelFgCol, | |
151 | ||
201ca879 VZ |
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 | // ---------------------------------------------------------------------------- | |
be5a51fb | 159 | // event tables and other macros for wxWidgets |
201ca879 VZ |
160 | // ---------------------------------------------------------------------------- |
161 | ||
201ca879 VZ |
162 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) |
163 | EVT_MENU(HtmlLbox_Quit, MyFrame::OnQuit) | |
5f732810 VZ |
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 | ||
201ca879 VZ |
170 | EVT_MENU(HtmlLbox_About, MyFrame::OnAbout) |
171 | ||
9a9b4940 VZ |
172 | EVT_MENU(HtmlLbox_SetBgCol, MyFrame::OnSetBgCol) |
173 | EVT_MENU(HtmlLbox_SetSelBgCol, MyFrame::OnSetSelBgCol) | |
174 | EVT_MENU(HtmlLbox_SetSelFgCol, MyFrame::OnSetSelFgCol) | |
5f732810 VZ |
175 | |
176 | EVT_UPDATE_UI(HtmlLbox_SelectAll, MyFrame::OnUpdateUISelectAll) | |
177 | ||
178 | ||
201ca879 VZ |
179 | EVT_LISTBOX(wxID_ANY, MyFrame::OnLboxSelect) |
180 | EVT_LISTBOX_DCLICK(wxID_ANY, MyFrame::OnLboxDClick) | |
181 | END_EVENT_TABLE() | |
182 | ||
201ca879 VZ |
183 | IMPLEMENT_APP(MyApp) |
184 | ||
185 | // ============================================================================ | |
5f732810 | 186 | // MyFrame |
201ca879 VZ |
187 | // ============================================================================ |
188 | ||
189 | // ---------------------------------------------------------------------------- | |
5f732810 | 190 | // MyFrame ctor/dtor |
201ca879 VZ |
191 | // ---------------------------------------------------------------------------- |
192 | ||
193 | // frame constructor | |
194 | MyFrame::MyFrame() | |
07850a49 | 195 | : wxFrame(NULL, wxID_ANY, _T("HtmlLbox wxWidgets Sample"), |
201ca879 VZ |
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; | |
5f732810 VZ |
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, | |
9a9b4940 | 212 | _T("&Draw separators\tCtrl-D"), |
5f732810 VZ |
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")); | |
9a9b4940 VZ |
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")); | |
201ca879 VZ |
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 | ||
201ca879 VZ |
231 | // now append the freshly created menu to the menu bar... |
232 | wxMenuBar *menuBar = new wxMenuBar(); | |
233 | menuBar->Append(menuFile, _T("&File")); | |
5f732810 | 234 | menuBar->Append(menuHLbox, _T("&Listbox")); |
201ca879 VZ |
235 | menuBar->Append(helpMenu, _T("&Help")); |
236 | ||
5f732810 VZ |
237 | menuBar->Check(HtmlLbox_DrawSeparator, true); |
238 | ||
201ca879 VZ |
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); | |
be5a51fb | 246 | SetStatusText(_T("Welcome to wxWidgets!")); |
201ca879 VZ |
247 | #endif // wxUSE_STATUSBAR |
248 | ||
249 | // create the child controls | |
5f732810 | 250 | m_hlbox = new MyHtmlListBox(this); |
07850a49 | 251 | wxTextCtrl *text = new wxTextCtrl(this, wxID_ANY, _T(""), |
201ca879 VZ |
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); | |
8613d47b | 258 | sizer->Add(m_hlbox, 1, wxGROW); |
201ca879 VZ |
259 | sizer->Add(text, 1, wxGROW); |
260 | ||
261 | SetSizer(sizer); | |
262 | } | |
263 | ||
264 | MyFrame::~MyFrame() | |
265 | { | |
266 | delete wxLog::SetActiveTarget(NULL); | |
267 | } | |
268 | ||
5f732810 VZ |
269 | // ---------------------------------------------------------------------------- |
270 | // menu event handlers | |
271 | // ---------------------------------------------------------------------------- | |
272 | ||
201ca879 VZ |
273 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) |
274 | { | |
07850a49 WS |
275 | // true is to force the frame to close |
276 | Close(true); | |
201ca879 VZ |
277 | } |
278 | ||
279 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) | |
280 | { | |
281 | wxMessageBox(_T("This sample shows wxHtmlListBox class.\n") | |
282 | _T("\n") | |
283 |