[gtkmm-documentation/gtkmm-3-24] Add Gtk::FileChooserNative example



commit d2c7a50e8215493b5f1d40c42354cfd84fc3eaba
Author: Kjell Ahlstedt <kjellahlstedt gmail com>
Date:   Wed Nov 7 16:49:25 2018 +0100

    Add Gtk::FileChooserNative example
    
    Bug 783801

 examples/Makefile.am                               |  6 ++
 .../dialogs/filechoosernative/examplewindow.cc     | 76 ++++++++++++++++++++++
 .../book/dialogs/filechoosernative/examplewindow.h | 37 +++++++++++
 examples/book/dialogs/filechoosernative/main.cc    | 27 ++++++++
 4 files changed, 146 insertions(+)
---
diff --git a/examples/Makefile.am b/examples/Makefile.am
index a34dc70..74142f0 100644
--- a/examples/Makefile.am
+++ b/examples/Makefile.am
@@ -39,6 +39,7 @@ check_PROGRAMS =                                      \
        book/dialogs/aboutdialog/example                \
        book/dialogs/colorchooserdialog/example \
        book/dialogs/filechooserdialog/example          \
+       book/dialogs/filechoosernative/example          \
        book/dialogs/fontchooserdialog/example  \
        book/dialogs/messagedialog/example              \
        book/dialogs/simple/example                     \
@@ -310,6 +311,11 @@ book_dialogs_filechooserdialog_example_SOURCES =   \
        book/dialogs/filechooserdialog/examplewindow.h  \
        book/dialogs/filechooserdialog/main.cc
 
+book_dialogs_filechoosernative_example_SOURCES =       \
+       book/dialogs/filechoosernative/examplewindow.cc \
+       book/dialogs/filechoosernative/examplewindow.h  \
+       book/dialogs/filechoosernative/main.cc
+
 book_dialogs_fontchooserdialog_example_SOURCES =               \
        book/dialogs/fontchooserdialog/examplewindow.cc \
        book/dialogs/fontchooserdialog/examplewindow.h  \
diff --git a/examples/book/dialogs/filechoosernative/examplewindow.cc 
b/examples/book/dialogs/filechoosernative/examplewindow.cc
new file mode 100644
index 0000000..986c2b6
--- /dev/null
+++ b/examples/book/dialogs/filechoosernative/examplewindow.cc
@@ -0,0 +1,76 @@
+/* gtkmm example Copyright (C) 2017 gtkmm development team
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "examplewindow.h"
+#include <iostream>
+
+ExampleWindow::ExampleWindow()
+: m_ButtonBox(Gtk::ORIENTATION_VERTICAL),
+  m_Button_File_Open("Choose File To Open"),
+  m_Button_File_Save("Choose File To Save")
+{
+  set_title("Gtk::FileChooserNative example");
+
+  add(m_ButtonBox);
+
+  m_ButtonBox.pack_start(m_Button_File_Open, Gtk::PACK_EXPAND_WIDGET);
+  m_Button_File_Open.signal_clicked().connect(sigc::bind(sigc::mem_fun(*this,
+    &ExampleWindow::on_button_file_clicked), Gtk::FILE_CHOOSER_ACTION_OPEN));
+
+  m_ButtonBox.pack_start(m_Button_File_Save, Gtk::PACK_EXPAND_WIDGET);
+  m_Button_File_Save.signal_clicked().connect(sigc::bind(sigc::mem_fun(*this,
+    &ExampleWindow::on_button_file_clicked), Gtk::FILE_CHOOSER_ACTION_SAVE));
+
+  show_all();
+}
+
+ExampleWindow::~ExampleWindow()
+{
+}
+
+void ExampleWindow::on_button_file_clicked(Gtk::FileChooserAction action)
+{
+  const bool open = (action == Gtk::FILE_CHOOSER_ACTION_OPEN);
+  auto dialog = Gtk::FileChooserNative::create("Please choose a file", *this, action);
+
+  // Show the dialog and wait for a user response:
+  const int result = dialog->run();
+
+  // Handle the response:
+  switch (result)
+  {
+  case Gtk::RESPONSE_ACCEPT:
+  {
+    std::cout << (open ? "Open" : "Save") << " clicked." << std::endl;
+
+    // Notice that this is a std::string, not a Glib::ustring.
+    auto filename = dialog->get_filename();
+    std::cout << "File selected: " <<  filename << std::endl;
+    break;
+  }
+
+  case Gtk::RESPONSE_CANCEL:
+    std::cout << "Cancel clicked." << std::endl;
+    break;
+
+  case Gtk::RESPONSE_DELETE_EVENT:
+    std::cout << "Dialog closed." << std::endl;
+    break;
+
+  default:
+    std::cout << "Unexpected button clicked." << std::endl;
+    break;
+  }
+}
diff --git a/examples/book/dialogs/filechoosernative/examplewindow.h 
b/examples/book/dialogs/filechoosernative/examplewindow.h
new file mode 100644
index 0000000..dda64bf
--- /dev/null
+++ b/examples/book/dialogs/filechoosernative/examplewindow.h
@@ -0,0 +1,37 @@
+/* gtkmm example Copyright (C) 2017 gtkmm development team
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef GTKMM_EXAMPLEWINDOW_H
+#define GTKMM_EXAMPLEWINDOW_H
+
+#include <gtkmm.h>
+
+class ExampleWindow : public Gtk::Window
+{
+public:
+  ExampleWindow();
+  virtual ~ExampleWindow();
+
+protected:
+  // Signal handlers:
+  void on_button_file_clicked(Gtk::FileChooserAction action);
+
+  // Child widgets:
+  Gtk::ButtonBox m_ButtonBox;
+  Gtk::Button m_Button_File_Open;
+  Gtk::Button m_Button_File_Save;
+};
+
+#endif // GTKMM_EXAMPLEWINDOW_H
diff --git a/examples/book/dialogs/filechoosernative/main.cc b/examples/book/dialogs/filechoosernative/main.cc
new file mode 100644
index 0000000..629c67a
--- /dev/null
+++ b/examples/book/dialogs/filechoosernative/main.cc
@@ -0,0 +1,27 @@
+/* gtkmm example Copyright (C) 2017 gtkmm development team
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "examplewindow.h"
+#include <gtkmm/application.h>
+
+int main(int argc, char* argv[])
+{
+  auto app = Gtk::Application::create("org.gtkmm.example");
+
+  ExampleWindow window;
+
+  // Shows the window and returns when it is closed.
+  return app->run(window, argc, argv);
+}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]