The windows version is missing many widgets Topic is solved

Do you have a typical platform dependent issue you're battling with ? Ask it here. Make sure you mention your platform, compiler, and wxWidgets version.
Post Reply
adrianix2
In need of some credit
In need of some credit
Posts: 4
Joined: Wed Jul 29, 2020 6:32 am

The windows version is missing many widgets

Post by adrianix2 » Wed Jul 29, 2020 8:42 am

Hi everybody. I'm trying make my app working on linux and windows. On linux looks good, but when I compile it in CodeBlocks on Windows many widgets are not displayed. Version with buttons (on pictures) is linux. Do you know how fix it?

Communicate.h

Code: Select all

#include "Panels.h"
#include <wx/wxprec.h>
#include <wx/filename.h>
#include <wx/textfile.h>

class Communicate : public wxFrame
{
public:
    Communicate(const wxString& title);
    ~Communicate();
    
    void OnQuit(wxCommandEvent& event);
    void OnOpenFile(wxCommandEvent& eventt);
    void OnSearch(wxCommandEvent& eventt);
    
    LeftPanel* getLeftPanel() { return lPanel; }
    RightPanel* getRightPanel() { return rPanel; }
    wxPanel* getParentPanel() { return panelParent; }

private:
    LeftPanel *lPanel;
    RightPanel *rPanel;
    wxPanel *panelParent;

};
Communicate.cpp

Code: Select all

#include "Communicate.h"

Communicate::Communicate(const wxString& title)
       : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(650, 500))
{
  this->panelParent = new wxPanel(this, wxID_ANY);

  wxMenuBar* menuBar = new wxMenuBar;
  wxMenu* menuFile = new wxMenu;
  menuFile->Append(wxID_OPEN, wxT("&Open"));
  menuFile->Append(wxID_FIND, wxT("&Search"));
  menuFile->Append(wxID_EXIT, wxT("&Quit"));
  menuBar->Append(menuFile, wxT("&File"));
  this->SetMenuBar(menuBar);

  Connect(wxID_OPEN, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(Communicate::OnOpenFile));
  Connect(wxID_FIND, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(Communicate::OnSearch));
  Connect(wxID_EXIT, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(Communicate::OnQuit));

  wxBoxSizer *mainBox = new wxBoxSizer(wxHORIZONTAL);

  this->lPanel = new LeftPanel(this->panelParent, wxID_ANY);
  this->rPanel = new RightPanel(this->panelParent, wxID_ANY);


  mainBox->Add(lPanel, 2, wxEXPAND | wxALL, 5);
  mainBox->Add(rPanel, 8, wxEXPAND | wxALL, 5);

  this->panelParent->SetSizer(mainBox);

  this->Centre();
}

void Communicate::OnQuit(wxCommandEvent& WXUNUSED(event))
{
  Close(true);
}

void Communicate::OnOpenFile(wxCommandEvent& event)
{
    wxTextFile file;

    wxFileDialog* FileDialog1 = new wxFileDialog(this);
    int result;
    result = FileDialog1->ShowModal();
    if(result==wxID_OK)
    {
        file.Close();

        //add extension checking
        wxStaticText* textArea = rPanel->getTextArea();
        textArea->SetLabel(FileDialog1->GetPath());
    }
}

void Communicate::OnSearch(wxCommandEvent& event)
{
  wxTextEntryDialog myDialog(this, _("Enter what you are looking for..."), _("Search"), _(""));
  if ( myDialog.ShowModal() == wxID_OK )
  {
      wxStaticText* textArea = rPanel->getTextArea();
      textArea->SetLabel(myDialog.GetValue());
      this->rPanel->FitInside();
  }
}

Communicate::~Communicate()
{
  delete rPanel;
  delete lPanel;
  delete panelParent;
  std::cout << "com" << std::endl;
}
Panels.h

Code: Select all

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

class LeftPanel : public wxScrolledWindow
{
public:
    LeftPanel(wxWindow* parent, wxWindowID id);
    ~LeftPanel();

    void showId(wxCommandEvent & event);
    std::vector<wxButton*> getButtons() { return buttons; }
    void addButton();
    wxWindow* getParentWindow() { return windowParent; }

private:
    wxBoxSizer* sizer;
    std::vector <wxButton*> buttons;
    wxWindow *windowParent;
};

