]> git.saurik.com Git - wxWidgets.git/commitdiff
added possibility to customize the listbox colours
authorVadim Zeitlin <vadim@wxwidgets.org>
Fri, 13 Jun 2003 22:26:45 +0000 (22:26 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Fri, 13 Jun 2003 22:26:45 +0000 (22:26 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@21138 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

docs/latex/wx/htmllbox.tex
include/wx/htmllbox.h
include/wx/vlbox.h
samples/htlbox/htlbox.cpp
src/generic/htmllbox.cpp
src/generic/vlbox.cpp

index 9e6875eb17476029dbd210e10bab68d29c3d2cd6..63729f58564f41a3266f4b98c505868b41f9696c 100644 (file)
@@ -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}
 
index cd95ee26e992115650af094c10fedf64bc503be6..97f2ae413edae12f43b86f44e60572b4f8266aa2 100644 (file)
@@ -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()
 };
index e2a2dc8063e0b2e0de324e09195c9513a67e96f8..5e09100e7fb83345b7318eeb37a7990d03363046 100644 (file)
@@ -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()
 };
 
index adae3ed978a5110ee8e65e469676ad7d64ba9130..06563fe4d743bda450daae82485109cb4065ff0c 100644 (file)
@@ -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
 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("<h%d><font color=#%2x%2x%2x>")
-                                _T("Item</font> <b>%lu</b>")
-                                _T("</h%d>"),
-                                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("<h%d><font color=#%2x%2x%2x>")
+                            _T("Item</font> <b>%lu</b>")
+                            _T("</h%d>"),
+                            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;
+}
+
index 8f0ac4ab7eba9b5c14743ceee3fba3bc595e79fa..a9d2f468c3423d21481ea6731e8382593dbe7f17 100644 (file)
 #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
index bc846054a238b49dceae8b7b79ea47752fe1b91f..86501ead577034d4827e9063200a250793c4b7ef 100644 (file)
@@ -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
                 {