how to use the DrawBackground function of wxAuiToolBarArt Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
newage
Knows some wx things
Knows some wx things
Posts: 28
Joined: Sun Dec 08, 2013 6:34 am

how to use the DrawBackground function of wxAuiToolBarArt

Post by newage » Sat Dec 14, 2013 6:18 am

Can you write a sample to me ?
I want to change the background of wxauitool,but I don't how to do.

http://forums.wxwidgets.org/viewtopic.p ... ar#p106391
I find this web,but I don't how to do.

change the grey to light green or some else
(Sorry,my english is very poor.)
auitoolbar.PNG
auitoolbar.PNG (10.83 KiB) Viewed 2813 times

User avatar
T-Rex
Moderator
Moderator
Posts: 1195
Joined: Sat Oct 23, 2004 9:58 am
Location: Zaporizhzhya, Ukraine
Contact:

Re: how to use the DrawBackground function of wxAuiToolBarAr

Post by T-Rex » Sat Dec 14, 2013 8:47 am

Have you tried to look at existing toolbar art's code? There original source code is quite a good sample. Any problem with it?

wxAuiBlackToolBarArt.h

Code: Select all

#ifndef _WX_AUI_BLACK_TOOLBARART_H
#define _WX_AUI_BLACK_TOOLBARART_H

#include <wx/wx.h>

#if wxUSE_AUI

#include <wx/pen.h>
#include <wx/brush.h>
#include <wx/bitmap.h>
#include <wx/colour.h>

#include <wx/aui/framemanager.h>
#include <wx/aui/auibar.h>

class WXDLLIMPEXP_AUI wxAuiBlackToolBarArt : public wxAuiToolBarArt
{
protected:
	unsigned int m_flags;
    int m_text_orientation;
    int m_separator_size;
    int m_gripper_size;
    int m_overflow_size;

	// not use extended yet
	wxBitmap m_button_dropdown_bmp;
    wxBitmap m_disabled_button_dropdown_bmp;
    wxBitmap m_overflow_bmp;
    wxBitmap m_disabled_overflow_bmp;
    wxColour m_base_colour;
    wxColour m_highlight_colour;
    wxFont m_font;

public:
	wxAuiBlackToolBarArt();
	virtual ~wxAuiBlackToolBarArt();

	wxAuiToolBarArt* Clone();

    void SetFlags(unsigned int flags);
	unsigned int GetFlags();
    void SetFont(const wxFont& font);
	wxFont GetFont();
    void SetTextOrientation(int orientation);
	int GetTextOrientation();

	wxSize GetLabelSize(
                 wxDC& dc,
                 wxWindow* wnd,
                 const wxAuiToolBarItem& item);

    wxSize GetToolSize(
                 wxDC& dc,
                 wxWindow* wnd,
                 const wxAuiToolBarItem& item);

    int GetElementSize(int element_id);
    void SetElementSize(int element_id, int size);

    void DrawBackground(
				 wxDC& dc,
				 wxWindow* wnd,
				 const wxRect& rect);

    void DrawLabel(
                 wxDC& dc,
                 wxWindow* wnd,
                 const wxAuiToolBarItem& item,
                 const wxRect& rect);

    void DrawButton(
                 wxDC& dc,
                 wxWindow* wnd,
                 const wxAuiToolBarItem& item,
                 const wxRect& rect);

    void DrawDropDownButton(
                 wxDC& dc,
                 wxWindow* wnd,
                 const wxAuiToolBarItem& item,
                 const wxRect& rect);

    void DrawControlLabel(
                 wxDC& dc,
                 wxWindow* wnd,
                 const wxAuiToolBarItem& item,
                 const wxRect& rect);

    void DrawSeparator(
                 wxDC& dc,
                 wxWindow* wnd,
                 const wxRect& rect);

    void DrawGripper(
                 wxDC& dc,
                 wxWindow* wnd,
                 const wxRect& rect);

    void DrawOverflowButton(
                 wxDC& dc,
                 wxWindow* wnd,
                 const wxRect& rect,
                 int state);

	int ShowDropDown(
                 wxWindow* wnd,
                 const wxAuiToolBarItemArray& items);
};

#endif // wxUSE_AUI

#endif
wxAuiBlackToolBarArt.cpp

Code: Select all

#include "wx/wxprec.h"

#ifdef __BORLANDC__
#pragma hdrstop
#endif

#if wxUSE_AUI

#include "wxAuiBlackToolBarArt.h"

//-----------------------------------------------------------------------------
wxBitmap wxAuiBitmapFromBits(const unsigned char bits[], int w, int h, const wxColour& color);
unsigned char wxAuiBlendColour(unsigned char fg, unsigned char bg, double alpha);
wxColor wxAuiStepColour(const wxColor& c, int percent);

const int BUTTON_DROPDOWN_WIDTH = 10;
static const unsigned char DISABLED_TEXT_GREY_HUE = wxAuiBlendColour(0, 255, 0.4);
const wxColour DISABLED_TEXT_COLOR(DISABLED_TEXT_GREY_HUE,
                                   DISABLED_TEXT_GREY_HUE,
                                   DISABLED_TEXT_GREY_HUE);

//-----------------------------------------------------------------------------
class ToolbarCommandCapture : public wxEvtHandler
{
public:

    ToolbarCommandCapture() { m_last_id = 0; }
    int GetCommandId() const { return m_last_id; }

    bool ProcessEvent(wxEvent& evt)
    {
        if (evt.GetEventType() == wxEVT_COMMAND_MENU_SELECTED)
        {
            m_last_id = evt.GetId();
            return true;
        }

        if (GetNextHandler())
            return GetNextHandler()->ProcessEvent(evt);

        return false;
    }

private:
    int m_last_id;
};

//-----------------------------------------------------------------------------
static wxColor GetBaseColor()
{

#if defined( __WXMAC__ ) && wxOSX_USE_COCOA_OR_CARBON
    wxColor base_colour = wxColour( wxMacCreateCGColorFromHITheme(kThemeBrushToolbarBackground));
#else
    wxColor base_colour = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE);
#endif

    // the base_colour is too pale to use as our base colour,
    // so darken it a bit --
    if ((255-base_colour.Red()) +
        (255-base_colour.Green()) +
        (255-base_colour.Blue()) < 60)
    {
        base_colour = wxAuiStepColour(base_colour, 92);
    }

    return base_colour;
}

//-----------------------------------------------------------------------------
wxAuiBlackToolBarArt::wxAuiBlackToolBarArt()
{
	m_flags = 0;
    m_separator_size = 7;
    m_gripper_size = 8;
    m_overflow_size = 16;

	m_base_colour = GetBaseColor();
	m_highlight_colour = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT);

	m_font = *wxNORMAL_FONT;
    m_text_orientation = wxAUI_TBTOOL_TEXT_BOTTOM;

    static unsigned char button_dropdown_bits[] = { 0xe0, 0xf1, 0xfb };
    static unsigned char overflow_bits[] = { 0x80, 0xff, 0x80, 0xc1, 0xe3, 0xf7 };

    m_button_dropdown_bmp = wxAuiBitmapFromBits(button_dropdown_bits, 5, 3, *wxBLACK);
    m_disabled_button_dropdown_bmp = wxAuiBitmapFromBits(button_dropdown_bits, 5, 3, wxColor(128,128,128));
    m_overflow_bmp = wxAuiBitmapFromBits(overflow_bits, 7, 6, *wxBLACK);
    m_disabled_overflow_bmp = wxAuiBitmapFromBits(overflow_bits, 7, 6, wxColor(128,128,128));
}

//-----------------------------------------------------------------------------
wxAuiBlackToolBarArt::~wxAuiBlackToolBarArt()
{
    m_font = *wxNORMAL_FONT;
}

//-----------------------------------------------------------------------------
wxAuiToolBarArt* wxAuiBlackToolBarArt::Clone()
{
    return static_cast<wxAuiToolBarArt*>(new wxAuiBlackToolBarArt);
}

//-----------------------------------------------------------------------------
void wxAuiBlackToolBarArt::SetFlags(unsigned int flags)
{
    m_flags = flags;
}

//-----------------------------------------------------------------------------
unsigned int wxAuiBlackToolBarArt::GetFlags()
{
	return m_flags;
}

//-----------------------------------------------------------------------------
void wxAuiBlackToolBarArt::SetFont(const wxFont& font)
{
    m_font = font;
}

//-----------------------------------------------------------------------------
wxFont wxAuiBlackToolBarArt::GetFont()
{
	return m_font;
}

//-----------------------------------------------------------------------------
void wxAuiBlackToolBarArt::SetTextOrientation(int orientation)
{
    m_text_orientation = orientation;
}

//-----------------------------------------------------------------------------
int wxAuiBlackToolBarArt::GetTextOrientation()
{
	return m_text_orientation;
}

//-----------------------------------------------------------------------------
int wxAuiBlackToolBarArt::GetElementSize(int element_id)
{
    switch (element_id)
    {
        case wxAUI_TBART_SEPARATOR_SIZE: return m_separator_size;
        case wxAUI_TBART_GRIPPER_SIZE:   return m_gripper_size;
        case wxAUI_TBART_OVERFLOW_SIZE:  return m_overflow_size;
        default: return 0;
    }
}

