Help language development. Donate to The Perl Foundation

Gnome::Gtk3 cpan:MARTIMM last updated on 2019-06-10

perl6-gnome-gtk3-0.15.0/

[toc]

Gnome Gtk3 - Widget toolkit for graphical interfaces

License

Description

First of all, I would like to thank the developers of the GTK::Simple project because of the information I got while reading the code. Also because one of the files is copied unaltered for which I did not had to think about to get that right. The examples in that project are also useful to compare code with each other and to see what is or is not possible.

The purpose of this project is to create an interface to the GTK+ version 3 library.

History

There is already a bit of history for this package. It started off building the GTK::Glade package which soon became too big. So a part was separated into GTK::V3. After some working with the library I felt that the class names were a bit too long and that the words gtk and gdk was repeated too many times in the class path. E.g. there was GTK::V3::Gtk::GtkButton and GTK::V3::Gdk::GdkScreen to name a few. So, finally it was split into several other packages named, Gnome::N, Gnome::Glib, Gnome::GObject, Gnome::Gdk3 and Gnome::Gtk3 according to what is shown on the developers page here. The classes in these packages are now renamed into e.g. Gnome::Gtk3::Button, Gnome::Gdk3::Screen, Gnome::GObject::Object and Gnome::Glib::List. As a side effect the package GTK::Glade is also renamed into Gnome::Glade3 to show that it is from Gnome and that it is based on Gtk version 3.

Example

This example does the same as the example from GTK::Simple to show you the differences between the implementations. What immediately is clear is that this example is somewhat longer. To sum up;

Pros

Cons

A screenshot of the example -this screenshot-. The code can be found at examples/01-hello-world.pl6.

use v6;

use Gnome::Gtk3::Main;
use Gnome::Gtk3::Window;
use Gnome::Gtk3::Grid;
use Gnome::Gtk3::Button;

# Instantiate main module for UI control
my Gnome::Gtk3::Main $m .= new;

# Class to handle signals
class AppSignalHandlers {

  # Handle 'Hello World' button click
  method first-button-click ( :widget($b1), :other-button($b2) ) {
    $b1.set-sensitive(False);
    $b2.set-sensitive(True);
  }

  # Handle 'Goodbye' button click
  method second-button-click ( ) {
    $m.gtk-main-quit;
  }

  # Handle window managers 'close app' button
  method exit-program ( ) {
    $m.gtk-main-quit;
  }
}

# Create a top level window and set a title
my Gnome::Gtk3::Window $top-window .= new(:empty);
$top-window.set-title('Hello GTK!');
$top-window.set-border-width(20);

# Create a grid and add it to the window
my Gnome::Gtk3::Grid $grid .= new(:empty);
$top-window.gtk-container-add($grid);

# Create buttons and disable the second one
my Gnome::Gtk3::Button $button .= new(:label('Hello World'));
my Gnome::Gtk3::Button $second .= new(:label('Goodbye'));
$second.set-sensitive(False);

# Add buttons to the grid
$grid.gtk-grid-attach( $button, 0, 0, 1, 1);
$grid.gtk-grid-attach( $second, 0, 1, 1, 1);

# Instantiate the event handler class and register signals
my AppSignalHandlers $ash .= new;
$button.register-signal(
  $ash, 'first-button-click', 'clicked',  :other-button($second)
);
$second.register-signal( $ash, 'second-button-click', 'clicked');

$top-window.register-signal( $ash, 'exit-program', 'destroy');

# Show everything and activate all
$top-window.show-all;

$m.gtk-main;

Design

I want to follow the interface of the classes in Gtk, Gdk and Glib as closely as possible by keeping the names of the native functions the same as provided with the following exceptions; * The native subroutines are defined in their corresponding classes. They are set up in such a way that they have become methods in those classes. Many subs also have as their first argument the native object. This object is held in the class and is automatically inserted when the sub is called. E.g. a definition like the following in the Gnome::Gtk3::Button class

sub gtk_button_set_label ( N-GObject $widget, Str $label )
  is native(&gtk-lib)
  { * }

can be used as

my Gnome::Gtk3::Button $button .= new(:empty);
$button.gtk_button_set_label('Start Program');
$button.gtk_widget_set_tooltip_text('When pressed, program will start');
my Str $button-label = $button.get_label;

In the documentation this will be shown with brackets around the part that can be left out. In this case it is shown as [gtk_button_] get_label.

