Source code of Windows XP (NT5)
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.

556 lines
14 KiB

  1. // pbrusfrm.cpp : implementation of the CPBFrame class
  2. //
  3. #include "stdafx.h"
  4. #include "resource.h"
  5. #include "global.h"
  6. #include "pbrusfrm.h"
  7. #include "pbrusvw.h"
  8. #include "pbrush.h"
  9. #include "pbrusdoc.h"
  10. #include "bmobject.h"
  11. #include "docking.h"
  12. #include "minifwnd.h"
  13. #include "imgwnd.h"
  14. #include "imgwell.h"
  15. #include "imgtools.h"
  16. #include "imgdlgs.h"
  17. #include "cmpmsg.h"
  18. #include "props.h"
  19. #include "colorsrc.h"
  20. #include <htmlhelp.h>
  21. #include "imaging.h"
  22. #ifdef _DEBUG
  23. #undef THIS_FILE
  24. static CHAR BASED_CODE THIS_FILE[] = __FILE__;
  25. #endif
  26. IMPLEMENT_DYNCREATE( CPBFrame, CFrameWnd )
  27. #include "memtrace.h"
  28. #define UM_FILE_ERROR WM_USER + 1001
  29. /*************************** CPBFrame **************************************/
  30. BEGIN_MESSAGE_MAP( CPBFrame, CFrameWnd )
  31. //{{AFX_MSG_MAP(CPBFrame)
  32. ON_WM_ACTIVATEAPP()
  33. ON_WM_CREATE()
  34. ON_WM_DESTROY()
  35. ON_WM_SETFOCUS()
  36. ON_WM_PALETTECHANGED()
  37. ON_WM_QUERYNEWPALETTE()
  38. ON_WM_GETMINMAXINFO()
  39. ON_WM_MOVE()
  40. ON_WM_SIZE()
  41. ON_WM_ERASEBKGND()
  42. ON_WM_DEVMODECHANGE()
  43. ON_WM_WININICHANGE()
  44. ON_COMMAND(ID_HELP, OnHelp)
  45. ON_WM_SYSCOLORCHANGE()
  46. ON_WM_CLOSE()
  47. ON_UPDATE_COMMAND_UI(ID_FILE_SCAN_NEW, OnUpdateAcquire)
  48. ON_COMMAND(ID_FILE_SCAN_NEW, OnAcquire)
  49. ON_UPDATE_COMMAND_UI(ID_FILE_SELECT_SOURCE, OnUpdateSelectSource)
  50. ON_COMMAND(ID_FILE_SELECT_SOURCE, OnSelectSource)
  51. //}}AFX_MSG_MAP
  52. ON_MESSAGE(UM_FILE_ERROR, OnFileError)
  53. // Global help commands
  54. ON_COMMAND(ID_HELP_INDEX, CFrameWnd::OnHelpIndex)
  55. ON_COMMAND(ID_HELP_USING, CFrameWnd::OnHelpUsing)
  56. ON_COMMAND(ID_CONTEXT_HELP, CFrameWnd::OnContextHelp)
  57. ON_COMMAND(ID_DEFAULT_HELP, CFrameWnd::OnHelpIndex)
  58. ON_UPDATE_COMMAND_UI(ID_VIEW_TOOL_BOX, CFrameWnd::OnUpdateControlBarMenu)
  59. ON_COMMAND_EX(ID_VIEW_TOOL_BOX, CFrameWnd::OnBarCheck)
  60. ON_UPDATE_COMMAND_UI(ID_VIEW_COLOR_BOX, CFrameWnd::OnUpdateControlBarMenu)
  61. ON_COMMAND_EX(ID_VIEW_COLOR_BOX, CFrameWnd::OnBarCheck)
  62. END_MESSAGE_MAP()
  63. /***************************************************************************/
  64. /*********************** CPBFrame construction/destruction *****************/
  65. CPBFrame::CPBFrame()
  66. {
  67. // Just small enough so that the control bars fit
  68. m_szFrameMin = CSize( 275, 410 );
  69. m_pMgr = new CWIAMgr;
  70. #ifdef USE_TWAIN
  71. if (!m_pMgr || !m_pMgr->IsAvailable())
  72. {
  73. delete m_pMgr;
  74. m_pMgr = new CTwainMgr;
  75. }
  76. #endif //USE_TWAIN
  77. }
  78. /***************************************************************************/
  79. CPBFrame::~CPBFrame()
  80. {
  81. // also can't delete objects derived from class cframewnd, must destroy their
  82. // window => deletion indirectly
  83. delete m_pMgr;
  84. }
  85. /*************************** CPBFrame diagnostics **************************/
  86. #ifdef _DEBUG
  87. void CPBFrame::AssertValid() const
  88. {
  89. CFrameWnd::AssertValid();
  90. }
  91. /***************************************************************************/
  92. void CPBFrame::Dump(CDumpContext& dc) const
  93. {
  94. CFrameWnd::Dump(dc);
  95. }
  96. #endif //_DEBUG
  97. /***************************************************************************/
  98. TCHAR mszMSPaintClass[] = TEXT("MSPaintApp");
  99. BOOL CPBFrame::PreCreateWindow( CREATESTRUCT& cs )
  100. {
  101. cs.dwExStyle |= WS_EX_WINDOWEDGE;
  102. cs.style |= WS_CLIPCHILDREN;
  103. BOOL bResult = CFrameWnd::PreCreateWindow( cs );
  104. if (bResult)
  105. {
  106. WINDOWPLACEMENT& wpSaved = theApp.m_wpPlacement;
  107. RECT& rcSaved = wpSaved.rcNormalPosition;
  108. CPoint ptPBrush(rcSaved.left, rcSaved.top);
  109. CSize sizePBrush(rcSaved.right - rcSaved.left, rcSaved.bottom - rcSaved.top);
  110. CPoint pt = theApp.CheckWindowPosition( ptPBrush, sizePBrush );
  111. if (pt.x || pt.y)
  112. {
  113. cs.x = pt.x;
  114. cs.y = pt.y;
  115. }
  116. sizePBrush.cx = max(sizePBrush.cx, m_szFrameMin.cx);
  117. sizePBrush.cy = max(sizePBrush.cy, m_szFrameMin.cy);
  118. if (sizePBrush.cx && sizePBrush.cy)
  119. {
  120. cs.cx = sizePBrush.cx;
  121. cs.cy = sizePBrush.cy;
  122. }
  123. rcSaved.left = cs.x;
  124. rcSaved.top = cs.y;
  125. rcSaved.right = rcSaved.left + cs.cx;
  126. rcSaved.bottom = rcSaved.top + cs.cy;
  127. WNDCLASS wndcls;
  128. HINSTANCE hInst = AfxGetInstanceHandle();
  129. // see if the class already exists
  130. if (! ::GetClassInfo( hInst, mszMSPaintClass, &wndcls ))
  131. {
  132. // get default stuff
  133. ::GetClassInfo( hInst, cs.lpszClass, &wndcls );
  134. // register a new class
  135. wndcls.lpszClassName = mszMSPaintClass;
  136. wndcls.hIcon = ::LoadIcon( hInst, MAKEINTRESOURCE( ID_MAINFRAME ) );
  137. ASSERT( wndcls.hIcon != NULL );
  138. if (! AfxRegisterClass( &wndcls ))
  139. AfxThrowResourceException();
  140. }
  141. cs.lpszClass = mszMSPaintClass;
  142. }
  143. return bResult;
  144. }
  145. /***************************************************************************/
  146. CWnd* CPBFrame::GetMessageBar()
  147. {
  148. if (m_statBar.m_hWnd != NULL)
  149. return &m_statBar;
  150. return NULL;
  151. }
  152. /***************************************************************************/
  153. void CPBFrame::OnHelp()
  154. {
  155. if (m_dwPromptContext)
  156. CFrameWnd::OnHelp();
  157. else
  158. ::HtmlHelpA( ::GetDesktopWindow(), "mspaint.chm", HH_DISPLAY_TOPIC, 0L );
  159. }
  160. /***************************************************************************/
  161. int CPBFrame::OnCreate( LPCREATESTRUCT lpCreateStruct )
  162. {
  163. if (CFrameWnd::OnCreate( lpCreateStruct ) == -1)
  164. return -1;
  165. g_pStatBarWnd = &m_statBar;
  166. g_pImgToolWnd = &m_toolBar;
  167. g_pImgColorsWnd = &m_colorBar;
  168. return 0;
  169. }
  170. /***************************************************************************/
  171. void CPBFrame::OnDestroy()
  172. {
  173. CFrameWnd::OnDestroy();
  174. theApp.m_wpPlacement.length = sizeof(WINDOWPLACEMENT);
  175. GetWindowPlacement(&theApp.m_wpPlacement);
  176. theApp.SaveProfileSettings();
  177. }
  178. /***************************************************************************/
  179. BOOL CPBFrame::OnEraseBkgnd(CDC* pDC)
  180. {
  181. if ( !m_pViewActive ) //fix gray background on screen while IME disappear problem
  182. {
  183. CRect cRectClient;
  184. GetClientRect( &cRectClient );
  185. pDC->FillRect( &cRectClient, GetSysBrush( COLOR_BTNFACE ) );
  186. }
  187. return CFrameWnd::OnEraseBkgnd( pDC );
  188. }
  189. /***************************************************************************/
  190. void CPBFrame::OnActivateApp(BOOL bActive, HTASK hTask)
  191. {
  192. theApp.m_bActiveApp = bActive;
  193. CFrameWnd::OnActivateApp(bActive, hTask);
  194. }
  195. /***************************************************************************/
  196. void CPBFrame::OnMove( int x, int y )
  197. {
  198. CRect cRectWindow;
  199. GetWindowRect( &cRectWindow );
  200. m_ptPosition.x = cRectWindow.left;
  201. m_ptPosition.y = cRectWindow.top;
  202. CWnd::OnMove( x, y );
  203. }
  204. /***************************************************************************/
  205. void CPBFrame::OnSize( UINT nType, int cx, int cy )
  206. {
  207. CFrameWnd::OnSize( nType, cx, cy );
  208. CRect rect;
  209. GetWindowRect( &rect );
  210. m_szFrame = rect.Size();
  211. }
  212. /***************************************************************************/
  213. void CPBFrame::OnSetFocus(CWnd* pOldWnd)
  214. {
  215. CFrameWnd::OnSetFocus( pOldWnd );
  216. // We need to update the window here because the SetFocus below will update
  217. // the image window, and then some async paints can come after that which
  218. // will cause us to put the background color over parts of the window (see
  219. // WIN95C bug #4080).
  220. UpdateWindow();
  221. CPBView* pView = (CPBView*)GetActiveView();
  222. if (pView
  223. && pView->IsKindOf( RUNTIME_CLASS( CPBView ) )
  224. && pView->m_pImgWnd != NULL
  225. && ::IsWindow(pView->m_pImgWnd->m_hWnd) )
  226. pView->m_pImgWnd->SetFocus();
  227. }
  228. /***************************************************************************/
  229. void CPBFrame::OnPaletteChanged( CWnd* pFocusWnd )
  230. {
  231. CFrameWnd::OnPaletteChanged( pFocusWnd );
  232. CPBView* pView = (CPBView*)GetActiveView();
  233. if (pView != NULL && pView->IsKindOf( RUNTIME_CLASS( CPBView ) ))
  234. {
  235. pView->OnPaletteChanged( pFocusWnd );
  236. }
  237. }
  238. /***************************************************************************/
  239. BOOL CPBFrame::OnQueryNewPalette()
  240. {
  241. CPBView* pView = (CPBView*)GetActiveView();
  242. if (pView != NULL && ::IsWindow(pView->m_hWnd) && pView->IsKindOf( RUNTIME_CLASS( CPBView ) ))
  243. {
  244. pView->OnQueryNewPalette();
  245. }
  246. return CFrameWnd::OnQueryNewPalette();
  247. }
  248. /***************************************************************************/
  249. void CPBFrame::OnGetMinMaxInfo(MINMAXINFO FAR* lpMMI)
  250. {
  251. lpMMI->ptMinTrackSize.x = m_szFrameMin.cx;
  252. lpMMI->ptMinTrackSize.y = m_szFrameMin.cy;
  253. CFrameWnd::OnGetMinMaxInfo(lpMMI);
  254. }
  255. /*****************************************************************************/
  256. void CPBFrame::OnDevModeChange( LPTSTR lpDeviceName )
  257. {
  258. CClientDC dc( this );
  259. if (dc.m_hDC != NULL)
  260. theApp.GetSystemSettings( &dc );
  261. CFrameWnd::OnDevModeChange( lpDeviceName );
  262. }
  263. /*****************************************************************************/
  264. void CPBFrame::OnWinIniChange( LPCTSTR lpszSection )
  265. {
  266. CClientDC dc( this );
  267. CDocument *pDoc;
  268. if (dc.m_hDC != NULL)
  269. theApp.GetSystemSettings( &dc );
  270. if ( m_statBar.m_hWnd != NULL
  271. && theApp.m_fntStatus.m_hObject != NULL)
  272. m_statBar.SetFont( &theApp.m_fntStatus, FALSE );
  273. CFrameWnd::OnWinIniChange( lpszSection );
  274. if (pDoc = GetActiveDocument())
  275. {
  276. pDoc->UpdateAllViews( NULL );
  277. GetActiveView()->UpdateWindow();
  278. }
  279. }
  280. /*****************************************************************************/
  281. void CPBFrame::ActivateFrame( int nCmdShow )
  282. {
  283. WINDOWPLACEMENT& wpSaved = theApp.m_wpPlacement;
  284. if (theApp.m_bPrintOnly)
  285. {
  286. nCmdShow = SW_HIDE;
  287. }
  288. else if (!IsWindowVisible())
  289. {
  290. switch (nCmdShow)
  291. {
  292. case SW_SHOW:
  293. case SW_SHOWNORMAL:
  294. switch (wpSaved.showCmd)
  295. {
  296. case SW_HIDE:
  297. case SW_MINIMIZE:
  298. case SW_SHOWMINIMIZED:
  299. case SW_SHOWMINNOACTIVE:
  300. break;
  301. default:
  302. nCmdShow = wpSaved.showCmd;
  303. break;
  304. }
  305. break;
  306. }
  307. wpSaved.showCmd = nCmdShow;
  308. wpSaved.length = sizeof(WINDOWPLACEMENT);
  309. SetWindowPlacement(&wpSaved);
  310. }
  311. //
  312. // We have to reassign the global toolbar pointers here, in case
  313. // they were pointing to the inplace frame's toolbars and that window
  314. // was deleted.
  315. //
  316. g_pStatBarWnd = &m_statBar;
  317. g_pImgToolWnd = &m_toolBar;
  318. g_pImgColorsWnd = &m_colorBar;
  319. CFrameWnd::ActivateFrame( nCmdShow );
  320. }
  321. /*****************************************************************************/
  322. #ifdef xyzzyz
  323. void CPBFrame::OnUpdateFrameTitle( BOOL bAddToTitle )
  324. {
  325. if (theApp.m_bEmbedded && ! theApp.m_bLinked)
  326. {
  327. CFrameWnd::OnUpdateFrameTitle( bAddToTitle );
  328. return;
  329. }
  330. // get old text for comparison against new text
  331. CString sOld;
  332. CString sText;
  333. GetWindowText( sOld );
  334. CPBDoc* pDocument = (CPBDoc*)GetActiveDocument();
  335. if (bAddToTitle && pDocument != NULL)
  336. {
  337. const TCHAR* psTitle = pDocument->GetTitle();
  338. if (psTitle != NULL)
  339. {
  340. sText += GetName( psTitle );
  341. sText += TEXT(" - ");
  342. sText.MakeLower();
  343. }
  344. }
  345. sText += m_strTitle;
  346. // set title if changed, but don't remove completely
  347. if (sText != sOld)
  348. SetWindowText( sText );
  349. }
  350. #endif
  351. /*****************************************************************************/
  352. LRESULT CPBFrame::OnFileError( WPARAM, LPARAM )
  353. {
  354. theApp.FileErrorMessageBox();
  355. return 0;
  356. }
  357. /***************************************************************************/
  358. void CPBFrame::OnSysColorChange()
  359. {
  360. CFrameWnd ::OnSysColorChange();
  361. ResetSysBrushes();
  362. }
  363. void CPBFrame::OnClose()
  364. {
  365. SaveBarState(TEXT("General"));
  366. CFrameWnd ::OnClose();
  367. }
  368. /***************************************************************************/
  369. void CPBFrame::OnSelectSource()
  370. {
  371. CWaitCursor DisplayWaitCursor;
  372. // if m_pMgr == 0, "Select Source" menu item will not be available anyway
  373. ASSERT(m_pMgr != 0);
  374. HRESULT hr = m_pMgr->SelectSource(
  375. GetSafeHwnd(),
  376. WIA_SELECT_DEVICE_NODEFAULT
  377. );
  378. if (!SUCCEEDED(hr))
  379. {
  380. CmpMessageBox(IDS_ERROR_SELECT_SCAN,
  381. AFX_IDS_APP_TITLE, MB_OK | MB_ICONEXCLAMATION);
  382. }
  383. }
  384. void CPBFrame::OnAcquire()
  385. {
  386. CWaitCursor DisplayWaitCursor;
  387. // if m_pMgr == 0, "Scan New" menu item will not be available anyway
  388. ASSERT(m_pMgr != 0);
  389. // call the WIA interface to acquire the image
  390. HGLOBAL hDib = 0;
  391. HRESULT hr = m_pMgr->Acquire(GetSafeHwnd(), &hDib);
  392. if (!SUCCEEDED(hr))
  393. {
  394. // if the interface has failed, display the generic error message
  395. CmpMessageBox(IDS_ERROR_SCAN_NEW,
  396. AFX_IDS_APP_TITLE, MB_OK | MB_ICONEXCLAMATION);
  397. }
  398. else if (hr == S_OK)
  399. {
  400. // if the user has not cancelled the operation, get the image
  401. CPBView *pView = (CPBView*)GetActiveView();
  402. if (pView)
  403. {
  404. CPBDoc *pDoc = (CPBDoc *)pView->GetDocument();
  405. // prompt to save the current document if it was modified
  406. if (pDoc && pDoc->SaveModified())
  407. {
  408. pDoc->SetModifiedFlag(FALSE);
  409. // and set this as the new image
  410. theApp.OnFileNew ();
  411. pDoc->SetDibHandle (hDib);
  412. pView->m_pImgWnd->Invalidate();
  413. pView->m_pImgWnd->CheckScrollBars();
  414. }
  415. }
  416. }
  417. }
  418. void CPBFrame::OnUpdateSelectSource(CCmdUI* pCmdUI)
  419. {
  420. pCmdUI->Enable(m_pMgr && m_pMgr->NumDevices(GetSafeHwnd()) > 0);
  421. }
  422. void CPBFrame::OnUpdateAcquire(CCmdUI* pCmdUI)
  423. {
  424. pCmdUI->Enable(m_pMgr && m_pMgr->NumDevices(GetSafeHwnd()) > 0);
  425. }