My GTemplate

Introduction

My GTemplate is simple but feature rich GUI Application template written in C using GTK3/GTK4. Several features are included along with a script to bootstrap your own project.

If you wish to learn GTK, see the GNOME Platform Demos and Getting started with GTK3.

If you find this useful, you may:

  • donate me via librepay.
  • Help free software by switching to any GNU/Linux.
  • Regardless of the Operating System you use, always buy Debian main compatible hardware or at least ask for one.

my-gtemplate.png

Figure 1: Obligatory screenshot

my-gtemplate-w32.png

Figure 2: The same on Windows 8

Source: GitLab GitHub

Features

  • Written in C programming language
  • [G]Object Oriented Design
  • Supports GTK3 and GTK4
  • Uses Meson build system
  • Flatpak support included
  • GitLab CI configuration included
  • Microsoft Windows support with MinGW and msitools
  • GtkBuilder .ui templates for UI
  • UI based on GNOME HIG
  • Application specific CSS
  • Keyboard shortcuts window
  • Handles commandline arguments
  • Tab completion for commandline
  • Internationalisation using gettext
  • Documentations for code using gtk-doc
  • Mallard based help for GNOME Help
  • Manpage in docbook XML format
  • GSettings integration
  • Symbolic and non-symbolic icons
  • Script to generate non-symbolic icons from symbolic ones
  • Uncrustify configuration and script
  • Meson based unit tests
  • And many more…

Follow the instructions below to compile the project from source:

Install pre-requisites

To build the project you require a recent version of some GNU/Linux. Feel free to use the OS of your choice. Though it’s recommended to use the latest version Ubuntu, Fedora or Debian testing.

To bootstrap a new project, a few packages are required:

# On Debian, Ubuntu and derivatives
sudo apt install -y git meson flatpak flatpak-builder \
uncrustify build-essential libgtk-3-dev appstream-util \
bash-completion xsltproc docbook-xsl gedit gettext \
ccache libgtk-4-dev libadwaita-1-dev libhandy-1-dev \
gtk-doc-tools gnome-builder

# On Fedora and derivatives
sudo dnf install -y @c-development @development-tools \
gettext-devel gtk3-devel uncrustify gettext-devel meson \
flatpak flatpak-builder desktop-file-utils git gedit \
bash-completion docbook-style-xsl gtk4-devel libxslt \
ccache itstool libhandy-devel gtk-doc gnome-builder

Many developers use GNU Emacs or vim as Editor. If you don’t have such an editor of choice, it’s recommended to use GNOME Builder. You may also find Flatpak helpful when developing with GNOME Builder. To configure Flatpak, see Flatpak setup guide.

If you are planning to build for Microsoft Windows, Fedora may be the only OS choice as it includes many MinGW packages required for building GTK3 applications.

Creating a new project from template

To create a new project from the template, clone the repository, edit new-project.sh script and replace the default values with your project specific values.

# Clone the repository
git clone https://gitlab.com/sadiq/my-gtemplate.git
cd my-gtemplate

# Edit the file 'new-project.sh' in my-gtemplate dir
gedit new-project.sh

Let’s say we are creating a GTK3 project for managing notes. Let’s choose some (in)sane names for our project and change new-project.sh accordingly:

# First checkout gtk3 branch (the master branch targets gtk4)
git checkout gtk3
# Make necessary changes to the variables.
# Only some of them are listed here.
APP_NAME="gee-note"
APP_LAST_NAME="note"
APP_REAL_NAME="Gee Note"
# Assuming your repo is https://gitlab.com/username/gee-note
APP_ID="com.gitlab.username.gee-note"
APP_SHRT="gnt"
APP_SHRT_CAP="Gnt"

Now let’s try to build a template for our project:

# Make necessary changes to new-project.sh and run
./new-project.sh

# APP_NAME will be used as directory name, so for us
# it will be gee-note.  Move it
mv gee-note ~
cd ~/gee-note

# Now compile using meson
# You may replace 'build' with any non-existing directory name
meson build && cd build
ninja
# And install
sudo ninja install

# Let’s now run it
gee-note
# Or if you haven’t installed, run with
./run # from the 'build' directory
# Or Run with verbose logs
# Add more "v" to increase verbosity
./run -vvvv # or gee-note -vvvv

Building for Microsoft Windows

A build script to compile and build .msi installer is included in the repository. The build script make use of MinGW packages to compile the source, and msitools to create an .msi installer out of it. You don’t require to have access to any Windows machine to compile or build the installer. All the process can be done on a Fedora GNU/Linux.

The script has been tested only with Fedora 28. And only GTK3 is supported.

First, install the required packages:

# Instal mingw64-gtk3 for 64 bit build
sudo dnf install -y mingw32-gtk3 meson gcc gtk3-devel msitools

To build, Update the file build-aux/w32/defaults.sh.in and adapt the variables as per your needs and do:

# Compile
~/gee-note$ ./build-aux/w32/make-w32.sh
# Alternately, give arguments to specify details
# First argument: Whether the build has to be optimized
# Posible values (arg1): yes no
# Second argument: Architecture the program should run
# Possible values (arg2): i686 x86_64
~/gee-note$ ./build-aux/w32/make-w32.sh yes i686

# And then follow the instructions printed by the
# script to create an msi installer.

The generated .msi installer has a size of about 26 MiB. Around 18 MiB of that is Adwaita icon theme. You may be able to optimize the size if you include a custom version of Adwaita with only the required icons. You can also strip the binaries which can further save around 1-2 MiB.

You may use wine to test the installer and the executable:

# Install the .msi file.  Use the
# right .msi path shown in previous step
wine msiexec /i /path/to/installer.msi /l*v log.txt

# Run the executable.  Adjust the path if needed
cd "$HOME/.wine/drive_c/Program Files/My GTemplate"
XDG_DATA_DIRS="./share" wine bin/my-gtemplate.exe

And on Windows, as usual, you can simply double click the .msi file to install.

Required changes for new projects

The project created by running new-project.sh shall still have stale data. So, those files should be updated to match your project. At the minimum, the following list of files are thus supposed to be changed (The items in paren suggests the alternate name you might have set in new-project.sh.):

  • All README.md files
  • my-gtemplate.doap (APP_NAME)
  • docs/help/C/ (All files)
  • docs/man/my-gtemplate.xml (APP_NAME)
    • dummy examples and descriptions
  • desktop.in file in data (APP_ID)
    • At least the Comment and Keywords values
  • Image in data/icons/hicolors/scalable/apps/
    • And then run data/icons/hicolor/render-icons.sh script
  • data/appdata/ (All files, except meson.build)
  • src/resources/ui/mgt-window.ui (APP_SHRT)
    • subtitle in GtkHeaderBar

Of course, that’s just the minimum change. When you develop, you’ll have to change everything in src/ directory, and probably other directories too.

License

Written in 2017-2021 by Mohammed Sadiq <sadiq@sadiqpk.org>

To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this software to the public domain worldwide. This software is distributed without any warranty.

You should have received a copy of the CC0 Public Domain Dedication along with this software. If not, see the license online.