ARGH
[matches/honours.git] / research / transmission_spectroscopy / TOF / Win32++ / samples / FastGDI / src / Mainfrm.cpp
1 ////////////////////////////////////////////////////\r
2 // Mainfrm.cpp\r
3 \r
4 #include "stdafx.h"\r
5 #include "mainfrm.h"\r
6 #include "resource.h"\r
7 \r
8 \r
9 // Definitions for the CMainFrame class\r
10 CMainFrame::CMainFrame()\r
11 {\r
12         // Constructor for CMainFrame. Its called after CFrame's constructor\r
13 \r
14         //Set m_MyView as the view window of the frame\r
15         SetView(m_MyView);\r
16 \r
17         // Set the registry key name, and load the initial window position\r
18         // Use a registry key name like "CompanyName\\Application"\r
19         LoadRegistrySettings(_T("Win32++\\Fast GDI Demo"));\r
20 \r
21         // Load the settings from the registry with 4 MRU entries\r
22         LoadRegistryMRUSettings(4);\r
23 }\r
24 \r
25 CMainFrame::~CMainFrame()\r
26 {\r
27         // Destructor for CMainFrame.\r
28 }\r
29 \r
30 void CMainFrame::OnAdjustImage()\r
31 {\r
32         if (GetMyView().GetImage())\r
33         {\r
34                 // Initiate the Colour Adjust Dialog\r
35                 CColourDialog Dialog(IDD_DIALOG1);\r
36                 Dialog.DoModal();\r
37         }\r
38         else\r
39                 MessageBox(_T("Open a Bitmap file first!"), _T("Error"), MB_OK);\r
40 }\r
41 \r
42 void CMainFrame::ModifyBitmap(int cRed, int cGreen, int cBlue, BOOL bGray)\r
43 {\r
44         TintBitmap(FromHandle(GetMyView().GetImage()), cRed, cGreen, cBlue);\r
45         if (bGray)      GrayScaleBitmap(FromHandle(GetMyView().GetImage()));\r
46 \r
47         GetMyView().RedrawWindow(0, 0, RDW_NOERASE|RDW_INVALIDATE|RDW_UPDATENOW);\r
48 }\r
49 \r
50 BOOL CMainFrame::OnCommand(WPARAM wParam, LPARAM lParam)\r
51 {\r
52         // OnCommand responds to menu and and toolbar input\r
53 \r
54         switch(LOWORD(wParam))\r
55         {\r
56         case IDM_FILE_NEW:\r
57                 OnFileNew();\r
58                 return TRUE;\r
59         case IDM_FILE_OPEN:\r
60                 OnFileOpen();\r
61                 return TRUE;\r
62         case IDM_FILE_SAVE:\r
63                 OnFileSave();\r
64                 return TRUE;\r
65         case IDM_FILE_SAVEAS:\r
66                 OnFileSaveAs();\r
67                 return TRUE;\r
68         case IDM_FILE_EXIT:\r
69                 // End the application\r
70                 ::PostMessage(m_hWnd, WM_CLOSE, 0, 0);\r
71                 return TRUE;\r
72         case IDM_IMAGE_ADJUST:\r
73                 OnAdjustImage();\r
74                 return TRUE;\r
75         case IDW_VIEW_STATUSBAR:\r
76                 OnViewStatusBar();\r
77                 return TRUE;\r
78         case IDW_VIEW_TOOLBAR:\r
79                 OnViewToolBar();\r
80                 return TRUE;\r
81         case IDM_HELP_ABOUT:\r
82                 // Display the help dialog\r
83                 OnHelp();\r
84                 return TRUE;\r
85         case IDW_FILE_MRU_FILE1:\r
86         case IDW_FILE_MRU_FILE2:\r
87         case IDW_FILE_MRU_FILE3:\r
88         case IDW_FILE_MRU_FILE4:\r
89                 OnFileOpenMRU(wParam, lParam);\r
90                 return TRUE;\r
91         }\r
92 \r
93         return FALSE;\r
94 }\r
95 \r
96 void CMainFrame::OnCreate()\r
97 {\r
98         // OnCreate controls the way the frame is created.\r
99         // Overriding CFrame::Oncreate is optional.\r
100         // The default for the following variables is TRUE\r
101 \r
102         // m_bShowIndicatorStatus = FALSE;      // Don't show statusbar indicators\r
103         // m_bShowMenuStatus = FALSE;           // Don't show toolbar or menu status\r
104         // m_bUseReBar = FALSE;                         // Don't use rebars\r
105         // m_bUseThemes = FALSE;            // Don't use themes\r
106         // m_bUseToolBar = FALSE;                       // Don't use a toolbar\r
107         // m_bUseCustomDraw = FALSE;            // Don't use custom draw for menu items\r
108 \r
109         // call the base class function\r
110         CFrame::OnCreate();\r
111 }\r
112 \r
113 void CMainFrame::OnFileNew()\r
114 {\r
115         CToolBar& TB = GetToolBar();\r
116         TB.DisableButton(IDM_FILE_SAVEAS);\r
117         TB.DisableButton(IDM_IMAGE_ADJUST);\r
118         m_MyView.FileOpen(NULL);\r
119         m_MyView.Invalidate();\r
120 \r
121         // Set the caption\r
122         SetWindowText(_T("FastGDI"));\r
123 }\r
124 \r
125 void CMainFrame::OnFileOpen()\r
126 {\r
127         CFile File;\r
128         CString str = File.OpenFileDialog(0, OFN_FILEMUSTEXIST, _T("Scribble Files (*.bmp)\0*.bmp\0\0"), this);\r
129         if (!str.IsEmpty())\r
130         {\r
131                 // Load the bitmap\r
132                 m_MyView.FileOpen(str);\r
133 \r
134                 // Save the filename\r
135                 m_PathName = str;\r
136                 AddMRUEntry(str);\r
137 \r
138                 // Turn on the ToolBar adjust button\r
139                 CToolBar& TB = GetToolBar();\r
140                 TB.EnableButton(IDM_FILE_SAVEAS);\r
141                 TB.EnableButton(IDM_IMAGE_ADJUST);\r
142                 EnableMenuItem(GetFrameMenu(), IDM_IMAGE_ADJUST, MF_BYCOMMAND | MF_ENABLED);\r
143 \r
144                 // Resize the frame to match the bitmap\r
145                 if (GetMyView().GetImage())\r
146                 {\r
147                         GetMyView().ShowScrollBar(SB_HORZ, FALSE);\r
148                         GetMyView().ShowScrollBar(SB_VERT, FALSE);\r
149                         CRect rcImage = GetMyView().GetImageRect();\r
150                         AdjustFrameRect(rcImage);\r
151                 }\r
152 \r
153                 GetMyView().RedrawWindow(0, 0, RDW_NOERASE|RDW_INVALIDATE|RDW_UPDATENOW);\r
154 \r
155                 // Set the caption\r
156                 CString str = _T("FastGDI - ") + m_PathName;\r
157                 SetWindowText(str);\r
158         }\r
159 }\r
160 \r
161 BOOL CMainFrame::OnFileOpenMRU(WPARAM wParam, LPARAM lParam)\r
162 {\r
163         UNREFERENCED_PARAMETER(lParam);\r
164 \r
165         UINT nMRUIndex = LOWORD(wParam) - IDW_FILE_MRU_FILE1;\r
166         CString strMRUText = GetMRUEntry(nMRUIndex);\r
167         CToolBar& TB = GetToolBar();\r
168 \r
169         if (m_MyView.FileOpen(strMRUText))\r
170         {\r
171                 m_PathName = strMRUText;\r
172                 TB.EnableButton(IDM_FILE_SAVEAS);\r
173                 TB.EnableButton(IDM_IMAGE_ADJUST);\r
174 \r
175                 // Adjust the window size\r
176                 CRect rcImage = GetMyView().GetImageRect();\r
177                 AdjustFrameRect(rcImage);\r
178         }\r
179         else\r
180         {\r
181                 RemoveMRUEntry(strMRUText);\r
182                 TB.DisableButton(IDM_FILE_SAVEAS);\r
183                 TB.DisableButton(IDM_IMAGE_ADJUST);\r
184         }\r
185 \r
186         // Resize the frame to match the bitmap\r
187         if (GetMyView().GetImage())\r
188         {\r
189                 GetMyView().ShowScrollBar(SB_HORZ, FALSE);\r
190                 GetMyView().ShowScrollBar(SB_VERT, FALSE);\r
191                 CRect rcImage = GetMyView().GetImageRect();\r
192                 AdjustFrameRect(rcImage);\r
193         }\r
194 \r
195         GetMyView().RedrawWindow(0, 0, RDW_NOERASE|RDW_INVALIDATE|RDW_UPDATENOW);\r
196 \r
197         // Set the caption\r
198         CString str = _T("FastGDI - ") + m_PathName;\r
199         SetWindowText(str);\r
200         return TRUE;\r
201 }\r
202 \r
203 void CMainFrame::OnFileSave()\r
204 {\r
205         if (!m_PathName.IsEmpty())\r
206         {\r
207                 CString str = m_PathName + _T("  already exists.\nDo you want to replace it?");\r
208 \r
209                 if (IDYES == MessageBox(str, _T("FileSaveAs"), MB_YESNO | MB_ICONWARNING))\r
210                         m_MyView.FileSave(m_PathName);\r
211         }\r
212 }\r
213 \r
214 void CMainFrame::OnFileSaveAs()\r
215 {\r
216         CFile File;\r
217         CString str = File.SaveFileDialog(0, OFN_OVERWRITEPROMPT, _T("Bitmap Files (*.bmp)\0*.bmp\0\0"), _T("bmp"), 0);\r
218         if (!str.IsEmpty())\r
219         {\r
220                 // Set the caption\r
221                 m_PathName = str;\r
222                 CString Title = _T("FastGDI - ") + m_PathName;\r
223                 SetWindowText(Title);\r
224 \r
225                 // Save the file name\r
226                 m_MyView.FileSave(str);\r
227         }\r
228 }\r
229 \r
230 void CMainFrame::OnInitialUpdate()\r
231 {\r
232         // The frame is now created.\r
233         // Place any additional startup code here.\r
234 \r
235         TRACE(_T("Frame created\n"));\r
236 }\r
237 \r
238 inline void CMainFrame::OnMenuUpdate(UINT nID)\r
239 // Called when menu items are about to be displayed\r
240 {\r
241         BOOL IsImageLoaded = (BOOL)GetMyView().GetImage();\r
242 \r
243         switch(nID)\r
244         {\r
245         case IDM_FILE_SAVE:\r
246                 EnableMenuItem(GetFrameMenu(), IDM_FILE_SAVE, IsImageLoaded? MF_ENABLED : MF_GRAYED);\r
247                 break;\r
248         case IDM_FILE_SAVEAS:\r
249                 EnableMenuItem(GetFrameMenu(), IDM_FILE_SAVEAS, IsImageLoaded? MF_ENABLED : MF_GRAYED);\r
250                 break;\r
251         case IDM_IMAGE_ADJUST:\r
252                 EnableMenuItem(GetFrameMenu(), IDM_IMAGE_ADJUST, IsImageLoaded? MF_ENABLED : MF_GRAYED);\r
253                 break;\r
254         }\r
255 }\r
256 \r
257 void CMainFrame::SetupToolBar()\r
258 {\r
259         // Set the Resource IDs for the toolbar buttons\r
260         AddToolBarButton( IDM_FILE_NEW  );\r
261         AddToolBarButton( IDM_FILE_OPEN );\r
262         AddToolBarButton( IDM_FILE_SAVEAS, FALSE );\r
263 \r
264         AddToolBarButton( 0 );  // Separator\r
265         AddToolBarButton( IDM_IMAGE_ADJUST, FALSE );\r
266 \r
267         AddToolBarButton( 0 );  // Separator\r
268         AddToolBarButton( IDM_HELP_ABOUT );\r
269 \r
270         // Use large toolbar buttons\r
271         SetToolBarImages(RGB(192, 192, 192), IDB_TOOLBAR_BIG, 0, 0);\r
272 }\r
273 \r
274 LRESULT CMainFrame::WndProc(UINT uMsg, WPARAM wParam, LPARAM lParam)\r
275 {\r
276 //      switch (uMsg)\r
277 //      {\r
278                 //Additional messages to be handled go here\r
279 //      }\r
280 \r
281         // pass unhandled messages on for default processing\r
282         return WndProcDefault(uMsg, wParam, lParam);\r
283 }\r
284 \r

UCC git Repository :: git.ucc.asn.au