added possibility to customize the listbox colours

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@21138 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2003-06-13 22:26:45 +00:00
parent 6874238c54
commit 9a9b4940a1
6 changed files with 252 additions and 56 deletions

View File

@@ -51,7 +51,7 @@ Destructor cleans up whatever resources we use.
\membersection{wxHtmlListBox::Create}\label{wxhtmllistboxcreate} \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 Creates the control and optionally sets the initial number of items in it
(it may also be set or changed later with (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 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} \membersection{wxHtmlListBox::OnGetItem}\label{wxhtmllistboxongetitem}

View File

@@ -17,6 +17,7 @@
class WXDLLEXPORT wxHtmlCell; class WXDLLEXPORT wxHtmlCell;
class WXDLLEXPORT wxHtmlWinParser; class WXDLLEXPORT wxHtmlWinParser;
class WXDLLEXPORT wxHtmlListBoxCache; class WXDLLEXPORT wxHtmlListBoxCache;
class WXDLLEXPORT wxHtmlListBoxStyle;
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// wxHtmlListBox // wxHtmlListBox
@@ -73,6 +74,20 @@ protected:
virtual wxString OnGetItemMarkup(size_t n) const; 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 // we implement both of these functions in terms of OnGetItem(), they are
// not supposed to be overridden by our descendants // not supposed to be overridden by our descendants
virtual void OnDrawItem(wxDC& dc, const wxRect& rect, size_t n) const; virtual void OnDrawItem(wxDC& dc, const wxRect& rect, size_t n) const;
@@ -90,11 +105,19 @@ protected:
void CacheItem(size_t n) const; void CacheItem(size_t n) const;
private: private:
// this class caches the pre-parsed HTML to speed up display
wxHtmlListBoxCache *m_cache; wxHtmlListBoxCache *m_cache;
// HTML parser we use // HTML parser we use
wxHtmlWinParser *m_htmlParser; 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() DECLARE_EVENT_TABLE()
}; };

View File

@@ -120,6 +120,9 @@ public:
// get the margins around each item // get the margins around each item
wxPoint GetMargins() const { return m_ptMargins; } wxPoint GetMargins() const { return m_ptMargins; }
// get the background colour of selected cells
const wxColour& GetSelectionBackground() const { return m_colBgSel; }
// operations // operations
// ---------- // ----------
@@ -179,6 +182,9 @@ public:
void SetMargins(const wxPoint& pt); void SetMargins(const wxPoint& pt);
void SetMargins(wxCoord x, wxCoord y) { SetMargins(wxPoint(x, y)); } void SetMargins(wxCoord x, wxCoord y) { SetMargins(wxPoint(x, y)); }
// change the background colour of the selected cells
void SetSelectionBackground(const wxColour& col);
protected: protected:
// the derived class must implement this function to actually draw the item // the derived class must implement this function to actually draw the item
@@ -258,6 +264,10 @@ private:
// margins // margins
wxPoint m_ptMargins; wxPoint m_ptMargins;
// the selection bg colour
wxColour m_colBgSel;
DECLARE_EVENT_TABLE() DECLARE_EVENT_TABLE()
}; };

View File

