cross platform native open/save file dialogs
Solution 1
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.
Solution 2
tinyfiledialogs offers many modal dialogs and popup notifications (both for graphic and console modes). It is a cross-platform C file (with a 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:
char const * selection = tinyfd_openFileDialog( // there is also a wchar_t version
"Select file", // title
"C:\\", // optional initial directory
2, // number of filter patterns
lFilterPatterns, // char const * lFilterPatterns[2] = { "*.txt", "*.jpg" };
NULL, // optional filter description
0 // forbid multiple selections
);
Solution 3
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
Solution 4
https://doc.qt.io/qt-5/qfiledialog.html provides a good abstraction of os file open services
Solution 5
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
Related videos on Youtube
Ponkadoodle
Updated on September 14, 2021Comments
-
Ponkadoodle over 2 years
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?
-
rid almost 13 yearsIf 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?
-
Russell Smith almost 13 years@hans Passant: what is a "shopping question"?
-
André Caron almost 13 years
-
user1703401 almost 13 years@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.
-
Vortico over 6 yearsI wrote github.com/AndrewBelt/osdialog for this purpose.
-
-
Ponkadoodle almost 13 yearsI may be misreading this, but it appears that I must purchase a license to use Qt commercially, and I would prefer something free.
-
André almost 13 yearsNot necessarily true. Since Qt 4.something it has LGPL license. Which basically means you can link dynamically against Qt in a commercial product.
-
André almost 13 yearsYou forgot Qt, which is arguably THE open source battle tested mature, ... GUI library.
-
Ponkadoodle over 9 yearsWow, 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!
-
Michael over 6 yearsSorry 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?
-
tinyfiledialogs over 6 yearsit's a zlib licence (the license is at the top of the source and header files).
-
Michael over 6 yearsAwesome! Thanks.
-
Bo Sundman almost 6 yearsI 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.
-
ohthepain about 5 yearsI 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.
-
ohthepain about 5 yearsAlso, your popen call in tinyfd_saveFileDialog was called with a "r" for read instead of a "w" for write.
-
tinyfiledialogs about 5 yearsWith 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.
-
noway about 5 yearsHere 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.
-
Vortico about 5 yearsI'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.
-
ForeverZer0 about 5 yearsAlthough 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.
-
So_oP about 4 yearsWhy i cannot save file in folder which name contains russian letters?
-
tinyfiledialogs about 4 yearsI 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 ?
-
MG lolenstine about 3 yearsI do realise that this is an old comment, but it might help out someone. @ohthepain 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. -
X.Arthur about 3 yearsWhat are the pros/cons compared to
native file dialog extended
? (github.com/btzy/nativefiledialog-extended) -
X.Arthur about 3 yearsThere is a
native File dialog extended
(github.com/btzy/nativefiledialog-extended) lib based on yours.