annotate src/win32/FileDlg.cpp @ 5:8fe0c57e53d2

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