Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Load/Save File Dialog

Credits go to SiegeLord for helping out!
This thread is locked; no one can reply to it. rss feed Print
Load/Save File Dialog
Anomie
Member #9,403
January 2008
avatar

Is there some easy way to summon a generic load file/save file dialog from the depths of my OS? Working with the Allegro GUI seems to be about as much fun as removing my teeth with a hand-drill. (not that I've tried using any other GUI...)

It seems like these dialogs are used in every single GUI'd application ever, and it's always identical (except for GIMP...), so is there some simple function to bring one up in Windows?

______________
I am the Lorax. I speak for the trees. I speak for the trees for the trees have no tongues.

SiegeLord
Member #7,827
October 2006
avatar

Quote:

Is there some easy way to summon a generic load file/save file dialog from the depths of my OS? Working with the Allegro GUI seems to be about as much fun as removing my teeth with a hand-drill. (not that I've tried using any other GUI...)

First, an off-topic comment. I never had any trouble using allegro GUI myself, it is simple and well documented. I found the Dialog Editor to be very useful for it. But anyway.. onto your main question.

Quote:

so is there some simple function to bring one up in Windows?

Yes, here it is: [url http://msdn.microsoft.com/en-us/library/dk77e5e7(VS.80).aspx]
CFileDialog[/url].

Unfortunately for you it is very very hard (imo at least) to get that function into your program, since somehow you will have to link with MFC. Nonetheless, a search of the forums shows that this worked once for one person in 2001. He did not provide the code showing how to do that however.

Brainstorming a bit, it may be possible to use wxWidgets and use its file dialog functions (which map to native dialogs for Windows), however I do not know whether it is possible to bind a wxWindow to a pre-existing allegro window.

But basically, what you ask for is very hard to accomplish.

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

Anomie
Member #9,403
January 2008
avatar

Isn't the Allegro GUI totally blocking, until it's closed down? Otherwise, that editor is very interesting, indeed.

And thanks for the answer!

______________
I am the Lorax. I speak for the trees. I speak for the trees for the trees have no tongues.

SiegeLord
Member #7,827
October 2006
avatar

Quote:

Isn't the Allegro GUI totally blocking, until it's closed down?

Depends on what you use. If you use do_dialog, then yes, it is blocking. Usually, however, you use the:init_dialog/update_dialog/shutdown_dialog
trio.

Basically, you call the init_dialog on your DIALOG* array when you wish to invoke/initialize the GUI, and then in your game loop you call update_dialog on the DIALOG_PLAYER. Later on you call the shutdown_dialog to clean up.

I can't seem to find a good tutorial on this unfortunately... although I had little trouble using this. (Although I did have to force it to draw by calling dialog_message with the MSG_DRAW message every time before I did update_dialog)...

EDIT: If you end up sticking with Allegro's gui, I highly recommend AGUP to make it look a little nicer.

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

Anomie
Member #9,403
January 2008
avatar

Well, it's good to hear that it's not so bad. And that editor kinda makes it more like brushing my teeth than removing them. I'm still in a bit of trouble though. How would I make a normal menu, with sub-menus and such? Is there some method of naming each element that I'm not aware of?

[edit] Writing that, I figured I'd just look through the examples to figure out how to replicate that generic file dialog window in Allegro, but I can't find any examples using it... Any help with that one?

______________
I am the Lorax. I speak for the trees. I speak for the trees for the trees have no tongues.

Timorg
Member #2,028
March 2002

Something I thought I would whip something up. The joy of having done some windows programing before.

So name this "main.c"

1#include <allegro.h>
2#include <winalleg.h>
3 
4int main()
5{
6 char savename[1025];
7 
8 
9 OPENFILENAME tt;
10 tt.lStructSize = sizeof(OPENFILENAME);
11 tt.hwndOwner = NULL;
12 tt.lpstrFilter = "All Files\0*.*\0\0";
13 tt.lpstrCustomFilter = NULL;
14 tt.nFilterIndex = 0;
15 tt.lpstrFile = savename;
16 tt.nMaxFile = 1025;
17 tt.lpstrFileTitle = NULL;
18 tt.lpstrInitialDir = NULL;
19 tt.lpstrTitle = NULL;
20 tt.Flags = 0;
21 tt.lpstrDefExt = NULL;
22 
23 #if (_WIN32_WINNT >= 0x0500)
24 tt.pvReserved = NULL;
25 tt.dwReserved = 0;
26 tt.FlagsEx = 0;
27 #endif // (_WIN32_WINNT >= 0x0500)
28
29 strcpy(savename, "save.bin");
30 
31 LPOPENFILENAME t = &tt;
32 
33 allegro_init();
34 install_keyboard();
35 set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
36 
37 tt.hwndOwner = win_get_window();
38 
39 GetOpenFileName(t);
40 
41 allegro_message("%s", savename);
42 
43 readkey();
44 
45 return 0;
46 
47}END_OF_MAIN()

Then in mingw go...

gcc -odialog.exe main.c -lalleg -lcomdlg32

For more information about the function and the struct look at

GetOpenFileName http://msdn.microsoft.com/en-us/library/ms646927(VS.85).aspx
OPENFILENAME http://msdn.microsoft.com/en-us/library/ms646839(VS.85).aspx
GetSaveFileName http://msdn.microsoft.com/en-us/library/ms646928(VS.85).aspx

You should read through the docs, to make sure I haven't made a mistake in understanding OPENFILENAME.

This assumes everything is in ansi strings, but I call the ansi function so all good. You might need to tweak stuff if you want this to work under NT you need to detect it and use "OPENFILENAME_SIZE_VERSION_400" not "sizeof(OPENFILENAME)"

I hope this helps, I haven't used this function in a long while.

If anyone does end up using this in something complete a thanks in a doc wouldn't go astray :P You don't have to btw. :D

If you have problems, post them and I will help where I can.

____________________________________________________________________________________________
"c is much better than c++ if you don't need OOP simply because it's smaller and requires less load time." - alethiophile
OMG my sides are hurting from laughing so hard... :D

SiegeLord
Member #7,827
October 2006
avatar

Aha, I knew that only working with MFC would come to haunt me some day. Now, if only we can replicate this for GNOME, KDE and whatever Mac uses, we can make this a part of Allegro 5! :D

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

Timorg
Member #2,028
March 2002

I would be willing to attempt it in gnome and kde (when the computer comes back from the shop), but I don't have a mac to do that part.

I'm not sure implementing it based on desktop environments is that good an idea, we need something that will work in fluxbox without gnome or kde stuff installed, my current setup. :D

This doesn't really belong in allegro core anyway, it should be an addon.

____________________________________________________________________________________________
"c is much better than c++ if you don't need OOP simply because it's smaller and requires less load time." - alethiophile
OMG my sides are hurting from laughing so hard... :D

Thomas Fjellstrom
Member #476
June 2000
avatar

Quote:

This doesn't really belong in allegro core anyway, it should be an addon.

If freedesktop.org would get something done for once, there would be a xdg-file-dialog command you could shell out to on most distros. But sadly, most of the people involved with fd.org are gnome peeps, and they don't seem to like finishing anything.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

SiegeLord
Member #7,827
October 2006
avatar

Quote:

This doesn't really belong in allegro core anyway, it should be an addon.

Naturally not, most everything seems to be an addon for Allegro 5 anyway. :)

Quote:

I'm not sure implementing it based on desktop environments is that good an idea, we need something that will work in fluxbox without gnome or kde stuff installed, my current setup.

The idea is to have an easy to use function able to call up the native file open dialog if possible for the current system. I guess in Fluxbox's case, if it doesn't have such a standard file open dialog (I don't have much experience with Fluxbox), some sort of Allegro GUI dialog would be called as a fallback option.

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

Timorg
Member #2,028
March 2002

Oh just a thought here is the src and compiled example. Clicky. (Its compiled against 4.2.2)

Edit: I could install the gnome stuff and the kde stuff, its automatically installed with a gnome or kde app anyway, but small is the important thing for this system. So only gtk apps here.

Edit: I just noticed I missed out on cookies. :P

____________________________________________________________________________________________
"c is much better than c++ if you don't need OOP simply because it's smaller and requires less load time." - alethiophile
OMG my sides are hurting from laughing so hard... :D

Anomie
Member #9,403
January 2008
avatar

Awesome! That looks like it's just what I needed. Compiles fine with MinGW, too. (that was certainly an issue with MFC stuff...) Just gotta figure out what's going on...then I'll be set!

Um... * awards Schrodinger's Cookie? *

Just don't look at it.

[edit]And I'm not too worried about the NT bit...if I was wanting it to run on anything but my machine, I'd have to find something more portable anyways.

______________
I am the Lorax. I speak for the trees. I speak for the trees for the trees have no tongues.

Go to: