Subversion Repositories Kolibri OS

Rev

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

Rev Author Line No. Line
7818 leency 1
#define MEMSIZE 1024*80
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
//                                                   //
7818 leency 24
//                 INTERNAL INCLUDES                 //
7466 leency 25
//                                                   //
26
//===================================================//
27
 
7818 leency 28
proc_info Form;
29
llist list;
30
 
5819 leency 31
#define TOOLBAR_H 34
7784 leency 32
#define TOOLBAR_ICON_WIDTH  24
33
#define TOOLBAR_ICON_HEIGHT 22
5819 leency 34
 
7818 leency 35
dword bg_color;
36
dword text_color;
5923 leency 37
 
7818 leency 38
#include "search.h"
39
#include "prepare_page.h"
40
#include "data.h"
5923 leency 41
 
7818 leency 42
int encoding;
43
int curcol_scheme;
5962 leency 44
 
7818 leency 45
#include "ini.h"
7466 leency 46
 
7818 leency 47
//===================================================//
48
//                                                   //
49
//                       DATA                        //
50
//                                                   //
51
//===================================================//
7466 leency 52
 
5819 leency 53
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};
54
 
55
char title[4196];
56
 
7218 leency 57
bool help_opened = false;
5819 leency 58
 
7818 leency 59
int charsets_mx,
60
    reopenin_mx,
61
    colscheme_mx,
62
    search_mx;
7466 leency 63
 
5819 leency 64
enum {
5962 leency 65
	OPEN_FILE,
5819 leency 66
	MAGNIFY_MINUS,
67
	MAGNIFY_PLUS,
7818 leency 68
	CHANGE_CHARSET,
69
	REOPEN_IN_APP,
5819 leency 70
	SHOW_INFO,
7466 leency 71
	SHOW_FILE_PROPERTIES,
7818 leency 72
	COLOR_SCHEME,
73
	SEARCH_BTN,
74
	BTN_FIND_NEXT,
75
	BTN_FIND_CLOSE
5819 leency 76
};
77
 
7781 leency 78
dword menu_id;
79
 
7466 leency 80
//===================================================//
81
//                                                   //
82
//                       CODE                        //
83
//                                                   //
84
//===================================================//
85
 
5923 leency 86
void InitDlls()
87
{
88
	load_dll(boxlib,    #box_lib_init,   0);
89
	load_dll(libio,     #libio_init,     1);
90
	load_dll(libimg,    #libimg_init,    1);
91
	load_dll(libini,    #lib_init,       1);
92
	load_dll(iconv_lib, #iconv_open,     0);
93
	load_dll(Proc_lib,  #OpenDialog_init,0);
94
}
95
 
5819 leency 96
void main()
5923 leency 97
{
5987 leency 98
	InitDlls();
5819 leency 99
	OpenDialog_init stdcall (#o_dialog);
7516 leency 100
	if (param) strcpy(#openfile_path, #param);
7286 leency 101
	LoadIniSettings();
7466 leency 102
	EventSetColorScheme(curcol_scheme);
6806 leency 103
	kfont.init(DEFAULT_FONT);
5819 leency 104
	Libimg_LoadImage(#skin, abspath("toolbar.png"));
7818 leency 105
	LoadFile(#param);
5819 leency 106
	list.no_selection = true;
7227 leency 107
	SetEventMask(EVM_REDRAW + EVM_KEY + EVM_BUTTON + EVM_MOUSE + EVM_MOUSE_FILTER);
5819 leency 108
	loop()
109
	{
5923 leency 110
		switch(WaitEvent())
111
		{
112
			case evMouse:
113
				HandleMouseEvent();
5819 leency 114
				break;
5923 leency 115
			case evKey:
116
				HandleKeyEvent();
117
				break;
118
			case evButton:
119
				HandleButtonEvent();
120
				break;
121
			case evReDraw:
7781 leency 122
				if (CheckActiveProcess(Form.ID)) EventMenuClick();
5923 leency 123
				draw_window();
124
		}
5819 leency 125
	}
126
}
127
 
7466 leency 128
//===================================================//
129
//                                                   //
7818 leency 130
//                  EVENT HANDLERS                   //
7466 leency 131
//                                                   //
132
//===================================================//
5980 leency 133
 
134
void HandleButtonEvent()
5819 leency 135
{
7818 leency 136
	int btn = GetButtonID();
5980 leency 137
	if (btn==1) {
138
		SaveIniSettings();
139
		ExitProcess();
140
	}
7818 leency 141
	switch(btn-10)
5923 leency 142
	{
5980 leency 143
		case OPEN_FILE:
7818 leency 144
			EventOpenDialog();
5980 leency 145
			break;
7218 leency 146
		case SHOW_FILE_PROPERTIES:
147
			EventShowFileProperties();
148
			break;
5980 leency 149
		case MAGNIFY_PLUS:
150
			EventMagnifyPlus();
151
			break;
152
		case MAGNIFY_MINUS:
153
			EventMagnifyMinus();
154
			break;
7818 leency 155
		case CHANGE_CHARSET:
156
			EventShowCharsetsList();
5980 leency 157
			break;
7818 leency 158
		case REOPEN_IN_APP:
7466 leency 159
			EventShowReopenMenu();
5980 leency 160
			break;
7466 leency 161
		case COLOR_SCHEME:
162
			EventShowColorSchemesList();
163
			break;
5980 leency 164
		case SHOW_INFO:
165
			EventShowInfo();
166
			break;
7818 leency 167
		case SEARCH_BTN:
168
			if (search.visible) {
169
				search.hide();
170
			} else {
171
				search.show();
172
			}
173
			break;
174
		case BTN_FIND_NEXT:
175
			EventSearchNext();
176
			break;
177
		case BTN_FIND_CLOSE:
178
			search.hide();
179
			break;
5923 leency 180
	}
5819 leency 181
}
182
 
5980 leency 183
void HandleKeyEvent()
5819 leency 184
{
7818 leency 185
	int new_y;
5980 leency 186
	if (help_opened) {
187
		help_opened = false;
188
		DrawPage();
189
		return;
190
	}
191
	GetKeys();
192
	if (key_modifier & KEY_LCTRL) || (key_modifier & KEY_RCTRL) {
193
		switch (key_scancode)
194
		{
7218 leency 195
			case SCAN_CODE_KEY_O:
7818 leency 196
				EventOpenDialog();
197
				return;
7218 leency 198
			case SCAN_CODE_KEY_I:
199
				EventShowFileProperties();
7818 leency 200
				return;
7798 leency 201
			case SCAN_CODE_PLUS:
5980 leency 202
			case SCAN_CODE_UP:
203
				EventMagnifyPlus();
7818 leency 204
				return;
5980 leency 205
			case SCAN_CODE_DOWN:
7798 leency 206
			case SCAN_CODE_MINUS:
5980 leency 207
				EventMagnifyMinus();
7818 leency 208
				return;
7218 leency 209
			case SCAN_CODE_KEY_E:
7466 leency 210
				EventShowReopenMenu();
7818 leency 211
				return;
5980 leency 212
			case SCAN_CODE_TAB:
7818 leency 213
				EventShowCharsetsList();
214
				return;
215
			case SCAN_CODE_KEY_F:
216
				search.show();
217
				return;
218
			case SCAN_CODE_HOME:
219
				list.KeyHome();
220
				DrawPage();
5819 leency 221
		}
222
	}
7818 leency 223
	switch (key_scancode)
224
	{
225
		case SCAN_CODE_F1:
226
			EventShowInfo();
227
			return;
228
		case SCAN_CODE_ESC:
229
			search.hide();
230
			return;
231
		case SCAN_CODE_ENTER:
232
			if (! search_box.flags & ed_focus) return;
233
		case SCAN_CODE_F3:
234
			EventSearchNext();
235
			return;
236
	}
237
	if (!search.edit_key()) {
238
		if (list.ProcessKey(key_scancode)) DrawPage();
239
	}
5980 leency 240
}
5819 leency 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
	}
7818 leency 255
	search.edit_mouse();
5819 leency 256
}
257
 
7818 leency 258
//===================================================//
259
//                                                   //
260
//                      EVENTS                       //
261
//                                                   //
262
//===================================================//
5980 leency 263
 
7818 leency 264
bool EventSearchNext()
5819 leency 265
{
7818 leency 266
	int new_y = search.find_next(list.first, bg_color);
267
	if (new_y) {
268
		list.first = new_y / list.item_h;
269
		list.CheckDoesValuesOkey();
270
		DrawPage();
271
	}
272
}
273
 
274
void EventOpenDialog()
275
{
5980 leency 276
	OpenDialog_start stdcall (#o_dialog);
7286 leency 277
	if (o_dialog.status) {
7818 leency 278
		LoadFile(#openfile_path);
279
		search.clear();
280
		ParseAndPaint();
7286 leency 281
	}
5819 leency 282
}
283
 
7218 leency 284
void EventShowFileProperties()
285
{
286
	char ss_param[4096];
287
	if (!param) return;
7768 leency 288
	strcpy(#ss_param, "-p ");
289
	strcpy(#ss_param+3, #param);
7462 leency 290
	RunProgram("/sys/File managers/Eolite", #ss_param);
7218 leency 291
}
292
 
5980 leency 293
void EventMagnifyPlus()
5819 leency 294
{
6806 leency 295
	kfont.size.pt++;
296
	if(!kfont.changeSIZE())
297
		kfont.size.pt--;
5980 leency 298
	else
7818 leency 299
		ParseAndPaint();
5819 leency 300
}
301
 
5980 leency 302
void EventMagnifyMinus()
303
{
6806 leency 304
	kfont.size.pt--;
305
	if(!kfont.changeSIZE())
306
		kfont.size.pt++;
5980 leency 307
	else
7818 leency 308
		ParseAndPaint();
5980 leency 309
}
310
 
7818 leency 311
void EventShowCharsetsList()
5980 leency 312
{
7818 leency 313
	menu_id = CHANGE_CHARSET;
7782 leency 314
	open_lmenu(Form.left+5 + charsets_mx, Form.top+29+skin_height, MENU_ALIGN_TOP_LEFT,
7872 leency 315
		encoding+1, "UTF-8\nKOI8-RU\nCP1251\nCP1252\nISO8859-5\nCP866\nAUTO");
7466 leency 316
}
317
 
318
void EventShowReopenMenu()
319
{
7818 leency 320
	menu_id = REOPEN_IN_APP;
7781 leency 321
	open_lmenu(Form.left+5 + reopenin_mx, Form.top+29+skin_height, MENU_ALIGN_TOP_LEFT, 0,
322
		"Tinypad\nTextEdit\nWebView\nFB2Read\nHexView\nOther");
5980 leency 323
}
324
 
7466 leency 325
void EventShowColorSchemesList()
5980 leency 326
{
7818 leency 327
	menu_id = COLOR_SCHEME;
328
	open_lmenu(Form.left+2 + colscheme_mx + 26,
329
		Form.top+29+skin_height, MENU_ALIGN_TOP_RIGHT,
7781 leency 330
		curcol_scheme+1, #color_scheme_names);
5980 leency 331
}
332
 
7466 leency 333
void EventSetColorScheme(dword _setn)
334
{
335
	curcol_scheme = _setn;
336
	bg_color   = color_schemes[curcol_scheme*2];
337
	text_color = color_schemes[curcol_scheme*2+1];
7818 leency 338
	if (list.count) ParseAndPaint();
7466 leency 339
}
340
 
5980 leency 341
void EventShowInfo() {
342
	help_opened = true;
7818 leency 343
	DrawBar(list.x, list.y, list.w, list.h, bg_color);
344
	WriteText(list.x + 10, list.y + 10, 10000001b, text_color, VERSION);
7872 leency 345
	WriteTextLines(list.x + 10, list.y+40, 10010000b, text_color, ABOUT, 20);
5980 leency 346
}
347
 
7818 leency 348
void EventChangeCharset(dword id)
7462 leency 349
{
350
	encoding = id;
7818 leency 351
	LoadFile(#openfile_path);
352
	ParseAndPaint();
7462 leency 353
	draw_window();
354
}
355
 
7818 leency 356
void EventOpenFileInAnotherProgram(dword _id)
7462 leency 357
{
7818 leency 358
	dword app;
7466 leency 359
	byte open_param[4096];
7818 leency 360
	switch(_id) {
361
		case 0:
362
			app = "/sys/tinypad";
7462 leency 363
			break;
7818 leency 364
		case 1:
365
			app = "/sys/develop/t_edit";
7462 leency 366
			break;
7818 leency 367
		case 2:
368
			app = "/sys/network/webview";
7462 leency 369
			break;
7818 leency 370
		case 3:
371
			app = "/sys/fb2read";
7462 leency 372
			break;
7818 leency 373
		case 4:
374
			app = "/sys/develop/heed";
7462 leency 375
			break;
7818 leency 376
		case 5:
7768 leency 377
			open_param[0]='~';
378
			strcpy(#open_param+1,#param);
7466 leency 379
			RunProgram("/sys/@open", #open_param);
380
			break;
7462 leency 381
	}
7818 leency 382
	RunProgram(app, #param);
7462 leency 383
}
384
 
7818 leency 385
void EventMenuClick()
386
{
387
	dword click_id = get_menu_click();
388
 
389
	if (click_id) && (menu_id)
390
	{
391
		if (menu_id == CHANGE_CHARSET) EventChangeCharset(click_id-1);
392
		else if (menu_id == REOPEN_IN_APP) EventOpenFileInAnotherProgram(click_id-1);
393
		else if (menu_id == COLOR_SCHEME) EventSetColorScheme(click_id-1);
394
		else notify("'Error: wrong menu number'E");
395
	}
396
	menu_id = NULL;
397
}
398
 
7466 leency 399
//===================================================//
400
//                                                   //
401
//               DRAWS AND OTHER FUNCS               //
402
//                                                   //
403
//===================================================//
5980 leency 404
 
7818 leency 405
void LoadFile(dword f_path)
5819 leency 406
{
407
	int tmp;
408
	if (ESBYTE[f_path]) {
409
		strcpy(#param, f_path);
7818 leency 410
		if (!io.read(#param)) goto NO_DATA;
5819 leency 411
		strcpy(#title, #param);
412
		strcat(#title, " - Text Reader");
413
	}
414
	else {
7818 leency 415
		NO_DATA:
5819 leency 416
		if (list.count) return;
5923 leency 417
		io.buffer_data = INTRO_TEXT;
5819 leency 418
		strcpy(#title, "Text Reader");
419
	}
7872 leency 420
 
421
	// Autodetecting encoding
422
	if (encoding==CH_AUTO) {
423
		if (strstr(io.buffer_data, "\208\190")) ChangeCharset(CH_UTF8, "CP866", io.buffer_data);
424
		else if (chrnum(io.buffer_data, '\246')>5) || (strstr(io.buffer_data, "οπ")) ChangeCharset(CH_CP1251, "CP866", io.buffer_data);
425
	} else {
426
		if (encoding!=CH_CP866) ChangeCharset(encoding, "CP866", io.buffer_data);
427
	}
428
 
5819 leency 429
	list.ClearList();
430
}
431
 
7818 leency 432
int DrawToolbarButton(char image_id, int x)
433
{
434
	DrawOvalBorder(x, 5, TOOLBAR_ICON_WIDTH, TOOLBAR_ICON_HEIGHT, sc.work_graph,
435
		sc.work_graph,sc.work_graph, sc.work_dark);
436
	img_draw stdcall(skin.image, x+1, 5+1, TOOLBAR_ICON_WIDTH, TOOLBAR_ICON_HEIGHT, TOOLBAR_ICON_WIDTH*image_id, 0);
437
 
438
	if (menu_id) && (menu_id == image_id) {
439
		DrawRectangle3D(x+1, 6, TOOLBAR_ICON_WIDTH-1, TOOLBAR_ICON_HEIGHT-1, 0xCCCccc, 0xF8FCF8);
440
		PutShadow(x+1, 6, TOOLBAR_ICON_WIDTH, TOOLBAR_ICON_HEIGHT, true, 2);
441
	}
442
	DefineHiddenButton(x+1, 6, TOOLBAR_ICON_WIDTH, TOOLBAR_ICON_HEIGHT, 10+image_id);
443
	return x;
444
}
445
 
5980 leency 446
void draw_window()
447
{
7818 leency 448
	#define BUTTONS_GAP 5
449
	#define BLOCKS_GAP 18
7218 leency 450
	#define TOOLBAR_BUTTON_WIDTH 26
451
	incn x;
7818 leency 452
	int old_w;
6746 leency 453
	DefineAndDrawWindow(Form.left,Form.top,Form.width,Form.height,0x73,0,#title,0);
5980 leency 454
	GetProcessInfo(#Form, SelfInfo);
7806 leency 455
	sc.get();
5980 leency 456
	if (Form.status_window>2) return;
457
 
7818 leency 458
	if (Form.width  < 340) { MoveSize(OLD,OLD,340,OLD); return; }
5980 leency 459
	if (Form.height < 200) { MoveSize(OLD,OLD,OLD,200); return; }
460
 
7806 leency 461
	DrawBar(0, 0, Form.cwidth, TOOLBAR_H - 1, sc.work);
462
	DrawBar(0, TOOLBAR_H - 1, Form.cwidth, 1, sc.work_graph);
5980 leency 463
 
7218 leency 464
	x.n = 0;
465
	DrawToolbarButton(OPEN_FILE,       x.inc(8));
7466 leency 466
	DrawToolbarButton(SHOW_FILE_PROPERTIES, x.inc(TOOLBAR_BUTTON_WIDTH + BUTTONS_GAP));
467
 
468
	DrawToolbarButton(MAGNIFY_MINUS,   x.inc(TOOLBAR_BUTTON_WIDTH + BLOCKS_GAP));
7428 leency 469
	DrawToolbarButton(MAGNIFY_PLUS,    x.inc(TOOLBAR_BUTTON_WIDTH - 1));
7466 leency 470
 
7818 leency 471
	search_mx    = DrawToolbarButton(SEARCH_BTN,     x.inc(TOOLBAR_BUTTON_WIDTH + BLOCKS_GAP));
472
	charsets_mx  = DrawToolbarButton(CHANGE_CHARSET, x.inc(TOOLBAR_BUTTON_WIDTH + BUTTONS_GAP));
473
	reopenin_mx  = DrawToolbarButton(REOPEN_IN_APP,  x.inc(TOOLBAR_BUTTON_WIDTH + BUTTONS_GAP));
474
 
475
	x.n = Form.cwidth - 34;
476
	DrawToolbarButton(SHOW_INFO, x.n);
477
	colscheme_mx = DrawToolbarButton(COLOR_SCHEME,   x.inc(-TOOLBAR_BUTTON_WIDTH - BUTTONS_GAP));
478
 
7872 leency 479
	if (search.draw(BTN_FIND_NEXT+10, BTN_FIND_CLOSE+10, Form.cheight - SEARCH_H)) {
7818 leency 480
		DrawRectangle3D(search_mx+1, 6, TOOLBAR_ICON_WIDTH-1,
481
			TOOLBAR_ICON_HEIGHT-1, 0xCCCccc, 0xF8FCF8);
482
	}
483
 
484
	old_w = list.w;
485
 
486
	list.SetSizes(0, TOOLBAR_H, Form.cwidth-scroll.size_x-1,
487
		Form.cheight-TOOLBAR_H-search.height(), math.round(kfont.size.pt * 1.4));
488
 
489
	if ((Form.cwidth-scroll.size_x-1 == old_w) && (list.count)) {
5980 leency 490
		DrawPage();
491
	} else {
7818 leency 492
		ParseAndPaint();
7466 leency 493
	}
494
 
5980 leency 495
	DrawRectangle(scroll.start_x, scroll.start_y, scroll.size_x, scroll.size_y-1, scroll.bckg_col);
5923 leency 496
}
5980 leency 497
 
498
void DrawPage()
499
{
6808 leency 500
	kfont.ShowBufferPart(list.x, list.y, list.w, list.h, list.first*list.item_h*list.w);
7286 leency 501
 
502
	scroll.max_area = list.count;
503
	scroll.cur_area = list.visible;
504
	scroll.position = list.first;
505
	scroll.all_redraw = 0;
506
	scroll.start_x = list.x + list.w;
507
	scroll.start_y = list.y;
508
	scroll.size_y = list.h;
509
	scrollbar_v_draw(#scroll);
7818 leency 510
}