diff 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 diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/win32/FileDlg.cpp	Sat Mar 03 10:31:27 2012 -0600
     1.3 @@ -0,0 +1,199 @@
     1.4 +// FileDlg.cpp: implementation of the FileDlg class.
     1.5 +//
     1.6 +//////////////////////////////////////////////////////////////////////
     1.7 +//#define WINVER 0x0410 // windows 98 - just for this 1 file - just in case
     1.8 +#include "stdafx.h"
     1.9 +#include <commdlg.h>
    1.10 +#include <dlgs.h>
    1.11 +#include "resource.h"
    1.12 +#include "FileDlg.h"
    1.13 +#include "Sound.h"
    1.14 +#include "VBA.h"
    1.15 +
    1.16 +static FileDlg *instance = NULL;
    1.17 +
    1.18 +static UINT_PTR CALLBACK HookFunc(HWND hwnd,
    1.19 +                                  UINT msg,
    1.20 +                                  WPARAM wParam,
    1.21 +                                  LPARAM lParam)
    1.22 +{
    1.23 +	if (instance)
    1.24 +	{
    1.25 +		if (msg == WM_NOTIFY)
    1.26 +		{
    1.27 +			OFNOTIFY *notify = (OFNOTIFY *)lParam;
    1.28 +			if (notify)
    1.29 +			{
    1.30 +				if (notify->hdr.code == CDN_TYPECHANGE)
    1.31 +				{
    1.32 +					instance->OnTypeChange(hwnd);
    1.33 +					return 1;
    1.34 +				}
    1.35 +			}
    1.36 +		}
    1.37 +	}
    1.38 +	return 0;
    1.39 +}
    1.40 +
    1.41 +static UINT_PTR CALLBACK HookFuncOldStyle(HWND hwnd,
    1.42 +                                          UINT msg,
    1.43 +                                          WPARAM wParam,
    1.44 +                                          LPARAM lParam)
    1.45 +{
    1.46 +	if (instance)
    1.47 +	{
    1.48 +		if (msg == WM_COMMAND)
    1.49 +		{
    1.50 +			if (HIWORD(wParam) == CBN_SELCHANGE)
    1.51 +			{
    1.52 +				if (LOWORD(wParam) == cmb1)
    1.53 +				{
    1.54 +					// call method with combobox handle to keep
    1.55 +					// behaviour there
    1.56 +					instance->OnTypeChange((HWND)lParam);
    1.57 +					return 1;
    1.58 +				}
    1.59 +			}
    1.60 +		}
    1.61 +	}
    1.62 +	return 0;
    1.63 +}
    1.64 +
    1.65 +/////////////////////////////////////////////////////////////////////////////
    1.66 +// FileDlg
    1.67 +
    1.68 +//////////////////////////////////////////////////////////////////////
    1.69 +// Construction/Destruction
    1.70 +//////////////////////////////////////////////////////////////////////
    1.71 +
    1.72 +FileDlg::FileDlg(CWnd *parent, LPCTSTR file, LPCTSTR filter,
    1.73 +                 int filterIndex, LPCTSTR ext, LPCTSTR *exts, LPCTSTR initialDir,
    1.74 +                 LPCTSTR title, bool save, bool noReadOnly)
    1.75 +{
    1.76 +	OSVERSIONINFO info;
    1.77 +	info.dwOSVersionInfoSize = sizeof(info);
    1.78 +	GetVersionEx(&info);
    1.79 +	m_file = file;
    1.80 +	int size = sizeof(OPENFILENAME);
    1.81 +
    1.82 +	// avoid problems if OPENFILENAME is already defined with the extended fields
    1.83 +	// needed for the enhanced open/save dialog
    1.84 +#if _WIN32_WINNT < 0x0500
    1.85 +	if (info.dwPlatformId == VER_PLATFORM_WIN32_NT)
    1.86 +	{
    1.87 +		if (info.dwMajorVersion >= 5)
    1.88 +			size = sizeof(OPENFILENAMEEX);
    1.89 +	}
    1.90 +#endif
    1.91 +
    1.92 +	ZeroMemory(&m_ofn, sizeof(m_ofn));
    1.93 +	m_ofn.lpstrFile       = m_file.GetBuffer(MAX_PATH);
    1.94 +	m_ofn.nMaxFile        = MAX_PATH;
    1.95 +	m_ofn.lStructSize     = size;
    1.96 +	m_ofn.hwndOwner       = parent ? parent->GetSafeHwnd() : NULL;
    1.97 +	m_ofn.nFilterIndex    = filterIndex;
    1.98 +	m_ofn.lpstrInitialDir = initialDir;
    1.99 +	m_ofn.lpstrTitle      = title;
   1.100 +	m_ofn.lpstrDefExt     = ext;
   1.101 +	m_ofn.lpfnHook        = HookFunc;
   1.102 +	m_ofn.Flags  = OFN_PATHMUSTEXIST | OFN_ENABLESIZING | OFN_ENABLEHOOK;
   1.103 +	m_ofn.Flags |= OFN_EXPLORER;
   1.104 +	if (noReadOnly)
   1.105 +		m_ofn.Flags |= OFN_HIDEREADONLY;
   1.106 +	m_filter = filter;
   1.107 +
   1.108 +	char *p = m_filter.GetBuffer(0);
   1.109 +
   1.110 +	while ((p = strchr(p, '|')) != NULL)
   1.111 +		*p++ = 0;
   1.112 +	m_ofn.lpstrFilter = m_filter;
   1.113 +
   1.114 +	if (theApp.videoOption == VIDEO_320x240)
   1.115 +	{
   1.116 +		m_ofn.lpTemplateName = MAKEINTRESOURCE(IDD_OPENDLG);
   1.117 +		m_ofn.lpfnHook       = HookFuncOldStyle;
   1.118 +		m_ofn.Flags |= OFN_ENABLETEMPLATE;
   1.119 +		m_ofn.Flags &= ~OFN_EXPLORER;
   1.120 +	}
   1.121 +
   1.122 +	isSave     = save;
   1.123 +	extensions = exts;
   1.124 +
   1.125 +	instance = this;
   1.126 +}
   1.127 +
   1.128 +FileDlg::~FileDlg()
   1.129 +{
   1.130 +	instance = NULL;
   1.131 +}
   1.132 +
   1.133 +void FileDlg::OnTypeChange(HWND hwnd)
   1.134 +{
   1.135 +	HWND parent = GetParent(hwnd);
   1.136 +
   1.137 +	HWND fileNameControl = ::GetDlgItem(parent, cmb13);
   1.138 +
   1.139 +	if (fileNameControl == NULL)
   1.140 +		fileNameControl = ::GetDlgItem(parent, edt1);
   1.141 +
   1.142 +	if (fileNameControl == NULL)
   1.143 +		return;
   1.144 +
   1.145 +	CString filename;
   1.146 +	GetWindowText(fileNameControl, filename.GetBuffer(MAX_PATH), MAX_PATH);
   1.147 +	filename.ReleaseBuffer();
   1.148 +
   1.149 +	HWND typeControl = ::GetDlgItem(parent, cmb1);
   1.150 +
   1.151 +	ASSERT(typeControl != NULL);
   1.152 +
   1.153 +	int sel = ::SendMessage(typeControl, CB_GETCURSEL, 0, 0);
   1.154 +
   1.155 +	ASSERT(sel != -1);
   1.156 +
   1.157 +	LPCTSTR typeName = extensions[sel];
   1.158 +
   1.159 +	// sel could easily be an invalid index of extensions, so check for null guard
   1.160 +	for(int i = 0; i <= sel; i++)
   1.161 +		if(extensions[i] == NULL)
   1.162 +			typeName = "";
   1.163 +
   1.164 +	if (filename.GetLength() == 0)
   1.165 +	{
   1.166 +		if(*typeName)
   1.167 +			filename.Format("*%s", typeName);
   1.168 +	}
   1.169 +	else
   1.170 +	{
   1.171 +		int index = filename.Find('.');
   1.172 +		if (index == -1)
   1.173 +		{
   1.174 +			filename = filename + typeName;
   1.175 +		}
   1.176 +		else
   1.177 +		{
   1.178 +			filename = filename.Left(index) + typeName;
   1.179 +		}
   1.180 +	}
   1.181 +	SetWindowText(fileNameControl, filename);
   1.182 +}
   1.183 +
   1.184 +int FileDlg::getFilterIndex()
   1.185 +{
   1.186 +	return m_ofn.nFilterIndex;
   1.187 +}
   1.188 +
   1.189 +int FileDlg::DoModal()
   1.190 +{
   1.191 +	systemSoundClearBuffer();
   1.192 +	BOOL res = isSave ? GetSaveFileName(&m_ofn) :
   1.193 +	           GetOpenFileName(&m_ofn);
   1.194 +
   1.195 +	return res ? IDOK : IDCANCEL;
   1.196 +}
   1.197 +
   1.198 +LPCTSTR FileDlg::GetPathName()
   1.199 +{
   1.200 +	return (LPCTSTR)m_file;
   1.201 +}
   1.202 +