From 9a9b4940a1f8eef8eb3e7ff65ed45f1daf14c2d9 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Fri, 13 Jun 2003 22:26:45 +0000 Subject: [PATCH] added possibility to customize the listbox colours git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@21138 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- docs/latex/wx/htmllbox.tex | 36 ++++++++- include/wx/htmllbox.h | 23 ++++++ include/wx/vlbox.h | 10 +++ samples/htlbox/htlbox.cpp | 162 ++++++++++++++++++++++++++----------- src/generic/htmllbox.cpp | 56 ++++++++++++- src/generic/vlbox.cpp | 21 +++-- 6 files changed, 252 insertions(+), 56 deletions(-) diff --git a/docs/latex/wx/htmllbox.tex b/docs/latex/wx/htmllbox.tex index 9e6875eb17..63729f5856 100644 --- a/docs/latex/wx/htmllbox.tex +++ b/docs/latex/wx/htmllbox.tex @@ -51,7 +51,7 @@ Destructor cleans up whatever resources we use. \membersection{wxHtmlListBox::Create}\label{wxhtmllistboxcreate} -\func{bool}{Create}{\param{wxWindow* }{parent}, \param{wxWindowID }{id = wxID\_ANY}, \param{const wxPoint\& }{pos = wxDefaultPosition}, \param{const wxSize\& }{size = wxDefaultSize}, \param{size\_t }{countItems = 0}, \param{long }{style = 0}, \param{const wxString\& }{name = wxVListBoxNameStr}} +\func{bool}{Create}{\param{wxWindow* }{parent}, \param{wxWindowID }{id = wxID\_ANY}, \param{const wxPoint\& }{pos = wxDefaultPosition}, \param{const wxSize\& }{size = wxDefaultSize}, \param{long }{style = 0}, \param{const wxString\& }{name = wxVListBoxNameStr}} Creates the control and optionally sets the initial number of items in it (it may also be set or changed later with @@ -63,6 +63,40 @@ wxListBox styles can not be used here. Returns {\tt true} on success or {\tt false} if the control couldn't be created +\membersection{wxHtmlListBox::GetSelectedTextBgColour}\label{wxhtmllistboxgetselectedtextbgcolour} + +\constfunc{wxColour}{GetSelectedTextBgColour}{\param{const wxColour\& }{colBg}} + +This virtual function may be overridden to change the appearance of the +background of the selected cells in the same way as +\helpref{GetSelectedTextColour}{wxhtmllistboxgetselectedtextcolour}. + +It should be rarely, if ever, used because +\helpref{SetSelectionBackground}{wxvlistboxsetselectionbackground} allows to +change the selection background for all cells at once and doing anything more +fancy is probably going to look strangely. + +\wxheading{See also} + +\helpref{GetSelectedTextColour}{wxhtmllistboxgetselectedtextcolour} + + +\membersection{wxHtmlListBox::GetSelectedTextColour}\label{wxhtmllistboxgetselectedtextcolour} + +\constfunc{wxColour}{GetSelectedTextColour}{\param{const wxColour\& }{colFg}} + +This virtual function may be overridden to customize the appearance of the +selected cells. It is used to determine how the colour {\it colFg} is going to +look inside selection. By default all original colours are completely ignored +and the standard, system-dependent, selection colour is used but the program +may wish to override this to achieve some custom appearance. + +\wxheading{See also} + +\helpref{GetSelectedTextBgColour}{wxhtmllistboxgetselectedtextbgcolour},\\ +\helpref{SetSelectionBackground}{wxvlistboxsetselectionbackground},\\ +\helpref{wxSystemSettings::GetColour}{wxsystemsettingsgetcolour} + \membersection{wxHtmlListBox::OnGetItem}\label{wxhtmllistboxongetitem} diff --git a/include/wx/htmllbox.h b/include/wx/htmllbox.h index cd95ee26e9..97f2ae413e 100644 --- a/include/wx/htmllbox.h +++ b/include/wx/htmllbox.h @@ -17,6 +17,7 @@ class WXDLLEXPORT wxHtmlCell; class WXDLLEXPORT wxHtmlWinParser; class WXDLLEXPORT wxHtmlListBoxCache; +class WXDLLEXPORT wxHtmlListBoxStyle; // ---------------------------------------------------------------------------- // wxHtmlListBox @@ -73,6 +74,20 @@ protected: virtual wxString OnGetItemMarkup(size_t n) const; + // this method allows to customize the selection appearance: it may be used + // to specify the colour of the text which normally has the given colour + // colFg when it is inside the selection + // + // by default, the original colour is not used at all and all text has the + // same (default for this system) colour inside selection + virtual wxColour GetSelectedTextColour(const wxColour& colFg) const; + + // this is the same as GetSelectedTextColour() but allows to customize the + // background colour -- this is even more rarely used as you can change it + // globally using SetSelectionBackground() + virtual wxColour GetSelectedTextBgColour(const wxColour& colBg) const; + + // we implement both of these functions in terms of OnGetItem(), they are // not supposed to be overridden by our descendants virtual void OnDrawItem(wxDC& dc, const wxRect& rect, size_t n) const; @@ -90,11 +105,19 @@ protected: void CacheItem(size_t n) const; private: + // this class caches the pre-parsed HTML to speed up display wxHtmlListBoxCache *m_cache; // HTML parser we use wxHtmlWinParser *m_htmlParser; + // rendering style for the parser which allows us to customize our colours + wxHtmlListBoxStyle *m_htmlRendStyle; + + + // it calls our GetSelectedTextColour() and GetSelectedTextBgColour() + friend class wxHtmlListBoxStyle; + DECLARE_EVENT_TABLE() }; diff --git a/include/wx/vlbox.h b/include/wx/vlbox.h index e2a2dc8063..5e09100e7f 100644 --- a/include/wx/vlbox.h +++ b/include/wx/vlbox.h @@ -120,6 +120,9 @@ public: // get the margins around each item wxPoint GetMargins() const { return m_ptMargins; } + // get the background colour of selected cells + const wxColour& GetSelectionBackground() const { return m_colBgSel; } + // operations // ---------- @@ -179,6 +182,9 @@ public: void SetMargins(const wxPoint& pt); void SetMargins(wxCoord x, wxCoord y) { SetMargins(wxPoint(x, y)); } + // change the background colour of the selected cells + void SetSelectionBackground(const wxColour& col); + protected: // the derived class must implement this function to actually draw the item @@ -258,6 +264,10 @@ private: // margins wxPoint m_ptMargins; + // the selection bg colour + wxColour m_colBgSel; + + DECLARE_EVENT_TABLE() }; diff --git a/samples/htlbox/htlbox.cpp b/samples/htlbox/htlbox.cpp index adae3ed978..06563fe4d7 100644 --- a/samples/htlbox/htlbox.cpp +++ b/samples/htlbox/htlbox.cpp @@ -39,6 +39,8 @@ #include "wx/dc.h" #endif +#include "wx/colordlg.h" + #include "wx/htmllbox.h" // you can also have a file containing HTML strings for testing, enable this if @@ -66,56 +68,22 @@ class MyHtmlListBox : public wxHtmlListBox { public: - MyHtmlListBox(wxWindow *parent, bool multi = false) - : wxHtmlListBox(parent, -1, wxDefaultPosition, wxDefaultSize, - multi ? wxLB_MULTIPLE : 0) - { - SetMargins(5, 5); - -#ifdef USE_HTML_FILE - if ( !m_file.Open("results") ) - { - wxLogError("Failed to open results file"); - } - else - { - SetItemCount(m_file.GetLineCount()); - } -#else - SetItemCount(10); -#endif + MyHtmlListBox(wxWindow *parent, bool multi = false); - if ( HasMultipleSelection() ) - Select(3); - else - SetSelection(3); - } + void SetChangeSelFg(bool change) { m_change = change; } protected: - virtual wxString OnGetItem(size_t n) const - { -#ifdef USE_HTML_FILE - wxString s; - if ( m_file.IsOpened() ) - s = m_file[n]; - - return s; -#else - int level = n % 6 + 1; - return wxString::Format(_T("") - _T("Item %lu") - _T(""), - level, - abs(n - 192) % 256, - abs(n - 256) % 256, - abs(n - 128) % 256, - (unsigned long)n, level); -#endif - } + virtual wxString OnGetItem(size_t n) const; + // change the appearance by overriding these functions virtual void OnDrawSeparator(wxDC& dc, wxRect& rect, size_t n) const; + virtual wxColour GetSelectedTextColour(const wxColour& colFg) const; + bool m_change; + +#ifdef USE_HTML_FILE wxTextFile m_file; +#endif }; class MyFrame : public wxFrame @@ -133,6 +101,11 @@ public: void OnToggleMulti(wxCommandEvent& event); void OnSelectAll(wxCommandEvent& event); + void OnSetBgCol(wxCommandEvent& event); + void OnSetSelBgCol(wxCommandEvent& event); + void OnSetSelFgCol(wxCommandEvent& event); + + void OnUpdateUISelectAll(wxUpdateUIEvent& event); void OnLboxSelect(wxCommandEvent& event); @@ -169,6 +142,10 @@ enum HtmlLbox_ToggleMulti, HtmlLbox_SelectAll, + HtmlLbox_SetBgCol, + HtmlLbox_SetSelBgCol, + HtmlLbox_SetSelFgCol, + // it is important for the id corresponding to the "About" command to have // this standard value as otherwise it won't be handled properly under Mac // (where it is special and put into the "Apple" menu) @@ -189,6 +166,9 @@ BEGIN_EVENT_TABLE(MyFrame, wxFrame) EVT_MENU(HtmlLbox_About, MyFrame::OnAbout) + EVT_MENU(HtmlLbox_SetBgCol, MyFrame::OnSetBgCol) + EVT_MENU(HtmlLbox_SetSelBgCol, MyFrame::OnSetSelBgCol) + EVT_MENU(HtmlLbox_SetSelFgCol, MyFrame::OnSetSelFgCol) EVT_UPDATE_UI(HtmlLbox_SelectAll, MyFrame::OnUpdateUISelectAll) @@ -226,7 +206,7 @@ MyFrame::MyFrame() _T("Set &margins...\tCtrl-G"), _T("Change the margins around the items")); menuHLbox->AppendCheckItem(HtmlLbox_DrawSeparator, - _T("Draw &separators\tCtrl-S"), + _T("&Draw separators\tCtrl-D"), _T("Toggle drawing separators between cells")); menuHLbox->AppendSeparator(); menuHLbox->AppendCheckItem(HtmlLbox_ToggleMulti, @@ -234,6 +214,12 @@ MyFrame::MyFrame() _T("Toggle multiple selection on/off")); menuHLbox->AppendSeparator(); menuHLbox->Append(HtmlLbox_SelectAll, _T("Select &all items\tCtrl-A")); + menuHLbox->AppendSeparator(); + menuHLbox->Append(HtmlLbox_SetBgCol, _T("Set &background...\tCtrl-B")); + menuHLbox->Append(HtmlLbox_SetSelBgCol, + _T("Set &selection background...\tCtrl-S")); + menuHLbox->AppendCheckItem(HtmlLbox_SetSelFgCol, + _T("Keep &foreground in selection\tCtrl-F")); // the "About" item should be in the help menu wxMenu *helpMenu = new wxMenu; @@ -297,7 +283,7 @@ void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) this); } -void MyFrame::OnSetMargins(wxCommandEvent&) +void MyFrame::OnSetMargins(wxCommandEvent& WXUNUSED(event)) { long margin = wxGetNumberFromUser ( @@ -328,7 +314,7 @@ void MyFrame::OnToggleMulti(wxCommandEvent& event) sizer->Layout(); } -void MyFrame::OnSelectAll(wxCommandEvent& event) +void MyFrame::OnSelectAll(wxCommandEvent& WXUNUSED(event)) { m_hlbox->SelectRange(0, m_hlbox->GetItemCount() - 1); } @@ -338,6 +324,36 @@ void MyFrame::OnUpdateUISelectAll(wxUpdateUIEvent& event) event.Enable( m_hlbox && m_hlbox->HasMultipleSelection() ); } +void MyFrame::OnSetBgCol(wxCommandEvent& WXUNUSED(event)) +{ + wxColour col = wxGetColourFromUser(this, m_hlbox->GetBackgroundColour()); + if ( col.Ok() ) + { + m_hlbox->SetBackgroundColour(col); + m_hlbox->Refresh(); + + SetStatusText(_T("Background colour changed.")); + } +} + +void MyFrame::OnSetSelBgCol(wxCommandEvent& WXUNUSED(event)) +{ + wxColour col = wxGetColourFromUser(this, m_hlbox->GetSelectionBackground()); + if ( col.Ok() ) + { + m_hlbox->SetSelectionBackground(col); + m_hlbox->Refresh(); + + SetStatusText(_T("Selection background colour changed.")); + } +} + +void MyFrame::OnSetSelFgCol(wxCommandEvent& event) +{ + m_hlbox->SetChangeSelFg(!event.IsChecked()); + m_hlbox->Refresh(); +} + // ---------------------------------------------------------------------------- // listbox event handlers // ---------------------------------------------------------------------------- @@ -378,6 +394,34 @@ void MyFrame::OnLboxSelect(wxCommandEvent& event) // MyHtmlListBox // ============================================================================ +MyHtmlListBox::MyHtmlListBox(wxWindow *parent, bool multi) + : wxHtmlListBox(parent, -1, wxDefaultPosition, wxDefaultSize, + multi ? wxLB_MULTIPLE : 0) +{ + m_change = true; + + SetMargins(5, 5); + +#ifdef USE_HTML_FILE + if ( !m_file.Open("results") ) + { + wxLogError("Failed to open results file"); + } + else + { + SetItemCount(m_file.GetLineCount()); + } +#else + SetItemCount(10); +#endif + + // select something + if ( HasMultipleSelection() ) + Select(3); + else + SetSelection(3); +} + void MyHtmlListBox::OnDrawSeparator(wxDC& dc, wxRect& rect, size_t) const { if ( ((MyFrame *)GetParent())-> @@ -389,3 +433,29 @@ void MyHtmlListBox::OnDrawSeparator(wxDC& dc, wxRect& rect, size_t) const } } +wxString MyHtmlListBox::OnGetItem(size_t n) const +{ +#ifdef USE_HTML_FILE + wxString s; + if ( m_file.IsOpened() ) + s = m_file[n]; + + return s; +#else + int level = n % 6 + 1; + return wxString::Format(_T("") + _T("Item %lu") + _T(""), + level, + abs(n - 192) % 256, + abs(n - 256) % 256, + abs(n - 128) % 256, + (unsigned long)n, level); +#endif +} + +wxColour MyHtmlListBox::GetSelectedTextColour(const wxColour& colFg) const +{ + return m_change ? wxHtmlListBox::GetSelectedTextColour(colFg) : colFg; +} + diff --git a/src/generic/htmllbox.cpp b/src/generic/htmllbox.cpp index 8f0ac4ab7e..a9d2f468c3 100644 --- a/src/generic/htmllbox.cpp +++ b/src/generic/htmllbox.cpp @@ -37,8 +37,12 @@ #include "wx/html/forcelnk.h" FORCE_WXHTML_MODULES() -// ---------------------------------------------------------------------------- +// ============================================================================ // private classes +// ============================================================================ + +// ---------------------------------------------------------------------------- +// wxHtmlListBoxCache // ---------------------------------------------------------------------------- // this class is used by wxHtmlListBox to cache the parsed representation of @@ -117,6 +121,32 @@ private: size_t m_items[SIZE]; }; +// ---------------------------------------------------------------------------- +// wxHtmlListBoxStyle +// ---------------------------------------------------------------------------- + +// just forward wxDefaultHtmlRenderingStyle callbacks to the main class so that +// they could be overridden by the user code +class wxHtmlListBoxStyle : public wxDefaultHtmlRenderingStyle +{ +public: + wxHtmlListBoxStyle(wxHtmlListBox& hlbox) : m_hlbox(hlbox) { } + + virtual wxColour GetSelectedTextColour(const wxColour& colFg) + { + return m_hlbox.GetSelectedTextColour(colFg); + } + + virtual wxColour GetSelectedTextBgColour(const wxColour& colBg) + { + return m_hlbox.GetSelectedTextBgColour(colBg); + } + +private: + const wxHtmlListBox& m_hlbox; +}; + + // ---------------------------------------------------------------------------- // event tables // ---------------------------------------------------------------------------- @@ -136,6 +166,7 @@ END_EVENT_TABLE() void wxHtmlListBox::Init() { m_htmlParser = NULL; + m_htmlRendStyle = new wxHtmlListBoxStyle(*this); m_cache = new wxHtmlListBoxCache; } @@ -152,11 +183,30 @@ bool wxHtmlListBox::Create(wxWindow *parent, wxHtmlListBox::~wxHtmlListBox() { delete m_cache; + if ( m_htmlParser ) { delete m_htmlParser->GetDC(); delete m_htmlParser; } + + delete m_htmlRendStyle; +} + +// ---------------------------------------------------------------------------- +// wxHtmlListBox appearance +// ---------------------------------------------------------------------------- + +wxColour wxHtmlListBox::GetSelectedTextColour(const wxColour& colFg) const +{ + return m_htmlRendStyle-> + wxDefaultHtmlRenderingStyle::GetSelectedTextColour(colFg); +} + +wxColour +wxHtmlListBox::GetSelectedTextBgColour(const wxColour& WXUNUSED(colBg)) const +{ + return GetSelectionBackground(); } // ---------------------------------------------------------------------------- @@ -231,7 +281,9 @@ void wxHtmlListBox::OnDrawItem(wxDC& dc, const wxRect& rect, size_t n) const wxHtmlSelection htmlSel; htmlSel.Set(wxPoint(0, 0), cell, wxPoint(INT_MAX, INT_MAX), cell); htmlRendInfo.SetSelection(&htmlSel); - //htmlRendInfo.SetSelectionState(wxHTML_SEL_IN); + if ( m_htmlRendStyle ) + htmlRendInfo.SetStyle(m_htmlRendStyle); + htmlRendInfo.GetState().SetSelectionState(wxHTML_SEL_IN); } // note that we can't stop drawing exactly at the window boundary as then diff --git a/src/generic/vlbox.cpp b/src/generic/vlbox.cpp index bc846054a2..86501ead57 100644 --- a/src/generic/vlbox.cpp +++ b/src/generic/vlbox.cpp @@ -69,11 +69,12 @@ bool wxVListBox::Create(wxWindow *parent, if ( !wxVScrolledWindow::Create(parent, id, pos, size, style, name) ) return false; - SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX)); - if ( style & wxLB_MULTIPLE ) m_selStore = new wxSelectionStore; + SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX)); + m_colBgSel = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT); + return true; } @@ -282,7 +283,7 @@ int wxVListBox::GetNextSelected(unsigned long& cookie) const } // ---------------------------------------------------------------------------- -// wxVListBox painting +// wxVListBox appearance parameters // ---------------------------------------------------------------------------- void wxVListBox::SetMargins(const wxPoint& pt) @@ -295,6 +296,15 @@ void wxVListBox::SetMargins(const wxPoint& pt) } } +void wxVListBox::SetSelectionBackground(const wxColour& col) +{ + m_colBgSel = col; +} + +// ---------------------------------------------------------------------------- +// wxVListBox painting +// ---------------------------------------------------------------------------- + wxCoord wxVListBox::OnGetLineHeight(size_t line) const { return OnMeasureItem(line) + 2*m_ptMargins.y; @@ -337,10 +347,7 @@ void wxVListBox::OnPaint(wxPaintEvent& event) { if ( isSelected ) { - wxBrush brush(wxSystemSettings:: - GetColour(wxSYS_COLOUR_HIGHLIGHT), - wxSOLID); - dc.SetBrush(brush); + dc.SetBrush(wxBrush(m_colBgSel, wxSOLID)); } else // !selected { -- 2.45.2