@@ -39,6 +39,8 @@
#include "wx/dc.h" #include "wx/dc.h"
#endif #endif
#include "wx/colordlg.h"
#include "wx/htmllbox.h" #include "wx/htmllbox.h"
// you can also have a file containing HTML strings for testing, enable this if // you can also have a file containing HTML strings for testing, enable this if
@@ -66,56 +68,22 @@
class MyHtmlListBox : public wxHtmlListBox class MyHtmlListBox : public wxHtmlListBox
{ {
public: public:
MyHtmlListBox(wxWindow *parent, bool multi = false) MyHtmlListBox(wxWindow *parent, bool multi = false);
: wxHtmlListBox(parent, -1, wxDefaultPosition, wxDefaultSize,
multi ? wxLB_MULTIPLE : 0)
{
SetMargins(5, 5);
#ifdef USE_HTML_FILE void SetChangeSelFg(bool change) { m_change = change; }
if ( !m_file.Open("results") )
{
wxLogError("Failed to open results file");
}
else
{
SetItemCount(m_file.GetLineCount());
}
#else
SetItemCount(10);
#endif
if ( HasMultipleSelection() )
Select(3);
else
SetSelection(3);
}
protected: protected:
virtual wxString OnGetItem(size_t n) const 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
}
// change the appearance by overriding these functions
virtual void OnDrawSeparator(wxDC& dc, wxRect& rect, size_t n) const; 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; wxTextFile m_file;
#endif
}; };
class MyFrame : public wxFrame class MyFrame : public wxFrame
@@ -133,6 +101,11 @@ public:
void OnToggleMulti(wxCommandEvent& event); void OnToggleMulti(wxCommandEvent& event);
void OnSelectAll(wxCommandEvent& event); void OnSelectAll(wxCommandEvent& event);
void OnSetBgCol(wxCommandEvent& event);
void OnSetSelBgCol(wxCommandEvent& event);
void OnSetSelFgCol(wxCommandEvent& event);
void OnUpdateUISelectAll(wxUpdateUIEvent& event); void OnUpdateUISelectAll(wxUpdateUIEvent& event);
void OnLboxSelect(wxCommandEvent& event); void OnLboxSelect(wxCommandEvent& event);
@@ -169,6 +142,10 @@ enum
HtmlLbox_ToggleMulti, HtmlLbox_ToggleMulti,
HtmlLbox_SelectAll, HtmlLbox_SelectAll,
HtmlLbox_SetBgCol,
HtmlLbox_SetSelBgCol,
HtmlLbox_SetSelFgCol,
// it is important for the id corresponding to the "About" command to have // 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 // this standard value as otherwise it won't be handled properly under Mac
// (where it is special and put into the "Apple" menu) // (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_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) EVT_UPDATE_UI(HtmlLbox_SelectAll, MyFrame::OnUpdateUISelectAll)
@@ -226,7 +206,7 @@ MyFrame::MyFrame()
_T("Set &margins...\tCtrl-G"), _T("Set &margins...\tCtrl-G"),
_T("Change the margins around the items")); _T("Change the margins around the items"));
menuHLbox->AppendCheckItem(HtmlLbox_DrawSeparator, menuHLbox->AppendCheckItem(HtmlLbox_DrawSeparator,
_T("Draw &separators\tCtrl-S"), _T("&Draw separators\tCtrl-D"),
_T("Toggle drawing separators between cells")); _T("Toggle drawing separators between cells"));
menuHLbox->AppendSeparator(); menuHLbox->AppendSeparator();
menuHLbox->AppendCheckItem(HtmlLbox_ToggleMulti, menuHLbox->AppendCheckItem(HtmlLbox_ToggleMulti,
@@ -234,6 +214,12 @@ MyFrame::MyFrame()
_T("Toggle multiple selection on/off")); _T("Toggle multiple selection on/off"));
menuHLbox->AppendSeparator(); menuHLbox->AppendSeparator();
menuHLbox->Append(HtmlLbox_SelectAll, _T("Select &all items\tCtrl-A")); 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 // the "About" item should be in the help menu
wxMenu *helpMenu = new wxMenu; wxMenu *helpMenu = new wxMenu;
@@ -297,7 +283,7 @@ void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
this); this);
} }
void MyFrame::OnSetMargins(wxCommandEvent&) void MyFrame::OnSetMargins(wxCommandEvent& WXUNUSED(event))
{ {
long margin = wxGetNumberFromUser long margin = wxGetNumberFromUser
( (
@@ -328,7 +314,7 @@ void MyFrame::OnToggleMulti(wxCommandEvent& event)
sizer->Layout(); sizer->Layout();
} }
void MyFrame::OnSelectAll(wxCommandEvent& event) void MyFrame::OnSelectAll(wxCommandEvent& WXUNUSED(event))
{ {
m_hlbox->SelectRange(0, m_hlbox->GetItemCount() - 1); m_hlbox->SelectRange(0, m_hlbox->GetItemCount() - 1);
} }
@@ -338,6 +324,36 @@ void MyFrame::OnUpdateUISelectAll(wxUpdateUIEvent& event)
event.Enable( m_hlbox && m_hlbox->HasMultipleSelection() ); 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 // listbox event handlers
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@@ -378,6 +394,34 @@ void MyFrame::OnLboxSelect(wxCommandEvent& event)
// MyHtmlListBox // 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 void MyHtmlListBox::OnDrawSeparator(wxDC& dc, wxRect& rect, size_t) const
{ {
if ( ((MyFrame *)GetParent())-> 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;
}

View File

@@ -37,8 +37,12 @@
#include "wx/html/forcelnk.h" #include "wx/html/forcelnk.h"
FORCE_WXHTML_MODULES() FORCE_WXHTML_MODULES()
// ---------------------------------------------------------------------------- // ============================================================================
// private classes // private classes
// ============================================================================
// ----------------------------------------------------------------------------
// wxHtmlListBoxCache
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// this class is used by wxHtmlListBox to cache the parsed representation of // this class is used by wxHtmlListBox to cache the parsed representation of
@@ -117,6 +121,32 @@ private:
size_t m_items[SIZE]; 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 // event tables
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@@ -136,6 +166,7 @@ END_EVENT_TABLE()
void wxHtmlListBox::Init() void wxHtmlListBox::Init()
{ {
m_htmlParser = NULL; m_htmlParser = NULL;
m_htmlRendStyle = new wxHtmlListBoxStyle(*this);
m_cache = new wxHtmlListBoxCache; m_cache = new wxHtmlListBoxCache;
} }
@@ -152,11 +183,30 @@ bool wxHtmlListBox::Create(wxWindow *parent,
wxHtmlListBox::~wxHtmlListBox() wxHtmlListBox::~wxHtmlListBox()
{ {
delete m_cache; delete m_cache;
if ( m_htmlParser ) if ( m_htmlParser )
{ {
delete m_htmlParser->GetDC(); delete m_htmlParser->GetDC();
delete m_htmlParser; 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; wxHtmlSelection htmlSel;
htmlSel.Set(wxPoint(0, 0), cell, wxPoint(INT_MAX, INT_MAX), cell); htmlSel.Set(wxPoint(0, 0), cell, wxPoint(INT_MAX, INT_MAX), cell);
htmlRendInfo.SetSelection(&htmlSel); 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 // note that we can't stop drawing exactly at the window boundary as then

View File

@@ -69,11 +69,12 @@ bool wxVListBox::Create(wxWindow *parent,
if ( !wxVScrolledWindow::Create(parent, id, pos, size, style, name) ) if ( !wxVScrolledWindow::Create(parent, id, pos, size, style, name) )
return false; return false;
SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX));
if ( style & wxLB_MULTIPLE ) if ( style & wxLB_MULTIPLE )
m_selStore = new wxSelectionStore; m_selStore = new wxSelectionStore;
SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX));
m_colBgSel = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT);
return true; return true;
} }
@@ -282,7 +283,7 @@ int wxVListBox::GetNextSelected(unsigned long& cookie) const
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// wxVListBox painting // wxVListBox appearance parameters
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
void wxVListBox::SetMargins(const wxPoint& pt) 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 wxCoord wxVListBox::OnGetLineHeight(size_t line) const
{ {
return OnMeasureItem(line) + 2*m_ptMargins.y; return OnMeasureItem(line) + 2*m_ptMargins.y;
@@ -337,10 +347,7 @@ void wxVListBox::OnPaint(wxPaintEvent& event)
{ {
if ( isSelected ) if ( isSelected )
{ {
wxBrush brush(wxSystemSettings:: dc.SetBrush(wxBrush(m_colBgSel, wxSOLID));
GetColour(wxSYS_COLOUR_HIGHLIGHT),
wxSOLID);
dc.SetBrush(brush);
} }
else // !selected else // !selected
{ {