home *** CD-ROM | disk | FTP | other *** search
/ Xentax forum attachments archive / xentax.7z / 4819 / rage.7z / stdafx.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2011-10-19  |  20.7 KB  |  832 lines

  1. #include "stdafx.h"
  2.  
  3. #define DEBUG_ME 0
  4.  
  5. float float_16_to_32(unsigned short value)
  6. {
  7.  const unsigned short s = (value & 0x8000);
  8.  const unsigned short e = (value & 0x7C00) >> 10;
  9.  const unsigned short m = (value & 0x03FF);
  10.  const float sgn = (s ? -1.0f : 1.0f);
  11.  
  12.  // ok
  13.  if(e == 0) {
  14.     if(m == 0) return sgn*0.0f;
  15.     else return sgn*powf(2, -14.0f)*((float)m/1024.0f);
  16.    } 
  17.  if(e < 32) return sgn*powf(2.0f, (float)e - 15.0f)*(1.0f + ((float)m/1024.0f));
  18.  
  19.  // ouch!
  20.  if(m == 0) return std::numeric_limits<float>::quiet_NaN();
  21.  return std::numeric_limits<float>::quiet_NaN();
  22. }
  23.  
  24. bool error(const char* message)
  25. {
  26.  cout << message << endl;
  27.  return false;
  28. }
  29.  
  30. __int16 LE_read_sint16(ifstream& ifile)
  31. {
  32.  short temp;
  33.  ifile.read((char*)&temp, sizeof(temp));
  34.  if(DEBUG_ME) cout << temp << endl;
  35.  return temp;
  36. }
  37.  
  38. __int32 LE_read_sint32(ifstream& ifile)
  39. {
  40.  int temp;
  41.  ifile.read((char*)&temp, sizeof(temp));
  42.  if(DEBUG_ME) cout << temp << endl;
  43.  return temp;
  44. }
  45.  
  46. unsigned __int8 LE_read_uint08(ifstream& ifile)
  47. {
  48.  unsigned char temp;
  49.  ifile.read((char*)&temp, sizeof(temp));
  50.  if(DEBUG_ME) cout << temp << endl;
  51.  return temp;
  52. }
  53.  
  54. unsigned __int16 LE_read_uint16(ifstream& ifile)
  55. {
  56.  unsigned short temp;
  57.  ifile.read((char*)&temp, sizeof(temp));
  58.  if(DEBUG_ME) cout << temp << endl;
  59.  return temp;
  60. }
  61.  
  62. unsigned __int32 LE_read_uint32(ifstream& ifile)
  63. {
  64.  unsigned int temp;
  65.  ifile.read((char*)&temp, sizeof(temp));
  66.  if(DEBUG_ME) cout << temp << endl;
  67.  return temp;
  68. }
  69.  
  70. unsigned __int64 LE_read_uint64(ifstream& ifile)
  71. {
  72.  unsigned __int64 temp;
  73.  ifile.read((char*)&temp, sizeof(temp));
  74.  if(DEBUG_ME) cout << temp << endl;
  75.  return temp;
  76. }
  77.  
  78. unsigned __int8 BE_read_uint08(ifstream& ifile)
  79. {
  80.  unsigned __int8 temp;
  81.  ifile.read((char*)&temp, sizeof(temp));
  82.  if(DEBUG_ME) cout << (int)temp << endl;
  83.  return temp;
  84. }
  85.  
  86. unsigned __int16 BE_read_uint16(ifstream& ifile)
  87. {
  88.  unsigned short temp;
  89.  ifile.read((char*)&temp, sizeof(temp));
  90.  reverse_byte_order(&temp);
  91.  if(DEBUG_ME) cout << temp << endl;
  92.  return temp;
  93. }
  94.  
  95. unsigned __int32 BE_read_uint32(ifstream& ifile)
  96. {
  97.  unsigned __int32 temp;
  98.  ifile.read((char*)&temp, sizeof(temp));
  99.  reverse_byte_order(&temp);
  100.  if(DEBUG_ME) cout << temp << endl;
  101.  return temp;
  102. }
  103.  
  104. unsigned __int64 BE_read_uint64(ifstream& ifile)
  105. {
  106.  unsigned __int64 temp;
  107.  ifile.read((char*)&temp, sizeof(temp));
  108.  reverse_byte_order(&temp);
  109.  if(DEBUG_ME) cout << temp << endl;
  110.  return temp;
  111. }
  112.  
  113. unsigned __int8 LE_read_uint08(ifstream& ifile, unsigned int offset)
  114. {
  115.  ifile.seekg(offset);
  116.  unsigned char temp;
  117.  ifile.read((char*)&temp, sizeof(temp));
  118.  if(DEBUG_ME) cout << (int)temp << endl;
  119.  return temp;
  120. }
  121.  
  122. unsigned __int16 LE_read_uint16(ifstream& ifile, unsigned int offset)
  123. {
  124.  ifile.seekg(offset);
  125.  unsigned short temp;
  126.  ifile.read((char*)&temp, sizeof(temp));
  127.  if(DEBUG_ME) cout << temp << endl;
  128.  return temp;
  129. }
  130.  
  131. unsigned __int32 LE_read_uint32(ifstream& ifile, unsigned int offset)
  132. {
  133.  ifile.seekg(offset);
  134.  unsigned int temp;
  135.  ifile.read((char*)&temp, sizeof(temp));
  136.  if(DEBUG_ME) cout << temp << endl;
  137.  return temp;
  138. }
  139.  
  140. unsigned __int64 LE_read_uint64(ifstream& ifile, unsigned int offset)
  141. {
  142.  ifile.seekg(offset);
  143.  unsigned __int64 temp;
  144.  ifile.read((char*)&temp, sizeof(temp));
  145.  if(DEBUG_ME) cout << temp << endl;
  146.  return temp;
  147. }
  148.  
  149. unsigned __int8 BE_read_uint08(ifstream& ifile, unsigned int offset)
  150. {
  151.  ifile.seekg(offset);
  152.  unsigned __int8 temp;
  153.  ifile.read((char*)&temp, sizeof(temp));
  154.  if(DEBUG_ME) cout << (int)temp << endl;
  155.  return temp;
  156. }
  157.  
  158. unsigned __int16 BE_read_uint16(ifstream& ifile, unsigned int offset)
  159. {
  160.  ifile.seekg(offset);
  161.  unsigned short temp;
  162.  ifile.read((char*)&temp, sizeof(temp));
  163.  reverse_byte_order(&temp);
  164.  if(DEBUG_ME) cout << temp << endl;
  165.  return temp;
  166. }
  167.  
  168. unsigned __int32 BE_read_uint32(ifstream& ifile, unsigned int offset)
  169. {
  170.  ifile.seekg(offset);
  171.  unsigned __int32 temp;
  172.  ifile.read((char*)&temp, sizeof(temp));
  173.  reverse_byte_order(&temp);
  174.  if(DEBUG_ME) cout << temp << endl;
  175.  return temp;
  176. }
  177.  
  178. unsigned __int64 BE_read_uint64(ifstream& ifile, unsigned int offset)
  179. {
  180.  ifile.seekg(offset);
  181.  unsigned __int64 temp;
  182.  ifile.read((char*)&temp, sizeof(temp));
  183.  reverse_byte_order(&temp);
  184.  if(DEBUG_ME) cout << temp << endl;
  185.  return temp;
  186. }
  187.  
  188. signed __int16 BE_read_sint16(ifstream& ifile)
  189. {
  190.  signed __int16 temp;
  191.  ifile.read((char*)&temp, sizeof(temp));
  192.  reverse_byte_order(&temp);
  193.  if(DEBUG_ME) cout << temp << endl;
  194.  return temp;
  195. }
  196.  
  197. float BE_read_half_float(ifstream& ifile)
  198. {
  199.  unsigned short temp;
  200.  ifile.read((char*)&temp, sizeof(temp));
  201.  reverse_byte_order(&temp);
  202.  if(DEBUG_ME) cout << float_16_to_32(temp) << endl;
  203.  return float_16_to_32(temp);
  204. }
  205.  
  206. float LE_read_half_float(ifstream& ifile)
  207. {
  208.  unsigned short temp;
  209.  ifile.read((char*)&temp, sizeof(temp));
  210.  if(DEBUG_ME) cout << float_16_to_32(temp) << endl;
  211.  return float_16_to_32(temp);
  212. }
  213.  
  214. float LE_read_float(ifstream& ifile)
  215. {
  216.  float temp;
  217.  ifile.read((char*)&temp, sizeof(temp));
  218.  if(DEBUG_ME) cout << temp << endl;
  219.  return temp;
  220. }
  221.  
  222. float BE_read_float(ifstream& ifile)
  223. {
  224.  float temp;
  225.  ifile.read((char*)&temp, sizeof(temp));
  226.  reverse_byte_order(&temp);
  227.  if(DEBUG_ME) cout << temp << endl;
  228.  return temp;
  229. }
  230.  
  231. double LE_read_double(ifstream& ifile)
  232. {
  233.  double temp;
  234.  ifile.read((char*)&temp, sizeof(temp));
  235.  if(DEBUG_ME) cout << temp << endl;
  236.  return temp;
  237. }
  238.  
  239. double BE_read_double(ifstream& ifile)
  240. {
  241.  double temp;
  242.  ifile.read((char*)&temp, sizeof(temp));
  243.  reverse_byte_order(&temp);
  244.  if(DEBUG_ME) cout << temp << endl;
  245.  return temp;
  246. }
  247.  
  248. float LE_read_float(ifstream& ifile, unsigned int offset)
  249. {
  250.  ifile.seekg(offset);
  251.  float temp;
  252.  ifile.read((char*)&temp, sizeof(temp));
  253.  if(DEBUG_ME) cout << temp << endl;
  254.  return temp;
  255. }
  256.  
  257. float BE_read_float(ifstream& ifile, unsigned int offset)
  258. {
  259.  ifile.seekg(offset);
  260.  float temp;
  261.  ifile.read((char*)&temp, sizeof(temp));
  262.  reverse_byte_order(&temp);
  263.  if(DEBUG_ME) cout << temp << endl;
  264.  return temp;
  265. }
  266.  
  267. double LE_read_double(ifstream& ifile, unsigned int offset)
  268. {
  269.  ifile.seekg(offset);
  270.  double temp;
  271.  ifile.read((char*)&temp, sizeof(temp));
  272.  if(DEBUG_ME) cout << temp << endl;
  273.  return temp;
  274. }
  275.  
  276. double BE_read_double(ifstream& ifile, unsigned int offset)
  277. {
  278.  ifile.seekg(offset);
  279.  double temp;
  280.  ifile.read((char*)&temp, sizeof(temp));
  281.  reverse_byte_order(&temp);
  282.  if(DEBUG_ME) cout << temp << endl;
  283.  return temp;
  284. }
  285.  
  286. bool get_models_filename_list(deque<string>& namelist, const char* fext)
  287. {
  288.  // get path of application
  289.  char c_rootname[MAX_PATH];
  290.  GetModuleFileName(0, c_rootname, MAX_PATH);
  291.  
  292.  // split the pathname
  293.  char c_drive[1024];
  294.  char c_dir[1024];
  295.  _splitpath(c_rootname, c_drive, c_dir, nullptr, nullptr);
  296.  strlwr(c_drive);
  297.  strlwr(c_dir);
  298.  
  299.  // build pathname
  300.  string s_rootname(c_drive);
  301.  s_rootname += string(c_dir);
  302.  
  303.  // build directory list
  304.  deque<string> templist;
  305.  templist.insert(templist.end(), s_rootname);
  306.  
  307.  // transfer directories with EXT files
  308.  deque<string> rootlist;
  309.  for(size_t i = 0; i < templist.size(); i++)
  310.     {
  311.      find_file file;
  312.      bool added = false;
  313.      string path(templist[i]);
  314.      path += "*";
  315.      path += fext;
  316.      if(file.find(path.c_str())) {
  317.         rootlist.insert(rootlist.end(), templist[i]);
  318.         added = true;
  319.        }
  320.     }
  321.  
  322.  // erase the temporary list
  323.  templist.erase(templist.begin(), templist.end());
  324.  if(rootlist.empty()) return error("No files found in current directory or subdirectories.");
  325.  
  326.  // build namelist
  327.  namelist.clear();
  328.  for(size_t i = 0; i < rootlist.size(); i++)
  329.     {
  330.      // clear templist
  331.      find_file file;
  332.      templist.erase(templist.begin(), templist.end());
  333.  
  334.      // add TID files
  335.      string path(rootlist[i]);
  336.      path += "*";
  337.      path += fext;
  338.      if(file.find(path.c_str())) {
  339.         string temp(rootlist[i]);
  340.         temp += file.filename();
  341.         templist.insert(templist.end(), temp);
  342.        }
  343.      while(file.next()) {
  344.         string temp(rootlist[i]);
  345.         temp += file.filename();
  346.         templist.insert(templist.end(), temp);
  347.        }
  348.  
  349.      // must have something in templist
  350.      file.close();
  351.      if(!templist.size()) return error("No files found in root directory or subdirectories.");
  352.  
  353.      // sort temporary list and append to the namelist
  354.      sort(templist.begin(), templist.end());
  355.      namelist.insert(namelist.end(), templist.begin(), templist.end());
  356.     }
  357.  
  358.  // must have something in namelist
  359.  templist.erase(templist.begin(), templist.end());
  360.  if(!namelist.size()) return error("No files found in root directory or subdirectories.");
  361.  
  362.  return true;
  363. }
  364.  
  365. bool get_motion_filename_list(deque<string>& namelist, const char* fext)
  366. {
  367.  // get path of application
  368.  char c_rootname[MAX_PATH];
  369.  GetModuleFileName(0, c_rootname, MAX_PATH);
  370.  
  371.  // split the pathname
  372.  char c_drive[1024];
  373.  char c_dir[1024];
  374.  _splitpath(c_rootname, c_drive, c_dir, nullptr, nullptr);
  375.  strlwr(c_drive);
  376.  strlwr(c_dir);
  377.  
  378.  // build pathname
  379.  string s_rootname(c_drive);
  380.  s_rootname += string(c_dir);
  381.  s_rootname += "motion\\";
  382.  
  383.  // build directory list
  384.  deque<string> templist;
  385.  templist.insert(templist.end(), s_rootname);
  386.  
  387.  size_t i = 0;
  388.  for(;;)
  389.     {
  390.      // get wildcard
  391.      find_file file;
  392.      string path(templist[i]);
  393.      path += "*.*";
  394.      file.find(path.c_str());
  395.      if(!file) return error("Could not build wildcard path.");
  396.  
  397.      // insert directories
  398.      if(file.is_directory() && !file.is_dots() && !file.is_system()) {
  399.         string path(templist[i]);
  400.         path += string(file.filename());
  401.         path += string("\\");
  402.         templist.insert(templist.end(), path);
  403.        }
  404.  
  405.      while(file.next()) {
  406.         if(file.is_directory() && !file.is_dots() && !file.is_system()) {
  407.            string path(templist[i]);
  408.            path += string(file.filename());
  409.            path += string("\\");
  410.            templist.insert(templist.end(), path);
  411.           }
  412.        }
  413.  
  414.      file.close();
  415.  
  416.      // update index
  417.      if(++i == templist.size())
  418.         break;
  419.     }
  420.  
  421.  // transfer directories with EXT files
  422.  deque<string> rootlist;
  423.  for(size_t i = 0; i < templist.size(); i++)
  424.     {
  425.      find_file file;
  426.      bool added = false;
  427.      string path(templist[i]);
  428.      path += "*";
  429.      path += fext;
  430.      if(file.find(path.c_str())) {
  431.         rootlist.insert(rootlist.end(), templist[i]);
  432.         added = true;
  433.        }
  434.     }
  435.  
  436.  // erase the temporary list
  437.  templist.erase(templist.begin(), templist.end());
  438.  if(rootlist.empty()) return error("No files found in current directory or subdirectories.");
  439.  
  440.  // build namelist
  441.  namelist.clear();
  442.  for(size_t i = 0; i < rootlist.size(); i++)
  443.     {
  444.      // clear templist
  445.      find_file file;
  446.      templist.erase(templist.begin(), templist.end());
  447.  
  448.      // add TID files
  449.      string path(rootlist[i]);
  450.      path += "*";
  451.      path += fext;
  452.      if(file.find(path.c_str())) {
  453.         string temp(rootlist[i]);
  454.         temp += file.filename();
  455.         templist.insert(templist.end(), temp);
  456.        }
  457.      while(file.next()) {
  458.         string temp(rootlist[i]);
  459.         temp += file.filename();
  460.         templist.insert(templist.end(), temp);
  461.        }
  462.  
  463.      // must have something in templist
  464.      file.close();
  465.      if(!templist.size()) return error("No files found in root directory or subdirectories.");
  466.  
  467.      // sort temporary list and append to the namelist
  468.      sort(templist.begin(), templist.end());
  469.      namelist.insert(namelist.end(), templist.begin(), templist.end());
  470.     }
  471.  
  472.  // must have something in namelist
  473.  templist.erase(templist.begin(), templist.end());
  474.  if(!namelist.size()) return error("No files found in root directory or subdirectories.");
  475.  
  476.  return true;
  477. }
  478.  
  479. bool get_filename_list(deque<string>& namelist, const char* fext)
  480. {
  481.  // get path of application
  482.  char c_rootname[MAX_PATH];
  483.  GetModuleFileName(0, c_rootname, MAX_PATH);
  484.  
  485.  // split the pathname
  486.  char c_drive[1024];
  487.  char c_dir[1024];
  488.  _splitpath(c_rootname, c_drive, c_dir, nullptr, nullptr);
  489.  strlwr(c_drive);
  490.  strlwr(c_dir);
  491.  
  492.  // build pathname
  493.  string s_rootname(c_drive);
  494.  s_rootname += string(c_dir);
  495.  
  496.  // build directory list
  497.  deque<string> templist;
  498.  templist.insert(templist.end(), s_rootname);
  499.  
  500.  size_t i = 0;
  501.  for(;;)
  502.     {
  503.      // get wildcard
  504.      find_file file;
  505.      string path(templist[i]);
  506.      path += "*.*";
  507.      file.find(path.c_str());
  508.      if(!file) return error("Could not build wildcard path.");
  509.  
  510.      // insert directories
  511.      if(file.is_directory() && !file.is_dots() && !file.is_system()) {
  512.         string path(templist[i]);
  513.         path += string(file.filename());
  514.         path += string("\\");
  515.         templist.insert(templist.end(), path);
  516.        }
  517.  
  518.      while(file.next()) {
  519.         if(file.is_directory() && !file.is_dots() && !file.is_system()) {
  520.            string path(templist[i]);
  521.            path += string(file.filename());
  522.            path += string("\\");
  523.            templist.insert(templist.end(), path);
  524.           }
  525.        }
  526.  
  527.      file.close();
  528.  
  529.      // update index
  530.      if(++i == templist.size())
  531.         break;
  532.     }
  533.  
  534.  // transfer directories with EXT files
  535.  deque<string> rootlist;
  536.  for(size_t i = 0; i < templist.size(); i++)
  537.     {
  538.      find_file file;
  539.      bool added = false;
  540.      string path(templist[i]);
  541.      path += "*";
  542.      path += fext;
  543.      if(file.find(path.c_str())) {
  544.         rootlist.insert(rootlist.end(), templist[i]);
  545.         added = true;
  546.        }
  547.     }
  548.  
  549.  // erase the temporary list
  550.  templist.erase(templist.begin(), templist.end());
  551.  if(rootlist.empty()) return error("No files found in current directory or subdirectories.");
  552.  
  553.  // build namelist
  554.  namelist.clear();
  555.  for(size_t i = 0; i < rootlist.size(); i++)
  556.     {
  557.      // clear templist
  558.      find_file file;
  559.      templist.erase(templist.begin(), templist.end());
  560.  
  561.      // add TID files
  562.      string path(rootlist[i]);
  563.      path += "*";
  564.      path += fext;
  565.      if(file.find(path.c_str())) {
  566.         string temp(rootlist[i]);
  567.         temp += file.filename();
  568.         templist.insert(templist.end(), temp);
  569.        }
  570.      while(file.next()) {
  571.         string temp(rootlist[i]);
  572.         temp += file.filename();
  573.         templist.insert(templist.end(), temp);
  574.        }
  575.  
  576.      // must have something in templist
  577.      file.close();
  578.      if(!templist.size()) return error("No files found in root directory or subdirectories.");
  579.  
  580.      // sort temporary list and append to the namelist
  581.      sort(templist.begin(), templist.end());
  582.      namelist.insert(namelist.end(), templist.begin(), templist.end());
  583.     }
  584.  
  585.  // must have something in namelist
  586.  templist.erase(templist.begin(), templist.end());
  587.  if(!namelist.size()) return error("No files found in root directory or subdirectories.");
  588.  
  589.  return true;
  590. }
  591.  
  592. find_file::find_file() : handle(0)
  593. {
  594.  ZeroMemory(&find32, sizeof(find32));
  595. }
  596.  
  597. find_file::~find_file()
  598. {
  599.  close();
  600. }
  601.  
  602. void find_file::close(void)
  603. {
  604.  // must check for valid handle in 
  605.  // VC++ .NET or else exception results
  606.  if(handle) FindClose(handle);
  607.  handle = 0;
  608.  ZeroMemory((void*)&find32, sizeof(find32)); 
  609. }
  610.  
  611. bool find_file::find(const char* filename)
  612. {
  613.  close();
  614.  handle = FindFirstFile(filename, &find32);
  615.  if(handle == INVALID_HANDLE_VALUE) handle = 0;
  616.  return (handle != 0);
  617. }
  618.  
  619. bool find_file::next(void)
  620. {
  621.  if(handle == 0) return false;
  622.  return (FindNextFile(handle, &find32) == TRUE);
  623. }
  624.  
  625. bool find_file::is_dots(void)
  626. {
  627.  return (!strcmp(find32.cFileName, ".") || !strcmp(find32.cFileName, ".."));
  628. }
  629.  
  630. bool find_file::is_directory(void)
  631. {
  632.  return ((find32.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0);
  633. }
  634.  
  635. bool find_file::is_encrypted(void)
  636. {
  637.  return ((find32.dwFileAttributes & FILE_ATTRIBUTE_ENCRYPTED) != 0);
  638. }
  639.  
  640. bool find_file::is_compressed(void)
  641. {
  642.  return ((find32.dwFileAttributes & FILE_ATTRIBUTE_COMPRESSED) != 0);
  643. }
  644.  
  645. bool find_file::is_normal(void)
  646. {
  647.  return ((find32.dwFileAttributes & FILE_ATTRIBUTE_NORMAL) != 0);
  648. }
  649.  
  650. bool find_file::is_hidden(void)
  651. {
  652.  return ((find32.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) != 0);
  653. }
  654.  
  655. bool find_file::is_system(void)
  656. {
  657.  return ((find32.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM) != 0);
  658. }
  659.  
  660. bool find_file::is_archived(void)
  661. {
  662.  return ((find32.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE) != 0);
  663. }
  664.  
  665. bool find_file::is_readonly(void)
  666. {
  667.  return ((find32.dwFileAttributes & FILE_ATTRIBUTE_READONLY) != 0);
  668. }
  669.  
  670. const char* find_file::filename(void)
  671. {
  672.  strlwr(find32.cFileName);
  673.  return (const char*)find32.cFileName;
  674. }
  675.  
  676. size_t find_file::filesize(void)
  677. {
  678.  return (find32.nFileSizeHigh*(MAXDWORD + 1)) + find32.nFileSizeLow;
  679. }
  680.  
  681. bool find_file::operator !(void)const
  682. {
  683.  return !handle;
  684. }
  685.  
  686. // Center Dialog
  687. void CenterDialog(HWND window, bool in_parent)
  688. {
  689.  HWND dialog = window;
  690.  HWND parent = ((in_parent && GetParent(dialog)) ? GetParent(dialog) : GetDesktopWindow());
  691.  
  692.  RECT rect1; GetWindowRect(dialog, &rect1);
  693.  RECT rect2; GetWindowRect(parent, &rect2);
  694.  
  695.  int dx1 = rect1.right - rect1.left;
  696.  int dx2 = rect2.right - rect2.left;
  697.  
  698.  int dy1 = rect1.bottom - rect1.top;
  699.  int dy2 = rect2.bottom - rect2.top;
  700.  
  701.  int x = (dx2 - dx1)/2 + rect1.left;
  702.  int y = (dy2 - dy1)/2 + rect1.top;
  703.  
  704.  MoveWindow(dialog, x, y, dx1, dy1, TRUE);
  705. }
  706.  
  707. // Browse Directory Dialog
  708. int CALLBACK BrowseDirectoryDialogProc(HWND window, UINT message, LPARAM lparam, LPARAM)
  709. {
  710.  char directory[MAX_PATH];
  711.  switch(message) {
  712.    case(BFFM_INITIALIZED) : {
  713.         if(GetCurrentDirectory(sizeof(directory)/sizeof(char), directory))
  714.            SendMessage(window, BFFM_SETSELECTION,  1, (LPARAM)directory);
  715.         break;
  716.        }
  717.    case(BFFM_SELCHANGED) : {
  718.         if(SHGetPathFromIDList((LPITEMIDLIST)lparam ,directory))
  719.            SendMessage(window, BFFM_SETSTATUSTEXT, 0, (LPARAM)directory);
  720.         break;
  721.        }
  722.   }
  723.  return 0;
  724. }
  725.  
  726. BOOL BrowseDirectoryDialog(HWND parent, const char* caption, char* buffer)
  727. {
  728.  char display_name[MAX_PATH];
  729.  display_name[0] = '\0';
  730.  
  731.  BROWSEINFO bi = {
  732.   parent,
  733.   0,
  734.   display_name,
  735.   caption,
  736.   BIF_RETURNONLYFSDIRS,
  737.   BrowseDirectoryDialogProc,
  738.   0
  739.  };
  740.  
  741.  ITEMIDLIST* pIIL = ::SHBrowseForFolder(&bi);
  742.  
  743.  if(SHGetPathFromIDList(pIIL, buffer)) {
  744.     LPMALLOC pMalloc;
  745.     HRESULT hr = SHGetMalloc(&pMalloc);
  746.     pMalloc->Free(pIIL);
  747.     pMalloc->Release();
  748.     return TRUE;
  749.    }
  750.  
  751.  return FALSE;
  752. }
  753.  
  754. // Color Dialog
  755. BOOL ColorDialog(HWND parent, COLORREF& color)
  756. {
  757.  static COLORREF custom[16] = {
  758.   RGB(  0,  0,  0), RGB(255,255,255), RGB(128,128,128), RGB(192,192,192),
  759.   RGB(128,  0,  0), RGB(  0,128,  0), RGB(  0,  0,128), RGB(128,128,  0),
  760.   RGB(128,  0,128), RGB(  0,128,128), RGB(255, 255, 0), RGB(255,  0,255),
  761.   RGB(  0,255,255), RGB(255,  0,  0), RGB(  0,255,  0), RGB(  0,  0,255)
  762.  };
  763.  CHOOSECOLOR data;
  764.  ZeroMemory(&data, sizeof(data));
  765.  data.lStructSize    = sizeof(CHOOSECOLOR);
  766.  data.hwndOwner      = parent;
  767.  data.hInstance      = NULL;
  768.  data.rgbResult      = color;
  769.  data.lpCustColors   = custom;
  770.  data.Flags          = CC_RGBINIT | CC_FULLOPEN;
  771.  data.lCustData      = 0;
  772.  data.lpfnHook       = NULL;
  773.  data.lpTemplateName = NULL;
  774.  if(ChooseColor(&data) == FALSE) return FALSE; 
  775.  else color = data.rgbResult;
  776.  return TRUE;
  777. }
  778.  
  779. // OpenSave Dialogs
  780. BOOL OpenFileDialog(HWND parent, const char* filter, const char* title, const char* defext, char* filename, char* initdir)
  781. {
  782.  char buffer1[MAX_PATH];
  783.  char buffer2[MAX_PATH];
  784.  buffer1[0] = '\0';
  785.  buffer2[0] = '\0';
  786.  
  787.  OPENFILENAME ofn;
  788.  ZeroMemory(&ofn, sizeof(ofn));
  789.  ofn.lStructSize     = sizeof(ofn);
  790.  ofn.hwndOwner       = parent;
  791.  ofn.hInstance       = (HINSTANCE)GetModuleHandle(0);
  792.  ofn.lpstrFilter     = filter;
  793.  ofn.lpstrFile       = buffer1;
  794.  ofn.nMaxFile        = MAX_PATH;
  795.  ofn.lpstrFileTitle  = buffer2;
  796.  ofn.nMaxFileTitle   = MAX_PATH;
  797.  ofn.lpstrInitialDir = initdir;
  798.  ofn.lpstrTitle      = title;
  799.  ofn.Flags           = OFN_FILEMUSTEXIST;
  800.  ofn.lpstrDefExt     = defext;
  801.  
  802.  if(!GetOpenFileName(&ofn)) return FALSE;
  803.  memmove(filename, buffer1, strlen(buffer1) + 1);
  804.  return TRUE;
  805. }
  806.  
  807. BOOL SaveFileDialog(HWND parent, const char* filter, const char* title, const char* defext, char* filename, char* initdir)
  808. {
  809.  char buffer1[MAX_PATH];
  810.  char buffer2[MAX_PATH];
  811.  buffer1[0] = '\0';
  812.  buffer2[0] = '\0';
  813.  
  814.  OPENFILENAME ofn;
  815.  ZeroMemory(&ofn, sizeof(ofn));
  816.  ofn.lStructSize     = sizeof(ofn);
  817.  ofn.hwndOwner       = parent;
  818.  ofn.hInstance       = (HINSTANCE)GetModuleHandle(0);
  819.  ofn.lpstrFilter     = filter;
  820.  ofn.lpstrFile       = buffer1;
  821.  ofn.nMaxFile        = MAX_PATH;
  822.  ofn.lpstrFileTitle  = buffer2;
  823.  ofn.nMaxFileTitle   = MAX_PATH;
  824.  ofn.lpstrInitialDir = initdir;
  825.  ofn.lpstrTitle      = title;
  826.  ofn.Flags           = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;
  827.  ofn.lpstrDefExt     = defext;
  828.  
  829.  if(!GetSaveFileName(&ofn)) return FALSE;
  830.  memmove(filename, buffer1, strlen(buffer1) + 1);
  831.  return TRUE;
  832. }