Custom widget and wxBoxSizer with wxALIGN_CENTER Topic is solved

Are you writing your own components and need help with how to set them up or have questions about the components you are deriving from ? Ask them here.
Post Reply
jgeorgal
Experienced Solver
Experienced Solver
Posts: 58
Joined: Fri Sep 15, 2006 12:47 pm

Custom widget and wxBoxSizer with wxALIGN_CENTER

Post by jgeorgal » Mon Sep 25, 2006 12:14 am

Hello,

I created a replacement for the wxStaticBitmap widget, so that I can display images of arbitrary size in windows 9x. However, I cannot figure out how I can make the container sizer center the custom widget on the provided area.

Previously I used wxStaticBitmap in which in order to center the image, I had written:

Code: Select all

wxBoxSizer* vsizer = new wxBoxSizer(wxVERTICAL);
wxStaticBitmap* logoImg = new wxStaticBitmap(this, wxID_ANY, logoBmp);
vsizer->Add(logoImg, 0, wxEXPAND | wxALIGN_CENTER | wxALL, 10);


With the custom widget however, the above does not work! If I put a border around the widget, then it seems to occupy the whole area and does not respect the DoGetBestSize method I've overridden.

What do I need to do in order to make the above code work as expected ???

Here is my implementation of wxStaticBigBitmap:

Code: Select all

#ifndef WXSTATICBIGBITMAP_H
#define WXSTATICBIGBITMAP_H

#pragma warning (disable:4996)

#include <wx/control.h>
#include <wx/bitmap.h>

////////////////////////////////////////////////////////////////////////

class WXDLLEXPORT wxStaticBigBitmap : public wxControl {
public:
	wxStaticBigBitmap (void);

	wxStaticBigBitmap (
			wxWindow* parent,
			wxWindowID id,
			const wxBitmap& label,
			const wxPoint& pos = wxDefaultPosition,
			const wxSize& size = wxDefaultSize,
			long style = 0,
			const wxString& name = "staticBigBitmap"
	);
	~wxStaticBigBitmap (void);

	bool Create (
			wxWindow* parent,
			wxWindowID id,
			const wxBitmap& label,
			const wxPoint& pos = wxDefaultPosition,
			const wxSize& size = wxDefaultSize,
			long style = 0,
			const wxString& name = "staticBigBitmap"
	);

	wxBitmap    GetBitmap (void) const { return m_bitmap; }
	void		SetBitmap (const wxBitmap& bitmap);

	// overriden base class virtuals
	virtual bool AcceptsFocus (void) const { return false; }
	virtual bool HasTransparentBackground (void) { return true; }

protected:
        virtual wxBorder  GetDefaultBorder (void) const;
	virtual wxSize		DoGetBestSize (void) const;

	void OnEraseBackground (wxEraseEvent& event) {}
	void OnPaint (wxPaintEvent& event);

private:
        DECLARE_DYNAMIC_CLASS(wxStaticBigBitmap);
        DECLARE_NO_COPY_CLASS(wxStaticBigBitmap);
        DECLARE_EVENT_TABLE();

        wxBitmap m_bitmap;
};

////////////////////////////////////////////////////////////////////////


#endif // WXSTATICBIGBITMAP_H

Code: Select all

#include "wxStaticBigBitmap.h"
#include <wx/dcclient.h>

////////////////////////////////////////////////////////////////////////

IMPLEMENT_DYNAMIC_CLASS(wxStaticBigBitmap, wxControl);

BEGIN_EVENT_TABLE(wxStaticBigBitmap, wxControl)
	EVT_ERASE_BACKGROUND(wxStaticBigBitmap::OnEraseBackground)
	EVT_PAINT(wxStaticBigBitmap::OnPaint)
END_EVENT_TABLE();

//**********************************************************************

wxStaticBigBitmap::wxStaticBigBitmap (void)
{

}

//**********************************************************************

wxStaticBigBitmap::wxStaticBigBitmap (
		wxWindow*		parent,
		wxWindowID		id,
		const wxBitmap& label,
		const wxPoint&	pos,
		const wxSize&	size,
		long			style,
		const wxString& name
	)
{
	this->Create(parent, id, label, pos, size, style, name);
}

//**********************************************************************

wxStaticBigBitmap::~wxStaticBigBitmap (void)
{

}

//**********************************************************************

bool wxStaticBigBitmap::Create (
		wxWindow*		parent,
		wxWindowID		id,
		const wxBitmap& label,
		const wxPoint&	pos,
		const wxSize&	size,
		long			style,
		const wxString& name
	)
{
	if (!this->wxControl::Create(parent, id, pos, size, style, wxDefaultValidator, name))
            return false;
	this->SetBitmap(label);
	return true;
}

//**********************************************************************

void wxStaticBigBitmap::SetBitmap (const wxBitmap& bitmap)
{
	m_bitmap = bitmap;
	this->InvalidateBestSize();
}

//**********************************************************************

wxBorder wxStaticBigBitmap::GetDefaultBorder() const
	{ return wxBORDER_NONE; }

wxSize wxStaticBigBitmap::DoGetBestSize (void) const
{
	if (m_bitmap.Ok()) {
		wxSize best(m_bitmap.GetWidth(), m_bitmap.GetHeight());
		this->CacheBestSize(best);
		return best;
	}

	// this is completely arbitrary
	return wxSize(16, 16);
}

//**********************************************************************

void wxStaticBigBitmap::OnPaint (wxPaintEvent& event)
{
	wxPaintDC dc(this);
	dc.DrawBitmap(m_bitmap, 0, 0);
}

////////////////////////////////////////////////////////////////////////
Thank you,
Giannis

Cursor
Earned some good credits
Earned some good credits
Posts: 120
Joined: Sun Aug 29, 2004 3:09 pm
Location: Grenoble, France
Contact:

Post by Cursor » Wed Oct 04, 2006 11:10 am

As sizers seem to use the "best fitting size" to place windows, you can set it with wxWindow::SetBestFittingSize [1] when you want to modify it (when you load a bitmap ?).

Emilien

[1] : http://www.wxwidgets.org/manuals/2.6.3/ ... ittingsize
What is little and green, witch go up and down ??
Yoda playing with the force.

jgeorgal
Experienced Solver
Experienced Solver
Posts: 58
Joined: Fri Sep 15, 2006 12:47 pm

Post by jgeorgal » Sat Dec 09, 2006 12:13 pm

Cursor wrote:As sizers seem to use the "best fitting size" to place windows, you can set it with wxWindow::SetBestFittingSize [1] when you want to modify it (when you load a bitmap ?).
Hello Emilien,

Thank you for your answer.

I'd already tried that. The problem however was the wxEXPAND flag in the sizer so the line:

Code: Select all

vsizer->Add(logoImg, 0, wxEXPAND | wxALIGN_CENTER | wxALL, 10);
should be:

Code: Select all

vsizer->Add(logoImg, 0, wxALIGN_CENTER | wxALL, 10);
... and everything works as expected (I don't even remember why I put the wxEXPAND flag in the first place).

Thanks again,
Giannis

Post Reply