-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindowUnit.cpp
373 lines (335 loc) · 12.4 KB
/
MainWindowUnit.cpp
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
// ---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "MainWindowUnit.h"
#include "About.h"
// ---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "SHDocVw_OCX"
#pragma link "frxCtrls"
#pragma resource "*.dfm"
TForm1 *Form1;
// ---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) {
}
// ---------------Показать форму "О программе"---------------------------------------------------
void __fastcall TForm1::AboutActionClick(TObject *Sender) {
AboutBox->ShowModal();
}
// ---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender) {
defaultstyles = new StylesCollection; // создаем коллекцию стандартных и пользовательскиъх стилей
styles = new StylesCollection;
styles->LoadFromFile("styles.dat");
curstyle = new Style;
Style *st;
st = new Style(0, clBlack, "", fsNONE, L"Текущий стиль");
*curstyle = *st;
defaultstyles->AddStyle(st);
delete st;
st = new Style(0, clBlack, "", fsNONE, L"Стандартный");
defaultstyles->AddStyle(st);
delete st;
st = new Style(7, clRed, "", fsBOLD, L"Заголовок");
defaultstyles->AddStyle(st);
delete st;
st = new Style(1, clBlack, "", fsNONE, L"Маленький");
defaultstyles->AddStyle(st);
delete st;
st = new Style(7, clBlack, "", fsNONE, L"Огромный");
defaultstyles->AddStyle(st);
delete st;
st = new Style(0, clBlue, "", fsUNDERLINE, L"Ссылка");
defaultstyles->AddStyle(st);
delete st;
defaultstyles->FillComboBox(cbStyle);
styles->FillComboBox(cbStyle);
cbStyle->ItemIndex = 0;
cbTextFont->ItemIndex = -1;
}
// ---------------------------------------------------------------------------
void __fastcall TForm1::FormDestroy(TObject *Sender) {
// сохранить пользовательские стили при закрытии
styles->SaveToFile("styles.dat");
delete styles;
delete defaultstyles;
delete curstyle;
}
// Выбор имени файла для открытия и окрытие файла-----------------------------------------------
void __fastcall TForm1::acOpenFileExecute(TObject *Sender) {
if (OpenDocumentDialog->Execute()) {
acNewPage->Execute();
TDocumentForm *form = (TDocumentForm*)this->ActiveMDIChild;
if (!form)
return;
form->doc->OpenFile(OpenDocumentDialog->FileName);
form->Caption = ExtractFileName(OpenDocumentDialog->FileName);
}
}
// ---------------------------------------------------------------------------
// Удаление выбранного стиля ---------------------------------------------------
void __fastcall TForm1::acDeleteStyleExecute(TObject *Sender) {
String stylename;
int index = cbStyle->ItemIndex;
stylename = cbStyle->Items->Strings[index];
if (stylename[1] == '@') {
styles->DeleteStyle(stylename);
cbStyle->Clear();
defaultstyles->FillComboBox(cbStyle);
styles->FillComboBox(cbStyle);
cbStyle->ItemIndex = 0;
}
else
ShowMessage(L"Нельзя удалить стандартный стиль");
}
//Создание нового стиля---------------------------------------------------------
void __fastcall TForm1::acSaveUserStyleExecute(TObject *Sender) {
//
String stylename;
stylename = InputBox(L"Выберите имя для нового стиля", L"Имя стиля",
L"Новый стиль");
Style *style = new Style;
*style = *curstyle;
style->setstylename("@" + stylename);
styles->AddStyle(style);
cbStyle->Clear();
defaultstyles->FillComboBox(cbStyle);
styles->FillComboBox(cbStyle);
cbStyle->ItemIndex = cbStyle->Items->Count - 1;
}
//закрыть текущую дочернюю форму------------------------------------------------
void __fastcall TForm1::acCloseExecute(TObject *Sender) {
TDocumentForm *form = (TDocumentForm*)this->ActiveMDIChild;
if (!form)
return;
form->Close();
}
// Выбор имени файла для сохранения и сохранение--------------------------------
void __fastcall TForm1::acSaveFileExecute(TObject *Sender) {
TDocumentForm *form = (TDocumentForm*)this->ActiveMDIChild;
if (!form)
return;
if (form->doc->FileName == "") {
SaveDocumentDialog->FileName = form->Caption;
if (SaveDocumentDialog->Execute()) {
form->doc->FileName = SaveDocumentDialog->FileName;
form->Caption = SaveDocumentDialog->FileName;
}
else
return;
}
form->doc->SaveFile(form->doc->FileName);
}
// Новый документ---------------------------------------------------------------
void __fastcall TForm1::acNewPageExecute(TObject *Sender) {
// TO DO check if document opened
TDocumentForm *form = new TDocumentForm(Application);
if (!form)
return;
form->Caption = "Безымянный " + IntToStr(MDIChildCount);
form->Show();
}
// ---------------------------------------------------------------------------
void __fastcall TForm1::acBoldExecute(TObject *Sender) {
TDocumentForm *form = (TDocumentForm*)this->ActiveMDIChild;
if (!form)
return;
form->browser->Bold();
}
// ---------------------------------------------------------------------------
void __fastcall TForm1::acItalicsExecute(TObject *Sender) {
TDocumentForm *form = (TDocumentForm*)this->ActiveMDIChild;
if (!form)
return;
form->browser->Italic();
}
// ---------------------------------------------------------------------------
void __fastcall TForm1::acUnderlineExecute(TObject *Sender) {
TDocumentForm *form = (TDocumentForm*)this->ActiveMDIChild;
if (!form)
return;
form->browser->UnderLine();
}
// ---------------------------------------------------------------------------
void __fastcall TForm1::acCopyExecute(TObject *Sender) {
TDocumentForm *form = (TDocumentForm*)this->ActiveMDIChild;
if (!form)
return;
if (form->RichEdit1->Focused())
form->RichEdit1->CopyToClipboard();
else
form->browser->Copy();
}
// ---------------------------------------------------------------------------
void __fastcall TForm1::acPasteExecute(TObject *Sender) {
TDocumentForm *form = (TDocumentForm*)this->ActiveMDIChild;
if (!form)
return;
if (form->RichEdit1->Focused())
form->RichEdit1->PasteFromClipboard();
else
form->browser->Paste();
}
// ---------------------------------------------------------------------------
void __fastcall TForm1::acInsertListExecute(TObject *Sender) {
TDocumentForm *form = (TDocumentForm*)this->ActiveMDIChild;
if (!form)
return;
form->browser->InsertList();
}
// ---------------------------------------------------------------------------
void __fastcall TForm1::acInsertHyperlinkExecute(TObject *Sender) {
TDocumentForm *form = (TDocumentForm*)this->ActiveMDIChild;
if (!form)
return;
form->browser->HyperLink();
}
// Нажатие кнопки сохранить как ...---------------------------------------------
void __fastcall TForm1::acSaveFileAsExecute(TObject *Sender) {
TDocumentForm *form = (TDocumentForm*)this->ActiveMDIChild;
if (!form)
return;
SaveDocumentDialog->FileName = ExtractFileName(form->doc->FileName);
SaveDocumentDialog->InitialDir = ExtractFileDir(form->doc->FileName);
if (SaveDocumentDialog->Execute()) {
form->doc->FileName = SaveDocumentDialog->FileName;
form->Caption = SaveDocumentDialog->FileName;
}
else
return;
form->doc->SaveFile(form->doc->FileName);
}
// ---------------------------------------------------------------------------
void __fastcall TForm1::acCutExecute(TObject *Sender) {
TDocumentForm *form = (TDocumentForm*)this->ActiveMDIChild;
if (!form)
return;
if (form->RichEdit1->Focused())
form->RichEdit1->CutToClipboard();
else
form->browser->Cut();
}
// ---------------------------------------------------------------------------
void __fastcall TForm1::acInsertImageExecute(TObject *Sender) {
TDocumentForm *form = (TDocumentForm*)this->ActiveMDIChild;
if (!form)
return;
form->browser->InsertImage();
}
//Переодически вызывается раз в секунду для определения изменений в webbrowser ---------------------------------------------------------------------------
void __fastcall TForm1::tmUpdaterTimer(TObject *Sender) {
TDocumentForm *form = (TDocumentForm*)this->ActiveMDIChild;
if (!form) {
return;
}
if (!form->RichEdit1->Focused()) {
form->RichEdit1->Text = form->browser->GetText();
bool is_txt = form->browser->TxtRange();
acBold->Enabled = is_txt;
acItalics->Enabled = is_txt;
acUnderline->Enabled = is_txt;
acInsertHyperlink->Enabled = is_txt;
acInsertImage->Enabled = is_txt;
acInsertList->Enabled = is_txt;
acUndo->Enabled = form->browser->CanUndo();
acRedo->Enabled = form->browser->CanRedo();
acCut->Enabled = form->browser->CanCut();
acCopy->Enabled = form->browser->CanCopy();
acPaste->Enabled = form->browser->CanPaste();
}
else {
acBold->Enabled = false;
acItalics->Enabled = false;
acUnderline->Enabled = false;
acInsertHyperlink->Enabled = false;
acInsertImage->Enabled = false;
acInsertList->Enabled = false;
acUndo->Enabled = form->RichEdit1->CanUndo;
acRedo->Enabled = false;
acCut->Enabled = form->RichEdit1->SelLength > 0;
acCopy->Enabled = form->RichEdit1->SelLength > 0;
acPaste->Enabled = form->browser->CanPaste();
}
IHTMLTxtRange *range = form->browser->TxtRange();
if (range != NULL && !form->RichEdit1->Focused()) {
acBold->Checked = form->browser->isBold(range);
acItalics->Checked = form->browser->isItalic(range);
acUnderline->Checked = form->browser->isUnderline(range);
bTextColor->SymbolColor = form->browser->GetColor(range);
cbTextFont->Text = form->browser->GetFont(range);
cbTextSize->ItemIndex = form->browser->GetSize(range);
}
// exper
}
// ---------------------------------------------------------------------------
void __fastcall TForm1::acExitExecute(TObject *Sender) {
int count = MDIChildCount;
for (int i = count - 1; i >= 0; i--) {
MDIChildren[i]->Close();
}
Close();
}
// ---------------------------------------------------------------------------
void __fastcall TForm1::acUndoExecute(TObject *Sender) {
TDocumentForm *form = (TDocumentForm*)this->ActiveMDIChild;
if (!form)
return;
form->browser->Undo();
}
// ---------------------------------------------------------------------------
void __fastcall TForm1::acRedoExecute(TObject *Sender) {
TDocumentForm *form = (TDocumentForm*)this->ActiveMDIChild;
if (!form)
return;
form->browser->Redo();
}
// ---------------------------------------------------------------------------
void __fastcall TForm1::bTextColorClick(TObject *Sender) {
TDocumentForm *form = (TDocumentForm*)this->ActiveMDIChild;
if (!form)
return;
curstyle->setcolor(ColorToRGB(bTextColor->SymbolColor));
cbStyle->ItemIndex = 0;
form->browser->SetColor(ColorToRGB(bTextColor->SymbolColor));
}
// ---------------------------------------------------------------------------
void __fastcall TForm1::cbTextSizeSelect(TObject *Sender) {
TDocumentForm *form = (TDocumentForm*)this->ActiveMDIChild;
if (!form)
return;
int size = cbTextSize->ItemIndex;
cbStyle->ItemIndex = 0;
curstyle->setsize(size);
form->browser->SetSize(size);
}
// ---------------------------------------------------------------------------
void __fastcall TForm1::cbTextFontClick(TObject *Sender) {
TDocumentForm *form = (TDocumentForm*)this->ActiveMDIChild;
if (!form)
return;
curstyle->setface(cbTextFont->Text);
cbStyle->ItemIndex = 0;
form->browser->SetFont(cbTextFont->Text);
}
// Установить текущий стиль---------------------------------------------------------------------------
void TForm1::SetStyleToUI(Style *style) {
cbTextSize->ItemIndex = style->getsize();
cbTextFont->Text = style->getface();
bTextColor->SymbolColor = style->getcolor();
int fontstyle = style->getfontstyle();
acBold->Checked = ((fontstyle & fsBOLD) == fsBOLD);
acItalics->Checked = ((fontstyle & fsITALIC) == fsITALIC);
acUnderline->Checked = ((fontstyle & fsUNDERLINE) == fsUNDERLINE);
}
void __fastcall TForm1::cbStyleSelect(TObject *Sender) {
TDocumentForm *form = (TDocumentForm*)this->ActiveMDIChild;
if (!form)
return;
int index = cbStyle->ItemIndex;
Style *style = (Style*)cbStyle->Items->Objects[index];
*curstyle = *style;
SetStyleToUI(style);
form->browser->SetStyle(style);
cbStyle->ItemIndex = 0;
}
// ---------------------------------------------------------------------------