hyperlinkctrl.cpp

Go to the documentation of this file.
00001 
00002 // Name:        hyperlinkctrl.cpp
00003 // Purpose:     wxHyperlinkCtrl
00004 // Author:      Angelo Mandato
00005 // Created:     2005/08/11
00006 // RCS-ID:      $Id: hyperlinkctrl.cpp,v 1.1 2006/10/11 16:14:58 ndellamico Exp $
00007 // Copyright:   (c) 2001-2005 Angelo Mandato
00008 // Licence:     wxWidgets licence
00009 // Version:             1.4
00011 
00012 
00013 #ifdef __GNUG__
00014 #pragma implementation "hyperlinkctrl.h"
00015 #endif
00016 
00017 // For compilers that support precompilation, includes "wx.h".
00018 #include "wx/wxprec.h"
00019 
00020 #ifdef __BORLANDC__
00021 #pragma hdrstop
00022 #endif
00023 
00024 // includes
00025 #ifndef WX_PRECOMP
00026         // here goes the #include <wx/abc.h> directives for those
00027         // files which are not included by wxprec.h
00028   #include <wx/wx.h>
00029 #endif
00030 
00031 #include <wx/msgdlg.h>
00032 #include <wx/mimetype.h>
00033 #include <wx/app.h>
00034 #include <wx/menu.h>
00035 #include <wx/clipbrd.h>
00036 
00037 #include <wx/event.h>
00038 #include <wx/tooltip.h>
00039 
00040 #include "hyperlinkctrl.h"
00041 
00042 #ifdef __WIN32__
00043         #include <windows.h>
00044         #include <wx/msw/registry.h>
00045 #endif
00046 
00047 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LINK_CLICKED)
00048 #ifndef HYPERLINKCTRL_COMMANDEVENT
00049 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LINK_MCLICKED)
00050 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LINK_RCLICKED)
00051 #endif
00052 
00053 BEGIN_EVENT_TABLE(wxHyperlinkCtrl, wxStaticText)
00054         EVT_MOUSE_EVENTS( wxHyperlinkCtrl::OnMouseEvent)
00055         EVT_MOTION(wxHyperlinkCtrl::OnMouseEvent)
00056   EVT_MENU(HYPERLINKS_POPUP_COPY, wxHyperlinkCtrl::OnPopUpCopy )
00057 END_EVENT_TABLE()
00058 
00060 wxHyperlinkCtrl::wxHyperlinkCtrl(wxWindow *parent, wxWindowID id, const wxString &label,
00061                const wxPoint &pos, const wxSize &size, int style, const wxString& name, const wxString& szURL )
00062                            : wxStaticText(parent, id, label, pos, size, style|wxPOPUP_WINDOW, name)
00063 {
00064         if( szURL.IsEmpty() )
00065                 m_szURL = label;
00066         else
00067                 m_szURL = szURL;
00068 
00069         // Set Tooltip
00070         SetToolTip( m_szURL );
00071 
00072         // Set default properties
00073         ReportErrors(); // default: true
00074         SetUnderlines(); // default: true, true, true
00075         SetColours(); // default: blue, violet, blue
00076         SetVisited(); // default: false
00077         EnableRollover();// default: false
00078         SetBold(); // default: false
00079         SetLinkCursor(); // default: wxCURSOR_HAND
00080 
00081 //**Added By Mark McManus
00082     AutoBrowse(); // default true
00083     DoPopup(); // default true
00084 //**Mark McManus
00085 
00086   OpenInSameWindow(); // default false
00087         // Set control properties and refresh
00088         UpdateLink(true);
00089 }
00090 
00092 bool wxHyperlinkCtrl::GotoURL( const wxString &szUrl, const wxString &szBrowser, const bool bReportErrors, const bool bSameWinIfPossible )
00093 {
00094   wxLogNull logOff;
00095 
00096         // Use the browser specified
00097         if(!szBrowser.IsEmpty() )
00098         {
00099                 wxString szCmd = szBrowser;
00100                 szCmd += wxEmptyString;
00101                 szCmd += szUrl; // URLs are encoded so no need to put them in quotes
00102 
00103                 if( wxExecute( szCmd, FALSE) != 0 )
00104                         return true;
00105                 else
00106                         DisplayError(wxT("Unable to launch specified browser."), bReportErrors);
00107 
00108     return false;
00109         }
00110 
00111 #ifdef __WIN32__
00112 
00113   if( bSameWinIfPossible )
00114   {
00115           HINSTANCE result = ShellExecute(NULL, _T("open"), szUrl.c_str(), NULL,NULL, SW_SHOW);
00116 
00117           if( (unsigned int)result > HINSTANCE_ERROR )
00118                   return true;
00119 
00120           // Hack for Firefox, it returns FNF, do not know why
00121           if( (unsigned int)result == SE_ERR_FNF )
00122                   return true;
00123   }
00124 
00125         wxRegKey Regkey( wxT("HKEY_CLASSES_ROOT\\http\\shell\\open\\command") );
00126         Regkey.Open();
00127         wxString szOpenWith = Regkey;
00128 
00129         wxString szCmd = wxT("");
00130         if( szOpenWith.Find(wxT("%1")) != -1 )
00131         {
00132                 szCmd = szOpenWith;
00133                 szCmd.Replace( wxT("%1"), wxT("\"") + szUrl + wxT("\"") );
00134         }
00135         else
00136         {
00137                 // first strip the \" character from the front and parse out the string...
00138                 szOpenWith.Trim(false);
00139 
00140                 if( szOpenWith.GetChar(0) == wxT('\"') )
00141                 {
00142                         szOpenWith.Remove(0, 1);
00143                         int nEnd = szOpenWith.Find(wxT("\""));
00144                         if( nEnd > 0 )
00145                                 szOpenWith.Remove(nEnd);
00146                 }
00147                 else
00148                 {
00149                         int nEnd = szOpenWith.Find(wxT(" "));
00150                         if( nEnd > 0 )
00151                                 szOpenWith.Remove(nEnd);
00152                 }
00153                 
00154                 szCmd.Printf(wxT("\"%s\" \"%s\""), szOpenWith.c_str(), szUrl.c_str() );
00155         }
00156 
00157         if( wxExecute( szCmd, FALSE) != 0 )
00158                 return true;
00159         else
00160         {
00161                 DisplayError(wxT("Unable to launch default browser."), bReportErrors);
00162                 return false;
00163         }
00164 
00165 #else
00166         // Untested code follows
00167         wxFileType *ft = wxTheMimeTypesManager->GetFileTypeFromExtension( wxT("html") );
00168         if ( !ft )
00169         {
00170                 DisplayError(wxT("Impossible to determine the file type for extension html"), bReportErrors);
00171                 return false;
00172         }
00173 
00174         wxString cmd;
00175         bool bResult = ft->GetOpenCommand(&cmd, wxFileType::MessageParameters( szUrl, wxT("") ));
00176         delete ft;
00177 
00178         if (!bResult)
00179         {
00180                 DisplayError(wxT("Unable to retrieve command information"), bReportErrors);
00181                 return false;
00182         }
00183 
00184         bResult = (wxExecute( cmd, FALSE) != 0);
00185   if( !bResult )
00186     DisplayError(wxT("Unable to launch browser."), bReportErrors);
00187 
00188         return bResult;
00189 #endif
00190 }
00191 
00193 void wxHyperlinkCtrl::OnMouseEvent(wxMouseEvent& event)
00194 {
00195         if ( !event.Moving() )
00196   {
00197     if ( event.Entering() )
00198                 {
00199                         SetCursor( m_crHand ); //wxCursor( wxCURSOR_HAND ) );
00200 
00201                         if( m_bEnableRollover )
00202                         {
00203                                 SetForegroundColour(m_crLinkRolloverColor);
00204                                 wxFont fontTemp = GetFont();
00205                                 fontTemp.SetUnderlined( m_bRolloverUnderline );
00206                                 if( m_bBold )
00207                                         fontTemp.SetWeight(wxBOLD);
00208 
00209                                 SetFont( fontTemp );
00210                                 Refresh(true);
00211                         }
00212                 }
00213     else if ( event.Leaving() )
00214                 {
00215                         SetCursor( wxNullCursor );
00216                         if( m_bEnableRollover )
00217                                 UpdateLink(true);
00218                 }
00219     else if( !event.Moving() )
00220     {
00221       if ( event.LeftUp() ) 
00222       {
00223                                 if( m_bAutoBrowse )
00224         {
00225                                         GotoURL( m_szURL, m_szBrowserPath, m_bReportErrors, m_bSameWinIfPossible );
00226         }
00227         else
00228         {
00229 #ifdef HYPERLINKCTRL_COMMANDEVENT
00230           wxCommandEvent eventOut(wxEVT_COMMAND_LINK_CLICKED, GetId());
00231           eventOut.SetEventObject(this);
00232           GetParent()->ProcessEvent( eventOut );
00233 #else
00234           wxPoint posOnParent = GetPosition();
00235           posOnParent.x += event.GetPosition().x;
00236           posOnParent.y += event.GetPosition().y;
00237 
00238                                   wxHyperlinkEvent eventOut(wxEVT_COMMAND_LINK_CLICKED, GetId());
00239                                   eventOut.SetEventObject(this);
00240                                   eventOut.SetPosition( posOnParent );
00241           GetParent()->ProcessEvent( eventOut );
00242 #endif
00243         }
00244 
00245                                 SetVisited( true );
00246       }
00247       else if ( event.RightUp() )
00248       {
00249                                 //**Modified By Mark McManus, enhanced by Angelo Mandato
00250         if( m_bDoPopup )
00251         {
00252                                         wxMenu *menuPopUp = new wxMenu(wxT(""), wxMENU_TEAROFF);
00253                                         menuPopUp->Append(HYPERLINKS_POPUP_COPY, wxT("Copy"));
00254                                         PopupMenu( menuPopUp, wxPoint( event.m_x, event.m_y ) );
00255                                         delete menuPopUp; // ADDED 06/30/2004 (prevents memory leaks)
00256         }
00257                                 else
00258                                 {
00259 #ifndef HYPERLINKCTRL_COMMANDEVENT
00260           wxPoint posOnParent = GetPosition();
00261           posOnParent.x += event.GetPosition().x;
00262           posOnParent.y += event.GetPosition().y;
00263 
00264                                   wxHyperlinkEvent eventOut(wxEVT_COMMAND_LINK_RCLICKED, GetId());
00265                                   eventOut.SetEventObject(this);
00266                                   eventOut.SetPosition( posOnParent );
00267           GetParent()->ProcessEvent( eventOut );
00268 #endif
00269                                 }
00270         //**Mark McManus
00271       }
00272       else if( event.MiddleUp() )
00273       {
00274 #ifndef HYPERLINKCTRL_COMMANDEVENT
00275         wxPoint posOnParent = GetPosition();
00276         posOnParent.x += event.GetPosition().x;
00277         posOnParent.y += event.GetPosition().y;
00278 
00279                                 wxHyperlinkEvent eventOut(wxEVT_COMMAND_LINK_MCLICKED, GetId());
00280                                 eventOut.SetEventObject(this);
00281                                 eventOut.SetPosition( posOnParent );
00282         GetParent()->ProcessEvent( eventOut );
00283 #endif
00284       }
00285     }
00286   }
00287 
00288         event.Skip();
00289 }
00290 
00292 void wxHyperlinkCtrl::OnPopUpCopy( wxCommandEvent &event )
00293 {
00294   wxUnusedVar(event);
00295   wxTheClipboard->UsePrimarySelection();
00296 
00297   if (!wxTheClipboard->Open())
00298     return;
00299 
00300   wxTextDataObject *data = new wxTextDataObject( m_szURL );
00301   wxTheClipboard->SetData( data );
00302   wxTheClipboard->Close();
00303 }
00304 
00306 void wxHyperlinkCtrl::UpdateLink(const bool bRefresh)
00307 {
00308         wxFont fontTemp = GetFont();
00309 
00310         if( m_bVisited )
00311         {
00312                 SetForegroundColour( m_crVisitedColour );
00313                 fontTemp.SetUnderlined( m_bVisitedUnderline );
00314         }
00315         else
00316         {
00317                 SetForegroundColour( m_crLinkColour );
00318                 fontTemp.SetUnderlined( m_bLinkUnderline );
00319         }
00320 
00321         if( m_bBold )
00322                 fontTemp.SetWeight(wxBOLD);
00323 
00324         SetFont( fontTemp );
00325         Refresh( bRefresh );
00326 }
00327 
00328 #ifdef __WIN95__
00329 long wxHyperlinkCtrl::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
00330 {
00331         if (nMsg == WM_NCHITTEST)
00332                 return (long)HTCLIENT;
00333 
00334         return wxStaticText::MSWWindowProc( nMsg, wParam, lParam );
00335 }
00336 #endif
00337 
00338 void wxHyperlinkCtrl::DisplayError( const wxString &szError, const bool bReportErrors )
00339 {
00340         if( bReportErrors )
00341                 wxMessageBox( szError, wxT("Hyperlink Error"), wxOK | wxCENTRE | wxICON_ERROR  );
00342 }

Generated on Mon Mar 19 17:14:08 2007 for CAENScope by  doxygen 1.4.6-NO