From 2b9aac3387bba0d7f54aed5976521018684c2fd8 Mon Sep 17 00:00:00 2001 From: Benjamin Williams Date: Wed, 8 Nov 2006 15:19:38 +0000 Subject: [PATCH] wxAuiNotebook bitmaps working now git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@43198 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/aui/auibook.h | 197 +++++++++++++++++++++------------------ samples/aui/auidemo.cpp | 8 +- src/aui/auibook.cpp | 161 +++++++++++++++++++++++++------- 3 files changed, 238 insertions(+), 128 deletions(-) diff --git a/include/wx/aui/auibook.h b/include/wx/aui/auibook.h index 06afedaafb..0d1dd76910 100644 --- a/include/wx/aui/auibook.h +++ b/include/wx/aui/auibook.h @@ -57,6 +57,79 @@ enum wxAuiNotebookOption +// aui notebook event class + +class WXDLLIMPEXP_AUI wxAuiNotebookEvent : public wxNotifyEvent +{ +public: + wxAuiNotebookEvent(wxEventType command_type = wxEVT_NULL, + int win_id = 0) + : wxNotifyEvent(command_type, win_id) + { + old_selection = -1; + selection = -1; + drag_source = NULL; + } +#ifndef SWIG + wxAuiNotebookEvent(const wxAuiNotebookEvent& c) : wxNotifyEvent(c) + { + old_selection = c.old_selection; + selection = c.selection; + drag_source = c.drag_source; + } +#endif + wxEvent *Clone() const { return new wxAuiNotebookEvent(*this); } + + void SetSelection(int s) { selection = s; m_commandInt = s; } + int GetSelection() const { return selection; } + + void SetOldSelection(int s) { old_selection = s; } + int GetOldSelection() const { return old_selection; } + + void SetDragSource(wxAuiNotebook* s) { drag_source = s; } + wxAuiNotebook* GetDragSource() const { return drag_source; } + +public: + int old_selection; + int selection; + wxAuiNotebook* drag_source; + +#ifndef SWIG +private: + DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxAuiNotebookEvent) +#endif +}; + + +class WXDLLIMPEXP_AUI wxAuiNotebookPage +{ +public: + wxWindow* window; // page's associated window + wxString caption; // caption displayed on the tab + wxBitmap bitmap; // tab's bitmap + wxRect rect; // tab's hit rectangle + bool active; // true if the page is currently active +}; + +class WXDLLIMPEXP_AUI wxAuiTabContainerButton +{ +public: + + int id; // button's id + int cur_state; // current state (normal, hover, pressed, etc.) + int location; // buttons location (wxLEFT, wxRIGHT, or wxCENTER) + wxBitmap bitmap; // button's hover bitmap + wxBitmap dis_bitmap; // button's disabled bitmap + wxRect rect; // button's hit rectangle +}; + + +#ifndef SWIG +WX_DECLARE_USER_EXPORTED_OBJARRAY(wxAuiNotebookPage, wxAuiNotebookPageArray, WXDLLIMPEXP_AUI); +WX_DECLARE_USER_EXPORTED_OBJARRAY(wxAuiTabContainerButton, wxAuiTabContainerButtonArray, WXDLLIMPEXP_AUI); +#endif + + // tab art class class WXDLLIMPEXP_AUI wxAuiTabArt @@ -85,6 +158,7 @@ public: wxWindow* wnd, const wxRect& in_rect, const wxString& caption, + const wxBitmap& bitmap, bool active, int close_button_state, wxRect* out_tab_rect, @@ -107,6 +181,7 @@ public: wxDC& dc, wxWindow* wnd, const wxString& caption, + const wxBitmap& bitmap, bool active, int close_button_state, int* x_extent) = 0; @@ -116,21 +191,21 @@ public: const wxArrayString& items, int active_idx) = 0; - virtual int GetBestTabCtrlSize(wxWindow* wnd) = 0; + virtual int GetBestTabCtrlSize(wxWindow* wnd, + wxAuiNotebookPageArray& pages) = 0; }; -class WXDLLIMPEXP_AUI wxAuiSimpleTabArt : public wxAuiTabArt +class WXDLLIMPEXP_AUI wxAuiDefaultTabArt : public wxAuiTabArt { public: - wxAuiSimpleTabArt(); - virtual ~wxAuiSimpleTabArt(); + wxAuiDefaultTabArt(); + virtual ~wxAuiDefaultTabArt(); wxAuiTabArt* Clone(); void SetFlags(unsigned int flags); - void SetSizingInfo(const wxSize& tab_ctrl_size, size_t tab_count); @@ -142,11 +217,12 @@ public: wxDC& dc, wxWindow* wnd, const wxRect& rect); - + void DrawTab(wxDC& dc, wxWindow* wnd, const wxRect& in_rect, const wxString& caption, + const wxBitmap& bitmap, bool active, int close_button_state, wxRect* out_tab_rect, @@ -162,13 +238,14 @@ public: int orientation, const wxBitmap& bitmap_override, wxRect* out_rect); - - int GetIndentSize(); + int GetIndentSize(); + wxSize GetTabSize( wxDC& dc, wxWindow* wnd, const wxString& caption, + const wxBitmap& bitmap, bool active, int close_button_state, int* x_extent); @@ -178,7 +255,8 @@ public: const wxArrayString& items, int active_idx); - int GetBestTabCtrlSize(wxWindow* wnd); + int GetBestTabCtrlSize(wxWindow* wnd, + wxAuiNotebookPageArray& pages); protected: @@ -200,20 +278,22 @@ protected: wxBitmap m_disabled_windowlist_bmp; int m_fixed_tab_width; + int m_tab_ctrl_height; unsigned int m_flags; }; -class WXDLLIMPEXP_AUI wxAuiDefaultTabArt : public wxAuiTabArt +class WXDLLIMPEXP_AUI wxAuiSimpleTabArt : public wxAuiTabArt { public: - wxAuiDefaultTabArt(); - virtual ~wxAuiDefaultTabArt(); + wxAuiSimpleTabArt(); + virtual ~wxAuiSimpleTabArt(); wxAuiTabArt* Clone(); void SetFlags(unsigned int flags); + void SetSizingInfo(const wxSize& tab_ctrl_size, size_t tab_count); @@ -225,11 +305,12 @@ public: wxDC& dc, wxWindow* wnd, const wxRect& rect); - + void DrawTab(wxDC& dc, wxWindow* wnd, const wxRect& in_rect, const wxString& caption, + const wxBitmap& bitmap, bool active, int close_button_state, wxRect* out_tab_rect, @@ -245,13 +326,14 @@ public: int orientation, const wxBitmap& bitmap_override, wxRect* out_rect); - + int GetIndentSize(); - + wxSize GetTabSize( wxDC& dc, wxWindow* wnd, const wxString& caption, + const wxBitmap& bitmap, bool active, int close_button_state, int* x_extent); @@ -261,7 +343,8 @@ public: const wxArrayString& items, int active_idx); - int GetBestTabCtrlSize(wxWindow* wnd); + int GetBestTabCtrlSize(wxWindow* wnd, + wxAuiNotebookPageArray& pages); protected: @@ -289,80 +372,9 @@ protected: -// event declarations/classes - -class WXDLLIMPEXP_AUI wxAuiNotebookEvent : public wxNotifyEvent -{ -public: - wxAuiNotebookEvent(wxEventType command_type = wxEVT_NULL, - int win_id = 0) - : wxNotifyEvent(command_type, win_id) - { - old_selection = -1; - selection = -1; - drag_source = NULL; - } -#ifndef SWIG - wxAuiNotebookEvent(const wxAuiNotebookEvent& c) : wxNotifyEvent(c) - { - old_selection = c.old_selection; - selection = c.selection; - drag_source = c.drag_source; - } -#endif - wxEvent *Clone() const { return new wxAuiNotebookEvent(*this); } - - void SetSelection(int s) { selection = s; m_commandInt = s; } - int GetSelection() const { return selection; } - - void SetOldSelection(int s) { old_selection = s; } - int GetOldSelection() const { return old_selection; } - - void SetDragSource(wxAuiNotebook* s) { drag_source = s; } - wxAuiNotebook* GetDragSource() const { return drag_source; } - -public: - int old_selection; - int selection; - wxAuiNotebook* drag_source; - -#ifndef SWIG -private: - DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxAuiNotebookEvent) -#endif -}; - - - - - -class WXDLLIMPEXP_AUI wxAuiNotebookPage -{ -public: - wxWindow* window; // page's associated window - wxString caption; // caption displayed on the tab - wxBitmap bitmap; // tab's bitmap - wxRect rect; // tab's hit rectangle - bool active; // true if the page is currently active -}; - -class WXDLLIMPEXP_AUI wxAuiTabContainerButton -{ -public: - int id; // button's id - int cur_state; // current state (normal, hover, pressed, etc.) - int location; // buttons location (wxLEFT, wxRIGHT, or wxCENTER) - wxBitmap bitmap; // button's hover bitmap - wxBitmap dis_bitmap; // button's disabled bitmap - wxRect rect; // button's hit rectangle -}; -#ifndef SWIG -WX_DECLARE_USER_EXPORTED_OBJARRAY(wxAuiNotebookPage, wxAuiNotebookPageArray, WXDLLIMPEXP_AUI); -WX_DECLARE_USER_EXPORTED_OBJARRAY(wxAuiTabContainerButton, wxAuiTabContainerButtonArray, WXDLLIMPEXP_AUI); -#endif class WXDLLIMPEXP_AUI wxAuiTabContainer @@ -510,12 +522,19 @@ public: size_t GetPageCount() const; wxWindow* GetPage(size_t page_idx) const; - void SetArtProvider(wxAuiTabArt* art); wxAuiTabArt* GetArtProvider(); protected: + // these can be overridden + virtual void SetTabCtrlHeight(int height); + virtual int CalculateTabCtrlHeight(); + +protected: + + void DoSizing(); + void InitNotebook(long style); wxAuiTabCtrl* GetTabCtrlFromPoint(const wxPoint& pt); wxWindow* GetTabFrameFromTabCtrl(wxWindow* tab_ctrl); wxAuiTabCtrl* GetActiveTabCtrl(); @@ -524,8 +543,6 @@ protected: protected: - void DoSizing(); - void InitNotebook(long style); void OnChildFocus(wxChildFocusEvent& evt); void OnRender(wxAuiManagerEvent& evt); diff --git a/samples/aui/auidemo.cpp b/samples/aui/auidemo.cpp index c9036eea9d..cb9cdd2509 100644 --- a/samples/aui/auidemo.cpp +++ b/samples/aui/auidemo.cpp @@ -1448,8 +1448,10 @@ wxAuiNotebook* MyFrame::CreateNotebook() wxPoint(client_size.x, client_size.y), wxSize(430,200), m_notebook_style); + + wxBitmap page_bmp = wxArtProvider::GetBitmap(wxART_NORMAL_FILE, wxART_OTHER, wxSize(16,16)); - ctrl->AddPage(CreateHTMLCtrl(ctrl), wxT("Welcome to wxAUI")); + ctrl->AddPage(CreateHTMLCtrl(ctrl), wxT("Welcome to wxAUI") , false, page_bmp); wxPanel *panel = new wxPanel( ctrl, wxID_ANY ); wxFlexGridSizer *flex = new wxFlexGridSizer( 2 ); @@ -1465,10 +1467,10 @@ wxAuiNotebook* MyFrame::CreateNotebook() wxSP_ARROW_KEYS, 5, 50, 5 ), 0, wxALL|wxALIGN_CENTRE, 5 ); flex->Add( 5,5 ); flex->Add( 5,5 ); panel->SetSizer( flex ); - ctrl->AddPage( panel, wxT("wxPanel") ); + ctrl->AddPage( panel, wxT("wxPanel"), false, page_bmp ); ctrl->AddPage( new wxTextCtrl( ctrl, wxID_ANY, wxT("Some text"), - wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE|wxNO_BORDER) , wxT("wxTextCtrl 1") ); + wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE|wxNO_BORDER) , wxT("wxTextCtrl 1"), false, page_bmp ); ctrl->AddPage( new wxTextCtrl( ctrl, wxID_ANY, wxT("Some more text"), wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE|wxNO_BORDER) , wxT("wxTextCtrl 2") ); diff --git a/src/aui/auibook.cpp b/src/aui/auibook.cpp index c9dd327873..bc62aaef78 100644 --- a/src/aui/auibook.cpp +++ b/src/aui/auibook.cpp @@ -221,6 +221,7 @@ wxAuiDefaultTabArt::wxAuiDefaultTabArt() m_measuring_font = m_selected_font; m_fixed_tab_width = 100; + m_tab_ctrl_height = 0; wxColour base_colour = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE); @@ -284,6 +285,8 @@ void wxAuiDefaultTabArt::SetSizingInfo(const wxSize& tab_ctrl_size, if (m_fixed_tab_width > 220) m_fixed_tab_width = 220; + + m_tab_ctrl_height = tab_ctrl_size.y; } @@ -316,6 +319,7 @@ void wxAuiDefaultTabArt::DrawTab(wxDC& dc, wxWindow* wnd, const wxRect& in_rect, const wxString& caption_text, + const wxBitmap& bitmap, bool active, int close_button_state, wxRect* out_tab_rect, @@ -338,9 +342,15 @@ void wxAuiDefaultTabArt::DrawTab(wxDC& dc, dc.GetTextExtent(caption, &normal_textx, &normal_texty); // figure out the size of the tab - wxSize tab_size = GetTabSize(dc, wnd, caption, active, close_button_state, x_extent); - - wxCoord tab_height = tab_size.y; + wxSize tab_size = GetTabSize(dc, + wnd, + caption, + bitmap, + active, + close_button_state, + x_extent); + + wxCoord tab_height = m_tab_ctrl_height - 3; wxCoord tab_width = tab_size.x; wxCoord tab_x = in_rect.x; wxCoord tab_y = in_rect.y + in_rect.height - tab_height; @@ -388,6 +398,9 @@ void wxAuiDefaultTabArt::DrawTab(wxDC& dc, points[5].x = tab_x + tab_width; points[5].y = tab_y + tab_height - 4; + int drawn_tab_yoff = points[1].y; + int drawn_tab_height = points[0].y - points[1].y; + // draw gradient background if (active) @@ -423,17 +436,33 @@ void wxAuiDefaultTabArt::DrawTab(wxDC& dc, dc.DrawLine(points[0].x, points[0].y, points[5].x+1, points[5].y); } - int text_offset; + int text_offset = tab_x + 8; int close_button_width = 0; - if (close_button_state != wxAUI_BUTTON_STATE_HIDDEN) { close_button_width = m_active_close_bmp.GetWidth(); } - text_offset = tab_x + 8; + if (bitmap.IsOk()) + { + int bitmap_offset = tab_x + 8; + + // draw bitmap + dc.DrawBitmap(bitmap, + bitmap_offset, + drawn_tab_yoff + (drawn_tab_height/2) - (bitmap.GetHeight()/2) + 1, + true); + + text_offset = bitmap_offset + bitmap.GetWidth(); + text_offset += 3; // bitmap padding + } + else + { + text_offset = tab_x + 8; + } + wxString draw_text = ChopText(dc, caption, @@ -442,7 +471,7 @@ void wxAuiDefaultTabArt::DrawTab(wxDC& dc, // draw tab text dc.DrawText(draw_text, text_offset, - (tab_y + tab_height)/2 - (texty/2)); + drawn_tab_yoff + (drawn_tab_height)/2 - (texty/2) - 1); @@ -481,6 +510,7 @@ int wxAuiDefaultTabArt::GetIndentSize() wxSize wxAuiDefaultTabArt::GetTabSize(wxDC& dc, wxWindow* WXUNUSED(wnd), const wxString& caption, + const wxBitmap& bitmap, bool WXUNUSED(active), int close_button_state, int* x_extent) @@ -493,12 +523,24 @@ wxSize wxAuiDefaultTabArt::GetTabSize(wxDC& dc, dc.GetTextExtent(wxT("ABCDEFXj"), &tmp, &measured_texty); // add padding around the text - wxCoord tab_width = measured_textx + 16; - wxCoord tab_height = measured_texty + 10; + wxCoord tab_width = measured_textx; + wxCoord tab_height = measured_texty; + // if the close button is showing, add space for it if (close_button_state != wxAUI_BUTTON_STATE_HIDDEN) tab_width += m_active_close_bmp.GetWidth() + 3; + // if there's a bitmap, add space for it + if (bitmap.IsOk()) + { + tab_width += bitmap.GetWidth(); + tab_width += 3; // right side bitmap padding + tab_height = wxMax(tab_height, bitmap.GetHeight()); + } + + // add padding + tab_width += 16; + tab_height += 10; if (m_flags & wxAUI_NB_TAB_FIXED_WIDTH) { @@ -627,18 +669,34 @@ int wxAuiDefaultTabArt::ShowWindowList(wxWindow* wnd, return -1; } -int wxAuiDefaultTabArt::GetBestTabCtrlSize(wxWindow* wnd) +int wxAuiDefaultTabArt::GetBestTabCtrlSize(wxWindow* wnd, + wxAuiNotebookPageArray& pages) { wxClientDC dc(wnd); dc.SetFont(m_measuring_font); - int x_ext = 0; - wxSize s = GetTabSize(dc, - wnd, - wxT("ABCDEFGHIj"), - true, - wxAUI_BUTTON_STATE_HIDDEN, - &x_ext); - return s.y+3; + + int max_y = 0; + size_t i, page_count = pages.GetCount(); + for (i = 0; i < page_count; ++i) + { + wxAuiNotebookPage& page = pages.Item(i); + + // we don't use the caption text because we don't + // want tab heights to be different in the case + // of a very short piece of text on one tab and a very + // tall piece of text on another tab + int x_ext = 0; + wxSize s = GetTabSize(dc, + wnd, + wxT("ABCDEFGHIj"), + page.bitmap, + true, + wxAUI_BUTTON_STATE_HIDDEN, + &x_ext); + max_y = wxMax(max_y, s.y); + } + + return max_y+2; } void wxAuiDefaultTabArt::SetNormalFont(const wxFont& font) @@ -760,6 +818,7 @@ void wxAuiSimpleTabArt::DrawTab(wxDC& dc, wxWindow* wnd, const wxRect& in_rect, const wxString& caption_text, + const wxBitmap& bitmap, bool active, int close_button_state, wxRect* out_tab_rect, @@ -782,7 +841,13 @@ void wxAuiSimpleTabArt::DrawTab(wxDC& dc, dc.GetTextExtent(caption, &normal_textx, &normal_texty); // figure out the size of the tab - wxSize tab_size = GetTabSize(dc, wnd, caption, active, close_button_state, x_extent); + wxSize tab_size = GetTabSize(dc, + wnd, + caption, + bitmap, + active, + close_button_state, + x_extent); wxCoord tab_height = tab_size.y; wxCoord tab_width = tab_size.x; @@ -898,6 +963,7 @@ int wxAuiSimpleTabArt::GetIndentSize() wxSize wxAuiSimpleTabArt::GetTabSize(wxDC& dc, wxWindow* WXUNUSED(wnd), const wxString& caption, + const wxBitmap& WXUNUSED(bitmap), bool WXUNUSED(active), int close_button_state, int* x_extent) @@ -1040,7 +1106,8 @@ int wxAuiSimpleTabArt::ShowWindowList(wxWindow* wnd, return -1; } -int wxAuiSimpleTabArt::GetBestTabCtrlSize(wxWindow* wnd) +int wxAuiSimpleTabArt::GetBestTabCtrlSize(wxWindow* wnd, + wxAuiNotebookPageArray& WXUNUSED(pages)) { wxClientDC dc(wnd); dc.SetFont(m_measuring_font); @@ -1048,6 +1115,7 @@ int wxAuiSimpleTabArt::GetBestTabCtrlSize(wxWindow* wnd) wxSize s = GetTabSize(dc, wnd, wxT("ABCDEFGHIj"), + wxNullBitmap, true, wxAUI_BUTTON_STATE_HIDDEN, &x_ext); @@ -1440,6 +1508,7 @@ void wxAuiTabContainer::Render(wxDC* raw_dc, wxWindow* wnd) wxSize size = m_art->GetTabSize(dc, wnd, page.caption, + page.bitmap, page.active, close_button ? wxAUI_BUTTON_STATE_NORMAL : @@ -1645,6 +1714,7 @@ void wxAuiTabContainer::Render(wxDC* raw_dc, wxWindow* wnd) wnd, rect, page.caption, + page.bitmap, page.active, tab_button.cur_state, &page.rect, @@ -1681,6 +1751,7 @@ void wxAuiTabContainer::Render(wxDC* raw_dc, wxWindow* wnd) wnd, active_rect, page.caption, + page.bitmap, page.active, tab_button.cur_state, &page.rect, @@ -2263,25 +2334,44 @@ void wxAuiNotebook::SetArtProvider(wxAuiTabArt* art) { m_tabs.SetArtProvider(art); - // choose a default for the tab height - m_tab_ctrl_height = art->GetBestTabCtrlSize(this); - - wxAuiPaneInfoArray& all_panes = m_mgr.GetAllPanes(); - size_t i, pane_count = all_panes.GetCount(); - for (i = 0; i < pane_count; ++i) + SetTabCtrlHeight(CalculateTabCtrlHeight()); +} + +void wxAuiNotebook::SetTabCtrlHeight(int height) +{ + // if the tab control height needs to change, update + // all of our tab controls with the new height + if (m_tab_ctrl_height != height) { - wxAuiPaneInfo& pane = all_panes.Item(i); - if (pane.name == wxT("dummy")) - continue; - wxTabFrame* tab_frame = (wxTabFrame*)pane.window; - wxAuiTabCtrl* tabctrl = tab_frame->m_tabs; - tab_frame->SetTabCtrlHeight(m_tab_ctrl_height); - tabctrl->SetArtProvider(art->Clone()); - tab_frame->DoSizing(); + wxAuiTabArt* art = m_tabs.GetArtProvider(); + + m_tab_ctrl_height = height; + + wxAuiPaneInfoArray& all_panes = m_mgr.GetAllPanes(); + size_t i, pane_count = all_panes.GetCount(); + for (i = 0; i < pane_count; ++i) + { + wxAuiPaneInfo& pane = all_panes.Item(i); + if (pane.name == wxT("dummy")) + continue; + wxTabFrame* tab_frame = (wxTabFrame*)pane.window; + wxAuiTabCtrl* tabctrl = tab_frame->m_tabs; + tab_frame->SetTabCtrlHeight(m_tab_ctrl_height); + tabctrl->SetArtProvider(art->Clone()); + tab_frame->DoSizing(); + } } - } +int wxAuiNotebook::CalculateTabCtrlHeight() +{ + // find out new best tab height + wxAuiTabArt* art = m_tabs.GetArtProvider(); + + return art->GetBestTabCtrlSize(this, m_tabs.GetPages()); +} + + wxAuiTabArt* wxAuiNotebook::GetArtProvider() { return m_tabs.GetArtProvider(); @@ -2347,6 +2437,7 @@ bool wxAuiNotebook::InsertPage(size_t page_idx, else active_tabctrl->InsertPage(page, info, page_idx); + SetTabCtrlHeight(CalculateTabCtrlHeight()); DoSizing(); active_tabctrl->DoShowHide(); -- 2.45.2