GTK 4 Tutorial for beginners

Now that GTK 4 is widely used, “GTK” usually means GTK 4. Therefore, this tutorial simply says “GTK” except where the version needs to be explicitly mentioned.

This tutorial illustrates how to write C programs with the GTK library. It is aimed at beginners, so the contents are limited to the basics. The table of contents is at the end of this README.md.

  • Section 3 to 26 describe the basics, with the example of a simple editor tfe (Text File Editor).
  • Section 27 to 30 describe GtkDrawingArea.
  • Section 31 describes Drag and Drop.
  • Section 32 to 36 describe the list model and the list view (GtkListView, GtkGridView, and GtkColumnView). It also describes GtkExpression.

The latest version of the tutorial is located at the GTK4-tutorial GitHub repository. You can read it directly without downloading.

There’s a GitHub Page which is the HTML version of the tutorial.

GTK Documentation

Please refer to the GTK API Reference and the GNOME Developer Documentation Website for further information. These websites were opened in August 2021.

If you want to know about GObject and the type system, please refer to the GObject tutorial. GObject is the base of GTK, so it is important for developers to understand it as well as GTK.

Contribution

This tutorial is still under development and unstable. Even though the example codes have been tested on GTK (version 4.14.5), bugs may still exist. If you find any bugs, errors, or mistakes in the tutorial and C examples, please let me know. You can post them to GitHub issues. You can also post updated files to pull request. When submitting a pull request, please ensure that you only modify the files in the src directory. Don’t modify the files under gfm or html directories directly. After modifying some source files, run bundle exec rake to create GFM (GitHub Flavoured Markdown) files and run bundle exec rake html to create HTML files. Please refer to the Author Guide for further information.

If you have a question, feel free to post it to issue of this repository. Your questions and feedback are welcome and invaluable for improving this tutorial.

How to Get Gtk 4 Tutorial in HTML or PDF Format

If you want to get the tutorial in HTML or PDF format, build them with rake command, which is a ruby version of make. Type bundle exec rake html for HTML. Type bundle exec rake pdf for PDF. The Author Guide describes how to write source files.

License

The license of this repository is written in Section1.

In short:

  • GFDL1.3 for documents
  • GPL3 for programs

Table of contents

  1. Prerequisite and License
  2. Preparation
  3. GtkApplication and GtkApplicationWindow
  4. Widgets (1)
  5. Widgets (2)
  6. Strings and Memory Management
  7. Widgets (3)
  8. Defining a Final Class
  9. GtkBuilder and UI Files
  10. Build System
  11. Instance Initialization and Destruction
  12. Error Handling in GLib
  13. Completing the custom object implementation
  14. File Dialogs and the GIO Asynchronous Model
  15. Completing the Main Program
  16. Menus and Actions
  17. Stateful Actions
  18. Ui File for Menu and Action Entries
  19. GtkPopoverMenuBar and GtkMenuButton
  20. Managing the Application
  21. Creating TfeWindow
  22. Handling Unsaved Text
  23. Accelerators
  24. CSS and Pango
  25. GtkFontDialogButton and CSS Updates
  26. Gsettings
  27. GtkDrawingArea and Cairo
  28. Periodic Events
  29. Custom Drawing
  30. Tiny Turtle Graphics Interpreter
  31. Drag and Drop
  32. GtkListView
  33. GtkGridView and Activate Signal
  34. GtkExpression
  35. GtkColumnView
  36. GtkSignalListItemFactory