scorninpc / php-gtk3

GTK3 extension for PHP
https://andor.com.br/
GNU Lesser General Public License v3.0
118 stars 13 forks source link

Extendable GTK classes support #117

Closed d47081 closed 4 months ago

d47081 commented 4 months ago

I found interesting situation in app development.

To receive selected GtkNotebook Label, I'm using this construction:

        $this->gtk->connect(
            'switch-page',
            function (
                \GtkNotebook $entity,
                \GtkWidget $child,
                int $position
            ) {
                $label = $entity->get_tab_label(
                    $child
                );

                $this->container->browser->header->setTitle(
                    $label->get_text(),
                    $label->get_subtitle() // extended
                );
            }
        );

where $label->get_subtitle() is extra-feature implemented by GtkLabel extension class:

<?php

declare(strict_types=1);

namespace Yggverse\Yoda\Gtk\Browser\Container\Tab\Page\Title;

class Label extends \GtkLabel
{
    private string $_subtitle;

    public function set_subtitle(
        string $value
    ): void
    {
        $this->_subtitle = $value;
    }

    public function get_subtitle(): string
    {
        return $this->_subtitle;
    }
}

Of course, I got Fatal error: Undefined method get_subtitle because this method not defined in PHP-GTK3

But suppose it is possible to make extendable PHP classes support, or not?

scorninpc commented 4 months ago

This is not a PHP-GTK problem

If you provide a full working code, i can help with your logic

scorninpc commented 4 months ago

This is the case?

<?php
    /**
     * custom label
     */
    class CustomLabel extends \GtkLabel
    {
        private string $_subtitle;

        public function set_subtitle(string $value): void
        {
            $this->_subtitle = $value;
        }

        public function get_subtitle(): string
        {
            return $this->_subtitle;
        }
    }

    Gtk::init();

    $window = new GtkWindow();
    $window->set_size_request(500, 500);
    $window->set_title("PHP-GTK");

    // create the notebook
    $notebook = new GtkNotebook();
    $window->add($notebook);
    $notebook->set_scrollable(true);

    // store all labels
    $labels = [];

    // add page 1
    $labels[] = new CustomLabel("Page 1");
    $notebook->append_page(GtkButton::new_with_label("Container 1"), end($labels));

    // add page 2
    $labels[] = new CustomLabel("Page 2");
    $notebook->append_page(GtkButton::new_with_label("Container 2"), end($labels));

    // on switch page
    $notebook->connect("switch-page", function($widget, $page, $index) use ($labels) {

        // retriave custom label by tab index
        $label = $labels[$index];

        // use it
        var_dump($label->set_subtitle("OK"));
        var_dump($label);
    });

    // connect to signal that close program
    $window->connect("destroy", function() {
        Gtk::main_quit();
    });

    // show all and start
    $window->show_all();
    Gtk::main();

As PHP-GTK as a language bind, I need to do alot cast internally, so this destroy PHP classes and pass GTK class. You'll not able to create a PHP class in GTK heap. I cannot directly cast PHP class to a GPointer, you get it?

d47081 commented 4 months ago

Thanks, seems that global array could help.

Just interested in solution where PHP-GTK able to return back the extended object constructed by PHP. Anyway, even if it's possible by PHP-CPP, it may cause assertion issues, so yes, closed maybe.

d47081 commented 4 months ago

As PHP-GTK as a language bind, I need to do alot cast internally, so this destroy PHP classes and pass GTK class. You'll not able to create a PHP class in GTK heap. I cannot directly cast PHP class to a GPointer, you get it?

I don't understand exactly how does it work, but meant something like magic methods:

Php::Value GtkWidget_::__get(const Php::Value &name)
{
    return Php::Base::__get(name);
}
scorninpc commented 4 months ago

This is more of a GTK issue, not PHP-CPP. I can't cast a PHP object to gpointer and store the original PHP class. So when I use a GTK method that returns a gpointer, I can't correlate it with the original PHP class

Maybe if I rewrite all extension to store a PHP object, and not a GTK instance https://github.com/scorninpc/php-gtk3/blob/master/src/G/GObject.h#L27 (or both) .... So maybe in PHP-GTK4 I can try something, but i dont thing it's will work with alot internal hard coded references