//-----------------------------------------------------------------------------
void wxAuiBlackToolBarArt::SetElementSize(int element_id, int size)
{
    switch (element_id)
    {
        case wxAUI_TBART_SEPARATOR_SIZE: m_separator_size = size; break;
        case wxAUI_TBART_GRIPPER_SIZE:   m_gripper_size = size; break;
        case wxAUI_TBART_OVERFLOW_SIZE:  m_overflow_size = size; break;
    }
}

//-----------------------------------------------------------------------------
wxSize wxAuiBlackToolBarArt::GetToolSize(
                                        wxDC& dc,
                                        wxWindow* WXUNUSED(wnd),
                                        const wxAuiToolBarItem& item)
{
    if (!item.GetBitmap().IsOk() && !(m_flags & wxAUI_TB_TEXT))
        return wxSize(16,16);

    int width = item.GetBitmap().GetWidth();
    int height = item.GetBitmap().GetHeight();

    if (m_flags & wxAUI_TB_TEXT)
    {
        dc.SetFont(m_font);
        int tx, ty;

        if (m_text_orientation == wxAUI_TBTOOL_TEXT_BOTTOM)
        {
            dc.GetTextExtent(wxT("ABCDHgj"), &tx, &ty);
            height += ty;

            if ( !item.GetLabel().empty() )
            {
                dc.GetTextExtent(item.GetLabel(), &tx, &ty);
                width = wxMax(width, tx+6);
            }
        }
        else if ( m_text_orientation == wxAUI_TBTOOL_TEXT_RIGHT &&
                  !item.GetLabel().empty() )
        {
            width += 3; // space between left border and bitmap
            width += 3; // space between bitmap and text

            if ( !item.GetLabel().empty() )
            {
                dc.GetTextExtent(item.GetLabel(), &tx, &ty);
                width += tx;
                height = wxMax(height, ty);
            }
        }
    }

    // if the tool has a dropdown button, add it to the width
    if (item.HasDropDown())
        width += (BUTTON_DROPDOWN_WIDTH+4);

    return wxSize(width, height);
}

//-----------------------------------------------------------------------------
wxSize wxAuiBlackToolBarArt::GetLabelSize(
                                        wxDC& dc,
                                        wxWindow* WXUNUSED(wnd),
                                        const wxAuiToolBarItem& item)
{
    dc.SetFont(m_font);

    // get label's height
    int width = 0, height = 0;
    dc.GetTextExtent(wxT("ABCDHgj"), &width, &height);

    // get item's width
    width = item.GetMinSize().GetWidth();

    if (width == -1)
    {
        // no width specified, measure the text ourselves
        width = dc.GetTextExtent(item.GetLabel()).GetX();
    }

    return wxSize(width, height);
}

//-----------------------------------------------------------------------------
void wxAuiBlackToolBarArt::DrawBackground(
                                    wxDC& dc,
                                    wxWindow* WXUNUSED(wnd),
                                    const wxRect& rect)
{
	if ((rect.GetWidth()*rect.GetHeight()) == 0) {
		return;
	}

	if (m_flags & wxAUI_TB_VERTICAL) {
		// left rect
		dc.SetPen(*wxTRANSPARENT_PEN);
		dc.SetBrush( wxBrush(wxColor(0, 0, 0)) );
		dc.DrawRectangle( rect.x, rect.y, (rect.width/2), rect.height );

		dc.GradientFillLinear( wxRect(rect.x, rect.y, rect.width * 0.25, rect.height),
							wxColor(18, 18, 18), wxColor(45, 45, 45), wxEAST );
		dc.GradientFillLinear( wxRect(rect.x + rect.width * 0.25, rect.y, rect.width * 0.25, rect.height),
							wxColor(45, 45, 45), wxColor(0, 0, 0), wxEAST );

		// right gradient rect
		dc.GradientFillLinear( wxRect(rect.x + rect.width * 0.50, rect.y, rect.width * 0.25, rect.height),
							wxColor(76, 76, 76), wxColor(103, 103, 103), wxEAST );
		dc.GradientFillLinear( wxRect(rect.x + rect.width * 0.75, rect.y, rect.width * 0.25, rect.height),
							wxColor(103, 103, 103), wxColor(76, 76, 76), wxEAST );

		//
		dc.SetPen(wxPen(wxColor(76, 76, 76)));
		dc.DrawLine(rect.x + rect.width - 1, rect.y, rect.x + rect.width - 1, rect.y + rect.height);

	} else {
		// top gradient rect
		dc.GradientFillLinear( wxRect(rect.x, rect.y, rect.width, rect.height * 0.25),
							wxColor(76, 76, 76), wxColor(103, 103, 103), wxSOUTH );
		dc.GradientFillLinear( wxRect(rect.x, rect.y + rect.height * 0.25, rect.width, rect.height * 0.25),
							wxColor(103, 103, 103), wxColor(76, 76, 76), wxSOUTH );

		// bottom rect
		dc.SetPen(*wxTRANSPARENT_PEN);
		dc.SetBrush( wxBrush(wxColor(0, 0, 0)) );
		dc.DrawRectangle( rect.x, rect.y + (rect.height/2) - 1, rect.width, (rect.height/2) );

		dc.GradientFillLinear( wxRect(rect.x, rect.y + rect.height * 0.50, rect.width, rect.height *0.25),
							wxColor(0, 0, 0), wxColor(45, 45, 45), wxSOUTH );
		dc.GradientFillLinear( wxRect(rect.x, rect.y + rect.height * 0.75, rect.width, rect.height *0.25),
							wxColor(45, 45, 45), wxColor(18, 18, 18), wxSOUTH );

		//
		dc.SetPen(wxPen(wxColor(18, 18, 18)));
		dc.DrawLine(rect.x, rect.y + rect.height - 1, rect.x + rect.width, rect.y + rect.height - 1);
	}
}

//-----------------------------------------------------------------------------
void wxAuiBlackToolBarArt::DrawLabel(
                                    wxDC& dc,
                                    wxWindow* WXUNUSED(wnd),
                                    const wxAuiToolBarItem& item,
                                    const wxRect& rect)
{
    dc.SetFont(m_font);
    dc.SetTextForeground(*wxBLACK);

    // we only care about the text height here since the text
    // will get cropped based on the width of the item
    int text_width = 0, text_height = 0;
    dc.GetTextExtent(wxT("ABCDHgj"), &text_width, &text_height);

    // set the clipping region
    wxRect clip_rect = rect;
    clip_rect.width -= 1;
    dc.SetClippingRegion(clip_rect);

    int text_x, text_y;
    text_x = rect.x + 1;
    text_y = rect.y + (rect.height-text_height)/2;
    dc.DrawText(item.GetLabel(), text_x, text_y);
    dc.DestroyClippingRegion();
}

//-----------------------------------------------------------------------------
void wxAuiBlackToolBarArt::DrawButton(
                                    wxDC& dc,
                                    wxWindow* WXUNUSED(wnd),
                                    const wxAuiToolBarItem& item,
                                    const wxRect& rect)
{
    int text_width = 0, text_height = 0;

    if (m_flags & wxAUI_TB_TEXT) {
        dc.SetFont(m_font);

        int tx, ty;

        dc.GetTextExtent(wxT("ABCDHgj"), &tx, &text_height);
        text_width = 0;
        dc.GetTextExtent(item.GetLabel(), &text_width, &ty);
    }

    int bmp_x = 0, bmp_y = 0;
    int text_x = 0, text_y = 0;

    if (m_text_orientation == wxAUI_TBTOOL_TEXT_BOTTOM) {
        bmp_x = rect.x +
                (rect.width/2) -
                (item.GetBitmap().GetWidth()/2);

        bmp_y = rect.y +
                ((rect.height-text_height)/2) -
                (item.GetBitmap().GetHeight()/2);

        text_x = rect.x + (rect.width/2) - (text_width/2) + 1;
        text_y = rect.y + rect.height - text_height - 1;
    } else if (m_text_orientation == wxAUI_TBTOOL_TEXT_RIGHT) {
        bmp_x = rect.x + 3;

        bmp_y = rect.y +
                (rect.height/2) -
                (item.GetBitmap().GetHeight()/2);

        text_x = bmp_x + 3 + item.GetBitmap().GetWidth();
        text_y = rect.y +
                 (rect.height/2) -
                 (text_height/2);
    }


    if (!(item.GetState() & wxAUI_BUTTON_STATE_DISABLED)) {
        if (item.GetState() & wxAUI_BUTTON_STATE_PRESSED) {
            dc.SetPen(wxPen(m_highlight_colour));
            dc.SetBrush(wxBrush(wxAuiStepColour(m_highlight_colour, 150)));
            dc.DrawRectangle(rect);
        } else if ((item.GetState() & wxAUI_BUTTON_STATE_HOVER) || item.IsSticky()) {
            dc.SetPen(wxPen(m_highlight_colour));
            dc.SetBrush(wxBrush(wxAuiStepColour(m_highlight_colour, 170)));

            // draw an even lighter background for checked item hovers (since
            // the hover background is the same color as the check background)
			if (item.GetState() & wxAUI_BUTTON_STATE_CHECKED) {
                dc.SetBrush(wxBrush(wxAuiStepColour(m_highlight_colour, 180)));
			}

            dc.DrawRectangle(rect);
        } else if (item.GetState() & wxAUI_BUTTON_STATE_CHECKED) {
            // it's important to put this code in an else statment after the
            // hover, otherwise hovers won't draw properly for checked items
            dc.SetPen(wxPen(m_highlight_colour));
            dc.SetBrush(wxBrush(wxAuiStepColour(m_highlight_colour, 170)));
            dc.DrawRectangle(rect);
        }
    }

    wxBitmap bmp;
	if (item.GetState() & wxAUI_BUTTON_STATE_DISABLED) {
        bmp = item.GetDisabledBitmap();
	} else {
        bmp = item.GetBitmap();
	}

	if (!bmp.IsOk()) {
        return;
	}

    dc.DrawBitmap(bmp, bmp_x, bmp_y, true);

    // set the item's text color based on if it is disabled
    dc.SetTextForeground(*wxBLACK);
	if (item.GetState() & wxAUI_BUTTON_STATE_DISABLED) {
        dc.SetTextForeground(DISABLED_TEXT_COLOR);
	}

    if ( (m_flags & wxAUI_TB_TEXT) && !item.GetLabel().empty() ) {
        dc.DrawText(item.GetLabel(), text_x, text_y);
    }
}

