Subversion Repositories Kolibri OS

Rev

Rev 7872 | 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) {
7920 leency 361
		case 0: app = "/sys/tinypad"; break;
362
		case 1: app = "/sys/develop/t_edit"; break;
363
		case 2: app = "/sys/network/webview"; break;
364
		case 3: app = "/sys/fb2read"; break;
365
		case 4: app = "/sys/develop/heed"; break;
366
		case 5: open_param[0]='~';
7768 leency 367
			strcpy(#open_param+1,#param);
7466 leency 368
			RunProgram("/sys/@open", #open_param);
7920 leency 369
			return;
7462 leency 370
	}
7818 leency 371
	RunProgram(app, #param);
7462 leency 372
}
373
 
7818 leency 374
void EventMenuClick()
375
{
376
	dword click_id = get_menu_click();
377
 
378
	if (click_id) && (menu_id)
379
	{
380
		if (menu_id == CHANGE_CHARSET) EventChangeCharset(click_id-1);
381
		else if (menu_id == REOPEN_IN_APP) EventOpenFileInAnotherProgram(click_id-1);
382
		else if (menu_id == COLOR_SCHEME) EventSetColorScheme(click_id-1);
383
		else notify("'Error: wrong menu number'E");
384
	}
385
	menu_id = NULL;
386
}
387
 
7466 leency 388
//===================================================//
389
//                                                   //
390
//               DRAWS AND OTHER FUNCS               //
391
//                                                   //
392
//===================================================//
5980 leency 393
 
7818 leency 394
void LoadFile(dword f_path)
5819 leency 395
{
396
	int tmp;
397
	if (ESBYTE[f_path]) {
398
		strcpy(#param, f_path);
7818 leency 399
		if (!io.read(#param)) goto NO_DATA;
5819 leency 400
		strcpy(#title, #param);
401
		strcat(#title, " - Text Reader");
402
	}
403
	else {
7818 leency 404
		NO_DATA:
5819 leency 405
		if (list.count) return;
5923 leency 406
		io.buffer_data = INTRO_TEXT;
5819 leency 407
		strcpy(#title, "Text Reader");
408
	}
7872 leency 409
 
410
	// Autodetecting encoding
411
	if (encoding==CH_AUTO) {
412
		if (strstr(io.buffer_data, "\208\190")) ChangeCharset(CH_UTF8, "CP866", io.buffer_data);
413
		else if (chrnum(io.buffer_data, '\246')>5) || (strstr(io.buffer_data, "οπ")) ChangeCharset(CH_CP1251, "CP866", io.buffer_data);
414
	} else {
415
		if (encoding!=CH_CP866) ChangeCharset(encoding, "CP866", io.buffer_data);
416
	}
417
 
5819 leency 418
	list.ClearList();
419
}
420
 
7818 leency 421
int DrawToolbarButton(char image_id, int x)
422
{
423
	DrawOvalBorder(x, 5, TOOLBAR_ICON_WIDTH, TOOLBAR_ICON_HEIGHT, sc.work_graph,
424
		sc.work_graph,sc.work_graph, sc.work_dark);
425
	img_draw stdcall(skin.image, x+1, 5+1, TOOLBAR_ICON_WIDTH, TOOLBAR_ICON_HEIGHT, TOOLBAR_ICON_WIDTH*image_id, 0);
426
 
427
	if (menu_id) && (menu_id == image_id) {
428
		DrawRectangle3D(x+1, 6, TOOLBAR_ICON_WIDTH-1, TOOLBAR_ICON_HEIGHT-1, 0xCCCccc, 0xF8FCF8);
429
		PutShadow(x+1, 6, TOOLBAR_ICON_WIDTH, TOOLBAR_ICON_HEIGHT, true, 2);
430
	}
431
	DefineHiddenButton(x+1, 6, TOOLBAR_ICON_WIDTH, TOOLBAR_ICON_HEIGHT, 10+image_id);
432
	return x;
433
}
434
 
5980 leency 435
void draw_window()
436
{
7818 leency 437
	#define BUTTONS_GAP 5
438
	#define BLOCKS_GAP 18
7218 leency 439
	#define TOOLBAR_BUTTON_WIDTH 26
440
	incn x;
7818 leency 441
	int old_w;
6746 leency 442
	DefineAndDrawWindow(Form.left,Form.top,Form.width,Form.height,0x73,0,#title,0);
5980 leency 443
	GetProcessInfo(#Form, SelfInfo);
7806 leency 444
	sc.get();
5980 leency 445
	if (Form.status_window>2) return;
446
 
7818 leency 447
	if (Form.width  < 340) { MoveSize(OLD,OLD,340,OLD); return; }
5980 leency 448
	if (Form.height < 200) { MoveSize(OLD,OLD,OLD,200); return; }
449
 
7806 leency 450
	DrawBar(0, 0, Form.cwidth, TOOLBAR_H - 1, sc.work);
451
	DrawBar(0, TOOLBAR_H - 1, Form.cwidth, 1, sc.work_graph);
5980 leency 452
 
7218 leency 453
	x.n = 0;
454
	DrawToolbarButton(OPEN_FILE,       x.inc(8));
7466 leency 455
	DrawToolbarButton(SHOW_FILE_PROPERTIES, x.inc(TOOLBAR_BUTTON_WIDTH + BUTTONS_GAP));
456
 
457
	DrawToolbarButton(MAGNIFY_MINUS,   x.inc(TOOLBAR_BUTTON_WIDTH + BLOCKS_GAP));
7428 leency 458
	DrawToolbarButton(MAGNIFY_PLUS,    x.inc(TOOLBAR_BUTTON_WIDTH - 1));
7466 leency 459
 
7818 leency 460
	search_mx    = DrawToolbarButton(SEARCH_BTN,     x.inc(TOOLBAR_BUTTON_WIDTH + BLOCKS_GAP));
461
	charsets_mx  = DrawToolbarButton(CHANGE_CHARSET, x.inc(TOOLBAR_BUTTON_WIDTH + BUTTONS_GAP));
462
	reopenin_mx  = DrawToolbarButton(REOPEN_IN_APP,  x.inc(TOOLBAR_BUTTON_WIDTH + BUTTONS_GAP));
463
 
464
	x.n = Form.cwidth - 34;
465
	DrawToolbarButton(SHOW_INFO, x.n);
466
	colscheme_mx = DrawToolbarButton(COLOR_SCHEME,   x.inc(-TOOLBAR_BUTTON_WIDTH - BUTTONS_GAP));
467
 
7872 leency 468
	if (search.draw(BTN_FIND_NEXT+10, BTN_FIND_CLOSE+10, Form.cheight - SEARCH_H)) {
7818 leency 469
		DrawRectangle3D(search_mx+1, 6, TOOLBAR_ICON_WIDTH-1,
470
			TOOLBAR_ICON_HEIGHT-1, 0xCCCccc, 0xF8FCF8);
471
	}
472
 
473
	old_w = list.w;
474
 
475
	list.SetSizes(0, TOOLBAR_H, Form.cwidth-scroll.size_x-1,
476
		Form.cheight-TOOLBAR_H-search.height(), math.round(kfont.size.pt * 1.4));
477
 
478
	if ((Form.cwidth-scroll.size_x-1 == old_w) && (list.count)) {
5980 leency 479
		DrawPage();
480
	} else {
7818 leency 481
		ParseAndPaint();
7466 leency 482
	}
483
 
5980 leency 484
	DrawRectangle(scroll.start_x, scroll.start_y, scroll.size_x, scroll.size_y-1, scroll.bckg_col);
5923 leency 485
}
5980 leency 486
 
487
void DrawPage()
488
{
6808 leency 489
	kfont.ShowBufferPart(list.x, list.y, list.w, list.h, list.first*list.item_h*list.w);
7286 leency 490
 
491
	scroll.max_area = list.count;
492
	scroll.cur_area = list.visible;
493
	scroll.position = list.first;
494
	scroll.all_redraw = 0;
495
	scroll.start_x = list.x + list.w;
496
	scroll.start_y = list.y;
497
	scroll.size_y = list.h;
498
	scrollbar_v_draw(#scroll);
7818 leency 499
}