1 // MainFrm.h : interface of the CMainFrame class
3 /////////////////////////////////////////////////////////////////////////////
7 #if !defined(_IMAGEVIEW_MAINFRM_H_)
8 #define _IMAGEVIEW_MAINFRM_H_
11 // Selective message forwarding macros
13 #define FORWARD_MSG(msg) if ( uMsg == msg ) \
14 { lResult = ::SendMessage( GetParent(), uMsg, wParam, lParam ); return bHandled = TRUE;}
16 #define FORWARD_NOTIFICATION_ID(uID) if (( uMsg == WM_NOTIFY) && ( wParam == uID)) \
17 { lResult = ::SendMessage( GetParent(), uMsg, wParam, lParam ); return bHandled = TRUE;}
21 // CMainFrame : Application main window
23 typedef CWinTraits
< WS_CLIPCHILDREN
| WS_CLIPSIBLINGS
,0> CCeFrameTraits
;
26 public CFrameWindowImpl
<CMainFrame
,CWindow
,CCeFrameTraits
>,
27 public CUpdateUI
<CMainFrame
>,
28 public CMessageFilter
, public CIdleHandler
,
29 public CFullScreenFrame
<CMainFrame
, false>,
30 public CAppWindow
<CMainFrame
>
33 // CZoomMenuBar: MenuBar forwarding trackbar messages
34 class CZoomMenuBar
: public CWindowImpl
< CZoomMenuBar
, CMenuBarCtrl
>
37 DECLARE_WND_SUPERCLASS( L
"ZoomMenuBar", L
"ToolbarWindow32");
39 BEGIN_MSG_MAP(CZoomMenuBar
)
40 FORWARD_MSG(WM_HSCROLL
)
41 FORWARD_MSG(WM_CTLCOLORSTATIC
)
42 FORWARD_NOTIFICATION_ID(ID_ZOOM
)
46 // Data and declarations
48 typedef CFrameWindowImpl
<CMainFrame
,CWindow
,CCeFrameTraits
> BaseFrame
;
49 typedef CFullScreenFrame
<CMainFrame
, false> BaseFullScreen
;
51 DECLARE_APP_FRAME_CLASS(NULL
, IDR_MAINFRAME
, L
"Software\\WTL\\ImageView")
52 CImageViewView m_view
;
54 CZoomMenuBar m_MenuBar
;
56 // CMessageFilter pure virtual definition
57 virtual BOOL
PreTranslateMessage(MSG
* pMsg
)
59 if( BaseFrame::PreTranslateMessage(pMsg
))
62 return m_view
.PreTranslateMessage(pMsg
);
66 // CFrameWindowImplBase::UpdateLayout override for CCS_TOP status bar
67 void UpdateLayout(BOOL bResizeBars
= TRUE
)
69 CRect rectWnd
, rectTool
;
70 GetClientRect( rectWnd
);
73 if( m_view
.m_TitleBar
.IsWindow() && ( m_view
.m_TitleBar
.GetStyle() & WS_VISIBLE
))
76 m_view
.m_TitleBar
.SendMessage( WM_SIZE
);
77 m_view
.m_TitleBar
.GetWindowRect( rectTool
);
78 rectWnd
.top
+= rectTool
.Size().cy
;
82 if ( m_view
.IsWindow())
84 m_view
.GetWindowRect( rectTool
);
85 if ( rectTool
!= rectWnd
)
86 m_view
.SetWindowPos( NULL
, rectWnd
, SWP_NOZORDER
| SWP_NOACTIVATE
);
90 // CAppWindow operations
91 bool AppHibernate( bool bHibernate
)
93 if ( bHibernate
) // go to sleep
94 if ( m_sFile
.IsEmpty()) // clipboard or no image
97 m_view
.m_bmp
.DeleteObject();
100 if ( HBITMAP hbm
= LoadImageFile( m_sFile
))
101 m_view
.m_bmp
.Attach( hbm
);
102 else // file was moved or deleted during hibernation
108 bool AppNewInstance( LPCTSTR lpstrCmdLine
)
110 return SetImageFile( lpstrCmdLine
) != NULL
;
116 BOOL bTitle
= m_view
.m_TitleBar
.IsWindowVisible();
117 info
.Save( bTitle
, L
"TitleBar");
118 info
.Save( m_view
.m_bShowScroll
, L
"ScrollBars");
119 info
.Save( m_bFullScreen
, L
"Full");
120 info
.Save( m_sFile
, L
"Image");
121 info
.Save( m_view
.GetScrollOffset(), L
"Scroll");
122 info
.Save( m_view
.m_fzoom
, L
"Zoom");
125 // File and image operations
126 HBITMAP
LoadImageFile( LPCTSTR szFileName
)
128 CString szImage
= szFileName
;
129 CBitmapHandle hBmp
= szImage
.Find( L
".bmp") != -1 ?
130 ::SHLoadDIBitmap( szFileName
) : ::SHLoadImageFile( szFileName
);
134 CString strMsg
= L
"Cannot load image from: ";
135 strMsg
+= szFileName
;
136 AtlMessageBox( m_hWnd
, (LPCTSTR
)strMsg
, IDR_MAINFRAME
, MB_OK
| MB_ICONERROR
);
141 bool SetImageFile( LPCTSTR szFileName
, double fZoom
= 1.0 , POINT ptScroll
= CPoint( -1, -1))
144 if ( szFileName
&& *szFileName
)
145 hBmp
= LoadImageFile( szFileName
);
146 bool bOK
= !hBmp
.IsNull();
149 m_sFile
= szFileName
;
150 CString sImageName
= m_sFile
.Mid( m_sFile
.ReverseFind(L
'\\') + 1);
151 m_view
.SetImage( hBmp
, sImageName
.Left( sImageName
.ReverseFind(L
'.')), fZoom
, ptScroll
);
156 UIEnable(ID_ZOOM
, bOK
);
157 UIEnable(ID_FILE_CLOSE
, bOK
);
158 UIEnable(ID_EDIT_COPY
, bOK
);
159 UIEnable(ID_VIEW_PROPERTIES
, bOK
);
166 m_view
.SetImage( NULL
);
169 // UpdateUI operations and map
170 virtual BOOL
OnIdle()
172 UIEnable( ID_EDIT_PASTE
, IsClipboardFormatAvailable( CF_DIB
));
174 UIUpdateChildWindows();
178 BEGIN_UPDATE_UI_MAP(CMainFrame
)
179 UPDATE_ELEMENT(ID_EDIT_COPY
, UPDUI_MENUPOPUP
| UPDUI_TOOLBAR
)
180 UPDATE_ELEMENT(ID_EDIT_PASTE
, UPDUI_MENUPOPUP
| UPDUI_TOOLBAR
)
181 UPDATE_ELEMENT(ID_FILE_CLOSE
, UPDUI_MENUPOPUP
| UPDUI_TOOLBAR
)
182 UPDATE_ELEMENT(ID_VIEW_PROPERTIES
, UPDUI_MENUPOPUP
| UPDUI_TOOLBAR
)
183 UPDATE_ELEMENT(ID_VIEW_TOOLBAR
, UPDUI_MENUPOPUP
)
184 UPDATE_ELEMENT(ID_VIEW_SCROLLBARS
, UPDUI_MENUPOPUP
)
185 UPDATE_ELEMENT(ID_VIEW_STATUS_BAR
, UPDUI_MENUPOPUP
)
186 UPDATE_ELEMENT(ID_ZOOM
, UPDUI_CHILDWINDOW
)
189 // Message map and handlers
190 BEGIN_MSG_MAP(CMainFrame
)
191 MESSAGE_HANDLER(WM_CREATE
, OnCreate
)
192 NOTIFY_CODE_HANDLER(GN_CONTEXTMENU
, OnContextMenu
)
193 COMMAND_ID_HANDLER(ID_APP_EXIT
, OnExit
)
194 COMMAND_ID_HANDLER(ID_FILE_OPEN
, OnFileOpen
)
195 COMMAND_ID_HANDLER(ID_FILE_CLOSE
, OnFileClose
)
196 COMMAND_ID_HANDLER(ID_EDIT_PASTE
, OnPaste
)
197 COMMAND_ID_HANDLER(ID_APP_ABOUT
, OnAppAbout
)
198 COMMAND_ID_HANDLER(ID_FILE_REGISTER
, OnRegister
)
199 COMMAND_ID_HANDLER(ID_VIEW_PROPERTIES
, OnProperties
)
200 COMMAND_ID_HANDLER(ID_VIEW_STATUS_BAR
, OnTitle
)
201 COMMAND_ID_HANDLER(ID_VIEW_TOOLBAR
, OnFullScreen
)
202 COMMAND_ID_HANDLER(ID_VIEW_SCROLLBARS
, OnScrollBars
)
203 CHAIN_MSG_MAP(BaseFullScreen
)
204 CHAIN_MSG_MAP(CAppWindow
<CMainFrame
>)
205 CHAIN_MSG_MAP(CUpdateUI
<CMainFrame
>)
206 CHAIN_MSG_MAP(BaseFrame
)
207 CHAIN_MSG_MAP_ALT_MEMBER(m_view
, 1)
210 // Creation and destruction
211 LRESULT
OnCreate(UINT
/*uMsg*/, WPARAM
/*wParam*/, LPARAM lParam
, BOOL
& /*bHandled*/)
215 // Full screen delayed restoration
217 info
.Restore( bFull
, L
"Full");
219 PostMessage( WM_COMMAND
, ID_VIEW_TOOLBAR
);
222 CreateSimpleCEMenuBar( IDR_MAINFRAME
, SHCMBF_HIDESIPBUTTON
);
223 m_MenuBar
.SubclassWindow( m_hWndCECommandBar
);
224 m_MenuBar
.LoadStdImages( IDB_STD_SMALL_COLOR
);
225 UIAddToolBar( m_hWndCECommandBar
);
229 m_MenuBar
.GetRect( ID_ZOOM
, rZoom
);
231 m_view
.m_ZoomCtrl
.Create( m_hWndCECommandBar
, rZoom
, NULL
,WS_CHILD
| TBS_TOP
| TBS_FIXEDLENGTH
, 0,
233 m_view
.m_ZoomCtrl
.SetThumbLength( 9);
234 rZoom
.DeflateRect( 1, 1);
235 m_view
.m_ZoomCtrl
.SetWindowPos( HWND_TOP
, rZoom
.left
, rZoom
.top
+ 1,
236 rZoom
.Width(), rZoom
.Height(), SWP_SHOWWINDOW
);
237 UIAddChildWindowContainer( m_hWndCECommandBar
);
241 info
.Restore( bTitle
, L
"TitleBar");
242 DWORD dwStyle
= WS_CHILD
| WS_CLIPCHILDREN
| WS_CLIPSIBLINGS
| CCS_TOP
;
244 dwStyle
|= WS_VISIBLE
;
245 CreateSimpleStatusBar( L
"", dwStyle
);
246 m_view
.m_TitleBar
.Attach( m_hWndStatusBar
);
247 UISetCheck( ID_VIEW_STATUS_BAR
, bTitle
);
250 info
.Restore( m_view
.m_bShowScroll
, L
"ScrollBars");
251 m_hWndClient
= m_view
.Create(m_hWnd
, rcDefault
, NULL
, WS_CHILD
| WS_VISIBLE
| WS_CLIPSIBLINGS
| WS_CLIPCHILDREN
);
252 UISetCheck( ID_VIEW_SCROLLBARS
, m_view
.m_bShowScroll
);
254 // register object for message filtering and idle updates
255 CMessageLoop
* pLoop
= _Module
.GetMessageLoop();
256 ATLASSERT(pLoop
!= NULL
);
257 pLoop
->AddMessageFilter(this);
258 pLoop
->AddIdleHandler(this);
260 // Image initialization
261 LPCTSTR pCmdLine
= (LPCTSTR
)((LPCREATESTRUCT
)lParam
)->lpCreateParams
;
262 if ( *pCmdLine
)// open the command line file
263 SetImageFile( pCmdLine
);
264 else // restore previous image if existing
266 CPoint
ptScroll( 0, 0);
268 info
.Restore( m_sFile
, L
"Image");
269 if ( !m_sFile
.IsEmpty())
271 info
.Restore( ptScroll
, L
"Scroll");
272 info
.Restore( fzoom
, L
"Zoom");
274 SetImageFile( m_sFile
, fzoom
, ptScroll
);
279 LRESULT
OnExit(WORD
/*wNotifyCode*/, WORD
/*wID*/, HWND
/*hWndCtl*/, BOOL
& /*bHandled*/)
281 PostMessage(WM_CLOSE
);
285 // File and image transfers
286 LRESULT
OnFileOpen(WORD
/*wNotifyCode*/, WORD
/*wID*/, HWND
/*hWndCtl*/, BOOL
& /*bHandled*/)
289 L
"Image Files\0*.bmp;*.jpg;*.gif;*.png\0"
290 L
"Bitmap Files (*.bmp)\0*.bmp\0"
291 L
"JPEG Files (*.jpg)\0*.jpg\0"
292 L
"PNG Files (*.png)\0*.png\0"
293 L
"GIF Files (*.gif)\0*.gif\0"
294 L
"All Files (*.*)\0*.*\0\0";
296 CFileDialog
dlg( TRUE
, NULL
, NULL
, OFN_HIDEREADONLY
| OFN_OVERWRITEPROMPT
, sFiles
);
298 if( FSDoModal( dlg
) == IDOK
)
299 SetImageFile( dlg
.m_szFileName
);
304 LRESULT
OnFileClose(WORD
/*wNotifyCode*/, WORD
/*wID*/, HWND
/*hWndCtl*/, BOOL
& /*bHandled*/)
310 LRESULT
OnPaste(WORD
/*wNotifyCode*/, WORD
/*wID*/, HWND
/*hWndCtl*/, BOOL
& /*bHandled*/)
312 if ( CBitmapHandle hbm
= AtlGetClipboardDib( m_hWnd
))
315 m_view
.SetImage( hbm
, L
"pasted");
316 UIEnable(ID_ZOOM
, true);
317 UIEnable(ID_FILE_CLOSE
, true);
318 UIEnable(ID_EDIT_COPY
, true);
319 UIEnable(ID_VIEW_PROPERTIES
, true);
322 AtlMessageBox( m_hWnd
, L
"Could not paste image from clipboard", IDR_MAINFRAME
, MB_OK
| MB_ICONERROR
);
326 // Dialogs and property sheet activation
327 LRESULT
OnAppAbout(WORD
/*wNotifyCode*/, WORD
/*wID*/, HWND
/*hWndCtl*/, BOOL
& /*bHandled*/)
330 CStdSimpleOrientedDialog
<IDD_ABOUTBOX
, IDD_ABOUTBOX_L
, SHIDIF_DONEBUTTON
| SHIDIF_FULLSCREENNOMENUBAR
> dlg
;
334 return FSDoModal( dlg
);
337 LRESULT
OnRegister(WORD
/*wNotifyCode*/, WORD
/*wID*/, HWND
/*hWndCtl*/, BOOL
& /*bHandled*/)
340 return FSDoModal( dlg
);
343 LRESULT
OnProperties(WORD
/*wNotifyCode*/, WORD
/*wID*/, HWND
/*hWndCtl*/, BOOL
& /*bHandled*/)
345 CImageProperties
prop( m_sFile
, m_view
);
346 return FSDoModal( prop
);
349 // User interface settings
350 LRESULT
OnTitle(WORD
/*wNotifyCode*/, WORD
/*wID*/, HWND
/*hWndCtl*/, BOOL
& /*bHandled*/)
352 BOOL bView
= !m_view
.m_TitleBar
.IsWindowVisible();
353 m_view
.m_TitleBar
.ShowWindow( bView
? SW_SHOWNOACTIVATE
: SW_HIDE
);
355 UISetCheck( ID_VIEW_STATUS_BAR
, bView
);
359 LRESULT
OnFullScreen(WORD
/*wNotifyCode*/, WORD
/*wID*/, HWND
/*hWndCtl*/, BOOL
& /*bHandled*/)
361 SetFullScreen( !m_bFullScreen
);
362 UISetCheck( ID_VIEW_TOOLBAR
, m_bFullScreen
);
366 LRESULT
OnScrollBars(WORD
/*wNotifyCode*/, WORD
/*wID*/, HWND
/*hWndCtl*/, BOOL
& bHandled
)
368 UISetCheck( ID_VIEW_SCROLLBARS
, !m_view
.m_bShowScroll
);
369 return bHandled
= FALSE
; // real processing is in m_view
372 LRESULT
OnContextMenu(int /*idCtrl*/, LPNMHDR pnmh
, BOOL
& /*bHandled*/)
374 PNMRGINFO pnmrgi
= (PNMRGINFO
)pnmh
;
376 menu
.LoadMenu( IDR_POPUP
);
377 menu
= menu
.GetSubMenu( 0);
378 return menu
.TrackPopupMenu( TPM_CENTERALIGN
| TPM_VERTICAL
,
379 pnmrgi
->ptAction
.x
, pnmrgi
->ptAction
.y
, m_hWnd
);
384 /////////////////////////////////////////////////////////////////////////////
386 #endif // !defined(_IMAGEVIEW_MAINFRM_H_)