Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
filedialog.cxx
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_v7
3///
4/// \macro_code
5///
6/// \date 2019-11-01
7/// \warning This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback is welcome!
8/// \author Sergey Linev <S.Linev@gsi.de>
9
10/*************************************************************************
11 * Copyright (C) 1995-2019, Rene Brun and Fons Rademakers. *
12 * All rights reserved. *
13 * *
14 * For the licensing terms see $ROOTSYS/LICENSE. *
15 * For the list of contributors see $ROOTSYS/README/CREDITS. *
16 *************************************************************************/
17
18// Show how RFileDialog can be used in sync and async modes
19// Normally file dialogs will be used inside other widgets as ui5 dialogs
20// By default, dialog starts in async mode - means macro immediately returns to command line
21// To start OpenFile dialog in sync mode, call `root "filedialog.cxx(1)" -q`.
22// Once file is selected, root execution will be stopped
23
24
25// macro must be here to let macro work on Windows
26R__LOAD_LIBRARY(libROOTBrowserv7)
27
28#include <ROOT/RFileDialog.hxx>
29
30
31using namespace ROOT::Experimental;
32
33void filedialog(int kind = 0)
34{
35 std::string fileName;
36
37 // example of sync methods, blocks until name is selected
38 switch (kind) {
39 case 1: fileName = RFileDialog::OpenFile("OpenFile title"); break;
40 case 2: fileName = RFileDialog::SaveAs("SaveAs title", "newfile.xml"); break;
41 case 3: fileName = RFileDialog::NewFile("NewFile title", "test.txt"); break;
42 }
43
44 if (kind > 0) {
45 printf("Selected file: %s\n", fileName.c_str());
46 return;
47 }
48
49 auto dialog = std::make_shared<RFileDialog>(RFileDialog::kOpenFile, "OpenFile dialog in async mode");
50
51 dialog->SetNameFilters({ "C++ files (*.cxx *.cpp *.c *.C)", "ROOT files (*.root)", "Image files (*.png *.jpg *.jpeg)", "Text files (*.txt)", "Any files (*)" });
52
53 dialog->SetSelectedFilter("ROOT files");
54
55 // use dialog capture to keep reference until file name is selected
56 dialog->SetCallback([dialog](const std::string &res) mutable {
57 printf("Selected file: %s\n", res.c_str());
58
59 // cleanup dialog - actually not needed, lambda is cleaned up after that call anyway
60 // dialog.reset();
61 });
62
63 dialog->Show();
64}
65
#define R__LOAD_LIBRARY(LIBRARY)
Definition Rtypes.h:472