cross platform native open/save file dialogs
Asked Answered
L

6

31

I'm writing a UI for my program using opengl with SDL in a combination of Lua and C++

What I need now is some library that will allow me to call a function that presents the user with a file select dialog for opening/saving a file. But if the OS offers native functionality for such a dialog, then I want to use that dialog (eg Window's GetOpenFileName).

The only platforms I need to support are Windows and Linux, but I want to be able to still use most of the SDL & openGL code I've already written.

What options are available?

Leelah answered 26/5, 2011 at 22:36 Comment(5)
If you only need two platforms and the code for each is simple and straight forward, why not simply have 2 separate methods, each for a certain platform, and call the appropriate one?Amygdala
@hans Passant: what is a "shopping question"?Magistral
@Bryan: blog.stackoverflow.com/2010/11/qa-is-hard-lets-go-shoppingTenon
@Bryan: "I'd like to buy a pony. It must be free, go 100 miles on one bale of straw. When you find one, I can arbitrarily reject it because I forgot to say that it needs a horn". That's a shopping question.Varro
I wrote github.com/AndrewBelt/osdialog for this purpose.Excretory
A
8

In my opinion, wxWidgets is the open source, battle tested, mature, cross platform, cross language, free, open source, GUI library. Beside wxWidgets, there are also many other such cross platform libraries, such as:

... and more.

You will most likely find you're looking for in the AbiWord source code. Look for "FileOpen" inside the src directory:

./af/xap/cocoa/xap_CocoaDlg_FileOpenSaveAs.cpp
./af/xap/cocoa/xap_CocoaDlg_FileOpenSaveAs.h
./af/xap/cocoa/xap_CocoaFileOpen_Views.nib
./af/xap/cocoa/xap_CocoaFileOpen_Views.nib/classes.nib
./af/xap/cocoa/xap_CocoaFileOpen_Views.nib/info.nib
./af/xap/cocoa/xap_CocoaFileOpen_Views.nib/keyedobjects.nib
./af/xap/gtk/xap_UnixDlg_FileOpenSaveAs.cpp
./af/xap/gtk/xap_UnixDlg_FileOpenSaveAs.h
./af/xap/win/xap_Win32Dlg_FileOpenSaveAs.cpp
./af/xap/win/xap_Win32Dlg_FileOpenSaveAs.h
./af/xap/xp/xap_Dlg_FileOpenSaveAs.cpp
./af/xap/xp/xap_Dlg_FileOpenSaveAs.h

The Windows version does indeed use GetOpenFileName().


Of course, as Andre points out while downvoting a valid alternative answer, there's also Qt, as you already know. Refer to raj's answer for a link.

Amygdala answered 26/5, 2011 at 23:26 Comment(1)
You forgot Qt, which is arguably THE open source battle tested mature, ... GUI library.Caveator
B
70

tinyfiledialogs offers many modal dialogs and popup notifications (both for graphic and console modes). It is a cross-platform single C/C++ file (+ header) to add to your C or C++ project. It aims to be extremely easy to use, has no init, no main loop, and no external dependencies. It is used by hundreds of projects on GitHub and of course, I am the author. Get it here: http://tinyfiledialogs.sourceforge.net

Usage example:

    // there is also a wchar_t version
    char const * selection = tinyfd_openFileDialog(
        "Select file", // title
        "C:\\", // optional initial directory
        2, // number of filter patterns
        lFilterPatterns, // char const * lFilterPatterns[2] = { "*.txt", "*.jpg" };
        NULL, // optional filter description
        0 // forbids multiple selections
        );

tinyfiledialogs handles char strings as UTF-8 (on windows and on unix). On Windows, It also offers UTF-16 / wchar_t functions.

*** v3.10 (2023): NEW FORTRAN module fully implemented with examples ***

*** v3.13 (2023): NEW PASCAL unit fully implemented with examples ***

*** v3.14 (2023): NEW R inteface fully implemented with examples ***

Bezel answered 5/12, 2017 at 10:33 Comment(13)
Sorry to be annoying, but I took a quick look and it and it looks awesome. I did not see a license included in the source. Can I do whatever I want with it?Billionaire
it's a zlib licence (the license is at the top of the source and header files).Bezel
Awesome! Thanks.Billionaire
I needed an open/save popup windows routine in my Fortran software and I found tinyfiledialog and managed to write an iso-c-interface that works nicely on WIndows gfortran. I had some problem porting this to Linux but I got some very useful help from the author.Adhesion
I am trying it out on macOS. I can't use it to open a file for read. All files with the selected extension are greyed out.Ukase
Also, your popen call in tinyfd_saveFileDialog was called with a "r" for read instead of a "w" for write.Ukase
With tinyfiledialogs, there is a test file "hello.c". This file compiles and runs perfectly with all the versions of macos. Why don't you send me an email (the address is at the top of all the source files) which details how you are calling the functions. Also, I am not sure which popen you are referring to (It is normal for popen to be open as "r" as we are reading from the input stream not writting in it. keep in mind that tinyfiledialogs won't save anything for you, it will simply return a filename). Please, get in touch.Bezel
Although i won't, I feel compelled to down-vote simply because your SoureForge link leads here for annoying and shameless vote farming, instead of simply providing the code. For that reason I will continue to look for something alternative, and give an upvote to the answers above this one.Calicle
Why i cannot save file in folder which name contains russian letters?Discomfortable
I believe it should work. can you get in touch via the email address at the top of the files? thanks. which version are you using ? (make sure you get it from the sourceforge site. there are many old versions on github). on which platform ? if on windows: are you usng the wchar calls, the UTF-8 or the MBCS ones ?Bezel
I do realise that this is an old comment, but it might help out someone. @Ukase I've had the same problem, but after skimming through examples, I realised that filters need to be specified as *.<file extension> and not just extension by itself.Breechblock
What are the pros/cons compared to native file dialog extended? (github.com/btzy/nativefiledialog-extended)Enochenol
github.com/LWJGL/lwjgl3/blob/master/modules/lwjgl/tinyfd/src/…Machiavelli
H
39

