home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 July / CMCD0704.ISO / Software / Freeware / Utilitare / VisualBoyAdvance-1.7.2 / src / win32 / Directories.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2004-05-13  |  6.6 KB  |  258 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. // Directories.cpp : implementation file
  20. //
  21.  
  22. #include "stdafx.h"
  23. #include "vba.h"
  24. #include "Directories.h"
  25. #include "Reg.h"
  26. #include "WinResUtil.h"
  27.  
  28. #include <shlobj.h>
  29.  
  30. #ifdef _DEBUG
  31. #define new DEBUG_NEW
  32. #undef THIS_FILE
  33. static char THIS_FILE[] = __FILE__;
  34. #endif
  35.  
  36. /////////////////////////////////////////////////////////////////////////////
  37. // Directories dialog
  38.  
  39. static int CALLBACK browseCallbackProc(HWND hWnd, UINT msg,
  40.                                        LPARAM l, LPARAM data)
  41. {
  42.   char *buffer = (char *)data;
  43.   switch(msg) {
  44.   case BFFM_INITIALIZED:
  45.     if(buffer[0])
  46.       SendMessage(hWnd, BFFM_SETSELECTION, TRUE, (LPARAM)buffer);
  47.     break;
  48.   default:
  49.     break;
  50.   }
  51.   return 0;
  52. }
  53.  
  54. Directories::Directories(CWnd* pParent /*=NULL*/)
  55.   : CDialog(Directories::IDD, pParent)
  56. {
  57.   //{{AFX_DATA_INIT(Directories)
  58.   // NOTE: the ClassWizard will add member initialization here
  59.   //}}AFX_DATA_INIT
  60. }
  61.  
  62.  
  63. void Directories::DoDataExchange(CDataExchange* pDX)
  64. {
  65.   CDialog::DoDataExchange(pDX);
  66.   //{{AFX_DATA_MAP(Directories)
  67.   DDX_Control(pDX, IDC_SAVE_PATH, m_savePath);
  68.   DDX_Control(pDX, IDC_ROM_PATH, m_romPath);
  69.   DDX_Control(pDX, IDC_GBROM_PATH, m_gbromPath);
  70.   DDX_Control(pDX, IDC_CAPTURE_PATH, m_capturePath);
  71.   DDX_Control(pDX, IDC_BATTERY_PATH, m_batteryPath);
  72.   //}}AFX_DATA_MAP
  73. }
  74.  
  75.  
  76. BEGIN_MESSAGE_MAP(Directories, CDialog)
  77.   //{{AFX_MSG_MAP(Directories)
  78.   ON_BN_CLICKED(IDC_BATTERY_DIR, OnBatteryDir)
  79.   ON_BN_CLICKED(IDC_BATTERY_DIR_RESET, OnBatteryDirReset)
  80.   ON_BN_CLICKED(IDC_CAPTURE_DIR, OnCaptureDir)
  81.   ON_BN_CLICKED(IDC_CAPTURE_DIR_RESET, OnCaptureDirReset)
  82.   ON_BN_CLICKED(IDC_GBROM_DIR, OnGbromDir)
  83.   ON_BN_CLICKED(IDC_GBROM_DIR_RESET, OnGbromDirReset)
  84.   ON_BN_CLICKED(IDC_ROM_DIR, OnRomDir)
  85.   ON_BN_CLICKED(IDC_ROM_DIR_RESET, OnRomDirReset)
  86.   ON_BN_CLICKED(IDC_SAVE_DIR, OnSaveDir)
  87.   ON_BN_CLICKED(IDC_SAVE_DIR_RESET, OnSaveDirReset)
  88.   //}}AFX_MSG_MAP
  89.   END_MESSAGE_MAP()
  90.  
  91.   /////////////////////////////////////////////////////////////////////////////
  92. // Directories message handlers
  93.  
  94. BOOL Directories::OnInitDialog() 
  95. {
  96.   CDialog::OnInitDialog();
  97.   
  98.   CString p = regQueryStringValue("romdir", NULL);
  99.   if(!p.IsEmpty()) {
  100.     int len = p.GetLength();
  101.     if(len > 0)
  102.       if(p[len-1] == '\\')
  103.         p = p.Left(len-1);
  104.     GetDlgItem(IDC_ROM_PATH)->SetWindowText(p);
  105.   }
  106.   
  107.   p = regQueryStringValue("gbromdir", NULL);
  108.   if(!p.IsEmpty()) {
  109.     int len = p.GetLength();
  110.     if(len > 0)
  111.       if(p[len-1] == '\\')
  112.         p = p.Left(len-1);
  113.     GetDlgItem(IDC_GBROM_PATH)->SetWindowText(p);
  114.   }
  115.   
  116.   p = regQueryStringValue("batteryDir", NULL);
  117.   if(!p.IsEmpty())
  118.     GetDlgItem(IDC_BATTERY_PATH)->SetWindowText( p);
  119.   p = regQueryStringValue("saveDir", NULL);
  120.   if(!p.IsEmpty())
  121.     GetDlgItem(IDC_SAVE_PATH)->SetWindowText(p);
  122.   p = regQueryStringValue("captureDir", NULL);
  123.   if(!p.IsEmpty())
  124.     GetDlgItem(IDC_CAPTURE_PATH)->SetWindowText(p);
  125.   
  126.   CenterWindow();
  127.   
  128.   return TRUE;  // return TRUE unless you set the focus to a control
  129.                 // EXCEPTION: OCX Property Pages should return FALSE
  130. }
  131.  
  132. void Directories::OnBatteryDir() 
  133. {
  134.   m_batteryPath.GetWindowText(initialFolderDir);
  135.   CString p = browseForDir(winResLoadString(IDS_SELECT_BATTERY_DIR));
  136.   if(!p.IsEmpty())
  137.     m_batteryPath.SetWindowText(p);
  138. }
  139.  
  140. void Directories::OnBatteryDirReset() 
  141. {
  142.   regDeleteValue("batteryDir");
  143.   m_batteryPath.SetWindowText("");
  144. }
  145.  
  146. void Directories::OnCaptureDir() 
  147. {
  148.   m_capturePath.GetWindowText(initialFolderDir);
  149.   CString p = browseForDir(winResLoadString(IDS_SELECT_CAPTURE_DIR));
  150.   if(!p.IsEmpty())
  151.     m_capturePath.SetWindowText(p);
  152. }
  153.  
  154. void Directories::OnCaptureDirReset() 
  155. {
  156.   regDeleteValue("captureDir");
  157.   m_capturePath.SetWindowText("");  
  158. }
  159.  
  160. void Directories::OnGbromDir() 
  161. {
  162.   m_gbromPath.GetWindowText(initialFolderDir);
  163.   CString p = browseForDir(winResLoadString(IDS_SELECT_ROM_DIR));
  164.   if(!p.IsEmpty())
  165.     m_gbromPath.SetWindowText(p);
  166. }
  167.  
  168. void Directories::OnGbromDirReset() 
  169. {
  170.   regDeleteValue("gbromdir");
  171.   m_gbromPath.SetWindowText("");  
  172. }
  173.  
  174. void Directories::OnRomDir() 
  175. {
  176.   m_romPath.GetWindowText(initialFolderDir);
  177.   CString p = browseForDir(winResLoadString(IDS_SELECT_ROM_DIR));
  178.   if(!p.IsEmpty())
  179.     m_romPath.SetWindowText(p);
  180. }
  181.  
  182. void Directories::OnRomDirReset() 
  183. {
  184.   regDeleteValue("romdir");
  185.   m_romPath.SetWindowText("");
  186. }
  187.  
  188. void Directories::OnSaveDir() 
  189. {
  190.   m_savePath.GetWindowText(initialFolderDir);
  191.   CString p = browseForDir(winResLoadString(IDS_SELECT_SAVE_DIR));
  192.   if(!p.IsEmpty())
  193.     m_savePath.SetWindowText(p);
  194. }
  195.  
  196. void Directories::OnSaveDirReset() 
  197. {
  198.   regDeleteValue("saveDir");
  199.   m_savePath.SetWindowText("");  
  200. }
  201.  
  202. void Directories::OnCancel() 
  203. {
  204.   EndDialog(FALSE);
  205. }
  206.  
  207. void Directories::OnOK() 
  208. {
  209.   CString buffer;
  210.   m_romPath.GetWindowText(buffer);
  211.   if(!buffer.IsEmpty())
  212.     regSetStringValue("romdir", buffer);
  213.   m_gbromPath.GetWindowText(buffer);
  214.   if(!buffer.IsEmpty())
  215.     regSetStringValue("gbromdir", buffer);      
  216.   m_batteryPath.GetWindowText(buffer);
  217.   if(!buffer.IsEmpty())
  218.     regSetStringValue("batteryDir", buffer);
  219.   m_savePath.GetWindowText(buffer);
  220.   if(!buffer.IsEmpty())
  221.     regSetStringValue("saveDir", buffer);
  222.   m_capturePath.GetWindowText(buffer);
  223.   if(!buffer.IsEmpty())
  224.     regSetStringValue("captureDir", buffer);      
  225.   EndDialog(TRUE);
  226. }
  227.  
  228. CString Directories::browseForDir(CString title)
  229. {
  230.   static char buffer[1024];
  231.   LPMALLOC pMalloc;
  232.   LPITEMIDLIST pidl;
  233.   
  234.   CString res;
  235.   
  236.   if(SUCCEEDED(SHGetMalloc(&pMalloc))) {
  237.     BROWSEINFO bi;
  238.     ZeroMemory(&bi, sizeof(bi));
  239.     bi.hwndOwner = m_hWnd;
  240.     bi.lpszTitle = title;
  241.     bi.pidlRoot = 0;
  242.     bi.ulFlags = BIF_RETURNONLYFSDIRS;
  243.     bi.lpfn = browseCallbackProc;
  244.     bi.lParam = (LPARAM)(LPCTSTR)initialFolderDir;
  245.     
  246.     pidl = SHBrowseForFolder(&bi);
  247.     
  248.     if(pidl) {
  249.       if(SHGetPathFromIDList(pidl, buffer)) {
  250.         res = buffer;
  251.       }
  252.       pMalloc->Free(pidl);
  253.       pMalloc->Release();
  254.     }
  255.   }
  256.   return res;
  257. }
  258.