issue220:tutoriel1
Différences
Ci-dessous, les différences entre deux révisions de la page.
| Prochaine révision | Révision précédente | ||
| issue220:tutoriel1 [2025/08/24 17:09] – créée philou511 | issue220:tutoriel1 [2025/08/31 08:15] (Version actuelle) – d52fr | ||
|---|---|---|---|
| Ligne 1: | Ligne 1: | ||
| - | GTK4 Programming in C - Part 1 Getting Started | + | **GTK4 Programming in C - Part 1 Getting Started |
| - | Written by Alan Crispin | + | Written by Alan Crispin** |
| - | Introduction | + | //Bandeau de titre : // |
| - | This article is the first in a series on how to develop GTK4 applications using C. Some small programs will be developed to show how GTK4 and associated libraries can be used for Graphical User Interface (GUI) programming. In this first article, GTK4 is introduced. It shows how to install GTK4, the C compiler, and the GNU Make build automation tool. A small demo application is developed to demonstrate how to create a window and use button and label widgets. A screenshot is shown below. | ||
| + | **Introduction | ||
| + | This article is the first in a series on how to develop GTK4 applications using C. Some small programs will be developed to show how GTK4 and associated libraries can be used for Graphical User Interface (GUI) programming. In this first article, GTK4 is introduced. It shows how to install GTK4, the C compiler, and the GNU Make build automation tool. A small demo application is developed to demonstrate how to create a window and use button and label widgets. A screenshot is shown below. | ||
| + | Throughout this series, Ubuntu 24.04 will be used which uses GTK4 version GTK4.14. The series assumes that the reader has a working knowledge of the C programming language. There are many online tutorials and videos on C programming. | ||
| + | The full source code for this project can be downloaded using the web link below. | ||
| + | https:// | ||
| + | Introduction | ||
| + | Cet article est le premier d'une série sur le développement d' | ||
| - | Throughout this series, Ubuntu 24.04 will be used which uses GTK4 version GTK4.14. | + | Tout au long de cette série, nous utiliserons |
| - | The full source | + | Le code source complet de ce projet peut être téléchargé via le lien ci-dessous. |
| https:// | https:// | ||
| - | GTK4 | + | |
| + | **GTK4 | ||
| GTK4 is a library for creating graphical user interfaces which is widely available in Linux distribution repositories. It is a portable toolkit meaning that if applications are written for one platform they can be ported to another. Linux, of course, is the preferred platform. Although C will be used in these articles, GTK4 has bindings for other programming languages. GTK is licensed under the GNU Library General Public License v2. | GTK4 is a library for creating graphical user interfaces which is widely available in Linux distribution repositories. It is a portable toolkit meaning that if applications are written for one platform they can be ported to another. Linux, of course, is the preferred platform. Although C will be used in these articles, GTK4 has bindings for other programming languages. GTK is licensed under the GNU Library General Public License v2. | ||
| Ligne 26: | Ligne 33: | ||
| GTK4 uses the GObject library which provides object-oriented programming (OOP) features by using function-like macros. In OOP, constructors are used to initialise a new object from a specific class. This means that when you use the GTK4 API, it provides a list of classes with their constructors, | GTK4 uses the GObject library which provides object-oriented programming (OOP) features by using function-like macros. In OOP, constructors are used to initialise a new object from a specific class. This means that when you use the GTK4 API, it provides a list of classes with their constructors, | ||
| - | Widgets are the fundamental building blocks when creating a GTK4 Graphical User Interface (GUI) application. Widgets are pre-built user interface elements such as labels, text entries and buttons. GtkWidget is the base class for all widgets. Every widget derives from GtkWidget. It provides a common set of properties, methods and signals to ensure consistency and styling of widgets. | + | Widgets are the fundamental building blocks when creating a GTK4 Graphical User Interface (GUI) application. Widgets are pre-built user interface elements such as labels, text entries and buttons. GtkWidget is the base class for all widgets. Every widget derives from GtkWidget. It provides a common set of properties, methods and signals to ensure consistency and styling of widgets.** |
| - | GTK4 API | + | GTK4 |
| + | |||
| + | GTK4 est une bibliothèque de création d' | ||
| + | |||
| + | Gtk4 utilise la bibliothèque GObject, qui offre des fonctionnalités de programmation orientée objet (POO) grâce à des macros de type fonction. En POO, les constructeurs permettent d' | ||
| + | |||
| + | Les widgets sont les éléments fondamentaux de la création d'une application d' | ||
| + | |||
| + | |||
| + | **GTK4 API | ||
| The GTK4 API information is an essential resource when developing a GTK application providing information about constructors, | The GTK4 API information is an essential resource when developing a GTK application providing information about constructors, | ||
| - | For example, the API information on the button class reveals that it has a constructor called gtk_button_new_with_label(), | + | For example, the API information on the button class reveals that it has a constructor called gtk_button_new_with_label(), |
| + | |||
| + | API GTK4 | ||
| + | |||
| + | Les informations de l'API GTK4 sont une ressource essentielle pour le développement d'une application GTK. Elles fournissent des informations sur les constructeurs, | ||
| + | |||
| + | Par exemple, les informations de l'API sur la classe button révèlent qu' | ||
| - | Install GTK4 | + | **Install GTK4 |
| To build GTK applications from source, it is necessary to install the libgtk-4-dev package which contains the header and development files for the GTK4 library. When the package libgtk-4-dev is installed, other libraries such as GLib and Gio are also installed. GLib offers various data structures, string manipulation, | To build GTK applications from source, it is necessary to install the libgtk-4-dev package which contains the header and development files for the GTK4 library. When the package libgtk-4-dev is installed, other libraries such as GLib and Gio are also installed. GLib offers various data structures, string manipulation, | ||
| With Ubuntu 24.04 the following packages need to be installed using the terminal commands below. | With Ubuntu 24.04 the following packages need to be installed using the terminal commands below. | ||
| + | |||
| + | sudo apt update | ||
| + | sudo apt install build-essential | ||
| + | sudo apt install libgtk-4-dev** | ||
| + | |||
| + | Installer GTK4 | ||
| + | |||
| + | Pour créer des applications GTK à partir des sources, il est nécessaire d' | ||
| + | |||
| + | Sous Ubuntu 24.04, ces paquets doivent être installés à l'aide des commandes de terminal ci-dessous. | ||
| sudo apt update | sudo apt update | ||
| Ligne 44: | Ligne 77: | ||
| sudo apt install libgtk-4-dev | sudo apt install libgtk-4-dev | ||
| - | A number of different code editors can be used when developing GTK applications including Geany and GNOME Builder. | + | |
| + | **A number of different code editors can be used when developing GTK applications including Geany and GNOME Builder. | ||
| Geany can be installed using the Ubuntu App Center. Geany has an integrated terminal and a sidebar that has a symbols tab. This is very useful as it shows a list of symbols (functions, classes and variables) found within the current open file. This list can be filtered. | Geany can be installed using the Ubuntu App Center. Geany has an integrated terminal and a sidebar that has a symbols tab. This is very useful as it shows a list of symbols (functions, classes and variables) found within the current open file. This list can be filtered. | ||
| - | With Ubuntu 24.04 the latest version of GNOME Builder is installed using Flatpak. A tutorial on how to install Flatpak on Ubuntu is in the external links below. GNOME Builder can be used for developing both GTK4 and GNOME applications. In this series of articles, GNOME (libadwaita) applications are not being developed. Some feature highlights of GNOME Builder include browser pages so that GTK4 API information can be displayed within the IDE, code completion, integral terminal, build, rebuild, clean, and run command menu items. | + | With Ubuntu 24.04 the latest version of GNOME Builder is installed using Flatpak. A tutorial on how to install Flatpak on Ubuntu is in the external links below. GNOME Builder can be used for developing both GTK4 and GNOME applications. In this series of articles, GNOME (libadwaita) applications are not being developed. Some feature highlights of GNOME Builder include browser pages so that GTK4 API information can be displayed within the IDE, code completion, integral terminal, build, rebuild, clean, and run command menu items.** |
| + | |||
| + | Plusieurs éditeurs de code peuvent être utilisés pour développer des applications GTK, notamment Geany et GNOME Builder. | ||
| + | |||
| + | Geany peut être installé via l'App Center d' | ||
| + | |||
| + | Avec Ubuntu 24.04, la dernière version de GNOME Builder s' | ||
| - | First Application | + | **First Application |
| Open the main.c file in the download. It shows how to use GTK4 to create a window containing label and button widgets positioned using a box layout container. A GTK4 program must start with the directive #include < | Open the main.c file in the download. It shows how to use GTK4 to create a window containing label and button widgets positioned using a box layout container. A GTK4 program must start with the directive #include < | ||
| Ligne 56: | Ligne 97: | ||
| The main function is the entry point for the program and is used to create a GtkApplication object and run it. The pointer called “app” is declared and initialised using the gtk_application_new() constructor. The function macro g_signal_connect() connects the “activate” signal to the activate() function. The activate signal is emitted when the application is started with g_application_run() which takes the command-line arguments. The g_object_unref() method is called on the GtkApplication app pointer when the application is closed. | The main function is the entry point for the program and is used to create a GtkApplication object and run it. The pointer called “app” is declared and initialised using the gtk_application_new() constructor. The function macro g_signal_connect() connects the “activate” signal to the activate() function. The activate signal is emitted when the application is started with g_application_run() which takes the command-line arguments. The g_object_unref() method is called on the GtkApplication app pointer when the application is closed. | ||
| - | Notice that “org.gkt.demo” is the application ID. As an aside, GTK and GNOME make use of “reverse DNS” style identifiers for applications and a desktop file should be named using the application ID. That is < | + | Notice that “org.gkt.demo” is the application ID. As an aside, GTK and GNOME make use of “reverse DNS” style identifiers for applications and a desktop file should be named using the application ID. That is < |
| - | #include < | + | Première application |
| + | |||
| + | Dans le téléchargement, | ||
| + | |||
| + | La fonction main est le point d' | ||
| + | |||
| + | Notez que « org.gkt.demo » correspond à l'ID de l' | ||
| + | |||
| + | |||
| + | **#include < | ||
| | | ||
| Ligne 81: | Ligne 131: | ||
| | | ||
| | | ||
| + | | ||
| + | | ||
| + | | ||
| + | } | ||
| + | |||
| + | int main (int argc, char **argv) | ||
| + | { | ||
| + | | ||
| + | int status; | ||
| + | app = gtk_application_new (" | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | }** | ||
| + | |||
| + | #include < | ||
| + | |||
| + | | ||
| + | { | ||
| + | | ||
| + | | ||
| + | } | ||
| + | |||
| + | | ||
| + | { | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | label =gtk_label_new(" | ||
| + | box =gtk_box_new(GTK_ORIENTATION_VERTICAL, | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| | | ||
| | | ||
| Ligne 97: | Ligne 186: | ||
| } | } | ||
| - | In the activate() function, a GTK window is created using the gtk_application_window_new() constructor. The window title “Hello Window” is set using gtk_window_set_title(). In GTK, GTK_WINDOW() is a macro that performs a type cast to convert a GtkWidget pointer to a GtkWindow pointer. The window size is set using gtk_window_set_default_size() and a window is displayed using gtk_window_present(). | ||
| - | Button, label and box GtkWidget pointers are declared. The label widget | + | **In the activate() function, a GTK window |
| - | The button property “has-frame” | + | Button, label and box GtkWidget pointers are declared. |
| - | There are macros similar to GTK_LABEL for almost every widget such GTK_ENTRY(object) which casts the object to GtkEntry*. A GtkEntry allows a user to input and edit a single line of text. These macros are a cornerstone of how GTK implements object oriented programming and type safety in the C language. | + | Dans la fonction activate(), une fenêtre GTK est créée à l'aide du constructeur gtk_application_window_new(). Le titre de la fenêtre, « Hello Window », est défini à l'aide de gtk_window_set_title(). En GTK, GTK_WINDOW() est une macro qui convertit un pointeur GtkWidget en pointeur GtkWindow par conversion de type. La taille de la fenêtre est définie à l'aide de gtk_window_set_default_size() et une fenêtre est affichée à l'aide de gtk_window_present(). |
| - | Makefile | + | Les pointeurs GtkWidget de bouton, d' |
| + | |||
| + | |||
| + | **The button property “has-frame” is set to FALSE using the setter function gtk_button_set_has_frame(). Then g_signal_connect() is used to connect a callback function called button_clicked() to the “clicked” signal of the button. The function arguments of the button clicked callback function are the GtkButton and a gpointer. A gpointer (generic pointer) is an untyped pointer meaning that it is not associated with any data type and so requires casting. It has to be explicitly cast to the correct specific pointer type or else the compiler does not know how to interpret it. In this case the gpointer is the label widget. In the callback function the macro GTK_LABEL() casts the GtkWidget label pointer obtained from the gpointer user_data to a GtkLabel. The gtk_label_set_text() method then sets the label text to “Button Clicked”. When the button is clicked the text in the label changes to “Button Clicked”. | ||
| + | |||
| + | There are macros similar to GTK_LABEL for almost every widget such GTK_ENTRY(object) which casts the object to GtkEntry*. A GtkEntry allows a user to input and edit a single line of text. These macros are a cornerstone of how GTK implements object oriented programming and type safety in the C language.** | ||
| + | |||
| + | La propriété du bouton « has-frame » est définie sur FALSE à l'aide de la fonction de définition gtk_button_set_has_frame(). Ensuite, g_signal_connect() est utilisée pour connecter une fonction de rappel appelée button_clicked() au signal « clicked » du bouton. Les arguments de la fonction de rappel de clic du bouton sont le GtkButton et un gpointer. Un gpointer (pointeur générique) est un pointeur non typé, ce qui signifie qu'il n'est associé à aucun type de données et nécessite donc un transtypage. Il doit être explicitement transtypé vers le type de pointeur spécifique approprié, sinon le compilateur ne sait pas comment l' | ||
| + | |||
| + | Il existe des macros similaires à GTK_LABEL pour presque tous les widgets, comme GTK_ENTRY(object) qui convertit l' | ||
| + | |||
| + | |||
| + | **Makefile | ||
| GNU Make is a build automation tool that manages the build process of a project. The overall objective is to create a Makefile to build the project executable with the GKT4 libraries and headers. Make and other build systems are used in software development because they recompile files only if changes have been made which is important for large projects having many source code files. An explanation of the Makefile for this GTK4 project is contained in the code download and so only how to use it is covered here. | GNU Make is a build automation tool that manages the build process of a project. The overall objective is to create a Makefile to build the project executable with the GKT4 libraries and headers. Make and other build systems are used in software development because they recompile files only if changes have been made which is important for large projects having many source code files. An explanation of the Makefile for this GTK4 project is contained in the code download and so only how to use it is covered here. | ||
| Ligne 117: | Ligne 217: | ||
| ./demo | ./demo | ||
| - | The Makefile contains a target called ‘clean’ which will delete the executable and any object files so that a fresh build can be performed using the source code files which remain untouched. Running “make clean” at the command line removes the object file and the executable. | + | The Makefile contains a target called ‘clean’ which will delete the executable and any object files so that a fresh build can be performed using the source code files which remain untouched. Running “make clean” at the command line removes the object file and the executable.** |
| - | External links | + | Makefile |
| + | |||
| + | GNU Make est un outil d' | ||
| + | |||
| + | Pour utiliser Makefile afin de compiler le projet de démonstration, | ||
| + | |||
| + | make | ||
| + | |||
| + | Pour exécuter l' | ||
| + | |||
| + | ./demo | ||
| + | |||
| + | Le Makefile contient une cible appelée « clean » qui supprime l' | ||
| + | |||
| + | |||
| + | **External links | ||
| GTK Toolkit | GTK Toolkit | ||
| Ligne 140: | Ligne 255: | ||
| Flatpak on Ubuntu: The Right Way to Set It Up and Use It | Flatpak on Ubuntu: The Right Way to Set It Up and Use It | ||
| + | https:// | ||
| + | |||
| + | Liens externes | ||
| + | |||
| + | Boîte à outils GTK | ||
| + | https:// | ||
| + | |||
| + | API GTK4 | ||
| + | https:// | ||
| + | |||
| + | Programmation en C avec Code Vault | ||
| + | https:// | ||
| + | |||
| + | Introduction aux Makefiles | ||
| + | https:// | ||
| + | |||
| + | Geany est un éditeur de code source léger. | ||
| + | https:// | ||
| + | |||
| + | GNOME Builder est un IDE pour le développement de logiciels basés sur GTK et GNOME. | ||
| + | https:// | ||
| + | |||
| + | Flatpak sur Ubuntu : comment l' | ||
| https:// | https:// | ||
| + | Bio : | ||
| + | |||
| + | **Alan is retired and a Linux enthusiast. He has worked in education and with industry and has used many programming languages including C, C++, Delphi and Java. His Linux projects can be found on his Github page https:// | ||
| - | Alan is retired and a Linux enthusiast. He has worked in education and with industry and has used many programming languages including | + | Alan est retraité et passionné de Linux. |
issue220/tutoriel1.1756048145.txt.gz · Dernière modification : 2025/08/24 17:09 de philou511
