Απλό παράδειγμα

Για να ξεκινήσουμε την εισαγωγή μας στη gtkmm, θα αρχίσουμε με το πιο απλό δυνατό πρόγραμμα. Αυτό το πρόγραμμα θα δημιουργήσει ένα κενό παράθυρο 200 x 200 εικονοστοιχεία.

Source Code

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

#include <gtkmm.h>

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

  Gtk::Window window;
  window.set_default_size(200, 200);

  return app->run(window);
}

Θα εξηγήσουμε τώρα κάθε γραμμή του παραδείγματος

#include <gtkmm.h>

Όλα τα προγράμματα της gtkmm πρέπει να περιλαμβάνουν συγκεκριμένες κεφαλίδες της gtkmm· η gtkmm.h περιλαμβάνει το πλήρες πακέτο της gtkmm. Αυτό συνήθως δεν είναι καλή ιδέα, επειδή περιλαμβάνει ένα Mb περίπου κεφαλίδες, αλλά για απλά προγράμματα, επαρκεί.

The next statement:

Glib::RefPtr<Gtk::Application> app = Gtk::Application::create(argc, argv, "org.gtkmm.examples.base");
creates a Gtk::Application object, stored in a RefPtr smartpointer. This is needed in all gtkmm applications. The create() method for this object initializes gtkmm, and checks the arguments passed to your application on the command line, looking for standard options such as --display. It takes these from the argument list, leaving anything it does not recognize for your application to parse or ignore. This ensures that all gtkmm applications accept the same set of standard arguments.

Οι επόμενες δύο γραμμές κώδικα δημιουργούν ένα παράθυρο και ορίζουν το προεπιλεγμένο (αρχικό) μέγεθος τους:

Gtk::Window window;
window.set_default_size(200, 200);

Η τελευταία γραμμή εμφανίζει το παράθυρο και εισάγει τον κύριο βρόχο επεξεργασίας της gtkmm, που θα τελειώσει, όταν κλείσει το παράθυρο. Η συνάρτηση main(), θα επιστρέψει τότε με τον κατάλληλο κώδικα επιτυχίας ή σφάλματος.

return app->run(window);

After putting the source code in simple.cc you can compile the above program with gcc using:

g++ simple.cc -o simple `pkg-config gtkmm-3.0 --cflags --libs`
Note that you must surround the pkg-config invocation with backquotes. Backquotes cause the shell to execute the command inside them, and to use the command's output as part of the command line. Note also that simple.cc must come before the pkg-config invocation on the command line.