Sign in Join
Wiki copytemporary

GTK2 Interface

(GTK+ only)

Introduction

The gtk2 widgetset is stable and all components work. And it needs optimizations.

The documentation can be found here.

The minimum Gtk 2 version supported is Gtk 2.8

Road map for the gtk2 interface

Here: Roadmap#Widgetset_dependent_components

Quick start guide for Linux, FreeBSD, etc

The first thing to do is to install the gtk2 libraries including the development packages. For example: linux/debian systems call them libgtk2.0-dev. There are complete installers for Windows here.

The gtk2 is the default widgetset, so it should work out of the box.

You can compile the LCL for gtk2. First open your normal compiled Lazarus. Then go on the menu "Tools" --> "Configure Build Lazarus". Set LCL to "clean+build" and everything else to "None". Now select "gtk2" and click on the "Ok" button. Next go to the menu "Tools" --> "Build Lazarus". Now the LCL is compiled for gtk2 too.

To compile a project for gtk2 just select it as the target widgetset on the Compiler Options dialog.

Ubuntu Unity liboverlay

The Ubuntu's Unity LIBOVERLAY override breaks the gtk_grab_add function, so mouse capturing does not work in synedit, treeview and many more LCL controls that descend from TCustomControl. That means dragging or selecting text with the mouse does not work.

This is true for many applications, which is why liboverlay has an internal list to disable itself for many famous applications (e.g. gimp).

That's why the gtk2 interface of the LCL disables liboverlay by default. You can enable it by compiling the LCL with -dEnableLibOverlay.

If it is enabled: The LCL will write a warning with DebugLn if it spots liboverlay.

To disable liboverlay for a single application set the environment variable LIBOVERLAY_SCROLLBAR to '0' before starting the application. There is no way to disable liboverlay when the application is already running. For example:

LIBOVERLAY_SCROLLBAR=0 ./lazarus

You can disable it on a whole machine by uninstalling the package "liboverlay-scrollbar-0.2-0".

The IDE starter "startlazarus" sets this variable before starting the IDE (since 1.0). startlazarus is called for example when you use the desktop menu item.

Ubuntu Unity Menuproxy

In more recent Ubuntu versions, Ubuntu tries to create a "one menubar" look like macOS has, but this fails miserably with multi window programs like Lazarus. The main problem is not being able to access the menu without first selecting the main lazarus window. (the window that would hold the menubar).

Turning off seems to be impossible without massive changes (probably that is because then everybody would turn such features off immediately).

Turning off manually is possible through setting environment variable UBUNTU_MENUPROXY equal to 0 before starting.

UBUNTU_MENUPROXY=0 ./lazarus

or put it in a script like:

#!/bin/sh
export UBUNTU_MENUPROXY=0
./startlazarus

Raspberry Pi OS

Note: the following advice is proably not necessary if you have installed libgtk2.0-dev as mentioned further up this page

Lazarus GTK2 cannot be compiled by default, because of missing symlinks for libraries. These symlinks are missed:

The existing .so files are located in the "/usr/lib/aarch64-linux-gnu". For all mentioned files, you need to make symlinks like this:

cd /usr/lib/aarch64-linux-gnu
sudo ln -s libatk-1.0.so.0 libatk-1.0.so

Using the GTK2 interface under macOS

Installing X11

If you want to use the gtk widgetset instead of the default Carbon widgetset, then make sure X11 is installed. X11 is no longer included with Mac, but X11 server and client libraries are available from the XQuartz project. The last XQuartz release was version 2.7.11 on 2016-10-29 for Snow Leopard 10.6.3 or later.

How do you know whether X11 is installed? Look in the /Applications/Utilities folder for an application named XQuartz.app. All further details are in /opt/X11/ (soft linked from /usr/X11).

Tip - Drag and drop X11 onto the dock so you'll have a one-click way of opening an X11 window.

Installing GTK2-Lazarus with fink

Install Fink, the package manager for macOS. Install the GTK2 version of Lazarus with the following command:

 fink install lazarus-gtk2

You will be asked to install a number of other packages. Simply answer yes. The installation takes quite a while. After it has finished, the GTK2 application can be started with a double click on /Applications/Fink/lazarus.

Note: Projects using GTK2 can equally be created using the Aqua version of Lazarus, which is installed with "fink install lazarus-aqua".

Installing gtk using MacPorts

Follow these steps to install the libraries using MacPorts. Install MacPorts from [**http://www.macports.org**](http://www.macports.org/)) When MacPorts is set up, you need to install two library sets using the following commands:

sudo /opt/local/bin/port install gtk1
sudo /opt/local/bin/port install gdk-pixbuf

Then make sure that Lazarus can find the library by using the command below before running Lazarus. Alternatively you can add this command to your bash startup script (/etc/profile or ~/.bash_profile) before you start the shell to run Lazarus:

export DYLD_FALLBACK_LIBRARY_PATH="/opt/local/lib:$DYLD_FALLBACK_LIBRARY_PATH"

If this doesn't work, simply do

sudo ln -s /opt/local /sw

Using the GTK2 libraries included with Gimp

Now that the Cocoa widgetset is the default for Lazarus on macOS, there probably isn't much need for developing or distributing GTK2-based apps on macOS. However, you might want to test your apps with the GTK2 widgetset to see how well the graphical portion will work under Linux, where GTK2 is likely to be the default. You can also compile Lazarus itself against the GTK2 widgetset if you're curious about how Lazarus looks with GTK2.

An easy way to obtain recent GTK2 libraries is to download and install Gimp from http://www.gimp.org/macintosh/. Gimp is not only a useful image editor in its own right, but it also installs GTK2 version 2.16.6 libraries that you can link your apps against.

Copy and paste the files given below into a text editor and save them as gimplib.cfg and glaunch.sh. File gimplib.cfg tells FPC how to link against the GTK2 libraries; file glaunch.sh initializes GTK2 and starts a GTK2-compiled executable specified on the command line.

To compile an app against the GTK2 widgetset and link it with the Gimp GTK2 libraries, do the following:

To run a GTK2-compiled executable, open an X11 window, change to your app's folder, and enter the following (which assumes glaunch.sh is in ~/tools/scripts):

To compile Lazarus itself, do the following:

This will rebuild lazarus, lazbuild and startlazarus. You'll probably want to rename your lazarus executables:

To run the GTK2-compiled Lazarus:

Here is the gimplib.cfg config file. Note that this assumes you installed Gimp in your Applications folder.

pascal
-k'-headerpad_max_install_names'
-k'-L/usr/lib'
-k'-L/usr/X11R6/lib'
-k'-L/Applications/Gimp.app/Contents/Resources/lib'
-k'-lgthread-2.0'
pascal
-k'-dylib_file'
-k'/tmp/skl/Gimp.app/Contents/Resources/lib/libpangoft2-1.0.0.dylib:/Applications/Gimp.app/Contents/Resources/lib/libpangoft2-1.0.0.dylib'
-k'-dylib_file'
-k'/tmp/skl/Gimp.app/Contents/Resources/lib/libz.1.dylib:/Applications/Gimp.app/Contents/Resources/lib/libz.1.dylib'
-k'-dylib_file'
-k'/tmp/skl/Gimp.app/Contents/Resources/lib/libpangocairo-1.0.0.dylib:/Applications/Gimp.app/Contents/Resources/lib/libpangocairo-1.0.0.dylib'
-k'-dylib_file'
-k'/tmp/skl/Gimp.app/Contents/Resources/lib/libgmodule-2.0.0.dylib:/Applications/Gimp.app/Contents/Resources/lib/libgmodule-2.0.0.dylib'
-k'-dylib_file'
-k'/tmp/skl/Gimp.app/Contents/Resources/lib/libintl.8.dylib:/Applications/Gimp.app/Contents/Resources/lib/libintl.8.dylib'
-k'-dylib_file'
-k'/tmp/skl/Gimp.app/Contents/Resources/lib/libiconv.2.dylib:/Applications/Gimp.app/Contents/Resources/lib/libiconv.2.dylib'
-k'-dylib_file'
-k'/tmp/skl/Gimp.app/Contents/Resources/lib/libcairo.2.dylib:/Applications/Gimp.app/Contents/Resources/lib/libcairo.2.dylib'
-k'-dylib_file'
-k'/tmp/skl/Gimp.app/Contents/Resources/lib/libfreetype.6.dylib:/Applications/Gimp.app/Contents/Resources/lib/libfreetype.6.dylib'
-k'-dylib_file'
-k'/tmp/skl/Gimp.app/Contents/Resources/lib/libfontconfig.1.dylib:/Applications/Gimp.app/Contents/Resources/lib/libfontconfig.1.dylib'
-k'-dylib_file'
-k'/tmp/skl/Gimp.app/Contents/Resources/lib/libexpat.1.dylib:/Applications/Gimp.app/Contents/Resources/lib/libexpat.1.dylib'
-k'-dylib_file'
-k'/tmp/skl/Gimp.app/Contents/Resources/lib/libXrender.1.dylib:/Applications/Gimp.app/Contents/Resources/lib/libXrender.1.dylib'
-k'-dylib_file'
-k'/tmp/skl/Gimp.app/Contents/Resources/lib/libtiff.3.dylib:/Applications/Gimp.app/Contents/Resources/lib/libtiff.3.dylib'
-k'-dylib_file'
-k'/tmp/skl/Gimp.app/Contents/Resources/lib/libjpeg.62.dylib:/Applications/Gimp.app/Contents/Resources/lib/libjpeg.62.dylib'
-k'-dylib_file'
-k'/tmp/skl/Gimp.app/Contents/Resources/lib/libpng12.0.dylib:/Applications/Gimp.app/Contents/Resources/lib/libpng12.0.dylib'
-k'-dylib_file'
-k'/tmp/skl/Gimp.app/Contents/Resources/lib/libXinerama.1.dylib:/Applications/Gimp.app/Contents/Resources/lib/libXinerama.1.dylib'
-k'-dylib_file'
-k'/tmp/skl/Gimp.app/Contents/Resources/lib/libpixman-1.0.dylib:/Applications/Gimp.app/Contents/Resources/lib/libpixman-1.0.dylib'
-k'-dylib_file'
-k'/tmp/skl/Gimp.app/Contents/Resources/lib/libgio-2.0.0.dylib:/Applications/Gimp.app/Contents/Resources/lib/libgio-2.0.0.dylib'
-k'-dylib_file'
-k'/tmp/skl/Gimp.app/Contents/Resources/lib/libjasper.1.dylib:/Applications/Gimp.app/Contents/Resources/lib/libjasper.1.dylib'

Here is the glaunch.sh script file:

pascal
#!/bin/sh
pascal
TMP="/tmp/$UID/TemporaryItems"
GRES="Gimp.app/Contents/Resources"
GIMP="/Applications/$GRES"
if ! [ -e "$GIMP" ]
then
  GIMP="~/Desktop/$GRES"
  if ! [ -e "$GIMP" ]
  then
    GIMP="~/$GRES"
    if ! [ -e "$GIMP" ]
    then
      GIMP="~/Applications/$GRES"
      if ! [ -e "$GIMP" ]
      then
        GIMP=""
      fi
    fi
  fi
fi
pascal
if [ "$GIMP" = "" ]
then
  echo "Can't locate Gimp"
else
  export "DYLD_FORCE_FLAT_NAMESPACE=1"
  export "DYLD_INSERT_LIBRARIES=/usr/lib/libiconv.dylib"
  export "DYLD_LIBRARY_PATH=/System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/ImageIO.framework/Versions/A/Resources:$GIMP/lib"
  export "PANGO_RC_FILE=$TMP/pangorc"
  export "FONTCONFIG_PATH=$GIMP/etc/fonts"
  export "GTK_IM_MODULE_FILE=$TMP/gtk.immodules"
  export "GDK_PIXBUF_MODULE_FILE=$TMP/gdk-pixbuf.loaders"
  export "GTK_DATA_PREFIX=$GIMP"
  export "GTK_EXE_PREFIX=$GIMP"
pascal
mkdir -p "$TMP"
sed 's|/tmp/skl/Gimp.app/Contents/Resources/etc/pango|'"$TMP|g" "$GIMP/etc/pango/pangorc" > "$TMP/pangorc"
sed 's|/tmp/skl/Gimp.app/Contents/Resources|'"$GIMP|g" "$GIMP/etc/pango/pango.modules" > "$TMP/pango.modules"
cp -f "$GIMP/etc/pango/pangox.aliases" "$TMP"
sed 's|/tmp/skl/Gimp.app/Contents/Resources|'"$GIMP|g" "$GIMP/etc/gtk-2.0/gtk.immodules" > "$TMP/gtk.immodules"
sed 's|/tmp/skl/Gimp.app/Contents/Resources|'"$GIMP|g" "$GIMP/etc/gtk-2.0/gdk-pixbuf.loaders" > "$TMP/gdk-pixbuf.loaders"
pascal
./$1
pascal
fi

Using the Gtk2 interface under Microsoft Windows

Getting the needed Gtk2 libraries

Using the Gtk2 libraries from gtk.org:

or using the GTK-Runtime project:

or using the Gtk2 libraries provided by Gimp:

Standalone Application under Windows

You may prefer a standalone application to avoid versions conflicts. All needed libraries are in the packages mentioned above. They should be placed in the same folder of your executable. You will need all these libraries:

Themes

The default GTK2 interface is quite ugly and not very integrated with the Windows Desktop. Your GTK2 application may use the Windows Theme if you use the gtk-wimp project. Into your application's folder you should add the following files (Available here):

A sample project

A complete and working "Hello world" project (with sources) is available here.

Current issues

Here are some notes about some of the current issues. It is not a complete list, but mainly a collection of the information about some of the most annoying problems, that can not be solved simply.

Editor Options allows to select incompatible fonts

TFontDialog must be extended to show only monospace fonts by default and only if the users insists show all fonts.

Moving with keys in the OI sometimes does not move the edit field

ToDo

Destroying handle during OnKeyDown gives AV

This is a more general problem, not only KeyDown. There are two solutions:

Z-Order of controls

In Gtk2 many controls don't have a window, so they are just painted on their parent. This makes they be painted before controls with a window, so we get a wrong Z-Order.

Here is a list of gtk widgets that don't have windows: https://developer.gnome.org/gtk-tutorial/stable/x481.html

The solution is adding a GtkEventBox under each of these widgets: https://developer.gnome.org/gtk-tutorial/stable/c1226.html#SEC-EVENTBOX

Bug reports

Open Dialog Options

TOpenDialog has several options to control the dialog. When implementing these options in Gtk2, however, one needs to watch out because of the vast amount of bugs in Gtk 2, which may prevent the options from working. For example, ofForceShowHidden doesn't work because of Gtk 2 bugs, althougth it is implemented correctly.

Bug reports

TScrollBox and Scrolling problems

TScrollBox and Scrolling seems deeply broken in Gtk 2.

Bug reports

Shortcut keys

Bug reports

Up/Down key navigation between TEdits

A standard Gtk 2 feature allows the use of up/down key to navigate between GtkEntry widgets (like TEdits), but this is undesirable if the user implements a dropdrow list to be attached to the TEdit. As we cannot know what code the user has written, the best option is to disable this feature. Possible ways to do that are:

As of lazarus 0.9.31 r31562 GtkEntry behaviour is same as under other widgetsets - NO FOCUS CHANGING BY VK_UP or VK_DOWN.

Bug reports

Done issues

Done: Synedit is very slow

Actually this is not a bug of the gtk2 intf, but a problem of gtk2 and some video drivers. On some systems SynEdit is as fast as any other text editor. Some bugs were fixed with gtk 2.12. SynEdit already paints every token with the ExtTextOut function. It does not use a TCanvas text function, which would be even slower.

Bug Tracker item: http://www.freepascal.org/mantis/view.php?id=7717

For people using the Nvidia drivers, setting GlyphCache=1 may speed up SynEdit. You will need the beta-drivers: http://www.nvnews.net/vbulletin/showthread.php?s=399832e3761a522d34db6faaf8423ad9&t=118088

Done: Small TButton does not center text

Actually the text is centered, but some themes (like the default under ubuntu) define a big minimum space above and below the label. The gtk2 default is not that big. So, it is a user setting.

Bug Reports

Done: TOpenPictureDialog does not update the preview

Done: Only the main form appears in the window list

Probably a bug in the window hints parameters.

Done: Slow and visible resizing when creating/resizing a dialog

This has several reasons, which fall into 3 categories:

1. Too many resizes. Status: Acceptable.

2. Slow painting. Status: It seems other gtk2 apps are as slow, so maybe it is a gtk2 issue.

3. Some resizes happen after painting. Status: Acceptable.

Things done to handle the above issues:

Done: Selecting a TPage in the designer gives the wrong bounds rectangle

GetClientOrigin needed extra code for TNotebook, which does not have a normal client area.

Done: Sometimes the hint window of the component palette is not resized

Sometimes the gtk does not resize the gdkwindow. This is now forced and it seems to fix this problem.

Done: gtk criticals in IDE codetools options dialog

Empty comboboxes do not have all private structures, so you can not set callbacks.

Done: System Colors

The system colors depend on the theme, but many themes don't give adequate colors and many controls use system colors, so we need them to have a correct value. To solve this, a list of ignored system colors was constructed, and in case the user sets any of these colors, it is ignored and the default theme color is used instead.

List of colors which were tested and need to be ignored for the correct functioning of programs:

Bug reports

Gtk1/Gtk2 separation roadmap

List of already separated elements:

Next tasks:

Profiling

There are various compiler switches:


From the Free Pascal wiki, page "GTK2 Interface", as it stood on 2022-11-25 after a change by Dbannon, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.

Revision 3, last changed by WikiArchive .

Talk about this page

Nobody has opened a bench for this page yet. Sign in to open one.