~pkgcrosswire/xiphos/main

« back to all changes in this revision

Viewing changes to editkit/editkit.vala

  • Committer: Dmitrijs Ledkovs
  • Date: 2010-11-14 00:38:52 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: dmitrij.ledkov@ubuntu.com-20101114003852-sjt227lz4qqi85xj
New upstream release 3.1.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
using Gtk;
 
2
using WebKit;
 
3
 
 
4
        public void help () {
 
5
        }
 
6
 
 
7
 
 
8
private class EditKit : Window {
 
9
 
 
10
        private const string MAIN_UI  = "main.ui";
 
11
    private WebView web_view;
 
12
 
 
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}
 
41
        };
 
42
 
 
43
    private EditKit () {
 
44
        this.title = title;
 
45
        resize (800, 600);
 
46
        create_widgets ();
 
47
        connect_signals ();
 
48
    }
 
49
 
 
50
        private void create_widgets () {
 
51
 
 
52
                var actions = new ActionGroup("Actions");
 
53
                actions.set_translation_domain("xiphos");
 
54
                actions.add_actions(entries, this);
 
55
 
 
56
                actions.get_action("insertimage").set_property("icon-name", "insert-image");
 
57
                actions.get_action("insertlink").set_property("icon-name", "insert-link");
 
58
 
 
59
                var ui = new UIManager();
 
60
                ui.insert_action_group(actions, 0);
 
61
 
 
62
                try {
 
63
                        ui.add_ui_from_file(MAIN_UI);
 
64
                } catch (Error e) {
 
65
                        stdout.printf("Error: %s\n", e.message);
 
66
                }
 
67
 
 
68
                this.add_accel_group(ui.get_accel_group());
 
69
 
 
70
                var toolbar1 = ui.get_widget("/toolbar_main");
 
71
                var toolbar2 = ui.get_widget("/toolbar_format");
 
72
                var menubar = ui.get_widget("/menubar_main");
 
73
 
 
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);
 
85
                add (vbox);
 
86
 
 
87
        }
 
88
 
 
89
    private void connect_signals () {
 
90
        this.destroy.connect (Gtk.main_quit);
 
91
    }
 
92
 
 
93
        private void on_action(Action action) {
 
94
                this.web_view.execute_script("document.execCommand('" + action.get_name() + "', false, false);");
 
95
        }
 
96
 
 
97
        private void on_open() {
 
98
 
 
99
                string filename;
 
100
                string content;
 
101
 
 
102
                FileChooserDialog dialog = new FileChooserDialog(
 
103
                        "Select an HTML file",
 
104
                        this,
 
105
                        Gtk.FileChooserAction.OPEN,
 
106
                        Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
 
107
                        Gtk.STOCK_OPEN, Gtk.ResponseType.ACCEPT,
 
108
                        null);
 
109
 
 
110
                if (dialog.run() == Gtk.ResponseType.ACCEPT) {
 
111
                        filename = dialog.get_filename();
 
112
                        try {
 
113
                                FileUtils.get_contents(filename, out content);
 
114
                        } catch (FileError e) {
 
115
                                stdout.printf("Error: %s\n", e.message);
 
116
                        }
 
117
                                this.web_view.load_html_string(content, "file:///");
 
118
                }
 
119
 
 
120
                dialog.destroy();
 
121
        }
 
122
 
 
123
        private void on_new() {
 
124
                this.web_view.load_html_string("", "file:///");
 
125
        }
 
126
 
 
127
        private void on_save() {
 
128
 
 
129
                string filename;
 
130
                string content;
 
131
 
 
132
                FileChooserDialog dialog = new FileChooserDialog(
 
133
                        "Select an HTML file",
 
134
                        this,
 
135
                        Gtk.FileChooserAction.SAVE,
 
136
                        Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
 
137
                        Gtk.STOCK_OPEN, Gtk.ResponseType.ACCEPT,
 
138
                        null);
 
139
 
 
140
                dialog.set_do_overwrite_confirmation(true);
 
141
 
 
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();
 
146
                        try {
 
147
                                FileUtils.set_contents(filename, content);
 
148
                        } catch (FileError e) {
 
149
                                stdout.printf("Error: %s\n", e.message);
 
150
                        }
 
151
                }
 
152
 
 
153
                dialog.destroy();
 
154
        }
 
155
 
 
156
        private void on_paste() {
 
157
                this.web_view.paste_clipboard();
 
158
        }
 
159
 
 
160
        private void on_select_font() {
 
161
 
 
162
        }
 
163
 
 
164
        private void on_select_color() {
 
165
 
 
166
                Gdk.Color color;
 
167
                ColorSelectionDialog dialog = new ColorSelectionDialog("Select Color");
 
168
 
 
169
                if (dialog.run() == Gtk.ResponseType.OK) {
 
170
                        var colsel = (ColorSelection) dialog.get_color_selection();
 
171
                        colsel.get_current_color(out color);
 
172
 
 
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) + "');");
 
178
                }
 
179
 
 
180
                dialog.destroy();
 
181
        }
 
182
 
 
183
        private void on_insert_image() {
 
184
 
 
185
        }
 
186
 
 
187
        private void on_insert_link() {
 
188
 
 
189
        }
 
190
 
 
191
    private static int main (string[] args) {
 
192
 
 
193
        Gtk.init (ref args);
 
194
        var editor = new EditKit ();
 
195
                editor.show_all();
 
196
        Gtk.main ();
 
197
        return 0;
 
198
    }
 
199
}