Opening files

In this step, we make our application show the contents of all the files that it is given on the commandline.

To this end, we add a data member to our application window and keep a pointer to the Gtk::Stack there. We get the pointer with a call to Gtk::Builder::get_widget() in the application window's constructor.

Now we revisit the ExampleAppWindow::open_file_view() method that is called for each commandline argument, and construct a Gtk::TextView that we then add as a page to the stack.

Lastly, we add a Gtk::StackSwitcher to the titlebar area in the ui file, and we tell it to display information about our stack.

The stack switcher gets all its information it needs to display tabs from the stack that it belongs to. Here, we are passing the label to show for each file as the last argument to the Gtk::Stack::add() method.

Our application is beginning to take shape:

Figure 29-3Opening files

Source Code

File: exampleapplication.h (For use with gtkmm 4)

#include "../step1/exampleapplication.h"
// Equal to the corresponding file in step1

File: exampleappwindow.h (For use with gtkmm 4)


#include <gtkmm.h>

class ExampleAppWindow : public Gtk::ApplicationWindow
  ExampleAppWindow(BaseObjectType* cobject,
    const Glib::RefPtr<Gtk::Builder>& refBuilder);

  static ExampleAppWindow* create();

  void open_file_view(const Glib::RefPtr<Gio::File>& file);

  Glib::RefPtr<Gtk::Builder> m_refBuilder;
  Gtk::Stack* m_stack;


File: (For use with gtkmm 4)

#include "../step1/"
// Equal to the corresponding file in step1

File: (For use with gtkmm 4)

#include "exampleappwindow.h"
#include <iostream>
#include <stdexcept>

ExampleAppWindow::ExampleAppWindow(BaseObjectType* cobject,
  const Glib::RefPtr<Gtk::Builder>& refBuilder)
: Gtk::ApplicationWindow(cobject),
  m_stack = m_refBuilder->get_widget<Gtk::Stack>("stack");
  if (!m_stack)
    throw std::runtime_error("No \"stack\" object in window.ui");

ExampleAppWindow* ExampleAppWindow::create()
  // Load the Builder file and instantiate its widgets.
  auto refBuilder = Gtk::Builder::create_from_resource("/org/gtkmm/exampleapp/window.ui");

  auto window = Gtk::Builder::get_widget_derived<ExampleAppWindow>(refBuilder, "app_window");
  if (!window)
    throw std::runtime_error("No \"app_window\" object in window.ui");

  return window;

void ExampleAppWindow::open_file_view(const Glib::RefPtr<Gio::File>& file)
  const Glib::ustring basename = file->get_basename();

  auto scrolled = Gtk::make_managed<Gtk::ScrolledWindow>();
  auto view = Gtk::make_managed<Gtk::TextView>();
  m_stack->add(*scrolled, basename, basename);

    char* contents = nullptr;
    gsize length = 0;
    file->load_contents(contents, length);
    view->get_buffer()->set_text(contents, contents+length);
  catch (const Glib::Error& ex)
    std::cout << "ExampleAppWindow::open_file_view(\"" << file->get_parse_name()
      << "\"):\n  " << ex.what() << std::endl;

File: (For use with gtkmm 4)

#include "../step2/"
// Equal to the corresponding file in step2

File: window.ui (For use with gtkmm 4)

<?xml version="1.0" encoding="UTF-8"?>
  <object class="GtkApplicationWindow" id="app_window">
    <property name="title" translatable="yes">Example Application</property>
    <property name="default-width">600</property>
    <property name="default-height">400</property>
    <property name="hide-on-close">True</property>
    <child type="titlebar">
      <object class="GtkHeaderBar" id="header">
        <child type="title">
          <object class="GtkStackSwitcher" id="tabs">
            <property name="stack">stack</property>
      <object class="GtkBox" id="content_box">
        <property name="orientation">vertical</property>
          <object class="GtkStack" id="stack"/>