//-----------------------------------------------------------------------------
void wxAuiBlackToolBarArt::DrawDropDownButton(
                                    wxDC& dc,
                                    wxWindow* WXUNUSED(wnd),
                                    const wxAuiToolBarItem& item,
                                    const wxRect& rect)
{
    int text_width = 0, text_height = 0, text_x = 0, text_y = 0;
    int bmp_x = 0, bmp_y = 0, dropbmp_x = 0, dropbmp_y = 0;

    wxRect button_rect = wxRect(rect.x,
                                rect.y,
                                rect.width-BUTTON_DROPDOWN_WIDTH,
                                rect.height);
    wxRect dropdown_rect = wxRect(rect.x+rect.width-BUTTON_DROPDOWN_WIDTH-1,
                                  rect.y,
                                  BUTTON_DROPDOWN_WIDTH+1,
                                  rect.height);

    if (m_flags & wxAUI_TB_TEXT) {
        dc.SetFont(m_font);

        int tx, ty;
        if (m_flags & wxAUI_TB_TEXT) {
            dc.GetTextExtent(wxT("ABCDHgj"), &tx, &text_height);
            text_width = 0;
        }

        dc.GetTextExtent(item.GetLabel(), &text_width, &ty);
    }

    dropbmp_x = dropdown_rect.x +
                (dropdown_rect.width/2) -
                (m_button_dropdown_bmp.GetWidth()/2);
    dropbmp_y = dropdown_rect.y +
                (dropdown_rect.height/2) -
                (m_button_dropdown_bmp.GetHeight()/2);

    if (m_text_orientation == wxAUI_TBTOOL_TEXT_BOTTOM) {
        bmp_x = button_rect.x +
                (button_rect.width/2) -
                (item.GetBitmap().GetWidth()/2);
        bmp_y = button_rect.y +
                ((button_rect.height-text_height)/2) -
                (item.GetBitmap().GetHeight()/2);

        text_x = rect.x + (rect.width/2) - (text_width/2) + 1;
        text_y = rect.y + rect.height - text_height - 1;
    } else if (m_text_orientation == wxAUI_TBTOOL_TEXT_RIGHT) {
        bmp_x = rect.x + 3;

        bmp_y = rect.y +
                (rect.height/2) -
                (item.GetBitmap().GetHeight()/2);

        text_x = bmp_x + 3 + item.GetBitmap().GetWidth();
        text_y = rect.y +
                 (rect.height/2) -
                 (text_height/2);
    }

    if (item.GetState() & wxAUI_BUTTON_STATE_PRESSED) {
        dc.SetPen(wxPen(m_highlight_colour));
        dc.SetBrush(wxBrush(wxAuiStepColour(m_highlight_colour, 140)));
        dc.DrawRectangle(button_rect);
        dc.DrawRectangle(dropdown_rect);
    } else if (item.GetState() & wxAUI_BUTTON_STATE_HOVER ||
             item.IsSticky()) {
        dc.SetPen(wxPen(m_highlight_colour));
        dc.SetBrush(wxBrush(wxAuiStepColour(m_highlight_colour, 170)));
        dc.DrawRectangle(button_rect);
        dc.DrawRectangle(dropdown_rect);
    }

    wxBitmap bmp;
    wxBitmap dropbmp;
    if (item.GetState() & wxAUI_BUTTON_STATE_DISABLED) {
        bmp = item.GetDisabledBitmap();
        dropbmp = m_disabled_button_dropdown_bmp;
    } else {
        bmp = item.GetBitmap();
        dropbmp = m_button_dropdown_bmp;
    }

	if (!bmp.IsOk()) {
        return;
	}

    dc.DrawBitmap(bmp, bmp_x, bmp_y, true);
    dc.DrawBitmap(dropbmp, dropbmp_x, dropbmp_y, true);

    // set the item's text color based on if it is disabled
    dc.SetTextForeground(*wxBLACK);
	if (item.GetState() & wxAUI_BUTTON_STATE_DISABLED) {
        dc.SetTextForeground(DISABLED_TEXT_COLOR);
	}

    if ( (m_flags & wxAUI_TB_TEXT) && !item.GetLabel().empty() ) {
        dc.DrawText(item.GetLabel(), text_x, text_y);
    }
}

