Setting default application for file Topic is solved

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
Argon
Earned a small fee
Earned a small fee
Posts: 10
Joined: Fri Oct 27, 2017 9:11 pm

Setting default application for file

Post by Argon »

Hi there.
Ive got a small application which generates a textfile as a specific filetype. Im using wxWidgets 2.9.2 (Windows)

Is there a wxWidgets way to set my application as default for this filetype permanently?

i tried using wxMimeTypesManager with wxFileType and wxFileTypeInfo to set the default settings. But it doesnt really work.
The only thing which works, is to set the default filetype Icon.

Code: Select all

wxMimeTypesManager mgr;
wxFileType* pFileType = mgr.GetFileTypeFromExtension ("extension");
pFileType->SetDefaultIcon(icon.GetFullPath(),0); 
mgr.Associate("extention");
To set the wxFileTypeInfo i tried to set the information via the set function, and via an Array and later tried to use wxMimeTypeManaer::Fallbacks().
which accured in an error / appcrash.
using the following

Code: Select all

 static const wxFileTypeInfo fallbacks[] =
{
	wxFileTypeInfo(wxT("extension"),
		wxT("application.GetFullPath()"),
		wxT("application.GetFullPath() -p %s"),
		wxT("application info"),
		wxT("extension"), wxNullPtr),
	wxFileTypeInfo()
	mgr.Associate(fallbacks);
};
i dont know what im doing wrong. Or is it not possible to set these values permanently?
Do i have to go thru the way to edit the registry?

With best regards.
User avatar
doublemax
Moderator
Moderator
Posts: 19162
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Setting default application for file

Post by doublemax »

Is this real code? I'm surprised it compiles. You put "mgr.Associate(fallbacks);" into the initialization for the array. It should be outside.

Code: Select all

wxT("application.GetFullPath() -p %s")
This will literally put "application.GetFullPath()" into the string, but not the return value of that method call. Try this:

Code: Select all

application.GetFullPath() + wxT(" -p %s")
It's probably necessary to put the filename parameter in quotes:

Code: Select all

application.GetFullPath() + wxT(" -p \"%s\"")
Use the source, Luke!
Argon
Earned a small fee
Earned a small fee
Posts: 10
Joined: Fri Oct 27, 2017 9:11 pm

Re: Setting default application for file

Post by Argon »

Thanks for the reply.

Its true, that mgr.Assosiacte(fallbacks) where in false place. In my code it was outside.

But unfortunatly the application crashes at the point of mgr.associate.
User avatar
doublemax
Moderator
Moderator
Posts: 19162
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Setting default application for file

Post by doublemax »

Code: Select all

mgr.associate
It's a little strange that mgr is not a pointer. You should not create an own instance of wxMimeTypesManager, but use the global instance wxTheMimeTypesManager.

If that still crashes, please show more complete code in context.
Use the source, Luke!
Argon
Earned a small fee
Earned a small fee
Posts: 10
Joined: Fri Oct 27, 2017 9:11 pm

Re: Setting default application for file

Post by Argon »

Hi Doublemax.
Thanks for that advice.

what im doing is just:

Code: Select all

wxFileName iconpath = standartpath;
wxFileName applicationpath= standartpath;
iconpath.SetName("icon");
iconpath.SetExt("ico");
if(iconpath.FileExists())
{
	wxString string= "extension";
	wxFileType* pFileType = mgr.GetFileTypeFromExtension("extension");
	//pFileType->SetDefaultIcon(iconpath.GetFullPath());
	static const wxFileTypeInfo fallbacks[] =
	{
                wxFileTypeInfo(wxT("extension"),
                               wxT("applicationpath.GetFullPath()"),
                               applicationpath.GetFullPath() + wxT("-p \%s\""),
                               wxT("extension"),
                               wxT("extension"), wxNullPtr),
                wxFileTypeInfo()
	};
	mgr.AddFallbacks(fallbacks);	
	mgr.Associate(extension);
}
using wxMimeFileManager as global pointer crashes the application ( Compiles normal )

My extension does not exist on the PC. so might that be a problem?
With how i understood the MImefiletypemanager, that isnt a problem.

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

Re: Setting default application for file

Post by doublemax »

Tested under Windows:

Code: Select all

wxString apppath( "c:\\path\\to\\some.exe" );
if( wxFile::Exists(apppath) )
{
  wxFileTypeInfo ftype( wxT("application/doublemax") );   // mimetype
  ftype.AddExtension( wxT("dmax") );
  ftype.SetOpenCommand( apppath + wxT(" \"%s\"") );
  //ftype.SetPrintCommand( apppath + wxT(" -p \"%s\"") ); // if the application supports printing
  ftype.SetDescription( wxT("long description") );
  ftype.SetShortDesc( wxT("short description") );
  ftype.SetIcon( apppath );   // use the exe icon, this works only under Windows

  wxTheMimeTypesManager->Associate(ftype);
}
Use the source, Luke!
Argon
Earned a small fee
Earned a small fee
Posts: 10
Joined: Fri Oct 27, 2017 9:11 pm

Re: Setting default application for file

Post by Argon »

ITs so easy. if you see it.

Thanks doublemax.
Post Reply