Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1309 lines
33 KiB

  1. /**************************************************/
  2. /* */
  3. /* */
  4. /* MDI Child Window( Reference) */
  5. /* */
  6. /* */
  7. /* Copyright (c) 1997-1999 Microsoft Corporation. */
  8. /**************************************************/
  9. #include "stdafx.h"
  10. #include "eudcedit.h"
  11. #include "refrwnd.h"
  12. #include "editwnd.h"
  13. #include "mainfrm.h"
  14. #define FREELIAIS 1000
  15. LOGFONT ReffLogFont;
  16. CBitmap DupBmp;
  17. CRect DupRect;
  18. #ifdef _DEBUG
  19. #undef THIS_FILE
  20. static char BASED_CODE THIS_FILE[] = __FILE__;
  21. #endif
  22. extern CEditWnd *pEditChild;
  23. CMenu NEAR CRefrWnd::menu;
  24. IMPLEMENT_DYNCREATE(CRefrWnd, CMDIChildWnd)
  25. BEGIN_MESSAGE_MAP(CRefrWnd, CMDIChildWnd)
  26. //{{AFX_MSG_MAP(CRefrWnd)
  27. ON_BN_CLICKED( IDB_CLOSE_REF, OnClickClose)
  28. ON_WM_PAINT()
  29. ON_WM_CREATE()
  30. ON_WM_SIZE()
  31. ON_WM_LBUTTONDOWN()
  32. ON_WM_LBUTTONUP()
  33. ON_WM_MOUSEMOVE()
  34. ON_COMMAND(ID_GAIJI_COPY, OnGaijiCopy)
  35. ON_UPDATE_COMMAND_UI(ID_GAIJI_COPY, OnUpdateGaijiCopy)
  36. ON_WM_SETCURSOR()
  37. ON_WM_MDIACTIVATE()
  38. ON_WM_KEYDOWN()
  39. //}}AFX_MSG_MAP
  40. END_MESSAGE_MAP()
  41. /****************************************/
  42. /* */
  43. /* Create reference Window */
  44. /* */
  45. /****************************************/
  46. BOOL
  47. CRefrWnd::Create(
  48. LPCTSTR szTitle,
  49. LONG Style,
  50. RECT ReffWndRect,
  51. CMDIFrameWnd* Parent)
  52. {
  53. // Set Mainframe menu for reference
  54. if( menu.m_hMenu == NULL)
  55. menu.LoadMenu( IDR_MAINFRAME);
  56. m_hMenuShared = menu.m_hMenu;
  57. // Register WindowClass
  58. const TCHAR *pszReffWndClass =
  59. AfxRegisterWndClass( CS_BYTEALIGNCLIENT,
  60. NULL, (HBRUSH)(COLOR_WINDOW + 1), NULL);
  61. return CMDIChildWnd::Create( pszReffWndClass,
  62. szTitle, Style, ReffWndRect, Parent);
  63. }
  64. /****************************************/
  65. /* */
  66. /* Process before window create */
  67. /* */
  68. /****************************************/
  69. int
  70. CRefrWnd::OnCreate(
  71. LPCREATESTRUCT lpCreateStruct)
  72. {
  73. if( CMDIChildWnd::OnCreate( lpCreateStruct) == -1)
  74. return -1;
  75. // Check if m_hWnd is mirrored then we need to set the colse button on the other side.
  76. m_bCloseOnLeft = (BOOL)(GetWindowLongPtr(m_hWnd, GWL_EXSTYLE) & WS_EX_LAYOUTRTL);
  77. // Then turn off miroring if any.
  78. if (m_bCloseOnLeft) {
  79. ModifyStyleEx( WS_EX_LAYOUTRTL, 0);
  80. }
  81. if( !CreateNewBitmap())
  82. return -1;
  83. if( !ClipPickValueInit())
  84. return -1;
  85. if( !InitSelectLogfont())
  86. return -1;
  87. if( !LoadCloseBitmap())
  88. return -1;
  89. return 0;
  90. }
  91. /****************************************/
  92. /* */
  93. /* Create New Bitmap */
  94. /* */
  95. /****************************************/
  96. BOOL
  97. CRefrWnd::CreateNewBitmap()
  98. {
  99. WORD wSize;
  100. HANDLE BmpHdl;
  101. BYTE *pBmp;
  102. CClientDC dc( this);
  103. if( !ImageDC.CreateCompatibleDC( &dc))
  104. return FALSE;
  105. wSize = (WORD)((( BITMAP_WIDTH +15) /16) *2) *(WORD)BITMAP_HEIGHT;
  106. if (wSize == 0)
  107. {
  108. return (FALSE);
  109. }
  110. if(( BmpHdl = LocalAlloc( LMEM_MOVEABLE, wSize)) == 0)
  111. return FALSE;
  112. if(( pBmp = (BYTE *)LocalLock( BmpHdl)) == NULL){
  113. LocalUnlock( BmpHdl);
  114. LocalFree( BmpHdl);
  115. return FALSE;
  116. }
  117. memset( pBmp, 0xffff, wSize);
  118. if( !ImageBmp.CreateBitmap( BITMAP_WIDTH, BITMAP_HEIGHT,
  119. 1, 1, (LPSTR)pBmp)){
  120. LocalUnlock( BmpHdl);
  121. LocalFree( BmpHdl);
  122. return FALSE;
  123. }
  124. LocalUnlock( BmpHdl);
  125. LocalFree( BmpHdl);
  126. ImageDC.SelectObject( &ImageBmp);
  127. return TRUE;
  128. }
  129. /****************************************/
  130. /* */
  131. /* Initialize clipboard format */
  132. /* */
  133. /****************************************/
  134. BOOL
  135. CRefrWnd::ClipPickValueInit()
  136. {
  137. if( !( ClipboardFormat = RegisterClipboardFormat(TEXT("EudcEdit"))))
  138. return FALSE;
  139. else return TRUE;
  140. }
  141. /****************************************/
  142. /* */
  143. /* Initialize selected logfont */
  144. /* */
  145. /****************************************/
  146. BOOL
  147. CRefrWnd::InitSelectLogfont()
  148. {
  149. CFont cFont;
  150. #ifdef BUILD_ON_WINNT
  151. cFont.CreateStockObject(DEFAULT_GUI_FONT);
  152. #else
  153. cFont.CreateStockObject( SYSTEM_FONT);
  154. #endif
  155. cFont.GetObject( sizeof(LOGFONT), &ReffLogFont);
  156. cFont.DeleteObject();
  157. return TRUE;
  158. }
  159. /****************************************/
  160. /* */
  161. /* Load Close Bitmap (to close) */
  162. /* */
  163. /****************************************/
  164. BOOL
  165. CRefrWnd::LoadCloseBitmap()
  166. {
  167. CRect rect;
  168. this->GetClientRect( &rect);
  169. rect.top = 2;
  170. rect.bottom = CAPTION_HEIGHT - 2;
  171. rect.right -= 2;
  172. rect.left = rect.right - ( rect.bottom - rect.top) - 5;
  173. if( !CloseBtm.Create( NULL, WS_CHILD | WS_VISIBLE | BS_OWNERDRAW |
  174. WS_EX_TOPMOST | BS_PUSHBUTTON, rect, this, IDB_CLOSE_REF) ||
  175. !CloseBtm.LoadBitmaps( IDB_CLOSEBMP, IDB_CLOSEBMPP))
  176. return FALSE;
  177. else{
  178. CloseBtm.SizeToContent();
  179. return TRUE;
  180. }
  181. }
  182. /****************************************/
  183. /* */
  184. /* Default Constructor */
  185. /* */
  186. /****************************************/
  187. CRefrWnd::CRefrWnd()
  188. {
  189. RectVisible = FALSE;
  190. IsCapture = FALSE;
  191. SelectItems = RECTCLIP;
  192. GridShow = TRUE;
  193. bFocus = FALSE;
  194. m_bCloseOnLeft = FALSE;
  195. }
  196. /****************************************/
  197. /* */
  198. /* Destructor */
  199. /* */
  200. /****************************************/
  201. CRefrWnd::~CRefrWnd()
  202. {
  203. if( ImageBmp.Detach() != NULL)
  204. ImageBmp.DeleteObject();
  205. if( ImageDC.Detach() != NULL)
  206. ImageDC.DeleteDC();
  207. menu.DestroyMenu();
  208. }
  209. /****************************************/
  210. /* */
  211. /* MESSAGE "WM_PAINT" */
  212. /* */
  213. /****************************************/
  214. void
  215. CRefrWnd::OnPaint()
  216. {
  217. CString WndCaption;
  218. CFont RefferFont;
  219. BYTE sWork[10];
  220. CSize CharSize;
  221. short xOffset, yOffset;
  222. int Length;
  223. CPaintDC dc( this);
  224. this->GetClientRect( &ReffWndRect);
  225. ZoomRate = ReffWndRect.right /BITMAP_WIDTH;
  226. ReffLogFont.lfHeight = BITMAP_HEIGHT;
  227. ReffLogFont.lfWidth = 0;
  228. ReffLogFont.lfQuality = PROOF_QUALITY;
  229. if( !RefferFont.CreateFontIndirect( &ReffLogFont))
  230. return;
  231. CFont *OldFont = ImageDC.SelectObject( &RefferFont);
  232. if( !ReferCode)
  233. Length = 0;
  234. /*
  235. else if( !(ReferCode & 0xff00)){
  236. // SBCS
  237. sWork[0] = (BYTE)( ReferCode & 0x00ff);
  238. sWork[1] = (BYTE)'\0';
  239. Length = 1;
  240. }else{
  241. // DBCS
  242. sWork[0] = (BYTE)(( ReferCode & 0xff00) >> 8);
  243. sWork[1] = (BYTE)( ReferCode & 0x00ff);
  244. sWork[2] = (BYTE)'\0';
  245. Length = 2;
  246. }
  247. */
  248. else
  249. {
  250. sWork[0] = LOBYTE(ReferCode);
  251. sWork[1] = HIBYTE(ReferCode);
  252. Length = 1;
  253. }
  254. if( Length){
  255. CRect TextImage;
  256. GetTextExtentPoint32W( ImageDC.GetSafeHdc(), (const unsigned short *)sWork,
  257. Length, &CharSize);
  258. /*
  259. GetTextExtentPoint32A( ImageDC.GetSafeHdc(), (LPCSTR)sWork,
  260. Length, &CharSize);*/
  261. TextImage.SetRect( 0, 0, BITMAP_WIDTH, BITMAP_HEIGHT);
  262. if( CharSize.cx < BITMAP_WIDTH)
  263. xOffset = (short)(( BITMAP_HEIGHT - CharSize.cx) /2);
  264. else xOffset = 0;
  265. if( CharSize.cy < BITMAP_HEIGHT)
  266. yOffset = (short)(( BITMAP_WIDTH - CharSize.cy) /2);
  267. else yOffset = 0;
  268. /*
  269. if( ReffLogFont.lfFaceName[0] == '@' && Length == 2)
  270. xOffset = yOffset = 0;
  271. */
  272. if( ReffLogFont.lfFaceName[0] == '@' && Length == 1)
  273. xOffset = yOffset = 0;
  274. /*
  275. ExtTextOutA(ImageDC.GetSafeHdc(), xOffset, yOffset, ETO_OPAQUE,
  276. &TextImage, (LPCSTR)sWork, Length, NULL);
  277. */
  278. ExtTextOutW(ImageDC.GetSafeHdc(), xOffset, yOffset, ETO_OPAQUE,
  279. &TextImage, (const unsigned short *)sWork, Length, NULL);
  280. }
  281. dc.StretchBlt( 0, CAPTION_HEIGHT, ReffWndRect.Width(),
  282. ReffWndRect.Height() - CAPTION_HEIGHT,
  283. &ImageDC, 0, 0, BITMAP_WIDTH, BITMAP_HEIGHT,SRCCOPY);
  284. CRect rect;
  285. rect.CopyRect( &ReffWndRect);
  286. rect.top = 2;
  287. rect.bottom = CAPTION_HEIGHT - 2;
  288. if (m_bCloseOnLeft) {
  289. rect.left += 2;
  290. rect.right = rect.left + ( rect.bottom - rect.top) - 2;
  291. } else {
  292. rect.right -= 2;
  293. rect.left = rect.right - ( rect.bottom - rect.top) - 2;
  294. }
  295. CloseBtm.SetWindowPos( NULL, rect.left, rect.top,
  296. 0, 0, SWP_NOSIZE );
  297. CaptionDraw();
  298. if( RectVisible)
  299. RubberBandPaint( &dc);
  300. if( GridShow && ZoomRate >= 2)
  301. DrawGridLine( &dc);
  302. ImageDC.SelectObject( OldFont);
  303. RefferFont.DeleteObject();
  304. return;
  305. }
  306. /****************************************/
  307. /* */
  308. /* Draw Caption */
  309. /* */
  310. /****************************************/
  311. void
  312. CRefrWnd::CaptionDraw()
  313. {
  314. COLORREF TextColor;
  315. CString WndCaption;
  316. CRect CaptionRect;
  317. CBrush CaptionBrush;
  318. CFont *OldFont;
  319. int BkMode;
  320. CDC dc;
  321. dc.Attach( ::GetDC( this->GetSafeHwnd()));
  322. // Get brush with active caption color
  323. CaptionRect.CopyRect( &ReffWndRect);
  324. if (bFocus)
  325. {
  326. CaptionBrush.CreateSolidBrush(::GetSysColor( COLOR_ACTIVECAPTION));
  327. }
  328. else
  329. {
  330. CaptionBrush.CreateSolidBrush(::GetSysColor(COLOR_INACTIVECAPTION));
  331. }
  332. CaptionRect.bottom = CAPTION_HEIGHT;
  333. dc.FillRect( &CaptionRect, &CaptionBrush);
  334. CaptionBrush.DeleteObject();
  335. // Get font to draw caption
  336. #ifdef BUILD_ON_WINNT
  337. OldFont = (CFont *)dc.SelectStockObject(DEFAULT_GUI_FONT);
  338. #else
  339. OldFont = (CFont *)dc.SelectStockObject(SYSTEM_FONT);
  340. #endif
  341. BkMode = dc.SetBkMode( TRANSPARENT);
  342. if (bFocus)
  343. {
  344. TextColor = dc.SetTextColor( ::GetSysColor(COLOR_CAPTIONTEXT));
  345. }
  346. else
  347. {
  348. TextColor = dc.SetTextColor( ::GetSysColor(COLOR_INACTIVECAPTIONTEXT));
  349. }
  350. WndCaption.LoadString( IDS_REFERENCE_STR);
  351. dc.TextOut( ReffWndRect.right /2 - 30, 1, WndCaption);
  352. dc.SelectObject( OldFont);
  353. dc.SetTextColor( TextColor);
  354. dc.SetBkMode( BkMode);
  355. //
  356. // redraw the close button.
  357. //
  358. CloseBtm.Invalidate(FALSE);
  359. ::ReleaseDC( NULL, dc.Detach());
  360. return;
  361. }
  362. /****************************************/
  363. /* */
  364. /* Draw Grid */
  365. /* */
  366. /****************************************/
  367. void
  368. CRefrWnd::DrawGridLine(
  369. CDC *dc)
  370. {
  371. CPen GlyphPen;
  372. register int i;
  373. if (!dc)
  374. {
  375. return;
  376. }
  377. // Create pen to draw grid
  378. GlyphPen.CreatePen( PS_SOLID, 1, COLOR_GRID);
  379. CPen *OldPen = dc->SelectObject( &GlyphPen);
  380. // Draw grid
  381. for( i = ZoomRate - 1; i < ReffWndRect.right; i += ZoomRate){
  382. dc->MoveTo( i, CAPTION_HEIGHT-1);
  383. dc->LineTo( i, ReffWndRect.bottom);
  384. }
  385. for( i =ZoomRate +CAPTION_HEIGHT -1;i<ReffWndRect.bottom;i += ZoomRate){
  386. dc->MoveTo( 0, i);
  387. dc->LineTo( ReffWndRect.right, i);
  388. }
  389. dc->SelectObject( OldPen);
  390. GlyphPen.DeleteObject();
  391. }
  392. /****************************************/
  393. /* */
  394. /* Draw RubberBand */
  395. /* */
  396. /****************************************/
  397. void
  398. CRefrWnd::RubberBandPaint(
  399. CDC *dc)
  400. {
  401. CPen *OldPen;
  402. CBrush *OldBrush;
  403. int OldMode;
  404. if (!dc)
  405. {
  406. return;
  407. }
  408. OldPen = (CPen *)dc->SelectStockObject( BLACK_PEN);
  409. OldBrush = (CBrush *)dc->SelectStockObject( NULL_BRUSH);
  410. OldMode = dc->SetROP2( R2_NOTXORPEN);
  411. dc->Rectangle( &MoveRect);
  412. dc->SelectObject( OldPen);
  413. dc->SelectObject( OldBrush);
  414. dc->SetROP2( OldMode);
  415. }
  416. /****************************************/
  417. /* */
  418. /* MESSAGE "WM_LBUTTONDOWN" */
  419. /* */
  420. /****************************************/
  421. void
  422. CRefrWnd::OnLButtonDown(
  423. UINT ,
  424. CPoint point)
  425. {
  426. CRect CaptionRect;
  427. CaptionRect.CopyRect( &ReffWndRect);
  428. CaptionRect.top = CAPTION_HEIGHT;
  429. if( !CaptionRect.PtInRect( point))
  430. return;
  431. IsCapture = TRUE;
  432. this->SetCapture();
  433. CorrectMouseDownPt( point);
  434. if( RectVisible){
  435. if( MoveRect.PtInRect( point)){
  436. RectVisible = FALSE;
  437. this->InvalidateRect( &MoveRect, FALSE);
  438. this->UpdateWindow();
  439. RectVisible = TRUE;
  440. m_ptMouse.x = point.x - MoveRect.left;
  441. m_ptMouse.y = point.y - MoveRect.top;
  442. this->ClientToScreen( &point);
  443. MoveRectangle( point);
  444. m_ptLast = point;
  445. return;
  446. }else{
  447. RectVisible = FALSE;
  448. this->InvalidateRect( &MoveRect, FALSE);
  449. this->UpdateWindow();
  450. }
  451. }
  452. if( SelectItems == RECTCLIP)
  453. RubberBand( TRUE);
  454. else{
  455. CPoint Sp;
  456. Sp.x = ptStart.x;
  457. Sp.y = ptStart.y + CAPTION_HEIGHT;
  458. m_pointArray.RemoveAll();
  459. m_selectArray.RemoveAll();
  460. m_pointArray.Add( Sp);
  461. Sp.x = ptStart.x /ZoomRate;
  462. Sp.y = ptStart.y /ZoomRate;
  463. m_selectArray.Add( Sp);
  464. }
  465. }
  466. /****************************************/
  467. /* */
  468. /* MESSAGE "WM_MOUSEMOVE" */
  469. /* */
  470. /****************************************/
  471. void
  472. CRefrWnd::OnMouseMove(
  473. UINT ,
  474. CPoint point)
  475. {
  476. if( IsCapture){
  477. CorrectMouseUpPt( point);
  478. if( ptEnd.x == ptPrev.x && ptEnd.y == ptPrev.y)
  479. return;
  480. if( RectVisible){
  481. this->ClientToScreen( &point);
  482. MoveRectangle( m_ptLast);
  483. m_ptLast = point;
  484. MoveRectangle( m_ptLast);
  485. }else{
  486. if( SelectItems == RECTCLIP)
  487. RubberBand( FALSE);
  488. else{
  489. DrawFreeForm( FALSE);
  490. SelectFreeForm( FALSE);
  491. }
  492. }
  493. }
  494. }
  495. /****************************************/
  496. /* */
  497. /* MESSAGE "WM_LBUTTONUP" */
  498. /* */
  499. /****************************************/
  500. void
  501. CRefrWnd::OnLButtonUp(
  502. UINT ,
  503. CPoint point)
  504. {
  505. CRect WorkRect;
  506. int wSize;
  507. char *pDupBmp;
  508. if (!bFocus)
  509. {
  510. bFocus = TRUE;
  511. CaptionDraw();
  512. pEditChild->bFocus = FALSE;
  513. pEditChild->CaptionDraw();
  514. }
  515. if( IsCapture){
  516. CorrectMouseUpPt( point);
  517. if( RectVisible){
  518. MoveRectangle( m_ptLast);
  519. WorkRect.CopyRect( &MoveRect);
  520. WorkRect.left = ptStart.x - MoveRect.left;
  521. WorkRect.top = ptStart.y - MoveRect.top+CAPTION_HEIGHT;
  522. WorkRect.right = MoveRect.Width();
  523. WorkRect.bottom = MoveRect.Height();
  524. this->ClientToScreen( &point);
  525. DupBmp.CreateBitmap( BITMAP_WIDTH, BITMAP_HEIGHT,
  526. 1, 1, NULL);
  527. wSize = ((( BITMAP_WIDTH +15) /16) *2) *BITMAP_HEIGHT;
  528. pDupBmp = (char *)malloc( wSize);
  529. if( SelectItems == FREEFORM){
  530. CBitmap hStdBitmap;
  531. CDC hStdDC;
  532. CBrush BlackBrush;
  533. hStdDC.CreateCompatibleDC( &ImageDC);
  534. hStdBitmap.CreateCompatibleBitmap( &ImageDC,
  535. BITMAP_WIDTH, BITMAP_HEIGHT);
  536. CBitmap *hOldSObj =
  537. hStdDC.SelectObject( &hStdBitmap);
  538. hStdDC.PatBlt( 0, 0, BITMAP_WIDTH,
  539. BITMAP_HEIGHT, WHITENESS);
  540. BlackBrush.CreateStockObject( BLACK_BRUSH);
  541. hStdDC.FillRgn( &PickRgn, &BlackBrush);
  542. BlackBrush.DeleteObject();
  543. hStdDC.BitBlt( 0, 0, BITMAP_WIDTH,
  544. BITMAP_HEIGHT, &ImageDC,0, 0, SRCPAINT);
  545. hStdBitmap.GetBitmapBits( wSize,
  546. (LPVOID)pDupBmp);
  547. hStdDC.SelectObject( &hOldSObj);
  548. hStdBitmap.DeleteObject();
  549. hStdDC.DeleteDC();
  550. }else{
  551. ImageBmp.GetBitmapBits( wSize, (LPVOID)pDupBmp);
  552. }
  553. DupBmp.SetBitmapBits( wSize, (LPVOID)pDupBmp);
  554. free( pDupBmp);
  555. DupRect.CopyRect( &MoveRect);
  556. AfxGetMainWnd()->SendMessage( WM_DUPLICATE,
  557. (WPARAM)&WorkRect,(LPARAM)&point);
  558. DupBmp.DeleteObject();
  559. }else if( SelectItems == RECTCLIP){
  560. IllegalRect( &ptStart, &ptEnd);
  561. MoveRect.SetRect( ptStart.x, ptStart.y + CAPTION_HEIGHT,
  562. ptEnd.x + ZoomRate + 1,
  563. ptEnd.y + ZoomRate + CAPTION_HEIGHT + 1);
  564. if( abs( ptEnd.x - ptStart.x) < ZoomRate*2 ||
  565. abs( ptEnd.y - ptStart.y) < ZoomRate*2){
  566. this->InvalidateRect( &MoveRect, FALSE);
  567. this->UpdateWindow();
  568. }else{
  569. RectVisible = TRUE;
  570. this->InvalidateRect( &MoveRect, FALSE);
  571. this->UpdateWindow();
  572. }
  573. PickRect.SetRect(ptStart.x/ZoomRate, ptStart.y/ZoomRate,
  574. ( ptEnd.x+ZoomRate)/ZoomRate,
  575. ( ptEnd.y+ZoomRate)/ZoomRate);
  576. }else{
  577. CPoint nArray[FREELIAIS];
  578. CPoint pArray[FREELIAIS];
  579. DrawFreeForm( FALSE);
  580. SelectFreeForm( FALSE);
  581. DrawFreeForm( TRUE);
  582. SelectFreeForm( TRUE);
  583. if( m_pointArray.GetSize() >= FREELIAIS ||
  584. m_selectArray.GetSize() >= FREELIAIS ){
  585. IsCapture = FALSE;
  586. ReleaseCapture();
  587. this->Invalidate( FALSE);
  588. this->UpdateWindow();
  589. return;
  590. }
  591. for( int i = 0; i < m_pointArray.GetSize(); i++)
  592. nArray[i] = m_pointArray[i];
  593. for( int k = 0; k < m_selectArray.GetSize(); k++)
  594. pArray[k] = m_selectArray[k];
  595. if( FreeRgn.GetSafeHandle() != NULL)
  596. FreeRgn.DeleteObject();
  597. if( PickRgn.GetSafeHandle() != NULL)
  598. PickRgn.DeleteObject();
  599. FreeRgn.CreatePolygonRgn( nArray,
  600. (int)(m_pointArray.GetSize()), ALTERNATE);
  601. PickRgn.CreatePolygonRgn( pArray,
  602. (int)(m_selectArray.GetSize()), ALTERNATE);
  603. if( FreeRgn.GetSafeHandle() == NULL ||
  604. PickRgn.GetSafeHandle() == NULL ){
  605. m_pointArray.RemoveAll();
  606. m_selectArray.RemoveAll();
  607. }else{
  608. RectVisible = TRUE;
  609. FreeRgn.GetRgnBox( &MoveRect);
  610. PickRgn.GetRgnBox( &PickRect);
  611. if( PickRect.Width() < 3 ||
  612. PickRect.Height() < 3){
  613. RectVisible = FALSE;
  614. FreeRgn.DeleteObject();
  615. PickRgn.DeleteObject();
  616. }
  617. MoveRect.right += 1;
  618. MoveRect.bottom += 1;
  619. this->InvalidateRect( &MoveRect);
  620. this->UpdateWindow();
  621. }
  622. }
  623. IsCapture = FALSE;
  624. }
  625. ReleaseCapture();
  626. }
  627. /****************************************/
  628. /* */
  629. /* Correct Mouse Down Point */
  630. /* */
  631. /****************************************/
  632. void
  633. CRefrWnd::CorrectMouseDownPt(
  634. CPoint point)
  635. {
  636. CRect WorkRect;
  637. WorkRect.CopyRect( &ReffWndRect);
  638. ptStart.x = point.x;
  639. ptStart.y = point.y - CAPTION_HEIGHT;
  640. if( ptStart.y < 0)
  641. ptStart.y = 0;
  642. if( SelectItems == RECTCLIP){
  643. ptStart.x = ( ptStart.x /ZoomRate) *ZoomRate;
  644. ptStart.y = ( ptStart.y /ZoomRate) *ZoomRate;
  645. }else{
  646. ptStart.x = (( ptStart.x + ZoomRate/2) /ZoomRate) *ZoomRate;
  647. ptStart.y = (( ptStart.y + ZoomRate/2) /ZoomRate) *ZoomRate;
  648. }
  649. ptEnd = ptPrev = ptStart;
  650. }
  651. /****************************************/
  652. /* */
  653. /* Correct Mouse Up point */
  654. /* */
  655. /****************************************/
  656. void
  657. CRefrWnd::CorrectMouseUpPt(
  658. CPoint point)
  659. {
  660. ptPrev = ptEnd;
  661. ptEnd.x = point.x;
  662. ptEnd.y = point.y - CAPTION_HEIGHT;
  663. CRect WorkRect;
  664. WorkRect.CopyRect( &ReffWndRect);
  665. if( ptEnd.x < 0) ptEnd.x = 0;
  666. if( ptEnd.y < 0) ptEnd.y = 0;
  667. if( ptEnd.x > WorkRect.right){
  668. if( SelectItems == RECTCLIP)
  669. ptEnd.x = WorkRect.right - ZoomRate;
  670. else ptEnd.x = WorkRect.right;
  671. }
  672. if( ptEnd.y > WorkRect.bottom - CAPTION_HEIGHT){
  673. if( SelectItems == RECTCLIP){
  674. ptEnd.y = WorkRect.bottom - CAPTION_HEIGHT
  675. - ZoomRate;
  676. }else ptEnd.y = WorkRect.bottom - CAPTION_HEIGHT;
  677. }
  678. if( SelectItems == RECTCLIP){
  679. ptEnd.x = ( ptEnd.x /ZoomRate) *ZoomRate;
  680. ptEnd.y = ( ptEnd.y /ZoomRate) *ZoomRate;
  681. }else{
  682. ptEnd.x = (( ptEnd.x + ZoomRate/2) /ZoomRate) *ZoomRate;
  683. ptEnd.y = (( ptEnd.y + ZoomRate/2) /ZoomRate) *ZoomRate;
  684. }
  685. if( SelectItems == RECTCLIP){
  686. if( ptEnd.x - ptStart.x <= ZoomRate &&
  687. ptEnd.x - ptStart.x >= 0)
  688. ptEnd.x = ptStart.x + ZoomRate;
  689. if( ptStart.x - ptEnd.x <= ZoomRate &&
  690. ptStart.x - ptEnd.x > 0)
  691. ptEnd.x = ptStart.x - ZoomRate;
  692. if( ptStart.y - ptEnd.y <= ZoomRate &&
  693. ptStart.y - ptEnd.y > 0)
  694. ptEnd.y = ptStart.y - ZoomRate;
  695. if( ptEnd.y - ptStart.y <= ZoomRate &&
  696. ptEnd.y - ptStart.y >= 0)
  697. ptEnd.y = ptStart.y + ZoomRate;
  698. }
  699. }
  700. /****************************************/
  701. /* */
  702. /* Correct Illegal rectangle */
  703. /* */
  704. /****************************************/
  705. void
  706. CRefrWnd::IllegalRect(
  707. PPOINT ptTL,
  708. PPOINT ptBR)
  709. {
  710. int Tmp;
  711. if( ptTL->x > ptBR->x){
  712. Tmp = ptTL->x;
  713. ptTL->x = ptBR->x;
  714. ptBR->x = Tmp;
  715. }
  716. if( ptTL->y > ptBR->y){
  717. Tmp = ptTL->y;
  718. ptTL->y = ptBR->y;
  719. ptBR->y = Tmp;
  720. }
  721. }
  722. /****************************************/
  723. /* */
  724. /* COMMAND "COPY" */
  725. /* */
  726. /****************************************/
  727. void
  728. CRefrWnd::OnGaijiCopy()
  729. {
  730. ClipImageCopy();
  731. CMainFrame *pMain = new CMainFrame;
  732. pMain->CustomActivate();
  733. delete pMain;
  734. }
  735. /************************************************/
  736. /* */
  737. /* COMMAND "COPY" (Update) */
  738. /* */
  739. /************************************************/
  740. void
  741. CRefrWnd::OnUpdateGaijiCopy(
  742. CCmdUI *pCmdUI)
  743. {
  744. if( RectVisible)
  745. pCmdUI->Enable(TRUE);
  746. else pCmdUI->Enable(FALSE);
  747. }
  748. /****************************************/
  749. /* */
  750. /* Copy Bitmap data */
  751. /* */
  752. /****************************************/
  753. BOOL
  754. CRefrWnd::ClipImageCopy()
  755. {
  756. CBitmap hStdBitmap;
  757. CDC hStdDC;
  758. hStdDC.CreateCompatibleDC( &ImageDC);
  759. hStdBitmap.CreateCompatibleBitmap( &ImageDC,
  760. PickRect.Width(), PickRect.Height());
  761. CBitmap *hOldSObj = hStdDC.SelectObject( &hStdBitmap);
  762. hStdDC.PatBlt( 0, 0, PickRect.Width(), PickRect.Height(), WHITENESS);
  763. if( SelectItems == FREEFORM){
  764. CBrush BlackBrush;
  765. BlackBrush.CreateStockObject( BLACK_BRUSH);
  766. PickRgn.OffsetRgn( 0 - PickRect.left, 0 - PickRect.top);
  767. hStdDC.FillRgn( &PickRgn, &BlackBrush);
  768. BlackBrush.DeleteObject();
  769. hStdDC.BitBlt( 0, 0, PickRect.Width(), PickRect.Height(),
  770. &ImageDC, PickRect.left, PickRect.top, SRCPAINT);
  771. }else{
  772. hStdDC.BitBlt( 0, 0, PickRect.Width(), PickRect.Height(),
  773. &ImageDC, PickRect.left, PickRect.top, SRCCOPY);
  774. }
  775. if (!this->OpenClipboard()) {
  776. hStdDC.SelectObject( hOldSObj);
  777. hStdBitmap.DeleteObject();
  778. hStdDC.DeleteDC();
  779. return FALSE;
  780. }
  781. EmptyClipboard();
  782. if( !SetClipboardData( CF_BITMAP, hStdBitmap.Detach())) {
  783. hStdDC.SelectObject( hOldSObj);
  784. hStdBitmap.DeleteObject();
  785. hStdDC.DeleteDC();
  786. CloseClipboard();
  787. return FALSE;
  788. }
  789. CloseClipboard();
  790. hStdDC.SelectObject( hOldSObj);
  791. hStdBitmap.DeleteObject();
  792. hStdDC.DeleteDC();
  793. RectVisible = FALSE;
  794. this->InvalidateRect( &MoveRect, FALSE);
  795. this->UpdateWindow();
  796. return TRUE;
  797. }
  798. /****************************************/
  799. /* */
  800. /* Draw Rubber Band rectanble */
  801. /* */
  802. /****************************************/
  803. void
  804. CRefrWnd::RubberBand(
  805. BOOL TestFlag)
  806. {
  807. CPen *OldPen;
  808. CBrush *OldBrush;
  809. CPoint ptTL;
  810. CPoint ptBR;
  811. CClientDC dc( this);
  812. OldPen = (CPen *)dc.SelectStockObject( BLACK_PEN);
  813. OldBrush = (CBrush *)dc.SelectStockObject( NULL_BRUSH);
  814. int OldMode = dc.SetROP2( R2_NOTXORPEN);
  815. CRect BRect;
  816. if( !TestFlag){
  817. ptTL.x = ptStart.x;
  818. ptTL.y = ptStart.y + CAPTION_HEIGHT;
  819. ptBR.x = ptPrev.x;
  820. ptBR.y = ptPrev.y + CAPTION_HEIGHT;
  821. IllegalRect( &ptTL, &ptBR);
  822. BRect.SetRect( ptTL.x, ptTL.y,
  823. ptBR.x + ZoomRate +1, ptBR.y + ZoomRate +1);
  824. dc.Rectangle( &BRect);
  825. }
  826. ptTL.x = ptStart.x;
  827. ptTL.y = ptStart.y + CAPTION_HEIGHT;
  828. ptBR.x = ptEnd.x;
  829. ptBR.y = ptEnd.y + CAPTION_HEIGHT;
  830. IllegalRect( &ptTL, &ptBR);
  831. ptPrev = ptBR;
  832. BRect.SetRect( ptTL.x, ptTL.y, ptBR.x + ZoomRate +1,ptBR.y+ZoomRate+1);
  833. dc.Rectangle( &BRect);
  834. dc.SelectObject( OldPen);
  835. dc.SelectObject( OldBrush);
  836. dc.SetROP2( OldMode);
  837. }
  838. /****************************************/
  839. /* */
  840. /* Initialize bitmap data */
  841. /* */
  842. /****************************************/
  843. BOOL
  844. CRefrWnd::UpdateBitmap()
  845. {
  846. WORD wSize;
  847. HANDLE BitHandle;
  848. BYTE *pBitmap;
  849. wSize = (WORD)((( BITMAP_WIDTH + 15) /16) *2) *(WORD)BITMAP_HEIGHT;
  850. if(( BitHandle = LocalAlloc( LMEM_MOVEABLE, wSize)) == 0)
  851. return FALSE;
  852. if(( pBitmap = (BYTE *)LocalLock( BitHandle)) == NULL){
  853. LocalUnlock( BitHandle);
  854. LocalFree( BitHandle);
  855. return FALSE;
  856. }
  857. memset( pBitmap, 0xffff, wSize);
  858. ImageBmp.SetBitmapBits((DWORD)wSize, (const void far *)pBitmap);
  859. LocalUnlock( BitHandle);
  860. LocalFree( BitHandle);
  861. RectVisible = FALSE;
  862. this->Invalidate(FALSE);
  863. this->UpdateWindow();
  864. return TRUE;
  865. }
  866. /****************************************/
  867. /* */
  868. /* MESSAGE "WM_SETCURSOR" */
  869. /* */
  870. /****************************************/
  871. BOOL
  872. CRefrWnd::OnSetCursor(
  873. CWnd* pWnd,
  874. UINT nHitTest,
  875. UINT message)
  876. {
  877. CPoint point;
  878. CRect CaptionRect;
  879. HCURSOR hArrowCur;
  880. GetCursorPos( &point);
  881. this->GetClientRect( &CaptionRect);
  882. this->ScreenToClient( &point);
  883. CaptionRect.top = CAPTION_HEIGHT;
  884. if( CaptionRect.PtInRect( point))
  885. if( MoveRect.PtInRect( point) && RectVisible)
  886. ::SetCursor((HCURSOR)ArrowCursor[ALLDIRECT]);
  887. else ::SetCursor((HCURSOR)ToolCursor[SelectItems]);
  888. else{
  889. hArrowCur = AfxGetApp()->LoadStandardCursor(IDC_ARROW);
  890. ::SetCursor( hArrowCur);
  891. }
  892. return TRUE;
  893. }
  894. /****************************************/
  895. /* */
  896. /* MESSAGE "WM_MDIACTIVATE" */
  897. /* */
  898. /****************************************/
  899. void
  900. CRefrWnd::OnMDIActivate(
  901. BOOL bActivate,
  902. CWnd* pActivateWnd,
  903. CWnd* pDeactivateWnd)
  904. {
  905. if( bActivate == FALSE){
  906. RectVisible = FALSE;
  907. this->InvalidateRect( &MoveRect, FALSE);
  908. this->UpdateWindow();
  909. }
  910. }
  911. /****************************************/
  912. /* */
  913. /* MESSAGE "WM_SIZE" */
  914. /* */
  915. /****************************************/
  916. void
  917. CRefrWnd::OnSize(
  918. UINT nType,
  919. int cx,
  920. int cy)
  921. {
  922. int NewZoomRate;
  923. NewZoomRate = cx / BITMAP_WIDTH;
  924. if( RectVisible && NewZoomRate > 1){
  925. MoveRect.left = ( MoveRect.left /ZoomRate) * NewZoomRate;
  926. MoveRect.top = ((( MoveRect.top - CAPTION_HEIGHT)
  927. / ZoomRate) * NewZoomRate) + CAPTION_HEIGHT;
  928. MoveRect.right = ( MoveRect.right /ZoomRate) * NewZoomRate + 1;
  929. MoveRect.bottom = ((( MoveRect.bottom -CAPTION_HEIGHT)
  930. / ZoomRate) * NewZoomRate) + CAPTION_HEIGHT + 1;
  931. }else if( RectVisible && NewZoomRate <= 1){
  932. RectVisible = FALSE;
  933. }
  934. CMDIChildWnd::OnSize(nType, cx, cy);
  935. this->Invalidate(FALSE);
  936. this->UpdateWindow();
  937. }
  938. /****************************************/
  939. /* */
  940. /* Select FreeForm */
  941. /* */
  942. /****************************************/
  943. void
  944. CRefrWnd::SelectFreeForm(
  945. BOOL MouseSts)
  946. {
  947. CPoint Ep, Sp, Cp;
  948. CPoint Fp, Inc;
  949. CPoint Dp, Err;
  950. BOOL Slope;
  951. int D;
  952. int Tmp;
  953. if( !MouseSts){
  954. Sp.x = ptPrev.x /ZoomRate;
  955. Sp.y = ptPrev.y /ZoomRate;
  956. Ep.x = Fp.x = ptEnd.x /ZoomRate;
  957. Ep.y = Fp.y = ptEnd.y /ZoomRate;
  958. }else{
  959. Sp.x = ptEnd.x /ZoomRate;
  960. Sp.y = ptEnd.y /ZoomRate;
  961. Ep.x = Fp.x = ptStart.x /ZoomRate;
  962. Ep.y = Fp.y = ptStart.y /ZoomRate;
  963. }
  964. if( Fp.x >= Sp.x)
  965. Inc.x = 1;
  966. else Inc.x = -1;
  967. if( Fp.y >= Sp.y)
  968. Inc.y = 1;
  969. else Inc.y = -1;
  970. Dp.x = ( Fp.x - Sp.x)*Inc.x;
  971. Dp.y = ( Fp.y - Sp.y)*Inc.y;
  972. if( !Dp.x && !Dp.y)
  973. return;
  974. if( Dp.x < Dp.y){
  975. Tmp = Dp.y;
  976. Dp.y = Dp.x;
  977. Dp.x = Tmp;
  978. Tmp = Inc.x;
  979. Inc.x = Inc.y;
  980. Inc.y = Tmp;
  981. Slope = TRUE;
  982. }else Slope = FALSE;
  983. Err.x = Dp.y * 2;
  984. Err.y = ( Dp.y - Dp.x) * 2;
  985. D = Err.x - Dp.x;
  986. Ep = Sp;
  987. while(1){
  988. m_selectArray.Add( Ep);
  989. if( Ep.x == Fp.x && Ep.y == Fp.y)
  990. break;
  991. if( Slope){
  992. Tmp = Ep.x;
  993. Ep.x = Ep.y;
  994. Ep.y = Tmp;
  995. }
  996. Ep.x += Inc.x;
  997. if( D < 0)
  998. D += Err.x;
  999. else{
  1000. Ep.y += Inc.y;
  1001. D += Err.y;
  1002. }
  1003. if( Slope){
  1004. Tmp = Ep.x;
  1005. Ep.x = Ep.y;
  1006. Ep.y = Tmp;
  1007. }
  1008. }
  1009. }
  1010. /****************************************/
  1011. /* */
  1012. /* Draw FreeForm */
  1013. /* */
  1014. /****************************************/
  1015. void
  1016. CRefrWnd::DrawFreeForm(
  1017. BOOL MouseSts)
  1018. {
  1019. CPoint Ep, Sp, Cp;
  1020. CPoint Fp, Inc;
  1021. CPoint Dp, Err;
  1022. CPoint P1, P2;
  1023. BOOL Slope;
  1024. int D;
  1025. int Tmp;
  1026. CClientDC dc( this);
  1027. CPen *OldPen = (CPen *)dc.SelectStockObject( BLACK_PEN);
  1028. int OldMode = dc.SetROP2( R2_NOTXORPEN);
  1029. if( !MouseSts){
  1030. Sp.x = ptPrev.x;
  1031. Sp.y = ptPrev.y + CAPTION_HEIGHT;
  1032. Ep.x = Fp.x = ptEnd.x;
  1033. Ep.y = Fp.y = ptEnd.y + CAPTION_HEIGHT;
  1034. }else{
  1035. Sp.x = ptEnd.x;
  1036. Sp.y = ptEnd.y + CAPTION_HEIGHT;
  1037. Ep.x = Fp.x = ptStart.x;
  1038. Ep.y = Fp.y = ptStart.y + CAPTION_HEIGHT;
  1039. }
  1040. if( Fp.x >= Sp.x)
  1041. Inc.x = ZoomRate;
  1042. else Inc.x = 0 - ZoomRate;
  1043. if( Fp.y >= Sp.y)
  1044. Inc.y = ZoomRate;
  1045. else Inc.y = 0 - ZoomRate;
  1046. Dp.x = ( Fp.x - Sp.x)*Inc.x;
  1047. Dp.y = ( Fp.y - Sp.y)*Inc.y;
  1048. if( !Dp.x && !Dp.y)
  1049. return;
  1050. if( Dp.x < Dp.y){
  1051. Tmp = Dp.y;
  1052. Dp.y = Dp.x;
  1053. Dp.x = Tmp;
  1054. Tmp = Inc.x;
  1055. Inc.x = Inc.y;
  1056. Inc.y = Tmp;
  1057. Slope = TRUE;
  1058. }else Slope = FALSE;
  1059. Err.x = Dp.y * 2;
  1060. Err.y = ( Dp.y - Dp.x) * 2;
  1061. D = Err.x - Dp.x;
  1062. Ep = Sp;
  1063. dc.MoveTo( Sp);
  1064. while(1){
  1065. if( Sp.x != Ep.x && Sp.y != Ep.y){
  1066. if( Sp.y < Ep.y && Sp.x > Ep.x){
  1067. Cp.x = Sp.x;
  1068. Cp.y = Ep.y;
  1069. }else if( Sp.y < Ep.y && Sp.x < Ep.x){
  1070. Cp.x = Sp.x;
  1071. Cp.y = Ep.y;
  1072. }else if( Sp.y > Ep.y && Sp.x > Ep.x){
  1073. Cp.y = Sp.y;
  1074. Cp.x = Ep.x;
  1075. }else{
  1076. Cp.y = Sp.y;
  1077. Cp.x = Ep.x;
  1078. }
  1079. dc.LineTo( Cp);
  1080. dc.LineTo( Ep);
  1081. P1 = Cp;
  1082. P2 = Ep;
  1083. m_pointArray.Add( P1);
  1084. m_pointArray.Add( P2);
  1085. }else if( Sp.x != Ep.x || Sp.y != Ep.y){
  1086. dc.LineTo( Ep);
  1087. P1 = Ep;
  1088. m_pointArray.Add( P1);
  1089. }
  1090. Sp.x = Ep.x;
  1091. Sp.y = Ep.y;
  1092. if( Ep.x == Fp.x && Ep.y == Fp.y)
  1093. break;
  1094. if( Slope){
  1095. Tmp = Ep.x;
  1096. Ep.x = Ep.y;
  1097. Ep.y = Tmp;
  1098. }
  1099. Ep.x += Inc.x;
  1100. if( D < 0)
  1101. D += Err.x;
  1102. else{
  1103. Ep.y += Inc.y;
  1104. D += Err.y;
  1105. }
  1106. if( Slope){
  1107. Tmp = Ep.x;
  1108. Ep.x = Ep.y;
  1109. Ep.y = Tmp;
  1110. }
  1111. }
  1112. dc.SelectObject( OldPen);
  1113. dc.SetROP2( OldMode);
  1114. }
  1115. /****************************************/
  1116. /* */
  1117. /* Move Rectangle */
  1118. /* */
  1119. /****************************************/
  1120. void
  1121. CRefrWnd::MoveRectangle(
  1122. CPoint point)
  1123. {
  1124. CDC dc;
  1125. dc.Attach( ::GetDC( NULL));
  1126. dc.PatBlt( point.x - m_ptMouse.x, point.y - m_ptMouse.y,
  1127. MoveRect.Width(), 2, PATINVERT);
  1128. dc.PatBlt( point.x - m_ptMouse.x + MoveRect.Width(),
  1129. point.y - m_ptMouse.y, 2, MoveRect.Height(), PATINVERT);
  1130. dc.PatBlt( point.x - m_ptMouse.x, point.y - m_ptMouse.y
  1131. + MoveRect.Height(), MoveRect.Width() + 2, 2, PATINVERT);
  1132. dc.PatBlt( point.x - m_ptMouse.x, point.y - m_ptMouse.y + 2, 2,
  1133. MoveRect.Height() - 2, PATINVERT);
  1134. ::ReleaseDC( NULL, dc.Detach());
  1135. }
  1136. /****************************************/
  1137. /* */
  1138. /* COMMAND "Close Ref" */
  1139. /* */
  1140. /****************************************/
  1141. void
  1142. CRefrWnd::OnClickClose()
  1143. {
  1144. AfxGetMainWnd()->SendMessage( WM_COMMAND, ID_REFFER_CLOSE, 0);
  1145. }
  1146. /****************************************/
  1147. /* */
  1148. /* MESSAGE "WM_KEYDOWN" */
  1149. /* */
  1150. /****************************************/
  1151. void
  1152. CRefrWnd::OnKeyDown(
  1153. UINT nChar,
  1154. UINT nRepCnt,
  1155. UINT nFlags)
  1156. {
  1157. if( nChar == VK_ESCAPE){
  1158. if( RectVisible){
  1159. RectVisible = FALSE;
  1160. this->Invalidate(FALSE);
  1161. this->UpdateWindow();
  1162. }
  1163. }else CMDIChildWnd::OnKeyDown(nChar, nRepCnt, nFlags);
  1164. }