00001 
00002 
00004 
00005 #include "stdafx.h"
00006 #include "GridURLCell.h"
00007 #include "../GridCtrl_src/GridCtrl.h"
00008 
00009 IMPLEMENT_DYNCREATE(CGridURLCell, CGridCell)
00010 
00011 #ifndef _WIN32_WCE
00012 HCURSOR CGridURLCell::g_hLinkCursor = NULL;
00013 #endif
00014 
00015 
00016 URLStruct CGridURLCell::g_szURIprefixes[] = { 
00017     { _T("www."),    _tcslen(_T("www."))    },
00018     { _T("http:"),   _tcslen(_T("http:"))   },
00019     { _T("mailto:"), _tcslen(_T("mailto:")) },
00020     { _T("ftp:"),    _tcslen(_T("ftp:"))    },
00021     { _T("https:"),  _tcslen(_T("https:"))  },
00022     { _T("news:"),   _tcslen(_T("news:"))   },
00023     { _T("gopher:"), _tcslen(_T("gopher:")) },
00024     { _T("telnet:"), _tcslen(_T("telnet:")) },
00025     { _T("url:"),    _tcslen(_T("url:"))    },
00026     { _T("file:"),   _tcslen(_T("file:"))   },
00027     { _T("ftp."),    _tcslen(_T("ftp."))    }
00028 };
00029 
00031 
00033 
00034 CGridURLCell::CGridURLCell()
00035 {
00036 #ifndef _WIN32_WCE
00037     g_hLinkCursor = GetHandCursor();
00038 #endif
00039         m_bLaunchUrl = TRUE;
00040         m_clrUrl = GetSysColor(COLOR_HIGHLIGHT);
00041 }
00042 
00043 CGridURLCell::~CGridURLCell()
00044 {
00045 }
00046 
00047 BOOL CGridURLCell::Draw(CDC* pDC, int nRow, int nCol, CRect rect, BOOL bEraseBkgnd)
00048 {
00049         
00050         if (HasUrl(GetText()))
00051                 SetTextClr(m_clrUrl);
00052 
00053     
00054     m_Rect = rect;
00055 
00056     return CGridCell::Draw(pDC, nRow, nCol, rect, bEraseBkgnd);
00057 }
00058 
00059 #pragma warning(disable:4100)
00060 BOOL CGridURLCell::Edit(int nRow, int nCol, CRect rect, CPoint point, UINT nID, UINT nChar)
00061 {
00062     return FALSE;
00063 }
00064 #pragma warning(default:4100)
00065 
00066 void CGridURLCell::OnClick(CPoint PointCellRelative)
00067 {
00068 #ifndef _WIN32_WCE
00069     CString strURL;
00070     if (GetAutoLaunchUrl() && OverURL(PointCellRelative, strURL))
00071                 ShellExecute(NULL, _T("open"), strURL, NULL,NULL, SW_SHOW);
00072 #endif
00073 }
00074 
00075 
00076 BOOL CGridURLCell::OnSetCursor()
00077 {
00078 #ifndef _WIN32_WCE
00079     CString strURL;
00080     CPoint pt(GetMessagePos());
00081     GetGrid()->ScreenToClient(&pt);
00082     pt = pt - m_Rect.TopLeft();
00083 
00084     if (OverURL(pt, strURL))
00085     {
00086         SetCursor(g_hLinkCursor);
00087                 return TRUE;
00088         }
00089         else
00090 #endif
00091                 return CGridCell::OnSetCursor();
00092 }
00093 
00094 #ifndef _WIN32_WCE
00095 HCURSOR CGridURLCell::GetHandCursor()
00096 {
00097         if (g_hLinkCursor == NULL)              
00098         {
00099         
00100                 CString strWndDir;
00101                 GetWindowsDirectory(strWndDir.GetBuffer(MAX_PATH), MAX_PATH);
00102                 strWndDir.ReleaseBuffer();
00103 
00104                 strWndDir += _T("\\winhlp32.exe");
00105                 
00106                 HMODULE hModule = LoadLibrary(strWndDir);
00107                 if( hModule )
00108                 {
00109                         HCURSOR hHandCursor = ::LoadCursor(hModule, MAKEINTRESOURCE(106));
00110                         if( hHandCursor )
00111                         {
00112                                 g_hLinkCursor = CopyCursor(hHandCursor);
00113                         }
00114                 }
00115                 FreeLibrary(hModule);
00116         }
00117 
00118         return g_hLinkCursor;
00119 }
00120 #endif
00121 
00123 
00124 
00125 BOOL CGridURLCell::HasUrl(CString str)
00126 {
00127     int nNumPrefixes = sizeof(g_szURIprefixes) / sizeof(g_szURIprefixes[0]);
00128     for (int i = 0; i < nNumPrefixes; i++)
00129         
00130         if (str.Find(g_szURIprefixes[i].szURLPrefix) >= 0)
00131             return TRUE;
00132 
00133     return FALSE;
00134 }
00135 
00136 
00137 BOOL CGridURLCell::OverURL(CPoint& pt, CString& strURL)
00138 {
00139     
00140 
00141         BOOL bOverURL = FALSE;
00142         CSize size = GetTextExtent(GetText());
00143 
00144         
00145         pt.x += m_Rect.left;
00146         CPoint center = m_Rect.CenterPoint();
00147 
00148         if ((m_nFormat & DT_RIGHT) && pt.x >= (m_Rect.right - size.cx))
00149         {
00150                 bOverURL = TRUE;
00151         }       
00152         else if ((m_nFormat & DT_CENTER) && 
00153              ((center.x - (size.cx/2)) <= pt.x) && (pt.x <= (center.x + (size.cx/2))) )
00154         {
00155                 bOverURL = TRUE;
00156         }
00157         else if (pt.x <= (size.cx + m_Rect.left))
00158         {
00159                 bOverURL = TRUE;
00160         }
00161 
00162     if (!bOverURL)
00163         return FALSE;
00164 
00165     
00166         bOverURL = FALSE;
00167         strURL = GetText();
00168 
00169         
00170         float width = (float)size.cx/(float)strURL.GetLength();
00171 
00172         
00173         pt.x -= m_Rect.left;
00174         if (m_nFormat & DT_RIGHT)
00175         {
00176                 int wide = m_Rect.Width() - size.cx;
00177                 pt.x -= wide;
00178                 if (pt.x <= 0)
00179                         return FALSE;
00180         }
00181 
00182         if (m_nFormat & DT_CENTER)
00183         {
00184                 int wide = m_Rect.Width() - size.cx;
00185                 pt.x -= (wide/2);
00186                 if (pt.x <= 0 || pt.x > (size.cx + (wide/2)))
00187                         return FALSE;
00188         }
00189 
00190         
00191         int ltrs = (int)((float)pt.x/width);
00192 #if  !defined(_WIN32_WCE) || (_WIN32_WCE > 210)
00193         
00194         int endSpace = strURL.Find(_T(' '), ltrs);
00195         if (endSpace != -1)
00196                 strURL.Delete(endSpace, strURL.GetLength()-endSpace);
00197 
00198         int beginSpace = strURL.ReverseFind(_T(' '));
00199         if (beginSpace != -1)
00200                 strURL.Delete(0, ++beginSpace);
00201 #endif
00202 
00203         
00204         return HasUrl(strURL);
00205 }
00206