I while ago I was looking for an open source folder selector for win32 based on CEGUI, but I didn't found anyone.
So I decided to make my own, and share it with others. This is how it looks:
It's pretty simple so far, but if you just need a folder navigator that allows you to move trough directories and logical units, it can do the job.
I've tried to keep the things as simple as possible for the example purpose in the downloadable file below:
Download the MSVC2k8 project & source<--Contains Source and a pre-compiled example in folder bin
Wikified version<--Wikified documentation.
Folder selector is showed in a CEGUI Window, using the method:
Code: Select all
void show(CEGUI::Window* root);
Where root is the window where you want the folder selector to be showed.
Here you've got the Folder Selector header. As you can see, it uses DiskObject, wich is a small class without no more dependencies than "windows.h" and stl.
Code: Select all
#pragma once
#include <CEGUI.h>
#include "DiskObject.h"
class GUI_FolderSelector
{
public:
GUI_FolderSelector();
~GUI_FolderSelector(void);
/**
@short makes the Folder selector to appear in CEGUI::Window
*/
void show(CEGUI::Window* root);
/**
@short hide Folder Selector
*/
void hide();
/**
@short Returns the last selected file
*/
std::string getResult();
/**
@short set the current folder
*/
void setFolder(std::string);
/**
@short sets the filter string. The string will be used to enable/disable OK
button when files in the current folder matches the filter string
(i.e., "*.jpg" string will enable OK button only when the current folder
contains files with extension .jpg). By default is "*", showing all files.
*/
void setFileFiler(std::string s){d_fileFilter = s;}
private:
bool handleClick(const CEGUI::EventArgs& e);
void wireEvents();
void updateFolderList();
DiskObject* disk;
CEGUI::Window* frame;
std::string d_result;
std::string d_fileFilter;
std::vector<std::string> d_itemList;
};
This is how main loop looks:
Code: Select all
#include "SDL.h"
#include "GUI_Menu.h"
#include "GUI_FolderSelector.h"
#define QUIT -1
#define MENU 0
#define FOLDER_SELECTOR 1
GUI_FolderSelector fs;
void init()
{
// ... This is just an example, complete code in downloadable file
}
void setupCEGUI()
{
// ... This is just an example, complete code in downloadable file
}
bool handleClick(const CEGUI::EventArgs &e)
{
using namespace CEGUI;
WindowManager& winMgr = WindowManager::getSingleton();
Window* btn = static_cast<Window*>(winMgr.getWindow("Root/Play"));
btn->setText(fs.getResult());
return 0;
}
int main(int argc, char **argv)
{
using namespace CEGUI;
// init SDL/OpenGL Framework
init();
// init CEGUI
setupCEGUI();
// create main menu
WindowManager& winMgr = WindowManager::getSingleton();
GUI_Menu menu("menu.layout");
//Main loop
do
{
menu.injectInput();
menu.render();
if (menu.getState() == FOLDER_SELECTOR)
{
// makes appear the folder selector
fs.show(CEGUI::System::getSingleton().getGUISheet());
// Subscribe OK button event
winMgr.getWindow("FolderSelector/Frame/Ok")->
subscribeEvent(PushButton::EventClicked,
Event::Subscriber(&handleClick));
// back to menu (fs will auto-hide when done)
menu.setState(MENU);
}
SDL_Delay(10);
} while (menu.getState() != QUIT);
return 0;
}
I know this is not the piece of code that is going to save the world, but it can (hopefully) save some minutes to other developers.
I want to thank CEGUI team for this magnificent library. A revision from you to my code will be a very good feedback
Any other feedback is also welcomed.