Subversion Repositories Kolibri OS

Rev

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

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