Adding Your Code To 'CustomClass' Control

Are you writing your own components and need help with how to set them up or have questions about the components you are deriving from ? Ask them here.
Post Reply
Everydaydiesel
Earned some good credits
Earned some good credits
Posts: 111
Joined: Wed Oct 28, 2015 9:48 pm

Adding Your Code To 'CustomClass' Control

Post by Everydaydiesel » Thu Oct 19, 2017 7:23 pm

Hello, i have a wxDialog and I am trying to create my first control.

I added this code. (taken from http://zetcode.com/gui/wxwidgets/customwidgets/)

widget.h

Code: Select all

#ifndef WIDGET_H
#define WIDGET_H

#include <wx/wx.h>

class Widget : public wxPanel
{
public:
  Widget(wxPanel *parent, int id );

  wxPanel *m_parent;


  void OnSize(wxSizeEvent& event);
  void OnPaint(wxPaintEvent& event);  

};

#endif

widget.cpp

Code: Select all

#include <wx/wx.h>
#include "widget.h"
#include "burning.h"

int num[] = { 75, 150, 225, 300, 375, 450, 525, 600, 675 };
int asize = sizeof(num)/sizeof(num[1]);

Widget::Widget(wxPanel *parent, int id)
      : wxPanel(parent, id, wxDefaultPosition, wxSize(-1, 30), wxSUNKEN_BORDER)
{
 
  m_parent = parent;

  Connect(wxEVT_PAINT, wxPaintEventHandler(Widget::OnPaint));
  Connect(wxEVT_SIZE, wxSizeEventHandler(Widget::OnSize));

}

void Widget::OnPaint(wxPaintEvent& event)
{
  wxFont font(9, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL,
            wxFONTWEIGHT_NORMAL, false, wxT("Courier 10 Pitch"));

  wxPaintDC dc(this);
  dc.SetFont(font);
  wxSize size = GetSize();
  int width = size.GetWidth();

  Burning *burn = (Burning *) m_parent->GetParent();

  int cur_width = burn->GetCurWidth();

  int step = (int) round(width / 10.0);


  int till = (int) ((width / 750.0) * cur_width);
  int full = (int) ((width / 750.0) * 700);


  if (cur_width >= 700) {

      dc.SetPen(wxPen(wxColour(255, 255, 184))); 
      dc.SetBrush(wxBrush(wxColour(255, 255, 184)));
      dc.DrawRectangle(0, 0, full, 30);
      dc.SetPen(wxPen(wxColour(255, 175, 175)));
      dc.SetBrush(wxBrush(wxColour(255, 175, 175)));
      dc.DrawRectangle(full, 0, till-full, 30);

  } else { 

      dc.SetPen(wxPen(wxColour(255, 255, 184)));
      dc.SetBrush(wxBrush(wxColour(255, 255, 184)));
      dc.DrawRectangle(0, 0, till, 30);

  }

  dc.SetPen(wxPen(wxColour(90, 80, 60)));
  for ( int i=1; i <= asize; i++ ) {

  dc.DrawLine(i*step, 0, i*step, 6);
  wxSize size = dc.GetTextExtent(wxString::Format(wxT("%d"), num[i-1]));
  dc.DrawText(wxString::Format(wxT("%d"), num[i-1]), 
      i*step-size.GetWidth()/2, 8);
   }
}

void Widget::OnSize(wxSizeEvent& event)
{
  Refresh();
}

burning.h

Code: Select all

#ifndef BURNING_H
#define BURNING_H

#include <wx/wx.h>
#include "widget.h"

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

  void OnScroll(wxScrollEvent& event);
  int GetCurWidth();

  
  wxSlider *m_slider;
  Widget *m_wid;

  int cur_width;

};

#endif

burning.cpp

Code: Select all

#include "burning.h"
#include "widget.h"

int ID_SLIDER = 1;

