wxBitmapButton question

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
roarek82
Earned a small fee
Earned a small fee
Posts: 13
Joined: Sat Aug 26, 2006 2:40 am

wxBitmapButton question

Post by roarek82 » Sat Aug 26, 2006 2:51 am

Hi.
I want to change shape of wxBitmapButton. I want to create own custom button.
I want create wxBitmapButton with all functions like wxBitmapButton but I dont want rectangle wxBitmapButton.
I want to draw button shape. I want to draw elipse or circle button.
ANd my question how to do it?

I know that I must inherit from wxBitmapButton class but how to change shape of this button?

Sorry for my bad english BTW

I found on internet sth like this:

Code: Select all

class PlayButton : public wxBitmapButton
{
public:
    PlayButton(wxWindow* parent, wxWindowID id, int size);
};

const float MFACTOR = 0.22;

PlayButton::PlayButton(wxWindow* parent, wxWindowID id, int size)
{
    // calculate the triangle points
    int low  = (int) int((size - 4) *    MFACTOR)  + 2;
    int high = (int) int((size - 4) * (1-MFACTOR)) + 2;
    wxPoint points[3] = { wxPoint(low,  high), 
			  wxPoint(high, size/2), 
			  wxPoint(low,  low) };

    wxBitmap bitmap(size, size);
    wxMemoryDC dc;

    // draw the triangle
    dc.SelectObject(bitmap);
    dc.Clear();
    dc.SetPen(*wxBLACK_PEN);
    dc.SetBrush(*wxBLACK_BRUSH);
    dc.DrawPolygon(3, points);
    dc.SelectObject(wxNullBitmap);

    Reparent(parent);
    SetBitmapLabel(bitmap);
}
but it doesnt working

there is link http://lists.wxwidgets.org/archive/wx-u ... 27197.html


Regards
Kamil

BTW How to simply make own controls? I meaning change shape , colours and other functions.

benedicte
wxWorld Domination!
wxWorld Domination!
Posts: 1409
Joined: Wed Jan 19, 2005 3:44 pm
Location: Paris, France

Post by benedicte » Sat Aug 26, 2006 3:27 pm

Did you try to move the drawing part of the code into the "Draw" function instead of letting it in the constructor?

roarek82
Earned a small fee
Earned a small fee
Posts: 13
Joined: Sat Aug 26, 2006 2:40 am

Post by roarek82 » Sat Sep 02, 2006 12:34 pm

Here is my code

Projekt.cpp

Code: Select all

/////////////////////////////////////////////////////////////////////////////
// Name:        Projekt.cpp
// Author:      XX
// Created:     XX/XX/XX
// Copyright:   
/////////////////////////////////////////////////////////////////////////////

#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
    #pragma implementation "Projekt.h"
#endif

// For compilers that support precompilation
#include "wx/wxprec.h"

#ifdef __BORLANDC__
    #pragma hdrstop
#endif

// Include private headers
#include "Projekt.h"


// WDR: class implementations

//----------------------------------------------------------------------------
// MyDialog
//----------------------------------------------------------------------------

// WDR: event table for MyDialog
const float MFACTOR = 0.22; 

PlayButton::PlayButton(wxWindow* parent, wxWindowID id, int size)
{ 
    // calculate the triangle points 
    int low  = (int) int((size - 4) *    MFACTOR)  + 2; 
    int high = (int) int((size - 4) * (1-MFACTOR)) + 2; 
    wxPoint points[3] = { wxPoint(low,  high), 
                          wxPoint(high, size/2), 
                          wxPoint(low,  low) }; 

    wxBitmap bitmap(size, size); 
    wxMemoryDC dc; 

    // draw the triangle 
    dc.SelectObject(bitmap); 
    dc.Clear(); 
    dc.SetPen(*wxBLACK_PEN); 
    dc.SetBrush(*wxBLACK_BRUSH); 
    dc.DrawPolygon(3, points); 
    dc.SelectObject(wxNullBitmap); 

    Reparent(parent); 
    SetBitmapLabel(bitmap); 
}


//------------------------------------------------------------------------------
// MyFrame
//------------------------------------------------------------------------------

// WDR: event table for MyFrame

BEGIN_EVENT_TABLE(MyFrame,wxFrame)
    EVT_MENU(wxID_ABOUT, MyFrame::OnAbout)
    EVT_MENU(wxID_EXIT, MyFrame::OnQuit)
    EVT_CLOSE(MyFrame::OnCloseWindow)
END_EVENT_TABLE()

MyFrame::MyFrame( wxWindow *parent, wxWindowID id, const wxString &title,
    const wxPoint &position, const wxSize& size, long style ) :
    wxFrame( parent, id, title, position, size, style )
{
    CreateMyMenuBar();
//
    CreateStatusBar(1);
    SetStatusText( wxT("Back and") );
	MyDialogFunc( this, true );
//    PlayButton(this,20, 1000);///<-----------play button here
    FullScreen();
    
     // insert main window here
}

