DateiDialog.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. Text* tmp = type->z(i)->getTeilText(
  115. type->z(i)->positionVon(".") + 1);
  116. txt.append(*tmp);
  117. tmp->release();
  118. txt += ";";
  119. }
  120. }
  121. if (txt.getLength() > 0)
  122. txt.remove(txt.getLength() - 1);
  123. wchar_t* defEnd = new wchar_t[txt.getLength() + 1];
  124. #pragma warning(disable : 4996)
  125. mbstowcs(defEnd, txt, txt.getLength() + 1);
  126. hr = pfd->SetDefaultExtension(defEnd);
  127. if (SUCCEEDED(hr))
  128. {
  129. hr = pfd->Show(NULL);
  130. if (SUCCEEDED(hr))
  131. {
  132. IShellItem* psiResult;
  133. hr = pfd->GetResult(&psiResult);
  134. if (SUCCEEDED(hr))
  135. {
  136. PWSTR pszFilePath = NULL;
  137. hr = psiResult->GetDisplayName(
  138. SIGDN_FILESYSPATH, &pszFilePath);
  139. if (SUCCEEDED(hr))
  140. {
  141. char* txt
  142. = new char[wcslen(pszFilePath) + 1];
  143. #pragma warning(disable : 4996)
  144. wcstombs(txt,
  145. pszFilePath,
  146. wcslen(pszFilePath) + 1);
  147. Text* ret = new Text(txt);
  148. delete[] txt;
  149. psiResult->Release();
  150. delete[] defEnd;
  151. for (int i = 0; i < anz; i++)
  152. {
  153. delete[] c_rgSaveTypes[i].pszName;
  154. delete[] c_rgSaveTypes[i].pszSpec;
  155. }
  156. delete[] c_rgSaveTypes;
  157. pfd->Release();
  158. return ret;
  159. }
  160. psiResult->Release();
  161. }
  162. }
  163. }
  164. delete[] defEnd;
  165. }
  166. }
  167. for (int i = 0; i < anz; i++)
  168. {
  169. delete[] c_rgSaveTypes[i].pszName;
  170. delete[] c_rgSaveTypes[i].pszSpec;
  171. }
  172. delete[] c_rgSaveTypes;
  173. }
  174. }
  175. pfd->Release();
  176. }
  177. return 0;
  178. }
  179. // Inhalt der DateiDialogTh Klasse aus DateiDialog.h
  180. // Konstruktor
  181. DateiDialogTh::DateiDialogTh()
  182. : Thread()
  183. {
  184. dialog = new DateiDialog();
  185. ret = 0;
  186. open = 0;
  187. }
  188. // Destruktor
  189. DateiDialogTh::~DateiDialogTh()
  190. {
  191. warteAufThread(5000);
  192. if (run) ende();
  193. dialog->release();
  194. if (ret) ret->release();
  195. }
  196. // nicht constant
  197. void DateiDialogTh::setOpen(bool b)
  198. {
  199. open = b;
  200. }
  201. void DateiDialogTh::removeDateiTypen()
  202. {
  203. dialog->removeDateiTypen();
  204. }
  205. void DateiDialogTh::addDateiTyp(const char* name, const char* typ)
  206. {
  207. dialog->addDateiTyp(name, typ);
  208. }
  209. void DateiDialogTh::addDateiTyp(Text* name, Text* typ)
  210. {
  211. dialog->addDateiTyp(name, typ);
  212. }
  213. void DateiDialogTh::setDateiTypAuswahl(int i)
  214. {
  215. dialog->setDateiTypAuswahl(i);
  216. }
  217. void DateiDialogTh::thread()
  218. {
  219. if (ret) ret = (Text*)ret->release();
  220. ret = dialog->anzeigen(open);
  221. }
  222. // constant
  223. Text* DateiDialogTh::getPfad() const
  224. {
  225. return ret ? dynamic_cast<Text*>(ret->getThis()) : 0;
  226. }
  227. Text* DateiDialogTh::zPfad() const
  228. {
  229. return ret;
  230. }