Burning::Burning(const wxString& title)
       : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(350, 200))
{

  cur_width = 75;

  wxPanel *panel = new wxPanel(this, wxID_ANY);
  wxPanel *centerPanel = new wxPanel(panel, wxID_ANY);

  m_slider = new wxSlider(centerPanel, ID_SLIDER, 75, 0, 750, wxPoint(-1, -1), 
      wxSize(150, -1), wxSL_LABELS);

  wxBoxSizer *vbox = new wxBoxSizer(wxVERTICAL);
  wxBoxSizer *hbox = new wxBoxSizer(wxHORIZONTAL);
  wxBoxSizer *hbox2 = new wxBoxSizer(wxHORIZONTAL);
  wxBoxSizer *hbox3 = new wxBoxSizer(wxHORIZONTAL);

  m_wid = new Widget(panel, wxID_ANY);
  hbox->Add(m_wid, 1, wxEXPAND);

  hbox2->Add(centerPanel, 1, wxEXPAND);
  hbox3->Add(m_slider, 0, wxTOP | wxLEFT, 35);

  centerPanel->SetSizer(hbox3);

  vbox->Add(hbox2, 1, wxEXPAND);
  vbox->Add(hbox, 0, wxEXPAND);

  panel->SetSizer(vbox);
  m_slider->SetFocus();

  Connect(ID_SLIDER, wxEVT_COMMAND_SLIDER_UPDATED, 
      wxScrollEventHandler(Burning::OnScroll)); 

  Centre();

}

void Burning::OnScroll(wxScrollEvent& WXUNUSED(event))
{
 cur_width = m_slider->GetValue();
 m_wid->Refresh();
}


int Burning::GetCurWidth() 
{
 return cur_width;
}

What I am trying to do is add that control to a 'CustomClass' control that i drag and dropped into RAD environment of the Code Blocks IDE

so then in my main project, I have the main.h of the dialog form

Code: Select all


#ifndef TESTACUSTOMCONTROLMAIN_H
#define TESTACUSTOMCONTROLMAIN_H

//(*Headers(TestACustomControlDialog)
#include <wx/dialog.h>
#include <wx/button.h>
//*)

class TestACustomControlDialog: public wxDialog
{
    public:

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

    private:

        //(*Handlers(TestACustomControlDialog)
        void OnQuit(wxCommandEvent& event);
        void OnAbout(wxCommandEvent& event);
        void OnInit(wxInitDialogEvent& event);
        //*)

        //(*Identifiers(TestACustomControlDialog)
        static const long ID_CUSTOM1;
        static const long ID_BUTTON1;
        //*)

        //(*Declarations(TestACustomControlDialog)
        wxButton* Button1;
        CustomClass* Custom1;
        //*)

        DECLARE_EVENT_TABLE()
};

#endif // TESTACUSTOMCONTROLMAIN_H

And it throws this error

||=== Build: Debug in TestACustomControl (compiler: GNU GCC Compiler) ===|
TestACustomControl/TestACustomControlMain.h|40|error: ‘CustomClass’ does not name a type|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|


here is the main .cpp if you are intersted

Code: Select all


#include "TestACustomControlMain.h"
#include <wx/msgdlg.h>

//(*InternalHeaders(TestACustomControlDialog)
#include <wx/string.h>
#include <wx/intl.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(TestACustomControlDialog)
const long TestACustomControlDialog::ID_CUSTOM1 = wxNewId();
const long TestACustomControlDialog::ID_BUTTON1 = wxNewId();
//*)

BEGIN_EVENT_TABLE(TestACustomControlDialog,wxDialog)
    //(*EventTable(TestACustomControlDialog)
    //*)
END_EVENT_TABLE()

TestACustomControlDialog::TestACustomControlDialog(wxWindow* parent,wxWindowID id)
{
    //(*Initialize(TestACustomControlDialog)
    Create(parent, id, _("wxWidgets app"), wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE, _T("id"));
    Custom1 = new CustomClass(this,ID_CUSTOM1,wxPoint(8,8),wxSize(384,376),0,wxDefaultValidator,_T("ID_CUSTOM1"));
    Button1 = new wxButton(this, ID_BUTTON1, _("Label"), wxPoint(152,400), wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON1"));

    Connect(wxID_ANY,wxEVT_INIT_DIALOG,(wxObjectEventFunction)&TestACustomControlDialog::OnInit);
    //*)
}

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

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

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

void TestACustomControlDialog::OnInit(wxInitDialogEvent& event)
{
}


My question is, how can I take the 'burning' control and insert it into the "CustomClass" control instead of opening a new window.

Code: Select all

// this code of course opens a new window, but I want to display it via the "CustomClass" wxcontrol
Burning *burning = new Burning(wxT("The Burning Widget"));
burning->Show(true);

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

Re: Adding Your Code To 'CustomClass' Control

Post by doublemax » Thu Oct 19, 2017 7:52 pm

I think the CB forum (if one exist) would be a better place to ask this.
Use the source, Luke!

Post Reply