php-gtk3 icon indicating copy to clipboard operation
php-gtk3 copied to clipboard

create_tag() in GtkTextBuffer does not work as expected

Open SantosSi opened this issue 1 year ago • 2 comments

The purpose was to change the appearance on a text without changing the text itself, so no HTML markup allowed.

// txtvMsgstr is a GtkTextView from a GLADE .ui file
$txtViewMsgstr = $this->builder->get_object('txtvMsgstr');
$txtBufferMsgStr = $txtViewMsgstr->get_buffer();
$txtTagUnderline    = $txtBuffer->create_tag('underline', 'underline', 1);

This issues the following error message: value "((PangoUnderline) -1472307712)" of type 'PangoUnderline' is invalid or out of range for property 'underline' of type 'PangoUnderline'

gtk3-demo uses in TextView/Hypertext: "underline", PANGO_UNDERLINE_SINGLE but since there is no such or similarly named constant in PHP-GTK3, I looked up its definition in the GTK3 source code, which defines the value as 1 and used that instead.

Other unsuccessful tests, because $txtBuffer->get_tag_table() returns an empty array:

         $txtTagHighlightB   = $txtBuffer->create_tag('bgBlue', 'background', 'blue');
         $txtTagHighlightO   = $txtBuffer->create_tag('bgOrange', 'background', 'orange');
         $txtTagFgRed        = $txtBuffer->create_tag('fgRed', 'foreground', 'red');
         $txtTagUnderlineSet = $txtBuffer->create_tag('underlineSet', 'underline-set', true);
         $txtTagUnderline    = $txtBuffer->create_tag('underline', 'underline', true);
         $txtTagBold         = $txtBuffer->create_tag('bold', 'weight', 1);
         $txtTagItalics      = $txtBuffer->create_tag('italics', 'style', 'italics');
         $txtTagStrike       = $txtBuffer->create_tag('strikethrough', 'strikethrough', true);

SantosSi avatar Apr 24 '24 15:04 SantosSi

You can always use value instead of constant

https://docs.gtk.org/Pango/enum.Underline.html

Anyway, I'll try to do some demonstration, and if something doesn't work I can fix it.

If you can provide a small complete example will be nice

scorninpc avatar Apr 24 '24 18:04 scorninpc

Ack, that was where I got the value from for the constant!

Here's a complete example:

<?php
namespace silverio\MyTestSpace;
class Test
{
    function testing()
    {
        $msgstr = 'This is a test!';
        $start = 5;
        $end = 7;

        \Gtk::init();
        $window = new \GtkWindow();
        $txtViewMsgstr = new \GtkTextView();
        $window->add($txtViewMsgstr);
        $txtBuffer = $txtViewMsgstr->get_buffer();
        $txtBuffer->set_text($msgstr);
        $txtTagUnderline = $txtBuffer->create_tag('underline', 'underline', 1);
        $startIter = $txtBuffer->get_iter_at_offset($start);
        $endIter = $txtBuffer->get_iter_at_offset($end);
        $txtBuffer->apply_tag($txtTagUnderline, $startIter, $endIter);
        $window->connect("destroy", fn() => \Gtk::main_quit());
        $window->show_all();
        \Gtk::main();
    }
}

$test = new Test;
$test->testing();

SantosSi avatar Apr 24 '24 19:04 SantosSi