~ubuntu-branches/debian/squeeze/stella/squeeze

« back to all changes in this revision

Viewing changes to src/win32/StellaX/HyperLink.cxx

  • Committer: Bazaar Package Importer
  • Author(s): Mario Iseli
  • Date: 2006-04-08 18:38:25 UTC
  • mfrom: (1.1.2 upstream) (2.1.1 etch)
  • Revision ID: james.westby@ubuntu.com-20060408183825-vu1jk57rk929derx
* New Maintainer (Closes: #361345)
* New upstream release (Closes: #349725)
* Build-Depend now on libslang2-dev (Closes: #325577)
* Complete rebuild of debian/, upgraded to policy-standards
  3.6.2 and compat-level 5.
* Removed stellarc since stella only reads ~/.stellarc and even
  works without a first config.
* New debian/watch file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
//============================================================================
2
 
//
3
 
//   SSSS    tt          lll  lll          XX     XX
4
 
//  SS  SS   tt           ll   ll           XX   XX
5
 
//  SS     tttttt  eeee   ll   ll   aaaa     XX XX
6
 
//   SSSS    tt   ee  ee  ll   ll      aa     XXX
7
 
//      SS   tt   eeeeee  ll   ll   aaaaa    XX XX
8
 
//  SS  SS   tt   ee      ll   ll  aa  aa   XX   XX
9
 
//   SSSS     ttt  eeeee llll llll  aaaaa  XX     XX
10
 
//
11
 
// Copyright (c) 1995-2000 by Jeff Miller
12
 
// Copyright (c) 2004 by Stephen Anthony
13
 
//
14
 
// See the file "license" for information on usage and redistribution of
15
 
// this file, and for a DISCLAIMER OF ALL WARRANTIES.
16
 
//
17
 
// $Id: HyperLink.cxx,v 1.2 2004/07/15 03:03:27 stephena Exp $
18
 
//============================================================================
19
 
 
20
 
#include "pch.hxx"
21
 
#include "HyperLink.hxx"
22
 
 
23
 
#include <shellapi.h>
24
 
 
25
 
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
26
 
CHyperLink::CHyperLink()
27
 
          : m_bOverControl(FALSE),
28
 
            m_bVisited(FALSE),
29
 
            m_hFont(NULL),
30
 
            m_hLinkCursor(NULL)
31
 
{
32
 
  m_crLinkColor    = RGB(  0,   0, 238);   // Blue
33
 
  m_crVisitedColor = RGB( 85,  26, 139);   // Purple
34
 
  m_crHoverColor = ::GetSysColor(COLOR_HIGHLIGHT);
35
 
 
36
 
  SetDefaultCursor();
37
 
}
38
 
 
39
 
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
40
 
CHyperLink::~CHyperLink()
41
 
{
42
 
  if (m_hFont)
43
 
  {
44
 
    DeleteObject( m_hFont );
45
 
    m_hFont = NULL;
46
 
  }
47
 
}
48
 
 
49
 
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
50
 
void CHyperLink::SetURL( LPCTSTR ctszURL )
51
 
{
52
 
  if (ctszURL == NULL)
53
 
    return;
54
 
 
55
 
  lstrcpy(m_tszURL, ctszURL);
56
 
}
57
 
 
58
 
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
59
 
void CHyperLink::PreSubclassWindow( HWND hwnd )
60
 
{
61
 
  GetWindowText( hwnd, m_tszText, MAX_HYPERLINK_TEXT_LEN );
62
 
  m_hFont = (HFONT)::SendMessage(hwnd, WM_GETFONT, 0, 0);
63
 
}
64
 
 
65
 
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
66
 
LRESULT CHyperLink::WndProc( HWND hwnd, UINT msg, WPARAM wParam,
67
 
                             LPARAM lParam, BOOL& rfHandled ) 
68
 
{
69
 
  switch (msg)
70
 
  {
71
 
    case WM_PAINT:
72
 
      rfHandled = TRUE;
73
 
      return OnPaint(hwnd);
74
 
 
75
 
    case WM_SETCURSOR:
76
 
      rfHandled = TRUE;
77
 
      return OnSetCursor(hwnd, LOWORD(lParam), HIWORD(lParam));
78
 
 
79
 
    case WM_LBUTTONUP:
80
 
      rfHandled = TRUE;
81
 
      return OnLButtonUp(hwnd, wParam, LOWORD(lParam), HIWORD(lParam));
82
 
 
83
 
    case WM_MOUSEMOVE:
84
 
      rfHandled = TRUE;
85
 
      return OnMouseMove(hwnd, wParam, LOWORD(lParam), HIWORD(lParam));
86
 
  }
87
 
 
88
 
  return 0;
89
 
}
90
 
 
91
 
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
92
 
LRESULT CHyperLink::OnPaint( HWND hwnd )
93
 
{
94
 
  PAINTSTRUCT ps;
95
 
  HDC hdc = BeginPaint(hwnd, &ps);
96
 
        
97
 
  SelectObject(hdc, m_hFont);
98
 
        
99
 
  if (m_bOverControl)
100
 
    SetTextColor(hdc, m_crHoverColor);
101
 
  else if (m_bVisited)
102
 
    SetTextColor(hdc, m_crVisitedColor);
103
 
  else
104
 
    SetTextColor(hdc, m_crLinkColor);
105
 
 
106
 
  SetBkMode( hdc, TRANSPARENT );
107
 
  TextOut( hdc, 0, 0, m_tszText, lstrlen(m_tszText) );
108
 
  EndPaint(hwnd, &ps);
109
 
 
110
 
  return 0;
111
 
}
112
 
 
113
 
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
114
 
BOOL CHyperLink::OnSetCursor( HWND hwnd, WORD nHittest, WORD wMouseMsg )
115
 
{
116
 
  UNUSED_ALWAYS( hwnd );
117
 
  UNUSED_ALWAYS( nHittest );
118
 
  UNUSED_ALWAYS( wMouseMsg );
119
 
 
120
 
  if (m_hLinkCursor)
121
 
  {
122
 
    SetCursor(m_hLinkCursor);
123
 
    return TRUE;
124
 
  }
125
 
 
126
 
  return FALSE;
127
 
}
128
 
 
129
 
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
130
 
LRESULT CHyperLink::OnLButtonUp( HWND hwnd, WPARAM fwKeys, WORD xPos, WORD yPos )
131
 
{
132
 
  UNUSED_ALWAYS( fwKeys );
133
 
  UNUSED_ALWAYS( xPos );
134
 
  UNUSED_ALWAYS( yPos );
135
 
 
136
 
  int result = (int)GotoURL(m_tszURL, SW_SHOW);
137
 
  m_bVisited = (result > HINSTANCE_ERROR);
138
 
  if (!m_bVisited) 
139
 
  {
140
 
    MessageBeep(MB_ICONEXCLAMATION);
141
 
    ReportError(result);
142
 
  } 
143
 
  else 
144
 
  {
145
 
    m_bVisited = TRUE; 
146
 
    InvalidateRect(hwnd, NULL, FALSE); 
147
 
  }
148
 
 
149
 
  return 0;
150
 
}
151
 
 
152
 
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
153
 
LRESULT CHyperLink::OnMouseMove( HWND hwnd, WPARAM fwKeys, WORD xPos, WORD yPos )
154
 
{
155
 
  UNUSED_ALWAYS( fwKeys );
156
 
 
157
 
  // call defwindowproc?
158
 
  if (m_bOverControl)
159
 
  {
160
 
    RECT rc;
161
 
    GetClientRect(hwnd, &rc);
162
 
 
163
 
    POINT pt = { xPos, yPos };
164
 
    if (!PtInRect(&rc, pt))
165
 
    {
166
 
      m_bOverControl = FALSE;
167
 
      ReleaseCapture();
168
 
      RedrawWindow(hwnd, NULL, NULL, 
169
 
        RDW_INVALIDATE | RDW_UPDATENOW | RDW_ERASE);
170
 
    }
171
 
  }
172
 
  else
173
 
  {
174
 
    m_bOverControl = TRUE;
175
 
    RedrawWindow(hwnd, NULL, NULL, 
176
 
      RDW_INVALIDATE | RDW_UPDATENOW | RDW_ERASE);
177
 
    SetCapture(hwnd);
178
 
  }
179
 
 
180
 
  return 0;
181
 
}
182
 
 
183
 
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
184
 
// The following appeared in Paul DiLascia's Jan 1998 MSJ articles.
185
 
// It loads a "hand" cursor from the winhlp32.exe module
186
 
void CHyperLink::SetDefaultCursor()
187
 
{
188
 
  if (m_hLinkCursor == NULL)                // No cursor handle - load our own
189
 
  {
190
 
    // Get the windows directory
191
 
    TCHAR tszWndDir[MAX_PATH + 1];
192
 
    GetWindowsDirectory(tszWndDir, MAX_PATH);
193
 
 
194
 
    lstrcat(tszWndDir, _T("\\winhlp32.exe"));
195
 
 
196
 
    // This retrieves cursor #106 from winhlp32.exe, which is a hand pointer
197
 
    HMODULE hModule = LoadLibrary(tszWndDir);
198
 
    if (hModule)
199
 
    {
200
 
      HCURSOR hHandCursor = ::LoadCursor(hModule, MAKEINTRESOURCE(106));
201
 
      if (hHandCursor)
202
 
        m_hLinkCursor = CopyCursor(hHandCursor);
203
 
    }
204
 
    FreeLibrary(hModule);
205
 
  }
206
 
}
207
 
 
208
 
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
209
 
LONG CHyperLink::GetRegKey( HKEY key, LPCTSTR subkey, LPTSTR retdata )
210
 
{
211
 
  HKEY hkey;
212
 
  LONG retval = RegOpenKeyEx(key, subkey, 0, KEY_QUERY_VALUE, &hkey);
213
 
 
214
 
  if (retval == ERROR_SUCCESS)
215
 
  {
216
 
    long datasize = MAX_PATH;
217
 
    TCHAR data[MAX_PATH];
218
 
    RegQueryValue(hkey, NULL, data, &datasize);
219
 
    lstrcpy(retdata,data);
220
 
    RegCloseKey(hkey);
221
 
  }
222
 
 
223
 
  return retval;
224
 
}
225
 
 
226
 
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
227
 
void CHyperLink::ReportError( int nError )
228
 
{
229
 
  TCHAR tsz[MAX_HYPERLINK_TEXT_LEN + 1];
230
 
 
231
 
  switch (nError) 
232
 
  {
233
 
    case 0:
234
 
      lstrcpy(tsz, _T("The operating system is out\nof memory or resources."));
235
 
      break;
236
 
 
237
 
    case SE_ERR_PNF:
238
 
      lstrcpy(tsz, _T("The specified path was not found."));
239
 
      break;
240
 
 
241
 
    case SE_ERR_FNF:
242
 
      lstrcpy(tsz, _T("The specified file was not found."));
243
 
      break;
244
 
 
245
 
    case ERROR_BAD_FORMAT:
246
 
      lstrcpy(tsz, _T("The .EXE file is invalid\n(non-Win32 .EXE or error in .EXE image)."));
247
 
      break;
248
 
 
249
 
    case SE_ERR_ACCESSDENIED:
250
 
      lstrcpy(tsz, _T("The operating system denied\naccess to the specified file."));
251
 
      break;
252
 
 
253
 
    case SE_ERR_ASSOCINCOMPLETE:
254
 
      lstrcpy(tsz,_T("The filename association is\nincomplete or invalid."));
255
 
      break;
256
 
 
257
 
    case SE_ERR_DDEBUSY:
258
 
      lstrcpy(tsz, _T("The DDE transaction could not\nbe completed because other DDE transactions\nwere being processed."));
259
 
      break;
260
 
 
261
 
    case SE_ERR_DDEFAIL:
262
 
      lstrcpy(tsz, _T("The DDE transaction failed."));
263
 
      break;
264
 
 
265
 
    case SE_ERR_DDETIMEOUT:
266
 
      lstrcpy(tsz, _T("The DDE transaction could not\nbe completed because the request timed out."));
267
 
      break;
268
 
 
269
 
    case SE_ERR_DLLNOTFOUND:
270
 
      lstrcpy(tsz, _T("The specified dynamic-link library was not found."));
271
 
      break;
272
 
 
273
 
    case SE_ERR_NOASSOC:
274
 
      lstrcpy(tsz, __T("There is no application associated\nwith the given filename extension."));
275
 
      break;
276
 
 
277
 
    case SE_ERR_OOM:
278
 
      lstrcpy(tsz, _T("There was not enough memory to complete the operation."));
279
 
      break;
280
 
 
281
 
    case SE_ERR_SHARE:
282
 
      lstrcpy(tsz, _T("A sharing violation occurred."));
283
 
      break;
284
 
 
285
 
    default:
286
 
      wsprintf(tsz, _T("Unknown error %d occurred."), nError); 
287
 
      break;
288
 
  }
289
 
 
290
 
  TCHAR tszCaption[MAX_HYPERLINK_TEXT_LEN + 1];
291
 
  lstrcpy(tszCaption, _T("Unable to open hyperlink"));
292
 
 
293
 
  MessageBox(NULL, tsz, tszCaption, MB_ICONEXCLAMATION | MB_OK);
294
 
}
295
 
 
296
 
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
297
 
HINSTANCE CHyperLink::GotoURL( LPCTSTR url, int showcmd )
298
 
{
299
 
  TCHAR key[MAX_PATH + MAX_PATH];
300
 
 
301
 
  // First try ShellExecute()
302
 
  HINSTANCE result = ShellExecute(NULL, _T("open"), url, NULL,NULL, showcmd);
303
 
 
304
 
  // If it failed, get the .htm regkey and lookup the program
305
 
  if ((UINT)result <= HINSTANCE_ERROR)
306
 
  {
307
 
    if (GetRegKey(HKEY_CLASSES_ROOT, _T(".htm"), key) == ERROR_SUCCESS)
308
 
    {
309
 
      lstrcat(key, _T("\\shell\\open\\command"));
310
 
 
311
 
      if (GetRegKey(HKEY_CLASSES_ROOT,key,key) == ERROR_SUCCESS)
312
 
      {
313
 
        TCHAR *pos;
314
 
        pos = _tcsstr(key, _T("\"%1\""));
315
 
        if (pos == NULL)
316
 
        {                     // No quotes found
317
 
          pos = strstr(key, _T("%1"));       // Check for %1, without quotes 
318
 
          if (pos == NULL)                   // No parameter at all...
319
 
            pos = key+lstrlen(key)-1;
320
 
          else
321
 
            *pos = '\0';                   // Remove the parameter
322
 
        }
323
 
        else
324
 
          *pos = '\0';                       // Remove the parameter
325
 
 
326
 
        lstrcat(pos, _T(" "));
327
 
        lstrcat(pos, url);
328
 
        result = (HINSTANCE) WinExec(key,showcmd);
329
 
      }
330
 
    }
331
 
  }
332
 
 
333
 
  return result;
334
 
}