How to show parent object.

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
dkaip
Super wx Problem Solver
Super wx Problem Solver
Posts: 334
Joined: Wed Jan 20, 2010 1:15 pm

How to show parent object.

Post by dkaip »

Hi.
From a dialog i call the wxImagePanel to see a picture (https://wiki.wxwidgets.org/An_image_panel). When dlg->Show(false) disappear, and then close the image frame, in destructor i must say dlg->Show() and all are ok. But when from other object type i call wxImagePanel, now other object must Show.
How to tell destructor to Show the object that call wxImagePanel? Thank's
Jim.
*I know that is easy, but now i am learning all that.

Code: Select all

            dlg->Show(false);
            wxFrame *frame;
            wxImagePanel * drawPane;
            wxInitAllImageHandlers();
            wxBoxSizer* sizer = new wxBoxSizer(wxHORIZONTAL);
            frame = new wxFrame(NULL, wxID_ANY, thefile, wxPoint(50,50), wxSize(800,600));
            drawPane = new wxImagePanel(frame,thefile);
            sizer->Add(drawPane, 1, wxEXPAND);
            frame->SetSizer(sizer);
            frame->Show();

class wxImagePanel : public wxPanel
{
    wxImage image;
    wxBitmap resized;
    int w, h;

public:
    wxImagePanel(wxFrame* parent, wxString file);
    ~wxImagePanel();
............

wxImagePanel::~wxImagePanel()
{
dlg->Show();
}
User avatar
eranon
Can't get richer than this
Can't get richer than this
Posts: 867
Joined: Sun May 13, 2012 11:42 pm
Location: France
Contact:

Re: How to show parent object.

Post by eranon »

Hello,

The way you create your frame sounds a little weird for me (it's like you didn't build your frame content from its ctor, while you should derive from wxFrame and build it inside its ctor), but I'll focus on your question: if you want to do something with your panel's parent, you can store its pointer in a member variable, then you can access it when you want/need.
[Ind. dev. - wxWidgets 3.0/3.1 under "Win 7 64-bit, TDM64-GCC" + "OS X 10.9, LLVM Clang"]
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How to show parent object.

Post by doublemax »

From a code structure point of view i'd say that the code to hide and show the dialog should not be in wxImagePanel. It should be either in the wxFrame destructor, or even one level higher, in the code that creates the wxFrame.

It's not totally clear what you're trying to do, but it sounds like you want to display an image in a frame where a modal dialog would be better.
Use the source, Luke!
dkaip
Super wx Problem Solver
Super wx Problem Solver
Posts: 334
Joined: Wed Jan 20, 2010 1:15 pm

Re: How to show parent object.

Post by dkaip »

Hello. Thanks for answer. From a dialog *dlg an object wxImagePanel shows image from file image...
With dlg->show(false) dialog disappear and destructor calls dlg->Show(). But some times a addimagefilesDialog frame dialog from base dialog calls wxImagePanel. Now the destructor use addimagefilesDialog->Show().
I must use 2 different classes for 2 different destructors.
If i must use one class how destructor knows the the dialog name for name->Show()?

Code: Select all

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

class wxImagePanel : public wxPanel
{
    wxImage image;
    wxBitmap resized;
    int w, h;

public:
    wxImagePanel(wxFrame* parent, wxString file);
    ~wxImagePanel();

    void paintEvent(wxPaintEvent & evt);
    void paintNow();
    void OnSize(wxSizeEvent& event);
    void render(wxDC& dc);
    DECLARE_EVENT_TABLE()
};

extern addimagefilesDialog* addimages;

BEGIN_EVENT_TABLE(wxImagePanel, wxPanel)
// catch paint events
    EVT_PAINT(wxImagePanel::paintEvent)
//Size event
    EVT_SIZE(wxImagePanel::OnSize)
END_EVENT_TABLE()

wxImagePanel::~wxImagePanel()
{
addimages->Show();
}

wxImagePanel::wxImagePanel(wxFrame* parent, wxString file ) :
    wxPanel(parent)
{
    // load the file... ideally add a check to see if loading was successful
    image.LoadFile(file);
    w = -1;
    h = -1;
}

User avatar
eranon
Can't get richer than this
Can't get richer than this
Posts: 867
Joined: Sun May 13, 2012 11:42 pm
Location: France
Contact:

Re: How to show parent object.

Post by eranon »

Red in diagonale, but not sure you didn't read in diagonale too... As doublemax pointed, your design is far from usual (take time to consider what we said above).

This said, whatever the object (ie. instance of class) or struct you want to access, you do it through a pointer. You don't need to know its name, you simply need to have a pointer to it (and of course its type, but in your case, it sounds to be a single stable type).

The best place to store a pointer to your addimagefilesDialog instance is as member variable (eg. m_addimages). You can pass this pointer as constructor parameters for example (or init it to NULL then define it afterward through a public method depending on your use case -- sometime, we don't know everything at ctor-time -- but it's more specific).

NB: In your wxImagePanel class, close to all seems to be public, while it should not : for example, your OnSize handler will be never called from outside unless very very special design and, by the way, shouldn't be public...
[Ind. dev. - wxWidgets 3.0/3.1 under "Win 7 64-bit, TDM64-GCC" + "OS X 10.9, LLVM Clang"]
Post Reply