Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
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 #include <ROOT/RFileDialog.hxx>
19 
20 // Show how RFileDialog can be used in sync and async modes
21 // Normally file dialogs will be used inside other widgets as ui5 dialogs
22 // By default, dialog starts in async mode - means macro immediately returns to command line
23 // To start OpenFile dialog in sync mode, call `root "filedialog.cxx(1)" -q`.
24 // Once file is selected, root execution will be stopped
25 
26 
27 using namespace ROOT::Experimental;
28 
29 void filedialog(int kind = 0)
30 {
31  std::string fileName;
32 
33  // example of sync methods, blocks until name is selected
34  switch (kind) {
35  case 1: fileName = RFileDialog::OpenFile("OpenFile title"); break;
36  case 2: fileName = RFileDialog::SaveAs("SaveAs title", "newfile.xml"); break;
37  case 3: fileName = RFileDialog::NewFile("NewFile title", "test.txt"); break;
38  }
39 
40  if (kind > 0) {
41  printf("Selected file: %s\n", fileName.c_str());
42  return;
43  }
44 
45  auto dialog = std::make_shared<RFileDialog>(RFileDialog::kOpenFile, "OpenFile dialog in async mode");
46 
47  dialog->SetNameFilters({ "C++ files (*.cxx *.cpp *.c *.C)", "Image files (*.png *.jpg *.jpeg)", "Text files (*.txt)", "Any files (*)" });
48 
49  dialog->SetSelectedFilter("C++ files");
50 
51  // use dialog capture to keep reference until file name is selected
52  dialog->SetCallback([dialog](const std::string &res) mutable {
53  printf("Selected file: %s\n", res.c_str());
54 
55  // cleanup dialog - actually not needed, lambda is cleaned up after that call anyway
56  // dialog.reset();
57  });
58 
59  dialog->Show();
60 }
61