my Str $button-label1 = $button.gtk-button-get-label;
my Str $button-label2 = $button.get-label;
sub gtk_grid_attach (
  N-GObject $grid, N-GObject $child,
  int32 $x, int32 $y, int32 $width, int32 $height
) is native(&gtk-lib)
  { * }

And its use;

my Gnome::Gtk::Grid $grid .= new(:empty);
my Gnome::Gtk::Label $label .= new(:label('server name'));
$grid.gtk-grid-attach( $label, 0, 0, 1, 1);

Errors and crashes

I came to the conclusion that Perl6 is not (yet) capable to return a proper message when some type of mistakes are made. E.g. spelling errors or using wrong types when using the native call interface. Most of them end up in MoarVM panic: Internal error: Unwound entire stack and missed handler. Other times it ends in just a plain crash. Some of the crashes are happening within GTK and cannot be captured by Perl6. One of those moments are the use of GTK calls without initializing GTK with gtk_init. The panic mentioned above mostly happens when perl6 code is called from C as a callback and an exception is (re)thrown. The stack might not be interpreted completely at that moment hence the message.

A few measures are implemented to help a bit preventing problems;

Documentation

Pdf from pod Link to Gnome Developer
Gnome::Gtk3::AboutDialog GtkAboutDialog.html
Gnome::Gtk3::Bin GtkBin.html
Gnome::Gtk3::Builder GtkBuilder.html
Gnome::Gtk3::Button GtkButton.html
[Gnome::Gtk3::Box][Gnome::Gtk3::Box pdf] GtkBox.html
Gnome::Gtk3::CheckButton GtkCheckButton.html
Gnome::Gtk3::ComboBox GtkComboBox.html
Gnome::Gtk3::ComboBoxText GtkComboBoxText.html
Gnome::Gtk3::ColorButton GtkColorButton.html
[Gnome::Gtk3::ColorChooser][Gnome::Gtk3::ColorChooser pdf] GtkColorChooser.html
Gnome::Gtk3::ColorChooserDialog GtkColorChooserDialog.html
[Gnome::Gtk3::ColorChooserWidget][Gnome::Gtk3::ColorChooserWidget pdf] GtkColorChooserWidget.html
Gnome::Gtk3::Container GtkContainer.html
Gnome::Gtk3::CssProvider GtkCssProvider.html
Gnome::Gtk3::StyleContext GtkStyleContext.html
Gnome::Gtk3::Dialog GtkDialog.html
Gnome::Gtk3::Entry GtkEntry.html
Gnome::Gtk3::FileChooser GtkFileChooser.html
Gnome::Gtk3::FileChooserDialog GtkFileChooserDialog.html
Gnome::Gtk3::FileFilter GtkFileFilter.html
Gnome::Gtk3::Grid GtkGrid.html
Gnome::Gtk3::Image GtkImage.html
Gnome::Gtk3::ImageMenuItem GtkImageMenuItem.html
Gnome::Gtk3::Label GtkLabel.html
[ Gnome::Gtk3::LevelBar ][ Gnome::Gtk3::LevelBar pdf] GtkLevelBar.html
Gnome::Gtk3::ListBox GtkListBox.html
Gnome::Gtk3::Main GtkMain.html
Gnome::Gtk3::MenuItem GtkMenuItem.html
Gnome::Gtk3::Orientable GtkOrientable.html
Gnome::Gtk3::Paned GtkPaned.html
Gnome::Gtk3::RadioButton GtkRadioButton.html
Gnome::Gtk3::Range GtkRange.html
Gnome::Gtk3::Scale GtkScale.html
Gnome::Gtk3::StyleContext GtkStyleContext.html
Gnome::Gtk3::TextBuffer GtkTextBuffer.html
Gnome::Gtk3::TextTagTable GtkTextTagTable.html
Gnome::Gtk3::TextView GtkTextView.html
Gnome::Gtk3::ToggleButton GtkToggleButton.html
Gnome::Gtk3::Widget GtkWidget.html
Gnome::Gtk3::Window GtkWindow.html

Release notes

Miscellaneous

TODO

Versions of involved software

Installation

There are several crossing dependencies from one package to the other because it was one package in the past. To get all packages, just install the Gnome::Gtk3 package and the rest will be installed with it.

zef install Gnome::Gtk3

Issues

There are always some problems! If you find one, please help by filing an issue at my github project.

Attribution

Author

Name: Marcel Timmerman Github account name: MARTIMM