aboutsummaryrefslogtreecommitdiffstats
path: root/apps/windoze/Delphi20/swordvc/swtxtdsp.pas
blob: 27a63f7e002c54034e6d73e2ba53dce8e1d03808 (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
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
unit SWTXTDSP;

interface

uses
  istreams, SWModule, SWVSKey, Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ComCtrls;
var
	defRTFHeader :AnsiString;
	defRTFTrailer :AnsiString;

	defRTFChapterMarkPre :AnsiString;
	defRTFChapterMarkPost :AnsiString;

     defRTFVerseMarkPre :AnsiString;
     defRTFVerseMarkPost :AnsiString;

     defRTFVersePre :AnsiString;
     defRTFVersePost :AnsiString;

type
     charrange = record
          min : longint;
          max : longint;
     end;

	TSWTextDisp = class(TRichEdit)
	private
		FSWModule: TSWModule;
          FCurVerseMrk : boolean;
          FCurVersePos : boolean;
          FRTFHeader: AnsiString;
          FRTFTrailer: AnsiString;
          FRTFChapterMarkPre :AnsiString;
          FRTFChapterMarkPost :AnsiString;
          FRTFVerseMarkPre :AnsiString;
          FRTFVerseMarkPost :AnsiString;
          FRTFVersePre :AnsiString;
          FRTFVersePost :AnsiString;
          FMarkCurrentVerse :boolean;
          FCharRange : charrange;
          FAutoPosition : boolean;
          function GetMySelStart: integer;
          procedure SetMySelStart(iselstart : integer);
	protected
	     RTFStream: TMemoryStream;
		procedure SetSWModule(imodule: TSWModule); virtual;
          function  GetSWModule: TSWModule; virtual;
		procedure Loaded; override;
	public
     	constructor Create(AOwner: TComponent); override;
 		destructor  Destroy; override;
		procedure   Display; virtual;
	published
          property CurVersePos : boolean read FCurVersePos write FCurVersePos default True;
          property CurVerseMrk : boolean read FCurVerseMrk write FCurVerseMrk default True;
     	property SWModule: TSWModule read GetSWModule write SetSWModule;
     	property RTFHeader: AnsiString read FRTFHeader write FRTFHeader;
     	property RTFChapterMarkPre: AnsiString read FRTFChapterMarkPre write FRTFChapterMarkPre;
     	property RTFChapterMarkPost: AnsiString read FRTFChapterMarkPost write FRTFChapterMarkPost;
     	property RTFVerseMarkPre: AnsiString read FRTFVerseMarkPre write FRTFVerseMarkPre;
     	property RTFVerseMarkPost: AnsiString read FRTFVerseMarkPost write FRTFVerseMarkPost;
     	property RTFVersePre: AnsiString read FRTFVersePre write FRTFVersePre;
     	property RTFVersePost: AnsiString read FRTFVersePost write FRTFVersePost;
     	property RTFTrailer: AnsiString read FRTFTrailer write FRTFTrailer;
     	property MarkCurrentVerse: boolean read FMarkCurrentVerse write FMarkCurrentVerse default true;
          Property AutoPosition: boolean read FAutoPosition write FAutoPosition default true;
//          property SelStart read GetMySelStart write SetMySelStart;
  end;


procedure Register;

implementation

constructor TSWTextDisp.Create(AOwner: TComponent);
begin
	inherited Create(AOwner);

     FCurVersePos := True;
     FCurVerseMrk := True;

	RTFHeader := defRTFHeader;
	RTFTrailer := defRTFTrailer;

	RTFChapterMarkPre := defRTFChapterMarkPre;
	RTFChapterMarkPost := defRTFChapterMarkPost;

     RTFVerseMarkPre := defRTFVerseMarkPre;
     RTFVerseMarkPost := defRTFVerseMarkPost;

     RTFVersePre := defRTFVersePre;
     RTFVersePost := defRTFVersePost;

//     RTFStream := TMemoryStream.Create;

     MarkCurrentVerse := true;
     AutoPosition := true;
end;


destructor TSWTextDisp.Destroy;
begin
	if (RTFStream <> nil) then
	     RTFStream.Free;
	inherited Destroy;
end;


procedure TSWTextDisp.Loaded;
begin
	inherited Loaded;

	if (RTFHeader = '') then
		RTFHeader := defRTFHeader;
	if (RTFTrailer = '') then
		RTFTrailer := defRTFTrailer;

	if (RTFChapterMarkPre = '') then
		RTFChapterMarkPre := defRTFChapterMarkPre;
	if (RTFChapterMarkPost = '') then
		RTFChapterMarkPost := defRTFChapterMarkPost;

	if (RTFVerseMarkPre = '') then
	     RTFVerseMarkPre := defRTFVerseMarkPre;
	if (RTFVerseMarkPost = '') then
	     RTFVerseMarkPost := defRTFVerseMarkPost;

	if (RTFVersePre = '') then
	     RTFVersePre := defRTFVersePre;
	if (RTFVersePost = '') then
	     RTFVersePost := defRTFVersePost;

	if not (csDesigning in ComponentState) then
     begin
	if (RTFStream = nil) then
	     RTFStream := TMemoryStream.Create;
     end;
end;

//#define EM_EXGETSEL				(WM_USER + 52)
//#define EM_EXSETSEL				(WM_USER + 55)

function TSWTextDisp.GetMySelStart: integer;
begin
     SendMessage(Handle, (WM_USER + 52), 0, longint(@Fcharrange));
     GetMySelStart := Fcharrange.min;
end;


procedure TSWTextDisp.SetMySelStart(iselstart : integer);
begin
     Fcharrange.min := iselstart;
     Fcharrange.max := iselstart;
     SendMessage(Handle, (WM_USER + 55), 0, longint(@Fcharrange));
end;


procedure TSWTextDisp.Display;
var
	book, chap, verse, versepos, currentpos: integer;
     newtext, tmptext: AnsiString;
     buf: PChar;
begin
	chap := TSWVerseKey(SWModule.Key).Chapter;
	book := TSWVerseKey(SWModule.Key).Book;
     verse:= TSWVerseKey(SWModule.Key).Verse;
     TSWVerseKey(SWModule.Key).Verse := 1;
	newtext := RTFHeader;
     newtext := newtext + RTFChapterMarkPre + IntToStr(TSWVerseKey(SWModule.Key).Chapter) + RTFChapterMarkPost;
     SWModule.Error; // clear error;
     currentpos := 0;
     while ((TSWVerseKey(SWModule.Key).Book = book) and (TSWVerseKey(SWModule.Key).Chapter = chap) and (SWModule.Error = 0)) do
     begin
		if (TSWVerseKey(SWModule.Key).Verse = verse) then
          begin
               if (MarkCurrentVerse = true) then	{This does not work for shading background of current verse, but should }
          	   newtext := newtext + '\pard \nowidctlpar \shading2000 \cf2' // \cf2 = second color in color table
               else newtext := newtext + '\pard \nowidctlpar \cf0';
          end  else newtext := newtext + '\pard \nowidctlpar \cf0';

          newtext := newtext + RTFVerseMarkPre + IntToStr(TSWVerseKey(SWModule.Key).Verse) + RTFVerseMarkPost;
          newtext := newtext + RTFVersePre + SWModule.Text + RTFVersePost;
		if ((TSWVerseKey(SWModule.Key).Verse = verse) and (AutoPosition = true)) then
          begin
          	tmptext := newtext + RTFTrailer;
               RTFStream.Clear;
               RTFStream.WriteBuffer(Pointer(tmptext)^, Length(tmptext));
               RTFStream.Position := 0;
               FMarkCurrentVerse := true;
               Lines.LoadFromStream(RTFStream);
               PlainText := true;
               versepos := GetTextLen; //RTFStream.Position;
               PlainText := false;
          end;
		SWModule.Inc;
     end;
	newtext := newtext + RTFTrailer;
     TSWVerseKey(SWModule.Key).Verse   := 1; { When setting chapter: if (verse <> new chapter range) don't autonormalize. (we could've just turned the autonormalize option off then back on, but this is cooler) }
     TSWVerseKey(SWModule.Key).Book    := book;
     TSWVerseKey(SWModule.Key).Chapter := chap;
     TSWVerseKey(SWModule.Key).Verse   := verse;
     RTFStream.Clear;
     RTFStream.WriteBuffer(Pointer(newtext)^, Length(newtext));
     RTFStream.Position := 0;
	Lines.LoadFromStream(RTFStream);

	{ Position control text at current verse }
     if (AutoPosition = true) then
     begin
          SelStart := versepos;
          SendMessage(Handle, EM_SCROLLCARET, 0, 0);
     end;
end;


procedure TSWTextDisp.SetSWModule(imodule: TSWModule);
begin
	FSWModule := imodule;
end;


function  TSWTextDisp.GetSWModule: TSWModule;
begin
	GetSWModule := FSWModule;
end;


procedure Register;
begin
     RegisterComponents('SWORD', [TSWTextDisp]);
end;
initialization
     defRTFHeader          := '{\rtf1\ansi{\fonttbl{\f4\froman\fcharset0\fprq2 Times New Roman;}}{\colortbl;\red0\green0\blue255;\red0\green200\blue50;}';
     defRTFTrailer         := '{\fs24 \par }}';

     defRTFChapterMarkPre  := '\pard \qc\nowidctlpar{\fs30\b Chapter ';
	defRTFChapterMarkPost := '\par\fs10\par}';

     defRTFVerseMarkPre    := '{\fs20\cf1\super ';
     defRTFVerseMarkPost   := '}';

     defRTFVersePre        := '{\fs24';
     defRTFVersePost       := ' }';
finalization
            
end.