27

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?

5
  • 2
    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?
    – rid
    May 26, 2011 at 22:40
  • 1
    @hans Passant: what is a "shopping question"? May 27, 2011 at 12:22
  • 3
    @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. May 27, 2011 at 13:22
  • I wrote github.com/AndrewBelt/osdialog for this purpose.
    – Vortico
    Sep 21, 2017 at 22:39

5 Answers 5

54

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
        );

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

12
  • 1
    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?
    – Michael
    Jan 13, 2018 at 14:18
  • 3
    it's a zlib licence (the license is at the top of the source and header files). Jan 13, 2018 at 20:05
  • Awesome! Thanks.
    – Michael
    Jan 13, 2018 at 20:43
  • 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.
    – Bo Sundman
    Jul 24, 2018 at 22:07
  • 2
    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. Apr 16, 2019 at 1:25
35

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

4
  • 2
    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! Dec 5, 2014 at 7:01
  • 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. Feb 21, 2019 at 17:08
  • 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.
    – Vortico
    Mar 21, 2019 at 9:57
  • There is a native File dialog extended (github.com/btzy/nativefiledialog-extended) lib based on yours.
    – X.Arthur
    Apr 27, 2021 at 8:50
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.

1
  • 18
    You forgot Qt, which is arguably THE open source battle tested mature, ... GUI library.
    – André
    May 27, 2011 at 8:22
5

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

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. May 26, 2011 at 22:56
  • 4
    Not necessarily true. Since Qt 4.something it has LGPL license. Which basically means you can link dynamically against Qt in a commercial product.
    – André
    May 27, 2011 at 8:21
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.