Subversion Repositories Kolibri OS

Rev

Rev 5886 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
5819 leency 1
#define MEMSIZE 4096*20
2
 
3
#include "../lib/font.h"
5886 pavelyakov 4
#include "../lib/io.h"
5819 leency 5
#include "../lib/gui.h"
6
#include "../lib/list_box.h"
7
#include "../lib/obj/box_lib.h"
8
#include "../lib/obj/libini.h"
9
#include "../lib/obj/iconv.h"
10
#include "../lib/obj/proc_lib.h"
11
#include "../lib/patterns/libimg_load_skin.h"
12
#include "../lib/patterns/simple_open_dialog.h"
13
 
14
#define TOOLBAR_H 34
5923 leency 15
#define TOOLBAR_ICON_WIDTH  26
16
#define TOOLBAR_ICON_HEIGHT 24
5819 leency 17
 
5923 leency 18
#define TOOLBAR_OPEN_FILE_LEFT       8
19
#define TOOLBAR_MAGNIFY_PLUS_LEFT    42
20
#define TOOLBAR_MAGNIFY_MINUS_LEFT   67
21
#define TOOLBAR_CHANGE_ENCODING_LEFT 101
22
#define TOOLBAR_RUN_EDIT_LEFT        135
23
 
24
#define DEFAULT_FONT   "/sys/fonts/Tahoma.kf"
25
#define DEFAULT_EDITOR "/sys/tinypad"
26
 
27
#define INTRO_TEXT "This is a plain text reader.\nTry to open some text file."
28
 
5819 leency 29
char default_dir[] = "/rd/1";
30
od_filter filter2 = {0,0};
31
 
32
scroll_bar scroll = { 15,200,398,44,0,2,115,15,0,0xeeeeee,0xBBBbbb,0xeeeeee,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1};
33
llist list;
34
 
35
proc_info Form;
36
char title[4196];
37
 
38
byte help_opened = false;
39
 
5825 leency 40
char char_width[255];
41
dword line_offset;
42
#define DWORD 4;
43
 
5819 leency 44
enum {
5923 leency 45
	OPEN_FILE = 10,
5819 leency 46
	MAGNIFY_MINUS,
47
	MAGNIFY_PLUS,
48
	CHANGE_ENCODING,
49
	RUN_EDIT,
50
	SHOW_INFO,
51
};
52
 
53
#include "ini.h"
54
#include "menu.h"
55
 
