From bed7d9fe741d2dead09be41c790f4e129f96bd97 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Fri, 22 Apr 2016 00:58:38 +0200 Subject: [PATCH] Add wxDataViewCtrl::SetHeaderAttr() too This is currently only implemented in the generic version but could be implemented at least for GTK+ native one as well in the future. --- docs/changes.txt | 1 + include/wx/dataview.h | 7 ++++++- include/wx/generic/dataview.h | 2 ++ interface/wx/dataview.h | 19 +++++++++++++++++++ samples/dataview/dataview.cpp | 20 ++++++++++++++++++++ src/generic/datavgen.cpp | 19 +++++++++++++++++++ 6 files changed, 67 insertions(+), 1 deletion(-) diff --git a/docs/changes.txt b/docs/changes.txt index 874ae50525..674d97d92e 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -71,6 +71,7 @@ All (GUI): - Add support for wxSL_MIN_MAX_LABELS and wxSL_VALUE_LABEL to XRC (ousnius). - Update Scintilla to v3.6.3 (Paul Kulchenko). - Make wxDataViewCtrl::Expand() expand ancestors in native ports too. +- Add wxDataViewCtrl::SetHeaderAttr(). - Add wxListCtrl::SetHeaderAttr(). wxGTK: diff --git a/include/wx/dataview.h b/include/wx/dataview.h index 3b8bc71e25..8349a29082 100644 --- a/include/wx/dataview.h +++ b/include/wx/dataview.h @@ -29,6 +29,7 @@ #include "wx/systhemectrl.h" class WXDLLIMPEXP_FWD_CORE wxImageList; +class WXDLLIMPEXP_FWD_CORE wxItemAttr; #if !(defined(__WXGTK20__) || defined(__WXOSX__) ) || defined(__WXUNIVERSAL__) // #if !(defined(__WXOSX__)) || defined(__WXUNIVERSAL__) @@ -128,7 +129,7 @@ private: // wxDataViewItemAttr: a structure containing the visual attributes of an item // ---------------------------------------------------------------------------- -// TODO: this should be renamed to wxItemAttr or something general like this +// TODO: Merge with wxItemAttr somehow. class WXDLLIMPEXP_ADV wxDataViewItemAttr { @@ -732,6 +733,10 @@ public: // define control visual attributes // -------------------------------- + // Header attributes: only implemented in the generic version currently. + virtual bool SetHeaderAttr(const wxItemAttr& WXUNUSED(attr)) + { return false; } + virtual wxVisualAttributes GetDefaultAttributes() const wxOVERRIDE { return GetClassDefaultAttributes(GetWindowVariant()); diff --git a/include/wx/generic/dataview.h b/include/wx/generic/dataview.h index 224f90f166..1f742e63c7 100644 --- a/include/wx/generic/dataview.h +++ b/include/wx/generic/dataview.h @@ -256,6 +256,8 @@ public: virtual void EditItem(const wxDataViewItem& item, const wxDataViewColumn *column) wxOVERRIDE; + virtual bool SetHeaderAttr(const wxItemAttr& attr) wxOVERRIDE; + // These methods are specific to generic wxDataViewCtrl implementation and // should not be used in portable code. wxColour GetAlternateRowColour() const { return m_alternateRowColour; } diff --git a/interface/wx/dataview.h b/interface/wx/dataview.h index a4a9362fbe..2c29c76049 100644 --- a/interface/wx/dataview.h +++ b/interface/wx/dataview.h @@ -1547,6 +1547,25 @@ public: */ void SetCurrentItem(const wxDataViewItem& item); + /** + Set custom colours and/or font to use for the header. + + This method allows to customize the display of the control header (it + does nothing if @c wxDV_NO_HEADER style is used). + + Currently it is only implemented in the generic version and just + returns @false without doing anything elsewhere. + + @param attr The attribute defining the colour(s) and font to use. It + can be default, in which case the attributes are reset to their + default values. + @return @true if the attributes were updated, @false if the method is + not implemented or failed. + + @since 3.1.1 + */ + bool SetHeaderAttr(const wxItemAttr& attr); + /** Sets the indentation. */ diff --git a/samples/dataview/dataview.cpp b/samples/dataview/dataview.cpp index a51a3146e6..89f0d66367 100644 --- a/samples/dataview/dataview.cpp +++ b/samples/dataview/dataview.cpp @@ -32,6 +32,7 @@ #include "wx/numdlg.h" #include "wx/spinctrl.h" #include "wx/imaglist.h" +#include "wx/itemattr.h" #include "wx/notebook.h" #include "mymodels.h" @@ -74,6 +75,7 @@ private: // event handlers void OnStyleChange(wxCommandEvent& event); void OnSetBackgroundColour(wxCommandEvent& event); + void OnCustomHeaderAttr(wxCommandEvent& event); void OnSetForegroundColour(wxCommandEvent& event); void OnIncIndent(wxCommandEvent& event); void OnDecIndent(wxCommandEvent& event); @@ -290,6 +292,7 @@ enum ID_CLEARLOG = wxID_HIGHEST+1, ID_BACKGROUND_COLOUR, ID_FOREGROUND_COLOUR, + ID_CUSTOM_HEADER_ATTR, ID_STYLE_MENU, ID_INC_INDENT, ID_DEC_INDENT, @@ -344,6 +347,7 @@ wxBEGIN_EVENT_TABLE(MyFrame, wxFrame) EVT_MENU( ID_FOREGROUND_COLOUR, MyFrame::OnSetForegroundColour ) EVT_MENU( ID_BACKGROUND_COLOUR, MyFrame::OnSetBackgroundColour ) + EVT_MENU( ID_CUSTOM_HEADER_ATTR, MyFrame::OnCustomHeaderAttr ) EVT_MENU( ID_INC_INDENT, MyFrame::OnIncIndent ) EVT_MENU( ID_DEC_INDENT, MyFrame::OnDecIndent ) @@ -431,6 +435,7 @@ MyFrame::MyFrame(wxFrame *frame, const wxString &title, int x, int y, int w, int file_menu->Append(ID_CLEARLOG, "&Clear log\tCtrl-L"); file_menu->Append(ID_FOREGROUND_COLOUR, "Set &foreground colour...\tCtrl-S"); file_menu->Append(ID_BACKGROUND_COLOUR, "Set &background colour...\tCtrl-B"); + file_menu->AppendCheckItem(ID_CUSTOM_HEADER_ATTR, "C&ustom header attributes"); file_menu->Append(ID_STYLE_MENU, "&Style", style_menu); file_menu->Append(ID_INC_INDENT, "&Increase indent\tCtrl-I"); file_menu->Append(ID_DEC_INDENT, "&Decrease indent\tShift-Ctrl-I"); @@ -788,6 +793,21 @@ void MyFrame::OnSetBackgroundColour(wxCommandEvent& WXUNUSED(event)) } } +void MyFrame::OnCustomHeaderAttr(wxCommandEvent& event) +{ + wxItemAttr attr; + if ( event.IsChecked() ) + { + attr.SetTextColour(*wxRED); + attr.SetFont(wxFontInfo(20).Bold()); + } + //else: leave it as default to disable custom header attributes + + wxDataViewCtrl * const dvc = m_ctrl[m_notebook->GetSelection()]; + if ( !dvc->SetHeaderAttr(attr) ) + wxLogMessage("Sorry, header attributes not supported on this platform"); +} + void MyFrame::OnIncIndent(wxCommandEvent& WXUNUSED(event)) { wxDataViewCtrl * const dvc = m_ctrl[m_notebook->GetSelection()]; diff --git a/src/generic/datavgen.cpp b/src/generic/datavgen.cpp index 41f4403240..90c921b5b5 100644 --- a/src/generic/datavgen.cpp +++ b/src/generic/datavgen.cpp @@ -41,6 +41,7 @@ #include "wx/renderer.h" #include "wx/dcbuffer.h" #include "wx/icon.h" +#include "wx/itemattr.h" #include "wx/list.h" #include "wx/listimpl.cpp" #include "wx/imaglist.h" @@ -5202,6 +5203,24 @@ bool wxDataViewCtrl::IsSelected( const wxDataViewItem & item ) const return false; } +bool wxDataViewCtrl::SetHeaderAttr(const wxItemAttr& attr) +{ + if ( !m_headerArea ) + return false; + + // Call all functions unconditionally to reset the previously set + // attributes, if any. + m_headerArea->SetForegroundColour(attr.GetTextColour()); + m_headerArea->SetBackgroundColour(attr.GetBackgroundColour()); + m_headerArea->SetFont(attr.GetFont()); + + // If the font has changed, the size of the header might need to be + // updated. + Layout(); + + return true; +} + void wxDataViewCtrl::SetAlternateRowColour(const wxColour& colour) { m_alternateRowColour = colour;