//-----------------------------------------------------------------------------
void wxAuiBlackToolBarArt::DrawControlLabel(
                                    wxDC& dc,
                                    wxWindow* WXUNUSED(wnd),
                                    const wxAuiToolBarItem& item,
                                    const wxRect& rect)
{
	if (!(m_flags & wxAUI_TB_TEXT)) {
        return;
	}

	if (m_text_orientation != wxAUI_TBTOOL_TEXT_BOTTOM) {
        return;
	}

    int text_x = 0, text_y = 0;
    int text_width = 0, text_height = 0;

    dc.SetFont(m_font);

    int tx, ty;
    if (m_flags & wxAUI_TB_TEXT) {
        dc.GetTextExtent(wxT("ABCDHgj"), &tx, &text_height);
        text_width = 0;
    }

    dc.GetTextExtent(item.GetLabel(), &text_width, &ty);

    // don't draw the label if it is wider than the item width
	if (text_width > rect.width) {
        return;
	}

    // set the label's text color
    dc.SetTextForeground(*wxBLACK);

    text_x = rect.x + (rect.width/2) - (text_width/2) + 1;
    text_y = rect.y + rect.height - text_height - 1;

    if ( (m_flags & wxAUI_TB_TEXT) && !item.GetLabel().empty() ) {
        dc.DrawText(item.GetLabel(), text_x, text_y);
    }
}

//-----------------------------------------------------------------------------
void wxAuiBlackToolBarArt::DrawSeparator(
                                    wxDC& dc,
                                    wxWindow* WXUNUSED(wnd),
                                    const wxRect& _rect)
{
    bool horizontal = true;
	if (m_flags & wxAUI_TB_VERTICAL) {
        horizontal = false;
	}

    wxRect rect = _rect;

    if (horizontal) {
        rect.x += (rect.width/2);
        rect.width = 1;
        int new_height = (rect.height*3)/4;
        rect.y += (rect.height/2) - (new_height/2);
        rect.height = new_height;
    } else {
        rect.y += (rect.height/2);
        rect.height = 1;
        int new_width = (rect.width*3)/4;
        rect.x += (rect.width/2) - (new_width/2);
        rect.width = new_width;
    }

    wxColour start_colour = wxAuiStepColour(m_base_colour, 80);
    wxColour end_colour = wxAuiStepColour(m_base_colour, 80);
    dc.GradientFillLinear(rect, start_colour, end_colour, horizontal ? wxSOUTH : wxEAST);
}

//-----------------------------------------------------------------------------
void wxAuiBlackToolBarArt::DrawGripper(wxDC& dc,
                                    wxWindow* WXUNUSED(wnd),
                                    const wxRect& rect)
{
	dc.SetPen( *wxTRANSPARENT_PEN );

	// background
	//dc.SetBrush( wxBrush(wxColor(0,0,0)) );
    //dc.DrawRectangle( rect.x, rect.y, rect.width,rect.height );

	if (m_flags & wxAUI_TB_VERTICAL) {
		// left gradient rect
		dc.SetPen(*wxTRANSPARENT_PEN);
		dc.SetBrush( wxBrush(wxColor(0, 0, 0)) );
		dc.DrawRectangle( rect.x-2, rect.y, (rect.width+4)/2, rect.height );

		dc.GradientFillLinear( wxRect(rect.x-2, rect.y, (rect.width+4) * 0.25, rect.height),
							wxColor(15, 15, 15), wxColor(39, 39, 39), wxEAST );
		dc.GradientFillLinear( wxRect(rect.x-2 + (rect.width+4) * 0.25, rect.y, (rect.width+4) * 0.25, rect.height),
							wxColor(39, 39, 39), wxColor(0, 0, 0), wxEAST );

		// right gradient rect
		dc.GradientFillLinear( wxRect(rect.x-2 + (rect.width+4) * 0.50, rect.y, (rect.width+4) * 0.25, rect.height),
							wxColor(65, 65, 65), wxColor(89, 89, 89), wxEAST );
		dc.GradientFillLinear( wxRect(rect.x-2 + (rect.width+4) * 0.75, rect.y, (rect.width+4) * 0.25, rect.height),
							wxColor(89, 89, 89), wxColor(65, 65, 65), wxEAST );
	} else {
		// top gradient rect
		dc.GradientFillLinear( wxRect(rect.x, rect.y-2, rect.width, (rect.height+4) * 0.25),
							wxColor(65, 65, 65), wxColor(89, 89, 89), wxSOUTH );
		dc.GradientFillLinear( wxRect(rect.x, rect.y-2 + (rect.height+4) * 0.25, rect.width, (rect.height+4) * 0.25),
							wxColor(89, 89, 89), wxColor(65, 65, 65), wxSOUTH );

		// bottom rect
		dc.SetPen(*wxTRANSPARENT_PEN);
		dc.SetBrush( wxBrush(wxColor(0, 0, 0)) );
		dc.DrawRectangle( rect.x, rect.y-2 + ((rect.height+4)/2) - 1, rect.width, ((rect.height+4)/2) );

		dc.GradientFillLinear( wxRect(rect.x, rect.y-2 + (rect.height+4) * 0.50, rect.width, (rect.height+4) *0.25),
							wxColor(0, 0, 0), wxColor(39, 39, 39), wxSOUTH );
		dc.GradientFillLinear( wxRect(rect.x, rect.y-2 + (rect.height+4) * 0.75, rect.width, (rect.height+4) *0.25),
							wxColor(39, 39, 39), wxColor(15, 15, 15), wxSOUTH );
	}

	const int START_PIXELS = 2;
	const int DELTA_PIXELS = 5;

	// 3x3 bar
	wxColour gripper_colours[9];
	gripper_colours[0] = wxColour(88,88,88);
	gripper_colours[1] = wxColour(132,132,132);
	gripper_colours[2] = wxColour(220,220,220);
	gripper_colours[3] = wxColour(132,132,132);
	gripper_colours[4] = wxColour(220,220,220);
	gripper_colours[5] = wxColour(255,255,255);
	gripper_colours[6] = wxColour(220,220,220);
	gripper_colours[7] = wxColour(255,255,255);
	gripper_colours[8] = wxColour(255,255,255);

	int cnt;
	int cur_x, cur_y;
	if (m_flags & wxAUI_TB_VERTICAL) {
		cnt = (rect.GetWidth() - START_PIXELS) / DELTA_PIXELS;
		cur_x = START_PIXELS;
		for (int i = 0; i < cnt; ++i) {
			for (int xx = 0; xx < 3; ++xx) {
				for (int yy = 0; yy < 3; ++yy) {
					dc.SetPen(wxPen(gripper_colours[xx*3 + yy]));
					dc.DrawPoint(cur_x + 2 - xx, rect.GetTop() + 2 + yy);
				}
			}
			cur_x += DELTA_PIXELS;
		}
	} else {
		cnt = (rect.GetHeight() + 1 - START_PIXELS) / DELTA_PIXELS;
		cur_y = START_PIXELS;
		for (int i = 0; i < cnt; ++i) {
			for (int yy = 0; yy < 3; ++yy) {
				for (int xx = 0; xx < 3; ++xx) {
					dc.SetPen(wxPen(gripper_colours[xx + yy*3]));
					dc.DrawPoint(rect.GetLeft() + 2 + xx, rect.GetTop() + cur_y + yy);
				}
			}
			cur_y += DELTA_PIXELS;
		}
	}
}

//-----------------------------------------------------------------------------
void wxAuiBlackToolBarArt::DrawOverflowButton(wxDC& dc,
                                          wxWindow* wnd,
                                          const wxRect& rect,
                                          int state)
{
    if (state & wxAUI_BUTTON_STATE_HOVER ||
        state & wxAUI_BUTTON_STATE_PRESSED) {
        wxRect cli_rect = wnd->GetClientRect();
        wxColor light_gray_bg = wxAuiStepColour(m_highlight_colour, 170);

        if (m_flags & wxAUI_TB_VERTICAL) {
            dc.SetPen(wxPen(m_highlight_colour));
            dc.DrawLine(rect.x, rect.y, rect.x+rect.width, rect.y);
            dc.SetPen(wxPen(light_gray_bg));
            dc.SetBrush(wxBrush(light_gray_bg));
            dc.DrawRectangle(rect.x, rect.y+1, rect.width, rect.height);
        } else {
            dc.SetPen(wxPen(m_highlight_colour));
            dc.DrawLine(rect.x, rect.y, rect.x, rect.y+rect.height);
            dc.SetPen(wxPen(light_gray_bg));
            dc.SetBrush(wxBrush(light_gray_bg));
            dc.DrawRectangle(rect.x+1, rect.y, rect.width, rect.height);
        }
    }

    int x = rect.x+1+(rect.width-m_overflow_bmp.GetWidth())/2;
    int y = rect.y+1+(rect.height-m_overflow_bmp.GetHeight())/2;
    dc.DrawBitmap(m_overflow_bmp, x, y, true);
}

