PHP-GTK2 Newsletter

PHP-GTK2 Tips & Techniques
FREE Newsletter
by kksou



Sample Code 147: How to paste text from clipboard into GtkTextView - Part 2?
Written by kksou   
Friday, 19 January 2007
Problem

You want to paste text from clipboard into GtkTextView by pressing the Insert key, and you want any html tags to be removed before the text are inserted as shown below:

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


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   
36   
37   
38   
39   
<?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 2\n".
"Press Insert to paste the text.\n".
"Will remove all html tags");
$title->modify_font(new PangoFontDescription("Times New Roman Italic 10"));
$title->modify_fg(Gtk::STATE_NORMAL, GdkColor::parse("#0000ff"));
$title->set_size_request(-1, 60);
$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();
Gtk::main();

// process toolbar
  • 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. Check if insert key is pressed.
  3. Check if there's any text in the clipboard.
  4. Get the text from the clipboard.
  5. Remove all the html tags.
  6. Insert the text at the cursor position.

Note

Note that you can still press Ctrl-V to paste the text. The difference is that Ctrl-V will paste the tags as it is, i.e. with all the html tags. However, if you use the Insert key, the html tags will be removed.


Related Links

User reviews   Average user ratings:    5.0   (from 1 user)
  1. Daniel Lucraft
    April 02, 2008 9:22am

Note: You have to be a registered member to leave a comment. Free registration here.

 
< Prev   Next >

Blog - Forum - Privacy Policy - Contact Us
Copyright © 2006-2008. kksou.com. All Rights Reserved