PHP-GTK2 Newsletter

PHP-GTK2 Tips & Techniques
FREE Newsletter
by kksou



Sample Code 146: How to paste text from clipboard into GtkTextView - Part 1?
Written by kksou   
Thursday, 18 January 2007
Problem

You want to paste text from clipboard into GtkTextView by pressing the Insert key as shown below:

How to paste text from clipboard into GtkTextView - Part 1?


Solution

Sample Code

Note: Copy some text into clipboard from other applications, then press the Insert button. Text will be inserted at the cursor position.

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   
<?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("Paste Text from clipboard into GtkTextView - Part 1\n".
"Press insert to paste the text");
$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, 0, 0);
$alignment->add($title);
$vbox->pack_start($alignment, 0, 0);
$vbox->pack_start(new GtkLabel(), 0, 0);

// Create a new buffer and a new view to show the buffer.
$buffer = new GtkTextBuffer();
$view = new GtkTextView();
$view->set_buffer($buffer);
$view->modify_font(new PangoFontDescription("Arial 12"));
$view->set_wrap_mode(Gtk::WRAP_WORD);

$scrolled_win = new GtkScrolledWindow();
$scrolled_win->set_policy( Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
$vbox->pack_start($scrolled_win);

$scrolled_win->add($view);

$clipboard = new GtkClipboard($view->get_display(), 
    Gdk::atom_intern('CLIPBOARD')); // note 1
$window->connect('key-press-event', 'on_keypress');
$window->show_all();
  • 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
  1. Set up the clipboard.
  2. If insert key is pressed, paste the text from clipboard into GtkTextBuffer at the cursor position.
  3. For all other keys, pass them back to the default keypress handler.

Note

Note that you can also press Ctrl-V to paste the text. It's inherently supported by php-gtk. So why go through all the trouble to duplicate the same functionality? For more control, of course...!

In the next article, we will see how we can do some processing before inserting the text into the GtkTextBuffer.


Related Links
 

Add comment


Security code
Refresh

< Prev   Next >

Blog - Forum - Privacy Policy - Contact Us
Links - Classes - Social Business - BPM - Web - General
Copyright © 2006-2013. kksou.com. All Rights Reserved