Code Block - Windows to Code Block - Ubuntu

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
omahdezavalos
In need of some credit
In need of some credit
Posts: 7
Joined: Mon Jan 07, 2019 9:40 pm

Code Block - Windows to Code Block - Ubuntu

Post by omahdezavalos »

I been follow a tutorial from a6mg in YouTube
(PART 2: C++ GUI - Creating a dialog with wxWidgets and CodeBlocls), trying to replicated in my system Ubuntu 18.04-CodeBlocks-wxWidget 3.04-g++ without any success,
any suggesting why is this happening, should be work, but it does not, run without complain but does no do loading or anything that is suppose to do.
https://www.youtube.com/watch?v=aPdzPPQSibI&t=1s

Code: Select all

/***************************************************************
 * Name:      image_uTubeMain.cpp
 * Purpose:   Code for Application Frame
 * Author:    oh ()
 * Created:   2019-02-15
 * Copyright: oh ()
 * License:
 **************************************************************/

#include "wx_pch.h"
#include "image_uTubeMain.h"
#include <wx/msgdlg.h>
#include <wx/bitmap.h>
#include <wx/filename.h>
#include <wx/dcmemory.h>
#include <wx/clipbrd.h>

//(*InternalHeaders(image_uTubeFrame)
#include <wx/settings.h>
#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(image_uTubeFrame)
const long image_uTubeFrame::ID_BUTTON1 = wxNewId();
const long image_uTubeFrame::ID_BUTTON2 = wxNewId();
const long image_uTubeFrame::ID_BUTTON3 = wxNewId();
const long image_uTubeFrame::ID_BUTTON4 = wxNewId();
const long image_uTubeFrame::ID_BUTTON5 = wxNewId();
const long image_uTubeFrame::ID_CUSTOM1 = wxNewId();
const long image_uTubeFrame::ID_CUSTOM2 = wxNewId();
const long image_uTubeFrame::ID_PANEL1 = wxNewId();
const long image_uTubeFrame::ID_BUTTON6 = wxNewId();
const long image_uTubeFrame::idMenuQuit = wxNewId();
const long image_uTubeFrame::idMenuAbout = wxNewId();
const long image_uTubeFrame::ID_STATUSBAR1 = wxNewId();
//*)

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

image_uTubeFrame::image_uTubeFrame(wxWindow* parent,wxWindowID id)
{
    //(*Initialize(image_uTubeFrame)
    wxMenuItem* MenuItem2;
    wxMenuItem* MenuItem1;
    wxMenu* Menu1;
    wxMenuBar* MenuBar1;
    wxMenu* Menu2;

    Create(parent, id, _("Image U Tuber"), wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_STYLE, _T("id"));
    SetClientSize(wxSize(462,354));
    Clear = new wxButton(this, ID_BUTTON1, _("Clear"), wxPoint(304,32), wxSize(136,29), 0, wxDefaultValidator, _T("ID_BUTTON1"));
    Load = new wxButton(this, ID_BUTTON2, _("Load"), wxPoint(304,72), wxSize(136,29), 0, wxDefaultValidator, _T("ID_BUTTON2"));
    DrawText = new wxButton(this, ID_BUTTON3, _("DrawText"), wxPoint(304,112), wxSize(136,29), 0, wxDefaultValidator, _T("ID_BUTTON3"));
    DrawCircle = new wxButton(this, ID_BUTTON4, _("DrawCircle"), wxPoint(304,152), wxSize(136,29), 0, wxDefaultValidator, _T("ID_BUTTON4"));
    Copy = new wxButton(this, ID_BUTTON5, _("Copy"), wxPoint(304,256), wxSize(136,29), 0, wxDefaultValidator, _T("ID_BUTTON5"));
    Panel1 = new wxPanel(this, ID_PANEL1, wxPoint(24,40), wxSize(264,280), wxSIMPLE_BORDER|wxTAB_TRAVERSAL, _T("ID_PANEL1"));
    Panel1->SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
    my_wxclientdc = new wxClientDC(Panel1);
    my_wxmemorydc = new wxMemoryDC();
    SaveAs = new wxButton(this, ID_BUTTON6, _("Save as ..."), wxPoint(304,296), wxSize(136,29), 0, wxDefaultValidator, _T("ID_BUTTON6"));
    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);
    FileDialog1 = new wxFileDialog(this, _("Select file"), wxEmptyString, wxEmptyString, _("BMP, GIF and PNG files|*.bmp;*.gif;*.png"), wxFD_DEFAULT_STYLE, wxDefaultPosition, wxDefaultSize, _T("wxFileDialog"));
    FileDialog2 = new wxFileDialog(this, _("Select file"), wxEmptyString, wxEmptyString, _("BMP files|*.bmp|JPEG files|*.jpeg|XPM files|*.xpm"), wxFD_SAVE|wxFD_OVERWRITE_PROMPT, wxDefaultPosition, wxDefaultSize, _T("wxFileDialog"));

    Connect(ID_BUTTON1,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&image_uTubeFrame::OnClearClick);
    Connect(ID_BUTTON2,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&image_uTubeFrame::OnLoadClick);
    Connect(ID_BUTTON3,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&image_uTubeFrame::OnDrawTextClick);
    Connect(ID_BUTTON4,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&image_uTubeFrame::OnDrawCircleClick);
    Connect(ID_BUTTON5,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&image_uTubeFrame::OnCopyClick);
    Connect(ID_BUTTON6,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&image_uTubeFrame::OnSaveAsClick);
    Connect(idMenuQuit,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&image_uTubeFrame::OnQuit);
    Connect(idMenuAbout,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&image_uTubeFrame::OnAbout);
    //*)
}

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

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

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

