-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
197 lines (145 loc) · 6.9 KB
/
MainWindow.xaml.cs
File metadata and controls
197 lines (145 loc) · 6.9 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Win32;
using System.IO;
namespace LAB1SEM2_NOTEBOOK
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
fntFamily.ItemsSource = Fonts.SystemFontFamilies.OrderBy(f => f.Source);
fntSize.ItemsSource = new List<double>() { 8, 9, 10, 11, 12, 14, 16, 18, 20, 22, 24, 26, 28, 36, 48, 72 };
appTheme.SelectedItem = thmLight;
txtEditor.Focus();
}
private void CommandBinding_Executed_New(object sender, RoutedEventArgs e)
{
txtEditor.SelectAll();
txtEditor.Selection.Text = "";
}
private void CommandBinding_Executed_Open(object sender, ExecutedRoutedEventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "Text files (*.txt)| *.txt";
if(openFileDialog.ShowDialog() == true)
{
txtEditor.SelectAll();
txtEditor.Selection.Text = "";
txtEditor.AppendText(File.ReadAllText(openFileDialog.FileName));
}
}
private void CommandBinding_Executed_Save(object sender, ExecutedRoutedEventArgs e)
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "Text file (*.txt)|*.txt";
if (saveFileDialog.ShowDialog() == true)
{
File.WriteAllText(saveFileDialog.FileName, GetText(txtEditor));
}
}
private void CommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e) => e.CanExecute = true;
private void CommandBinding_CanExecute_1(object sender, CanExecuteRoutedEventArgs e) => e.CanExecute = true;
private void CommandBinding_CanExecute_2(object sender, CanExecuteRoutedEventArgs e) => e.CanExecute = true;
public void SetFontFamily(RichTextBox target, FontFamily value)
{
TextRange selectionTextRange = new TextRange(target.Selection.Start, target.Selection.End);
selectionTextRange.ApplyPropertyValue(TextElement.FontFamilyProperty, value);
if (fntSize.SelectedItem != null) {
double tempsize = (double)fntSize.SelectedItem;
selectionTextRange.ApplyPropertyValue(TextElement.FontSizeProperty, tempsize);
}
target.Focus();
}
public static string GetText(RichTextBox rtb) //Get text
{
TextRange textRange = new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd);
string text = textRange.Text;
return text;
}
private void txtEditor_SelectionChanged(object sender, RoutedEventArgs e)
{
object temp = txtEditor.Selection.GetPropertyValue(Inline.FontWeightProperty);
btnBold.IsChecked = (temp != DependencyProperty.UnsetValue) && (temp.Equals(FontWeights.Bold));
temp = txtEditor.Selection.GetPropertyValue(Inline.FontStyleProperty);
btnItalic.IsChecked = (temp != DependencyProperty.UnsetValue) && (temp.Equals(FontStyles.Italic));
temp = txtEditor.Selection.GetPropertyValue(Inline.TextDecorationsProperty);
btnUnderline.IsChecked = (temp != DependencyProperty.UnsetValue) && (temp.Equals(TextDecorations.Underline));
temp = txtEditor.Selection.GetPropertyValue(Inline.FontFamilyProperty);
fntFamily.SelectedItem = temp;
temp = txtEditor.Selection.GetPropertyValue(Inline.FontSizeProperty);
fntSize.Text = temp.ToString();
}
private void cmbFontFamily_SelectionChanged(object sender, SelectionChangedEventArgs e) { //=> SetFontFamily(txtEditor, (FontFamily)fntFamily.SelectedItem);
if(fntFamily.SelectedItem != null )
{
txtEditor.Selection.ApplyPropertyValue(Inline.FontFamilyProperty, fntFamily.SelectedItem);
}
else if (fntFamily.SelectedItem != null & txtEditor.Selection.IsEmpty)
{
TextRange selectionTextRange = new TextRange(txtEditor.Selection.Start, txtEditor.Selection.End);
selectionTextRange.ApplyPropertyValue(TextElement.FontFamilyProperty, fntFamily.SelectedItem);
}
txtEditor.Focus();
}
private void cmbFontSize_TextChanged(object sender, TextChangedEventArgs e) { //=> SetFontSize(txtEditor, (double)fntSize.SelectedItem);
try
{
if (fntSize.SelectedItem != null)
{
txtEditor.Selection.ApplyPropertyValue(Inline.FontSizeProperty, fntSize.Text);
}
else if (fntSize.SelectedItem != null & txtEditor.Selection.IsEmpty)
{
TextRange selectionTextRange = new TextRange(txtEditor.Selection.Start, txtEditor.Selection.End);
selectionTextRange.ApplyPropertyValue(TextElement.FontSizeProperty, fntSize.SelectedItem);
}
else if (fntSize.SelectedItem == null)
{
double curText = Convert.ToDouble(fntSize.Text);
if (curText >= 1)
txtEditor.Selection.ApplyPropertyValue(Inline.FontSizeProperty, fntSize.Text);
}
}
catch (Exception ex) {
}
}
private void mnInfo_Click(object sender, RoutedEventArgs e)
{
var infodlg = new Window1();
infodlg.ShowDialog();
}
private void appTheme_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (appTheme.SelectedItem == thmLight)
{
txtEditor.Background = new SolidColorBrush(Colors.White);
txtEditor.Foreground = new SolidColorBrush(Colors.Black);
menu.Background = new SolidColorBrush(Colors.LightGray);
}
else if (appTheme.SelectedItem == thmDark)
{
txtEditor.Background = new SolidColorBrush(Colors.Black);
txtEditor.Foreground = new SolidColorBrush(Colors.WhiteSmoke);
menu.Background = new SolidColorBrush(Colors.SlateGray);
}
}
}
}