Subversion Repositories Kolibri OS

Rev

Rev 7474 | 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
 
7466 leency 3
//===================================================//
4
//                                                   //
5
//                       LIB                         //
6
//                                                   //
7
//===================================================//
8
 
5886 pavelyakov 9
#include "../lib/io.h"
7288 leency 10
#include "../lib/gui.h"
5819 leency 11
#include "../lib/list_box.h"
7286 leency 12
#include "../lib/kfont.h"
7230 leency 13
 
5819 leency 14
#include "../lib/obj/box_lib.h"
15
#include "../lib/obj/libini.h"
7230 leency 16
#include "../lib/obj/libimg.h"
5819 leency 17
#include "../lib/obj/iconv.h"
18
#include "../lib/obj/proc_lib.h"
7230 leency 19
 
5819 leency 20
#include "../lib/patterns/simple_open_dialog.h"
21
 
7466 leency 22
//===================================================//
23
//                                                   //
24
//                       DATA                        //
25
//                                                   //
26
//===================================================//
27
 
5819 leency 28
#define TOOLBAR_H 34
5923 leency 29
#define TOOLBAR_ICON_WIDTH  26
30
#define TOOLBAR_ICON_HEIGHT 24
5819 leency 31
 
5923 leency 32
#define DEFAULT_EDITOR "/sys/tinypad"
33
 
5980 leency 34
#define INTRO_TEXT "This is a plain Text Reader.\nTry to open some text file."
7466 leency 35
#define VERSION "Text Reader v1.3"
5962 leency 36
#define ABOUT "Idea: Leency, punk_joker
37
Code: Leency, Veliant, KolibriOS Team
5923 leency 38
 
5962 leency 39
Hotkeys:
40
Ctrl+O - open file
7218 leency 41
Ctrl+I - show file properties
5962 leency 42
Ctrl+Up - bigger font
43
Ctrl+Down - smaller font
44
Ctrl+Tab - select charset
7462 leency 45
Ctrl+E - reopen current file in another app
5962 leency 46
 
47
Press any key..."
48
 
7466 leency 49
dword color_schemes[] = {
50
0xFFFfff, 0,
51
0xF0F0F0, 0,
7493 leency 52
0xFDF6E3, 0x101A21,
53
0xFCF0DA, 0x171501,
7466 leency 54
0xF0F0C7, 0,
55
0x282C34, 0xABB2BF,
56
0x282923, 0xD8D8D2
57
};
58
 
59
char color_scheme_names[] =
60
"White & Black
61
Grey & Black      RtfRead
7493 leency 62
Linen & Black     Horst
63
Antique & Black   Pocket
7466 leency 64
Lemon & Black     Fb2Read
65
DarkGrey & Grey   Godot
66
DarkGrey & Grey   Monokai";
67
 
5819 leency 68
char default_dir[] = "/rd/1";
7286 leency 69
od_filter filter2 = { 8, "TXT\0\0" };
5819 leency 70
 
71
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};
72
llist list;
73
 
74
proc_info Form;
75
char title[4196];
76
 
7218 leency 77
bool help_opened = false;
7466 leency 78
int charsets_mx;
79
int reopenin_mx;
80
int colscheme_mx;
5819 leency 81
 
7466 leency 82
int curcol_scheme;
83
 
5819 leency 84
enum {
5962 leency 85
	OPEN_FILE,
5819 leency 86
	MAGNIFY_MINUS,
87
	MAGNIFY_PLUS,
88
	CHANGE_ENCODING,
89
	RUN_EDIT,
90
	SHOW_INFO,
7466 leency 91
	SHOW_FILE_PROPERTIES,
92
	COLOR_SCHEME
5819 leency 93
};
94
 
7051 leency 95
int encoding;
96
 
7466 leency 97
dword bg_color;
98
dword text_color;
7462 leency 99
 
7466 leency 100
//===================================================//
101
//                                                   //
102
//                 INTERNAL INCLUDES                 //
103
//                                                   //
104
//===================================================//
105
 
5819 leency 106
#include "ini.h"
5980 leency 107
#include "prepare_page.h"
5819 leency 108
 
7466 leency 109
//===================================================//
110
//                                                   //
111
//                       CODE                        //
112
//                                                   //
113
//===================================================//
114
 
5923 leency 115
void InitDlls()
116
{
117
	load_dll(boxlib,    #box_lib_init,   0);
118
	load_dll(libio,     #libio_init,     1);
119
	load_dll(libimg,    #libimg_init,    1);
120
	load_dll(libini,    #lib_init,       1);
121
	load_dll(iconv_lib, #iconv_open,     0);
122
	load_dll(Proc_lib,  #OpenDialog_init,0);
123
}
124
 
5819 leency 125
void main()
5923 leency 126
{
5987 leency 127
	InitDlls();
5819 leency 128
	OpenDialog_init stdcall (#o_dialog);
7286 leency 129
	LoadIniSettings();
7466 leency 130
	EventSetColorScheme(curcol_scheme);
6806 leency 131
	kfont.init(DEFAULT_FONT);
5819 leency 132
	Libimg_LoadImage(#skin, abspath("toolbar.png"));
133
	OpenFile(#param);
134
	list.no_selection = true;
7227 leency 135
	SetEventMask(EVM_REDRAW + EVM_KEY + EVM_BUTTON + EVM_MOUSE + EVM_MOUSE_FILTER);
5819 leency 136
	loop()
137
	{
5923 leency 138
		switch(WaitEvent())
139
		{
140
			case evMouse:
141
				HandleMouseEvent();
5819 leency 142
				break;
5923 leency 143
			case evKey:
144
				HandleKeyEvent();
145
				break;
146
			case evButton:
147
				HandleButtonEvent();
148
				break;
149
			case evReDraw:
7462 leency 150
				EventMenuClick();
5923 leency 151
				draw_window();
152
		}
5819 leency 153
	}
154
}
155
 
7466 leency 156
//===================================================//
157
//                                                   //
158
//                      EVENTS                       //
159
//                                                   //
160
//===================================================//
5980 leency 161
 
162
void HandleButtonEvent()
5819 leency 163
{
5923 leency 164
 
5980 leency 165
	byte btn = GetButtonID();
166
	if (btn==1) {
167
		SaveIniSettings();
168
		ExitProcess();
169
	}
170
	btn-=10;
171
	switch(btn)
5923 leency 172
	{
5980 leency 173
		case OPEN_FILE:
174
			EventOpenFile();
175
			break;
7218 leency 176
		case SHOW_FILE_PROPERTIES:
177
			EventShowFileProperties();
178
			break;
5980 leency 179
		case MAGNIFY_PLUS:
180
			EventMagnifyPlus();
181
			break;
182
		case MAGNIFY_MINUS:
183
			EventMagnifyMinus();
184
			break;
185
		case CHANGE_ENCODING:
7462 leency 186
			EventShowEncodingList();
5980 leency 187
			break;
188
		case RUN_EDIT:
7466 leency 189
			EventShowReopenMenu();
5980 leency 190
			break;
7466 leency 191
		case COLOR_SCHEME:
192
			EventShowColorSchemesList();
193
			break;
5980 leency 194
		case SHOW_INFO:
195
			EventShowInfo();
196
			break;
5923 leency 197
	}
5819 leency 198
}
199
 
200
 
5980 leency 201
void HandleKeyEvent()
5819 leency 202
{
5980 leency 203
	if (help_opened) {
204
		help_opened = false;
205
		DrawPage();
206
		return;
207
	}
208
	GetKeys();
7218 leency 209
	if (key_scancode == SCAN_CODE_F1) {
5980 leency 210
		EventShowInfo();
211
		return;
212
	}
213
	if (key_modifier & KEY_LCTRL) || (key_modifier & KEY_RCTRL) {
214
		switch (key_scancode)
215
		{
7218 leency 216
			case SCAN_CODE_KEY_O:
5980 leency 217
				EventOpenFile();
218
				break;
7218 leency 219
			case SCAN_CODE_KEY_I:
220
				EventShowFileProperties();
221
				break;
5980 leency 222
			case SCAN_CODE_UP:
223
				EventMagnifyPlus();
224
				break;
225
			case SCAN_CODE_DOWN:
226
				EventMagnifyMinus();
227
				break;
7218 leency 228
			case SCAN_CODE_KEY_E:
7466 leency 229
				EventShowReopenMenu();
5980 leency 230
				break;
231
			case SCAN_CODE_TAB:
232
				EventChangeEncoding();
233
				break;
5819 leency 234
		}
5980 leency 235
		return;
5819 leency 236
	}
5980 leency 237
	if (list.ProcessKey(key_scancode))
238
		DrawPage();
239
}
5819 leency 240
 
241
 
5980 leency 242
void HandleMouseEvent()
243
{
244
	mouse.get();
245
	list.wheel_size = 7;
246
	if (list.MouseScroll(mouse.vert)) {
247
		DrawPage();
248
		return;
5819 leency 249
	}
5980 leency 250
	scrollbar_v_mouse (#scroll);
251
	if (list.first != scroll.position) {
252
		list.first = scroll.position;
253
		DrawPage();
254
	}
5819 leency 255
}
256
 
5980 leency 257
/* ----------------------------------------------------- */
258
 
259
void EventOpenFile()
5819 leency 260
{
5980 leency 261
	OpenDialog_start stdcall (#o_dialog);
7286 leency 262
	if (o_dialog.status) {
263
		OpenFile(#openfile_path);
264
		PreparePage();
265
	}
5819 leency 266
}
267
 
7218 leency 268
void EventShowFileProperties()
269
{
270
	char ss_param[4096];
271
	if (!param) return;
272
	sprintf(#ss_param, "-p %s", #param);
7462 leency 273
	RunProgram("/sys/File managers/Eolite", #ss_param);
7218 leency 274
}
275
 
5980 leency 276
void EventMagnifyPlus()
5819 leency 277
{
6806 leency 278
	kfont.size.pt++;
279
	if(!kfont.changeSIZE())
280
		kfont.size.pt--;
5980 leency 281
	else
282
		PreparePage();
5819 leency 283
}
284
 
5980 leency 285
void EventMagnifyMinus()
286
{
6806 leency 287
	kfont.size.pt--;
288
	if(!kfont.changeSIZE())
289
		kfont.size.pt++;
5980 leency 290
	else
291
		PreparePage();
292
}
293
 
7466 leency 294
void EventShowEncodingList()
5980 leency 295
{
7466 leency 296
	menu.selected = encoding + 1;
297
	menu.show(Form.left+5 + charsets_mx, Form.top+29+skin_height, 130,
298
		"UTF-8\nKOI8-RU\nCP1251\nCP1252\nISO8859-5\nCP866", 10);
299
}
300
 
301
void EventShowReopenMenu()
302
{
7462 leency 303
	menu.selected = 0;
7466 leency 304
	menu.show(Form.left+5 + reopenin_mx, Form.top+29+skin_height, 130,
305
		"Tinypad\nTextEdit\nWebView\nFB2Read\nHexView\nOther", 20);
5980 leency 306
}
307
 
7466 leency 308
void EventShowColorSchemesList()
5980 leency 309
{
7466 leency 310
	menu.selected = curcol_scheme + 1;
311
	menu.show(Form.left+5 + colscheme_mx, Form.top+29+skin_height, 175, #color_scheme_names, 30);
5980 leency 312
}
313
 
7466 leency 314
void EventSetColorScheme(dword _setn)
315
{
316
	curcol_scheme = _setn;
317
	bg_color   = color_schemes[curcol_scheme*2];
318
	text_color = color_schemes[curcol_scheme*2+1];
319
}
320
 
5980 leency 321
void EventShowInfo() {
322
	help_opened = true;
323
	DrawBar(list.x, list.y, list.w, list.h, 0xFFFfff);
324
	WriteText(list.x + 10, list.y + 10, 10000001b, 0x555555, VERSION);
325
	WriteTextLines(list.x + 10, list.y+40, 10110000b, 0, ABOUT, 20);
326
}
327
 
7462 leency 328
void EventChangeEncoding(dword id)
329
{
330
	encoding = id;
331
	OpenFile(#openfile_path);
332
	PreparePage();
333
	draw_window();
334
}
335
 
336
void EventOpenFileInAnotherProgram(dword _app)
337
{
338
	RunProgram(_app, #param);
339
}
340
 
341
void EventMenuClick()
342
{
7466 leency 343
	byte open_param[4096];
7462 leency 344
	switch(menu.cur_y)
345
	{
346
		//Encoding
347
		case 10...15:
348
			EventChangeEncoding(menu.cur_y-10);
349
			break;
350
		//Reopen
351
		case 20:
352
			EventOpenFileInAnotherProgram("/sys/tinypad");
353
			break;
354
		case 21:
355
			EventOpenFileInAnotherProgram("/sys/develop/t_edit");
356
			break;
357
		case 22:
358
			EventOpenFileInAnotherProgram("/sys/network/webview");
359
			break;
360
		case 23:
361
			EventOpenFileInAnotherProgram("/sys/fb2read");
362
			break;
363
		case 24:
364
			EventOpenFileInAnotherProgram("/sys/develop/heed");
365
			break;
7466 leency 366
		case 25:
367
			sprintf(#open_param,"~%s",#param);
368
			RunProgram("/sys/@open", #open_param);
369
			break;
370
		//ColorSchemes
371
		case 30...38:
372
			EventSetColorScheme(menu.cur_y-30);
373
			PreparePage();
374
			break;
7462 leency 375
	}
376
	menu.cur_y = 0;
377
}
378
 
7466 leency 379
//===================================================//
380
//                                                   //
381
//               DRAWS AND OTHER FUNCS               //
382
//                                                   //
383
//===================================================//
5980 leency 384
 
5819 leency 385
void OpenFile(dword f_path)
386
{
387
	int tmp;
388
	if (ESBYTE[f_path]) {
389
		strcpy(#param, f_path);
390
		io.read(#param);
391
		strcpy(#title, #param);
392
		strcat(#title, " - Text Reader");
393
	}
394
	else {
395
		if (list.count) return;
5923 leency 396
		io.buffer_data = INTRO_TEXT;
5819 leency 397
		strcpy(#title, "Text Reader");
398
	}
399
	if (encoding!=CH_CP866) ChangeCharset(charsets[encoding], "CP866", io.buffer_data);
400
	list.KeyHome();
401
	list.ClearList();
402
}
403
 
5980 leency 404
void draw_window()
405
{
7466 leency 406
	#define BUTTONS_GAP 6
407
	#define BLOCKS_GAP 15
7218 leency 408
	#define TOOLBAR_BUTTON_WIDTH 26
409
	incn x;
6746 leency 410
	DefineAndDrawWindow(Form.left,Form.top,Form.width,Form.height,0x73,0,#title,0);
5980 leency 411
	GetProcessInfo(#Form, SelfInfo);
412
	if (Form.status_window>2) return;
413
 
414
	if (Form.width  < 200) { MoveSize(OLD,OLD,200,OLD); return; }
415
	if (Form.height < 200) { MoveSize(OLD,OLD,OLD,200); return; }
416
 
417
	DrawBar(0, 0, Form.cwidth, TOOLBAR_H - 1, 0xe1e1e1);
418
	DrawBar(0, TOOLBAR_H - 1, Form.cwidth, 1, 0x7F7F7F);
419
 
7218 leency 420
	x.n = 0;
421
	DrawToolbarButton(OPEN_FILE,       x.inc(8));
7466 leency 422
	DrawToolbarButton(SHOW_FILE_PROPERTIES, x.inc(TOOLBAR_BUTTON_WIDTH + BUTTONS_GAP));
423
 
424
	DrawToolbarButton(MAGNIFY_MINUS,   x.inc(TOOLBAR_BUTTON_WIDTH + BLOCKS_GAP));
7428 leency 425
	DrawToolbarButton(MAGNIFY_PLUS,    x.inc(TOOLBAR_BUTTON_WIDTH - 1));
7466 leency 426
	DrawToolbarButton(COLOR_SCHEME,    x.inc(TOOLBAR_BUTTON_WIDTH + BUTTONS_GAP)); colscheme_mx = x.n;
427
 
428
	DrawToolbarButton(CHANGE_ENCODING, x.inc(TOOLBAR_BUTTON_WIDTH + BLOCKS_GAP)); charsets_mx = x.n;
429
	DrawToolbarButton(RUN_EDIT,        x.inc(TOOLBAR_BUTTON_WIDTH + BLOCKS_GAP)); reopenin_mx = x.n;
5980 leency 430
	DrawToolbarButton(SHOW_INFO,       Form.cwidth - 34);
431
 
432
	if ((Form.cwidth-scroll.size_x-1 == list.w) &&
433
		(Form.cheight-TOOLBAR_H == list.h) &&
434
		(list.count)
435
	)
436
	{
437
		DrawPage();
438
	} else {
439
		PreparePage();
7466 leency 440
	}
441
 
5980 leency 442
	DrawRectangle(scroll.start_x, scroll.start_y, scroll.size_x, scroll.size_y-1, scroll.bckg_col);
5923 leency 443
}
5980 leency 444
 
445
void DrawPage()
446
{
6808 leency 447
	kfont.ShowBufferPart(list.x, list.y, list.w, list.h, list.first*list.item_h*list.w);
5980 leency 448
	DrawScroller();
7286 leency 449
}
450
 
451
void DrawToolbarButton(char image_id, int x)
452
{
453
	DefineButton(x+1, 6, TOOLBAR_ICON_WIDTH-2, TOOLBAR_ICON_HEIGHT-2, 10+image_id + BT_HIDE, 0);
7428 leency 454
	img_draw stdcall(skin.image, x, 5, TOOLBAR_ICON_WIDTH, TOOLBAR_ICON_HEIGHT, TOOLBAR_ICON_WIDTH-1*image_id, 0);
7286 leency 455
}
456
 
457
void DrawScroller()
458
{
459
	scroll.max_area = list.count;
460
	scroll.cur_area = list.visible;
461
	scroll.position = list.first;
462
	scroll.all_redraw = 0;
463
	scroll.start_x = list.x + list.w;
464
	scroll.start_y = list.y;
465
	scroll.size_y = list.h;
466
	scrollbar_v_draw(#scroll);
5980 leency 467
}