Commit 72e5d287 authored by hybrid's avatar hybrid

Enable file open dialog to restore initial path after close and add another...

Enable file open dialog to restore initial path after close and add another parameter for optional start directory.

git-svn-id: svn://svn.code.sf.net/p/irrlicht/code/trunk@3962 dfc29bdd-3216-0410-991c-e03cc46cb475
parent 89951605
...@@ -156,7 +156,8 @@ public: ...@@ -156,7 +156,8 @@ public:
This pointer should not be dropped. See IReferenceCounted::drop() for This pointer should not be dropped. See IReferenceCounted::drop() for
more information. */ more information. */
virtual IGUIImageList* createImageList( video::ITexture* texture, virtual IGUIImageList* createImageList( video::ITexture* texture,
core::dimension2d<s32> imageSize, bool useAlphaChannel ) = 0; core::dimension2d<s32> imageSize,
bool useAlphaChannel ) = 0;
//! Returns pointer to the font with the specified filename. //! Returns pointer to the font with the specified filename.
/** Loads the font if it was not loaded before. /** Loads the font if it was not loaded before.
...@@ -198,8 +199,8 @@ public: ...@@ -198,8 +199,8 @@ public:
//! Returns the root gui element. //! Returns the root gui element.
/** This is the first gui element, the (direct or indirect) parent of all /** This is the first gui element, the (direct or indirect) parent of all
other gui elements. It is a valid IGUIElement, with dimensions the same other gui elements. It is a valid IGUIElement, with dimensions the same
size as the screen. You should not need to use this method directly, unless size as the screen. You should not need to use this method directly, unless
you wish to reparent GUI elements to the top level. you wish to reparent GUI elements to the top level.
\return Pointer to the root element of the GUI. The returned pointer \return Pointer to the root element of the GUI. The returned pointer
should not be dropped. See IReferenceCounted::drop() for more should not be dropped. See IReferenceCounted::drop() for more
...@@ -353,11 +354,16 @@ public: ...@@ -353,11 +354,16 @@ public:
until this messagebox is removed. until this messagebox is removed.
\param parent Parent gui element of the dialog. \param parent Parent gui element of the dialog.
\param id Id to identify the gui element. \param id Id to identify the gui element.
\param restoreCWD If set to true, the current workingn directory will be
restored after the dialog is closed in some way. Otherwise the working
directory will be the one that the file dialog was last showing.
\param startDir Optional path for which the file dialog will be opened.
\return Pointer to the created file open dialog. Returns 0 if an error \return Pointer to the created file open dialog. Returns 0 if an error
occurred. This pointer should not be dropped. See occurred. This pointer should not be dropped. See
IReferenceCounted::drop() for more information. */ IReferenceCounted::drop() for more information. */
virtual IGUIFileOpenDialog* addFileOpenDialog(const wchar_t* title = 0, virtual IGUIFileOpenDialog* addFileOpenDialog(const wchar_t* title=0,
bool modal=true, IGUIElement* parent=0, s32 id=-1) = 0; bool modal=true, IGUIElement* parent=0, s32 id=-1,
bool restoreCWD=false, io::path::char_type* startDir=0) = 0;
//! Adds a color select dialog. //! Adds a color select dialog.
/** \param title The title of the dialog. /** \param title The title of the dialog.
......
...@@ -1185,11 +1185,13 @@ IGUITreeView* CGUIEnvironment::addTreeView(const core::rect<s32>& rectangle, ...@@ -1185,11 +1185,13 @@ IGUITreeView* CGUIEnvironment::addTreeView(const core::rect<s32>& rectangle,
//! adds a file open dialog. The returned pointer must not be dropped. //! adds a file open dialog. The returned pointer must not be dropped.
IGUIFileOpenDialog* CGUIEnvironment::addFileOpenDialog(const wchar_t* title, IGUIFileOpenDialog* CGUIEnvironment::addFileOpenDialog(const wchar_t* title,
bool modal, IGUIElement* parent, s32 id) bool modal, IGUIElement* parent, s32 id,
bool restoreCWD, io::path::char_type* startDir)
{ {
parent = parent ? parent : this; parent = parent ? parent : this;
IGUIFileOpenDialog* d = new CGUIFileOpenDialog(title, this, parent, id); IGUIFileOpenDialog* d = new CGUIFileOpenDialog(title, this, parent, id,
restoreCWD, startDir);
d->drop(); d->drop();
if (modal) if (modal)
......
...@@ -134,7 +134,9 @@ public: ...@@ -134,7 +134,9 @@ public:
virtual IGUIMeshViewer* addMeshViewer(const core::rect<s32>& rectangle, IGUIElement* parent=0, s32 id=-1, const wchar_t* text=0); virtual IGUIMeshViewer* addMeshViewer(const core::rect<s32>& rectangle, IGUIElement* parent=0, s32 id=-1, const wchar_t* text=0);
//! Adds a file open dialog. //! Adds a file open dialog.
virtual IGUIFileOpenDialog* addFileOpenDialog(const wchar_t* title = 0, bool modal=true, IGUIElement* parent=0, s32 id=-1); virtual IGUIFileOpenDialog* addFileOpenDialog(const wchar_t* title = 0,
bool modal=true, IGUIElement* parent=0, s32 id=-1,
bool restoreCWD=false, io::path::char_type* startDir=0);
//! Adds a color select dialog. //! Adds a color select dialog.
virtual IGUIColorSelectDialog* addColorSelectDialog(const wchar_t* title = 0, bool modal=true, IGUIElement* parent=0, s32 id=-1); virtual IGUIColorSelectDialog* addColorSelectDialog(const wchar_t* title = 0, bool modal=true, IGUIElement* parent=0, s32 id=-1);
......
...@@ -28,7 +28,8 @@ const s32 FOD_HEIGHT = 250; ...@@ -28,7 +28,8 @@ const s32 FOD_HEIGHT = 250;
//! constructor //! constructor
CGUIFileOpenDialog::CGUIFileOpenDialog(const wchar_t* title, CGUIFileOpenDialog::CGUIFileOpenDialog(const wchar_t* title,
IGUIEnvironment* environment, IGUIElement* parent, s32 id) IGUIEnvironment* environment, IGUIElement* parent, s32 id,
bool restoreCWD, io::path::char_type* startDir)
: IGUIFileOpenDialog(environment, parent, id, : IGUIFileOpenDialog(environment, parent, id,
core::rect<s32>((parent->getAbsolutePosition().getWidth()-FOD_WIDTH)/2, core::rect<s32>((parent->getAbsolutePosition().getWidth()-FOD_WIDTH)/2,
(parent->getAbsolutePosition().getHeight()-FOD_HEIGHT)/2, (parent->getAbsolutePosition().getHeight()-FOD_HEIGHT)/2,
...@@ -42,16 +43,33 @@ CGUIFileOpenDialog::CGUIFileOpenDialog(const wchar_t* title, ...@@ -42,16 +43,33 @@ CGUIFileOpenDialog::CGUIFileOpenDialog(const wchar_t* title,
Text = title; Text = title;
IGUISkin* skin = Environment->getSkin(); FileSystem = Environment?Environment->getFileSystem():0;
if (FileSystem)
{
FileSystem->grab();
if (restoreCWD)
RestoreDirectory = FileSystem->getWorkingDirectory();
if (startDir)
{
StartDirectory = startDir;
FileSystem->changeWorkingDirectoryTo(startDir);
}
}
else
return;
IGUISpriteBank* sprites = 0; IGUISpriteBank* sprites = 0;
video::SColor color(255,255,255,255); video::SColor color(255,255,255,255);
IGUISkin* skin = Environment->getSkin();
if (skin) if (skin)
{ {
sprites = skin->getSpriteBank(); sprites = skin->getSpriteBank();
color = skin->getColor(EGDC_WINDOW_SYMBOL); color = skin->getColor(EGDC_WINDOW_SYMBOL);
} }
const s32 buttonw = environment->getSkin()->getSize(EGDS_WINDOW_BUTTON_WIDTH); const s32 buttonw = skin->getSize(EGDS_WINDOW_BUTTON_WIDTH);
const s32 posx = RelativeRect.getWidth() - buttonw - 4; const s32 posx = RelativeRect.getWidth() - buttonw - 4;
CloseButton = Environment->addButton(core::rect<s32>(posx, 3, posx + buttonw, 3 + buttonw), this, -1, CloseButton = Environment->addButton(core::rect<s32>(posx, 3, posx + buttonw, 3 + buttonw), this, -1,
...@@ -91,11 +109,6 @@ CGUIFileOpenDialog::CGUIFileOpenDialog(const wchar_t* title, ...@@ -91,11 +109,6 @@ CGUIFileOpenDialog::CGUIFileOpenDialog(const wchar_t* title,
FileNameText->setAlignment(EGUIA_UPPERLEFT, EGUIA_LOWERRIGHT, EGUIA_UPPERLEFT, EGUIA_UPPERLEFT); FileNameText->setAlignment(EGUIA_UPPERLEFT, EGUIA_LOWERRIGHT, EGUIA_UPPERLEFT, EGUIA_UPPERLEFT);
FileNameText->grab(); FileNameText->grab();
FileSystem = Environment->getFileSystem();
if (FileSystem)
FileSystem->grab();
setTabGroup(true); setTabGroup(true);
fillListBox(); fillListBox();
...@@ -121,7 +134,12 @@ CGUIFileOpenDialog::~CGUIFileOpenDialog() ...@@ -121,7 +134,12 @@ CGUIFileOpenDialog::~CGUIFileOpenDialog()
FileNameText->drop(); FileNameText->drop();
if (FileSystem) if (FileSystem)
{
// revert to original CWD if path was set in constructor
if (RestoreDirectory.size())
FileSystem->changeWorkingDirectoryTo(RestoreDirectory);
FileSystem->drop(); FileSystem->drop();
}
if (FileList) if (FileList)
FileList->drop(); FileList->drop();
...@@ -312,6 +330,36 @@ void CGUIFileOpenDialog::draw() ...@@ -312,6 +330,36 @@ void CGUIFileOpenDialog::draw()
} }
//! Writes attributes of the element.
/* Not sure if this will really work out properly. Saving paths can be
rather problematic. */
void CGUIFileOpenDialog::serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options) const
{
IGUIFileOpenDialog::serializeAttributes(out,options);
out->addString("StartDirectory", StartDirectory.c_str());
out->addBool("RestoreDirectory", (RestoreDirectory.size()!=0));
}
//! Reads attributes of the element
/* Note that thiese paths changes will happen at arbitrary places upon
load of the gui description. This may be undesired. */
void CGUIFileOpenDialog::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options)
{
StartDirectory = in->getAttributeAsString("StartDirectory");
const bool restore = in->getAttributeAsBool("RestoreDirectory");
if (restore)
RestoreDirectory = FileSystem->getWorkingDirectory();
else
RestoreDirectory = "";
if (StartDirectory.size())
FileSystem->changeWorkingDirectoryTo(StartDirectory);
IGUIFileOpenDialog::deserializeAttributes(in,options);
}
//! fills the listbox with files. //! fills the listbox with files.
void CGUIFileOpenDialog::fillListBox() void CGUIFileOpenDialog::fillListBox()
{ {
...@@ -396,4 +444,3 @@ void CGUIFileOpenDialog::sendCancelEvent() ...@@ -396,4 +444,3 @@ void CGUIFileOpenDialog::sendCancelEvent()
} // end namespace irr } // end namespace irr
#endif // _IRR_COMPILE_WITH_GUI_ #endif // _IRR_COMPILE_WITH_GUI_
...@@ -24,7 +24,9 @@ namespace gui ...@@ -24,7 +24,9 @@ namespace gui
public: public:
//! constructor //! constructor
CGUIFileOpenDialog(const wchar_t* title, IGUIEnvironment* environment, IGUIElement* parent, s32 id); CGUIFileOpenDialog(const wchar_t* title, IGUIEnvironment* environment,
IGUIElement* parent, s32 id, bool restoreCWD=false,
io::path::char_type* startDir=0);
//! destructor //! destructor
virtual ~CGUIFileOpenDialog(); virtual ~CGUIFileOpenDialog();
...@@ -41,6 +43,9 @@ namespace gui ...@@ -41,6 +43,9 @@ namespace gui
//! draws the element and its children //! draws the element and its children
virtual void draw(); virtual void draw();
virtual void serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options=0) const;
virtual void deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options=0);
protected: protected:
//! fills the listbox with files. //! fills the listbox with files.
...@@ -55,6 +60,8 @@ namespace gui ...@@ -55,6 +60,8 @@ namespace gui
core::position2d<s32> DragStart; core::position2d<s32> DragStart;
core::stringw FileName; core::stringw FileName;
io::path FileDirectory; io::path FileDirectory;
io::path RestoreDirectory;
io::path StartDirectory;
IGUIButton* CloseButton; IGUIButton* CloseButton;
IGUIButton* OKButton; IGUIButton* OKButton;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment