Making the Codeblocks IDE Look Good on Linux

How to Override GTK3 and Apply GTK2 Themes to Codeblocks

Image for post
Image for post

Introduction

Codeblocks is a fantastic IDE and one of the best for C/C++ development on Linux. However, as of version 20.03, codeblocks is still using the GTK2 library to draw its graphical user interface on Linux. As a result, codeblocks may appear to not draw correctly upon installation as most Linux systems now use GTK3 — the newer version of GTK that works slightly differently to its older versions.

To add to the confusion, if you inspect the dependencies of your codeblocks installation, it will appear that codeblocks is indeed calling the GTK3 library, when it is in fact still using GTK2 as its back-end.

Furthermore, If your Linux system is drawing applications via GTK3 with a custom theme that you have applied as well, chances are that codeblocks will render incorrectly and may even appear to be unusable like in the following screenshot:

Image for post
Image for post
A GTK3 theme applied to codeblocks being drawn by GTK2.

The Solution

This article will discuss how to install a codeblocks version that relies solely on the GTK2 library such that if you were to inspect its dependencies, all GTK packages will be labelled as version 2. Luckily, the codeblocks SVN repository meets this requirement.

With this in mind, we shall use the yay package manager to download and build the codeblocks SVN repository on Arch Linux, which will produce a version of codeblocks that draws its interface with GTK2.

The article will then walk through the process of downloading a custom GTK2 theme and explicitly telling codeblocks to apply this theme when launching the IDE. This way, your system will still be using GTK3 as its GUI back-end, and only codeblocks will know about and use the saved GTK2 theme.

I will also provide a couple of methods for efficiently launching codeblocks with the applied GTK2 theme. The first method will involve defining an alias inside .bashrc called cb that can be invoked inside a terminal. The second method will detail how to create an openbox keybinding. Since I’m on a Lenovo laptop with a Windows key, our keybinding will be defined as W-c.

Looking at a Codeblocks Installation

This section will detail how to inspect your current codeblocks installation and view its dependencies on Arch Linux. If you don’t have codeblocks installed, you can skip this section as we shall be installing it via the codeblocks-svn repository in the next section. However, feel free to continue reading if you are curious about your current codeblocks installation and how the GUI is drawn on Linux.

About wxWidgets

Codeblocks uses the cross-platform wxWidgets library to draw its GUI on your operating system. The way wxWigets works is that it calls an operating system’s native GUI library to draw an application’s user interface. The underlying GUI library on Linux is GTK, while Windows calls the Win32 API, and Mac OS falls back to Cocoa. Overall, wxWidgets enables a developer to write and compile the same GUI code on several platforms with minimal or no code changes.

Inspecting Dependencies with Pactree

Codeblocks installations on Linux rely on a package called wxGTK, which is a port of wxWidgets that calls the underlying GTK library. The official wxWidgets documentation describes wxGTK as:

…wxGTK is a port of wxWidgets using the GTK+ library. It makes use of GTK+’s native widgets wherever possible and uses wxWidgets’ generic controls when needed…

With this in mind, we can inspect a codeblocks installation on Arch Linux with a utility called pactree to view its dependencies. To get pactree, install the pacman-contrib package:

$ pacman -S pacman-contrib

To view your codeblocks dependencies with pactree, execute the following:

$ pactree -d 1 codeblocks
  • The value 1 is passed to the -d option to display dependencies one level down the package’s dependency tree. Modify this value or discard it altogether to see a package’s full dependency tree.

The output shows that codeblocks depends on the wxgtk3 package — even though codeblocks is drawn via GTK2 when it is launched! As of this writing in November 2020, it seems that the wxgtk3 wrapper still calls the old GTK2 library. Hopefully, this will be fixed in the future such that the GTK3 library is called instead of GTK2.

The codeblocks-svn repository that we shall build in the next section depends on the wxgtk2 package instead of wxgtk3, guaranteeing that the older GTK2 library will be called to draw the codeblocks GUI. This will ensure that no conflicts are occurring with the newer GTK3 library.

Installing Codeblocks via the SVN Repository

If you have codeblocks installed on your Linux system, be sure to remove it at this point as we shall be building it from source via the codeblocks SVN repository. Assuming you installed codeblocks via pacman on Arch Linux, remove it via the following command:

$ sudo pacman -Rcns codeblocks

From here, install the codeblocks-svn package from the AUR. The following command downloads and builds the package via yay:

$ yay -S codeblocks-svn
  • If your Linux distribution’s package manager does not have access to codeblocks-svn, you will need to download the repository and build codeblocks manually. Refer to this page for detailed instructions on how to do this.

After installation is complete, we can inspect the codeblocks dependencies again with pactree to confirm that wxgtk2 is being loaded instead of wxgtk3:

$ pactree -d 1 codeblocks
Image for post
Image for post
Codeblocks now depends on wxgtk2.

The installation now relies solely on GTK2. The next section will detail how to download a GTK2 theme and apply it to codeblocks.

Applying a GTK2 Theme

It is now time to download a GTK2 theme for our new codeblocks installation. Feel free to download a GTK2 theme of your liking instead of the one detailed in this section. You may also have some GTK2 themes on your Linux system in the /usr/shared/themes directory that can also be used.

