Subversion Repositories Kolibri OS

Rev

Rev 9896 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
9896 akron1 1
(*
9898 akron1 2
    Copyright 2020-2021, 2023 Anton Krotov
9896 akron1 3
 
4
    This file is part of fb2read.
5
 
6
    fb2read is free software: you can redistribute it and/or modify
7
    it under the terms of the GNU General Public License as published by
8
    the Free Software Foundation, either version 3 of the License, or
9
    (at your option) any later version.
10
 
11
    fb2read is distributed in the hope that it will be useful,
12
    but WITHOUT ANY WARRANTY; without even the implied warranty of
13
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
    GNU General Public License for more details.
15
 
16
    You should have received a copy of the GNU General Public License
17
    along with fb2read. If not, see .
18
*)
19
 
20
MODULE SearchForm;
21
 
22
IMPORT
23
 
9898 akron1 24
    SYSTEM, SU := SysUtils, W := Window, box_lib, K := KOSAPI, Encoding;
9896 akron1 25
 
26
 
27
CONST
28
 
29
    BTN_CLOSE  =  1;
30
    BTN_FIND   = 19;
31
    BTN_CANCEL = 20;
32
 
33
    BtnH = 25;
34
    BtnW = 80;
35
 
36
    WINDOW_BEVEL = 4;
37
 
38
    MAXCHARS = 2000;
39
 
40
 
41
TYPE
42
 
43
    STRING* = ARRAY MAXCHARS OF CHAR;
44
 
45
    PROC = PROCEDURE (case: BOOLEAN; str: STRING): BOOLEAN;
46
 
47
 
48
VAR
49
 
50
    PID, Slot: INTEGER;
51
    Stack: ARRAY 1000000 OF CHAR;
9898 akron1 52
    Window: W.tWindow;
9896 akron1 53
    str: STRING;
54
 
55
    callback: PROC;
56
    case: box_lib.checkbox;
57
    text: box_lib.edit_box;
58
 
59
 
60
PROCEDURE DrawText (x, y: INTEGER; text: ARRAY OF CHAR);
61
VAR
62
	L: INTEGER;
63
BEGIN
64
	L := LENGTH(text);
65
	SU.Box(x, y, L*SU.FontW, SU.FontH, SU.winColor, SU.winColor);
66
	SU.OutText(x, y, text, L, SU.textColor)
67
END DrawText;
68
 
69
 
70
PROCEDURE buttons;
71
BEGIN
72
    SU.CreateButton(BTN_FIND, 5, 80, BtnW, BtnH, SU.btnColor, "find");
73
    SU.CreateButton(BTN_CANCEL, 5 - BtnW + text.width, 80, BtnW, BtnH, SU.btnColor, "cancel");
74
    box_lib.check_box_draw2(case); DrawText(25, 50, "match case");
75
    box_lib.edit_box_draw(text)
76
END buttons;
77
 
78
 
79
PROCEDURE DrawWindow;
80
BEGIN
81
	SU.GetSystemColors;
82
    SU.WindowRedrawStatus(1);
9898 akron1 83
    SU.DefineAndDrawWindow(Window.left, Window.top, Window.width, Window.height,
84
          SU.winColor, LSL(ORD({0, 1}), 4) + 4, Window.caption);
9896 akron1 85
    buttons;
86
    SU.WindowRedrawStatus(2)
87
END DrawWindow;
88
 
89
 
90
PROCEDURE close* (ok: BOOLEAN);
91
VAR
92
    pid, i, j, k, n: INTEGER;
93
    found: BOOLEAN;
94
    str0: STRING;
9898 akron1 95
    u: Encoding.tUtf8;
9896 akron1 96
 
97
BEGIN
98
    found := TRUE;
99
    box_lib.edit_box_get_value(text, str);
100
 
101
    IF ok THEN
102
        IF str # "" THEN
103
            j := 0;
104
            i := 0;
105
            WHILE str[i] # 0X DO
9898 akron1 106
                u := Encoding.cp866[ORD(str[i])].utf8;
107
                n := Encoding.cp866[ORD(str[i])].len;
9896 akron1 108
                FOR k := 0 TO n - 1 DO
109
                    str0[j] := u[k];
110
                    INC(j)
111
                END;
112
                INC(i)
113
            END;
114
            found := callback(box_lib.check_box_get_value(case), str0)
115
        ELSE
116
            found := FALSE
117
        END
118
    END;
119
 
120
    IF found THEN
121
        pid := PID;
122
        PID := 0;
123
        IF pid # 0 THEN
124
            SU.TerminateThreadId(pid)
125
        END
126
    ELSE
127
        IF str # "" THEN
128
        	DrawText(5 + BtnW + 10, 80 + 4, "not found")
129
        END
130
    END
131
END close;
132
 
133
 
134
PROCEDURE ButtonClick;
135
BEGIN
136
    CASE SU.GetButtonCode() OF
137
    |0                     :
138
    |BTN_CLOSE, BTN_CANCEL : close(FALSE)
139
    |BTN_FIND              : close(TRUE)
140
    END;
141
    buttons
142
END ButtonClick;
143
 
144
 
145
PROCEDURE show;
146
VAR
147
    scrWidth, scrHeight, key: INTEGER;
148
 
149
BEGIN
150
    SU.SetEventsMask({0, 1, 2, 5, 30, 31});
9898 akron1 151
    W.init(Window, 0, 0, 320, 140, "Search");
9896 akron1 152
    SU.GetScreenSize(scrWidth, scrHeight);
9898 akron1 153
    Window.left := (scrWidth - Window.width) DIV 2;
154
    Window.top  := (scrHeight - Window.height) DIV 2;
9896 akron1 155
 
156
    DrawWindow;
157
    WHILE TRUE DO
158
        CASE SU.WaitForEvent() OF
159
        |1: DrawWindow
9898 akron1 160
        |2: key := SU.GetKey();
161
        	IF key DIV 65536 = 28 THEN (* enter *)
9896 akron1 162
        		close(TRUE)
9898 akron1 163
        	ELSIF key DIV 65536 = 1 THEN (* esc *)
9896 akron1 164
        		close(FALSE)
165
        	ELSE
166
        		box_lib.edit_box_key_safe(text, key)
167
        	END
168
        |3: ButtonClick
169
        |6:
170
            box_lib.check_box_mouse2(case);
171
            box_lib.edit_box_mouse(text)
172
        ELSE
173
        END
174
    END
175
END show;
176
 
177
 
178
PROCEDURE open*;
179
BEGIN
180
    IF PID = 0 THEN
181
        PID  := SU.NewThread(show, Stack);
182
        Slot := SU.GetThreadSlot(PID)
183
    ELSE
184
        SU.FocusWindow(Slot)
185
    END
186
END open;
187
 
188
 
189
PROCEDURE init* (proc: PROC);
190
BEGIN
191
    callback := proc;
192
    PID := 0;
193
    case := box_lib.kolibri_new_check_box(5, 50, 16, 16, SYSTEM.SADR(""), 14 * 8 + 5);
194
    text := box_lib.kolibri_new_edit_box(5, 10, 300, MAXCHARS DIV 3);
195
    text.flags := 4002H;
196
END init;
197
 
198
 
199
END SearchForm.