remove more unportable ASM, obsoleting C_CORE define
[rofl0r-VisualBoyAdvance.git] / src / win32 / FileDlg.cpp
blob14e30c9a93a693571942e8e9954ada5116bc7dd6
1 // VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator.
2 // Copyright (C) 1999-2003 Forgotten
3 // Copyright (C) 2005 Forgotten and the VBA development team
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2, or(at your option)
8 // any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software Foundation,
17 // Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 // FileDlg.cpp: implementation of the FileDlg class.
22 #include "stdafx.h"
23 #include <commdlg.h>
24 #include <dlgs.h>
26 #include "VBA.h"
27 #include "FileDlg.h"
28 #include "../System.h"
29 #include "resource.h"
31 #ifdef _DEBUG
32 #define new DEBUG_NEW
33 #undef THIS_FILE
34 static char THIS_FILE[] = __FILE__;
35 #endif
37 static FileDlg *instance = NULL;
39 static UINT_PTR CALLBACK HookFunc(HWND hwnd,
40 UINT msg,
41 WPARAM wParam,
42 LPARAM lParam)
44 if(instance) {
45 if(msg == WM_NOTIFY) {
46 OFNOTIFY *notify = (OFNOTIFY *)lParam;
47 if(notify) {
48 if(notify->hdr.code == CDN_TYPECHANGE) {
49 instance->OnTypeChange(hwnd);
50 return 1;
55 return 0;
58 static UINT_PTR CALLBACK HookFuncOldStyle(HWND hwnd,
59 UINT msg,
60 WPARAM wParam,
61 LPARAM lParam)
63 if(instance) {
64 if(msg == WM_COMMAND) {
65 if(HIWORD(wParam) == CBN_SELCHANGE) {
66 if(LOWORD(wParam) == cmb1) {
67 // call method with combobox handle to keep
68 // behaviour there
69 instance->OnTypeChange((HWND)lParam);
70 return 1;
75 return 0;
78 /////////////////////////////////////////////////////////////////////////////
79 // FileDlg
81 //////////////////////////////////////////////////////////////////////
82 // Construction/Destruction
83 //////////////////////////////////////////////////////////////////////
85 FileDlg::FileDlg(CWnd *parent, LPCTSTR file, LPCTSTR filter,
86 int filterIndex, LPCTSTR ext, LPCTSTR *exts, LPCTSTR initialDir,
87 LPCTSTR title, bool save)
89 OSVERSIONINFO info;
90 info.dwOSVersionInfoSize = sizeof(info);
91 GetVersionEx(&info);
92 m_file = file;
93 int size = sizeof(OPENFILENAME);
95 // avoid problems if OPENFILENAME is already defined with the extended fields
96 // needed for the enhanced open/save dialog
97 #if _WIN32_WINNT < 0x0500
98 if(info.dwPlatformId == VER_PLATFORM_WIN32_NT) {
99 if(info.dwMajorVersion >= 5)
100 size = sizeof(OPENFILENAMEEX);
102 #endif
104 ZeroMemory(&m_ofn, sizeof(m_ofn));
105 m_ofn.lpstrFile = m_file.GetBuffer(MAX_PATH);
106 m_ofn.nMaxFile = MAX_PATH;
107 m_ofn.lStructSize = size;
108 m_ofn.hwndOwner = parent ? parent->GetSafeHwnd() : NULL;
109 m_ofn.nFilterIndex = filterIndex;
110 m_ofn.lpstrInitialDir = initialDir;
111 m_ofn.lpstrTitle = title;
112 m_ofn.lpstrDefExt = ext;
113 m_ofn.lpfnHook = HookFunc;
114 m_ofn.Flags = OFN_PATHMUSTEXIST | OFN_ENABLESIZING | OFN_ENABLEHOOK;
115 m_ofn.Flags |= OFN_EXPLORER;
116 m_filter = filter;
118 char *p = m_filter.GetBuffer(0);
120 while ((p = strchr(p, '|')) != NULL)
121 *p++ = 0;
122 m_ofn.lpstrFilter = m_filter;
124 if(theApp.videoOption == VIDEO_320x240) {
125 m_ofn.lpTemplateName = MAKEINTRESOURCE(IDD_OPENDLG);
126 m_ofn.lpfnHook = HookFuncOldStyle;
127 m_ofn.Flags |= OFN_ENABLETEMPLATE;
128 m_ofn.Flags &= ~OFN_EXPLORER;
131 isSave = save;
132 extensions = exts;
134 instance = this;
137 FileDlg::~FileDlg()
139 instance = NULL;
142 void FileDlg::OnTypeChange(HWND hwnd)
144 HWND parent = GetParent(hwnd);
146 HWND fileNameControl = ::GetDlgItem(parent, cmb13);
148 if(fileNameControl == NULL)
149 fileNameControl = ::GetDlgItem(parent, edt1);
151 if(fileNameControl == NULL)
152 return;
154 CString filename;
155 GetWindowText(fileNameControl, filename.GetBuffer(MAX_PATH), MAX_PATH);
156 filename.ReleaseBuffer();
158 HWND typeControl = ::GetDlgItem(parent, cmb1);
160 ASSERT(typeControl != NULL);
162 LRESULT sel = ::SendMessage(typeControl, CB_GETCURSEL, 0, 0);
164 ASSERT(sel != -1);
166 LPCTSTR typeName = extensions[sel];
168 if(filename.GetLength() == 0) {
169 if(strlen(typeName) != 0)
170 filename.Format("*%s", typeName);
171 } else {
172 if(strlen(typeName) != 0) {
173 int index = filename.Find('.');
174 if (index == -1) {
175 filename = filename + typeName;
176 } else {
177 filename = filename.Left(index) + typeName;
181 SetWindowText(fileNameControl, filename);
184 int FileDlg::getFilterIndex()
186 return m_ofn.nFilterIndex;
189 int FileDlg::DoModal()
191 BOOL res = isSave ? GetSaveFileName(&m_ofn) :
192 GetOpenFileName(&m_ofn);
194 return res ? IDOK : IDCANCEL;
197 LPCTSTR FileDlg::GetPathName()
199 return (LPCTSTR)m_file;