summaryrefslogtreecommitdiff
path: root/apps/windoze/CBuilder5/prototype/editentryfrm.cpp
blob: e9404f884a24afec9948d036d8d8814460c9485b (plain)
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
#include <vcl.h>
#pragma hdrstop
#include <windows.hpp>
#include <stdlib.h>
#include <stdio.h>
#include "editentryfrm.h"
#include <swdisprtf.h>

const float RulerAdj = 4.0/3.0;
const int GutterWid = 6;
//----------------------------------------------------------------------------
#pragma resource "*.dfm"
TEditEntry *EditEntry;
//----------------------------------------------------------------------------
__fastcall TEditEntry::TEditEntry(TComponent *Owner)
  : TForm(Owner)
{
	ResultBuf = 0;
}


__fastcall TEditEntry::~TEditEntry()
{
	if (ResultBuf)
		delete [] ResultBuf;
}


//----------------------------------------------------------------------------
void __fastcall TEditEntry::SelectionChange(TObject */*Sender*/)
{
  char sizebuf[6];

  try {
	   FUpdating = True;
	   FirstInd->Left = int(RichEdit1->Paragraph->FirstIndent*RulerAdj)-
							4+GutterWid;
	   LeftInd->Left  = int((RichEdit1->Paragraph->LeftIndent+
							RichEdit1->Paragraph->FirstIndent)*RulerAdj)-
							4+GutterWid;
	   RightInd->Left = Ruler->ClientWidth-6-int(
						(RichEdit1->Paragraph->RightIndent+GutterWid)*RulerAdj);

	   BoldButton->Down = RichEdit1->SelAttributes->Style.Contains(fsBold);
	   ItalicButton->Down = RichEdit1->SelAttributes->Style.Contains(fsItalic);
	   UnderlineButton->Down = RichEdit1->SelAttributes->Style.Contains(fsUnderline);

	   BulletsButton->Down = bool(RichEdit1->Paragraph->Numbering);

	   FontSize->Text = itoa(RichEdit1->SelAttributes->Size, sizebuf, 10);

	   switch((int)RichEdit1->Paragraph->Alignment)
	   { case 0: LeftAlign->Down   = True; break;
		 case 1: RightAlign->Down  = True; break;
		 case 2: CenterAlign->Down = True; break;
	   }
	 }
	catch (...) {
	  FUpdating = False;
	}
	FUpdating = False;
}
//----------------------------------------------------------------------------
TTextAttributes *__fastcall TEditEntry::CurrText(void)
{
    return RichEdit1->SelAttributes;
}
//----------------------------------------------------------------------------
void __fastcall TEditEntry::CheckFileSave(void)
{
	if ( RichEdit1->Modified ) {
		switch(MessageBox(Handle, "Save Changes?","Modify Comment",MB_YESNOCANCEL | MB_ICONQUESTION)) {
		case ID_YES    : ExtractRTF(); break;
		case ID_CANCEL : Abort(); break;
		default: 
			if (ResultBuf)
				delete [] ResultBuf;
			ResultBuf = 0;
			break;
		};
	}
}


void __fastcall TEditEntry::SetupRuler(void)
{    int iCtr = 1;
	char sTmp[201];
	while (iCtr < 200) {
	  sTmp[iCtr] = 9;
	  iCtr++;
	  sTmp[iCtr] = '|';
	  iCtr++;
	}
	Ruler->Caption = (AnsiString)sTmp;
}
//----------------------------------------------------------------------------
void __fastcall TEditEntry::SetEditRect(void)
{    TRect Rct = Rect(GutterWid, 0, RichEdit1->ClientWidth-GutterWid,
						 ClientHeight);
	SendMessage(RichEdit1->Handle, EM_SETRECT, 0, long(&Rct));
}
//----------------------------------------------------------------------------
void __fastcall TEditEntry::FormCreate(TObject* /*Sender*/)
{    Application->OnHint = &ShowHint;
	SetupRuler();
}
//----------------------------------------------------------------------------
void __fastcall TEditEntry::ShowHint(TObject* /*Sender*/)
{    StatusBar->SimpleText = Application->Hint;
}
//----------------------------------------------------------------------------
void __fastcall TEditEntry::EditUndoClick(TObject* /*Sender*/)
{    if ( RichEdit1->HandleAllocated() )
	   SendMessage(RichEdit1->Handle, EM_UNDO, 0, 0);
}
//----------------------------------------------------------------------------
void __fastcall TEditEntry::EditCutClick(TObject* /*Sender*/)
{    RichEdit1->CutToClipboard();
}
//----------------------------------------------------------------------------
void __fastcall TEditEntry::EditCopyClick(TObject* /*Sender*/)
{    RichEdit1->CopyToClipboard();
}
//----------------------------------------------------------------------------
void __fastcall TEditEntry::EditPasteClick(TObject* /*Sender*/)
{    RichEdit1->PasteFromClipboard();
}
//----------------------------------------------------------------------------
void __fastcall TEditEntry::RulerResize(TObject* /*Sender*/)
{     RulerLine->Width = (int)Ruler->ClientWidth - (RulerLine->Left*2);
}
//----------------------------------------------------------------------------
void __fastcall TEditEntry::FormResize(TObject* Sender)
{    SetEditRect();
	SelectionChange(Sender);
}
//----------------------------------------------------------------------------
void __fastcall TEditEntry::FormPaint(TObject* /*Sender*/)
{    SetEditRect();
}
//----------------------------------------------------------------------------
void __fastcall TEditEntry::BoldButtonClick(TObject* /*Sender*/)
{    if ( !FUpdating )
	{  if ( BoldButton->Down )
		 CurrText()->Style = CurrText()->Style << fsBold;
	   else
		 CurrText()->Style = CurrText()->Style >> fsBold;
	}
}
//----------------------------------------------------------------------------
void __fastcall TEditEntry::ItalicButtonClick(TObject* /*Sender*/)
{    if ( !FUpdating )
	{
	   if ( ItalicButton->Down )
		 CurrText()->Style = CurrText()->Style << fsItalic;
	   else
		 CurrText()->Style = CurrText()->Style >> fsItalic;
	}
}
//----------------------------------------------------------------------------
void __fastcall TEditEntry::UnderlineButtonClick(TObject* /*Sender*/)
{
	if ( !FUpdating ) {
		if ( UnderlineButton->Down )
			CurrText()->Style = CurrText()->Style << fsUnderline;
		else CurrText()->Style = CurrText()->Style >> fsUnderline;
	}
}


void __fastcall TEditEntry::FontSizeChange(TObject* /*Sender*/)
{
	int fontsize = atoi(FontSize->Text.c_str());

	if ((!FUpdating) &&  (fontsize)) {
		if (fontsize < 1) {
			ShowMessage("Please Enter a Number betweek 1 and 1638");
			FontSize->Text = 1;
		}
		else if (fontsize > 1638) {
			ShowMessage("Please Enter a Number betweek 1 and 1638");
			FontSize->Text = 1638;
		}
		CurrText()->Size = atoi(FontSize->Text.c_str());
	}
}


void __fastcall TEditEntry::AlignClick(TObject* Sender)
{
	if ( !FUpdating ) {
		TControl *oAliBtn = (TControl*)(Sender);
		RichEdit1->Paragraph->Alignment = (TAlignment)oAliBtn->Tag;
	}
}
//----------------------------------------------------------------------------
void __fastcall TEditEntry::BulletsButtonClick(TObject* /*Sender*/)
{    if ( !FUpdating )
	   RichEdit1->Paragraph->Numbering = (TNumberingStyle)BulletsButton->Down;
}
//----------------------------------------------------------------------------
void __fastcall TEditEntry::FormCloseQuery(TObject* /*Sender*/,
	bool & CanClose)
{    try {
	  CheckFileSave();
	}
	catch (...) {
	  CanClose = False;
	}
}
//----------------------------------------------------------------------------

//***************************
//***Ruler Indent Dragging***
//***************************

//----------------------------------------------------------------------------
void __fastcall TEditEntry::RulerItemMouseDown(TObject * Sender,
	TMouseButton Button, TShiftState Shift, int X, int Y)
{    TLabel * oTmpLabel = (TLabel *)Sender;
	FDragOfs = oTmpLabel->Width / 2;
	oTmpLabel->Left = oTmpLabel->Left+X-FDragOfs;
	FDragging = True;
}
//----------------------------------------------------------------------------
void __fastcall TEditEntry::RulerItemMouseMove(TObject *Sender, TShiftState Shift, int X, int /*Y*/) {
    if (FDragging) {
	   TLabel *oTmpLabel = (TLabel *)Sender;
	   oTmpLabel->Left = oTmpLabel->Left+X-FDragOfs;
       oTmpLabel->Left -= oTmpLabel->Left % 10;
	}
}
//----------------------------------------------------------------------------
void __fastcall TEditEntry::FirstIndMouseUp(TObject *Sender, TMouseButton
	Button, TShiftState Shift, int X, int Y)
{    FDragging = False;
	RichEdit1->Paragraph->FirstIndent = int((FirstInd->Left+FDragOfs-GutterWid) / RulerAdj);
	LeftIndMouseUp(Sender, Button, Shift, X, Y);
}
//----------------------------------------------------------------------------
void __fastcall TEditEntry::LeftIndMouseUp(TObject *Sender, TMouseButton
	 /*Button*/, TShiftState /*Shift*/, int /*X*/,	int /*Y*/)
{    FDragging = False;
	RichEdit1->Paragraph->LeftIndent = int((LeftInd->Left+FDragOfs-GutterWid)/
			 RulerAdj)-RichEdit1->Paragraph->FirstIndent;
	SelectionChange(Sender);
}
//----------------------------------------------------------------------------
void __fastcall TEditEntry::RightIndMouseUp(TObject *Sender, TMouseButton
	 /*Button*/, TShiftState /*Shift*/, int /*X*/,	int /*Y*/)
{    FDragging = False;
	RichEdit1->Paragraph->RightIndent =
	    int((Ruler->ClientWidth-RightInd->Left+FDragOfs-2) /
	    RulerAdj)-2*GutterWid;
	SelectionChange(Sender);
}
//----------------------------------------------------------------------------
void __fastcall TEditEntry::FormActivate(TObject *Sender)
{
	System::AnsiString newtext, tmptext;

	if (Module) {
		TMemoryStream *RTFStream = new TMemoryStream();
		newtext = RTFDisplay->RTFHeader;
		Module->Error(); // clear error;
		newtext = newtext + "\\pard \\nowidctlpar \\cf0 ";
		(char *)*Module;	// force key to snap to entry before pulling out the text of the key
		tmptext = (char *)*Module;
	
		newtext = newtext + RTFDisplay->RTFVersePre + " " + tmptext + RTFDisplay->RTFVersePost;
		newtext = newtext + RTFDisplay->RTFTrailer;
		RTFStream->Clear();
		RTFStream->WriteBuffer(newtext.c_str(), newtext.Length());
		RTFStream->Position = 0;
		RichEdit1->Lines->LoadFromStream(RTFStream);
		delete RTFStream;
	}
	SelectionChange(this);
	RichEdit1->SetFocus();
	if (ResultBuf)
		delete [] ResultBuf;
	ResultBuf = 0;
}
//---------------------------------------------------------------------

void TEditEntry::ExtractRTF()
{
	System::AnsiString newtext, tmptext;
	TMemoryStream *RTFStream = new TMemoryStream();
	char *tmpbuf, *datastart;

	RTFStream->Clear();
	RichEdit1->Lines->SaveToStream(RTFStream);
	RTFStream->Position = 0;
	if (ResultBuf)
		delete [] ResultBuf;
	tmpbuf = new char [ RTFStream->Size + 1];
	RTFStream->ReadBuffer(tmpbuf, RTFStream->Size);
	tmpbuf[RTFStream->Size] = 0;
	strtok(strstr(tmpbuf, "colortbl"), "}");
	datastart = strtok(NULL, "");
	ResultBuf = new char [ strlen(datastart) + 2 ];
	ResultBuf[0] = '{';
	strcpy(ResultBuf+1, datastart);
	delete [] tmpbuf;
	delete RTFStream;
	//--------  Change all fonts to \f1
	for (int i = 0; i < strlen(ResultBuf)-4; i++) {
		if (ResultBuf[i] == '\\') {
			if (ResultBuf[i+1] == '\\') {		//	skip a real '\' character
				i += 1;
				continue;
			}
			if (ResultBuf[i+1] == 'f') {
				if (isdigit(ResultBuf[i+2])) {
					ResultBuf[i+2] = '1';
					if (isdigit(ResultBuf[i+3])) {
						memmove(&ResultBuf[i+3], &ResultBuf[i+4], strlen(&ResultBuf[i+3]));
					}
				}
			}
		}
	}
}


__fastcall TCharsetObject::TCharsetObject(int FCharset)
  : TObject()
{
     Charset = FCharset;
}
//---------------------------------------------------------------------------