home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 July / CMCD0704.ISO / Software / Freeware / Utilitare / VisualBoyAdvance-1.7.2 / src / win32 / FileDlg.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2004-05-13  |  5.1 KB  |  198 lines

  1. // VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator.
  2. // Copyright (C) 1999-2003 Forgotten
  3. // Copyright (C) 2004 Forgotten and the VBA development team
  4.  
  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.
  14. //
  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.
  18.  
  19. // FileDlg.cpp: implementation of the FileDlg class.
  20. //
  21. //////////////////////////////////////////////////////////////////////
  22. #include "stdafx.h"
  23. #include <commdlg.h>
  24. #include <dlgs.h>
  25.  
  26. #include "VBA.h"
  27. #include "FileDlg.h"
  28. #include "../System.h"
  29. #include "resource.h"
  30.  
  31. #ifdef _DEBUG
  32. #define new DEBUG_NEW
  33. #undef THIS_FILE
  34. static char THIS_FILE[] = __FILE__;
  35. #endif
  36.  
  37. static FileDlg *instance = NULL;
  38.  
  39. static UINT_PTR CALLBACK HookFunc(HWND hwnd,
  40.                                   UINT msg,
  41.                                   WPARAM wParam,
  42.                                   LPARAM lParam)
  43. {
  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;
  51.         }
  52.       }
  53.     }
  54.   }
  55.   return 0;
  56. }
  57.  
  58. static UINT_PTR CALLBACK HookFuncOldStyle(HWND hwnd,
  59.                                           UINT msg,
  60.                                           WPARAM wParam,
  61.                                           LPARAM lParam)
  62. {
  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;
  71.         }
  72.       }
  73.     }
  74.   }
  75.   return 0;
  76. }
  77.  
  78. /////////////////////////////////////////////////////////////////////////////
  79. // FileDlg
  80.  
  81. //////////////////////////////////////////////////////////////////////
  82. // Construction/Destruction
  83. //////////////////////////////////////////////////////////////////////
  84.  
  85. FileDlg::FileDlg(CWnd *parent, LPCTSTR file, LPCTSTR filter,
  86.                  int filterIndex, LPCTSTR ext, LPCTSTR *exts, LPCTSTR initialDir, 
  87.                  LPCTSTR title, bool save)
  88. {
  89.   OSVERSIONINFO info;
  90.   info.dwOSVersionInfoSize = sizeof(info);
  91.   GetVersionEx(&info);
  92.   m_file = file;
  93.   int size = sizeof(OPENFILENAME);
  94.   
  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);
  101.   }
  102. #endif
  103.  
  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;
  117.   
  118.   char *p = m_filter.GetBuffer(0);
  119.   
  120.   while ((p = strchr(p, '|')) != NULL)
  121.     *p++ = 0;
  122.   m_ofn.lpstrFilter = m_filter;
  123.  
  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;
  129.   }
  130.  
  131.   isSave = save;
  132.   extensions = exts;
  133.  
  134.   instance = this;
  135. }
  136.  
  137. FileDlg::~FileDlg()
  138. {
  139.   instance = NULL;
  140. }
  141.  
  142. void FileDlg::OnTypeChange(HWND hwnd)
  143. {
  144.   HWND parent = GetParent(hwnd);
  145.  
  146.   HWND fileNameControl = ::GetDlgItem(parent, cmb13);
  147.  
  148.   if(fileNameControl == NULL)
  149.     fileNameControl = ::GetDlgItem(parent, edt1);
  150.  
  151.   if(fileNameControl == NULL)
  152.     return;
  153.   
  154.   CString filename;
  155.   GetWindowText(fileNameControl, filename.GetBuffer(MAX_PATH), MAX_PATH);
  156.   filename.ReleaseBuffer();
  157.  
  158.   HWND typeControl = ::GetDlgItem(parent, cmb1);
  159.  
  160.   ASSERT(typeControl != NULL);
  161.  
  162.   int sel = ::SendMessage(typeControl, CB_GETCURSEL, 0, 0);
  163.  
  164.   ASSERT(sel != -1);
  165.   
  166.   LPCTSTR typeName = extensions[sel];
  167.   
  168.   if(filename.GetLength() == 0) {
  169.     filename.Format("*%s", typeName);
  170.   } else {
  171.     int index = filename.Find('.');
  172.     if (index == -1) {
  173.       filename = filename + typeName;
  174.     } else {
  175.       filename = filename.Left(index) + typeName;
  176.     }
  177.   }
  178.   SetWindowText(fileNameControl, filename);
  179. }
  180.  
  181. int FileDlg::getFilterIndex()
  182. {
  183.   return m_ofn.nFilterIndex;
  184. }
  185.  
  186. int FileDlg::DoModal()
  187. {
  188.   BOOL res = isSave ? GetSaveFileName(&m_ofn) :
  189.     GetOpenFileName(&m_ofn);
  190.  
  191.   return res ? IDOK : IDCANCEL;
  192. }
  193.  
  194. LPCTSTR FileDlg::GetPathName()
  195. {
  196.   return (LPCTSTR)m_file;
  197. }
  198.