How to override MacOpenFiles()

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
Neal_Miskin
In need of some credit
In need of some credit
Posts: 9
Joined: Sat Jan 12, 2019 10:00 pm

How to override MacOpenFiles()

Post by Neal_Miskin »

So, I'm trying to make a text editor program respond to double-clicks on files, or drag'n'drop onto the program icon.

So far I've gotten it to accept commands from the command line in Terminal by passing the arguments to a function within my main frame:

Code: Select all

        
bool TxtEdtApp::OnInit()
{
   //(*AppInitialize 
    bool wxsOK = true;
    wxInitAllImageHandlers();
    if ( wxsOK )
    {
    	TxtEdtFrame* Frame = new TxtEdtFrame(0);
    	Frame->Show();
    	SetTopWindow(Frame);
    	
/* Here's the bit I added */
        if (argc>1){
            Frame->OnRun(argv[1], argc);
        }

    }
    //*)
But this does not work when I try to select a text file and Open With in finder.

The closest thing to a solution I've found online is from the wxAppConsole Class reference page:
Under OS X, you need to override MacOpenFiles() since command line arguments are used differently there.
How exactly does one accomplish this?

Any help would be greatly appreciated.
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4193
Joined: Sun Jan 03, 2010 5:45 pm

Re: How to override MacOpenFiles()

Post by PB »

I know nothing about OSX but it seems simple enough, based on the mediaplayer sample:

Code: Select all

class wxMediaPlayerApp : public wxApp
{
public:
#ifdef __WXMAC__
    virtual void MacOpenFiles(const wxArrayString & fileNames ) wxOVERRIDE;
#endif
 ...

Code: Select all

#ifdef __WXMAC__

void wxMediaPlayerApp::MacOpenFiles(const wxArrayString & fileNames )
{
    // Called when a user drags files over our app
    m_frame->DoOpenFile(fileNames[0], true /* new page */);
}

#endif // __WXMAC__  
Neal_Miskin
In need of some credit
In need of some credit
Posts: 9
Joined: Sat Jan 12, 2019 10:00 pm

Re: How to override MacOpenFiles()

Post by Neal_Miskin »

I will try that later when I get time to to it.
Neal_Miskin
In need of some credit
In need of some credit
Posts: 9
Joined: Sat Jan 12, 2019 10:00 pm

Re: How to override MacOpenFiles()

Post by Neal_Miskin »

Well, I guess I sort of managed to get it working.

Code: Select all

class TxtEdtApp : public wxApp{
    public:
        virtual bool OnInit();
            #ifdef __WXMAC__
                virtual void MacOpenFiles(const wxArrayString & fileNames); //It is apparently not necessary to put "wxOVERRIDE" here.
            #endif //WXMAC
        wxString Cp;
};
But still had the problem that the TxtEdtApp::OnMacOpenFiles would not recognize the TxtEdtFrame object created in the OnInit() function.

So my solution was to just create a new frame within the MacOpenFiles() scope:

Code: Select all

#ifdef __WXMAC__
void TxtEdtApp::MacOpenFiles(const wxArrayString & fileNames ) {
        TxtEdtFrame* Frame = new TxtEdtFrame(0);
    	Frame->Show();
    	SetTopWindow(Frame);
        Frame->OnRunRun(fileNames[0], 2);
}
#endif
This works, but I wanted it to just use the one window. None of the things I tried for that worked:

Code: Select all

Frame->OnRunRun(fileNames[0], 2); //Use of undeclared identifier 'Frame'
TxtEdtFrame->OnRunRun(fileNames[0], 2); // Cannot use arrow operator on a type
TxtEdtFrame.OnRunRun(fileNames[0], 2); // Cannot use dot operator on a type
TxtEdtFrame::OnRunRun(fileNames[0], 2); // Call to non-static member function without object argument

// I tried various things like TxtEdtApp::TxtEdtFrame->OnRunRun(fileNames[0], 2); without any luck either.
Not a huge deal, but it would be cool if I could load a file in without having 2 window frames pop up. Any ideas how I might access the OnRunRun() function of the 1st frame created by the OnInit(), or failing that delete that frame and just use the newly created one from the MacOpenFiles()?
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4193
Joined: Sun Jan 03, 2010 5:45 pm

Re: How to override MacOpenFiles()

Post by PB »

Neal_Miskin wrote: Sat Mar 02, 2019 1:43 am

Code: Select all

virtual void MacOpenFiles(const wxArrayString & fileNames); //It is apparently not necessary to put "wxOVERRIDE" here.
 
wxOVERRIDE expands to override on compilers supporting it, does nothing on those that do not.
But still had the problem that the TxtEdtApp::OnMacOpenFiles would not recognize the TxtEdtFrame object created in the OnInit() function.
If you want to access the instance of TxtEdtFrame created in OnInit(), either create it as a member variable of TxtEditApp or in your case you can obtain it via GetTopWindow().
Both these solutions assume that MacOpenFiles() is called after you create an instance of TxtEdtFrame in your OnInit(). I am not sure if it is. If it is not, you need to got other way around and do not create a new frame in OnInit() if you already have one created from MacOpenFiles().
Post Reply