Here is a quick way to build a PHP editor with syntax
highlighting. This widget is nearly usable as a real php-gtk IDE.
It extends GtkSourceView widget and
internally manages both a text buffer and language
object classes.
Details
To use a GtkSourceView widget, you need to manage :
- a text buffer containing edited data,
- a view : a GtkSourceWiew widget or subclass,
- for syntax highlighting, there is some classes ; in the class
source below, you can see how to use them.
GtkSourceView widgets are extensions of
a GtkTextWidget model. So you can use
regular methods from this framework (see
GtkTextView, GtkTextBuffer
classes).
Supported Features
GtksourceView has builtin features :
- multiple undo (redo?),
- syntaxe highlighting (php, C, C++,bash ...),
- line numbering
- marker management
- brackets highlighting
Source code
here is some details :
- PhpEditWidget is a widget (extending GtkSourceView) ; so you can use it just like a regular widget,
- lets parent widget build what need to be done,
- we want to create a php editor with php syntax highlighting ; just feed php mime type to
GtkSourceLanguages* classes
- text editing with Gtk widget respect MVC partern model (Model, View, Controler);
GtkSourceBuffer is the Model part, GtkSourceView is the View part, the code you are writing is the Controler part. Text editing, changes are done with Buffer part (Model), the View part is responsible for display part (line numbering, syntax highlighting, user interactions), Controler part should be a php class overriding PhpEditWidget widget ; this widget is keeped as simple as possible for this tutorial.
- This is an all-in-one widget ; the MVC part is directly managed by
PhpEditWidget widget ; in simple usage, you can ignore text buffer existance, you will only need to use a ready to use widget. If you need to write more complexe usage, GtkSourceBuffer is still available as a protected attribute for experimentated developpers only
- File loading is made by a conveniant public method
- php source text is appended to text buffer (should be cleared before)
- text code should be protected from "undo" feature
- some IDE offer
window spliting ; this feature is make quickly with this split method; we need to create a new PhpEditor widget and share text buffer between widgets.
<?php
error_reporting(E_ALL);
class PhpEditWidget extends GtkSourceView { // Note 1
protected $buffer; // note 5
protected $lang;
protected $mime_lang;
function __construct() {
parent::__construct(); // note 2
# default lang
$this->mime_lang = 'text/x-php-source'; // note 3
$this->set_lang($this->mime_lang); // note 3
$this->buffer = new GtkSourceBuffer(); // note 4, note 5
$this->set_buffer($this->buffer); // note 4
$this->set_show_line_numbers(true);
$this->set_show_line_markers(true);
$this->buffer->set_language($this->lang);
$this->buffer->set_highlight(true);
}
protected function set_lang($mime_type) { // note 3
$lang_manager = new GtkSourceLanguagesManager();
$this->lang = $lang_manager->get_language_from_mime_type($mime_type);
}
public function load_file($file) { // note 6
if (!file_exists($file))
return false;
$lines = file_get_contents($file);
$this->buffer->begin_not_undoable_action(); // note 8
$this->buffer->set_text($lines); // note 7
$this->buffer->end_not_undoable_action(); // note 8
return true;
}
function split() { // note 9
$edit = new PhpEditWidget();
$edit->set_buffer($this->get_buffer());
return $edit;
}
} // end of class PhpEditWidget
/**
* In this part of the code, indenting matches the
* widgets containment, not the code structure
*/
$win = new GtkWindow();
$vbox = new GtkVPaned();
$win->add($vbox);
$scrolled_win = new GtkScrolledWindow();
$scrolled_win->set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
$vbox->add1($scrolled_win);
$php_edit = new PhpEditWidget();
$php_edit->load_file(__FILE__);
$scrolled_win->add_with_viewport($php_edit); // note 1
# test for a split-window like feature // note 9
$scrolled_win = new GtkScrolledWindow();
$scrolled_win->set_policy(
Gtk::POLICY_AUTOMATIC,
Gtk::POLICY_AUTOMATIC);
$vbox->add2($scrolled_win);
$scrolled_win->add_with_viewport($php_edit->split()); // note 9
$win->set_size_request(400, 600);
$win->maximize();
$win->set_position(Gtk::WIN_POS_CENTER);
$win->show_all();
$win->set_title("PhpEditWidget - demo");
$win->connect_simple("destroy", array("Gtk", "main_quit"));
Gtk::main();
?>
To do
- manage fonts,
- make same widget demo with Scintilla widget
Links
Kommentare
GtkSourceView
GtkSourceViewdoes work on windows there are links to the dll's on gnope.org forum.gtksourceview on windows
gtksourceviewworks on windows - there's a version of gnope that ships with it and it is included in the latest betaHowever, the mime-type listed above is incorrect for newer versions of gtksourceview (the windows beta uses gtksourceview 2.8) - the php language mime type is
application/x-php. Language spec files are just XML, so feel free to open one up in a text editor, the second line will have the mimetype you want