简体   繁体   中英

Making an application bundle with Xcode, C++ and wxWidgets

I'm new to macos, I'm trying to figure out how to make an application bundle so my code won't be just an executable file.

I'm working with xcode version 12.5 and writing a test gui application using c++ language and the wxWidgets library.

Now I tried to make a simple gui with just a button and a menu bar (The bar on top of the screen with different menus like File or About ). I encountered 2 issues:

  1. The first and the major one is that the menubar doesn't respond when the executable starts, it only works when focused on another program and then focuses back to the executable, then the menu is clickable and working. I read that this happens because I build the program as an executable and not as an application bundle. Although, couldn't figure out how to do so in xcode, and the internet doesn't quite answers how to do so.
  2. The button doesn't have a click effect when you only touch the mousepad ( you have to click it to make a blue click effect ), you have to click and hold the button to see the button change color.

CODE:

cpp file of the app ( inherits from wxApp )

bool instaStalkApp::OnInit(){
    instaFrame = new instaStalkFrame("InstaStalk", wxPoint(50,50), wxSize(P_WIDTH,P_HEIGHT));
    instaFrame->Show(true);
    SetTopWindow(instaFrame);
    
    mainPanel = new instaStalkPanel(instaFrame, ID_PANEL_MAIN, wxPoint(-1,-1), wxSize(P_WIDTH, P_HEIGHT), wxTAB_TRAVERSAL, "panel");
    
    testButton = new instaButton(mainPanel, ID_TEST_BUTTON_1, "test1" , wxPoint(P_WIDTH / 2 ,P_HEIGHT / 2), wxSize(100,20), "test1but");
    
    
    return true;
}

Header file of the button class:

#ifndef instaButton_hpp
#define instaButton_hpp

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

class instaButton : public wxButton {
private:
    
public:
    instaButton(wxWindow *parent, wxWindowID id, const wxString &label, const wxPoint &pos, const wxSize &size, const wxString &name);
    
    void On_Button1Click(wxCommandEvent &event);
    
    wxDECLARE_EVENT_TABLE();
};

#endif /* instaButton_hpp */

event table of the button:

wxBEGIN_EVENT_TABLE(instaButton, wxButton)
    EVT_BUTTON(ID_TEST_BUTTON_1, instaButton::On_Button1Click)
wxEND_EVENT_TABLE()

cpp file of custom button

#include <wx/wx.h>

#include "instaButton.hpp"


instaButton::instaButton(wxWindow *parent, wxWindowID id, const wxString &label, const wxPoint &pos, const wxSize &size, const wxString &name) : wxButton(parent, id, label, pos, size, 0, wxDefaultValidator, name){
    
}

void instaButton::On_Button1Click(wxCommandEvent &event){
    wxLogMessage("hi");
}

cpp file of the frame with the menuBar:

#include <wx/wx.h>

#include "instaStalkFrame.hpp"
#include "instaStalkPanel.hpp"
#include "consts.hpp"


instaStalkFrame::instaStalkFrame(const wxString& title, const wxPoint& pos, const wxSize& size) : wxFrame(NULL, wxID_ANY, title, pos, size){
    
    menuFile = new wxMenu;
    menuFile->Append(ID_TEST, "&Hello...\tCtrl-H",
                        "Help string shown in status bar for this menu item");
    menuFile->AppendSeparator();
    menuFile->Append(wxID_EXIT);
    
    menuHelp = new wxMenu;
    menuHelp->Append(wxID_ABOUT);
    
    menuBar = new wxMenuBar;
    menuBar->Append( menuFile, "&File" );
    menuBar->Append( menuHelp, "&Help" );
    
    SetMenuBar(menuBar);
    
}

void instaStalkFrame::OnExit(wxCommandEvent& event){
    Close(true);
}

void instaStalkFrame::OnAbout(wxCommandEvent& event){
    wxMessageBox( "This is a wxWidgets' Hello world sample",
                  "About Hello World", wxOK | wxICON_INFORMATION );
}

void instaStalkFrame::OnTest(wxCommandEvent& event){
    wxLogMessage("Test!");
}

In order to make an Application Bundle:

You should have been creating an OSX Application project inside Xcode. Check wxWiki (I know it is for older version of Xcode, but it still applies and you can try to match it in the newer version).

If you want to do that later - you can try to build minimal sample from thr wxWidgets distribution, see what it uses in order to create a bundle and replicate that in you test program. But you absolutely should make an Application Bundle project in Xcode to simplify your life.

The menu will start working when you create a Bundle. As you should.

I don't think hovering effect is implemented. You can ask about it on wx-users ML.

Let us know if you need any more guidance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM