Subversion Repositories Kolibri OS

Rev

Rev 7782 | 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
7784 leency 29
#define TOOLBAR_ICON_WIDTH  24
30
#define TOOLBAR_ICON_HEIGHT 22
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
 
7781 leency 100
dword menu_id;
101
 
7466 leency 102
//===================================================//
103
//                                                   //
104
//                 INTERNAL INCLUDES                 //
105
//                                                   //
106
//===================================================//
107
 
5819 leency 108
#include "ini.h"
5980 leency 109
#include "prepare_page.h"
5819 leency 110
 
7466 leency 111
//===================================================//
112
//                                                   //
113
//                       CODE                        //
114
//                                                   //
115
//===================================================//
116
 
5923 leency 117
void InitDlls()
118
{
119
	load_dll(boxlib,    #box_lib_init,   0);
120
	load_dll(libio,     #libio_init,     1);
121
	load_dll(libimg,    #libimg_init,    1);
122
	load_dll(libini,    #lib_init,       1);
123
	load_dll(iconv_lib, #iconv_open,     0);
124
	load_dll(Proc_lib,  #OpenDialog_init,0);
125
}
126
 
5819 leency 127
void main()
5923 leency 128
{
5987 leency 129
	InitDlls();
5819 leency 130
	OpenDialog_init stdcall (#o_dialog);
7516 leency 131
	if (param) strcpy(#openfile_path, #param);
7286 leency 132
	LoadIniSettings();
7466 leency 133
	EventSetColorScheme(curcol_scheme);
6806 leency 134
	kfont.init(DEFAULT_FONT);
5819 leency 135
	Libimg_LoadImage(#skin, abspath("toolbar.png"));
136
	OpenFile(#param);
137
	list.no_selection = true;
7227 leency 138
	SetEventMask(EVM_REDRAW + EVM_KEY + EVM_BUTTON + EVM_MOUSE + EVM_MOUSE_FILTER);
5819 leency 139
	loop()
140
	{
5923 leency 141
		switch(WaitEvent())
142
		{
143
			case evMouse:
144
				HandleMouseEvent();
5819 leency 145
				break;
5923 leency 146
			case evKey:
147
				HandleKeyEvent();
148
				break;
149
			case evButton:
150
				HandleButtonEvent();
151
				break;
152
			case evReDraw:
7781 leency 153
				if (CheckActiveProcess(Form.ID)) EventMenuClick();
5923 leency 154
				draw_window();
155
		}
5819 leency 156
	}
157
}
158
 
7466 leency 159
//===================================================//
160
//                                                   //
161
//                      EVENTS                       //
162
//                                                   //
163
//===================================================//
5980 leency 164
 
165
void HandleButtonEvent()
5819 leency 166
{
5923 leency 167
 
5980 leency 168
	byte btn = GetButtonID();
169
	if (btn==1) {
170
		SaveIniSettings();
171
		ExitProcess();
172
	}
173
	btn-=10;
174
	switch(btn)
5923 leency 175
	{
5980 leency 176
		case OPEN_FILE:
177
			EventOpenFile();
178
			break;
7218 leency 179
		case SHOW_FILE_PROPERTIES:
180
			EventShowFileProperties();
181
			break;
5980 leency 182
		case MAGNIFY_PLUS:
183
			EventMagnifyPlus();
184
			break;
185
		case MAGNIFY_MINUS:
186
			EventMagnifyMinus();
187
			break;
188
		case CHANGE_ENCODING:
7462 leency 189
			EventShowEncodingList();
5980 leency 190
			break;
191
		case RUN_EDIT:
7466 leency 192
			EventShowReopenMenu();
5980 leency 193
			break;
7466 leency 194
		case COLOR_SCHEME:
195
			EventShowColorSchemesList();
196
			break;
5980 leency 197
		case SHOW_INFO:
198
			EventShowInfo();
199
			break;
5923 leency 200
	}
5819 leency 201
}
202
 
203
 
5980 leency 204
void HandleKeyEvent()
5819 leency 205
{
5980 leency 206
	if (help_opened) {
207
		help_opened = false;
208
		DrawPage();
209
		return;
210
	}
211
	GetKeys();
7218 leency 212
	if (key_scancode == SCAN_CODE_F1) {
5980 leency 213
		EventShowInfo();
214
		return;
215
	}
216
	if (key_modifier & KEY_LCTRL) || (key_modifier & KEY_RCTRL) {
217
		switch (key_scancode)
218
		{
7218 leency 219
			case SCAN_CODE_KEY_O:
5980 leency 220
				EventOpenFile();
221
				break;
7218 leency 222
			case SCAN_CODE_KEY_I:
223
				EventShowFileProperties();
224
				break;
5980 leency 225
			case SCAN_CODE_UP:
226
				EventMagnifyPlus();
227
				break;
228
			case SCAN_CODE_DOWN:
229
				EventMagnifyMinus();
230
				break;
7218 leency 231
			case SCAN_CODE_KEY_E:
7466 leency 232
				EventShowReopenMenu();
5980 leency 233
				break;
234
			case SCAN_CODE_TAB:
235
				EventChangeEncoding();
236
				break;
5819 leency 237
		}
5980 leency 238
		return;
5819 leency 239
	}
5980 leency 240
	if (list.ProcessKey(key_scancode))
241
		DrawPage();
242
}
5819 leency 243
 
244
 
5980 leency 245
void HandleMouseEvent()
246
{
247
	mouse.get();
248
	list.wheel_size = 7;
249
	if (list.MouseScroll(mouse.vert)) {
250
		DrawPage();
251
		return;
5819 leency 252
	}
5980 leency 253
	scrollbar_v_mouse (#scroll);
254
	if (list.first != scroll.position) {
255
		list.first = scroll.position;
256
		DrawPage();
257
	}
5819 leency 258
}
259
 
5980 leency 260
/* ----------------------------------------------------- */
261
 
262
void EventOpenFile()
5819 leency 263
{
5980 leency 264
	OpenDialog_start stdcall (#o_dialog);
7286 leency 265
	if (o_dialog.status) {
266
		OpenFile(#openfile_path);
267
		PreparePage();
268
	}
5819 leency 269
}
270
 
7218 leency 271
void EventShowFileProperties()
272
{
273
	char ss_param[4096];
274
	if (!param) return;
7768 leency 275
	strcpy(#ss_param, "-p ");
276
	strcpy(#ss_param+3, #param);
7462 leency 277
	RunProgram("/sys/File managers/Eolite", #ss_param);
7218 leency 278
}
279
 
5980 leency 280
void EventMagnifyPlus()
5819 leency 281
{
6806 leency 282
	kfont.size.pt++;
283
	if(!kfont.changeSIZE())
284
		kfont.size.pt--;
5980 leency 285
	else
286
		PreparePage();
5819 leency 287
}
288
 
5980 leency 289
void EventMagnifyMinus()
290
{
6806 leency 291
	kfont.size.pt--;
292
	if(!kfont.changeSIZE())
293
		kfont.size.pt++;
5980 leency 294
	else
295
		PreparePage();
296
}
297
 
7466 leency 298
void EventShowEncodingList()
5980 leency 299
{
7782 leency 300
	open_lmenu(Form.left+5 + charsets_mx, Form.top+29+skin_height, MENU_ALIGN_TOP_LEFT,
301
		encoding+1, "UTF-8\nKOI8-RU\nCP1251\nCP1252\nISO8859-5\nCP866");
7781 leency 302
	menu_id = 10;
7466 leency 303
}
304
 
305
void EventShowReopenMenu()
306
{
7781 leency 307
	open_lmenu(Form.left+5 + reopenin_mx, Form.top+29+skin_height, MENU_ALIGN_TOP_LEFT, 0,
308
		"Tinypad\nTextEdit\nWebView\nFB2Read\nHexView\nOther");
309
	menu_id = 20;
5980 leency 310
}
311
 
7466 leency 312
void EventShowColorSchemesList()
5980 leency 313
{
7781 leency 314
	open_lmenu(Form.left+5 + colscheme_mx, Form.top+29+skin_height, MENU_ALIGN_TOP_LEFT,
315
		curcol_scheme+1, #color_scheme_names);
316
	menu_id = 30;
5980 leency 317
}
318
 
7466 leency 319
void EventSetColorScheme(dword _setn)
320
{
321
	curcol_scheme = _setn;
322
	bg_color   = color_schemes[curcol_scheme*2];
323
	text_color = color_schemes[curcol_scheme*2+1];
324
}
325
 
5980 leency 326
void EventShowInfo() {
327
	help_opened = true;
328
	DrawBar(list.x, list.y, list.w, list.h, 0xFFFfff);
329
	WriteText(list.x + 10, list.y + 10, 10000001b, 0x555555, VERSION);
330
	WriteTextLines(list.x + 10, list.y+40, 10110000b, 0, ABOUT, 20);
331
}
332
 
7462 leency 333
void EventChangeEncoding(dword id)
334
{
335
	encoding = id;
336
	OpenFile(#openfile_path);
337
	PreparePage();
338
	draw_window();
339
}
340
 
341
void EventOpenFileInAnotherProgram(dword _app)
342
{
343
	RunProgram(_app, #param);
344
}
345
 
346
void EventMenuClick()
347
{
7466 leency 348
	byte open_param[4096];
7781 leency 349
	dword click_id = get_menu_click();
350
	if (click_id) switch(click_id + menu_id - 1)
7462 leency 351
	{
352
		//Encoding
353
		case 10...15:
7781 leency 354
			EventChangeEncoding(click_id-1);
7462 leency 355
			break;
356
		//Reopen
357
		case 20:
358
			EventOpenFileInAnotherProgram("/sys/tinypad");
359
			break;
360
		case 21:
361
			EventOpenFileInAnotherProgram("/sys/develop/t_edit");
362
			break;
363
		case 22:
364
			EventOpenFileInAnotherProgram("/sys/network/webview");
365
			break;
366
		case 23:
367
			EventOpenFileInAnotherProgram("/sys/fb2read");
368
			break;
369
		case 24:
370
			EventOpenFileInAnotherProgram("/sys/develop/heed");
371
			break;
7466 leency 372
		case 25:
7768 leency 373
			open_param[0]='~';
374
			strcpy(#open_param+1,#param);
7466 leency 375
			RunProgram("/sys/@open", #open_param);
376
			break;
377
		//ColorSchemes
378
		case 30...38:
7781 leency 379
			EventSetColorScheme(click_id-1);
7466 leency 380
			PreparePage();
381
			break;
7462 leency 382
	}
383
}
384
 
7466 leency 385
//===================================================//
386
//                                                   //
387
//               DRAWS AND OTHER FUNCS               //
388
//                                                   //
389
//===================================================//
5980 leency 390
 
5819 leency 391
void OpenFile(dword f_path)
392
{
393
	int tmp;
394
	if (ESBYTE[f_path]) {
395
		strcpy(#param, f_path);
396
		io.read(#param);
397
		strcpy(#title, #param);
398
		strcat(#title, " - Text Reader");
399
	}
400
	else {
401
		if (list.count) return;
5923 leency 402
		io.buffer_data = INTRO_TEXT;
5819 leency 403
		strcpy(#title, "Text Reader");
404
	}
7781 leency 405
	if (encoding!=CH_CP866) ChangeCharset(encoding, "CP866", io.buffer_data);
5819 leency 406
	list.KeyHome();
407
	list.ClearList();
408
}
409
 
5980 leency 410
void draw_window()
411
{
7466 leency 412
	#define BUTTONS_GAP 6
413
	#define BLOCKS_GAP 15
7218 leency 414
	#define TOOLBAR_BUTTON_WIDTH 26
415
	incn x;
6746 leency 416
	DefineAndDrawWindow(Form.left,Form.top,Form.width,Form.height,0x73,0,#title,0);
5980 leency 417
	GetProcessInfo(#Form, SelfInfo);
7784 leency 418
	system.color.get();
5980 leency 419
	if (Form.status_window>2) return;
420
 
421
	if (Form.width  < 200) { MoveSize(OLD,OLD,200,OLD); return; }
422
	if (Form.height < 200) { MoveSize(OLD,OLD,OLD,200); return; }
423
 
7784 leency 424
	DrawBar(0, 0, Form.cwidth, TOOLBAR_H - 1, system.color.work);
425
	DrawBar(0, TOOLBAR_H - 1, Form.cwidth, 1, system.color.work_graph);
5980 leency 426
 
7218 leency 427
	x.n = 0;
428
	DrawToolbarButton(OPEN_FILE,       x.inc(8));
7466 leency 429
	DrawToolbarButton(SHOW_FILE_PROPERTIES, x.inc(TOOLBAR_BUTTON_WIDTH + BUTTONS_GAP));
430
 
431
	DrawToolbarButton(MAGNIFY_MINUS,   x.inc(TOOLBAR_BUTTON_WIDTH + BLOCKS_GAP));
7428 leency 432
	DrawToolbarButton(MAGNIFY_PLUS,    x.inc(TOOLBAR_BUTTON_WIDTH - 1));
7466 leency 433
	DrawToolbarButton(COLOR_SCHEME,    x.inc(TOOLBAR_BUTTON_WIDTH + BUTTONS_GAP)); colscheme_mx = x.n;
434
 
435
	DrawToolbarButton(CHANGE_ENCODING, x.inc(TOOLBAR_BUTTON_WIDTH + BLOCKS_GAP)); charsets_mx = x.n;
436
	DrawToolbarButton(RUN_EDIT,        x.inc(TOOLBAR_BUTTON_WIDTH + BLOCKS_GAP)); reopenin_mx = x.n;
5980 leency 437
	DrawToolbarButton(SHOW_INFO,       Form.cwidth - 34);
438
 
439
	if ((Form.cwidth-scroll.size_x-1 == list.w) &&
440
		(Form.cheight-TOOLBAR_H == list.h) &&
441
		(list.count)
442
	)
443
	{
444
		DrawPage();
445
	} else {
446
		PreparePage();
7466 leency 447
	}
448
 
5980 leency 449
	DrawRectangle(scroll.start_x, scroll.start_y, scroll.size_x, scroll.size_y-1, scroll.bckg_col);
5923 leency 450
}
5980 leency 451
 
452
void DrawPage()
453
{
6808 leency 454
	kfont.ShowBufferPart(list.x, list.y, list.w, list.h, list.first*list.item_h*list.w);
5980 leency 455
	DrawScroller();
7286 leency 456
}
457
 
458
void DrawToolbarButton(char image_id, int x)
459
{
7784 leency 460
	DefineButton(x+1, 6, TOOLBAR_ICON_WIDTH, TOOLBAR_ICON_HEIGHT, 10+image_id + BT_HIDE, 0);
461
	//img_draw stdcall(skin.image, x, 5, TOOLBAR_ICON_WIDTH, TOOLBAR_ICON_HEIGHT, TOOLBAR_ICON_WIDTH-1*image_id, 0);
462
	DrawOvalBorder(x, 5, TOOLBAR_ICON_WIDTH, TOOLBAR_ICON_HEIGHT, system.color.work_graph,
463
		system.color.work_graph,system.color.work_graph, system.color.work_dark);
464
	img_draw stdcall(skin.image, x+1, 5+1, TOOLBAR_ICON_WIDTH, TOOLBAR_ICON_HEIGHT, TOOLBAR_ICON_WIDTH*image_id, 0);
7286 leency 465
}
466
 
467
void DrawScroller()
468
{
469
	scroll.max_area = list.count;
470
	scroll.cur_area = list.visible;
471
	scroll.position = list.first;
472
	scroll.all_redraw = 0;
473
	scroll.start_x = list.x + list.w;
474
	scroll.start_y = list.y;
475
	scroll.size_y = list.h;
476
	scrollbar_v_draw(#scroll);
5980 leency 477
}