2023-10-03 20:37:00 +02:00
|
|
|
// Copyright (C) 2002-2012 Nikolaus Gebhardt
|
|
|
|
// This file is part of the "Irrlicht Engine".
|
|
|
|
// For conditions of distribution and use, see copyright notice in irrlicht.h
|
|
|
|
|
|
|
|
#include "CGUIFileOpenDialog.h"
|
|
|
|
|
|
|
|
#include "IGUISkin.h"
|
|
|
|
#include "IGUIEnvironment.h"
|
|
|
|
#include "IVideoDriver.h"
|
|
|
|
#include "IGUIButton.h"
|
|
|
|
#include "IGUIStaticText.h"
|
|
|
|
#include "IGUIFont.h"
|
|
|
|
#include "IGUIFontBitmap.h"
|
|
|
|
#include "IFileList.h"
|
|
|
|
#include "os.h"
|
|
|
|
|
|
|
|
namespace irr
|
|
|
|
{
|
|
|
|
namespace gui
|
|
|
|
{
|
|
|
|
|
|
|
|
const s32 FOD_WIDTH = 350;
|
|
|
|
const s32 FOD_HEIGHT = 250;
|
|
|
|
|
|
|
|
//! constructor
|
2024-03-20 19:35:52 +01:00
|
|
|
CGUIFileOpenDialog::CGUIFileOpenDialog(const wchar_t *title,
|
|
|
|
IGUIEnvironment *environment, IGUIElement *parent, s32 id,
|
|
|
|
bool restoreCWD, io::path::char_type *startDir) :
|
|
|
|
IGUIFileOpenDialog(environment, parent, id,
|
|
|
|
core::rect<s32>((parent->getAbsolutePosition().getWidth() - FOD_WIDTH) / 2,
|
|
|
|
(parent->getAbsolutePosition().getHeight() - FOD_HEIGHT) / 2,
|
|
|
|
(parent->getAbsolutePosition().getWidth() - FOD_WIDTH) / 2 + FOD_WIDTH,
|
|
|
|
(parent->getAbsolutePosition().getHeight() - FOD_HEIGHT) / 2 + FOD_HEIGHT)),
|
|
|
|
FileNameText(0), FileList(0), Dragging(false)
|
2023-10-03 20:37:00 +02:00
|
|
|
{
|
2024-03-20 19:35:52 +01:00
|
|
|
#ifdef _DEBUG
|
2023-10-03 20:37:00 +02:00
|
|
|
IGUIElement::setDebugName("CGUIFileOpenDialog");
|
2024-03-20 19:35:52 +01:00
|
|
|
#endif
|
2023-10-03 20:37:00 +02:00
|
|
|
|
|
|
|
Text = title;
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
FileSystem = Environment ? Environment->getFileSystem() : 0;
|
2023-10-03 20:37:00 +02:00
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
if (FileSystem) {
|
2023-10-03 20:37:00 +02:00
|
|
|
FileSystem->grab();
|
|
|
|
|
|
|
|
if (restoreCWD)
|
|
|
|
RestoreDirectory = FileSystem->getWorkingDirectory();
|
2024-03-20 19:35:52 +01:00
|
|
|
if (startDir) {
|
2023-10-03 20:37:00 +02:00
|
|
|
StartDirectory = startDir;
|
|
|
|
FileSystem->changeWorkingDirectoryTo(startDir);
|
|
|
|
}
|
2024-03-20 19:35:52 +01:00
|
|
|
} else
|
2023-10-03 20:37:00 +02:00
|
|
|
return;
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
IGUISpriteBank *sprites = 0;
|
|
|
|
video::SColor color(255, 255, 255, 255);
|
|
|
|
IGUISkin *skin = Environment->getSkin();
|
|
|
|
if (skin) {
|
2023-10-03 20:37:00 +02:00
|
|
|
sprites = skin->getSpriteBank();
|
|
|
|
color = skin->getColor(EGDC_WINDOW_SYMBOL);
|
|
|
|
}
|
|
|
|
|
|
|
|
const s32 buttonw = skin ? skin->getSize(EGDS_WINDOW_BUTTON_WIDTH) : 2;
|
|
|
|
const s32 posx = RelativeRect.getWidth() - buttonw - 4;
|
|
|
|
|
|
|
|
CloseButton = Environment->addButton(core::rect<s32>(posx, 3, posx + buttonw, 3 + buttonw), this, -1,
|
2024-03-20 19:35:52 +01:00
|
|
|
L"", skin ? skin->getDefaultText(EGDT_WINDOW_CLOSE) : L"Close");
|
2023-10-03 20:37:00 +02:00
|
|
|
CloseButton->setSubElement(true);
|
|
|
|
CloseButton->setTabStop(false);
|
2024-03-20 19:35:52 +01:00
|
|
|
if (sprites && skin) {
|
2023-10-03 20:37:00 +02:00
|
|
|
CloseButton->setSpriteBank(sprites);
|
|
|
|
CloseButton->setSprite(EGBS_BUTTON_UP, skin->getIcon(EGDI_WINDOW_CLOSE), color);
|
|
|
|
CloseButton->setSprite(EGBS_BUTTON_DOWN, skin->getIcon(EGDI_WINDOW_CLOSE), color);
|
|
|
|
}
|
|
|
|
CloseButton->setAlignment(EGUIA_LOWERRIGHT, EGUIA_LOWERRIGHT, EGUIA_UPPERLEFT, EGUIA_UPPERLEFT);
|
|
|
|
CloseButton->grab();
|
|
|
|
|
|
|
|
OKButton = Environment->addButton(
|
2024-03-20 19:35:52 +01:00
|
|
|
core::rect<s32>(RelativeRect.getWidth() - 80, 30, RelativeRect.getWidth() - 10, 50),
|
|
|
|
this, -1, skin ? skin->getDefaultText(EGDT_MSG_BOX_OK) : L"OK");
|
2023-10-03 20:37:00 +02:00
|
|
|
OKButton->setSubElement(true);
|
|
|
|
OKButton->setAlignment(EGUIA_LOWERRIGHT, EGUIA_LOWERRIGHT, EGUIA_UPPERLEFT, EGUIA_UPPERLEFT);
|
|
|
|
OKButton->grab();
|
|
|
|
|
|
|
|
CancelButton = Environment->addButton(
|
2024-03-20 19:35:52 +01:00
|
|
|
core::rect<s32>(RelativeRect.getWidth() - 80, 55, RelativeRect.getWidth() - 10, 75),
|
|
|
|
this, -1, skin ? skin->getDefaultText(EGDT_MSG_BOX_CANCEL) : L"Cancel");
|
2023-10-03 20:37:00 +02:00
|
|
|
CancelButton->setSubElement(true);
|
|
|
|
CancelButton->setAlignment(EGUIA_LOWERRIGHT, EGUIA_LOWERRIGHT, EGUIA_UPPERLEFT, EGUIA_UPPERLEFT);
|
|
|
|
CancelButton->grab();
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
FileBox = Environment->addListBox(core::rect<s32>(10, 55, RelativeRect.getWidth() - 90, 230), this, -1, true);
|
2023-10-03 20:37:00 +02:00
|
|
|
FileBox->setSubElement(true);
|
|
|
|
FileBox->setAlignment(EGUIA_UPPERLEFT, EGUIA_LOWERRIGHT, EGUIA_UPPERLEFT, EGUIA_LOWERRIGHT);
|
|
|
|
FileBox->grab();
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
FileNameText = Environment->addEditBox(0, core::rect<s32>(10, 30, RelativeRect.getWidth() - 90, 50), true, this);
|
2023-10-03 20:37:00 +02:00
|
|
|
FileNameText->setSubElement(true);
|
|
|
|
FileNameText->setAlignment(EGUIA_UPPERLEFT, EGUIA_LOWERRIGHT, EGUIA_UPPERLEFT, EGUIA_UPPERLEFT);
|
|
|
|
FileNameText->grab();
|
|
|
|
|
|
|
|
setTabGroup(true);
|
|
|
|
|
|
|
|
fillListBox();
|
|
|
|
}
|
|
|
|
|
|
|
|
//! destructor
|
|
|
|
CGUIFileOpenDialog::~CGUIFileOpenDialog()
|
|
|
|
{
|
|
|
|
if (CloseButton)
|
|
|
|
CloseButton->drop();
|
|
|
|
|
|
|
|
if (OKButton)
|
|
|
|
OKButton->drop();
|
|
|
|
|
|
|
|
if (CancelButton)
|
|
|
|
CancelButton->drop();
|
|
|
|
|
|
|
|
if (FileBox)
|
|
|
|
FileBox->drop();
|
|
|
|
|
|
|
|
if (FileNameText)
|
|
|
|
FileNameText->drop();
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
if (FileSystem) {
|
2023-10-03 20:37:00 +02:00
|
|
|
// revert to original CWD if path was set in constructor
|
|
|
|
if (RestoreDirectory.size())
|
|
|
|
FileSystem->changeWorkingDirectoryTo(RestoreDirectory);
|
|
|
|
FileSystem->drop();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FileList)
|
|
|
|
FileList->drop();
|
|
|
|
}
|
|
|
|
|
|
|
|
//! returns the filename of the selected file. Returns NULL, if no file was selected.
|
2024-03-20 19:35:52 +01:00
|
|
|
const wchar_t *CGUIFileOpenDialog::getFileName() const
|
2023-10-03 20:37:00 +02:00
|
|
|
{
|
|
|
|
return FileNameW.c_str();
|
|
|
|
}
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
const io::path &CGUIFileOpenDialog::getFileNameP() const
|
2023-10-03 20:37:00 +02:00
|
|
|
{
|
|
|
|
return FileName;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! Returns the directory of the selected file. Returns NULL, if no directory was selected.
|
2024-03-20 19:35:52 +01:00
|
|
|
const io::path &CGUIFileOpenDialog::getDirectoryName() const
|
2023-10-03 20:37:00 +02:00
|
|
|
{
|
|
|
|
return FileDirectoryFlat;
|
|
|
|
}
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
const wchar_t *CGUIFileOpenDialog::getDirectoryNameW() const
|
2023-10-03 20:37:00 +02:00
|
|
|
{
|
|
|
|
return FileDirectoryFlatW.c_str();
|
|
|
|
}
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
void CGUIFileOpenDialog::setFileName(const irr::io::path &name)
|
2023-10-03 20:37:00 +02:00
|
|
|
{
|
|
|
|
FileName = name;
|
|
|
|
pathToStringW(FileNameW, FileName);
|
|
|
|
}
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
void CGUIFileOpenDialog::setDirectoryName(const irr::io::path &name)
|
2023-10-03 20:37:00 +02:00
|
|
|
{
|
|
|
|
FileDirectory = name;
|
|
|
|
FileDirectoryFlat = name;
|
2024-03-20 19:35:52 +01:00
|
|
|
FileSystem->flattenFilename(FileDirectoryFlat);
|
2023-10-03 20:37:00 +02:00
|
|
|
pathToStringW(FileDirectoryFlatW, FileDirectoryFlat);
|
|
|
|
}
|
|
|
|
|
|
|
|
//! called if an event happened.
|
2024-03-20 19:35:52 +01:00
|
|
|
bool CGUIFileOpenDialog::OnEvent(const SEvent &event)
|
2023-10-03 20:37:00 +02:00
|
|
|
{
|
2024-03-20 19:35:52 +01:00
|
|
|
if (isEnabled()) {
|
|
|
|
switch (event.EventType) {
|
2023-10-03 20:37:00 +02:00
|
|
|
case EET_GUI_EVENT:
|
2024-03-20 19:35:52 +01:00
|
|
|
switch (event.GUIEvent.EventType) {
|
2023-10-03 20:37:00 +02:00
|
|
|
case EGET_ELEMENT_FOCUS_LOST:
|
|
|
|
Dragging = false;
|
|
|
|
break;
|
|
|
|
case EGET_BUTTON_CLICKED:
|
|
|
|
if (event.GUIEvent.Caller == CloseButton ||
|
2024-03-20 19:35:52 +01:00
|
|
|
event.GUIEvent.Caller == CancelButton) {
|
2023-10-03 20:37:00 +02:00
|
|
|
sendCancelEvent();
|
|
|
|
remove();
|
|
|
|
return true;
|
2024-03-20 19:35:52 +01:00
|
|
|
} else if (event.GUIEvent.Caller == OKButton) {
|
|
|
|
if (FileDirectory != L"") {
|
|
|
|
sendSelectedEvent(EGET_DIRECTORY_SELECTED);
|
2023-10-03 20:37:00 +02:00
|
|
|
}
|
2024-03-20 19:35:52 +01:00
|
|
|
if (FileName != L"") {
|
|
|
|
sendSelectedEvent(EGET_FILE_SELECTED);
|
2023-10-03 20:37:00 +02:00
|
|
|
remove();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
case EGET_LISTBOX_CHANGED: {
|
|
|
|
s32 selected = FileBox->getSelected();
|
|
|
|
if (FileList && FileSystem) {
|
|
|
|
if (FileList->isDirectory(selected)) {
|
|
|
|
setFileName("");
|
|
|
|
setDirectoryName(FileList->getFullFileName(selected));
|
|
|
|
} else {
|
|
|
|
setDirectoryName("");
|
|
|
|
setFileName(FileList->getFullFileName(selected));
|
2023-10-03 20:37:00 +02:00
|
|
|
}
|
2024-03-20 19:35:52 +01:00
|
|
|
return true;
|
2023-10-03 20:37:00 +02:00
|
|
|
}
|
2024-03-20 19:35:52 +01:00
|
|
|
} break;
|
|
|
|
|
|
|
|
case EGET_LISTBOX_SELECTED_AGAIN: {
|
|
|
|
const s32 selected = FileBox->getSelected();
|
|
|
|
if (FileList && FileSystem) {
|
|
|
|
if (FileList->isDirectory(selected)) {
|
|
|
|
setDirectoryName(FileList->getFullFileName(selected));
|
|
|
|
FileSystem->changeWorkingDirectoryTo(FileDirectory);
|
|
|
|
fillListBox();
|
|
|
|
setFileName("");
|
|
|
|
} else {
|
|
|
|
setFileName(FileList->getFullFileName(selected));
|
2023-10-03 20:37:00 +02:00
|
|
|
}
|
2024-03-20 19:35:52 +01:00
|
|
|
return true;
|
2023-10-03 20:37:00 +02:00
|
|
|
}
|
2024-03-20 19:35:52 +01:00
|
|
|
} break;
|
2023-10-03 20:37:00 +02:00
|
|
|
case EGET_EDITBOX_ENTER:
|
2024-03-20 19:35:52 +01:00
|
|
|
if (event.GUIEvent.Caller == FileNameText) {
|
|
|
|
io::path dir(FileNameText->getText());
|
|
|
|
if (FileSystem->changeWorkingDirectoryTo(dir)) {
|
2023-10-03 20:37:00 +02:00
|
|
|
fillListBox();
|
|
|
|
setFileName("");
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2024-03-20 19:35:52 +01:00
|
|
|
break;
|
2023-10-03 20:37:00 +02:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case EET_MOUSE_INPUT_EVENT:
|
2024-03-20 19:35:52 +01:00
|
|
|
switch (event.MouseInput.Event) {
|
2023-10-03 20:37:00 +02:00
|
|
|
case EMIE_MOUSE_WHEEL:
|
|
|
|
return FileBox->OnEvent(event);
|
|
|
|
case EMIE_LMOUSE_PRESSED_DOWN:
|
|
|
|
DragStart.X = event.MouseInput.X;
|
|
|
|
DragStart.Y = event.MouseInput.Y;
|
|
|
|
Dragging = true;
|
|
|
|
return true;
|
|
|
|
case EMIE_LMOUSE_LEFT_UP:
|
|
|
|
Dragging = false;
|
|
|
|
return true;
|
|
|
|
case EMIE_MOUSE_MOVED:
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
if (!event.MouseInput.isLeftPressed())
|
2023-10-03 20:37:00 +02:00
|
|
|
Dragging = false;
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
if (Dragging) {
|
2023-10-03 20:37:00 +02:00
|
|
|
// gui window should not be dragged outside its parent
|
|
|
|
if (Parent)
|
2024-03-20 19:35:52 +01:00
|
|
|
if (event.MouseInput.X < Parent->getAbsolutePosition().UpperLeftCorner.X + 1 ||
|
|
|
|
event.MouseInput.Y < Parent->getAbsolutePosition().UpperLeftCorner.Y + 1 ||
|
|
|
|
event.MouseInput.X > Parent->getAbsolutePosition().LowerRightCorner.X - 1 ||
|
|
|
|
event.MouseInput.Y > Parent->getAbsolutePosition().LowerRightCorner.Y - 1)
|
2023-10-03 20:37:00 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
move(core::position2d<s32>(event.MouseInput.X - DragStart.X, event.MouseInput.Y - DragStart.Y));
|
|
|
|
DragStart.X = event.MouseInput.X;
|
|
|
|
DragStart.Y = event.MouseInput.Y;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return IGUIElement::OnEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
//! draws the element and its children
|
|
|
|
void CGUIFileOpenDialog::draw()
|
|
|
|
{
|
|
|
|
if (!IsVisible)
|
|
|
|
return;
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
IGUISkin *skin = Environment->getSkin();
|
2023-10-03 20:37:00 +02:00
|
|
|
|
|
|
|
core::rect<s32> rect = AbsoluteRect;
|
|
|
|
|
|
|
|
rect = skin->draw3DWindowBackground(this, true, skin->getColor(EGDC_ACTIVE_BORDER),
|
2024-03-20 19:35:52 +01:00
|
|
|
rect, &AbsoluteClippingRect);
|
2023-10-03 20:37:00 +02:00
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
if (Text.size()) {
|
2023-10-03 20:37:00 +02:00
|
|
|
rect.UpperLeftCorner.X += 2;
|
|
|
|
rect.LowerRightCorner.X -= skin->getSize(EGDS_WINDOW_BUTTON_WIDTH) + 5;
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
IGUIFont *font = skin->getFont(EGDF_WINDOW);
|
2023-10-03 20:37:00 +02:00
|
|
|
if (font)
|
|
|
|
font->draw(Text.c_str(), rect,
|
|
|
|
skin->getColor(EGDC_ACTIVE_CAPTION),
|
|
|
|
false, true, &AbsoluteClippingRect);
|
|
|
|
}
|
|
|
|
|
|
|
|
IGUIElement::draw();
|
|
|
|
}
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
void CGUIFileOpenDialog::pathToStringW(irr::core::stringw &result, const irr::io::path &p)
|
2023-10-03 20:37:00 +02:00
|
|
|
{
|
|
|
|
core::multibyteToWString(result, p);
|
|
|
|
}
|
|
|
|
|
|
|
|
//! fills the listbox with files.
|
|
|
|
void CGUIFileOpenDialog::fillListBox()
|
|
|
|
{
|
|
|
|
IGUISkin *skin = Environment->getSkin();
|
|
|
|
|
|
|
|
if (!FileSystem || !FileBox || !skin)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (FileList)
|
|
|
|
FileList->drop();
|
|
|
|
|
|
|
|
FileBox->clear();
|
|
|
|
|
|
|
|
FileList = FileSystem->createFileList();
|
|
|
|
core::stringw s;
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
if (FileList) {
|
|
|
|
for (u32 i = 0; i < FileList->getFileCount(); ++i) {
|
2023-10-03 20:37:00 +02:00
|
|
|
pathToStringW(s, FileList->getFileName(i));
|
|
|
|
FileBox->addItem(s.c_str(), skin->getIcon(FileList->isDirectory(i) ? EGDI_DIRECTORY : EGDI_FILE));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
if (FileNameText) {
|
2023-10-03 20:37:00 +02:00
|
|
|
setDirectoryName(FileSystem->getWorkingDirectory());
|
|
|
|
pathToStringW(s, FileDirectory);
|
|
|
|
FileNameText->setText(s.c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//! sends the event that the file has been selected.
|
2024-03-20 19:35:52 +01:00
|
|
|
void CGUIFileOpenDialog::sendSelectedEvent(EGUI_EVENT_TYPE type)
|
2023-10-03 20:37:00 +02:00
|
|
|
{
|
|
|
|
SEvent event;
|
|
|
|
event.EventType = EET_GUI_EVENT;
|
|
|
|
event.GUIEvent.Caller = this;
|
|
|
|
event.GUIEvent.Element = 0;
|
|
|
|
event.GUIEvent.EventType = type;
|
|
|
|
Parent->OnEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
//! sends the event that the file choose process has been cancelled
|
|
|
|
void CGUIFileOpenDialog::sendCancelEvent()
|
|
|
|
{
|
|
|
|
SEvent event;
|
|
|
|
event.EventType = EET_GUI_EVENT;
|
|
|
|
event.GUIEvent.Caller = this;
|
|
|
|
event.GUIEvent.Element = 0;
|
|
|
|
event.GUIEvent.EventType = EGET_FILE_CHOOSE_DIALOG_CANCELLED;
|
|
|
|
Parent->OnEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // end namespace gui
|
|
|
|
} // end namespace irr
|