]>
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"
43 #include "wx/numdlg.h"
45 #include "wx/htmllbox.h"
47 // you can also have a file containing HTML strings for testing, enable this if
49 //#define USE_HTML_FILE
51 #include "wx/textfile.h"
54 // ----------------------------------------------------------------------------
56 // ----------------------------------------------------------------------------
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"
63 // ----------------------------------------------------------------------------
65 // ----------------------------------------------------------------------------
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
72 MyHtmlListBox(wxWindow
*parent
, bool multi
= false);
74 void SetChangeSelFg(bool change
) { m_change
= change
; }
77 virtual wxString
OnGetItem(size_t n
) const;
79 // change the appearance by overriding these functions
80 virtual void OnDrawSeparator(wxDC
& dc
, wxRect
& rect
, size_t n
) const;
81 virtual wxColour
GetSelectedTextColour(const wxColour
& colFg
) const;
89 DECLARE_NO_COPY_CLASS(MyHtmlListBox
)
92 class MyFrame
: public wxFrame
99 void OnQuit(wxCommandEvent
& event
);
100 void OnAbout(wxCommandEvent
& event
);
102 void OnSetMargins(wxCommandEvent
& event
);
103 void OnDrawSeparator(wxCommandEvent
&) { m_hlbox
->RefreshAll(); }
104 void OnToggleMulti(wxCommandEvent
& event
);
105 void OnSelectAll(wxCommandEvent
& event
);
107 void OnSetBgCol(wxCommandEvent
& event
);
108 void OnSetSelBgCol(wxCommandEvent
& event
);
109 void OnSetSelFgCol(wxCommandEvent
& event
);
112 void OnUpdateUISelectAll(wxUpdateUIEvent
& event
);
114 void OnLboxSelect(wxCommandEvent
& event
);
115 void OnLboxDClick(wxCommandEvent
& event
)
117 wxLogMessage(_T("Listbox item %ld double clicked."), event
.GetInt());
121 MyHtmlListBox
*m_hlbox
;
123 // any class wishing to process wxWindows events must use this macro
124 DECLARE_EVENT_TABLE()
127 class MyApp
: public wxApp
130 virtual bool OnInit() { (new MyFrame())->Show(); return TRUE
; }
133 // ----------------------------------------------------------------------------
135 // ----------------------------------------------------------------------------
137 // IDs for the controls and the menu commands
144 HtmlLbox_DrawSeparator
,
145 HtmlLbox_ToggleMulti
,
149 HtmlLbox_SetSelBgCol
,
150 HtmlLbox_SetSelFgCol
,
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
158 // ----------------------------------------------------------------------------
159 // event tables and other macros for wxWindows
160 // ----------------------------------------------------------------------------
162 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
163 EVT_MENU(HtmlLbox_Quit
, MyFrame::OnQuit
)
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
)
170 EVT_MENU(HtmlLbox_About
, MyFrame::OnAbout
)
172 EVT_MENU(HtmlLbox_SetBgCol
, MyFrame::OnSetBgCol
)
173 EVT_MENU(HtmlLbox_SetSelBgCol
, MyFrame::OnSetSelBgCol
)
174 EVT_MENU(HtmlLbox_SetSelFgCol
, MyFrame::OnSetSelFgCol
)
176 EVT_UPDATE_UI(HtmlLbox_SelectAll
, MyFrame::OnUpdateUISelectAll
)
179 EVT_LISTBOX(wxID_ANY
, MyFrame::OnLboxSelect
)
180 EVT_LISTBOX_DCLICK(wxID_ANY
, MyFrame::OnLboxDClick
)
185 // ============================================================================
187 // ============================================================================
189 // ----------------------------------------------------------------------------
191 // ----------------------------------------------------------------------------
195 : wxFrame(NULL
, -1, _T("HtmlLbox wxWindows Sample"),
196 wxDefaultPosition
, wxSize(400, 500))
198 // set the frame icon
199 SetIcon(wxICON(mondrian
));
203 wxMenu
*menuFile
= new wxMenu
;
204 menuFile
->Append(HtmlLbox_Quit
, _T("E&xit\tAlt-X"), _T("Quit this program"));
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
,
212 _T("&Draw separators\tCtrl-D"),
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"));
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"));
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"));
231 // now append the freshly created menu to the menu bar...
232 wxMenuBar
*menuBar
= new wxMenuBar();
233 menuBar
->Append(menuFile
, _T("&File"));
234 menuBar
->Append(menuHLbox
, _T("&Listbox"));
235 menuBar
->Append(helpMenu
, _T("&Help"));
237 menuBar
->Check(HtmlLbox_DrawSeparator
, true);
239 // ... and attach this menu bar to the frame
241 #endif // wxUSE_MENUS
244 // create a status bar just for fun (by default with 1 pane only)
246 SetStatusText(_T("Welcome to wxWindows!"));
247 #endif // wxUSE_STATUSBAR
249 // create the child controls
250 m_hlbox
= new MyHtmlListBox(this);
251 wxTextCtrl
*text
= new wxTextCtrl(this, -1, _T(""),
252 wxDefaultPosition
, wxDefaultSize
,
254 delete wxLog::SetActiveTarget(new wxLogTextCtrl(text
));
257 wxSizer
*sizer
= new wxBoxSizer(wxHORIZONTAL
);
258 sizer
->Add(m_hlbox
, 1, wxGROW
);
259 sizer
->Add(text
, 1, wxGROW
);
266 delete wxLog::SetActiveTarget(NULL
);
269 // ----------------------------------------------------------------------------
270 // menu event handlers
271 // ----------------------------------------------------------------------------
273 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
275 // TRUE is to force the frame to close
279 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
281 wxMessageBox(_T("This sample shows wxHtmlListBox class.\n")
283 _T("© 2003 Vadim Zeitlin"),
284 _T("About HtmlLbox"),
285 wxOK
| wxICON_INFORMATION
,
289 void MyFrame::OnSetMargins(wxCommandEvent
& WXUNUSED(event
))
291 long margin
= wxGetNumberFromUser
293 _T("Enter the margins to use for the listbox items."),
295 _T("HtmlLbox: Set the margins"),
302 m_hlbox
->SetMargins(margin
, margin
);
303 m_hlbox
->RefreshAll();
307 void MyFrame::OnToggleMulti(wxCommandEvent
& event
)
309 // we need to recreate the listbox
310 wxSizer
*sizer
= GetSizer();
311 sizer
->Detach(m_hlbox
);
314 m_hlbox
= new MyHtmlListBox(this, event
.IsChecked());
315 sizer
->Prepend(m_hlbox
, 1, wxGROW
);
320 void MyFrame::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
322 m_hlbox
->SelectAll();
325 void MyFrame::OnUpdateUISelectAll(wxUpdateUIEvent
& event
)
327 event
.Enable( m_hlbox
&& m_hlbox
->HasMultipleSelection() );
330 void MyFrame::OnSetBgCol(wxCommandEvent
& WXUNUSED(event
))
332 wxColour col
= wxGetColourFromUser(this, m_hlbox
->GetBackgroundColour());
335 m_hlbox
->SetBackgroundColour(col
);
338 SetStatusText(_T("Background colour changed."));
342 void MyFrame::OnSetSelBgCol(wxCommandEvent
& WXUNUSED(event
))
344 wxColour col
= wxGetColourFromUser(this, m_hlbox
->GetSelectionBackground());
347 m_hlbox
->SetSelectionBackground(col
);
350 SetStatusText(_T("Selection background colour changed."));
354 void MyFrame::OnSetSelFgCol(wxCommandEvent
& event
)
356 m_hlbox
->SetChangeSelFg(!event
.IsChecked());
360 // ----------------------------------------------------------------------------
361 // listbox event handlers
362 // ----------------------------------------------------------------------------
364 void MyFrame::OnLboxSelect(wxCommandEvent
& event
)
366 wxLogMessage(_T("Listbox selection is now %ld."), event
.GetInt());
368 if ( m_hlbox
->HasMultipleSelection() )
373 unsigned long cookie
;
374 for ( int item
= m_hlbox
->GetFirstSelected(cookie
);
376 item
= m_hlbox
->GetNextSelected(cookie
) )
387 wxLogMessage(_T("Selected items: %s"), s
.c_str());
390 SetStatusText(wxString::Format(
391 _T("# items selected = %lu"),
392 (unsigned long)m_hlbox
->GetSelectedCount()
396 // ============================================================================
398 // ============================================================================
400 MyHtmlListBox::MyHtmlListBox(wxWindow
*parent
, bool multi
)
401 : wxHtmlListBox(parent
, -1, wxDefaultPosition
, wxDefaultSize
,
402 multi
? wxLB_MULTIPLE
: 0)
409 if ( !m_file
.Open(_T("results")) )
411 wxLogError(_T("Failed to open results file"));
415 SetItemCount(m_file
.GetLineCount());
424 void MyHtmlListBox::OnDrawSeparator(wxDC
& dc
, wxRect
& rect
, size_t) const
426 if ( ((MyFrame
*)GetParent())->
427 GetMenuBar()->IsChecked(HtmlLbox_DrawSeparator
) )
429 dc
.SetPen(*wxBLACK_DASHED_PEN
);
430 dc
.DrawLine(rect
.x
, rect
.y
, rect
.GetRight(), rect
.y
);
431 dc
.DrawLine(rect
.x
, rect
.GetBottom(), rect
.GetRight(), rect
.GetBottom());
435 wxString
MyHtmlListBox::OnGetItem(size_t n
) const
439 if ( m_file
.IsOpened() )
444 int level
= n
% 6 + 1;
445 return wxString::Format(_T("<h%d><font color=#%2x%2x%2x>")
446 _T("Item</font> <b>%lu</b>")
452 (unsigned long)n
, level
);
456 wxColour
MyHtmlListBox::GetSelectedTextColour(const wxColour
& colFg
) const
458 return m_change
? wxHtmlListBox::GetSelectedTextColour(colFg
) : colFg
;