]>
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
32 #include "wx/textdlg.h"
36 #include "wx/msgdlg.h"
37 #include "wx/textctrl.h"
42 #include "wx/colordlg.h"
44 #include "wx/htmllbox.h"
46 // you can also have a file containing HTML strings for testing, enable this if
48 //#define USE_HTML_FILE
50 #include "wx/textfile.h"
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
57 // the application icon (under Windows and OS/2 it is in resources)
58 #if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__) || defined(__WXX11__)
59 #include "mondrian.xpm"
62 // ----------------------------------------------------------------------------
64 // ----------------------------------------------------------------------------
66 // to use wxHtmlListBox you must derive a new class from it as you must
67 // implement pure virtual OnGetItem()
68 class MyHtmlListBox
: public wxHtmlListBox
71 MyHtmlListBox(wxWindow
*parent
, bool multi
= false);
73 void SetChangeSelFg(bool change
) { m_change
= change
; }
76 virtual wxString
OnGetItem(size_t n
) const;
78 // change the appearance by overriding these functions
79 virtual void OnDrawSeparator(wxDC
& dc
, wxRect
& rect
, size_t n
) const;
80 virtual wxColour
GetSelectedTextColour(const wxColour
& colFg
) const;
89 class MyFrame
: public wxFrame
96 void OnQuit(wxCommandEvent
& event
);
97 void OnAbout(wxCommandEvent
& event
);
99 void OnSetMargins(wxCommandEvent
& event
);
100 void OnDrawSeparator(wxCommandEvent
&) { m_hlbox
->RefreshAll(); }
101 void OnToggleMulti(wxCommandEvent
& event
);
102 void OnSelectAll(wxCommandEvent
& event
);
104 void OnSetBgCol(wxCommandEvent
& event
);
105 void OnSetSelBgCol(wxCommandEvent
& event
);
106 void OnSetSelFgCol(wxCommandEvent
& event
);
109 void OnUpdateUISelectAll(wxUpdateUIEvent
& event
);
111 void OnLboxSelect(wxCommandEvent
& event
);
112 void OnLboxDClick(wxCommandEvent
& event
)
114 wxLogMessage(_T("Listbox item %ld double clicked."), event
.GetInt());
118 MyHtmlListBox
*m_hlbox
;
120 // any class wishing to process wxWindows events must use this macro
121 DECLARE_EVENT_TABLE()
124 class MyApp
: public wxApp
127 virtual bool OnInit() { (new MyFrame())->Show(); return TRUE
; }
130 // ----------------------------------------------------------------------------
132 // ----------------------------------------------------------------------------
134 // IDs for the controls and the menu commands
141 HtmlLbox_DrawSeparator
,
142 HtmlLbox_ToggleMulti
,
146 HtmlLbox_SetSelBgCol
,
147 HtmlLbox_SetSelFgCol
,
149 // it is important for the id corresponding to the "About" command to have
150 // this standard value as otherwise it won't be handled properly under Mac
151 // (where it is special and put into the "Apple" menu)
152 HtmlLbox_About
= wxID_ABOUT
155 // ----------------------------------------------------------------------------
156 // event tables and other macros for wxWindows
157 // ----------------------------------------------------------------------------
159 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
160 EVT_MENU(HtmlLbox_Quit
, MyFrame::OnQuit
)
162 EVT_MENU(HtmlLbox_SetMargins
, MyFrame::OnSetMargins
)
163 EVT_MENU(HtmlLbox_DrawSeparator
, MyFrame::OnDrawSeparator
)
164 EVT_MENU(HtmlLbox_ToggleMulti
, MyFrame::OnToggleMulti
)
165 EVT_MENU(HtmlLbox_SelectAll
, MyFrame::OnSelectAll
)
167 EVT_MENU(HtmlLbox_About
, MyFrame::OnAbout
)
169 EVT_MENU(HtmlLbox_SetBgCol
, MyFrame::OnSetBgCol
)
170 EVT_MENU(HtmlLbox_SetSelBgCol
, MyFrame::OnSetSelBgCol
)
171 EVT_MENU(HtmlLbox_SetSelFgCol
, MyFrame::OnSetSelFgCol
)
173 EVT_UPDATE_UI(HtmlLbox_SelectAll
, MyFrame::OnUpdateUISelectAll
)
176 EVT_LISTBOX(wxID_ANY
, MyFrame::OnLboxSelect
)
177 EVT_LISTBOX_DCLICK(wxID_ANY
, MyFrame::OnLboxDClick
)
182 // ============================================================================
184 // ============================================================================
186 // ----------------------------------------------------------------------------
188 // ----------------------------------------------------------------------------
192 : wxFrame(NULL
, -1, _T("HtmlLbox wxWindows Sample"),
193 wxDefaultPosition
, wxSize(400, 500))
195 // set the frame icon
196 SetIcon(wxICON(mondrian
));
200 wxMenu
*menuFile
= new wxMenu
;
201 menuFile
->Append(HtmlLbox_Quit
, _T("E&xit\tAlt-X"), _T("Quit this program"));
203 // create our specific menu
204 wxMenu
*menuHLbox
= new wxMenu
;
205 menuHLbox
->Append(HtmlLbox_SetMargins
,
206 _T("Set &margins...\tCtrl-G"),
207 _T("Change the margins around the items"));
208 menuHLbox
->AppendCheckItem(HtmlLbox_DrawSeparator
,
209 _T("&Draw separators\tCtrl-D"),
210 _T("Toggle drawing separators between cells"));
211 menuHLbox
->AppendSeparator();
212 menuHLbox
->AppendCheckItem(HtmlLbox_ToggleMulti
,
213 _T("&Multiple selection\tCtrl-M"),
214 _T("Toggle multiple selection on/off"));
215 menuHLbox
->AppendSeparator();
216 menuHLbox
->Append(HtmlLbox_SelectAll
, _T("Select &all items\tCtrl-A"));
217 menuHLbox
->AppendSeparator();
218 menuHLbox
->Append(HtmlLbox_SetBgCol
, _T("Set &background...\tCtrl-B"));
219 menuHLbox
->Append(HtmlLbox_SetSelBgCol
,
220 _T("Set &selection background...\tCtrl-S"));
221 menuHLbox
->AppendCheckItem(HtmlLbox_SetSelFgCol
,
222 _T("Keep &foreground in selection\tCtrl-F"));
224 // the "About" item should be in the help menu
225 wxMenu
*helpMenu
= new wxMenu
;
226 helpMenu
->Append(HtmlLbox_About
, _T("&About...\tF1"), _T("Show about dialog"));
228 // now append the freshly created menu to the menu bar...
229 wxMenuBar
*menuBar
= new wxMenuBar();
230 menuBar
->Append(menuFile
, _T("&File"));
231 menuBar
->Append(menuHLbox
, _T("&Listbox"));
232 menuBar
->Append(helpMenu
, _T("&Help"));
234 menuBar
->Check(HtmlLbox_DrawSeparator
, true);
236 // ... and attach this menu bar to the frame
238 #endif // wxUSE_MENUS
241 // create a status bar just for fun (by default with 1 pane only)
243 SetStatusText(_T("Welcome to wxWindows!"));
244 #endif // wxUSE_STATUSBAR
246 // create the child controls
247 m_hlbox
= new MyHtmlListBox(this);
248 wxTextCtrl
*text
= new wxTextCtrl(this, -1, _T(""),
249 wxDefaultPosition
, wxDefaultSize
,
251 delete wxLog::SetActiveTarget(new wxLogTextCtrl(text
));
254 wxSizer
*sizer
= new wxBoxSizer(wxHORIZONTAL
);
255 sizer
->Add(m_hlbox
, 1, wxGROW
);
256 sizer
->Add(text
, 1, wxGROW
);
263 delete wxLog::SetActiveTarget(NULL
);
266 // ----------------------------------------------------------------------------
267 // menu event handlers
268 // ----------------------------------------------------------------------------
270 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
272 // TRUE is to force the frame to close
276 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
278 wxMessageBox(_T("This sample shows wxHtmlListBox class.\n")
280 _T("© 2003 Vadim Zeitlin"),
281 _T("About HtmlLbox"),
282 wxOK
| wxICON_INFORMATION
,
286 void MyFrame::OnSetMargins(wxCommandEvent
& WXUNUSED(event
))
288 long margin
= wxGetNumberFromUser
290 _T("Enter the margins to use for the listbox items."),
292 _T("HtmlLbox: Set the margins"),
299 m_hlbox
->SetMargins(margin
, margin
);
300 m_hlbox
->RefreshAll();
304 void MyFrame::OnToggleMulti(wxCommandEvent
& event
)
306 // we need to recreate the listbox
307 wxSizer
*sizer
= GetSizer();
308 sizer
->Detach(m_hlbox
);
311 m_hlbox
= new MyHtmlListBox(this, event
.IsChecked());
312 sizer
->Prepend(m_hlbox
, 1, wxGROW
);
317 void MyFrame::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
319 m_hlbox
->SelectAll();
322 void MyFrame::OnUpdateUISelectAll(wxUpdateUIEvent
& event
)
324 event
.Enable( m_hlbox
&& m_hlbox
->HasMultipleSelection() );
327 void MyFrame::OnSetBgCol(wxCommandEvent
& WXUNUSED(event
))
329 wxColour col
= wxGetColourFromUser(this, m_hlbox
->GetBackgroundColour());
332 m_hlbox
->SetBackgroundColour(col
);
335 SetStatusText(_T("Background colour changed."));
339 void MyFrame::OnSetSelBgCol(wxCommandEvent
& WXUNUSED(event
))
341 wxColour col
= wxGetColourFromUser(this, m_hlbox
->GetSelectionBackground());
344 m_hlbox
->SetSelectionBackground(col
);
347 SetStatusText(_T("Selection background colour changed."));
351 void MyFrame::OnSetSelFgCol(wxCommandEvent
& event
)
353 m_hlbox
->SetChangeSelFg(!event
.IsChecked());
357 // ----------------------------------------------------------------------------
358 // listbox event handlers
359 // ----------------------------------------------------------------------------
361 void MyFrame::OnLboxSelect(wxCommandEvent
& event
)
363 wxLogMessage(_T("Listbox selection is now %ld."), event
.GetInt());
365 if ( m_hlbox
->HasMultipleSelection() )
370 unsigned long cookie
;
371 for ( int item
= m_hlbox
->GetFirstSelected(cookie
);
373 item
= m_hlbox
->GetNextSelected(cookie
) )
384 wxLogMessage(_T("Selected items: %s"), s
.c_str());
387 SetStatusText(wxString::Format(
388 _T("# items selected = %lu"),
389 (unsigned long)m_hlbox
->GetSelectedCount()
393 // ============================================================================
395 // ============================================================================
397 MyHtmlListBox::MyHtmlListBox(wxWindow
*parent
, bool multi
)
398 : wxHtmlListBox(parent
, -1, wxDefaultPosition
, wxDefaultSize
,
399 multi
? wxLB_MULTIPLE
: 0)
406 if ( !m_file
.Open("results") )
408 wxLogError("Failed to open results file");
412 SetItemCount(m_file
.GetLineCount());
421 void MyHtmlListBox::OnDrawSeparator(wxDC
& dc
, wxRect
& rect
, size_t) const
423 if ( ((MyFrame
*)GetParent())->
424 GetMenuBar()->IsChecked(HtmlLbox_DrawSeparator
) )
426 dc
.SetPen(*wxBLACK_DASHED_PEN
);
427 dc
.DrawLine(rect
.x
, rect
.y
, rect
.GetRight(), rect
.y
);
428 dc
.DrawLine(rect
.x
, rect
.GetBottom(), rect
.GetRight(), rect
.GetBottom());
432 wxString
MyHtmlListBox::OnGetItem(size_t n
) const
436 if ( m_file
.IsOpened() )
441 int level
= n
% 6 + 1;
442 return wxString::Format(_T("<h%d><font color=#%2x%2x%2x>")
443 _T("Item</font> <b>%lu</b>")
449 (unsigned long)n
, level
);
453 wxColour
MyHtmlListBox::GetSelectedTextColour(const wxColour
& colFg
) const
455 return m_change
? wxHtmlListBox::GetSelectedTextColour(colFg
) : colFg
;