00001
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include "precomp.h"
00028 #include "CCLicenseLocationWnd.h"
00029
00030 #include "PngImage.h"
00031 #include "globals.h"
00032 #include "debug.h"
00033 #include "resource.h"
00034 #include <windowsx.h>
00035
00037 ATOM CCLicenseLocationWnd::m_aClass = 0;
00039 int CCLicenseLocationWnd::m_nCount = 0;
00040
00041
00045 CCLicenseLocationWnd::~CCLicenseLocationWnd()
00046 {
00047
00048 UnregisterWindowClassNow();
00049
00050 if (m_hBmp != NULL)
00051 ::DeleteObject(m_hBmp);
00052 }
00053
00061 BOOL CCLicenseLocationWnd::CreateWnd(LPCTSTR lpName, DWORD dwStyle, const RECT& rect, HWND hParent)
00062 {
00063
00064 if (!RegisterWindowClass())
00065 return FALSE;
00066
00067
00068 HWND hWnd = ::CreateWindow((LPCTSTR)m_aClass, lpName, dwStyle, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, hParent, NULL, ghInstance, (void*)this);
00069 if (hWnd == NULL)
00070 {
00071
00072 UnregisterWindowClass();
00073 return FALSE;
00074 }
00075
00076
00077 ASSERT (hWnd == m_hWnd);
00078 return TRUE;
00079 }
00080
00084 bool CCLicenseLocationWnd::RegisterWindowClass()
00085 {
00086
00087 if (m_aClass != 0)
00088 {
00089
00090 m_nCount++;
00091 return true;
00092 }
00093
00094
00095 if (m_nCount != 0)
00096 return false;
00097
00098
00099 WNDCLASS info;
00100 info.style = CS_DBLCLKS;
00101 info.lpfnWndProc = WndProc;
00102 info.cbClsExtra = 0;
00103 info.cbWndExtra = 0;
00104 info.hInstance = ghInstance;
00105 info.hIcon = NULL;
00106 info.hCursor = LoadCursor(NULL, IDC_ARROW);
00107 info.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
00108 info.lpszMenuName = NULL;
00109 info.lpszClassName = _T("CCLicenseLocationWnd");
00110 m_aClass = ::RegisterClass(&info);
00111 if (m_aClass == 0)
00112 return false;
00113
00114
00115 m_nCount = 1;
00116 return true;
00117 }
00118
00122 void CCLicenseLocationWnd::UnregisterWindowClass()
00123 {
00124
00125 if (m_nCount == 0)
00126 return;
00127
00128
00129 m_nCount--;
00130 }
00131
00135 void CCLicenseLocationWnd::UnregisterWindowClassNow()
00136 {
00137
00138 if (m_nCount > 0)
00139 return;
00140
00141
00142 if (!::UnregisterClass((LPCTSTR)m_aClass, ghInstance))
00143 return;
00144
00145 m_aClass = 0;
00146 }
00147
00155 LRESULT CALLBACK CCLicenseLocationWnd::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
00156 {
00157 CCLicenseLocationWnd* pThis = NULL;
00158 if (uMsg == WM_CREATE)
00159 {
00160
00161 CREATESTRUCT* pStruct = (CREATESTRUCT*)lParam;
00162 pThis = (CCLicenseLocationWnd*)pStruct->lpCreateParams;
00163
00164
00165 ASSERT(pThis->m_hWnd == NULL);
00166
00167
00168 pThis->m_hWnd = hWnd;
00169 SetWindowLong(hWnd, GWLP_USERDATA, (LONG)pThis);
00170 }
00171 else
00172
00173 pThis = (CCLicenseLocationWnd*)GetWindowLong(hWnd, GWLP_USERDATA);
00174
00175 if (pThis == NULL)
00176
00177 return DefWindowProc(hWnd, uMsg, wParam, lParam);
00178
00179
00180 LRESULT lRet = pThis->ProcessMessage(uMsg, wParam, lParam);
00181
00182 if (uMsg == WM_NCDESTROY)
00183 {
00184
00185 pThis->m_hWnd = NULL;
00186 pThis->PostDestroy();
00187 }
00188
00189 return lRet;
00190 }
00191
00198 LRESULT CCLicenseLocationWnd::ProcessMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
00199 {
00200 switch (uMsg)
00201 {
00202 case WM_CREATE:
00203 {
00204
00205 CREATESTRUCT* pStruct = (CREATESTRUCT*)lParam;
00206 double dRatio = ((double)pStruct->cx) / pStruct->cy;
00207 double dPageRatio = ((double)m_szPage.cx) / m_szPage.cy;
00208 if (dRatio < dPageRatio)
00209 {
00210
00211 m_rectPageView.left = 5;
00212 m_rectPageView.right = pStruct->cx - 5;
00213 int nHeight = (m_szPage.cy * (m_rectPageView.right - m_rectPageView.left)) / m_szPage.cx;
00214 m_rectPageView.top = (pStruct->cy - nHeight) / 2;
00215 m_rectPageView.bottom = pStruct->cy - m_rectPageView.top;
00216 }
00217 else
00218 {
00219
00220 m_rectPageView.top = 5;
00221 m_rectPageView.bottom = pStruct->cy - 5;
00222 int nWidth = (m_szPage.cx * (m_rectPageView.bottom - m_rectPageView.top)) / m_szPage.cy;
00223 m_rectPageView.left = (pStruct->cx - nWidth) / 2;
00224 m_rectPageView.right = pStruct->cx - m_rectPageView.left;
00225 }
00226
00227
00228 PngImage png;
00229 if (png.LoadFromResource(IDPNG_SOMERIGHTS, true, ghInstance))
00230 {
00231 HDC hDC = ::GetDC(m_hWnd);
00232 m_szBitmap.cx = png.GetWidth();
00233 m_szBitmap.cy = png.GetHeight();
00234 m_hBmp = png.ToBitmap(hDC);
00235 ::ReleaseDC(m_hWnd, hDC);
00236 }
00237
00238
00239 CalcLicenseViewLocation();
00240 break;
00241 }
00242 case WM_PAINT:
00243 {
00244
00245
00246 RECT rect;
00247 if (!GetUpdateRect(m_hWnd, &rect, FALSE))
00248 break;
00249
00250
00251 PAINTSTRUCT ps;
00252 HDC hDC = BeginPaint(m_hWnd, &ps);
00253 int nSaveDC = ::SaveDC(hDC);
00254
00255
00256 ::SelectObject(hDC, GetStockObject(BLACK_PEN));
00257 ::SelectObject(hDC, GetStockObject(WHITE_BRUSH));
00258 ::Rectangle(hDC, m_rectPageView.left, m_rectPageView.top, m_rectPageView.right, m_rectPageView.bottom);
00259
00260
00261 BOOL bDrawn = FALSE;
00262 if (m_hBmp != NULL)
00263 {
00264 HDC hBmpDC = ::CreateCompatibleDC(hDC);
00265 if (hBmpDC != NULL)
00266 {
00267 HGDIOBJ hOldBmp = ::SelectObject(hBmpDC, m_hBmp);
00268 bDrawn = ::StretchBlt(hDC, m_rectLicenseView.left, m_rectLicenseView.top, m_rectLicenseView.right - m_rectLicenseView.left, m_rectLicenseView.bottom - m_rectLicenseView.top, hBmpDC, 0, 0, m_szBitmap.cx, m_szBitmap.cy, SRCCOPY);
00269 ::SelectObject(hBmpDC, hOldBmp);
00270 ::DeleteDC(hBmpDC);
00271 }
00272 }
00273 if (!bDrawn)
00274 ::Rectangle(hDC, m_rectLicenseView.left, m_rectLicenseView.top, m_rectLicenseView.right, m_rectLicenseView.bottom);
00275
00276
00277 ::SelectObject(hDC, GetStockObject(NULL_PEN));
00278 ::SelectObject(hDC, GetStockObject(GRAY_BRUSH));
00279 ::Rectangle(hDC, m_rectPageView.right, m_rectPageView.top + 3, m_rectPageView.right + 5, m_rectPageView.bottom + 5);
00280 ::Rectangle(hDC, m_rectPageView.left + 3, m_rectPageView.bottom, m_rectPageView.right + 5, m_rectPageView.bottom + 5);
00281
00282
00283 ::RestoreDC(hDC, nSaveDC);
00284 EndPaint(m_hWnd, &ps);
00285 return 0;
00286 }
00287
00288 case WM_LBUTTONDOWN:
00289 {
00290
00291 if (m_bDragging)
00292
00293 break;
00294
00295
00296 POINT pt;
00297 pt.x = GET_X_LPARAM(lParam);
00298 pt.y = GET_Y_LPARAM(lParam);
00299 if (!PtInRect(&m_rectLicenseView, pt))
00300
00301 break;
00302
00303
00304 m_szOffset.cx = m_rectLicenseView.left - pt.x;
00305 m_szOffset.cy = m_rectLicenseView.top - pt.y;
00306
00307
00308 SetCapture(m_hWnd);
00309 m_bDragging = true;
00310 return 0;
00311 }
00312
00313 case WM_MOUSEMOVE:
00314
00315
00316 if (m_bDragging)
00317 {
00318
00319 POINT pt;
00320 pt.x = GET_X_LPARAM(lParam);
00321 pt.y = GET_Y_LPARAM(lParam);
00322 MoveLicense(pt);
00323 return 0;
00324 }
00325 break;
00326
00327 case WM_LBUTTONUP:
00328
00329
00330 if (m_bDragging)
00331 {
00332
00333 POINT pt;
00334 pt.x = GET_X_LPARAM(lParam);
00335 pt.y = GET_Y_LPARAM(lParam);
00336 MoveLicense(pt);
00337
00338
00339 ReleaseCapture();
00340 m_bDragging = false;
00341 return 0;
00342 }
00343 break;
00344
00345 case WM_NCDESTROY:
00346
00347 UnregisterWindowClass();
00348 break;
00349 }
00350
00351
00352 return DefWindowProc(m_hWnd, uMsg, wParam, lParam);
00353 }
00354
00358 void CCLicenseLocationWnd::CalcLicenseViewLocation()
00359 {
00360
00361 m_rectLicenseView.left = m_rectPageView.left + (((m_rectPageView.right - m_rectPageView.left) * m_rectLocation.left) / m_szPage.cx);
00362 m_rectLicenseView.right = m_rectPageView.left + (((m_rectPageView.right - m_rectPageView.left) * m_rectLocation.right) / m_szPage.cx);
00363 m_rectLicenseView.top = m_rectPageView.top + (((m_rectPageView.bottom - m_rectPageView.top) * m_rectLocation.top) / m_szPage.cy);
00364 m_rectLicenseView.bottom = m_rectPageView.top + (((m_rectPageView.bottom - m_rectPageView.top) * m_rectLocation.bottom) / m_szPage.cy);
00365 }
00366
00370 void CCLicenseLocationWnd::MoveLicense(POINT pt)
00371 {
00372
00373 RECT rect;
00374 ::CopyRect(&rect, &m_rectLicenseView);
00375 ::OffsetRect(&rect, pt.x + m_szOffset.cx - m_rectLicenseView.left, pt.y + m_szOffset.cy - m_rectLicenseView.top);
00376
00377
00378 if (rect.top < m_rectPageView.top)
00379 ::OffsetRect(&rect, 0, m_rectPageView.top - rect.top);
00380 else if (rect.bottom > m_rectPageView.bottom)
00381 ::OffsetRect(&rect, 0, m_rectPageView.bottom - rect.bottom);
00382 if (rect.left < m_rectPageView.left)
00383 ::OffsetRect(&rect, m_rectPageView.left - rect.left, 0);
00384 else if (rect.right > m_rectPageView.right)
00385 ::OffsetRect(&rect, m_rectPageView.right - rect.right, 0);
00386
00387
00388 if (::EqualRect(&rect, &m_rectLicenseView))
00389
00390 return;
00391
00392
00393 RECT rectInvalid;
00394 UnionRect(&rectInvalid, &rect, &m_rectLicenseView);
00395 InvalidateRect(m_hWnd, &rectInvalid, FALSE);
00396
00397 ::CopyRect(&m_rectLicenseView, &rect);
00398
00399
00400 ::OffsetRect(&m_rectLocation, (((m_rectLicenseView.left - m_rectPageView.left) * m_szPage.cx) / (m_rectPageView.right - m_rectPageView.left)) - m_rectLocation.left, (((m_rectLicenseView.top - m_rectPageView.top) * m_szPage.cy) / (m_rectPageView.bottom - m_rectPageView.top)) - m_rectLocation.top);
00401
00402 SendLicenseLocationMsg();
00403 }
00404
00408 void CCLicenseLocationWnd::SendLicenseLocationMsg()
00409 {
00410
00411 DWORD dw = MAKEWPARAM(m_rectLocation.left, m_rectLocation.top);
00412 ::PostMessage(GetParent(m_hWnd), UM_LICENSEMOVED, 0, dw);
00413 }
00414
00418 void CCLicenseLocationWnd::SetXLocation(UINT uX)
00419 {
00420 uX = XfromPercent(uX);
00421
00422 if ((long)uX > m_szPage.cx - (m_rectLocation.right - m_rectLocation.left))
00423 uX = m_szPage.cx - (m_rectLocation.right - m_rectLocation.left);
00424
00425 ::OffsetRect(&m_rectLocation, uX - m_rectLocation.left, 0);
00426
00427 UpdateLocation();
00428 }
00429
00433 void CCLicenseLocationWnd::SetYLocation(UINT uY)
00434 {
00435 uY = YfromPercent(uY);
00436
00437 if ((long)uY > m_szPage.cy - (m_rectLocation.bottom - m_rectLocation.top))
00438 uY = m_szPage.cy - (m_rectLocation.bottom - m_rectLocation.top);
00439
00440 ::OffsetRect(&m_rectLocation, 0, uY - m_rectLocation.top);
00441
00442 UpdateLocation();
00443 }
00444
00445
00449 void CCLicenseLocationWnd::UpdateLocation()
00450 {
00451
00452 RECT rect;
00453 ::CopyRect(&rect, &m_rectLicenseView);
00454
00455 CalcLicenseViewLocation();
00456
00457
00458 if (!::EqualRect(&rect, &m_rectLicenseView))
00459 {
00460
00461 RECT rectInvalid;
00462 UnionRect(&rectInvalid, &rect, &m_rectLicenseView);
00463 InvalidateRect(m_hWnd, &rectInvalid, FALSE);
00464 }
00465
00466
00467 SendLicenseLocationMsg();
00468 }
00470
00471 LONG round (double f)
00472 {
00473 return (LONG)(f+0.5);
00474 }
00475
00477 LONG CCLicenseLocationWnd::XtoPercent (LONG val) const
00478 {
00479 LONG range = m_szPage.cx - (m_rectLocation.right-m_rectLocation.left);
00480 return round(100.0*val/range);
00481 }
00483
00484 LONG CCLicenseLocationWnd::XfromPercent (LONG val) const
00485 {
00486 LONG range = m_szPage.cx - (m_rectLocation.right-m_rectLocation.left);
00487 return round(1.0*range*val/100.0);
00488 }
00490
00491 LONG CCLicenseLocationWnd::YtoPercent (LONG val) const
00492 {
00493 LONG range = m_szPage.cy - (m_rectLocation.bottom-m_rectLocation.top);
00494 return round(100.0*val/range);
00495 }
00497
00498 LONG CCLicenseLocationWnd::YfromPercent (LONG val) const
00499 {
00500 LONG range = m_szPage.cy - (m_rectLocation.bottom-m_rectLocation.top);
00501 return round(1.0*range*val/100.0);
00502 }