(Part 1) Exploring wxwidgets using zetcode’s tutorial “Widgets communicate” and Code::blocks

So…
I’ve spent a couple of weeks learning about Code::blocks,  wxwidgets and the like and I’ve have found zetcodes tutorial very helpful…  It has some nice basic code, that I can tear apart and explore..

Anyway, the next in example zetcode’s wxwidget tutorial is titled “Widgets communicate”.  Basically, its a “+” and a “-” button either increment and decrement a label.

I feel I need to run through this tutorial to create a baseline of understand on how this stuff works, but this I gotta say… if I was  to implement an equivalent vb6 program,  I could have it done in under 3 minutes… I’m probably going to wind up spending days on this..  I keep telling myself, sometimes you need to slow down before you start speeding up.

Ok… So onwards… I’m almost through the section first programs:
So lets see if I can create a new code::block project, copy over the code and get it running with out consulting my prior notes in the blog….
I’m back it’s 6:06Am the house is quiet…. Back when its running….
I’m back… 6:15Am.  I got the project copied over to Code::blocks and Build and run in 9 minutes….
I got myself a little hung up setting up wxconfig in the C:B’s build options…

Now time to go through the code to find something that I don’t understand yet..
So… the basic output looks like this (btw… I guess you can tell I”ve been playing around with the Gimp…)

I still not exactly what I’m looking for but, I’m getting there…
Back to task at hand…
One of the things that caught my eye was the user defined constants for the event specifiers in panels.h

const int ID_PLUS = 101;
const int ID_MINUS = 102;

I’ve come to the conclusion that basically anything goes with these things and your on your own as far as making sure you don’t inadvertently duplicate something.  C:B/ the compiler won’t necessarily detect that you did something really stupid. I think my test in my previous post shows that….

My first exploration of this code is not so much wxwidgets as far as it is C++.  Panels.h basically has a left and write panel defined.  The right panel has something new as far a wxwidgets so I’m going to focus on that later…  Anyway…
Here is the declaration of the LeftPanel in Panels.h:

#include <wx/wx.h>
#include <wx/panel.h>

class LeftPanel : public wxPanel
{
public:
LeftPanel(wxPanel *parent);

void OnPlus(wxCommandEvent & event);
void OnMinus(wxCommandEvent & event);

wxButton *m_plus;
wxButton *m_minus;
wxPanel *m_parent;
int count;

};

So the way this works is that you have a Wxframe which has a WxPanel inside it which has a WxButton’s within it.  Oddly enough… Look at this class declaration, I am not confused….
Cool….

Ok… Now I need to look at the Class declaration for the right panel..

{
public:
RightPanel(wxPanel *parent);

void OnSetText(wxCommandEvent & event);

wxStaticText *m_text;

};

Now this WxStaticText is something new for me. The tool tips on the IDE is not picking this up for some reason so I guess I need to research this a little further..
According to the docs:

A static text control displays one or more lines of read-only text.

In VB6 speak aka a “label”.
Todo: need to ask the question why this isn’t hightlighting in code::blocks tool tips.

So… Other than being new, I get everything here. I guess the next logical step is to see how these to class are put assembled into a frame. The declaration for that occurs in Communicate.h
Let us see what we have there:

#include “Panels.h”
#include <wx/wxprec.h>

class Communicate : public wxFrame
{
public:
Communicate(const wxString& title);

LeftPanel *m_lp;
RightPanel *m_rp;
wxPanel *m_parent;

};

So… We define a class Communicate which inherits a wxFrame. The LeftPanel, Rightpanel pointers are declared along with wxPanel *m_parent. At this point I don’t understand what wxPane *m_parent does… in communicate.h.  I suppose I should comment it out and see what happens..

Oh… I guess I need that…

#include “Communicate.h”

Communicate::Communicate(const wxString& title)
: wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(290, 150))
{
m_parent = new wxPanel(this, wxID_ANY);

wxBoxSizer *hbox = new wxBoxSizer(wxHORIZONTAL);

m_lp = new LeftPanel(m_parent);
m_rp = new RightPanel(m_parent);

hbox->Add(m_lp, 1, wxEXPAND | wxALL, 5);
hbox->Add(m_rp, 1, wxEXPAND | wxALL, 5);

m_parent->SetSizer(hbox);

this->Centre();
}

What it looks like to me is that we’re putting the two panels within another panel and then have a class called wxBoxSizer.  The h-box->Add methods seems to be inherited from wxSizer

If believe this is the correct function:
wxSizerItem* Add(wxWindow* window, int proportion = 0,int flag = 0, int border = 0, wxObject* userData = NULL)
Add:

Appends a child to the sizer. wxSizer itself is an abstract class, but the parameters are equivalent in the derived classes that you will instantiate to use it so they are described here:

I guess in this instance we have proportion =1 makes the box stretchable with the box sizer and the border = 5  is apparently just the width.  (I going to have to juice one of these up to see what happens.)
Ok… I suppose we should show what main.cpp looks like.  I’ve been wanting to make something disappear.. I figured out how to do that..

#include “main.h”
#include “Communicate.h”

IMPLEMENT_APP(MyApp)

bool MyApp::OnInit()
{

Communicate *communicate = new Communicate(wxT(“Widgets communicate”));
communicate->Show(true);
communicate->m_lp->m_minus->Show(false) // I added this for fun
return true;

Ok… that’s all well and good but the main point of this “Widgets communcation” tutorial was …..
So… I think I do that in part two..

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *