]>
git.saurik.com Git - wxWidgets.git/blob - samples/htlbox/htlbox.cpp
adae3ed978a5110ee8e65e469676ad7d64ba9130
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
32 #include "wx/textdlg.h"
36 #include "wx/msgdlg.h"
37 #include "wx/textctrl.h"
42 #include "wx/htmllbox.h"
44 // you can also have a file containing HTML strings for testing, enable this if
46 //#define USE_HTML_FILE
48 #include "wx/textfile.h"
51 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
55 // the application icon (under Windows and OS/2 it is in resources)
56 #if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__) || defined(__WXX11__)
57 #include "mondrian.xpm"
60 // ----------------------------------------------------------------------------
62 // ----------------------------------------------------------------------------
64 // to use wxHtmlListBox you must derive a new class from it as you must
65 // implement pure virtual OnGetItem()
66 class MyHtmlListBox
: public wxHtmlListBox
69 MyHtmlListBox(wxWindow
*parent
, bool multi
= false)
70 : wxHtmlListBox(parent
, -1, wxDefaultPosition
, wxDefaultSize
,
71 multi
? wxLB_MULTIPLE
: 0)
76 if ( !m_file
.Open("results") )
78 wxLogError("Failed to open results file");
82 SetItemCount(m_file
.GetLineCount());
88 if ( HasMultipleSelection() )
95 virtual wxString
OnGetItem(size_t n
) const
99 if ( m_file
.IsOpened() )
104 int level
= n
% 6 + 1;
105 return wxString::Format(_T("<h%d><font color=#%2x%2x%2x>")
106 _T("Item</font> <b>%lu</b>")
112 (unsigned long)n
, level
);
116 virtual void OnDrawSeparator(wxDC
& dc
, wxRect
& rect
, size_t n
) const;
121 class MyFrame
: public wxFrame
128 void OnQuit(wxCommandEvent
& event
);
129 void OnAbout(wxCommandEvent
& event
);
131 void OnSetMargins(wxCommandEvent
& event
);
132 void OnDrawSeparator(wxCommandEvent
&) { m_hlbox
->RefreshAll(); }
133 void OnToggleMulti(wxCommandEvent
& event
);
134 void OnSelectAll(wxCommandEvent
& event
);
136 void OnUpdateUISelectAll(wxUpdateUIEvent
& event
);
138 void OnLboxSelect(wxCommandEvent
& event
);
139 void OnLboxDClick(wxCommandEvent
& event
)
141 wxLogMessage(_T("Listbox item %ld double clicked."), event
.GetInt());
145 MyHtmlListBox
*m_hlbox
;
147 // any class wishing to process wxWindows events must use this macro
148 DECLARE_EVENT_TABLE()
151 class MyApp
: public wxApp
154 virtual bool OnInit() { (new MyFrame())->Show(); return TRUE
; }
157 // ----------------------------------------------------------------------------
159 // ----------------------------------------------------------------------------
161 // IDs for the controls and the menu commands
168 HtmlLbox_DrawSeparator
,
169 HtmlLbox_ToggleMulti
,
172 // it is important for the id corresponding to the "About" command to have
173 // this standard value as otherwise it won't be handled properly under Mac
174 // (where it is special and put into the "Apple" menu)
175 HtmlLbox_About
= wxID_ABOUT
178 // ----------------------------------------------------------------------------
179 // event tables and other macros for wxWindows
180 // ----------------------------------------------------------------------------
182 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
183 EVT_MENU(HtmlLbox_Quit
, MyFrame::OnQuit
)
185 EVT_MENU(HtmlLbox_SetMargins
, MyFrame::OnSetMargins
)
186 EVT_MENU(HtmlLbox_DrawSeparator
, MyFrame::OnDrawSeparator
)
187 EVT_MENU(HtmlLbox_ToggleMulti
, MyFrame::OnToggleMulti
)
188 EVT_MENU(HtmlLbox_SelectAll
, MyFrame::OnSelectAll
)
190 EVT_MENU(HtmlLbox_About
, MyFrame::OnAbout
)
193 EVT_UPDATE_UI(HtmlLbox_SelectAll
, MyFrame::OnUpdateUISelectAll
)
196 EVT_LISTBOX(wxID_ANY
, MyFrame::OnLboxSelect
)
197 EVT_LISTBOX_DCLICK(wxID_ANY
, MyFrame::OnLboxDClick
)
202 // ============================================================================
204 // ============================================================================
206 // ----------------------------------------------------------------------------
208 // ----------------------------------------------------------------------------
212 : wxFrame(NULL
, -1, _T("HtmlLbox wxWindows Sample"),
213 wxDefaultPosition
, wxSize(400, 500))
215 // set the frame icon
216 SetIcon(wxICON(mondrian
));
220 wxMenu
*menuFile
= new wxMenu
;
221 menuFile
->Append(HtmlLbox_Quit
, _T("E&xit\tAlt-X"), _T("Quit this program"));
223 // create our specific menu
224 wxMenu
*menuHLbox
= new wxMenu
;
225 menuHLbox
->Append(HtmlLbox_SetMargins
,
226 _T("Set &margins...\tCtrl-G"),
227 _T("Change the margins around the items"));
228 menuHLbox
->AppendCheckItem(HtmlLbox_DrawSeparator
,
229 _T("Draw &separators\tCtrl-S"),
230 _T("Toggle drawing separators between cells"));
231 menuHLbox
->AppendSeparator();
232 menuHLbox
->AppendCheckItem(HtmlLbox_ToggleMulti
,
233 _T("&Multiple selection\tCtrl-M"),
234 _T("Toggle multiple selection on/off"));
235 menuHLbox
->AppendSeparator();
236 menuHLbox
->Append(HtmlLbox_SelectAll
, _T("Select &all items\tCtrl-A"));
238 // the "About" item should be in the help menu
239 wxMenu
*helpMenu
= new wxMenu
;
240 helpMenu
->Append(HtmlLbox_About
, _T("&About...\tF1"), _T("Show about dialog"));
242 // now append the freshly created menu to the menu bar...
243 wxMenuBar
*menuBar
= new wxMenuBar();
244 menuBar
->Append(menuFile
, _T("&File"));
245 menuBar
->Append(menuHLbox
, _T("&Listbox"));
246 menuBar
->Append(helpMenu
, _T("&Help"));
248 menuBar
->Check(HtmlLbox_DrawSeparator
, true);
250 // ... and attach this menu bar to the frame
252 #endif // wxUSE_MENUS
255 // create a status bar just for fun (by default with 1 pane only)
257 SetStatusText(_T("Welcome to wxWindows!"));
258 #endif // wxUSE_STATUSBAR
260 // create the child controls
261 m_hlbox
= new MyHtmlListBox(this);
262 wxTextCtrl
*text
= new wxTextCtrl(this, -1, _T(""),
263 wxDefaultPosition
, wxDefaultSize
,
265 delete wxLog::SetActiveTarget(new wxLogTextCtrl(text
));
268 wxSizer
*sizer
= new wxBoxSizer(wxHORIZONTAL
);
269 sizer
->Add(m_hlbox
, 1, wxGROW
);
270 sizer
->Add(text
, 1, wxGROW
);
277 delete wxLog::SetActiveTarget(NULL
);
280 // ----------------------------------------------------------------------------
281 // menu event handlers
282 // ----------------------------------------------------------------------------
284 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
286 // TRUE is to force the frame to close
290 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
292 wxMessageBox(_T("This sample shows wxHtmlListBox class.\n")
294 _T("© 2003 Vadim Zeitlin"),
295 _T("About HtmlLbox"),
296 wxOK
| wxICON_INFORMATION
,
300 void MyFrame::OnSetMargins(wxCommandEvent
&)
302 long margin
= wxGetNumberFromUser
304 _T("Enter the margins to use for the listbox items."),
306 _T("HtmlLbox: Set the margins"),
313 m_hlbox
->SetMargins(margin
, margin
);
314 m_hlbox
->RefreshAll();
318 void MyFrame::OnToggleMulti(wxCommandEvent
& event
)
320 // we need to recreate the listbox
321 wxSizer
*sizer
= GetSizer();
322 sizer
->Detach(m_hlbox
);
325 m_hlbox
= new MyHtmlListBox(this, event
.IsChecked());
326 sizer
->Prepend(m_hlbox
, 1, wxGROW
);
331 void MyFrame::OnSelectAll(wxCommandEvent
& event
)
333 m_hlbox
->SelectRange(0, m_hlbox
->GetItemCount() - 1);
336 void MyFrame::OnUpdateUISelectAll(wxUpdateUIEvent
& event
)
338 event
.Enable( m_hlbox
&& m_hlbox
->HasMultipleSelection() );
341 // ----------------------------------------------------------------------------
342 // listbox event handlers
343 // ----------------------------------------------------------------------------
345 void MyFrame::OnLboxSelect(wxCommandEvent
& event
)
347 wxLogMessage(_T("Listbox selection is now %ld."), event
.GetInt());
349 if ( m_hlbox
->HasMultipleSelection() )
354 unsigned long cookie
;
355 for ( int item
= m_hlbox
->GetFirstSelected(cookie
);
357 item
= m_hlbox
->GetNextSelected(cookie
) )
368 wxLogMessage(_T("Selected items: %s"), s
.c_str());
371 SetStatusText(wxString::Format(
372 _T("# items selected = %lu"),
373 (unsigned long)m_hlbox
->GetSelectedCount()
377 // ============================================================================
379 // ============================================================================
381 void MyHtmlListBox::OnDrawSeparator(wxDC
& dc
, wxRect
& rect
, size_t) const
383 if ( ((MyFrame
*)GetParent())->
384 GetMenuBar()->IsChecked(HtmlLbox_DrawSeparator
) )
386 dc
.SetPen(*wxBLACK_DASHED_PEN
);
387 dc
.DrawLine(rect
.x
, rect
.y
, rect
.GetRight(), rect
.y
);
388 dc
.DrawLine(rect
.x
, rect
.GetBottom(), rect
.GetRight(), rect
.GetBottom());