+// -----------------------------------------------------------------------
+// wxPGHeaderCtrl
+// -----------------------------------------------------------------------
+
+#if wxUSE_HEADERCTRL
+
+class wxPGHeaderCtrl : public wxHeaderCtrl
+{
+public:
+ wxPGHeaderCtrl(wxPropertyGridManager* manager) :
+ wxHeaderCtrl()
+ {
+ m_manager = manager;
+ EnsureColumnCount(2);
+
+ // Seed titles with defaults
+ m_columns[0]->SetTitle(_("Property"));
+ m_columns[1]->SetTitle(_("Value"));
+ }
+
+ virtual ~wxPGHeaderCtrl()
+ {
+ for (unsigned int i=0; i<m_columns.size(); i++ )
+ delete m_columns[i];
+ }
+
+ int DetermineColumnWidth(unsigned int idx, int* pMinWidth) const
+ {
+ const wxPropertyGridPage* page = m_page;
+ int colWidth = page->GetColumnWidth(idx);
+ int colMinWidth = page->GetColumnMinWidth(idx);
+ if ( idx == 0 )
+ {
+ wxPropertyGrid* pg = m_manager->GetGrid();
+ int margin = pg->GetMarginWidth();
+
+ // Compensate for the internal border
+ margin += (pg->GetSize().x - pg->GetClientSize().x) / 2;
+
+ colWidth += margin;
+ colMinWidth += margin;
+ }
+ *pMinWidth = colMinWidth;
+ return colWidth;
+ }
+
+ void OnPageChanged(const wxPropertyGridPage* page)
+ {
+ m_page = page;
+ OnPageUpdated();
+ }
+
+ void OnPageUpdated()
+ {
+ // Get column info from the page
+ const wxPropertyGridPage* page = m_page;
+ unsigned int colCount = page->GetColumnCount();
+ EnsureColumnCount(colCount);
+
+ for ( unsigned int i=0; i<colCount; i++ )
+ {
+ wxHeaderColumnSimple* colInfo = m_columns[i];
+ int colMinWidth = 0;
+ int colWidth = DetermineColumnWidth(i, &colMinWidth);
+ colInfo->SetWidth(colWidth);
+ colInfo->SetMinWidth(colMinWidth);
+ }
+
+ SetColumnCount(colCount);
+ }
+
+ void OnColumWidthsChanged()
+ {
+ const wxPropertyGridPage* page = m_page;
+ unsigned int colCount = page->GetColumnCount();
+
+ for ( unsigned int i=0; i<colCount; i++ )
+ {
+ wxHeaderColumnSimple* colInfo = m_columns[i];
+ int colMinWidth = 0;
+ int colWidth = DetermineColumnWidth(i, &colMinWidth);
+ colInfo->SetWidth(colWidth);
+ colInfo->SetMinWidth(colMinWidth);
+ UpdateColumn(i);
+ }
+ }
+
+ virtual const wxHeaderColumn& GetColumn(unsigned int idx) const
+ {
+ return *m_columns[idx];
+ }
+
+ void SetColumnTitle(unsigned int idx, const wxString& title)
+ {
+ EnsureColumnCount(idx+1);
+ m_columns[idx]->SetTitle(title);
+ }
+
+private:
+ void EnsureColumnCount(unsigned int count)
+ {
+ while ( m_columns.size() < count )
+ {
+ wxHeaderColumnSimple* colInfo = new wxHeaderColumnSimple("");
+ m_columns.push_back(colInfo);
+ }
+ }
+
+ void OnSetColumnWidth(int col, int colWidth)
+ {
+ wxPropertyGrid* pg = m_manager->GetGrid();
+
+ // Compensate for the internal border
+ int x = -((pg->GetSize().x - pg->GetClientSize().x) / 2);
+
+ for ( int i=0; i<col; i++ )
+ x += m_columns[i]->GetWidth();
+
+ x += colWidth;
+
+ pg->DoSetSplitterPosition(x, col,
+ wxPG_SPLITTER_REFRESH |
+ wxPG_SPLITTER_FROM_EVENT);
+ }
+
+ virtual bool ProcessEvent( wxEvent& event )
+ {
+ if ( event.IsKindOf(wxCLASSINFO(wxHeaderCtrlEvent)) )
+ {
+ wxHeaderCtrlEvent& hcEvent =
+ static_cast<wxHeaderCtrlEvent&>(event);
+
+ wxPropertyGrid* pg = m_manager->GetGrid();
+ int col = hcEvent.GetColumn();
+ int evtType = event.GetEventType();
+
+ if ( evtType == wxEVT_HEADER_RESIZING )
+ {
+ int colWidth = hcEvent.GetWidth();
+
+ OnSetColumnWidth(col, colWidth);
+
+ pg->SendEvent(wxEVT_PG_COL_DRAGGING,
+ NULL, NULL, 0,
+ (unsigned int)col);
+
+ return true;
+ }
+ else if ( evtType == wxEVT_HEADER_BEGIN_RESIZE )
+ {
+ // Never allow column resize if layout is static
+ if ( m_manager->HasFlag(wxPG_STATIC_SPLITTER) )
+ hcEvent.Veto();
+ // Allow application to veto dragging
+ else if ( pg->SendEvent(wxEVT_PG_COL_BEGIN_DRAG,
+ NULL, NULL, 0,
+ (unsigned int)col) )
+ hcEvent.Veto();
+
+ return true;
+ }
+ else if ( evtType == wxEVT_HEADER_END_RESIZE )
+ {
+ pg->SendEvent(wxEVT_PG_COL_END_DRAG,
+ NULL, NULL, 0,
+ (unsigned int)col);
+
+ return true;
+ }
+ }
+
+ return wxHeaderCtrl::ProcessEvent(event);
+ }
+
+ wxPropertyGridManager* m_manager;
+ const wxPropertyGridPage* m_page;
+ wxVector<wxHeaderColumnSimple*> m_columns;
+};
+
+#endif // wxUSE_HEADERCTRL
+