C++ Gtkmm Tutorial 1

7 October , 2013 Bill C++ Programming

Introduction
This will be the first in a series of C++ Gtkmm tutorials leading up to a simple game. This tutorial will explain how to set up the development environment, create a Gtkmm application window, resize the window and rename it.

IDE Configuration
The IDE (integrated development environment) that this tutorial series will be using is Code::Blocks. It has code completion, is very easy to use and is available in the Ubuntu repositories.

sudo apt-get install codeblocks

Other IDE’s can be used and I know there will be debate on which IDE is the best but this tutorial is focused on getting started with Gtkmm not IDE choice. To understand how the IDE works we will start with an empty project.

Select from the drop down menu File, New then Project. Select the empty project template.
gtmm_tut1_project_templates

Skip the wizard welcome page. You will be now be able to name your project.
gtmm_tut1_name_project

Make sure the GNU GCC Compiler is selected for Linux applications.
gtmm_tut1_compiler_selection

When the wizard ends, select New then Empty File from the drop down menu. A dialog will ask you if you want to add the file to the active project, select yes. When you name the file, make sure it has the extension cpp. example: “main.cpp”.

Right click the project and select build options. Below I have called it “tutorial1″ (highlighted in yellow).
gtmm_tut1_workspace_tree_project

We now have to add all of the compiler and linker flags. This can be done easily using pkg-config. Add the following to both debug and release.

In Compiler settings, Other options. Add the following line:

`pkg-config gtkmm-3.0 --cflags`

gtmm_tut1_compiler_settings

In Linker settings, Other linker options. Add the following line:

`pkg-config gtkmm-3.0 --libs`

gtmm_tut1_linker_settings

When the compiler and linker settings have been completed for both release and debug, close the dialog. The IDE is now set up to program Gtkmm programs.

The Code
Copy or type the following code into the empty cpp file.

#include <gtkmm.h>

int main(int argc, char *argv[])
{
    Glib::RefPtr<Gtk::Application> app = Gtk::Application::create(argc, argv, "com.gtkmm.tutorial1.base");

    Gtk::Window window;

    window.set_default_size(400, 200);
    window.set_title("Tutorial 1");

    return app->run(window);
}

This code will create a Gtkmm application, create a GTK window, resize the window and name it.
gtmm_tut1_app

Explaining The Code

#include <gtkmm.h>

This will load the definitions of the Gtkmm functions so they can be used in the application.

int main(int argc, char *argv[])

This is a function and because it has the name main, it is the entry point of the application or in other words the first function run when the application is run. Everything in this function will be between the braces (curly brackets {}).

Glib::RefPtr<Gtk::Application> app = Gtk::Application::create(argc, argv, "com.gtkmm.tutorial1.base");

This will create a Gtkmm application and store it in the pointer app. The arguments argc and argv are also given to the Gtkmm application.

Gtk::Window window;

Here we define the window and call it window.

window.set_default_size(400, 200);

This is where the window is given the width of 400 and the height of 200.

window.set_title("Tutorial 1");

The window is given a name of “Tutorial 1″.

return app->run(window);

This is the main Gtkmm loop. The function is given the window so it can be created and handle all of the control buttons such as minimize, maximize and close. The run function will also return a result which is then returned by the main function.

To Come
Tutorial 2 will explain how to use containers and create widget objects that are managed by Gtkmm.

c#, gtkmm, gui, how to, linux,

5 Responses to “C++ Gtkmm Tutorial 1”


Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Powered by WordPress. Designed by elogi.