Page 1 of 1

wxGauge... with an attached formattable label!

Posted: Tue Sep 06, 2005 2:42 pm
by lowjoel

Code: Select all

//---------------------------------------------------------------------------
//
// Name:        components.h
// Author:      Joel Low
// Created:     13/08/05 10:16
// Description: ClassTools Custom Components
//
//---------------------------------------------------------------------------

#ifndef __widget_components_h__
#define __widget_components_h__

class DLLIMPORT ctProgressBar : public wxPanel
{
	public:
		ctProgressBar(wxWindow* parent, wxWindowID id, int range, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize, long style = wxGA_HORIZONTAL, const wxValidator& validator = wxDefaultValidator, const wxString& name = "gauge", wxString format = "%d%%");
		int GetBezelFace() const;
		int GetRange() const;
		int GetShadowWidth() const;
		int GetValue() const;
		bool IsVertical() const;
		void SetBezelFace(int width);
		void SetRange(int range);
		void SetShadowWidth(int width);
		void SetValue(int value);
		void SetFormat(wxString format);
		
	private:
		wxGauge* gauge;          //Gauge
		wxString format;         //Format of label
		wxStaticText* label;     //Label
};

#endif

Code: Select all

//---------------------------------------------------------------------------
//
// Name:        ctprogressbar.cpp
// Author:      Joel Low
// Created:     06/09/05 09:06
// Description: ClassTools progress bar with indicator
//
//---------------------------------------------------------------------------

#include "../components.h"
using namespace std;

ctProgressBar::ctProgressBar(wxWindow* parent, wxWindowID id, int range, const wxPoint& pos, const wxSize& size, long style, const wxValidator& validator , const wxString& name, wxString format)
  : wxPanel(parent, id, pos, size, 0)
{
	//Create a sizer to control the way the control looks
	wxBoxSizer* sizer = new wxBoxSizer(wxVERTICAL);
	
	//Create the controls, selecting styles passed to the constructor
	gauge = new wxGauge(this, wxID_ANY, range, wxDefaultPosition, size, style, validator, name);
	label = new wxStaticText(this, wxID_ANY, wxString::Format(format, 0), wxDefaultPosition, wxSize(100, 17), wxALIGN_RIGHT);
	
	//Then set the format of the label and some default values
	SetValue(0);
	SetRange(100);
	SetFormat(format);
	
	//Add the controls and call the sizer to fit according to size.
	sizer->Add(gauge, 1, wxEXPAND | wxALIGN_TOP, 0);
	sizer->Add(label, 0, wxEXPAND | wxALIGN_TOP, 0);
	this->SetSizerAndFit(sizer); 
}

//Set the format of the label. %d's will be replaced by the value of the gauge
void ctProgressBar::SetFormat(wxString format)
{
	ctProgressBar::format = format;
	SetValue(GetValue());
}

//All other functions here mirror that of wxGauge.
void ctProgressBar::SetValue(int pos)
{
	gauge->SetValue(pos);
	label->SetLabel(wxString::Format(format, pos));
}

int ctProgressBar::GetBezelFace() const
{
	return gauge->GetBezelFace();
}

int ctProgressBar::GetRange() const
{
	return gauge->GetRange();
}

int ctProgressBar::GetShadowWidth() const
{
	return gauge->GetShadowWidth();
}

int ctProgressBar::GetValue() const
{
	return gauge->GetValue();
}

bool ctProgressBar::IsVertical() const
{
	return gauge->IsVertical();
}

void ctProgressBar::SetBezelFace(int width)
{
	gauge->SetBezelFace(width);
}

void ctProgressBar::SetRange(int range)
{
	gauge->SetRange(range);
}

void ctProgressBar::SetShadowWidth(int width)
{
	gauge->SetShadowWidth(width);
}
you can #define DLLIMPORT as __declspec(dllimport) if you are using a DLL, or just #define DLLIMPORT if you aint.

Regards,
Joel

Posted: Fri Sep 09, 2005 1:42 pm
by KaReL
Am I correct to notice that you:

1/ use wrong wxWindowId's (namely 1 & 2, if you don't care, you should use wxID_ANY)
2/ First you paint the gauge, and underneath you paint the label?

Code: Select all

If ( 2 == true )
{
  wxLogWarning( wxT("I think a lot of people would prefer to have the label painted ON TOP of the gauge") );
}

Posted: Fri Sep 09, 2005 2:07 pm
by lowjoel
idk... i prefer it below... feel free to customize it

and yea i just dumped 1 and 2...

Posted: Fri Sep 09, 2005 2:13 pm
by Jorg
I modified it to wxID_ANY (heck I was bored) ..
With regards,
- Jorgen