]>
git.saurik.com Git - wxWidgets.git/blob - samples/htlbox/htlbox.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: HtmlLbox wxWindows sample
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwindows.org>
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
27 // for all others, include the necessary headers (this file is usually all you
28 // need because it includes almost all "standard" wxWindows headers)
36 #include "wx/msgdlg.h"
37 #include "wx/textctrl.h"
42 #include "wx/htmllbox.h"
44 // ----------------------------------------------------------------------------
46 // ----------------------------------------------------------------------------
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"
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
57 class MyApp
: public wxApp
60 virtual bool OnInit();
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
68 MyHtmlListBox(wxWindow
*parent
, bool multi
)
69 : wxHtmlListBox(parent
, -1, wxDefaultPosition
, wxDefaultSize
,
70 multi
? wxLB_MULTIPLE
: 0)
73 if ( HasMultipleSelection() )
81 virtual wxString
OnGetItem(size_t n
) const
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>")
91 (unsigned long)n
, level
);
94 virtual void OnDrawSeparator(wxDC
& dc
,
96 size_t WXUNUSED(n
)) const
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());
105 class MyFrame
: public wxFrame
112 void OnQuit(wxCommandEvent
& event
);
113 void OnAbout(wxCommandEvent
& event
);
115 void OnLboxSelect(wxCommandEvent
& event
)
117 wxLogMessage(_T("Listbox selection is now %ld."), event
.GetInt());
119 if ( m_hlbox
->HasMultipleSelection() )
124 unsigned long cookie
;
125 for ( int item
= m_hlbox
->GetFirstSelected(cookie
);
127 item
= m_hlbox
->GetNextSelected(cookie
) )
138 wxLogMessage(_T("Selected items: %s"), s
.c_str());
141 SetStatusText(wxString::Format(
142 _T("# items selected = %lu"),
143 (unsigned long)m_hlbox
->GetSelectedCount()
147 void OnLboxDClick(wxCommandEvent
& event
)
149 wxLogMessage(_T("Listbox item %ld double clicked."), event
.GetInt());
153 MyHtmlListBox
*m_hlbox
;
155 // any class wishing to process wxWindows events must use this macro
156 DECLARE_EVENT_TABLE()
159 // ----------------------------------------------------------------------------
161 // ----------------------------------------------------------------------------
163 // IDs for the controls and the menu commands
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
175 // ----------------------------------------------------------------------------
176 // event tables and other macros for wxWindows
177 // ----------------------------------------------------------------------------
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
)
186 EVT_LISTBOX(wxID_ANY
, MyFrame::OnLboxSelect
)
187 EVT_LISTBOX_DCLICK(wxID_ANY
, MyFrame::OnLboxDClick
)
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
197 // ============================================================================
199 // ============================================================================
201 // ----------------------------------------------------------------------------
202 // the application class
203 // ----------------------------------------------------------------------------
205 // 'Main program' equivalent: the program execution "starts" here
208 (new MyFrame())->Show();
213 // ----------------------------------------------------------------------------
215 // ----------------------------------------------------------------------------
219 : wxFrame(NULL
, -1, _T("HtmlLbox wxWindows Sample"),
220 wxDefaultPosition
, wxSize(400, 500))
222 // set the frame icon
223 SetIcon(wxICON(mondrian
));
227 wxMenu
*menuFile
= new wxMenu
;
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"));
233 menuFile
->Append(HtmlLbox_Quit
, _T("E&xit\tAlt-X"), _T("Quit this program"));
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"));
240 // ... and attach this menu bar to the frame
242 #endif // wxUSE_MENUS
245 // create a status bar just for fun (by default with 1 pane only)
247 SetStatusText(_T("Welcome to wxWindows!"));
248 #endif // wxUSE_STATUSBAR
250 // create the child controls
251 m_hlbox
= new MyHtmlListBox(this, true);
252 wxTextCtrl
*text
= new wxTextCtrl(this, -1, _T(""),
253 wxDefaultPosition
, wxDefaultSize
,
255 delete wxLog::SetActiveTarget(new wxLogTextCtrl(text
));
258 wxSizer
*sizer
= new wxBoxSizer(wxHORIZONTAL
);
259 sizer
->Add(m_hlbox
, 1, wxGROW
);
260 sizer
->Add(text
, 1, wxGROW
);
267 delete wxLog::SetActiveTarget(NULL
);
271 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
273 // TRUE is to force the frame to close
277 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
279 wxMessageBox(_T("This sample shows wxHtmlListBox class.\n")
281 _T("© 2003 Vadim Zeitlin"),
282 _T("About HtmlLbox"),
283 wxOK
| wxICON_INFORMATION
,