view src/win32/FileDlg.cpp @ 1:f9f4f1b99eed

importing src directory
author Robert McIntyre <rlm@mit.edu>
date Sat, 03 Mar 2012 10:31:27 -0600
parents
children
line wrap: on
line source
1 // FileDlg.cpp: implementation of the FileDlg class.
2 //
3 //////////////////////////////////////////////////////////////////////
4 //#define WINVER 0x0410 // windows 98 - just for this 1 file - just in case
5 #include "stdafx.h"
6 #include <commdlg.h>
7 #include <dlgs.h>
8 #include "resource.h"
9 #include "FileDlg.h"
10 #include "Sound.h"
11 #include "VBA.h"
13 static FileDlg *instance = NULL;
15 static UINT_PTR CALLBACK HookFunc(HWND hwnd,
16 UINT msg,
17 WPARAM wParam,
18 LPARAM lParam)
19 {
20 if (instance)
21 {
22 if (msg == WM_NOTIFY)
23 {
24 OFNOTIFY *notify = (OFNOTIFY *)lParam;
25 if (notify)
26 {
27 if (notify->hdr.code == CDN_TYPECHANGE)
28 {
29 instance->OnTypeChange(hwnd);
30 return 1;
31 }
32 }
33 }
34 }
35 return 0;
36 }
38 static UINT_PTR CALLBACK HookFuncOldStyle(HWND hwnd,
39 UINT msg,
40 WPARAM wParam,
41 LPARAM lParam)
42 {
43 if (instance)
44 {
45 if (msg == WM_COMMAND)
46 {
47 if (HIWORD(wParam) == CBN_SELCHANGE)
48 {
49 if (LOWORD(wParam) == cmb1)
50 {
51 // call method with combobox handle to keep
52 // behaviour there
53 instance->OnTypeChange((HWND)lParam);
54 return 1;
55 }
56 }
57 }
58 }
59 return 0;
60 }
62 /////////////////////////////////////////////////////////////////////////////
63 // FileDlg
65 //////////////////////////////////////////////////////////////////////
66 // Construction/Destruction
67 //////////////////////////////////////////////////////////////////////
69 FileDlg::FileDlg(CWnd *parent, LPCTSTR file, LPCTSTR filter,
70 int filterIndex, LPCTSTR ext, LPCTSTR *exts, LPCTSTR initialDir,
71 LPCTSTR title, bool save, bool noReadOnly)
72 {
73 OSVERSIONINFO info;
74 info.dwOSVersionInfoSize = sizeof(info);
75 GetVersionEx(&info);
76 m_file = file;
77 int size = sizeof(OPENFILENAME);
79 // avoid problems if OPENFILENAME is already defined with the extended fields
80 // needed for the enhanced open/save dialog
81 #if _WIN32_WINNT < 0x0500
82 if (info.dwPlatformId == VER_PLATFORM_WIN32_NT)
83 {
84 if (info.dwMajorVersion >= 5)
85 size = sizeof(OPENFILENAMEEX);
86 }
87 #endif
89 ZeroMemory(&m_ofn, sizeof(m_ofn));
90 m_ofn.lpstrFile = m_file.GetBuffer(MAX_PATH);
91 m_ofn.nMaxFile = MAX_PATH;
92 m_ofn.lStructSize = size;
93 m_ofn.hwndOwner = parent ? parent->GetSafeHwnd() : NULL;
94 m_ofn.nFilterIndex = filterIndex;
95 m_ofn.lpstrInitialDir = initialDir;
96 m_ofn.lpstrTitle = title;
97 m_ofn.lpstrDefExt = ext;
98 m_ofn.lpfnHook = HookFunc;
99 m_ofn.Flags = OFN_PATHMUSTEXIST | OFN_ENABLESIZING | OFN_ENABLEHOOK;
100 m_ofn.Flags |= OFN_EXPLORER;
101 if (noReadOnly)
102 m_ofn.Flags |= OFN_HIDEREADONLY;
103 m_filter = filter;
105 char *p = m_filter.GetBuffer(0);
107 while ((p = strchr(p, '|')) != NULL)
108 *p++ = 0;
109 m_ofn.lpstrFilter = m_filter;
111 if (theApp.videoOption == VIDEO_320x240)
112 {
113 m_ofn.lpTemplateName = MAKEINTRESOURCE(IDD_OPENDLG);
114 m_ofn.lpfnHook = HookFuncOldStyle;
115 m_ofn.Flags |= OFN_ENABLETEMPLATE;
116 m_ofn.Flags &= ~OFN_EXPLORER;
117 }
119 isSave = save;
120 extensions = exts;
122 instance = this;
123 }
125 FileDlg::~FileDlg()
126 {
127 instance = NULL;
128 }
130 void FileDlg::OnTypeChange(HWND hwnd)
131 {
132 HWND parent = GetParent(hwnd);
134 HWND fileNameControl = ::GetDlgItem(parent, cmb13);
136 if (fileNameControl == NULL)
137 fileNameControl = ::GetDlgItem(parent, edt1);
139 if (fileNameControl == NULL)
140 return;
142 CString filename;
143 GetWindowText(fileNameControl, filename.GetBuffer(MAX_PATH), MAX_PATH);
144 filename.ReleaseBuffer();
146 HWND typeControl = ::GetDlgItem(parent, cmb1);
148 ASSERT(typeControl != NULL);
150 int sel = ::SendMessage(typeControl, CB_GETCURSEL, 0, 0);
152 ASSERT(sel != -1);
154 LPCTSTR typeName = extensions[sel];
156 // sel could easily be an invalid index of extensions, so check for null guard
157 for(int i = 0; i <= sel; i++)
158 if(extensions[i] == NULL)
159 typeName = "";
161 if (filename.GetLength() == 0)
162 {
163 if(*typeName)
164 filename.Format("*%s", typeName);
165 }
166 else
167 {
168 int index = filename.Find('.');
169 if (index == -1)
170 {
171 filename = filename + typeName;
172 }
173 else
174 {
175 filename = filename.Left(index) + typeName;
176 }
177 }
178 SetWindowText(fileNameControl, filename);
179 }
181 int FileDlg::getFilterIndex()
182 {
183 return m_ofn.nFilterIndex;
184 }
186 int FileDlg::DoModal()
187 {
188 systemSoundClearBuffer();
189 BOOL res = isSave ? GetSaveFileName(&m_ofn) :
190 GetOpenFileName(&m_ofn);
192 return res ? IDOK : IDCANCEL;
193 }
195 LPCTSTR FileDlg::GetPathName()
196 {
197 return (LPCTSTR)m_file;
198 }