Code To View A HTML File

From WxWiki

Jump to: navigation, search

This sample will use the system's registered http:// handler, usually the Default Browser, to display the passed URL. The URL may also be an absolute filename, in OS canonical form, eg "c:\subdir\filename.html", without the preceding "file://".

It accepts UNC paths in the form "\\server\share\file.html" on windows only.

void stViewHTMLFile(const wxString& url)
{
#ifdef __WXMAC__
 
	wxString url1(url);
	if ( url1.Left(5) != wxT("file:") )
		url1 = wxNativePathToURL(url1);
 
	OSStatus err;
	ICInstance inst;
	SInt32 startSel;
	SInt32 endSel;
 
	err = ICStart(&inst, 'STKA'); // put your app creator code here
	if ( err == noErr )
	{
#		if !TARGET_CARBON
			err = ICFindConfigFile(inst, 0, nil);
#		endif
 
		if ( err == noErr )
		{
			startSel = 0;
			endSel = wxStrlen(url1);
			err = ICLaunchURL(inst, "\p", url1, endSel, &startSel, &endSel);
		}
		ICStop(inst);
	}
 
#else
 
	wxFileType *ft =
	wxTheMimeTypesManager->GetFileTypeFromExtension(wxT("html"));
	if ( !ft )
	{
		wxLogError(
			_("Impossible to determine the file type for extension ")
			_("html. Please edit your MIME types."));
		return;
	}
 
	wxString cmd;
	bool ok = ft->GetOpenCommand(&cmd,
		wxFileType::MessageParameters(url, wxEmptyString));
	delete ft;
 
	if ( !ok )
	{
		// TODO: some kind of configuration dialog here.
		wxMessageBox(
			_("Could not determine the command for running the browser."),
			_("Browsing Problem"), wxOK | wxICON_EXCLAMATION);
		return;
	}
 
	if ( cmd.Find(wxT("http://")) != -1 )
		cmd.Replace(wxT("file://"), wxEmptyString);
 
	ok = ( wxExecute(cmd, FALSE) != 0 );
 
#endif
}
 
const wxString g_unixPathString(wxT("/"));
const wxString g_nativePathString(wxFILE_SEP_PATH);
 
// Returns the file URL for a native path
wxString wxNativePathToURL( const wxString& path )
{
#if wxCHECK_VERSION(2, 5, 0)
	return wxFileSystem::FileNameToURL(path);
#else
	wxString url = path ;
#ifdef __WXMSW__
	// unc notation
	if ( url.Find(wxT("\\\\")) == 0 )
	{
		url = url.Mid(2);
	}
	else
#endif
	{
		url.Replace(g_nativePathString, g_unixPathString);
	}
	url = wxT("file://") + url;
	return url;
#endif
}
Personal tools