//-----------------------------------------------------------------------------
int wxAuiBlackToolBarArt::ShowDropDown(wxWindow* wnd,
                                         const wxAuiToolBarItemArray& items)
{
    wxMenu menuPopup;

    size_t items_added = 0;

    size_t i, count = items.GetCount();
    for (i = 0; i < count; ++i) {
        wxAuiToolBarItem& item = items.Item(i);

        if (item.GetKind() == wxITEM_NORMAL) {
            wxString text = item.GetShortHelp();
			if (text.empty()) {
                text = item.GetLabel();
			}

			if (text.empty()) {
                text = wxT(" ");
			}

            wxMenuItem* m =  new wxMenuItem(&menuPopup, item.GetId(), text, item.GetShortHelp());

            m->SetBitmap(item.GetBitmap());
            menuPopup.Append(m);
            items_added++;
        } else if (item.GetKind() == wxITEM_SEPARATOR) {
			if (items_added > 0) {
                menuPopup.AppendSeparator();
			}
        }
    }

    // find out where to put the popup menu of window items
    wxPoint pt = ::wxGetMousePosition();
    pt = wnd->ScreenToClient(pt);

    // find out the screen coordinate at the bottom of the tab ctrl
    wxRect cli_rect = wnd->GetClientRect();
    pt.y = cli_rect.y + cli_rect.height;

    ToolbarCommandCapture* cc = new ToolbarCommandCapture;
    wnd->PushEventHandler(cc);
    wnd->PopupMenu(&menuPopup, pt);
    int command = cc->GetCommandId();
    wnd->PopEventHandler(true);

    return command;
}

//-----------------------------------------------------------------------------
#endif // wxUSE_AUI

newage
Knows some wx things
Knows some wx things
Posts: 28
Joined: Sun Dec 08, 2013 6:34 am

Re: how to use the DrawBackground function of wxAuiToolBarAr

Post by newage » Sat Dec 14, 2013 11:34 am

I fix the file.
wxwidgets\src\aui\auibar.cpp
//wxColour startColour = m_baseColour.ChangeLightness(150);//del this row
//wxColour endColour = m_baseColour.ChangeLightness(90);//del this row
wxColour startColour = wxColour(243,251,215);//add this row
wxColour endColour = wxColour(216,240,233);//add this row

and rebuild wx3.0.

But,I must rebuild wx,if i want to change the colour.

User avatar
T-Rex
Moderator
Moderator
Posts: 1195
Joined: Sat Oct 23, 2004 9:58 am
Location: Zaporizhzhya, Ukraine
Contact:

Re: how to use the DrawBackground function of wxAuiToolBarAr

Post by T-Rex » Sat Dec 14, 2013 2:46 pm

you should subclass from standard dockart/toolbarart class and use the derived class in your app, I showed you the sample of such derived class in previous post, no need to change wx sources and rebuild

newage
Knows some wx things
Knows some wx things
Posts: 28
Joined: Sun Dec 08, 2013 6:34 am

Re: how to use the DrawBackground function of wxAuiToolBarAr

Post by newage » Sun Dec 15, 2013 11:36 am

ok,I'm solved it.Just by fix the wx scrouce code.

eranif
Moderator
Moderator
Posts: 607
Joined: Tue Nov 29, 2005 7:10 pm
Location: Israel

Re: how to use the DrawBackground function of wxAuiToolBarAr

Post by eranif » Mon Dec 16, 2013 11:34 am

This is not the correct way of doing this, as T-Rex suggested, you need to subclass from the default art provider and override the "DrawBackground" function:

Code: Select all

class MyAuiTBArt : public wxAuiDefaultToolBarArt
{
public:
    MyAuiTBArt() {}
    virtual ~MyAuiTBArt() {}
    
    // This one is important, without it, it wont work
    wxAuiToolBarArt *Clone() {
        return new MyAuiTBArt();
    }
    virtual void DrawBackground(wxDC& dc, wxWindow* wnd, const wxRect& rect) {
        wxUnusedVar(wnd);
        wxColour initialColour = wxColour(243,251,215);
        wxColour endColour = wxColour(216,240,233);
        dc.GradientFillLinear(rect, initialColour, endColour);
    }
    
};
The important thing here (which is not mentioned in the docs) is that you _must_ implement the Clone() function or else your art provider won't be used by wx internally

To use the new art provider:

Code: Select all

myToolbar->SetArtProvider( new MyAuiTBArt() );
Eran
IDE: CodeLite + wxCrafter
OS: All
https://wxcrafter.codelite.org
https://codelite.org

newage
Knows some wx things
Knows some wx things
Posts: 28
Joined: Sun Dec 08, 2013 6:34 am

Re: how to use the DrawBackground function of wxAuiToolBarAr

Post by newage » Wed Dec 18, 2013 11:41 am

thanks.eranif.

Post Reply