The theme used for this demonstration is called BSM Simple Dark Menu which can be downloaded from this page. After extracting the download, let’s go ahead and prepare the relevant theme files for codeblocks. The following commands will create a ~/.themes directory and copy the theme files into it:

  # Enter downloaded directory
$ cd 121685-BSM\ Simple\ 13
# Rename theme directory to remove spaces
$ mv BSM\ Simple\ Dark\ Menu/ BSM-Simple-Dark-Menu
# Create a .themes directory in home folder
$ mkdir ~/.themes
# Copy the theme to the new .themes folder
$ cp -R BSM-Simple-Dark-Menu ~/.themes
# (Optional) Delete downloaded files
$ cd ..
$ rm -fr BSM\ Simple\ Dark\ Menu/

With the theme stored in a nice location on the file system, let’s now apply this theme to codeblocks when it is launched.

What we want to do is only apply our GTK2 theme to codeblocks and let our system draw everything else with GTK3 as normal. This can be achieved by setting an environment variable called GTK2_RC_FILES to the location of the theme’s gtkrc file when launching codeblocks via the terminal. Let’s test it out with the following command:

$ GTK2_RC_FILES=~/.themes/BSM-Simple-Dark-Menu/gtk-2.0/gtkrc 
codeblocks

Our GTK2 theme is now applied to codeblocks when it is launched:

Image for post
Image for post
The GTK2 theme is now applied to codeblocks.

Modifying the Font

Our theme is looking good, but it would be nice if we could set the font and make it a point or two bigger. Let’s modify the theme’s gtkrc file by firstly opening it up in a text editor such as vim:

vim ~/.themes/BSM-Simple-Dark-Menu/gtk-2.0/gtkrc

At the bottom of the gtkrc file, add the following code and replace the font name and size values with your preferred settings. I have went with the Open Sans font and a point size of 11:

style = “font”
{
font_name = “Open Sans 11”
}
widget_class “*” style “font”
gtk_font_name = “Open Sans 11”

Launch codeblocks again to see your font changes take effect:

$ GTK2_RC_FILES=~/.themes/BSM-Simple-Dark-Menu/gtk-2.0/gtkrc 
codeblocks
Image for post
Image for post
The new font settings seen in codeblocks.

At this point, codeblocks is now usable providing it is launched via the above command. Admittedly, this command is a long one to type. The next section will go through a couple of ways to launch codeblocks more efficiently.

Launching Codeblocks Efficiently

This section will walk through a couple of methods on how to efficiently launch our themed version of codeblocks.

Creating an Alias

Let’s create an alias in the .bashrc file called cb that will launch our themed version of codeblocks. Firstly open .bashrc with your text editor:

$ vim ~/.bashrc

Then define an alias called cb at the bottom of the file:

alias cb=”GTK2_RC_FILES=~/.themes/BSM-Simple-Dark-Menu/gtk-2.0/gtkrc 
codeblocks”

Save .bashrc and reload your bash environment with the source command:

$ source ~/.bashrc

Codeblocks can now be launched by simply typing cb in a terminal, and our theme will be applied accordingly. To run the process in the background, simply append an &:

$ cb&

Creating an Openbox Keybinding

Lastly, this section will walk though creating an openbox keybinding for Linux users using openbox as part of their desktop environment. Let’s create a keybinding that launches codeblocks when we press the Windows + C key combination.

The way we’ll do this is execute a shell script after pressing our keybinding. Let’s firstly create a ~/.scripts directory to store our shell script, along with the script file itself named launch-codeblocks.sh:

$ mkdir ~/.scripts
$ touch ~/.scripts/launch-codeblocks.sh

Then open launch-codeblocks.sh in a text editor and add the following command:

$ vim ~/.scripts/launch-codeblocks.sh# In launch-codeblocks.sh
GTK2_RC_FILES=~/.themes/BSM-Simple-Dark-Menu/gtk-2.0/gtkrc
codeblocks

With the script created, lets now open the openbox rc.xml file inside a text editor to define our keybinding:

$ vim ~/.config/openbox/rc.xml

Then, in your keybindings section add the following XML code:

<keybind key=”W-c”>
<action name=”Execute”>
<startupnotify>
<enabled>true</enabled>
<name>CodeBlocks</name>
</startupnotify>
<command>./.scripts/launch-codeblocks.sh</command>
</action>
</keybind>

Of course, feel free to replace W-c with your preferred key combination. Now our themed version of codeblocks will launch whenever we press Windows-C on our system.

In Conclusion

This article has covered installing codeblocks that utilizes the GTK2 library to draw its GUI, in addition to applying a custom GTK2 theme. We also discussed the way codeblocks draws its GUI via wxWidgets and how to inspect a particular codeblocks installation’s dependencies via pactree on Arch Linux.

I hope the methods presented in this article provide a solution for Linux users struggling to make the current version of codeblocks presentable, or even usable on their system. I’m sure that the wxGTK wrapper will be updated in the near future such that it uses the GTK3 library as its back-end. However, in the mean time we can adopt the methods presented in this article for theming the codeblocks IDE effectively.

Written by

MSc. Programmer and fan of open source software.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store