DateiDialog.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. #include "DateiDialog.h"
  2. #include <ShObjIdl.h>
  3. #include "Text.h"
  4. #pragma comment(lib, "Ole32.lib")
  5. #pragma comment(linker, \
  6. "/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
  7. void Framework::InitDialog()
  8. {
  9. CoInitialize(0);
  10. }
  11. using namespace Framework;
  12. // Inhalt der DateiDialog Klasse aus DateiDialog.h
  13. // Konstruktor
  14. DateiDialog::DateiDialog()
  15. : ReferenceCounter()
  16. {
  17. typeName = new RCArray<Text>();
  18. type = new RCArray<Text>();
  19. fileIndex = 1;
  20. }
  21. // Destruktor
  22. DateiDialog::~DateiDialog()
  23. {
  24. typeName->release();
  25. type->release();
  26. }
  27. // nicht constant
  28. void DateiDialog::removeDateiTypen()
  29. {
  30. typeName->leeren();
  31. type->leeren();
  32. }
  33. void DateiDialog::addDateiTyp(const char* name, const char* typ)
  34. {
  35. addDateiTyp(new Text(name), new Text(typ));
  36. }
  37. void DateiDialog::addDateiTyp(Text* name, Text* typ)
  38. {
  39. typeName->add(name);
  40. type->add(typ);
  41. }
  42. void DateiDialog::setDateiTypAuswahl(int i)
  43. {
  44. fileIndex = i + 1;
  45. }
  46. // constant
  47. Text* DateiDialog::anzeigen(bool open) const
  48. {
  49. IFileDialog* pfd = NULL;
  50. CoInitialize(NULL);
  51. HRESULT hr = 0;
  52. if (open)
  53. {
  54. CoCreateInstance(CLSID_FileOpenDialog,
  55. NULL,
  56. CLSCTX_INPROC_SERVER,
  57. IID_PPV_ARGS(&pfd));
  58. }
  59. else
  60. {
  61. CoCreateInstance(CLSID_FileSaveDialog,
  62. NULL,
  63. CLSCTX_INPROC_SERVER,
  64. IID_PPV_ARGS(&pfd));
  65. }
  66. if (SUCCEEDED(hr))
  67. {
  68. DWORD dwFlags;
  69. hr = pfd->GetOptions(&dwFlags);
  70. if (SUCCEEDED(hr))
  71. {
  72. hr = pfd->SetOptions(dwFlags | FOS_FORCEFILESYSTEM);
  73. if (SUCCEEDED(hr))
  74. {
  75. int anz = type->getEintragAnzahl();
  76. COMDLG_FILTERSPEC* c_rgSaveTypes = 0;
  77. if (!anz)
  78. {
  79. c_rgSaveTypes = new COMDLG_FILTERSPEC[1];
  80. c_rgSaveTypes[0] = {L"Alle Dateien", L"*.*"};
  81. anz = 1;
  82. }
  83. else
  84. {
  85. c_rgSaveTypes = new COMDLG_FILTERSPEC[anz];
  86. for (int i = 0; i < anz; i++)
  87. {
  88. wchar_t* n
  89. = new wchar_t[typeName->z(i)->getLength() + 1];
  90. #pragma warning(disable : 4996)
  91. mbstowcs(n,
  92. typeName->z(i)->getText(),
  93. typeName->z(i)->getLength() + 1);
  94. wchar_t* t = new wchar_t[type->z(i)->getLength() + 1];
  95. #pragma warning(disable : 4996)
  96. mbstowcs(t,
  97. type->z(i)->getText(),
  98. type->z(i)->getLength() + 1);
  99. c_rgSaveTypes[i].pszName = n;
  100. c_rgSaveTypes[i].pszSpec = t;
  101. }
  102. }
  103. hr = pfd->SetFileTypes(anz, c_rgSaveTypes);
  104. if (SUCCEEDED(hr))
  105. {
  106. hr = pfd->SetFileTypeIndex(fileIndex);
  107. if (SUCCEEDED(hr))
  108. {
  109. Text txt = "";
  110. for (int i = 0; i < anz; i++)
  111. {
  112. if (!type->z(i)->hat(".*"))
  113. {
  114. txt.append(type->z(i)->getTeilText(
  115. type->z(i)->positionVon(".") + 1));
  116. txt += ";";
  117. }
  118. }
  119. if (txt.getLength() > 0)
  120. txt.remove(txt.getLength() - 1);
  121. wchar_t* defEnd = new wchar_t[txt.getLength() + 1];
  122. #pragma warning(disable : 4996)
  123. mbstowcs(defEnd, txt, txt.getLength() + 1);
  124. hr = pfd->SetDefaultExtension(defEnd);
  125. if (SUCCEEDED(hr))
  126. {
  127. hr = pfd->Show(NULL);
  128. if (SUCCEEDED(hr))
  129. {
  130. IShellItem* psiResult;
  131. hr = pfd->GetResult(&psiResult);
  132. if (SUCCEEDED(hr))
  133. {
  134. PWSTR pszFilePath = NULL;
  135. hr = psiResult->GetDisplayName(
  136. SIGDN_FILESYSPATH, &pszFilePath);
  137. if (SUCCEEDED(hr))
  138. {
  139. char* txt
  140. = new char[wcslen(pszFilePath) + 1];
  141. #pragma warning(disable : 4996)
  142. wcstombs(txt,
  143. pszFilePath,
  144. wcslen(pszFilePath) + 1);
  145. Text* ret = new Text(txt);
  146. delete[] txt;
  147. psiResult->Release();
  148. delete[] defEnd;
  149. for (int i = 0; i < anz; i++)
  150. {
  151. delete[] c_rgSaveTypes[i].pszName;
  152. delete[] c_rgSaveTypes[i].pszSpec;
  153. }
  154. delete[] c_rgSaveTypes;
  155. pfd->Release();
  156. return ret;
  157. }
  158. psiResult->Release();
  159. }
  160. }
  161. }
  162. delete[] defEnd;
  163. }
  164. }
  165. for (int i = 0; i < anz; i++)
  166. {
  167. delete[] c_rgSaveTypes[i].pszName;
  168. delete[] c_rgSaveTypes[i].pszSpec;
  169. }
  170. delete[] c_rgSaveTypes;
  171. }
  172. }
  173. pfd->Release();
  174. }
  175. return 0;
  176. }
  177. // Inhalt der DateiDialogTh Klasse aus DateiDialog.h
  178. // Konstruktor
  179. DateiDialogTh::DateiDialogTh()
  180. : Thread()
  181. {
  182. dialog = new DateiDialog();
  183. ret = 0;
  184. open = 0;
  185. }
  186. // Destruktor
  187. DateiDialogTh::~DateiDialogTh()
  188. {
  189. warteAufThread(5000);
  190. if (run) ende();
  191. dialog->release();
  192. if (ret) ret->release();
  193. }
  194. // nicht constant
  195. void DateiDialogTh::setOpen(bool b)
  196. {
  197. open = b;
  198. }
  199. void DateiDialogTh::removeDateiTypen()
  200. {
  201. dialog->removeDateiTypen();
  202. }
  203. void DateiDialogTh::addDateiTyp(const char* name, const char* typ)
  204. {
  205. dialog->addDateiTyp(name, typ);
  206. }
  207. void DateiDialogTh::addDateiTyp(Text* name, Text* typ)
  208. {
  209. dialog->addDateiTyp(name, typ);
  210. }
  211. void DateiDialogTh::setDateiTypAuswahl(int i)
  212. {
  213. dialog->setDateiTypAuswahl(i);
  214. }
  215. void DateiDialogTh::thread()
  216. {
  217. if (ret) ret = (Text*)ret->release();
  218. ret = dialog->anzeigen(open);
  219. }
  220. // constant
  221. Text* DateiDialogTh::getPfad() const
  222. {
  223. return ret ? dynamic_cast<Text*>(ret->getThis()) : 0;
  224. }
  225. Text* DateiDialogTh::zPfad() const
  226. {
  227. return ret;
  228. }