|
@@ -1,56 +1,110 @@
|
|
-// using Gtk;
|
|
|
|
-// using Adw;
|
|
|
|
-// using GtkCommonMark;
|
|
|
|
-
|
|
|
|
-// namespace Publicate.Editors {
|
|
|
|
-
|
|
|
|
-// public class PlainTextEditor : Box, EditorWidget {
|
|
|
|
-
|
|
|
|
-// private ScrolledWindow scrolled_window;
|
|
|
|
-// private ClampScrollable clamp;
|
|
|
|
-// private TextView text_view;
|
|
|
|
-
|
|
|
|
-// private TextTag tag;
|
|
|
|
-
|
|
|
|
-// public PlainTextEditor() {
|
|
|
|
-// orientation = Orientation.VERTICAL;
|
|
|
|
-// scrolled_window = new ScrolledWindow ();
|
|
|
|
-// clamp = new ClampScrollable ();
|
|
|
|
-// clamp.maximum_size = 800;
|
|
|
|
-// scrolled_window.child = clamp;
|
|
|
|
-// scrolled_window.set_policy (PolicyType.NEVER, PolicyType.AUTOMATIC);
|
|
|
|
-// scrolled_window.vexpand = true;
|
|
|
|
-
|
|
|
|
-// text_view = new TextView ();
|
|
|
|
-// tag = text_view.buffer.create_tag (null);
|
|
|
|
-// text_view.monospace = true;
|
|
|
|
-// text_view.set_wrap_mode (WrapMode.WORD_CHAR);
|
|
|
|
-// text_view.top_margin = 18;
|
|
|
|
-// text_view.bottom_margin = 18;
|
|
|
|
-// text_view.left_margin = 18;
|
|
|
|
-// text_view.right_margin = 18;
|
|
|
|
-// clamp.child = text_view;
|
|
|
|
-
|
|
|
|
-// append(scrolled_window);
|
|
|
|
-// }
|
|
|
|
-
|
|
|
|
-// public void set_zoom_percentage (int percent) {
|
|
|
|
-// var scale = (float)percent / 100f;
|
|
|
|
-// tag.size_points = scale * 12;
|
|
|
|
-// }
|
|
|
|
|
|
+using Gtk;
|
|
|
|
+using Adw;
|
|
|
|
+using GtkCommonMark;
|
|
|
|
+
|
|
|
|
+namespace Publicate.Editors {
|
|
|
|
+
|
|
|
|
+ public class PlainTextEditor : Box, EditorWidget, Savable {
|
|
|
|
+
|
|
|
|
+ private Ppub.Publication publication;
|
|
|
|
+ private TextView text_view;
|
|
|
|
+
|
|
|
|
+ private ScrolledWindow scroller;
|
|
|
|
+
|
|
|
|
+ private ViewerWindow window;
|
|
|
|
+ private TabPage page;
|
|
|
|
+
|
|
|
|
+ public Adw.TabPage tab_page { get {
|
|
|
|
+ return page;
|
|
|
|
+ } }
|
|
|
|
+
|
|
|
|
+ public string asset_name { get{
|
|
|
|
+ return asset.name;
|
|
|
|
+ } }
|
|
|
|
+
|
|
|
|
+ private bool unsaved = false;
|
|
|
|
+ public bool has_unsaved_changes { get{
|
|
|
|
+ return unsaved;
|
|
|
|
+ } }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ private Ppub.Asset asset;
|
|
|
|
+
|
|
|
|
+ public PlainTextEditor(ViewerWindow win, TabView tab_view) {
|
|
|
|
+ window = win;
|
|
|
|
+
|
|
|
|
+ orientation = Orientation.VERTICAL;
|
|
|
|
+ scroller = new ScrolledWindow ();
|
|
|
|
+
|
|
|
|
+ text_view = new TextView();
|
|
|
|
+ text_view.monospace = true;
|
|
|
|
+ text_view.hexpand = true;
|
|
|
|
+ text_view.set_wrap_mode (WrapMode.WORD_CHAR);
|
|
|
|
+ text_view.top_margin = 18;
|
|
|
|
+ text_view.bottom_margin = 18;
|
|
|
|
+ text_view.left_margin = 18;
|
|
|
|
+ text_view.right_margin = 18;
|
|
|
|
+ text_view.buffer.changed.connect (text_changed);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ scroller.set_policy (PolicyType.NEVER, PolicyType.AUTOMATIC);
|
|
|
|
+ scroller.vexpand = true;
|
|
|
|
+
|
|
|
|
+ scroller.child = text_view;
|
|
|
|
+
|
|
|
|
+ append(scroller);
|
|
|
|
+
|
|
|
|
+ page = tab_view.add_page (this, null);
|
|
|
|
+ }
|
|
|
|
|
|
-// public async void load_asset (Ppub.Publication publication, Ppub.Asset asset) {
|
|
|
|
-// text_view.buffer.set_text("", 0);
|
|
|
|
-// MemoryOutputStream os = new MemoryOutputStream (null, GLib.realloc, GLib.free);
|
|
|
|
-// yield os.splice_async (publication.read_asset (asset.name), OutputStreamSpliceFlags.CLOSE_SOURCE | OutputStreamSpliceFlags.CLOSE_TARGET);
|
|
|
|
-// var text = os.steal_data ();
|
|
|
|
-// text.length = (int) os.get_data_size ();
|
|
|
|
|
|
+ public async void load_asset (Ppub.Publication publication, Ppub.Asset asset) {
|
|
|
|
+ this.publication = publication;
|
|
|
|
+ this.asset = asset;
|
|
|
|
+
|
|
|
|
+ text_view.buffer.set_text("", 0);
|
|
|
|
+ MemoryOutputStream os = new MemoryOutputStream (null, GLib.realloc, GLib.free);
|
|
|
|
+ yield os.splice_async (publication.read_asset (asset.name), OutputStreamSpliceFlags.CLOSE_SOURCE | OutputStreamSpliceFlags.CLOSE_TARGET);
|
|
|
|
+ var text = os.steal_data ();
|
|
|
|
+ text.length = (int) os.get_data_size ();
|
|
|
|
|
|
-// Gtk.TextIter iter;
|
|
|
|
-// text_view.buffer.get_start_iter (out iter);
|
|
|
|
-// text_view.buffer.insert_with_tags (ref iter, (string)text, text.length, tag);
|
|
|
|
-// }
|
|
|
|
|
|
+ text_view.buffer.set_text ((string)text, text.length);
|
|
|
|
+ set_tab_name(false);
|
|
|
|
+ }
|
|
|
|
|
|
-// }
|
|
|
|
|
|
+ public override async void save_asset(Ppub.Builder builder) {
|
|
|
|
+ var article_data = get_text().to_utf8();
|
|
|
|
+ var article_stream = new MemoryInputStream.from_data((uint8[])article_data);
|
|
|
|
+ var compression_info = new Ppub.CompressionInfo(article_stream);
|
|
|
|
+ article_stream.seek(0, SeekType.SET);
|
|
|
|
|
|
-// }
|
|
|
|
|
|
+ builder.add_asset(asset.name, asset.mimetype, article_stream, Invercargill.empty<string>(), compression_info);
|
|
|
|
+ set_tab_name(false);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private void text_changed() {
|
|
|
|
+ set_tab_name(true);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public string get_text() {
|
|
|
|
+ TextIter start_iter;
|
|
|
|
+ TextIter end_iter;
|
|
|
|
+ text_view.buffer.get_start_iter (out start_iter);
|
|
|
|
+ text_view.buffer.get_end_iter (out end_iter);
|
|
|
|
+
|
|
|
|
+ var text = text_view.buffer.get_text (start_iter, end_iter, true);
|
|
|
|
+ return text;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private void set_tab_name(bool unsaved) {
|
|
|
|
+ if(unsaved) {
|
|
|
|
+ tab_page.title = @"⏺ $(asset.name)";
|
|
|
|
+ }
|
|
|
|
+ else {
|
|
|
|
+ tab_page.title = asset.name;
|
|
|
|
+ }
|
|
|
|
+ this.unsaved = unsaved;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+}
|