8
private class EditKit : Window {
10
private const string MAIN_UI = "main.ui";
11
private WebView web_view;
13
private const ActionEntry[] entries = {
14
{"menuFile", null, "_File"},
15
{"new", STOCK_NEW, "_New", null, null, on_new},
16
{"open", STOCK_OPEN, "_Open", null, null, on_open},
17
{"save", STOCK_SAVE, "_Save", null, null, on_save},
18
{"print", STOCK_PRINT, "_Print", null, null, on_action},
19
{"menuEdit", null, "_Edit"},
20
{"undo", STOCK_UNDO, "_Undo", null, null, on_action},
21
{"redo", STOCK_REDO, "_Redo", null, null, on_action},
22
{"cut", STOCK_CUT, "_Cut", null, null, on_action},
23
{"copy", STOCK_COPY, "_Copy", null, null, on_action},
24
{"paste", STOCK_PASTE, "_Paste", null, null, on_paste},
25
{"menuInsert", null, "_Insert"},
26
{"insertimage", "insert-image", "Insert _Image", null, null, on_insert_image},
27
{"insertlink", "insert-link", "Insert _Link", null, null, on_insert_link},
28
{"menuFormat", null, "_Format"},
29
{"bold", STOCK_BOLD, "_Bold", """<ctrl>B""", null, on_action},
30
{"italic", STOCK_ITALIC, "_Italic", """<ctrl>I""", null, on_action},
31
{"underline", STOCK_UNDERLINE, "_Underline", """<ctrl>U""", null, on_action},
32
{"strikethrough", STOCK_STRIKETHROUGH, "_Strike", """<ctrl>T""", null, on_action},
33
{"font", STOCK_SELECT_FONT, "Select _Font", """<ctrl>F""", null, on_select_font},
34
{"color", STOCK_SELECT_COLOR, "Select _Color", null, null, on_select_color},
35
{"justifyleft", STOCK_JUSTIFY_LEFT, "Justify _Left", null, null, on_action},
36
{"justifyright", STOCK_JUSTIFY_RIGHT, "Justify _Right", null, null, on_action},
37
{"justifycenter", STOCK_JUSTIFY_CENTER, "Justify _Center", null, null, on_action},
38
{"justifyfull", STOCK_JUSTIFY_FILL, "Justify _Full", null, null, on_action},
39
{"indent", STOCK_INDENT, "_Icrease Indent", "<Control>bracketright", "Increase Indent", on_action},
40
{"outdent", STOCK_UNINDENT, "_Decrease Indent", "<Control>bracketleft", "Decrease Indent", on_action}
50
private void create_widgets () {
52
var actions = new ActionGroup("Actions");
53
actions.set_translation_domain("xiphos");
54
actions.add_actions(entries, this);
56
actions.get_action("insertimage").set_property("icon-name", "insert-image");
57
actions.get_action("insertlink").set_property("icon-name", "insert-link");
59
var ui = new UIManager();
60
ui.insert_action_group(actions, 0);
63
ui.add_ui_from_file(MAIN_UI);
65
stdout.printf("Error: %s\n", e.message);
68
this.add_accel_group(ui.get_accel_group());
70
var toolbar1 = ui.get_widget("/toolbar_main");
71
var toolbar2 = ui.get_widget("/toolbar_format");
72
var menubar = ui.get_widget("/menubar_main");
74
this.web_view = new WebView ();
75
web_view.set_editable(true);
76
web_view.load_html_string("", "file:///");
77
var scrolled_window = new ScrolledWindow (null, null);
78
scrolled_window.set_policy (PolicyType.AUTOMATIC, PolicyType.AUTOMATIC);
79
scrolled_window.add (this.web_view);
80
var vbox = new VBox (false, 0);
81
vbox.pack_start (menubar, false, true, 0);
82
vbox.pack_start (toolbar1, false, true, 0);
83
vbox.pack_start (toolbar2, false, true, 0);
84
vbox.pack_start (scrolled_window, true, true, 0);
89
private void connect_signals () {
90
this.destroy.connect (Gtk.main_quit);
93
private void on_action(Action action) {
94
this.web_view.execute_script("document.execCommand('" + action.get_name() + "', false, false);");
97
private void on_open() {
102
FileChooserDialog dialog = new FileChooserDialog(
103
"Select an HTML file",
105
Gtk.FileChooserAction.OPEN,
106
Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
107
Gtk.STOCK_OPEN, Gtk.ResponseType.ACCEPT,
110
if (dialog.run() == Gtk.ResponseType.ACCEPT) {
111
filename = dialog.get_filename();
113
FileUtils.get_contents(filename, out content);
114
} catch (FileError e) {
115
stdout.printf("Error: %s\n", e.message);
117
this.web_view.load_html_string(content, "file:///");
123
private void on_new() {
124
this.web_view.load_html_string("", "file:///");
127
private void on_save() {
132
FileChooserDialog dialog = new FileChooserDialog(
133
"Select an HTML file",
135
Gtk.FileChooserAction.SAVE,
136
Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
137
Gtk.STOCK_OPEN, Gtk.ResponseType.ACCEPT,
140
dialog.set_do_overwrite_confirmation(true);
142
if (dialog.run() == Gtk.ResponseType.ACCEPT) {
143
filename = dialog.get_filename();
144
this.web_view.execute_script ("document.title=document.documentElement.innerHTML;");
145
content = this.web_view.get_main_frame().get_title();
147
FileUtils.set_contents(filename, content);
148
} catch (FileError e) {
149
stdout.printf("Error: %s\n", e.message);
156
private void on_paste() {
157
this.web_view.paste_clipboard();
160
private void on_select_font() {
164
private void on_select_color() {
167
ColorSelectionDialog dialog = new ColorSelectionDialog("Select Color");
169
if (dialog.run() == Gtk.ResponseType.OK) {
170
var colsel = (ColorSelection) dialog.get_color_selection();
171
colsel.get_current_color(out color);
173
this.web_view.execute_script (
174
"document.execCommand('forecolor', null, '" +
175
color.to_string().substring(0,3) +
176
color.to_string().substring(5,2) +
177
color.to_string().substring(9,2) + "');");
183
private void on_insert_image() {
187
private void on_insert_link() {
191
private static int main (string[] args) {
194
var editor = new EditKit ();