void image_uTubeFrame::OnClearClick(wxCommandEvent& event)
{
     my_wxclientdc->Clear();
}

void image_uTubeFrame::OnLoadClick(wxCommandEvent& event)
{
    int dlg=FileDialog1->ShowModal();
        if(dlg==wxID_OK)
        {
            my_bitmap.LoadFile(FileDialog1->GetPath(),wxBITMAP_TYPE_ANY);
            my_wxclientdc->DrawBitmap(my_bitmap,0,0,false);
        }
}

void image_uTubeFrame::OnDrawTextClick(wxCommandEvent& event)
{
    my_wxclientdc->DrawText(wxT("wxWidgets logo"),100,200);
}

void image_uTubeFrame::OnDrawCircleClick(wxCommandEvent& event)
{
     my_wxclientdc->DrawCircle(20,20,10);
}

void image_uTubeFrame::OnCopyClick(wxCommandEvent& event)
{
     wxBitmap my_bitmap_tmp(Panel1->GetSize(),wxBITMAP_SCREEN_DEPTH);
        my_wxmemorydc->SelectObject(my_bitmap_tmp);
        my_wxmemorydc->Blit(wxPoint(0,0),Panel1->GetSize(),my_wxclientdc,wxPoint(-1,-1),wxCOPY,true,wxDefaultPosition);

        if (wxTheClipboard->Open())
            {
                wxTheClipboard->SetData(new wxBitmapDataObject(my_bitmap_tmp));
                wxTheClipboard->Close();
            }
}

void image_uTubeFrame::OnSaveAsClick(wxCommandEvent& event)
{
    wxBitmap my_bitmap_tmp(Panel1->GetSize(),wxBITMAP_SCREEN_DEPTH);
        my_wxmemorydc->SelectObject(my_bitmap_tmp);
        my_wxmemorydc->Blit(wxPoint(0,0),Panel1->GetSize(),my_wxclientdc,wxPoint(-1,-1),wxCOPY,true,wxDefaultPosition);

        int dlg=FileDialog2->ShowModal();
        if(dlg==wxID_OK)
        {
            wxFileName my_file(FileDialog2->GetPath());
            wxString my_file_ext=my_file.GetExt().Lower();

            if(my_file_ext==wxT("xpm"))my_bitmap_tmp.SaveFile(FileDialog2->GetPath(),wxBITMAP_TYPE_XPM,NULL);
            else if(my_file_ext==wxT("jpeg"))my_bitmap_tmp.SaveFile(FileDialog2->GetPath(),wxBITMAP_TYPE_JPEG,NULL);
            else if(my_file_ext==wxT("bmp"))my_bitmap_tmp.SaveFile(FileDialog2->GetPath(),wxBITMAP_TYPE_BMP,NULL);

            else wxMessageBox(wxT("Extension ERROR"),wxT("ERROR"));
        }
}