class RightPanel : public wxScrolledWindow
{
public:
    RightPanel(wxWindow* parent, wxWindowID id);
    ~RightPanel();

    wxStaticText* getTextArea() { return textArea; }
private:
    wxStaticText *textArea;

};
Panels.cpp

Code: Select all

#include <wx/stattext.h>
#include "Communicate.h"

LeftPanel::LeftPanel(wxWindow* parent, wxWindowID id)
       : wxScrolledWindow(parent, id)
{
    this->windowParent = parent;

    this->sizer = new wxBoxSizer(wxVERTICAL);

    for (int w=0; w<10; w++)
    {
        this->buttons.push_back(new wxButton(this, w + 111, wxString::Format(wxT("Button %i"), w)));
        Connect(w + 111, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(LeftPanel::showId));
        this->sizer->Add(buttons[w], 0, wxALL, 1);
    }

    this->SetSizer(sizer);
   
    // this part makes the scrollbars show up
    this->FitInside(); // ask the sizer about the needed size
    this->SetScrollRate(5, 5);
}

void LeftPanel::showId(wxCommandEvent& event)
{
  Communicate *comm = (Communicate *) this->windowParent->GetParent();
  RightPanel *rpanel = comm->getRightPanel();
  wxStaticText *textArea = rpanel->getTextArea();
  textArea->SetLabel(wxString::Format(wxT("%d"), event.GetId()));
  this->addButton();
}

void LeftPanel::addButton()
{
    wxButton* wxB = new wxButton(this, this->buttons.size() + 112, wxString::Format(wxT("Button %i"), this->buttons.size()));
    this->buttons.push_back(wxB);
    Connect(wxB->GetId(), wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(LeftPanel::showId));
    this->sizer->Add(wxB, 0, wxALL, 3);
    this->sizer->Layout();
    this->FitInside();

}

LeftPanel::~LeftPanel()
{
    //this->sizer->Clear(1);
    //delete this->sizer;
    for(auto btn: this->buttons)
    {
        delete btn;
    }
    this->buttons.clear();
}


RightPanel::RightPanel(wxWindow* parent, wxWindowID id)
       : wxScrolledWindow(parent, id)
{
    wxBoxSizer* sizer = new wxBoxSizer(wxHORIZONTAL);
                
    this->textArea = new wxStaticText(this, -1, 
        wxT("0asd\ndasd0\n0saasas\n0ww1wsa\nas\nasdad\nasd\nasdwasd\n"));
    sizer->Add( this->textArea, 0, wxALL, 3);
    this->SetSizer(sizer);

    // this part makes the scrollbars show up
    //need to do that after text changing
    this->FitInside(); // ask the sizer about the needed size
    this->SetScrollRate(5, 5);
}

RightPanel::~RightPanel()
{
    delete this->textArea;
}
Attachments
Untitled.png
Untitled.png (6.56 KiB) Viewed 130 times
Screenshot at 2020-07-29 10-39-27.png
Screenshot at 2020-07-29 10-39-27.png (28.25 KiB) Viewed 130 times

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

Re: The windows version is missing many widgets

Post by doublemax » Wed Jul 29, 2020 4:29 pm

Whenever something like this happens, resize the window manually. If the controls appear, it's a sizer/layout issue.

Code: Select all

this->panelParent->SetSizer(mainBox);
this->panelParent->Layout();   // new line
There are a few other issues worth mentioning:

When a wxWindow gets destroyed, it also destroys all its children (and in consequence all its descendants recursively).
In addition to that, a wxFrame destroys itself when it gets closed.

In combination this means that you hardly ever have to delete any wxWindow yourself. You can remove all of these occurrences in your code. If the only purpose of the "buttons" vector is do delete the buttons later, you don't need that either.

https://docs.wxwidgets.org/trunk/overvi ... etion.html

The wxFileDialog in OnOpenFile should be created on the stack (like pretty much every wxDialog). Like you did with wxTextEntryDialog.
Use the source, Luke!

adrianix2
In need of some credit
In need of some credit
Posts: 4
Joined: Wed Jul 29, 2020 6:32 am

Re: The windows version is missing many widgets

Post by adrianix2 » Thu Jul 30, 2020 7:14 am

Thanks. Now it works

Post Reply