Change Spacer size during runtime  [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.
Wanderer82
Earned some good credits
Earned some good credits
Posts: 130
Joined: Tue Jul 26, 2016 2:00 pm

Change Spacer size during runtime

Postby Wanderer82 » Wed Aug 08, 2018 9:24 pm

Hello

I'd like to know how I can change the size of a spacer (BoxSizer) during the runtime of my program...

Thanks a lot,
Thomas

cneng3
Knows some wx things
Knows some wx things
Posts: 26
Joined: Sat Apr 28, 2018 7:43 pm

Re: Change Spacer size during runtime

Postby cneng3 » Wed Aug 08, 2018 9:51 pm

All add() functions from wxSizer returns a point of wxSizerItem, which points to the new wxSizerItem they just created. In your case, this is your spacer. So as long as you keep tracks of these returned wxSizerItem pointers, you can modify them

Wanderer82
Earned some good credits
Earned some good credits
Posts: 130
Joined: Tue Jul 26, 2016 2:00 pm

Re: Change Spacer size during runtime

Postby Wanderer82 » Wed Aug 08, 2018 10:13 pm

That unfortunately is too demanding for me. I'd need a concrete code.

So what I'm doing is very basic... just adding a BoxSizer to my wxFrame:

Code: Select all

...
wxBoxSizer* BoxSizer6;
Create(parent, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_STYLE|wxSIMPLE_BORDER, _T("wxID_ANY"));
BoxSizer6 = new wxBoxSizer(wxHORIZONTAL);
BoxSizer6->Add((SuS+100),5,0, wxLEFT|wxRIGHT|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 3);
...


Now later on I need to change the BoxSizer6 size according to the variable "SuS" that changes after clicking a BitmapButton. So I need to just refreseh this BoxSizer6 so that it uses the new value of "SuS".

User avatar
doublemax
Moderator
Moderator
Posts: 12671
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Change Spacer size during runtime

Postby doublemax » Wed Aug 08, 2018 10:34 pm

Untested (just by reading the documentation):

Code: Select all

// in the header of the class that contains the below code, add this member variable:
// wxSizerItem *m_spacer;

m_spacer = BoxSizer6->Add((SuS+100),5,0, wxLEFT|wxRIGHT|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 3)//

// later, if you want to change the size of the spacer:

// If BoxSizer6 is a horizontal sizer
m_spacer->AssignSpacer( width, 0 );

// If BoxSizer6 is a veritical sizer
m_spacer->AssignSpacer( 0, height );
Use the source, Luke!

Wanderer82
Earned some good credits
Earned some good credits
Posts: 130
Joined: Tue Jul 26, 2016 2:00 pm

Re: Change Spacer size during runtime

Postby Wanderer82 » Wed Aug 08, 2018 10:51 pm

There's no error but nothing happens if I click Button1. And well, let me explain again: What I want to change is the size of a spacer inside a BoxSizer. Maybe that wasn't clear enough. I don't want to change the size of the BoxSizer itself.

Here's the entire code (I excluded most of the code in the extract above):

Wall_BehaveMain.h:

Code: Select all

#ifndef WALL_BEHAVEMAIN_H
#define WALL_BEHAVEMAIN_H

//(*Headers(Wall_BehaveFrame)
#include <wx/sizer.h>
#include <wx/stattext.h>
#include <wx/menu.h>
#include <wx/panel.h>
#include <wx/bmpbuttn.h>
#include <wx/statbmp.h>
#include <wx/button.h>
#include <wx/frame.h>
#include <wx/gauge.h>
#include <wx/statusbr.h>
//*)

class Wall_BehaveFrame: public wxFrame
{
    public:

        Wall_BehaveFrame(wxWindow* parent,wxWindowID id = -1);
        virtual ~Wall_BehaveFrame();

    private:

        //(*Handlers(Wall_BehaveFrame)
        void OnQuit(wxCommandEvent& event);
        void OnAbout(wxCommandEvent& event);
        void OnButton1Click(wxCommandEvent& event);
        void OnBitmapButton1Click(wxCommandEvent& event);
        void OnBitmapButton2Click(wxCommandEvent& event);
        void OnBitmapButton3Click(wxCommandEvent& event);

        //*)

        //(*Identifiers(Wall_BehaveFrame)
        static const long ID_STATICBITMAP1;
        static const long ID_STATICTEXT1;
        static const long ID_GAUGE3;
        static const long ID_STATICTEXT2;
        static const long ID_BITMAPBUTTON3;
        static const long ID_BITMAPBUTTON2;
        static const long ID_BITMAPBUTTON1;
        static const long ID_BUTTON1;
        static const long ID_PANEL1;
        static const long idMenuQuit;
        static const long idMenuAbout;
        static const long ID_STATUSBAR1;
        //*)

        //(*Declarations(Wall_BehaveFrame)
        wxGauge* Gauge3;
        wxStaticText* StaticText2;
        wxButton* Button1;
        wxStaticBitmap* StaticBitmap1;
        wxPanel* Panel1;
        wxBitmapButton* BitmapButton2;
        wxStaticText* StaticText1;
        wxBitmapButton* BitmapButton1;
        wxStatusBar* StatusBar1;
        wxBitmapButton* BitmapButton3;
        wxSizerItem *m_spacer;
        //*)

        DECLARE_EVENT_TABLE()
};

#endif // WALL_BEHAVEMAIN_H


Wall_BehaveMain.cpp:

Code: Select all

#include "Wall_BehaveMain.h"
#include <wx/msgdlg.h>
#include <fstream>
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

string int_to_string(int Wert_int)
{
    ostringstream Str_Wert;
    Str_Wert << Wert_int;
    string Wert_string(Str_Wert.str());
    return Wert_string;
}

int SuS = 0;
string SuS_string = int_to_string(SuS);
int Tot;
int Kugeln = 3;


//(*InternalHeaders(Wall_BehaveFrame)
#include <wx/bitmap.h>
#include <wx/settings.h>
#include <wx/font.h>
#include <wx/intl.h>
#include <wx/image.h>
#include <wx/string.h>
//*)

//helper functions
enum wxbuildinfoformat {
    short_f, long_f };

wxString wxbuildinfo(wxbuildinfoformat format)
{
    wxString wxbuild(wxVERSION_STRING);

    if (format == long_f )
    {
#if defined(__WXMSW__)
        wxbuild << _T("-Windows");
#elif defined(__UNIX__)
        wxbuild << _T("-Linux");
#endif

#if wxUSE_UNICODE
        wxbuild << _T("-Unicode build");
#else
        wxbuild << _T("-ANSI build");
#endif // wxUSE_UNICODE
    }

    return wxbuild;
}

//(*IdInit(Wall_BehaveFrame)
const long Wall_BehaveFrame::ID_STATICBITMAP1 = wxNewId();
const long Wall_BehaveFrame::ID_STATICTEXT1 = wxNewId();
const long Wall_BehaveFrame::ID_GAUGE3 = wxNewId();
const long Wall_BehaveFrame::ID_STATICTEXT2 = wxNewId();
const long Wall_BehaveFrame::ID_BITMAPBUTTON3 = wxNewId();
const long Wall_BehaveFrame::ID_BITMAPBUTTON2 = wxNewId();
const long Wall_BehaveFrame::ID_BITMAPBUTTON1 = wxNewId();
const long Wall_BehaveFrame::ID_BUTTON1 = wxNewId();
const long Wall_BehaveFrame::ID_PANEL1 = wxNewId();
const long Wall_BehaveFrame::idMenuQuit = wxNewId();
const long Wall_BehaveFrame::idMenuAbout = wxNewId();
const long Wall_BehaveFrame::ID_STATUSBAR1 = wxNewId();
//*)

BEGIN_EVENT_TABLE(Wall_BehaveFrame,wxFrame)
    //(*EventTable(Wall_BehaveFrame)
    //*)
END_EVENT_TABLE()

Wall_BehaveFrame::Wall_BehaveFrame(wxWindow* parent,wxWindowID id)
{
    //(*Initialize(Wall_BehaveFrame)
    wxBoxSizer* BoxSizer4;
    wxBoxSizer* BoxSizer6;
    wxBoxSizer* BoxSizer5;
    wxMenuItem* MenuItem2;
    wxMenuItem* MenuItem1;
    wxBoxSizer* BoxSizer2;
    wxMenu* Menu1;
    wxBoxSizer* BoxSizer1;
    wxMenuBar* MenuBar1;
    wxBoxSizer* BoxSizer3;
    wxMenu* Menu2;

    Create(parent, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_STYLE|wxSIMPLE_BORDER, _T("wxID_ANY"));
    BoxSizer1 = new wxBoxSizer(wxHORIZONTAL);
    Panel1 = new wxPanel(this, ID_PANEL1, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL, _T("ID_PANEL1"));
    BoxSizer2 = new wxBoxSizer(wxVERTICAL);
    BoxSizer3 = new wxBoxSizer(wxVERTICAL);
    BoxSizer6 = new wxBoxSizer(wxHORIZONTAL);
    m_spacer = BoxSizer6->Add((SuS+100),5,0, wxLEFT|wxRIGHT|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 3);
    StaticBitmap1 = new wxStaticBitmap(Panel1, ID_STATICBITMAP1, wxBitmap(wxImage(_T("C:\\Users\\Thomas\\Documents\\Programmprojekte\\Wall-Behave\\arrow-rose.png"))), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICBITMAP1"));
    BoxSizer6->Add(StaticBitmap1, 0, wxLEFT|wxRIGHT|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 0);
    BoxSizer3->Add(BoxSizer6, 0, wxEXPAND, 0);
    BoxSizer4 = new wxBoxSizer(wxHORIZONTAL);
    StaticText1 = new wxStaticText(Panel1, ID_STATICTEXT1, _("Andrea"), wxDefaultPosition, wxSize(100,20), 0, _T("ID_STATICTEXT1"));
    wxFont StaticText1Font(12,wxFONTFAMILY_SWISS,wxFONTSTYLE_NORMAL,wxFONTWEIGHT_BOLD,false,wxEmptyString,wxFONTENCODING_DEFAULT);
    StaticText1->SetFont(StaticText1Font);
    BoxSizer4->Add(StaticText1, 1, wxLEFT|wxRIGHT|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 6);
    Gauge3 = new wxGauge(Panel1, ID_GAUGE3, 60, wxDefaultPosition, wxSize(300,20), 0, wxDefaultValidator, _T("ID_GAUGE3"));
    Gauge3->SetValue(SuS);
    BoxSizer4->Add(Gauge3, 0, wxLEFT|wxRIGHT|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 0);
    StaticText2 = new wxStaticText(Panel1, ID_STATICTEXT2, _(SuS_string + " / 60"), wxDefaultPosition, wxSize(50,20), 0, _T("ID_STATICTEXT2"));
    wxFont StaticText2Font(11,wxFONTFAMILY_SWISS,wxFONTSTYLE_NORMAL,wxFONTWEIGHT_BOLD,false,_T("Arial"),wxFONTENCODING_DEFAULT);
    StaticText2->SetFont(StaticText2Font);
    BoxSizer4->Add(StaticText2, 0, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    BoxSizer3->Add(BoxSizer4, 0, wxALL|wxEXPAND, 0);
    BoxSizer5 = new wxBoxSizer(wxHORIZONTAL);
    BitmapButton3 = new wxBitmapButton(Panel1, ID_BITMAPBUTTON3, wxBitmap(wxImage(_T("C:\\Users\\Thomas\\Documents\\Programmprojekte\\Wall-Behave\\kugel-rot-ok.png"))), wxDefaultPosition, wxDefaultSize, wxNO_BORDER, wxDefaultValidator, _T("ID_BITMAPBUTTON3"));
    BitmapButton3->SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_MENU));
    BoxSizer5->Add(BitmapButton3, 0, wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 0);
    BitmapButton2 = new wxBitmapButton(Panel1, ID_BITMAPBUTTON2, wxBitmap(wxImage(_T("C:\\Users\\Thomas\\Documents\\Programmprojekte\\Wall-Behave\\kugel-rot-ok.png"))), wxDefaultPosition, wxDefaultSize, wxNO_BORDER, wxDefaultValidator, _T("ID_BITMAPBUTTON2"));
    BitmapButton2->SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_MENU));
    BoxSizer5->Add(BitmapButton2, 0, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 0);
    BitmapButton1 = new wxBitmapButton(Panel1, ID_BITMAPBUTTON1, wxBitmap(wxImage(_T("C:\\Users\\Thomas\\Documents\\Programmprojekte\\Wall-Behave\\kugel-rot-ok.png"))), wxDefaultPosition, wxDefaultSize, wxNO_BORDER, wxDefaultValidator, _T("ID_BITMAPBUTTON1"));
    BitmapButton1->SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_MENU));
    BoxSizer5->Add(BitmapButton1, 0, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 0);
    Button1 = new wxButton(Panel1, ID_BUTTON1, _("Übernehmen"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON1"));
    BoxSizer5->Add(Button1, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    BoxSizer3->Add(BoxSizer5, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    BoxSizer2->Add(BoxSizer3, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    Panel1->SetSizer(BoxSizer2);
    BoxSizer2->Fit(Panel1);
    BoxSizer2->SetSizeHints(Panel1);
    BoxSizer1->Add(Panel1, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 0);
    SetSizer(BoxSizer1);
    MenuBar1 = new wxMenuBar();
    Menu1 = new wxMenu();
    MenuItem1 = new wxMenuItem(Menu1, idMenuQuit, _("Quit\tAlt-F4"), _("Quit the application"), wxITEM_NORMAL);
    Menu1->Append(MenuItem1);
    MenuBar1->Append(Menu1, _("&File"));
    Menu2 = new wxMenu();
    MenuItem2 = new wxMenuItem(Menu2, idMenuAbout, _("About\tF1"), _("Show info about this application"), wxITEM_NORMAL);
    Menu2->Append(MenuItem2);
    MenuBar1->Append(Menu2, _("Help"));
    SetMenuBar(MenuBar1);
    StatusBar1 = new wxStatusBar(this, ID_STATUSBAR1, 0, _T("ID_STATUSBAR1"));
    int __wxStatusBarWidths_1[1] = { -1 };
    int __wxStatusBarStyles_1[1] = { wxSB_NORMAL };
    StatusBar1->SetFieldsCount(1,__wxStatusBarWidths_1);
    StatusBar1->SetStatusStyles(1,__wxStatusBarStyles_1);
    SetStatusBar(StatusBar1);
    BoxSizer1->Fit(this);
    BoxSizer1->SetSizeHints(this);

    Connect(ID_BITMAPBUTTON1,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&Wall_BehaveFrame::OnBitmapButton1Click);
    Connect(ID_BITMAPBUTTON2,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&Wall_BehaveFrame::OnBitmapButton2Click);
    Connect(ID_BITMAPBUTTON3,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&Wall_BehaveFrame::OnBitmapButton3Click);
    Connect(ID_BUTTON1,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&Wall_BehaveFrame::OnButton1Click);
    Connect(idMenuQuit,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&Wall_BehaveFrame::OnQuit);
    Connect(idMenuAbout,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&Wall_BehaveFrame::OnAbout);
    //*)
}

Wall_BehaveFrame::~Wall_BehaveFrame()
{
    //(*Destroy(Wall_BehaveFrame)
    //*)
}

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

void Wall_BehaveFrame::OnAbout(wxCommandEvent& event)
{
    wxString msg = wxbuildinfo(long_f);
    wxMessageBox(msg, _("Welcome to..."));
}

void Wall_BehaveFrame::OnBitmapButton1Click(wxCommandEvent& event)
{
    if (Kugeln == 3)
    {
        BitmapButton1->SetBitmap(wxImage(_T("C:\\Users\\Thomas\\Documents\\Programmprojekte\\Wall-Behave\\kugel-grau-ok.png")));
        Kugeln = Kugeln - 1;
    }
    else if (Kugeln == 2)
    {
        BitmapButton1->SetBitmap(wxImage(_T("C:\\Users\\Thomas\\Documents\\Programmprojekte\\Wall-Behave\\kugel-rot-ok.png")));
        Kugeln = Kugeln + 1;
    }
}

void Wall_BehaveFrame::OnBitmapButton2Click(wxCommandEvent& event)
{
    wxMessageBox(int_to_string(Kugeln));
    if (Kugeln == 3)
    {
    }
    else if (Kugeln == 2)
    {
        BitmapButton2->SetBitmap(wxImage(_T("C:\\Users\\Thomas\\Documents\\Programmprojekte\\Wall-Behave\\kugel-grau-ok.png")));
        Kugeln = Kugeln - 1;
    }
    else if (Kugeln == 1)
    {
        BitmapButton2->SetBitmap(wxImage(_T("C:\\Users\\Thomas\\Documents\\Programmprojekte\\Wall-Behave\\kugel-rot-ok.png")));
        Kugeln = Kugeln + 1;
    }
}

void Wall_BehaveFrame::OnBitmapButton3Click(wxCommandEvent& event)
{
    if (Kugeln == 1)
    {
        BitmapButton3->SetBitmap(wxImage(_T("C:\\Users\\Thomas\\Documents\\Programmprojekte\\Wall-Behave\\kugel-grau-ok.png")));
        Kugeln = Kugeln - 1;
    }
    else if (Kugeln == 0)
    {
        BitmapButton3->SetBitmap(wxImage(_T("C:\\Users\\Thomas\\Documents\\Programmprojekte\\Wall-Behave\\kugel-rot-ok.png")));
        Kugeln = Kugeln + 1;
    }
}


void Wall_BehaveFrame::OnButton1Click(wxCommandEvent& event)
{
    SuS = SuS + Kugeln;
    SuS_string = int_to_string(SuS);
    Gauge3->SetValue(SuS);
    StaticText2->SetLabelText(SuS_string + " / 60");
    BitmapButton1->SetBitmap(wxImage(_T("C:\\Users\\Thomas\\Documents\\Programmprojekte\\Wall-Behave\\kugel-rot-ok.png")));
    BitmapButton2->SetBitmap(wxImage(_T("C:\\Users\\Thomas\\Documents\\Programmprojekte\\Wall-Behave\\kugel-rot-ok.png")));
    BitmapButton3->SetBitmap(wxImage(_T("C:\\Users\\Thomas\\Documents\\Programmprojekte\\Wall-Behave\\kugel-rot-ok.png")));
    Kugeln = 3;
    m_spacer->AssignSpacer((SuS + 100), 0);
 }

User avatar
doublemax
Moderator
Moderator
Posts: 12671
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Change Spacer size during runtime  [SOLVED]

Postby doublemax » Thu Aug 09, 2018 12:00 am

What I want to change is the size of a spacer inside a BoxSizer.
Yes, that's how i understood it.

Aren't you assigning the same value for the spacer again?
Also, you need to call Layout() afterwards.
Use the source, Luke!

Wanderer82
Earned some good credits
Earned some good credits
Posts: 130
Joined: Tue Jul 26, 2016 2:00 pm

Re: Change Spacer size during runtime

Postby Wanderer82 » Thu Aug 09, 2018 6:49 am

Oh, that was it. The "Layout()" was missing, now it works. Thank you!

Wanderer82
Earned some good credits
Earned some good credits
Posts: 130
Joined: Tue Jul 26, 2016 2:00 pm

Re: Change Spacer size during runtime

Postby Wanderer82 » Thu Aug 09, 2018 9:38 am

No, I'm not assigning the same value as "SuS" is modified before:

SuS = SuS + Kugeln;


Return to “C++ Development”

Who is online

Users browsing this forum: No registered users and 12 guests