issue220:tutoriel1
Différences
Ci-dessous, les différences entre deux révisions de la page.
| Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
| issue220:tutoriel1 [2025/08/31 07:39] – d52fr | issue220:tutoriel1 [2025/08/31 08:15] (Version actuelle) – d52fr | ||
|---|---|---|---|
| Ligne 3: | Ligne 3: | ||
| Written by Alan Crispin** | Written by Alan Crispin** | ||
| - | Programmation en C dans GTK4 - partie 1 | + | //Bandeau de titre : //Programmation |
| Ligne 15: | Ligne 15: | ||
| https:// | https:// | ||
| + | |||
| + | Introduction | ||
| + | |||
| + | Cet article est le premier d'une série sur le développement d' | ||
| + | |||
| + | Tout au long de cette série, nous utiliserons Ubuntu 24.04, qui utilise la version GTK4.14 de GTK4. Cette série suppose que le lecteur maîtrise le langage de programmation C. De nombreux tutoriels et vidéos en ligne sur la programmation C sont disponibles. | ||
| + | |||
| + | Le code source complet de ce projet peut être téléchargé via le lien ci-dessous. | ||
| + | |||
| + | https:// | ||
| + | |||
| **GTK4 | **GTK4 | ||
| Ligne 23: | Ligne 34: | ||
| 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 | ||
| + | |||
| + | 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 | **GTK4 API | ||
| Ligne 29: | Ligne 49: | ||
| 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 | ||
| Ligne 35: | Ligne 62: | ||
| 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 40: | 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 | ||
| Ligne 53: | Ligne 98: | ||
| 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 < | ||
| + | |||
| + | 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 < | **#include < | ||
| Ligne 92: | Ligne 146: | ||
| | | ||
| }** | }** | ||
| + | |||
| + | #include < | ||
| + | |||
| + | | ||
| + | { | ||
| + | | ||
| + | | ||
| + | } | ||
| + | |||
| + | | ||
| + | { | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | label =gtk_label_new(" | ||
| + | box =gtk_box_new(GTK_ORIENTATION_VERTICAL, | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | } | ||
| + | |||
| + | int main (int argc, char **argv) | ||
| + | { | ||
| + | | ||
| + | int status; | ||
| + | app = gtk_application_new (" | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | } | ||
| + | |||
| **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(). | **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 is created using the gtk_label_new() constructor and its text set to “GTK4 Programming in C”. The button widget is created using the gtk_button_new_with_label() constructor with the button label text set to “Click Me”. The box layout container is created using the gtk_box_new() constructor and its orientation is set to vertical and is used to arrange child widgets (e.g. the label and button in this example). The gtk_box_append() method is used to add the label and then the button to the box container. The gtk_window_set_child() function is used to add the box to the window.** | Button, label and box GtkWidget pointers are declared. The label widget is created using the gtk_label_new() constructor and its text set to “GTK4 Programming in C”. The button widget is created using the gtk_button_new_with_label() constructor with the button label text set to “Click Me”. The box layout container is created using the gtk_box_new() constructor and its orientation is set to vertical and is used to arrange child widgets (e.g. the label and button in this example). The gtk_box_append() method is used to add the label and then the button to the box container. The gtk_window_set_child() function is used to add the box to the window.** | ||
| + | |||
| + | 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(). | ||
| + | |||
| + | 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”. | **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.** | 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 | **Makefile | ||
| Ligne 114: | Ligne 218: | ||
| 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.** | ||
| + | |||
| + | 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 | **External links | ||
| Ligne 137: | Ligne 256: | ||
| 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:// | 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:// | ||
issue220/tutoriel1.1756618748.txt.gz · Dernière modification : 2025/08/31 07:39 de d52fr
