Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create_tag() in GtkTextBuffer does not work as expected #102

Open
SantosSi opened this issue Apr 24, 2024 · 2 comments
Open

create_tag() in GtkTextBuffer does not work as expected #102

SantosSi opened this issue Apr 24, 2024 · 2 comments

Comments

@SantosSi
Copy link

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);

@scorninpc
Copy link
Owner

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

@SantosSi
Copy link
Author

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();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants