PHP-GTK2 Newsletter
PHP-GTK2 Tips & Techniques
FREE Newsletter
by kksou
Sample Code 156: How to insert images in GtkTextView? |
|
Written by kksou
|
|
Monday, 29 January 2007 |
|
Problem You want to allow users to insert images in a GtkTextView as shown below:

Solution
Sample Code The following image files are required by the sample code below. Please save a copy of the image files and put them in the same directory where you store the sample code.
 | icon_smile.gif |
 | icon_lol.gif |
 | icon_cool.gif |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| <?php $window = new GtkWindow(); $window->set_size_request(400, 240); $window->connect_simple('destroy', array('Gtk','main_quit')); $window->add($vbox = new GtkVBox());
// display title
$title = new GtkLabel("Insert images in GtkTextView\n". "Click on the Emoticons to insert the smileys"); $title->modify_font(new PangoFontDescription("Times New Roman Italic 10")); $title->modify_fg(Gtk::STATE_NORMAL, GdkColor::parse("#0000ff")); $title->set_size_request(-1, 40); $title->set_justify(Gtk::JUSTIFY_CENTER); $alignment = new GtkAlignment(0.5, 0.5, 0, 0); $alignment->add($title); $vbox->pack_start($alignment);
// Setup TextView
$buffer = new GtkTextBuffer(); $view = new GtkTextView(); $view->set_buffer($buffer); $view->modify_font(new PangoFontDescription("Arial 10")); $view->set_wrap_mode(Gtk::WRAP_WORD);
$hbox = new GtkHBox(); $hbox->pack_start(new ClickableImage('icon_smile.gif'), 0); // note 2
$hbox->pack_start(new ClickableImage('icon_lol.gif'), 0); // note 2
$hbox->pack_start(new ClickableImage('icon_cool.gif'), 0); // note 2
$vbox->pack_start($hbox, 0);
$scrolled_win = new GtkScrolledWindow(); $scrolled_win->set_policy( Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC); $vbox->pack_start($scrolled_win); $scrolled_win->add($view);
$window->show_all(); Gtk::main();
class ClickableImage extends GtkEventBox { // note 1
function __construct($img_filename) {
|
- Note that this is only 70% of the sample code. You have to be a registered member to see the entire sample code. Please login or register.
- Registration is free and immediate.
- Have some doubt about the registration? Please read this forum article.
Explanation
- Extends GtkEventBox to form a new class ClickableImage so that the icons are clickable.
- Set up three clickable Emoticons.
- Make the icons clickable.
- Get the current cursor position.
- Load the image.
- Create an anchor in GtkBuffer.
- Add the image at the anchor location.
User reviews There are no user reviews yet. Note: You have to be a registered member to leave a comment. Free registration here. |
|