5923 leency 56
void InitDlls()
57
{
58
	load_dll(boxlib,    #box_lib_init,   0);
59
	load_dll(libio,     #libio_init,     1);
60
	load_dll(libimg,    #libimg_init,    1);
61
	load_dll(libini,    #lib_init,       1);
62
	load_dll(iconv_lib, #iconv_open,     0);
63
	load_dll(Proc_lib,  #OpenDialog_init,0);
64
}
65
 
66
void EventShowInfo()
67
{
68
	ShowAbout();
69
}
70
 
71
void EventOpenFile()
72
{
73
	OpenDialog_start stdcall (#o_dialog);
74
	OpenFile(#openfile_path);
75
	PreparePage();
76
}
77
 
78
void EventMagnifyPlus()
79
{
80
	font.size.text++;
81
	if(!font.changeSIZE())
82
		font.size.text--;
83
	else
84
		PreparePage();
85
}
86
 
87
void EventMagnifyMinus()
88
{
89
	font.size.text--;
90
	if(!font.changeSIZE())
91
		font.size.text++;
92
	else
93
		PreparePage();
94
}
95
 
96
void EventRunEdit()
97
{
98
	io.run(DEFAULT_EDITOR, #param);
99
}
100
 
101
void EventChangeEncoding()
102
{
103
	CreateThread(#menu_rmb,#stak+4092);
104
}
105
 
106
void HandleMouseEvent()
107
{
108
	mouse.get();
109
	list.wheel_size = 7;
110
	if (list.MouseScroll(mouse.vert)) {
111
		DrawPage();
112
		return;
113
	}
114
	scrollbar_v_mouse (#scroll);
115
	if (list.first != scroll.position) {
116
		list.first = scroll.position;
117
		DrawPage();
118
	}
119
}
120
 
121
void HandleKeyEvent()
122
{
123
	if (help_opened) {
124
		help_opened = false;
125
		DrawPage();
126
		return;
127
	}
128
	GetKeys();
129
	if (key_scancode==059) {
130
		EventShowInfo();
131
		return;
132
	}
133
	if (key_modifier & KEY_LCTRL) || (key_modifier & KEY_RCTRL) {
134
		switch (key_scancode)
135
		{
136
			case 024:
137
				EventOpenFile();
138
				break;
139
			case SCAN_CODE_UP:
140
				EventMagnifyPlus();
141
				break;
142
			case SCAN_CODE_DOWN:
143
				EventMagnifyMinus();
144
				break;
145
			case 018:
146
				EventRunEdit();
147
				break;
148
			case SCAN_CODE_TAB:
149
				EventChangeEncoding();
150
				break;
151
		}
152
		return;
153
	}
154
	if (list.ProcessKey(key_scancode))
155
		DrawPage();
156
}
157
 
158
void HandleButtonEvent()
159
{
160
 
161
	byte btn = GetButtonID();
162
	if (btn==1) {
163
		SaveIniSettings();
164
		ExitProcess();
165
	}
166
	btn-=10;
167
	switch(btn)
168
	{
169
		case OPEN_FILE:
170
			EventOpenFile();
171
			break;
172
		case MAGNIFY_PLUS:
173
			EventMagnifyPlus();
174
			break;
175
		case MAGNIFY_MINUS:
176
			EventMagnifyMinus();
177
			break;
178
		case CHANGE_ENCODING:
179
			EventChangeEncoding();
180
			break;
181
		case RUN_EDIT:
182
			EventRunEdit();
183
			break;
184
		case SHOW_INFO:
185
			EventShowInfo();
186
			break;
187
	}
188
}
189
 
5819 leency 190
void main()
5923 leency 191
{
192
	InitDlls();
193
 
5819 leency 194
	OpenDialog_init stdcall (#o_dialog);
5923 leency 195
 
196
	font.no_bg_copy = true;
197
	font.color      = 0;
198
	font.bg_color   = 0xFFFFFF;
199
 
200
	font.load(DEFAULT_FONT);
201
 
202
	if (!font.data) {
203
		io.run("/sys/@notify","'Error: Font is not loaded.' -E");
204
		ExitProcess();
205
	}
206
 
5819 leency 207
	Libimg_LoadImage(#skin, abspath("toolbar.png"));
5923 leency 208
 
5819 leency 209
	LoadIniSettings();
5923 leency 210
 
5819 leency 211
	OpenFile(#param);
212
	list.no_selection = true;
213
	SetEventMask(10000000000000000000000001100111b);
214
	loop()
215
	{
5923 leency 216
		switch(WaitEvent())
217
		{
218
			case evMouse:
219
				HandleMouseEvent();
5819 leency 220
				break;
5923 leency 221
			case evKey:
222
				HandleKeyEvent();
223
				break;
224
			case evButton:
225
				HandleButtonEvent();
226
				break;
227
			case evReDraw:
228
				if (action_buf) {
229
					OpenFile(#param);
230
					PreparePage();
231
					action_buf = false;
232
				};
233
				draw_window();
234
		}
5819 leency 235
	}
236
}
237
 
238
void draw_window()
239
{
240
	DefineAndDrawWindow(Form.left,Form.top,Form.width,Form.height,0x73,0,#title);
241
	GetProcessInfo(#Form, SelfInfo);
242
	if (Form.status_window>2) return;
5923 leency 243
 
5819 leency 244
	if (Form.width  < 200) { MoveSize(OLD,OLD,200,OLD); return; }
245
	if (Form.height < 200) { MoveSize(OLD,OLD,OLD,200); return; }
5923 leency 246
 
5819 leency 247
	DrawBar(0, 0, Form.cwidth, TOOLBAR_H - 1, 0xe1e1e1);
248
	DrawBar(0, TOOLBAR_H - 1, Form.cwidth, 1, 0x7F7F7F);
5923 leency 249
 
250
	DrawToolbarButton(OPEN_FILE,       TOOLBAR_OPEN_FILE_LEFT);
251
	DrawToolbarButton(MAGNIFY_PLUS,    TOOLBAR_MAGNIFY_PLUS_LEFT);
252
	DrawToolbarButton(MAGNIFY_MINUS,   TOOLBAR_MAGNIFY_MINUS_LEFT);
253
	DrawToolbarButton(CHANGE_ENCODING, TOOLBAR_CHANGE_ENCODING_LEFT);
254
	DrawToolbarButton(RUN_EDIT,        TOOLBAR_RUN_EDIT_LEFT);
255
	DrawToolbarButton(SHOW_INFO,       Form.cwidth - 34);
256
 
257
	if ((Form.cwidth-scroll.size_x-1 == list.w) &&
258
		(Form.cheight-TOOLBAR_H == list.h) &&
259
		(list.count)
260
	)
261
	{
262
		DrawPage();
263
	} else {
264
		PreparePage();
265
	}
5819 leency 266
	DrawRectangle(scroll.start_x, scroll.start_y, scroll.size_x, scroll.size_y-1, scroll.bckg_col);
267
}
268
 
269
void DrawPage()
270
{
5825 leency 271
	_PutImage(list.x,list.y,list.w,list.h,list.first*list.item_h*list.w*3 + font.buffer);
5819 leency 272
	DrawScroller();
273
}
274
 
275
void PreparePage()
276
{
277
	char line[4096]=0;
278
	dword line_start;
279
	byte ch;
280
	dword bufoff;
281
	dword line_length=30;
282
	dword stroka_y = 5;
283
	dword stroka=0;
284
	int i, srch_pos;
5923 leency 285
 
5819 leency 286
	font.changeSIZE();
287
	list.w = Form.cwidth-scroll.size_x-1;
288
	//get font chars width, need to increase performance
5825 leency 289
	for (i=0; i<256; i++) char_width[i] = font.symbol_size(i);
5819 leency 290
	//get font buffer height
291
	for (bufoff=io.buffer_data; ESBYTE[bufoff]; bufoff++)
292
	{
293
		ch = ESBYTE[bufoff];
5825 leency 294
		line_length += char_width[ch];
5819 leency 295
		if (line_length>=list.w) || (ch==10) {
296
			srch_pos = bufoff;
297
			loop()
298
			{
299
				if (__isWhite(ESBYTE[srch_pos])) { bufoff=srch_pos+1; break; } //normal word-break
300
				if (srch_pos == line_start) break; //no white space found in whole line
301
				srch_pos--;
302
			}
303
			line_start = bufoff;
304
			line_length = 30;
305
			stroka++;
306
		}
307
	}
308
	//draw text in buffer
309
	list.count = stroka+2;
310
	list.SetSizes(0, TOOLBAR_H, list.w, Form.cheight-TOOLBAR_H, font.size.text+1);
311
	if (list.count < list.visible) list.count = list.visible;
312
 
5825 leency 313
	font.size.height = list.count+1*list.item_h;
5819 leency 314
	font.buffer_size = 0;
315
 
316
	line_length = 30;
317
	line_start = io.buffer_data;
318
	for (bufoff=io.buffer_data; ESBYTE[bufoff]; bufoff++)
319
	{
320
		ch = ESBYTE[bufoff];
5825 leency 321
		line_length += char_width[ch];
5819 leency 322
		if (line_length>=list.w) || (ch==10)
323
		{
324
			//set word break
325
			srch_pos = bufoff;
326
			loop()
327
			{
328
				if (__isWhite(ESBYTE[srch_pos])) { bufoff=srch_pos+1; break; } //normal word-break
329
				if (srch_pos == line_start) break; //no white space found in whole line
330
				srch_pos--;
331
			}
332
			i = bufoff-line_start;
333
			strlcpy(#line, line_start, i);
334
			font.prepare_buf(8,stroka_y,list.w,font.size.height, #line);
5825 leency 335
			stroka_y += list.item_h;
5819 leency 336
			line_start = bufoff;
337
			line_length = 30;
338
		}
339
	}
340
	font.prepare_buf(8,stroka_y,list.w,font.size.height, line_start);
341
	SmoothFont(font.buffer, font.size.width, font.size.height);
342
	DrawPage();
343
}
344
 
345
void DrawToolbarButton(char image_id, int x)
346
{
5923 leency 347
	DefineButton(x, 5, TOOLBAR_ICON_WIDTH-1, TOOLBAR_ICON_HEIGHT-1, 10+image_id + BT_HIDE, 0);
348
	img_draw stdcall(skin.image, x, 5, TOOLBAR_ICON_WIDTH, TOOLBAR_ICON_HEIGHT, 0, image_id*TOOLBAR_ICON_HEIGHT);
5819 leency 349
}
350
 
351
void DrawScroller()
352
{
353
	scroll.max_area = list.count;
354
	scroll.cur_area = list.visible;
355
	scroll.position = list.first;
356
	scroll.all_redraw = 0;
357
	scroll.start_x = list.x + list.w;
358
	scroll.start_y = list.y;
359
	scroll.size_y = list.h;
360
	scroll.start_x = list.x + list.w;
361
	scrollbar_v_draw(#scroll);
362
}
363
 
364
void OpenFile(dword f_path)
365
{
366
	int tmp;
367
	if (ESBYTE[f_path]) {
368
		strcpy(#param, f_path);
369
		io.read(#param);
370
		strcpy(#title, #param);
371
		strcat(#title, " - Text Reader");
372
	}
373
	else {
374
		if (list.count) return;
5923 leency 375
		io.buffer_data = INTRO_TEXT;
5819 leency 376
		strcpy(#title, "Text Reader");
377
	}
378
	if (encoding!=CH_CP866) ChangeCharset(charsets[encoding], "CP866", io.buffer_data);
379
	list.KeyHome();
380
	list.ClearList();
381
}
382
 
383
 
384
 
385
char *about[] = {
5923 leency 386
	"Text Reader v1.01",
5819 leency 387
	"Idea: Leency, punk_joker",
5923 leency 388
	"Code: Leency, Veliant, KolibriOS Team",
5819 leency 389
	" ",
390
	"Hotkeys:",
391
	"Ctrl+O - open file",
392
	"Ctrl+Up - bigger font",
393
	"Ctrl+Down - smaller font",
394
	"Ctrl+Tab - select charset",
395
	"Ctrl+E - edit current document",
396
	" ",
397
	"Press any key...",
398
 
399
};
400
 
401
ShowAbout() {
402
	int i;
403
	help_opened = true;
404
	DrawBar(list.x, list.y, list.w, list.h, 0xFFFfff);
405
	WriteText(list.x + 10, list.y + 10, 10000001b, 0x555555, about[0]);
406
	for (i=1; about[i]; i++) WriteText(list.x + 10, i+1*20 + list.y, 10110000b, 0, about[i]);
5923 leency 407
}