/***************************************************************
 * Name:      image_uTubeMain.h
 * Purpose:   Defines Application Frame
 * Author:    oh ()
 * Created:   2019-02-15
 * Copyright: oh ()
 * License:
 **************************************************************/

#ifndef IMAGE_UTUBEMAIN_H
#define IMAGE_UTUBEMAIN_H

//(*Headers(image_uTubeFrame)
#include <wx/dcmemory.h>
#include <wx/button.h>
#include <wx/menu.h>
#include <wx/filedlg.h>
#include <wx/panel.h>
#include <wx/statusbr.h>
#include <wx/frame.h>
#include <wx/dcclient.h>
//*)

class image_uTubeFrame: public wxFrame
{
    public:

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

    private:

        //(*Handlers(image_uTubeFrame)
        void OnQuit(wxCommandEvent& event);
        void OnAbout(wxCommandEvent& event);
        void OnClearClick(wxCommandEvent& event);
        void OnLoadClick(wxCommandEvent& event);
        void OnDrawTextClick(wxCommandEvent& event);
        void OnDrawCircleClick(wxCommandEvent& event);
        void OnCopyClick(wxCommandEvent& event);
        void OnSaveAsClick(wxCommandEvent& event);
        //*)

        //(*Identifiers(image_uTubeFrame)
        static const long ID_BUTTON1;
        static const long ID_BUTTON2;
        static const long ID_BUTTON3;
        static const long ID_BUTTON4;
        static const long ID_BUTTON5;
        static const long ID_CUSTOM1;
        static const long ID_CUSTOM2;
        static const long ID_PANEL1;
        static const long ID_BUTTON6;
        static const long idMenuQuit;
        static const long idMenuAbout;
        static const long ID_STATUSBAR1;
        //*)

        //(*Declarations(image_uTubeFrame)
        wxPanel* Panel1;
        wxStatusBar* StatusBar1;
        wxButton* Copy;
        wxFileDialog* FileDialog1;
        wxFileDialog* FileDialog2;
        wxButton* DrawCircle;
        wxButton* DrawText;
        wxMemoryDC* my_wxmemorydc;
        wxClientDC* my_wxclientdc;
        wxButton* Clear;
        wxButton* Load;
        wxButton* SaveAs;
        //*)
        wxBitmap my_bitmap;
        wxBitmap my_bitmap_tmp;

        DECLARE_EVENT_TABLE()
};

#endif // IMAGE_UTUBEMAIN_H

Manolo
Can't get richer than this
Can't get richer than this
Posts: 828
Joined: Mon Apr 30, 2012 11:07 pm

Re: Code Block - Windows to Code Block - Ubuntu

Post by Manolo »

Your code is too long for a starter. I recomend to study the samples/minimal provided with wx installation. Then see other samples and learn how to add features (file dialogs, drawing, etc).

Right now, your code lacks a class (say "MyApp") deriving from wxApp, and which overrides wxApp::OnInit(). The macro wxIMPLEMENT_APP(MyApp) must be called.

What you can avoid from many samples is the use of static event binding (those wxBEGIN_EVENT_TABLE/wxEND_EVENT_TABLE) and better use Bind(), which is also preferred to Connect(). See https://docs.wxwidgets.org/trunk/overview_events.html

Also, you are setting buttons and panels as children of the same frame. It's allowed, but is nosense. Better put your controls "under" a wxPanel, so you can use tab-key to navigate among controls.

Also, please, please, use wx sizers as soon as you understand them. They are really helpful.
Post Reply