Difference between revisions of "Builder Files"

From Inkscape Wiki
Jump to navigation Jump to search
Line 28: Line 28:
gtk-builder-tool does not understand the "child" property (in GtkScrolledWindow, GtkViewPort).
gtk-builder-tool does not understand the "child" property (in GtkScrolledWindow, GtkViewPort).


One can use a script to restore "True", "False", '"', and <nowiki>'''</nowiki> (the latter to avoid changing translatable strings). It works except for the property 'active' which can be a boolean (CheckButton) or an 'int' (ComboBox).
One can use a script to restore "True", "False", (without touching the 'active' property in ComboBox's). It also restores quotes and ampersands in strings (to avoid breaking translations). See below.


=== gtk4-builder-tool ===
=== gtk4-builder-tool ===
Line 164: Line 164:
* GtkWindow::window-position
* GtkWindow::window-position
* GtkWindow::window_position
* GtkWindow::window_position
== Restore "True"/"False"/quotes/ampersands. ==
=== Perl Script ===
<pre>
#!/usr/bin/perl
# Restore use of True (instead of '1') and False (instead of '0') after
# using gtk-builder-tool or gtk4-builder-tool.
use strict;
use warnings;
use File::Copy;
open my $true_false, '<', "true_false.txt" or die "Cannot read true_false.txt";
chomp (my @properties = <$true_false>);
my $pattern = "";
foreach (@properties) {
    $pattern = $pattern . "\"";
    $pattern = $pattern . $_;
    $pattern = $pattern . "\"";
    $pattern = $pattern. "|";
}
chop $pattern;
#print $pattern . "\n";
my $file = $ARGV[0];
#print $file . "\n";
my $file_out = $file . "_out";
open my $input, '<', $file or die "Cannot open $file";
open my $output, '>', $file_out or die "Cannot open $file_out";
my $class = "";
while (my $line = <$input>) {
    # Track current class
    if ($line =~ m/class=\"(.*?)\"/) {
        $class = $1;
        # print "Class: " . $class . "\n";
    }
    # For gtk4-builder-tool
    $line =~ s/translatable=\"1\"/translatable=\"yes\"/;
    $line =~ s/translatable=\"0\"/translatable=\"no\"/;
    if ($line =~ m/($pattern)/) {
        #  print $line;
        if ($line =~ m/active/ and $class =~ m/ComboBox/) {
            # Don't change 'active' property to True/False for ComboBox or ComboBoxText
            # print "FOUND COMBOBOX WITH ACTIVE\n";
        } else {
            $line =~ s/>0</>False</;
            $line =~ s/>1</>True</;
        }
        # print $line;
    }
    $line =~ s/&amp;quot;/\"/g;  # This is improper... but it would effect translations.
    $line =~ s/&amp;apos;/\'/g;
    print $output $line;
}
close $input;
close $output;
move ($file_out, $file) or die "Couldn't move $file_out to $file!";
</pre>
=== Input File: save as true_false.txt ===
<pre>
accepts-tab
activate-on-single-click
activates-default
active
always-show-image
can-default
can-focus
caps-lock-warning
column-homogeneous
cursor-visible
destroy-with-parent
double-buffered
draw-as-radio
draw-indicator
draw-value
editable
enable-popup
enable-search
expand
fill
fixed-height-mode
focus-on-click
has-default
has-entry
has-focus
has-frame
has-origin
has-resize-grip
has-tooltip
headers-clickable
headers-visible
hexpand
homogeneous
hover-selection
inline-completion
inverted
is-focus
label-fill
modal
no-show-all
numeric
overlay-scrolling
pass-through
popup-set-width
position-set
primary-icon-activatable
primary-icon-sensitive
propagate-natural-height
propagate-natural-width
receives-default
reserve-toggle-size
resizable
resize
resize-toplevel
reveal-child
row-homogeneous
search-mode-enabled
secondary-use-markup
selectable
sensitive
show-border
show-expanders
show-fill-level
show-text
shrink
single-line-mode
skip-taskbar-hint
sort-indicator
tab-expand
tab-fill
transitions-enabled
urgency-hint
use-action-appearance
use-fallback
use-markup
use-popover
use-stock
use-underline
vexpand
visible
visible-window
wide-handle
window-placement-set
wrap
</pre>

Revision as of 10:01, 22 August 2023


Gtk allows one to define the UI interface using "Builder" files. "Builder" files are XML files that describe widget layout, menus, etc.

Builder files should end with .ui but many of ours end with .glade as the UI creater program Glade was used to create them. Glade works only with Gtk3. Cambalache is a Glade replacement that works with both Gtk3 and Gtk4.

Tools

gtk-builder-tool

Allows one to simplify or examine Gtk3 builder files. Using the simplify option over our builder files results in:

  • Changing "True" to "1", "False" to "0"
  • Changing '"' to &quot; and ''' to &apos;.
  • Removing properties with default values, e.g.:
    • "can-focus" == False
    • "label-xalign" == 0
    • "receives-default" == False
    • "expand" == False
    • "fill" == True
    • "position" == 0
    • "xalign" == 0.5
  • Removing unneeded <packing>...</packing> (where all properties are default).
  • Removing <child><placeholder/></child> and <placeholder/>
  • Moving <!-- n-columns=x n-rows=y --> comment to before <child>.
  • Replacing underscores by hyphens in property names.

