aboutsummaryrefslogtreecommitdiff
path: root/ShiftOS.WinForms/Applications/TriWrite.cs
blob: 291aa307780b74838551ed7dad50ab00cd938f69 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
using ShiftOS.Objects.ShiftFS;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ShiftOS.Engine;

namespace ShiftOS.WinForms.Applications
{
    [WinOpen("triwrite")]
    [AppscapeEntry("TriWrite", "Part of the trilogy of office applications for enhancement of your system. TriWrite is easliy the best text editor out there for ShiftOS.", 1024, 750, "file_skimmer;textpad", "Office")]
    [DefaultTitle("TriWrite")]
    [Launcher("TriWrite", false, null, "Office")]
    public partial class TriWrite : UserControl, IShiftOSWindow
    {

        public TriWrite()
        {
            InitializeComponent(); //From the library of babel: "FIRST COMMIT FROM A MAC WOOOO TURIANS ARE COOL"
        }

        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            txtcontents.Text = "";
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var txt = new List<string>();
            txt.Add(".rtf");

            FileSkimmerBackend.GetFile(txt.ToArray(), FileOpenerStyle.Open, (path) => LoadFile(path));
        }

        public void LoadFile(string file)
        {
            txtcontents.Rtf = Utils.ReadAllText(file);
        }

        public void SaveFile(string file)
        {
            if (file.ToLower().EndsWith(".rtf"))
            {
                Utils.WriteAllText(file, txtcontents.Rtf);
            }
            else
            {
                Utils.WriteAllText(file, txtcontents.Text);
            }
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var txt = new List<string>();
            txt.Add(".rtf");
            txt.Add(".txt");
            FileSkimmerBackend.GetFile(txt.ToArray(), FileOpenerStyle.Save, (path) => SaveFile(path));
        }

        public void OnLoad()
        {
            UpdateUI();
        }

        public void OnSkinLoad()
        {
            ResetFonts();
        }

        public void ResetFonts()
        {
            fonts.Items.Clear();
            foreach(var font in FontFamily.Families)
            {
                fonts.Items.Add(font.Name);
            }
            UpdateUI();
        }

        public bool OnUnload()
        {
            return true;
        }

        public void OnUpgrade()
        {
        }

        public void UpdateUI()
        {
            bold.Checked = txtcontents.SelectionFont.Bold;
            italic.Checked = txtcontents.SelectionFont.Italic;
            underline.Checked = txtcontents.SelectionFont.Underline;
            strikethrough.Checked = txtcontents.SelectionFont.Strikeout;
            fonts.Text = txtcontents.SelectionFont.Name;
            size.Text = txtcontents.SelectionFont.Size.ToString();
            left.Checked = txtcontents.SelectionAlignment == HorizontalAlignment.Left;
            center.Checked = txtcontents.SelectionAlignment == HorizontalAlignment.Center;
            right.Checked = txtcontents.SelectionAlignment == HorizontalAlignment.Right;

        }

        private void txtcontents_SelectionChanged(object sender, EventArgs e)
        {
            UpdateUI();
        }

        public void SetFontStyle(bool bold, bool italic, bool underline, bool strikethrough)
        {
            FontStyle fs = FontStyle.Regular;
            if (bold)
                fs |= FontStyle.Bold;
            if (italic)
                fs |= FontStyle.Italic;
            if (underline)
                fs |= FontStyle.Underline;
            if (strikethrough)
                fs |= FontStyle.Strikeout;
            txtcontents.SelectionFont = new Font(txtcontents.SelectionFont, fs);
            UpdateUI();
        }

        private void bold_CheckedChanged(object sender, EventArgs e)
        {
            SetFontStyle(bold.Checked, italic.Checked, underline.Checked, strikethrough.Checked);
        }

        public void SetFontFamily(string family, float emSize)
        {
            var style = txtcontents.SelectionFont.Style;
            var size = emSize;
            txtcontents.SelectionFont = new Font(family, size, style);
            UpdateUI();
        }

        private void fonts_SelectedIndexChanged(object sender, EventArgs e)
        {
            SetFontFamily(fonts.Text, Convert.ToSingle(size.Text));
        }

        private void left_Click(object sender, EventArgs e)
        {
            txtcontents.SelectionAlignment = HorizontalAlignment.Left;
            UpdateUI();
        }

        private void center_Click(object sender, EventArgs e)
        {
            txtcontents.SelectionAlignment = HorizontalAlignment.Center;
            UpdateUI();

        }

        private void right_Click(object sender, EventArgs e)
        {
            txtcontents.SelectionAlignment = HorizontalAlignment.Right;
            UpdateUI();

        }

        private void size_Click(object sender, EventArgs e)
        {
            try
            {
                float s = Convert.ToSingle(size.Text);
                if(s != txtcontents.SelectionFont.Size)
                {
                    SetFontFamily(fonts.Text, s);
                }
            }
            catch
            {
                UpdateUI();
            }
        }
    }
}