generic wxListCtrl colour/font setting

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@4835 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
1999-12-06 10:43:35 +00:00
parent 1f897d251f
commit 0530737d1e
5 changed files with 111 additions and 54 deletions

View File

@@ -18,6 +18,7 @@ all (GUI):
- wxMenu(Bar)::Insert() and Remove() functions for dynamic menu menagament - wxMenu(Bar)::Insert() and Remove() functions for dynamic menu menagament
- wxToolBar supports arbitrary controls (not only buttons) and can be - wxToolBar supports arbitrary controls (not only buttons) and can be
dynamically changed (Delete/Insert functions) dynamically changed (Delete/Insert functions)
- wxListCtrl allows setting colour/fonts for individual items
- wxDC::DrawRotatedText() (contributed by Hans-Joachim Baader, limited to - wxDC::DrawRotatedText() (contributed by Hans-Joachim Baader, limited to
+/-90 degrees for now - contributions to improve it are welcome!) +/-90 degrees for now - contributions to improve it are welcome!)

View File

@@ -402,7 +402,7 @@ For list view mode (only), inserts a column. For more details, see \helpref{wxLi
\pythonnote{In place of a single overloaded method name, wxPython \pythonnote{In place of a single overloaded method name, wxPython
implements the following methods:\par implements the following methods:\par
\indented{2cm}{\begin{twocollist} \indented{2cm}{\begin{twocollist}
\twocolitem{\bf{InsertColumn(col, heading, format=wxLIST_FORMAT_LEFT, \twocolitem{\bf{InsertColumn(col, heading, format=wxLIST\_FORMAT\_LEFT,
width=-1)}}{Creates a column using a header string only.} width=-1)}}{Creates a column using a header string only.}
\twocolitem{\bf{InsertColumnInfo(col, item)}}{Creates a column using a \twocolitem{\bf{InsertColumnInfo(col, item)}}{Creates a column using a
wxListInfo.} wxListInfo.}
@@ -529,6 +529,12 @@ The {\bf m\_mask} member contains a bitlist specifying which of the other fields
The {\bf m\_stateMask} and {\bf m\_state} members take flags from the following: The {\bf m\_stateMask} and {\bf m\_state} members take flags from the following:
The wxListItem object can also contain item-specific colour and font
information: for this you need to call one of SetTextColour(),
SetBackgroundColour() or SetFont() functions on it passing it the colour/font
to use. If the colour/font is not specified, the default list control
colour/font is used.
\twocolwidtha{5cm} \twocolwidtha{5cm}
\begin{twocollist}\itemsep=0pt \begin{twocollist}\itemsep=0pt
\twocolitem{wxLIST\_STATE\_DONTCARE}{Don't care what the state is. Win32 only. } \twocolitem{wxLIST\_STATE\_DONTCARE}{Don't care what the state is. Win32 only. }

View File

@@ -63,10 +63,13 @@ public:
long m_data; long m_data;
int m_xpos,m_ypos; int m_xpos,m_ypos;
int m_width,m_height; int m_width,m_height;
wxColour *m_colour;
wxListItemAttr *m_attr;
public: public:
wxListItemData(); wxListItemData();
~wxListItemData() { delete m_attr; }
wxListItemData( const wxListItem &info ); wxListItemData( const wxListItem &info );
void SetItem( const wxListItem &info ); void SetItem( const wxListItem &info );
void SetText( const wxString &s ); void SetText( const wxString &s );
@@ -74,18 +77,19 @@ public:
void SetData( long data ); void SetData( long data );
void SetPosition( int x, int y ); void SetPosition( int x, int y );
void SetSize( int width, int height ); void SetSize( int width, int height );
void SetColour( wxColour *col );
bool HasImage() const; bool HasImage() const;
bool HasText() const; bool HasText() const;
bool IsHit( int x, int y ) const; bool IsHit( int x, int y ) const;
void GetText( wxString &s ); void GetText( wxString &s );
const wxString& GetText() { return m_text; }
int GetX( void ) const; int GetX( void ) const;
int GetY( void ) const; int GetY( void ) const;
int GetWidth() const; int GetWidth() const;
int GetHeight() const; int GetHeight() const;
int GetImage() const; int GetImage() const;
void GetItem( wxListItem &info ); void GetItem( wxListItem &info ) const;
wxColour *GetColour();
wxListItemAttr *GetAttributes() const { return m_attr; }
private: private:
DECLARE_DYNAMIC_CLASS(wxListItemData); DECLARE_DYNAMIC_CLASS(wxListItemData);
@@ -174,6 +178,10 @@ public:
void AssignRect( wxRect &dest, const wxRect &source ); void AssignRect( wxRect &dest, const wxRect &source );
private: private:
void SetAttributes(wxDC *dc,
const wxListItemAttr *attr,
const wxColour& colText, const wxFont& font);
DECLARE_DYNAMIC_CLASS(wxListLineData); DECLARE_DYNAMIC_CLASS(wxListLineData);
}; };
@@ -241,8 +249,9 @@ public:
bool *accept, wxString *res, wxListMainWindow *owner, bool *accept, wxString *res, wxListMainWindow *owner,
const wxString &value = "", const wxString &value = "",
const wxPoint &pos = wxDefaultPosition, const wxSize &size = wxDefaultSize, const wxPoint &pos = wxDefaultPosition, const wxSize &size = wxDefaultSize,
int style = 0,
#if wxUSE_VALIDATORS #if wxUSE_VALIDATORS
int style = 0, const wxValidator& validator = wxDefaultValidator, const wxValidator& validator = wxDefaultValidator,
#endif #endif
const wxString &name = "wxListTextCtrlText" ); const wxString &name = "wxListTextCtrlText" );
void OnChar( wxKeyEvent &event ); void OnChar( wxKeyEvent &event );
@@ -313,7 +322,7 @@ public:
void OnSetFocus( wxFocusEvent &event ); void OnSetFocus( wxFocusEvent &event );
void OnKillFocus( wxFocusEvent &event ); void OnKillFocus( wxFocusEvent &event );
void OnSize( wxSizeEvent &event ); void OnSize( wxSizeEvent &event );
void OnScroll(wxScrollWinEvent& event) ; void OnScroll(wxScrollWinEvent& event) ;
void DrawImage( int index, wxDC *dc, int x, int y ); void DrawImage( int index, wxDC *dc, int x, int y );
void GetImageSize( int index, int &width, int &height ); void GetImageSize( int index, int &width, int &height );
@@ -411,8 +420,8 @@ public:
void SetItemSpacing( int spacing, bool isSmall = FALSE ); void SetItemSpacing( int spacing, bool isSmall = FALSE );
int GetItemSpacing( bool isSmall ) const; int GetItemSpacing( bool isSmall ) const;
int GetSelectedItemCount() const; int GetSelectedItemCount() const;
// wxColour GetTextColour() const; // wxGLC has colours for every Item (see wxListItem) wxColour GetTextColour() const;
// void SetTextColour(const wxColour& col); void SetTextColour(const wxColour& col);
long GetTopItem() const; long GetTopItem() const;
void SetSingleStyle( long style, bool add = TRUE ) ; void SetSingleStyle( long style, bool add = TRUE ) ;

View File

@@ -291,7 +291,7 @@ void MyFrame::OnReportView(wxCommandEvent& WXUNUSED(event))
// we leave all mask fields to 0 and only change the colour // we leave all mask fields to 0 and only change the colour
wxListItem item; wxListItem item;
item.m_itemId = 0; item.m_itemId = 0;
item.SetTextColour(*wxBLUE); item.SetTextColour(*wxRED);
m_listCtrl->SetItem( item ); m_listCtrl->SetItem( item );
item.m_itemId = 2; item.m_itemId = 2;

View File

@@ -41,14 +41,15 @@ wxListItemData::wxListItemData()
m_ypos = 0; m_ypos = 0;
m_width = 0; m_width = 0;
m_height = 0; m_height = 0;
m_colour = wxBLACK; m_attr = NULL;
} }
wxListItemData::wxListItemData( const wxListItem &info ) wxListItemData::wxListItemData( const wxListItem &info )
{ {
m_image = -1; m_image = -1;
m_data = 0; m_data = 0;
m_colour = (wxColour *)&info.GetTextColour(); m_attr = NULL;
SetItem( info ); SetItem( info );
} }
@@ -57,7 +58,15 @@ void wxListItemData::SetItem( const wxListItem &info )
if (info.m_mask & wxLIST_MASK_TEXT) m_text = info.m_text; if (info.m_mask & wxLIST_MASK_TEXT) m_text = info.m_text;
if (info.m_mask & wxLIST_MASK_IMAGE) m_image = info.m_image; if (info.m_mask & wxLIST_MASK_IMAGE) m_image = info.m_image;
if (info.m_mask & wxLIST_MASK_DATA) m_data = info.m_data; if (info.m_mask & wxLIST_MASK_DATA) m_data = info.m_data;
m_colour = (wxColour *)&info.GetTextColour();
if ( info.HasAttributes() )
{
if ( m_attr )
*m_attr = *info.GetAttributes();
else
m_attr = new wxListItemAttr(*info.GetAttributes());
}
m_xpos = 0; m_xpos = 0;
m_ypos = 0; m_ypos = 0;
m_width = info.m_width; m_width = info.m_width;
@@ -91,11 +100,6 @@ void wxListItemData::SetSize( int width, int height )
if (height != -1) m_height = height; if (height != -1) m_height = height;
} }
void wxListItemData::SetColour( wxColour *col )
{
m_colour = col;
}
bool wxListItemData::HasImage() const bool wxListItemData::HasImage() const
{ {
return (m_image >= 0); return (m_image >= 0);
@@ -141,17 +145,21 @@ int wxListItemData::GetImage() const
return m_image; return m_image;
} }
void wxListItemData::GetItem( wxListItem &info ) void wxListItemData::GetItem( wxListItem &info ) const
{ {
info.m_text = m_text; info.m_text = m_text;
info.m_image = m_image; info.m_image = m_image;
info.m_data = m_data; info.m_data = m_data;
info.SetTextColour(*m_colour);
}
wxColour *wxListItemData::GetColour() if ( m_attr )
{ {
return m_colour; if ( m_attr->HasTextColour() )
info.SetTextColour(m_attr->GetTextColour());
if ( m_attr->HasBackgroundColour() )
info.SetBackgroundColour(m_attr->GetBackgroundColour());
if ( m_attr->HasFont() )
info.SetFont(m_attr->GetFont());
}
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@@ -286,8 +294,7 @@ void wxListLineData::CalculateSize( wxDC *dc, int spacing )
if (node) if (node)
{ {
wxListItemData *item = (wxListItemData*)node->Data(); wxListItemData *item = (wxListItemData*)node->Data();
wxString s; wxString s = item->GetText();
item->GetText( s );
long lw,lh; long lw,lh;
dc->GetTextExtent( s, &lw, &lh ); dc->GetTextExtent( s, &lw, &lh );
if (lw > m_spacing) m_bound_all.width = lw; if (lw > m_spacing) m_bound_all.width = lw;
@@ -300,8 +307,7 @@ void wxListLineData::CalculateSize( wxDC *dc, int spacing )
if (node) if (node)
{ {
wxListItemData *item = (wxListItemData*)node->Data(); wxListItemData *item = (wxListItemData*)node->Data();
wxString s; wxString s = item->GetText();
item->GetText( s );
long lw,lh; long lw,lh;
dc->GetTextExtent( s, &lw, &lh ); dc->GetTextExtent( s, &lw, &lh );
m_bound_all.width = lw; m_bound_all.width = lw;
@@ -577,6 +583,30 @@ int wxListLineData::GetImage( int index )
return -1; return -1;
} }
void wxListLineData::SetAttributes(wxDC *dc,
const wxListItemAttr *attr,
const wxColour& colText,
const wxFont& font)
{
if ( attr && attr->HasTextColour() )
{
dc->SetTextForeground(attr->GetTextColour());
}
else
{
dc->SetTextForeground(colText);
}
if ( attr && attr->HasFont() )
{
dc->SetFont(attr->GetFont());
}
else
{
dc->SetFont(font);
}
}
void wxListLineData::DoDraw( wxDC *dc, bool hilight, bool paintBG ) void wxListLineData::DoDraw( wxDC *dc, bool hilight, bool paintBG )
{ {
long dev_x = dc->LogicalToDeviceX( m_bound_all.x-2 ); long dev_x = dc->LogicalToDeviceX( m_bound_all.x-2 );
@@ -589,27 +619,51 @@ void wxListLineData::DoDraw( wxDC *dc, bool hilight, bool paintBG )
return; return;
} }
if (paintBG) wxWindow *listctrl = m_owner->GetParent();
// default foreground colour
wxColour colText;
if ( hilight )
{
colText = wxSystemSettings::GetSystemColour( wxSYS_COLOUR_HIGHLIGHTTEXT );
}
else
{
colText = listctrl->GetForegroundColour();
}
// default font
wxFont font = listctrl->GetFont();
// VZ: currently we set the colours/fonts only once, but like this (i.e.
// using SetAttributes() inside the loop), it will be trivial to
// customize the subitems (in report mode) too.
wxListItemData *item = (wxListItemData*)m_items.First()->Data();
wxListItemAttr *attr = item->GetAttributes();
SetAttributes(dc, attr, colText, font);
bool hasBgCol = attr && attr->HasBackgroundColour();
if ( paintBG || hasBgCol )
{ {
if (hilight) if (hilight)
{ {
dc->SetBrush( * m_hilightBrush ); dc->SetBrush( * m_hilightBrush );
dc->SetPen( * wxTRANSPARENT_PEN );
} }
else else
{ {
dc->SetBrush( * wxWHITE_BRUSH ); if ( hasBgCol )
dc->SetPen( * wxTRANSPARENT_PEN ); dc->SetBrush(wxBrush(attr->GetBackgroundColour(), wxSOLID));
else
dc->SetBrush( * wxWHITE_BRUSH );
} }
dc->SetPen( * wxTRANSPARENT_PEN );
dc->DrawRectangle( m_bound_hilight.x, m_bound_hilight.y, dc->DrawRectangle( m_bound_hilight.x, m_bound_hilight.y,
m_bound_hilight.width, m_bound_hilight.height ); m_bound_hilight.width, m_bound_hilight.height );
} }
dc->SetBackgroundMode(wxTRANSPARENT);
if (m_mode == wxLC_REPORT) if (m_mode == wxLC_REPORT)
{ {
wxString s;
wxColour *colour = (wxColour*) NULL;
wxNode *node = m_items.First(); wxNode *node = m_items.First();
while (node) while (node)
{ {
@@ -623,16 +677,9 @@ void wxListLineData::DoDraw( wxDC *dc, bool hilight, bool paintBG )
m_owner->GetImageSize( item->GetImage(), x, y ); m_owner->GetImageSize( item->GetImage(), x, y );
x += item->GetX() + 5; x += item->GetX() + 5;
} }
if (!colour)
colour = item->GetColour();
if (item->HasText()) if (item->HasText())
{ {
item->GetText( s ); dc->DrawText( item->GetText(), x, item->GetY() );
if (hilight)
dc->SetTextForeground( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_HIGHLIGHTTEXT ) );
else
dc->SetTextForeground( *colour );
dc->DrawText( s, x, item->GetY() );
} }
dc->DestroyClippingRegion(); dc->DestroyClippingRegion();
node = node->Next(); node = node->Next();
@@ -650,13 +697,7 @@ void wxListLineData::DoDraw( wxDC *dc, bool hilight, bool paintBG )
} }
if (item->HasText()) if (item->HasText())
{ {
wxString s; dc->DrawText( item->GetText(), m_bound_label.x, m_bound_label.y );
item->GetText( s );
if (hilight)
dc->SetTextForeground( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_HIGHLIGHTTEXT ) );
else
dc->SetTextForeground( * item->GetColour() );
dc->DrawText( s, m_bound_label.x, m_bound_label.y );
} }
} }
} }
@@ -2881,15 +2922,15 @@ int wxListCtrl::GetSelectedItemCount() const
return m_mainWin->GetSelectedItemCount(); return m_mainWin->GetSelectedItemCount();
} }
/*
wxColour wxListCtrl::GetTextColour() const wxColour wxListCtrl::GetTextColour() const
{ {
return GetForegroundColour();
} }
void wxListCtrl::SetTextColour(const wxColour& WXUNUSED(col)) void wxListCtrl::SetTextColour(const wxColour& col)
{ {
SetForegroundColour(col);
} }
*/
long wxListCtrl::GetTopItem() const long wxListCtrl::GetTopItem() const
{ {