Jednoduchý příklad

Abychom vás uvedli do gtkmm, začneme s nejjednodušším možným programem. Tento program vytvoří okno o rozměrech 200 × 200 pixelů.

Source Code

File: base.cc (For use with gtkmm 4)

#include <gtkmm.h>

class MyWindow : public Gtk::Window
{
public:
  MyWindow();
};

MyWindow::MyWindow()
{
  set_title("Basic application");
  set_default_size(200, 200);
}

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

  return app->make_window_and_run<MyWindow>(argc, argv);
}

Nyní si vysvětlíme jednotlivé řádky příkladu.

#include <gtkmm.h>

Všechny programy používající gtkmm musí mít vložené určité hlavičkové soubory gtkmm. gtkmm.h vloží celou sadu gtkmm. To obvykle není dobrý nápad, protože to vloží megabajty hlaviček, ale pro tento jednoduchý program nás to nemusí trápit.

The next statement:

auto app = Gtk::Application::create("org.gtkmm.examples.base");
creates a Gtk::Application object, stored in a Glib::RefPtr smartpointer. This is needed in all gtkmm applications. The create() method for this object initializes gtkmm.

Následujícídva řádky kódu vytvoří okno a nastaví jeho výchozí (počáteční) velikost:

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

The last line shows the window and enters the gtkmm main processing loop, which will finish when the window is closed. Your main() function will then return with an appropriate success or error code. The argc and argv arguments, passed to your application on the command line, can be checked when run() is called, but this simple application does not use those arguments.

return app->run(window, argc, argv);

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-4.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.