You should take a good look at Native File Dialog. It is a small, portable C library that lets you use load and save dialogs in a cross platform manner without linking huge dependencies like qt or wxWidgets.

I am the author, and I use it with SDL2 and OpenGL on Linux, Mac and Windows.

https://github.com/mlabbe/nativefiledialog

Household answered 4/12, 2014 at 3:30 Comment(4)
Wow, that looks fantastic. Win/OSX/Linux & gcc/clang/VS support, UTF-8, no other dependencies, and on top of all that, a friendly Zlib license? Incredible!Leelah
Here in 2019: I have updated the library to be compatible with recent operating system updates and fix a handful of bugs. If you use it previously, just recompile it and you'll reap the benefits! No api changes.Pires
I've been maintaining an alternative github.com/AndrewBelt/osdialog for a few years. It's similar but has other dialogs such as messages/prompts and color pickers.Excretory
There is a native File dialog extended (github.com/btzy/nativefiledialog-extended) lib based on yours.Enochenol
A
8

In my opinion, wxWidgets is the open source, battle tested, mature, cross platform, cross language, free, open source, GUI library. Beside wxWidgets, there are also many other such cross platform libraries, such as:

... and more.

You will most likely find you're looking for in the AbiWord source code. Look for "FileOpen" inside the src directory:

./af/xap/cocoa/xap_CocoaDlg_FileOpenSaveAs.cpp
./af/xap/cocoa/xap_CocoaDlg_FileOpenSaveAs.h
./af/xap/cocoa/xap_CocoaFileOpen_Views.nib
./af/xap/cocoa/xap_CocoaFileOpen_Views.nib/classes.nib
./af/xap/cocoa/xap_CocoaFileOpen_Views.nib/info.nib
./af/xap/cocoa/xap_CocoaFileOpen_Views.nib/keyedobjects.nib
./af/xap/gtk/xap_UnixDlg_FileOpenSaveAs.cpp
./af/xap/gtk/xap_UnixDlg_FileOpenSaveAs.h
./af/xap/win/xap_Win32Dlg_FileOpenSaveAs.cpp
./af/xap/win/xap_Win32Dlg_FileOpenSaveAs.h
./af/xap/xp/xap_Dlg_FileOpenSaveAs.cpp
./af/xap/xp/xap_Dlg_FileOpenSaveAs.h

The Windows version does indeed use GetOpenFileName().


Of course, as Andre points out while downvoting a valid alternative answer, there's also Qt, as you already know. Refer to raj's answer for a link.

Amygdala answered 26/5, 2011 at 23:26 Comment(1)
You forgot Qt, which is arguably THE open source battle tested mature, ... GUI library.Caveator
O
5

https://doc.qt.io/qt-5/qfiledialog.html provides a good abstraction of os file open services

Omnipotence answered 26/5, 2011 at 22:44 Comment(2)
I may be misreading this, but it appears that I must purchase a license to use Qt commercially, and I would prefer something free.Leelah
Not necessarily true. Since Qt 4.something it has LGPL license. Which basically means you can link dynamically against Qt in a commercial product.Caveator
K
1

I have used another good library:

https://github.com/samhocevar/portable-file-dialogs

It is a single header library with support for Linux / Mac / Windows

Koski answered 13/9, 2021 at 16:10 Comment(0)
M
0

I developed my own C library for native dialogs. It works on Windows, macOS and Linux, and provides all sorts of dialogs. It should do the job if you're looking for that exactly. Click here to see the repository.

Mesne answered 24/4, 2023 at 21:9 Comment(2)
Does it support opening file dialog?Secretary
yes, it does, for both saving and opening a file.Mesne

© 2022 - 2024 — McMap. All rights reserved.