Χρησιμοποιώντας παράγωγα γραφικά συστατικά

Μπορείτε να χρησιμοποιήσετε την Glade για την διευθέτηση προσαρμοσμένων γραφικών συστατικών που παράγονται από τις κλάσεις γραφικών συστατικών gtkmm. Αυτό κρατά τον κώδικά σας οργανωμένο και ενθυλακωμένο. Φυσικά δεν θα δείτε την ακριβή εμφάνιση και τις ιδιότητες του παραγόμενου γραφικού συστατικού σας στην Glade, αλλά μπορείτε να ορίσετε τη θέση του και τα θυγατρικά γραφικά συστατικά καθώς και τις ιδιότητες της βασικής κλάσης του gtkmm.

Use Gtk::Builder::get_widget_derived() like so:

DerivedDialog* pDialog = 0;
builder->get_widget_derived("DialogBasic", pDialog);

Η παραγόμενη κλάση σας πρέπει να έχει έναν κατασκευαστή που παίρνει έναν δείκτη στον υποκείμενο τύπο C και το στιγμιότυπο Gtk::Builder. Όλες οι σχετικές κλάσεις της gtkmm ορίζουν τύπο τον υποκείμενο τύπο τους της C ως BaseObjectType (Gtk::Dialog ορίζει τύπους BaseObjectType ως GtkDialog, για παράδειγμα).

You must call the base class's constructor in the initialization list, providing the C pointer. For instance,

DerivedDialog::DerivedDialog(BaseObjectType* cobject, const Glib::RefPtr<Gtk::Builder>& builder)
: Gtk::Dialog(cobject)
{
}

You could then encapsulate the manipulation of the child widgets in the constructor of the derived class, maybe using get_widget() or get_widget_derived() again. For instance,

DerivedDialog::DerivedDialog(BaseObjectType* cobject, const Glib::RefPtr<Gtk::Builder>& builder)
: Gtk::Dialog(cobject),
  m_builder(builder),
  m_pButton(0)
{
  //Get the Glade-instantiated Button, and connect a signal handler:
  m_builder->get_widget("quit_button", m_pButton);
  if(m_pButton)
  {
    m_pButton->signal_clicked().connect( sigc::mem_fun(*this, &DerivedDialog::on_button_quit) );
  }
}

26.3.1. Παράδειγμα

Αυτό το παράδειγμα εμφανίζει πώς να φορτώσετε ένα αρχείο Glade στον χρόνο εκτέλεσης και να πώς να προσπελάσετε τα γραφικά συστατικά μέσα από μια παράγωγη κλάση.

Source Code

File: deriveddialog.h (For use with gtkmm 3, not gtkmm 2)

#ifndef GTKMM_EXAMPLE_DERIVED_DIALOG_H
#define GTKMM_EXAMPLE_DERIVED_DIALOG_H

#include <gtkmm.h>


class DerivedDialog : public Gtk::Dialog
{
public:
  DerivedDialog(BaseObjectType* cobject, const Glib::RefPtr<Gtk::Builder>& refGlade);
  virtual ~DerivedDialog();

protected:
  //Signal handlers:
  void on_button_quit();

  Glib::RefPtr<Gtk::Builder> m_refGlade;
  Gtk::Button* m_pButton;
};

#endif //GTKMM_EXAMPLE_DERIVED_WINDOW_H

File: main.cc (For use with gtkmm 3, not gtkmm 2)

#include "deriveddialog.h"
#include <iostream>

int main (int argc, char **argv)
{
  auto app = Gtk::Application::create(argc, argv, "org.gtkmm.example");

  //Load the Glade file and instiate its widgets:
  auto refBuilder = Gtk::Builder::create();
  try
  {
    refBuilder->add_from_file("derived.glade");
  }
  catch(const Glib::FileError& ex)
  {
    std::cerr << "FileError: " << ex.what() << std::endl;
    return 1;
  }
  catch(const Glib::MarkupError& ex)
  {
    std::cerr << "MarkupError: " << ex.what() << std::endl;
    return 1;
  }
  catch(const Gtk::BuilderError& ex)
  {
    std::cerr << "BuilderError: " << ex.what() << std::endl;
    return 1;
  }

  //Get the GtkBuilder-instantiated dialog::
  DerivedDialog* pDialog = nullptr;
  refBuilder->get_widget_derived("DialogDerived", pDialog);
  if(pDialog)
  {
    //Start:
    app->run(*pDialog);
  }

  delete pDialog;

  return 0;
}

File: deriveddialog.cc (For use with gtkmm 3, not gtkmm 2)

#include "deriveddialog.h"

DerivedDialog::DerivedDialog(BaseObjectType* cobject, const Glib::RefPtr<Gtk::Builder>& refGlade)
: Gtk::Dialog(cobject),
  m_refGlade(refGlade),
  m_pButton(nullptr)
{
  //Get the Glade-instantiated Button, and connect a signal handler:
  m_refGlade->get_widget("quit_button", m_pButton);
  if(m_pButton)
  {
    m_pButton->signal_clicked().connect( sigc::mem_fun(*this, &DerivedDialog::on_button_quit) ); 
  }
}

DerivedDialog::~DerivedDialog()
{
}

void DerivedDialog::on_button_quit()
{
  hide(); //hide() will cause main::run() to end.
}