Home arrow PHP arrow Page 4 - Building Your Own Desktop Notepad Application Using PHP-GTK

Edit Operations - PHP

If you think building a desktop application such as Notepad is difficult, think again. If you use PHP-GTK, it's almost ridiculously easy. This article will show you how. By the time you're done, you'll have a full cross-platform application in about 100 lines.

  1. Building Your Own Desktop Notepad Application Using PHP-GTK
  2. Assumptions
  3. Calling each respective function
  4. Edit Operations
  5. About the modified flag
By: K.K.Sou
Rating: starstarstarstarstar / 9
September 23, 2008

print this article



Thanks to the rich set of the GTK+ library, the edit operations - namely cut, copy, paste and delete - each require only one line to do their jobs!

protected function cut() {

$this->buffer->cut_clipboard($this->clipboard, $this->view->get_editable());


protected function copy() {



protected function paste() {

$this->buffer->paste_clipboard($this->clipboard, null, true);


protected function delete() {

$this->buffer->delete_selection(true, $this->view->get_editable());



When the user selects Edit Find, or press Ctrl-F, we first pop up a Find dialog box to ask for the search string.

Note: The pop-up Find dialog is also defined in the Glade file.

When the user presses the "Find Next" button, it will call the on_search_button_clicked() callback function, which simply returns a response code of 101.

public function on_search_button_clicked($button=null) {



Yes, in case you're wondering, this number 101 is arbitrary. You could have used any other integer number.

For the convenience of the user, we should also allow the user to type in the search string and press <Enter>, without the need to click on the "Find Next" button. This is done by registering the "activate" signal on the search field. This signal will be emitted when the user presses <Enter> in the search field. All we need to do in the callback function is simulate a click on the "Find Next" button:

public function on_search_entry_activate($entry) {



When the dialog returns a response code of 101, we retrieve the search string

$this->search_str = $this->glade->get_widget('search_entry')->get_text();

and perform the actual search. Searching is essentially done by the search_range() function. You need to tell PHP-GTK where to start the search (indicated by the start iter) and the search string. If there is any match, it will store the match position in two variables, $match_start and $match_end.

$found = $start_iter->forward_search($str, 0, $match_start, $match_end, null);

If there is a match, we highlight the matched string and store this position as "last_search_pos." We do this so that if the user selects Edit - Find Next or presses F3, we will start searching from this "last_search_pos."

if ($found) {

$buffer->select_range($match_start, $match_end);

$buffer->create_mark('last_search_pos', $match_end, false);


Note that we also do an automatic wrapping for search. This means that it will first search from the current insert position to the end of the document. If it doesn't find anything, it will perform a second search from the start of the document to the current insert position.

$found = $this->search_range($str, $current_insert_pos, $buffer->get_end_iter());

if (!$found) $this->search_range($str, $buffer->get_start_iter(), $current_insert_pos);

Edit - Select All

This is a one liner. All we need to do is get the start of document and the end of document, and highlight everything in between.

protected function select_all() {

$this->buffer->select_range($this->buffer->get_start_iter(), $this->buffer->get_end_iter());


Edit - Time/Date

This is also a one liner. We simply insert the current date and time at the current cursor location.

protected function time_date() {

$this->buffer->insert_at_cursor(date('h:i A n/j/Y'));


Format - Word Wrap

This is the only menu item that uses GtkCheckMenuItem. When it is selected, it will call the on_wordwrap_toggled() callback function. If the menu item is checked, we turn on word wrap. If it is unchecked, we turn word wrap off.

public function on_wordwrap_toggled($menuitem) {

$active = $menuitem->get_active();

if ($menuitem->get_active()) $this->view->set_wrap_mode(Gtk::WRAP_WORD);

else $this->view->set_wrap_mode(Gtk::WRAP_NONE);


Format - Font

This is very similar to File - Open. We use GtkFontSelectionDialog for the font selection, retrieve the selected font and then set the font for the GtkTextView.

protected function font() {

$dialog = new GtkFontSelectionDialog('Select Font');

if ($dialog->run() == Gtk::RESPONSE_OK) {

$fontname = $dialog->get_font_name();

$this->view->modify_font(new PangoFontDescription($fontname));




Help - About

Setting up a dialog box is pretty straightforward using GtkAboutDialog.

protected function about() {

$dialog = new GtkAboutDialog();

$dialog->set_name('PHP-GTK2 Notepad');


$dialog->set_comments("A desktop Notepad application using PHP-GTK2nwritten by kksounnJune 16, 2008");




>>> More PHP Articles          >>> More By K.K.Sou

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: