PHP
  Home arrow PHP arrow Page 3 - Building Your Own Desktop Notepad Application Using PHP-GTK
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Building Your Own Desktop Notepad Application Using PHP-GTK
By: K.K.Sou
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 6
    2008-09-23


    Table of Contents:
  • Building Your Own Desktop Notepad Application Using PHP-GTK
  • Assumptions
  • Calling each respective function
  • Edit Operations
  • About the modified flag

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Building Your Own Desktop Notepad Application Using PHP-GTK - Calling each respective function
    ( Page 3 of 5 )

    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
     

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    Stay green...Green IT