The modified flag works as follows. First you need to register for the 'modified-changed' signal on the text buffer. $this->buffer->connect('modified-changed', array(&$this, 'on_modified_changed')); When you start a new document or right after saving a document, you reset the modified flag with: $this->buffer->set_modified(false); Then any time the text buffer is changed, the on_modified_changed() callback function will be called. In this example, all we did is update the title to add '(*)' behind the filename. public function on_modified_changed($buffer) { $this->set_title(true); } function set_title($modified = false) { $filename = $this->filename; if ($filename=='') $filename = 'Untitled'; if ($modified) $title = $filename.' (*) - php-gtk2 Notepad'; else $title = $filename.' - php-gtk2 Notepad'; $this->glade->get_widget('window1')->set_title($title); } The complete sample code and glade file Click on the following links to download the complete sample code and the corresponding glade file. Download link: Notepad App Source Final Words We've come to the end of this article. In this tutorial, you have seen how to build your own desktop notepad application using PHP-GTK, in just 100+ lines of code! Of course, there are some functionalities that have not been implemented yet, such as Undo, Redo, Replace and File Print. There is also room for improvement in some of the functionalities. For example, when no text is selected, the menu items Cut, Copy and Delete should be grayed out. When the user has modified an opened document, and then selects File New or File Open, the program should have a prompt that says "The text in the xxx.txt file has changed. Do you want to save the changes?" You might want to try implementing these as an exercise. If there are enough requests, I will post a sample solution as a follow-up to this article. Lastly, in case you are not too familiar with some of the Gtk widgets such as GtkMenuItems and GtkDialog, you might want to refer to the site kksou.com which contains sample codes with explanations for each individual widget. If you are new to PHP-GTK, you might also want to read the free online ebook "PHP-GTK2 Demysitified."
blog comments powered by Disqus |
|
|
|
|
|
|
|