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.

416 lines
14 KiB

  1. /******************************************************************************/
  2. /* T_TEXT.CPP: IMPLEMENTATION OF THE CTextTool CLASS */
  3. /* */
  4. /* */
  5. /******************************************************************************/
  6. /* */
  7. /* Methods in this file */
  8. /* */
  9. /* CText Tool Class Object */
  10. /* CTextTool::CTextTool */
  11. /* CTextTool::~CTextTool */
  12. /* CTextTool::CreateTextEditObject */
  13. /* CTextTool::PlaceTextOnBitmap */
  14. /* CTextTool::OnUpdateColors */
  15. /* CTextTool::OnCancel */
  16. /* CTextTool::OnStartDrag */
  17. /* CTextTool::OnEndDrag */
  18. /* CTextTool::OnDrag */
  19. /* CTextTool::OnClickOptions */
  20. /******************************************************************************/
  21. /* */
  22. /* This is the Text edit tool. It creates a tedit class object when the user */
  23. /* is done dragging the selection for the size desired. */
  24. /* */
  25. /* The Once a text object window exist, it is either cancelled or placed */
  26. /* according to the following rules. */
  27. /* */
  28. /* Cancel Rules */
  29. /* - During a Drag, if the user drags more than MAX_MOVE_DIST_FOR_PLACE */
  30. /* - At the End of a Drag, if the user lets up the mouse more than */
  31. /* MAX_MOVE_DIST_FOR_PLACE pixels from where they did the mosue down */
  32. /* - If the user selects anohter tool (in imgtools, select processing, see */
  33. /* CImgTool::Select()). */
  34. /* */
  35. /* Place Rules */
  36. /* - At the End of a Drag, if the user lets up the mouse less than or equal */
  37. /* to MAX_MOVE_DIST_FOR_PLACE pixels from where they did the mosue down */
  38. /* */
  39. /* Also, during the time the edit control object is visible/exists, the scroll*/
  40. /* bars are disabled. */
  41. /* */
  42. /******************************************************************************/
  43. #include "stdafx.h"
  44. #include "global.h"
  45. #include "pbrush.h"
  46. #include "pbrusdoc.h"
  47. #include "pbrusfrm.h"
  48. #include "pbrusvw.h"
  49. #include "docking.h"
  50. #include "minifwnd.h"
  51. #include "bmobject.h"
  52. #include "imgsuprt.h"
  53. #include "imgwnd.h"
  54. #include "imgbrush.h"
  55. #include "imgwell.h"
  56. #include "pictures.h"
  57. #include "tfont.h"
  58. #include "tedit.h"
  59. #include "t_Text.h"
  60. #ifdef _DEBUG
  61. #undef THIS_FILE
  62. static CHAR BASED_CODE THIS_FILE[] = __FILE__;
  63. #endif
  64. IMPLEMENT_DYNAMIC( CTextTool, CSelectTool )
  65. #include "memtrace.h"
  66. CTextTool NEAR g_TextTool;
  67. /******************************************************************************/
  68. CTextTool::CTextTool()
  69. {
  70. m_nCmdID = IDMX_TEXTTOOL;
  71. m_pCTedit = NULL;
  72. m_bIsUndoable = TRUE;
  73. m_bCanBePrevTool = FALSE;
  74. }
  75. /******************************************************************************/
  76. CTextTool::~CTextTool()
  77. {
  78. }
  79. /******************************************************************************/
  80. /* Creates the CTedit class object with the appropriate attributes and */
  81. /* dissables the scroll bars on the bitmap window */
  82. void CTextTool::CreateTextEditObject( CImgWnd* pImgWnd, MTI* pmti )
  83. {
  84. c_selectRect.SetRect( 0, 0, 0, 0 );
  85. if (pImgWnd == NULL
  86. || pImgWnd->m_pImg == NULL)
  87. return;
  88. BOOL bBackTransparent;
  89. CRect cRectTextBox;
  90. if (pmti->ptDown.x > pmti->pt.x)
  91. {
  92. cRectTextBox.left = pmti->pt.x;
  93. cRectTextBox.right = pmti->ptDown.x;
  94. }
  95. else
  96. {
  97. cRectTextBox.left = pmti->ptDown.x;
  98. cRectTextBox.right = pmti->pt.x;
  99. }
  100. if (pmti->ptDown.y > pmti->pt.y)
  101. {
  102. cRectTextBox.top = pmti->pt.y;
  103. cRectTextBox.bottom = pmti->ptDown.y;
  104. }
  105. else
  106. {
  107. cRectTextBox.top = pmti->ptDown.y;
  108. cRectTextBox.bottom = pmti->pt.y;
  109. }
  110. if (cRectTextBox.left < 0)
  111. cRectTextBox.left = 0;
  112. if (cRectTextBox.top < 0)
  113. cRectTextBox.top = 0;
  114. if (cRectTextBox.right > pImgWnd->m_pImg->cxWidth - 1)
  115. cRectTextBox.right = pImgWnd->m_pImg->cxWidth - 1;
  116. if (cRectTextBox.bottom > pImgWnd->m_pImg->cyHeight - 1)
  117. cRectTextBox.bottom = pImgWnd->m_pImg->cyHeight - 1;
  118. CRect rectImg;
  119. pImgWnd->GetClientRect( &rectImg );
  120. pImgWnd->ClientToImage( rectImg );
  121. if (cRectTextBox.left < rectImg.left)
  122. cRectTextBox.left = rectImg.left;
  123. if (cRectTextBox.top < rectImg.top )
  124. cRectTextBox.top = rectImg.top;
  125. if (cRectTextBox.right > rectImg.right)
  126. cRectTextBox.right = rectImg.right - 1;
  127. if (cRectTextBox.bottom > rectImg.bottom)
  128. cRectTextBox.bottom = rectImg.bottom -1;
  129. bBackTransparent = ! theImgBrush.m_bOpaque;
  130. pImgWnd->ImageToClient( cRectTextBox );
  131. m_pCTedit = new CTedit;
  132. if (m_pCTedit != NULL
  133. && m_pCTedit->Create( pImgWnd, crLeft, crRight, cRectTextBox, bBackTransparent ))
  134. {
  135. SetupRubber( pImgWnd->m_pImg );
  136. pImgWnd->EnableScrollBar( SB_BOTH, ESB_DISABLE_BOTH );
  137. }
  138. else
  139. {
  140. TRACE( TEXT("Create Edit Window Failed!\n") );
  141. theApp.SetMemoryEmergency();
  142. }
  143. }
  144. /******************************************************************************/
  145. /* Places the image of the text edit control on the bitmap */
  146. /* Then it deletes the text edit control, and re-enables the scroll bars */
  147. void CTextTool::PlaceTextOnBitmap( CImgWnd* pImgWnd )
  148. {
  149. if (m_pCTedit->IsModified())
  150. {
  151. CRect cRectClient;
  152. CDC* pDC = CDC::FromHandle(pImgWnd->m_pImg->hDC);
  153. m_pCTedit->GetClientRect ( &cRectClient );
  154. m_pCTedit->ClientToScreen( &cRectClient );
  155. pImgWnd->ScreenToClient ( &cRectClient );
  156. pImgWnd->ClientToImage ( cRectClient );
  157. m_pCTedit->GetBitmap( pDC, &cRectClient );
  158. InvalImgRect ( pImgWnd->m_pImg, &cRectClient );
  159. CommitImgRect( pImgWnd->m_pImg, &cRectClient );
  160. pImgWnd->FinishUndo( cRectClient );
  161. DirtyImg( pImgWnd->m_pImg );
  162. }
  163. m_pCTedit->DestroyWindow();
  164. m_pCTedit = NULL;
  165. pImgWnd->EnableScrollBar( SB_BOTH, ESB_ENABLE_BOTH );
  166. }
  167. /******************************************************************************/
  168. /* updates the foreground and background colors */
  169. void CTextTool::OnUpdateColors( CImgWnd* pImgWnd )
  170. {
  171. if (m_pCTedit != NULL)
  172. {
  173. m_pCTedit->SetTextColor( crLeft );
  174. m_pCTedit->SetBackColor( crRight );
  175. }
  176. }
  177. /******************************************************************************/
  178. void CTextTool::OnActivate( BOOL bActivate )
  179. {
  180. if (bActivate)
  181. {
  182. // Disallow activation if Zoomed.
  183. if (CImgWnd::GetCurrent()->GetZoom() > 1 )
  184. {
  185. ::MessageBeep( MB_ICONASTERISK );
  186. SelectPrevious();
  187. }
  188. }
  189. else
  190. {
  191. if (CWnd::GetCapture() != CImgWnd::c_pImgWndCur && m_pCTedit != NULL &&
  192. IsWindow(m_pCTedit->m_hWnd) )
  193. {
  194. CAttrEdit* pEdit = m_pCTedit->GetEditWindow();
  195. if (pEdit != NULL && IsWindow(pEdit->m_hWnd) && pEdit->GetWindowTextLength() > 0)
  196. PlaceTextOnBitmap( CImgWnd::c_pImgWndCur );
  197. else
  198. {
  199. m_pCTedit->DestroyWindow();
  200. m_pCTedit = NULL;
  201. InvalImgRect( CImgWnd::c_pImgWndCur->m_pImg, NULL ); // redraw selection
  202. CImgWnd::c_pImgWndCur->EnableScrollBar( SB_BOTH, ESB_ENABLE_BOTH );
  203. }
  204. }
  205. }
  206. CImgTool::OnActivate( bActivate );
  207. }
  208. /******************************************************************************/
  209. /* Deletes the text edit control, and refreshes the bitmap display, while */
  210. /* also re-enabling the scroll bars */
  211. void CTextTool::OnCancel(CImgWnd* pImgWnd)
  212. {
  213. if (m_pCTedit != NULL)
  214. {
  215. m_pCTedit->DestroyWindow();
  216. m_pCTedit = NULL;
  217. }
  218. InvalImgRect( pImgWnd->m_pImg, NULL ); // redraw selection
  219. pImgWnd->EnableScrollBar( SB_BOTH, ESB_ENABLE_BOTH );
  220. CImgTool::OnCancel( pImgWnd );
  221. }
  222. /******************************************************************************/
  223. void CTextTool::OnStartDrag( CImgWnd* pImgWnd, MTI* pmti )
  224. {
  225. CImgTool::OnStartDrag( pImgWnd, pmti );
  226. OnDrag( pImgWnd, pmti );
  227. }
  228. /******************************************************************************/
  229. /* if a text edit object does not exist, it creates one here. If one does */
  230. /* exist, it checks the distance between the point down and point up. If */
  231. /* less than or equal to MAX_MOVE_DIST_FOR_PLACE it places the bitmap, else */
  232. /* it assumes the user wants to abort the prior text editing session, and */
  233. /* destroys the prior text edit control and creates a new one with the newly */
  234. /* created dragged coordinate box (ptdown and ptup). */
  235. void CTextTool::OnEndDrag( CImgWnd* pImgWnd, MTI* pmti )
  236. {
  237. CSize cPtDownUpDistance = pmti->ptDown - pmti->pt;
  238. // if the text box exists on a button up, was the button up close enough
  239. // to the button down to decide to place instead of throw away and
  240. // create a new text edit box.
  241. if (m_pCTedit != NULL)
  242. {
  243. PlaceTextOnBitmap( pImgWnd );
  244. int iDist = max( (abs( cPtDownUpDistance.cx )),
  245. (abs( cPtDownUpDistance.cy )) );
  246. if (iDist <= MAX_MOVE_DIST_FOR_PLACE)
  247. {
  248. ClearStatusBarSize();
  249. CImgTool::OnEndDrag( pImgWnd, pmti );
  250. }
  251. else
  252. CreateTextEditObject( pImgWnd, pmti );
  253. }
  254. else // m_pCTedit == NULL either 1st time or destroyed, since on drag moved more than MAX_MOVE_DIS_FOR_PLACE
  255. {
  256. CreateTextEditObject( pImgWnd, pmti );
  257. }
  258. }
  259. /******************************************************************************/
  260. void CTextTool::OnDrag( CImgWnd* pImgWnd, MTI* pmti )
  261. {
  262. CPoint ptNew( pmti->pt.x, pmti->pt.y );
  263. CRect rectImg;
  264. pImgWnd->GetClientRect( &rectImg );
  265. pImgWnd->ClientToImage( rectImg );
  266. if (! rectImg.PtInRect( ptNew ))
  267. {
  268. if (ptNew.x < rectImg.left)
  269. ptNew.x = rectImg.left;
  270. if (ptNew.x > rectImg.right)
  271. ptNew.x = rectImg.right;
  272. if (ptNew.y < rectImg.top)
  273. ptNew.y = rectImg.top;
  274. if (ptNew.y > rectImg.bottom)
  275. ptNew.y = rectImg.bottom;
  276. pmti->pt = ptNew;
  277. }
  278. CSelectTool::OnDrag( pImgWnd, pmti );
  279. }
  280. /******************************************************************************/
  281. /* Set the text edit tool window's options for transparent or opaque */
  282. void CTextTool::OnClickOptions( CImgToolWnd* pWnd, const CRect& optionsRect,
  283. const CPoint& clickPoint )
  284. {
  285. CSelectTool::OnClickOptions( pWnd, optionsRect, clickPoint );
  286. if (m_pCTedit != NULL)
  287. m_pCTedit->SetTransparentMode( ! theImgBrush.m_bOpaque );
  288. }
  289. /******************************************************************************/
  290. /* report to the rest of the program if the font palette is showin */
  291. BOOL CTextTool::FontPaletteVisible()
  292. {
  293. return (m_pCTedit? m_pCTedit->IsFontPaletteVisible(): FALSE);
  294. }
  295. /******************************************************************************/
  296. /* toggle the visable state of the Font Palette */
  297. void CTextTool::ToggleFontPalette()
  298. {
  299. if (m_pCTedit)
  300. m_pCTedit->ShowFontPalette( m_pCTedit->IsFontPaletteVisible()? SW_HIDE: SW_SHOW );
  301. }
  302. /******************************************************************************/
  303. void CTextTool::OnShowControlBars(BOOL bShow)
  304. {
  305. if (m_pCTedit == NULL)
  306. {
  307. return;
  308. }
  309. if (bShow)
  310. {
  311. if (!theApp.m_bShowTextToolbar)
  312. {
  313. return;
  314. }
  315. m_pCTedit->ShowFontToolbar();
  316. }
  317. else
  318. {
  319. m_pCTedit->HideFontToolbar();
  320. }
  321. }
  322. /******************************************************************************/
  323. void CTextTool::CloseTextTool( CImgWnd* pImgWnd )
  324. {
  325. if (! m_pCTedit)
  326. return;
  327. if ( IsWindow(pImgWnd->m_hWnd) )
  328. {
  329. if (! m_pCTedit->IsModified())
  330. {
  331. OnCancel( pImgWnd );
  332. return;
  333. }
  334. if (pRubberImg != pImgWnd->m_pImg)
  335. SetupRubber( pImgWnd->m_pImg );
  336. // SetUndo( pImgWnd->m_pImg );
  337. PlaceTextOnBitmap( pImgWnd );
  338. pImgWnd->UpdateWindow();
  339. }
  340. }
  341. /******************************************************************************/