void MyFrame::CreateMyMenuBar()
{
    SetMenuBar( MyMenuBarFunc() );
}

// WDR: handler implementations for MyFrame



void MyFrame::FullScreen()
{
    Maximize(TRUE);
}


void MyFrame::OnAbout( wxCommandEvent &event )
{
    wxMessageDialog dialog( this, wxT("BACK END wykonal Kamil Wisniewski"),
        wxT("O programie"), wxOK|wxICON_INFORMATION );
    dialog.ShowModal();
}

void MyFrame::OnQuit( wxCommandEvent &event )
{
     Close( TRUE );
}

void MyFrame::OnCloseWindow( wxCloseEvent &event )
{
    // if ! saved changes -> return
    
    Destroy();
}

//------------------------------------------------------------------------------
// MyApp
//------------------------------------------------------------------------------

IMPLEMENT_APP(MyApp)

MyApp::MyApp()
{
}

bool MyApp::OnInit()
{
    MyFrame *frame = new MyFrame( NULL, -1, wxT("BACK END"), wxPoint(20,20), wxSize(1024,768));
    frame->Show( TRUE );
	//wxCustomButton *baton= new wxCustomButton(frame); 

    return TRUE;
}

int MyApp::OnExit()
{
    return 0;
}


Projekt.h

Code: Select all

/////////////////////////////////////////////////////////////////////////////
// Name:        Projekt.h
// Author:      XX
// Created:     XX/XX/XX
// Copyright:   
/////////////////////////////////////////////////////////////////////////////

#ifndef __Projekt_H__
#define __Projekt_H__

#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
    #pragma interface "Projekt.h"
#endif

// Include wxWindows' headers

#ifndef WX_PRECOMP
    #include <wx/wx.h>
#endif

#include "Projekt_wdr.h"

#define ID_TEST     100
// WDR: class declarations


class PlayButton : public wxBitmapButton 
{ 
public: 
    PlayButton(wxWindow* parent, wxWindowID id, int size); 
}; 




//----------------------------------------------------------------------------
// MyFrame
//----------------------------------------------------------------------------

class MyFrame: public wxFrame
{
public:
    // constructors and destructors
    MyFrame( wxWindow *parent, wxWindowID id, const wxString &title,
        const wxPoint& pos = wxDefaultPosition,
        const wxSize& size = wxDefaultSize,
        long style = wxDEFAULT_FRAME_STYLE );
    
private:
    // WDR: method declarations for MyFrame
    void CreateMyMenuBar();
    void FullScreen();
    
private:
    // WDR: member variable declarations for MyFrame
    
private:
    // WDR: handler declarations for MyFrame
    void OnTest( wxCommandEvent &event );
    void OnAbout( wxCommandEvent &event );
    void OnQuit( wxCommandEvent &event );
    void OnCloseWindow( wxCloseEvent &event );
    
private:
    DECLARE_EVENT_TABLE()
};

//----------------------------------------------------------------------------
// MyApp
//----------------------------------------------------------------------------

class MyApp: public wxApp
{
public:
    MyApp();
    
    virtual bool OnInit();
    virtual int OnExit();
};

#endif
Projekt_wdr.h

Code: Select all

//------------------------------------------------------------------------------
// Header generated by wxDesigner from file: Projekt.wdr
// Do not modify this file, all changes will be lost!
//------------------------------------------------------------------------------

#ifndef __WDR_Projekt_H__
#define __WDR_Projekt_H__

#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
    #pragma interface "Projekt_wdr.h"
#endif

// Include wxWidgets' headers

#ifndef WX_PRECOMP
    #include <wx/wx.h>
#endif

#include <wx/image.h>
#include <wx/statline.h>
#include <wx/spinbutt.h>
#include <wx/spinctrl.h>
#include <wx/splitter.h>
#include <wx/listctrl.h>
#include <wx/treectrl.h>
#include <wx/notebook.h>
#include <wx/grid.h>
#include <wx/toolbar.h>
#include <wx/tglbtn.h>

// Declare window functions

const int ID_BITMAPBUTTON = 10000;
const int ID_GRID = 10001;
wxSizer *MyDialogFunc( wxWindow *parent, bool call_fit = TRUE, bool set_sizer = TRUE );

// Declare menubar functions

const int ID_MENU = 10002;
wxMenuBar *MyMenuBarFunc();

// Declare toolbar functions

// Declare bitmap functions

wxBitmap MyBitmapsFunc( size_t index );

#endif

// End of generated file
Projekt_wdr.cpp
http://republika.pl/barbarka_kaluzna/Projekt_wdr.rar

and when I starting program I see sth like this

http://republika.pl/barbarka_kaluzna/scan.JPG

What is going on?
I want to put PlayButton below Menu

How to do it?

Regards
Kamil

Here all source code:
http://republika.pl/barbarka_kaluzna/PROJEKT.rar

Please help :) if somebody know how to do it.

roarek82
Earned a small fee
Earned a small fee
Posts: 13
Joined: Sat Aug 26, 2006 2:40 am

Post by roarek82 » Sat Sep 02, 2006 12:52 pm

and there is Projekt_wdr.cpp begin

where is code which I think maybe problem:

Code: Select all

//------------------------------------------------------------------------------
// Source code generated by wxDesigner from file: Projekt.wdr
// Do not modify this file, all changes will be lost!
//------------------------------------------------------------------------------

#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
    #pragma implementation "Projekt_wdr.h"
#endif

// For compilers that support precompilation
#include "wx/wxprec.h"

#ifdef __BORLANDC__
    #pragma hdrstop
#endif

// Include private header
#include "Projekt_wdr.h"
#include "Projekt.h"


// Euro sign hack of the year
#if wxUSE_UNICODE
    #define __WDR_EURO__ wxT("\u20ac")
#else
    #if defined(__WXMAC__)
        #define __WDR_EURO__ wxT("\xdb")
    #elif defined(__WXMSW__)
        #define __WDR_EURO__ wxT("\x80")
    #else
        #define __WDR_EURO__ wxT("\xa4")
    #endif
#endif

// Implement window functions

wxSizer *MyDialogFunc( wxWindow *parent, bool call_fit, bool set_sizer )
{
    wxBoxSizer *item0 = new wxBoxSizer( wxVERTICAL );

    wxBoxSizer *item1 = new wxBoxSizer( wxHORIZONTAL );
	PlayButton *baton=new PlayButton(parent, ID_BITMAPBUTTON, 10 ); //<-here is PlayButton I want put with other buttons (f.e. normal BitmapButton)
    wxBitmapButton *item2 = new wxBitmapButton( parent, ID_BITMAPBUTTON, MyBitmapsFunc( 0 ), wxDefaultPosition, wxDefaultSize );
    item1->Add( item2, 0, wxALIGN_CENTER, 5 );

    wxBitmapButton *item3 = new wxBitmapButton( parent, ID_BITMAPBUTTON, MyBitmapsFunc( 0 ), wxDefaultPosition, wxDefaultSize );
    item1->Add( item3, 0, wxALIGN_CENTER, 5 );

    item0->Add( item1, 1, wxGROW|wxALIGN_CENTER_VERTICAL, 5 );

    wxBoxSizer *item4 = new wxBoxSizer( wxHORIZONTAL );

    wxBoxSizer *item5 = new wxBoxSizer( wxVERTICAL );

    wxBoxSizer *item6 = new wxBoxSizer( wxVERTICAL );

    wxBitmapButton *item7 = new wxBitmapButton( parent, ID_BITMAPBUTTON, MyBitmapsFunc( 0 ), wxDefaultPosition, wxDefaultSize );
    item6->Add( baton, 0, wxALIGN_CENTER|wxALL, 5 );//there I add it

    wxBitmapButton *item8 = new wxBitmapButton( parent, ID_BITMAPBUTTON, MyBitmapsFunc( 0 ), wxDefaultPosition, wxDefaultSize );
    item6->Add( item8, 0, wxALIGN_CENTER|wxALL, 5 );

    item5->Add( item6, 5, wxGROW|wxALIGN_CENTER_VERTICAL, 5 );

    wxBoxSizer *item9 = new wxBoxSizer( wxVERTICAL );

    item5->Add( item9, 1, wxGROW|wxALIGN_CENTER_VERTICAL, 5 );

    item4->Add( item5, 1, wxGROW|wxALIGN_CENTER_HORIZONTAL, 5 );

    wxBoxSizer *item10 = new wxBoxSizer( wxVERTICAL );

    wxGrid *item11 = new wxGrid( parent, ID_GRID, wxDefaultPosition, wxSize(200,500), wxWANTS_CHARS );
    item11->CreateGrid( 10, 10, wxGrid::wxGridSelectCells );
    item10->Add( item11, 0, wxGROW|wxALIGN_CENTER_VERTICAL, 10 );

    item4->Add( item10, 10, wxGROW|wxALIGN_CENTER_HORIZONTAL, 5 );

    item0->Add( item4, 10, wxGROW|wxALIGN_CENTER_VERTICAL, 5 );

    if (set_sizer)
    {
        parent->SetSizer( item0 );
        if (call_fit)
            item0->SetSizeHints( parent );
    }
    
    return item0;
}

// Implement menubar functions

wxMenuBar *MyMenuBarFunc()
{
    wxMenuBar *item0 = new wxMenuBar;
    
    wxMenu* item1 = new wxMenu;
    item1->Append( wxID_ABOUT, wxT("O programie"), wxT("") );
    item1->Append( wxID_EXIT, wxT("Wyjscie"), wxT("") );
    item0->Append( item1, wxT("Plik") );
    
    return item0;
}

// Implement toolbar functions
all Projekt_wdr.cpp source in above post

p.s.
Sory for my bad english

roarek82
Earned a small fee
Earned a small fee
Posts: 13
Joined: Sat Aug 26, 2006 2:40 am

Post by roarek82 » Sat Sep 02, 2006 2:28 pm

Anybody know how to make circle BitmapButton?

Post Reply