gtk-builder-tool does not understand the "child" property (in GtkScrolledWindow, GtkViewPort).

One can use a script to restore "True", "False", (without touching the 'active' property in ComboBox's). It also restores quotes and ampersands in strings (to avoid breaking translations). See below.

gtk4-builder-tool

In addition to the uses of gtk-builder-tool, gtk4-bulder-tool has a --3to4 option that converts gtk3 builder files to gtk4 builder files, the conversion is not perfect. In particular, it does not remove properties it doesn't recognize.

Using the 'simplify' mode without --3to4 (after using gtk-builder-tool):

  • Changes translatable="true" or "yes" to translatable="1".
  • Strips many comments.

Using the 'simplify' mode with --3to4 results in:

  • Replacing <requires lib="gtk+" version="3.24"/> with <requires lib="gtk" version="4.0"/>
  • Changes <attribute name='xxx'... to <attribute name="xxx"...
  • Removing the property "visible" when its value is "True" (widgets are visible by default in Gtk4).
  • Replacing the property "can-focus" with value "True" by the property "focusable" with value 1.
  • Removing <packing>...</packing> for GtkBox.
  • Replacing <object>...</object><packing>...</packing> by <object>...<layout>...</layout></object> for GtkGrid.
  • Changing <child>...</child> to <property name="child">...</property> for child of GtkFlowBoxChild and GtkViewPort.
  • Changing the property "margin" to the four properties "margin-start", "margin-end", "margin-top", and "margin-bottom".
  • Adding the property "draw-value" with value 1 for GtkScale.
  • Adding the property "visible" with value 0 to GtkFlowBoxChild.

Properties not supported in Gtk4 and not replaced:

  • GtkAspectFrame::label-xalign
  • GtkAspectFrame::shadow-type
  • GtkBox::double-buffered
  • GtkBox::events
  • GtkBox::no-show-all
  • GtkButton::always-show-image
  • GtkButtonBox::can-focus
  • GtkButtonBox::hexpand
  • GtkButtonBox::layout-style
  • GtkButtonBox::visible
  • GtkButton::can-default
  • GtkButton::image
  • GtkButton::image-position
  • GtkButton::no-show-all
  • GtkButton::relief
  • GtkButton::use-stock
  • GtkCellRendererPixbuf::stock_size
  • GtkCheckButton::image
  • GtkCheckButton::no-show-all
  • GtkDialog::gravity
  • GtkDialog::has-resize-grip
  • GtkDialog::skip-taskbar-hint
  • GtkDialog::type-hint
  • GtkDialog::type_hint
  • GtkDialog::window-position
  • GtkEntry::caps-lock-warning
  • GtkEntry::no_show_all
  • GtkEventBox::can-focus
  • GtkEventBox::can_focus
  • GtkEventBox::halign
  • GtkEventBox::hexpand
  • GtkEventBox::name
  • GtkEventBox::no-show-all
  • GtkEventBox::valign
  • GtkEventBox::visible
  • GtkEventBox::visible-window
  • GtkExpander::label-fill
  • GtkFlowBox::no-show-all
  • GtkFrame::shadow-type
  • GtkGrid::no-show-all
  • GtkImage::no-show-all
  • GtkImage::stock
  • GtkLabel::no-show-all
  • GtkLabel::no_show_all
  • GtkLabel::ypad
  • GtkLinkButton::relief
  • GtkMenuButton::draw-indicator
  • GtkMenuButton::image
  • GtkMenuButton::no-show-all
  • GtkMenuButton::popup
  • GtkMenuButton::relief
  • GtkMenuButton::use-popover
  • GtkMenu::can-focus
  • GtkMenuItem::can-focus
  • GtkMenuItem::use-underline
  • GtkMenuItem::visible
  • GtkMenu::reserve-toggle-size
  • GtkMenu::visible
  • GtkModelButton::action-name
  • GtkModelButton::action-target
  • GtkModelButton::can-focus
  • GtkModelButton::halign
  • GtkModelButton::inverted
  • GtkModelButton::receives-default
  • GtkModelButton::visible
  • GtkNotebook::double-buffered
  • GtkNotebook::no-show-all
  • GtkPopover::constrain-to
  • GtkPopover::no-show-all
  • GtkPopover::relative-to
  • GtkPopover::transitions-enabled
  • GtkRadioMenuItem::can-focus
  • GtkRadioMenuItem::draw-as-radio
  • GtkRadioMenuItem::group
  • GtkRadioMenuItem::use-underline
  • GtkRadioMenuItem::visible
  • GtkScrolledWindow::double-buffered
  • GtkScrolledWindow::no-show-all
  • GtkScrolledWindow::shadow-type
  • GtkScrolledWindow::shadow_type
  • GtkScrolledWindow::window-placement-set
  • GtkSearchEntry::can-default
  • GtkSearchEntry::caps-lock-warning
  • GtkSearchEntry::completion
  • GtkSearchEntry::is-focus
  • GtkSearchEntry::primary-icon-activatable
  • GtkSearchEntry::primary-icon-name
  • GtkSearchEntry::primary-icon-sensitive
  • GtkSeparatorMenuItem::can-focus
  • GtkSeparatorMenuItem::margin-bottom
  • GtkSeparatorMenuItem::margin-top
  • GtkSeparatorMenuItem::visible
  • GtkSpinButton::caps-lock-warning
  • GtkSpinButton::input-purpose
  • GtkSpinner::active
  • GtkToggleButton::always-show-image
  • GtkToggleButton::image
  • GtkToggleButton::no-show-all
  • GtkToggleButton::relief
  • GtkToggleButton::use-action-appearance
  • GtkToolItem::can-focus
  • GtkToolItem::visible
  • GtkTreeView::no_show_all
  • GtkViewport::shadow-type
  • GtkWindow::can_default
  • GtkWindow::gravity
  • GtkWindow::is_focus
  • GtkWindow::urgency_hint
  • GtkWindow::window-position
  • GtkWindow::window_position

Restore "True"/"False"/quotes/ampersands.

Perl Script

#!/usr/bin/perl

# Restore use of True (instead of '1') and False (instead of '0') after
# using gtk-builder-tool or gtk4-builder-tool.

use strict;
use warnings;
use File::Copy;

open my $true_false, '<', "true_false.txt" or die "Cannot read true_false.txt";
chomp (my @properties = <$true_false>);

my $pattern = "";
foreach (@properties) {
    $pattern = $pattern . "\"";
    $pattern = $pattern . $_;
    $pattern = $pattern . "\"";
    $pattern = $pattern. "|";
}
chop $pattern;
#print $pattern . "\n";

my $file = $ARGV[0];
#print $file . "\n";

my $file_out = $file . "_out";
open my $input, '<', $file or die "Cannot open $file";
open my $output, '>', $file_out or die "Cannot open $file_out";

my $class = "";
while (my $line = <$input>) {

    # Track current class
    if ($line =~ m/class=\"(.*?)\"/) {
        $class = $1;
        # print "Class: " . $class . "\n";
    }

    # For gtk4-builder-tool
    $line =~ s/translatable=\"1\"/translatable=\"yes\"/;
    $line =~ s/translatable=\"0\"/translatable=\"no\"/;

    if ($line =~ m/($pattern)/) {
        #  print $line;
        if ($line =~ m/active/ and $class =~ m/ComboBox/) {
            # Don't change 'active' property to True/False for ComboBox or ComboBoxText
            # print "FOUND COMBOBOX WITH ACTIVE\n";
        } else {
            $line =~ s/>0</>False</;
            $line =~ s/>1</>True</;
        }
        # print $line;
    }
    $line =~ s/&quot;/\"/g;  # This is improper... but it would effect translations.
    $line =~ s/&apos;/\'/g;
    print $output $line;
}

close $input;
close $output;
move ($file_out, $file) or die "Couldn't move $file_out to $file!";

Input File: save as true_false.txt

accepts-tab
activate-on-single-click
activates-default
active
always-show-image
can-default
can-focus
caps-lock-warning
column-homogeneous
cursor-visible
destroy-with-parent
double-buffered
draw-as-radio
draw-indicator
draw-value
editable
enable-popup
enable-search
expand
fill
fixed-height-mode
focus-on-click
has-default
has-entry
has-focus
has-frame
has-origin
has-resize-grip
has-tooltip
headers-clickable
headers-visible
hexpand
homogeneous
hover-selection
inline-completion
inverted
is-focus
label-fill
modal
no-show-all
numeric
overlay-scrolling
pass-through
popup-set-width
position-set
primary-icon-activatable
primary-icon-sensitive
propagate-natural-height
propagate-natural-width
receives-default
reserve-toggle-size
resizable
resize
resize-toplevel
reveal-child
row-homogeneous
search-mode-enabled
secondary-use-markup
selectable
sensitive
show-border
show-expanders
show-fill-level
show-text
shrink
single-line-mode
skip-taskbar-hint
sort-indicator
tab-expand
tab-fill
transitions-enabled
urgency-hint
use-action-appearance
use-fallback
use-markup
use-popover
use-stock
use-underline
vexpand
visible
visible-window
wide-handle
window-placement-set
wrap