wxWidgets code to launch browser

In wxWidgets, which is a wonderful multi-platform GUI C++ library loaded with really coool helper functions which works in Windows, Linux and OSX (and more), has a method called LaunchDefaultBrowser, which has lots of issues with MIME types, blah, blah.

So, as I'm sick of LaunchDefaultBrowser, which seems to always call epiphany browser under  GNU/Linux, I decided to write a function to call a browser according to certain needs/parameters. So,

This function searches the PATH environment variable for this commands (according to no articular preference):

firefoxfirefox-binmozillamozilla-binoperakonquerorepiphany

It can probably be enhanced by some checks, but here you are:

void MainFrame::ExecuteURL(const wxString &link){

// variable declarations wxArrayString browsers; wxPathList path_list; bool BrowserWasFound = false; unsigned int i = 0; wxString path;

// Add directories to wxPathList's search path from PATH environment variablepath_list.AddEnvList(wxT("PATH"));

// Add browsers filenames. First item = most prioritybrowsers.Add(wxT("firefox"));browsers.Add(wxT("firefox-bin"));browsers.Add(wxT("mozilla"));browsers.Add(wxT("mozilla-bin"));browsers.Add(wxT("opera"));browsers.Add(wxT("konqueror"));browsers.Add(wxT("epiphany"));

for (i = 0; i < browsers.GetCount(); i++) { path = path_list.FindAbsoluteValidPath(browsers[i]); if (path.IsEmpty()) {  continue; } else {  BrowserWasFound = true;  break; }}

browsers.Clear();

if (BrowserWasFound) { path += wxT(" "); path += link; ::wxExecute(path);} else { wxMessageBox(wxT("No browser has been found."),MessageBoxHeader);}}

Hope it gets to be useful!

Artículos relacionados:

Si te gustó este articulo, ¿ Porque no dejas un comentario a continuación y continuas la conversación, o te suscribes a los feeds y recibes los artículos directamente en tu lector?

Comentarios

OK men it works…

OK men i tested it….and it works.

Leave a comment

(required)

(required)


Aquellos que leyeron este artículo luego leyeron lo siguiente: