PHP
  Home arrow PHP arrow Page 4 - 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? 
Google.com  
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 - Edit Operations
    ( Page 4 of 5 )

    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() {

    $this->buffer->copy_clipboard($this->clipboard);

    }


    protected function paste() {

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

    }


    protected function delete() {

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

    }

    Find

    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) {

    $this->glade->get_widget('find_dialog')->response(101);

    }

    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) {

    $this->glade->get_widget('search_button')->clicked();

    }

    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));

    }

    $dialog->destroy();

    }

    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_version('1.0');

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

    $dialog->run();

    $dialog->destroy();

    }



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

       

    PHP ARTICLES

    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - 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 ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek