CGUIFileOpenDialog.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. // Copyright (C) 2002-2012 Nikolaus Gebhardt
  2. // This file is part of the "Irrlicht Engine".
  3. // For conditions of distribution and use, see copyright notice in irrlicht.h
  4. #include "CGUIFileOpenDialog.h"
  5. #ifdef _IRR_COMPILE_WITH_GUI_
  6. #include <locale.h>
  7. #include "IGUISkin.h"
  8. #include "IGUIEnvironment.h"
  9. #include "IVideoDriver.h"
  10. #include "IGUIButton.h"
  11. #include "IGUIStaticText.h"
  12. #include "IGUIFont.h"
  13. #include "IGUIFontBitmap.h"
  14. #include "IFileList.h"
  15. #include "os.h"
  16. namespace irr
  17. {
  18. namespace gui
  19. {
  20. const s32 FOD_WIDTH = 350;
  21. const s32 FOD_HEIGHT = 250;
  22. //! constructor
  23. CGUIFileOpenDialog::CGUIFileOpenDialog(const wchar_t* title,
  24. IGUIEnvironment* environment, IGUIElement* parent, s32 id,
  25. bool restoreCWD, io::path::char_type* startDir)
  26. : IGUIFileOpenDialog(environment, parent, id,
  27. core::rect<s32>((parent->getAbsolutePosition().getWidth()-FOD_WIDTH)/2,
  28. (parent->getAbsolutePosition().getHeight()-FOD_HEIGHT)/2,
  29. (parent->getAbsolutePosition().getWidth()-FOD_WIDTH)/2+FOD_WIDTH,
  30. (parent->getAbsolutePosition().getHeight()-FOD_HEIGHT)/2+FOD_HEIGHT)),
  31. FileNameText(0), FileList(0), Dragging(false)
  32. {
  33. #ifdef _DEBUG
  34. IGUIElement::setDebugName("CGUIFileOpenDialog");
  35. #endif
  36. Text = title;
  37. FileSystem = Environment?Environment->getFileSystem():0;
  38. if (FileSystem)
  39. {
  40. FileSystem->grab();
  41. if (restoreCWD)
  42. RestoreDirectory = FileSystem->getWorkingDirectory();
  43. if (startDir)
  44. {
  45. StartDirectory = startDir;
  46. FileSystem->changeWorkingDirectoryTo(startDir);
  47. }
  48. }
  49. else
  50. return;
  51. IGUISpriteBank* sprites = 0;
  52. video::SColor color(255,255,255,255);
  53. IGUISkin* skin = Environment->getSkin();
  54. if (skin)
  55. {
  56. sprites = skin->getSpriteBank();
  57. color = skin->getColor(EGDC_WINDOW_SYMBOL);
  58. }
  59. const s32 buttonw = skin->getSize(EGDS_WINDOW_BUTTON_WIDTH);
  60. const s32 posx = RelativeRect.getWidth() - buttonw - 4;
  61. CloseButton = Environment->addButton(core::rect<s32>(posx, 3, posx + buttonw, 3 + buttonw), this, -1,
  62. L"", skin ? skin->getDefaultText(EGDT_WINDOW_CLOSE) : L"Close");
  63. CloseButton->setSubElement(true);
  64. CloseButton->setTabStop(false);
  65. if (sprites)
  66. {
  67. CloseButton->setSpriteBank(sprites);
  68. CloseButton->setSprite(EGBS_BUTTON_UP, skin->getIcon(EGDI_WINDOW_CLOSE), color);
  69. CloseButton->setSprite(EGBS_BUTTON_DOWN, skin->getIcon(EGDI_WINDOW_CLOSE), color);
  70. }
  71. CloseButton->setAlignment(EGUIA_LOWERRIGHT, EGUIA_LOWERRIGHT, EGUIA_UPPERLEFT, EGUIA_UPPERLEFT);
  72. CloseButton->grab();
  73. OKButton = Environment->addButton(
  74. core::rect<s32>(RelativeRect.getWidth()-80, 30, RelativeRect.getWidth()-10, 50),
  75. this, -1, skin ? skin->getDefaultText(EGDT_MSG_BOX_OK) : L"OK");
  76. OKButton->setSubElement(true);
  77. OKButton->setAlignment(EGUIA_LOWERRIGHT, EGUIA_LOWERRIGHT, EGUIA_UPPERLEFT, EGUIA_UPPERLEFT);
  78. OKButton->grab();
  79. CancelButton = Environment->addButton(
  80. core::rect<s32>(RelativeRect.getWidth()-80, 55, RelativeRect.getWidth()-10, 75),
  81. this, -1, skin ? skin->getDefaultText(EGDT_MSG_BOX_CANCEL) : L"Cancel");
  82. CancelButton->setSubElement(true);
  83. CancelButton->setAlignment(EGUIA_LOWERRIGHT, EGUIA_LOWERRIGHT, EGUIA_UPPERLEFT, EGUIA_UPPERLEFT);
  84. CancelButton->grab();
  85. FileBox = Environment->addListBox(core::rect<s32>(10, 55, RelativeRect.getWidth()-90, 230), this, -1, true);
  86. FileBox->setSubElement(true);
  87. FileBox->setAlignment(EGUIA_UPPERLEFT, EGUIA_LOWERRIGHT, EGUIA_UPPERLEFT, EGUIA_LOWERRIGHT);
  88. FileBox->grab();
  89. FileNameText = Environment->addEditBox(0, core::rect<s32>(10, 30, RelativeRect.getWidth()-90, 50), true, this);
  90. FileNameText->setSubElement(true);
  91. FileNameText->setAlignment(EGUIA_UPPERLEFT, EGUIA_LOWERRIGHT, EGUIA_UPPERLEFT, EGUIA_UPPERLEFT);
  92. FileNameText->grab();
  93. setTabGroup(true);
  94. fillListBox();
  95. }
  96. //! destructor
  97. CGUIFileOpenDialog::~CGUIFileOpenDialog()
  98. {
  99. if (CloseButton)
  100. CloseButton->drop();
  101. if (OKButton)
  102. OKButton->drop();
  103. if (CancelButton)
  104. CancelButton->drop();
  105. if (FileBox)
  106. FileBox->drop();
  107. if (FileNameText)
  108. FileNameText->drop();
  109. if (FileSystem)
  110. {
  111. // revert to original CWD if path was set in constructor
  112. if (RestoreDirectory.size())
  113. FileSystem->changeWorkingDirectoryTo(RestoreDirectory);
  114. FileSystem->drop();
  115. }
  116. if (FileList)
  117. FileList->drop();
  118. }
  119. //! returns the filename of the selected file. Returns NULL, if no file was selected.
  120. const wchar_t* CGUIFileOpenDialog::getFileName() const
  121. {
  122. return FileName.c_str();
  123. }
  124. //! Returns the directory of the selected file. Returns NULL, if no directory was selected.
  125. const io::path& CGUIFileOpenDialog::getDirectoryName()
  126. {
  127. FileSystem->flattenFilename ( FileDirectory );
  128. return FileDirectory;
  129. }
  130. //! called if an event happened.
  131. bool CGUIFileOpenDialog::OnEvent(const SEvent& event)
  132. {
  133. if (isEnabled())
  134. {
  135. switch(event.EventType)
  136. {
  137. case EET_GUI_EVENT:
  138. switch(event.GUIEvent.EventType)
  139. {
  140. case EGET_ELEMENT_FOCUS_LOST:
  141. Dragging = false;
  142. break;
  143. case EGET_BUTTON_CLICKED:
  144. if (event.GUIEvent.Caller == CloseButton ||
  145. event.GUIEvent.Caller == CancelButton)
  146. {
  147. sendCancelEvent();
  148. remove();
  149. return true;
  150. }
  151. else
  152. if (event.GUIEvent.Caller == OKButton )
  153. {
  154. if ( FileDirectory != L"" )
  155. {
  156. sendSelectedEvent( EGET_DIRECTORY_SELECTED );
  157. }
  158. if ( FileName != L"" )
  159. {
  160. sendSelectedEvent( EGET_FILE_SELECTED );
  161. remove();
  162. return true;
  163. }
  164. }
  165. break;
  166. case EGET_LISTBOX_CHANGED:
  167. {
  168. s32 selected = FileBox->getSelected();
  169. if (FileList && FileSystem)
  170. {
  171. if (FileList->isDirectory(selected))
  172. {
  173. FileName = L"";
  174. FileDirectory = FileList->getFullFileName(selected);
  175. }
  176. else
  177. {
  178. FileDirectory = L"";
  179. FileName = FileList->getFullFileName(selected);
  180. }
  181. return true;
  182. }
  183. }
  184. break;
  185. case EGET_LISTBOX_SELECTED_AGAIN:
  186. {
  187. const s32 selected = FileBox->getSelected();
  188. if (FileList && FileSystem)
  189. {
  190. if (FileList->isDirectory(selected))
  191. {
  192. FileDirectory = FileList->getFullFileName(selected);
  193. FileSystem->changeWorkingDirectoryTo(FileList->getFileName(selected));
  194. fillListBox();
  195. FileName = "";
  196. }
  197. else
  198. {
  199. FileName = FileList->getFullFileName(selected);
  200. }
  201. return true;
  202. }
  203. }
  204. break;
  205. case EGET_EDITBOX_ENTER:
  206. if (event.GUIEvent.Caller == FileNameText)
  207. {
  208. io::path dir( FileNameText->getText () );
  209. if ( FileSystem->changeWorkingDirectoryTo( dir ) )
  210. {
  211. fillListBox();
  212. FileName = L"";
  213. }
  214. return true;
  215. }
  216. break;
  217. default:
  218. break;
  219. }
  220. break;
  221. case EET_MOUSE_INPUT_EVENT:
  222. switch(event.MouseInput.Event)
  223. {
  224. case EMIE_MOUSE_WHEEL:
  225. return FileBox->OnEvent(event);
  226. case EMIE_LMOUSE_PRESSED_DOWN:
  227. DragStart.X = event.MouseInput.X;
  228. DragStart.Y = event.MouseInput.Y;
  229. Dragging = true;
  230. Environment->setFocus(this);
  231. return true;
  232. case EMIE_LMOUSE_LEFT_UP:
  233. Dragging = false;
  234. return true;
  235. case EMIE_MOUSE_MOVED:
  236. if ( !event.MouseInput.isLeftPressed () )
  237. Dragging = false;
  238. if (Dragging)
  239. {
  240. // gui window should not be dragged outside its parent
  241. if (Parent)
  242. if (event.MouseInput.X < Parent->getAbsolutePosition().UpperLeftCorner.X +1 ||
  243. event.MouseInput.Y < Parent->getAbsolutePosition().UpperLeftCorner.Y +1 ||
  244. event.MouseInput.X > Parent->getAbsolutePosition().LowerRightCorner.X -1 ||
  245. event.MouseInput.Y > Parent->getAbsolutePosition().LowerRightCorner.Y -1)
  246. return true;
  247. move(core::position2d<s32>(event.MouseInput.X - DragStart.X, event.MouseInput.Y - DragStart.Y));
  248. DragStart.X = event.MouseInput.X;
  249. DragStart.Y = event.MouseInput.Y;
  250. return true;
  251. }
  252. break;
  253. default:
  254. break;
  255. }
  256. default:
  257. break;
  258. }
  259. }
  260. return IGUIElement::OnEvent(event);
  261. }
  262. //! draws the element and its children
  263. void CGUIFileOpenDialog::draw()
  264. {
  265. if (!IsVisible)
  266. return;
  267. IGUISkin* skin = Environment->getSkin();
  268. core::rect<s32> rect = AbsoluteRect;
  269. rect = skin->draw3DWindowBackground(this, true, skin->getColor(EGDC_ACTIVE_BORDER),
  270. rect, &AbsoluteClippingRect);
  271. if (Text.size())
  272. {
  273. rect.UpperLeftCorner.X += 2;
  274. rect.LowerRightCorner.X -= skin->getSize(EGDS_WINDOW_BUTTON_WIDTH) + 5;
  275. IGUIFont* font = skin->getFont(EGDF_WINDOW);
  276. if (font)
  277. font->draw(Text.c_str(), rect,
  278. skin->getColor(EGDC_ACTIVE_CAPTION),
  279. false, true, &AbsoluteClippingRect);
  280. }
  281. IGUIElement::draw();
  282. }
  283. //! Writes attributes of the element.
  284. /* Not sure if this will really work out properly. Saving paths can be
  285. rather problematic. */
  286. void CGUIFileOpenDialog::serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options) const
  287. {
  288. IGUIFileOpenDialog::serializeAttributes(out,options);
  289. out->addString("StartDirectory", StartDirectory.c_str());
  290. out->addBool("RestoreDirectory", (RestoreDirectory.size()!=0));
  291. }
  292. //! Reads attributes of the element
  293. /* Note that thiese paths changes will happen at arbitrary places upon
  294. load of the gui description. This may be undesired. */
  295. void CGUIFileOpenDialog::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options)
  296. {
  297. StartDirectory = in->getAttributeAsString("StartDirectory");
  298. const bool restore = in->getAttributeAsBool("RestoreDirectory");
  299. if (restore)
  300. RestoreDirectory = FileSystem->getWorkingDirectory();
  301. else
  302. RestoreDirectory = "";
  303. if (StartDirectory.size())
  304. FileSystem->changeWorkingDirectoryTo(StartDirectory);
  305. IGUIFileOpenDialog::deserializeAttributes(in,options);
  306. }
  307. //! fills the listbox with files.
  308. void CGUIFileOpenDialog::fillListBox()
  309. {
  310. IGUISkin *skin = Environment->getSkin();
  311. if (!FileSystem || !FileBox || !skin)
  312. return;
  313. if (FileList)
  314. FileList->drop();
  315. FileBox->clear();
  316. FileList = FileSystem->createFileList();
  317. core::stringw s;
  318. #if !defined(_IRR_WINDOWS_CE_PLATFORM_)
  319. setlocale(LC_ALL,"");
  320. #endif
  321. if (FileList)
  322. {
  323. for (u32 i=0; i < FileList->getFileCount(); ++i)
  324. {
  325. #ifndef _IRR_WCHAR_FILESYSTEM
  326. const c8 *cs = (const c8 *)FileList->getFileName(i).c_str();
  327. wchar_t *ws = new wchar_t[strlen(cs) + 1];
  328. int len = mbstowcs(ws,cs,strlen(cs));
  329. ws[len] = 0;
  330. s = ws;
  331. delete [] ws;
  332. #else
  333. s = FileList->getFileName(i).c_str();
  334. #endif
  335. FileBox->addItem(s.c_str(), skin->getIcon(FileList->isDirectory(i) ? EGDI_DIRECTORY : EGDI_FILE));
  336. }
  337. }
  338. if (FileNameText)
  339. {
  340. #ifndef _IRR_WCHAR_FILESYSTEM
  341. const c8 *cs = (const c8 *)FileSystem->getWorkingDirectory().c_str();
  342. wchar_t *ws = new wchar_t[strlen(cs) + 1];
  343. int len = mbstowcs(ws,cs,strlen(cs));
  344. ws[len] = 0;
  345. s = ws;
  346. delete [] ws;
  347. #else
  348. s = FileSystem->getWorkingDirectory();
  349. #endif
  350. FileDirectory = s;
  351. FileNameText->setText(s.c_str());
  352. }
  353. }
  354. //! sends the event that the file has been selected.
  355. void CGUIFileOpenDialog::sendSelectedEvent( EGUI_EVENT_TYPE type)
  356. {
  357. SEvent event;
  358. event.EventType = EET_GUI_EVENT;
  359. event.GUIEvent.Caller = this;
  360. event.GUIEvent.Element = 0;
  361. event.GUIEvent.EventType = type;
  362. Parent->OnEvent(event);
  363. }
  364. //! sends the event that the file choose process has been canceld
  365. void CGUIFileOpenDialog::sendCancelEvent()
  366. {
  367. SEvent event;
  368. event.EventType = EET_GUI_EVENT;
  369. event.GUIEvent.Caller = this;
  370. event.GUIEvent.Element = 0;
  371. event.GUIEvent.EventType = EGET_FILE_CHOOSE_DIALOG_CANCELLED;
  372. Parent->OnEvent(event);
  373. }
  374. } // end namespace gui
  375. } // end namespace irr
  376. #endif // _IRR_COMPILE_WITH_GUI_