wxGauge... with an attached formattable label!

If you have a cool piece of software to share, but you are not hosting it officially yet, please dump it in here. If you have code snippets that are useful, please donate!
Post Reply
lowjoel
Moderator
Moderator
Posts: 1511
Joined: Sun Jun 19, 2005 11:37 am
Location: Singapore
Contact:

wxGauge... with an attached formattable label!

Post by lowjoel » Tue Sep 06, 2005 2:42 pm

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
Last edited by lowjoel on Sun Oct 09, 2005 10:20 pm, edited 1 time in total.

KaReL
Experienced Solver
Experienced Solver
Posts: 78
Joined: Mon Aug 30, 2004 8:52 am
Contact:

Post by KaReL » Fri Sep 09, 2005 1:42 pm

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") );
}
wxWidgets: SVN/trunk
OS: WinXP/2 + Ubuntu + Mac 10.4.11
Compiler: VS2005 + GCC 4.2 + GCC 4.0.1
-----
home: http://www.salvania.be

lowjoel
Moderator
Moderator
Posts: 1511
Joined: Sun Jun 19, 2005 11:37 am
Location: Singapore
Contact:

Post by lowjoel » Fri Sep 09, 2005 2:07 pm

idk... i prefer it below... feel free to customize it

and yea i just dumped 1 and 2...

Jorg
Moderator
Moderator
Posts: 3971
Joined: Fri Aug 27, 2004 9:38 pm
Location: Delft, Netherlands
Contact:

Post by Jorg » Fri Sep 09, 2005 2:13 pm

I modified it to wxID_ANY (heck I was bored) ..
With regards,
- Jorgen
Forensic Software Engineer
Netherlands Forensic Insitute
http://english.forensischinstituut.nl/
-------------------------------------
Jorg's WasteBucket
http://www.xs4all.nl/~jorgb/wb

Post Reply