PHP-GTK Tutorial - Getting text from a GtkTextView / Text Box in PHP-GTK

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    PHP-GTK Tutorial - Getting text from a GtkTextView / Text Box in PHP-GTK



    This tutorial is part of a series of PHP-GTK tutorials. For a full list of tutorials, see this thread.

    In order to display a text box on a GTK+ application, you need to make use of the GtkTextView class. A GtkTextView is used to be able to display a large portion of text in a text box on your GTK+ application window. If you are wondering as to how to make text scroll after it spans longer than the size of the GtkTextView (which is also relative to the bounds of the container it may be in and the size of the GtkWindow), you can make use of a GtkScrolledWindow. The GtkTextView actually added to the GtkScrolledWindow, and then the GtkScrolledWindow is packed into the container which is then subsequently added to the GtkWindow. An example of using a GtkScrolledWindow is like follows:

    PHP Code:
    $gtkScrolledWindow = new GtkScrolledWindow();
    $gtkScrolledWindow->add($textView);
    $gtkScrolledWindow->set_policy(Gtk::POLICY_AUTOMATIC,Gtk::POLICY_AUTOMATIC);
    $init_vbox->pack_start($gtkScrolledWindow); 
    The Gtk::POLICY_AUTOMATIC "policy" displays the scrollbars only as necessary. In other words, only when the text spans where the scrollbar is required to scroll through the GtkTextView.

    Going back onto the purpose of this tutorial, in order to fetch the current text within a GtkTextView, you need to fetch the current contents of the buffer. In most instances, you'd be fetching the text from the GtkTextView when an event is fired - perhaps as a result of a GtkButton press. To fetch the contents of the GtkTextView:

    PHP Code:
    $get_buffer $this->textView->get_buffer();
    $get_text $get_buffer->get_text($get_buffer->get_start_iter(), $get_buffer->get_end_iter()); 
    What's happening here? Well, the contents of the GtkTextView is stored in a buffer. To fetch the contents of the buffer, you have to make use of the get_buffer() method of the GtkTextView class. The get_buffer() method returns a GtkTextBuffer object. You can then make use of the get_text() method of the GtkTextBuffer class to fetch the text. However, you have to specify a specific range to fetch (the first parameter of get_text() is for the start and the second parameter is for the end). If you want to fetch the entire contents, luckily, you can make use of the get_start_iter() and get_end_iter() of the GtkTextBuffer class to fetch the entire contents.

    And that's it. If you intend to save the contents of the buffer to a file, you can then save the contents of $get_text to a file using the file_put_contents() function, and of course, fetch the contents of a file and put it into a GtkTextView using the file_get_contents() function in PHP.

    I would recommend you make extensive use of the PHP-GTK and PyGTK documentation for help while developing your PHP-GTK applications. In some areas, the PyGTK documentation is more elaborative than many areas of the PHP-GTK documentation - so make use of both. You can also check the documentation on gtk.org.
Working...
X