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

Calling each respective function - 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.

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

When the user selects a menu item, the callback function on_menu_select() is called. By retrieving the label of the menu item, we know which menu item the user has selected:

$item = $menu_item->child->get_label();

We then change the menu item label to lower case and remove any '_' and '.'. The resulting string is the function name. 

$item2 = strtolower($item);

$item2 = preg_replace(array('/_/','/./','#/#','/s/ '), array('','','_','_'), $item2);

if (preg_match('/^(new|open|save)$/', $item2, $matches)) $item2='file_'.$matches[1];

$this->$item2();

For example, when the user selects "Edit - Copy," the copy() function will be called. When the user selects "Format - Font...," the font() function will be called. Below is the entire callback function on_menu_select():

public function on_menu_select($menu_item) {

$item = $menu_item->child->get_label();

$item2 = strtolower($item);

$item2 = preg_replace(array('/_/','/./','#/#','/s/ '), array('','','_','_'), $item2);

if (preg_match('/^(new|open|save)$/', $item2, $matches)) $item2='file_'.$matches[1];

echo "menu selected: $item ($item2)n";

$this->$item2();

if ($item=='_Quit') Gtk::main_quit();

}

Setting up the Text Buffer

The Glade file allows you to specify most of the UI elements. But not all. For example, in this application, you need to create the text buffer in your PHP program and attach it to the text view as shown below. You cannot do this in the Glade file.

$this->buffer = new GtkTextBuffer();

$this->view = $glade->get_widget('textview1');

$this->view->set_buffer($this->buffer);

File - New

When the user selects File - New, we clear the buffer, reset the modified flag, clear the filename and update the title of the window.


protected function file_new() {

$this->buffer->set_text('');

$this->buffer->set_modified(false);

$this->filename = '';

$this->set_title();

}

File - Open

Opening a file is a breeze using the GtkFileChooserDialog widget.Once the user selects the file from the pop-up file chooser, we obtain the selected file with:

$dialog->get_filename();

We read in the entire contents and write them to the text buffer.

$contents = file_get_contents($this->filename);

$this->buffer->set_text($contents);

We then reset the modified flag and update the window's title to show the filename.

$this->buffer->set_modified(false);

$this->set_title();

The entire code to process File - Open is as follows:

protected function file_open() {

$dialog = new GtkFileChooserDialog("File Open", null, Gtk::FILE_CHOOSER_ACTION_OPEN, array(Gtk::STOCK_OK, Gtk::RESPONSE_OK), null);

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

$this->filename = $dialog->get_filename();

echo "selected_file = $this->filenamen";

$contents = file_get_contents($this->filename);

$this->buffer->set_text($contents);

$this->buffer->set_modified(false);

$this->set_title();

}

$dialog->destroy();

}

File - Save As

The code for processing File - Save As is very similar to that for File - Open. We use the GtkFileChooserDialog widget again to prompt the user for the location and filename of the new file. With the complete path of the filename, we are now ready to save the contents of the text buffer to the file.

protected function save_as() {

$dialog = new GtkFileChooserDialog("File Save", null, Gtk::FILE_CHOOSER_ACTION_SAVE, array(Gtk::STOCK_OK, Gtk::RESPONSE_OK), null);

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

$this->filename = $dialog->get_filename();

$this->save_buffer();

}

$dialog->destroy();

}

To save the text buffer, we first need to retrieve the contents of the entire text buffer:

$buffer_str = $this->buffer->get_text($this->buffer->get_start_iter(),

$this->buffer->get_end_iter());

We then write the content to the file using file_put_contents().

file_put_contents($this->filename, $buffer_str);

Finally, we reset the modified flag and update the window's title to reflect the new filename.

$this->buffer->set_modified(false);

$this->set_title();

The entire code for saving the buffer is as follows:

protected function save_buffer() {

$buffer_str = $this->get_buffer_str();

file_put_contents($this->filename, $buffer_str);

$this->buffer->set_modified(false);

$this->set_title();

}

File - Save

When the user selects File - Save, we first need to test if the user has previously specified a filename. If no filename exists, internally we process this as a File - Save As.

protected function file_save() {

if ($this->filename!='') $this->save_buffer();

else $this->save_as();

}

If a filename already exists, we simplify called the save_buffer() function described earlier to write the entire buffer contents to the file.

File - Quit

Exiting an application in PHP-GTK is as simple as calling the Gtk::main_quit()function. Since it's so simple, we just place it right inside the menu item signal handler on_menu_select():

if ($item=='_Quit') Gtk::main_quit();



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- 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: