Subversion Repositories Kolibri OS

Rev

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

Rev Author Line No. Line
7043 leency 1
//Calypte 0.35 - Leency
7041 leency 2
//Calypte 0.15 - Punk Joker
3
 
7043 leency 4
#define MEMSIZE 1024*100
7041 leency 5
 
5614 punk_joker 6
#ifndef AUTOBUILD
7
	#include "lang.h--"
8
#endif
9
 
5612 punk_joker 10
 
7041 leency 11
//===================================================//
12
//                                                   //
13
//                       LIB                         //
14
//                                                   //
15
//===================================================//
5915 punk_joker 16
 
7041 leency 17
#include "../lib/kolibri.h"
18
#include "../lib/file_system.h"
19
#include "../lib/gui.h"
20
#include "../lib/list_box.h"
21
#include "../lib/menu.h"
7043 leency 22
//#include "../lib/collection.h"
5915 punk_joker 23
 
7043 leency 24
 
7041 leency 25
#include "../lib/obj/iconv.h"
7043 leency 26
#include "../lib/obj/box_lib.h"
27
#include "../lib/obj/libio_lib.h" //TO CHECK: why opendial is a peace of shit!
7041 leency 28
#include "../lib/obj/proc_lib.h"
5915 punk_joker 29
 
7041 leency 30
#include "../lib/patterns/simple_open_dialog.h"
5915 punk_joker 31
 
7043 leency 32
 
7041 leency 33
char default_dir[] = "/rd/1";
34
od_filter filter2 = { "TXT",0};
5915 punk_joker 35
 
7041 leency 36
//===================================================//
37
//                                                   //
38
//                       DATA                        //
39
//                                                   //
40
//===================================================//
5915 punk_joker 41
 
7041 leency 42
/*=========  MENU  ==========*/
43
?define MENU1 "File"
44
?define MENU2 "Encoding"
45
?define MENU3 "Reopen"
5612 punk_joker 46
 
7041 leency 47
char menu_file_list[] =
48
"Open
49
Close
50
Properties
51
Exit";
5612 punk_joker 52
 
7041 leency 53
char menu_encoding_list[] =
54
"UTF-8
55
KOI8-RU
56
CP1251
57
CP1252
58
ISO8859-5
59
CP866";
5915 punk_joker 60
 
7041 leency 61
char menu_reopen_list[] =
62
"Tinypad
63
TextEdit
64
TextRead
65
WebView
66
FB2Read
67
HexView";
5612 punk_joker 68
 
7041 leency 69
enum {
70
	MENU_ID_FILE=10,
71
	FILE_SUBMENU_ID_OPEN=10,
72
	FILE_SUBMENU_ID_CLOSE,
73
	FILE_SUBMENU_ID_PROPERTIES,
74
	FILE_SUBMENU_ID_EXIT,
5674 pavelyakov 75
 
7041 leency 76
	MENU_ID_ENCODING=20,
5612 punk_joker 77
 
7041 leency 78
	MENU_ID_REOPEN=30,
79
	FILE_SUBMENU_ID_TINYPAD=30,
80
	FILE_SUBMENU_ID_TEXTEDIT,
81
	FILE_SUBMENU_ID_TEXTREAD,
82
	FILE_SUBMENU_ID_WEBVIEW,
83
	FILE_SUBMENU_ID_FB2READ,
84
	FILE_SUBMENU_ID_HEXVIEW
5612 punk_joker 85
};
86
 
7041 leency 87
int menu_file_x = 6;
88
int menu_encoding_x = NULL;
89
int menu_reopen_x = NULL;
90
/*======== MENU END ==========*/
5612 punk_joker 91
 
7041 leency 92
#define TITLE "Calypte v0.3"
93
char win_title[4096] = TITLE;
5612 punk_joker 94
 
7041 leency 95
#define TOPPANELH 23
96
#define BOTPANELH 10
97
#define WIN_W 750
98
#define WIN_H 550
7043 leency 99
#define SCROLL_SIZE 15
7041 leency 100
 
101
proc_info Form;
102
llist rows;
103
 
104
int encoding;
5915 punk_joker 105
 
106
dword old_width,old_height;
5612 punk_joker 107
 
108
dword bufpointer;
109
dword bufsize;
110
dword draw_sruct;
111
 
7043 leency 112
scroll_bar scroll_v = { SCROLL_SIZE,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};
113
scroll_bar scroll_h = { SCROLL_SIZE,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};
114
 
115
//collection s;
116
 
7041 leency 117
//===================================================//
118
//                                                   //
119
//                       CODE                        //
120
//                                                   //
121
//===================================================//
5612 punk_joker 122
 
7041 leency 123
void InitDlls()
124
{
7043 leency 125
	load_dll(boxlib,    #box_lib_init,   0);
7041 leency 126
	load_dll(libio,     #libio_init,     1);
127
	load_dll(iconv_lib, #iconv_open,     0);
128
	load_dll(Proc_lib,  #OpenDialog_init,0);
129
	OpenDialog_init stdcall (#o_dialog);
130
}
131
 
5612 punk_joker 132
void main()
133
{
5705 punk_joker 134
	int id;
5612 punk_joker 135
 
7041 leency 136
	InitDlls();
5612 punk_joker 137
 
7041 leency 138
	SetEventMask(EVM_REDRAW+EVM_KEY+EVM_BUTTON+EVM_MOUSE+EVM_MOUSE_FILTER);
5612 punk_joker 139
	loop()
140
	{
7041 leency 141
	  switch(WaitEvent())
142
	  {
5612 punk_joker 143
		case evMouse:
7041 leency 144
			mouse.get();
145
			rows.wheel_size = 3;
146
			if (rows.MouseScroll(mouse.vert)) {
147
				DrawText();
148
				break;
5612 punk_joker 149
			}
150
			break;
151
 
7041 leency 152
		case evButton:
5915 punk_joker 153
			id=GetButtonID();
7041 leency 154
			if (id==1) ExitProcess();
7043 leency 155
			if (id==MENU_ID_FILE)
156
				EventShowMenu(menu_file_x, #menu_file_list, MENU_ID_FILE, NULL);
157
			if (id==MENU_ID_ENCODING)
158
				EventShowMenu(menu_encoding_x, #menu_encoding_list, MENU_ID_ENCODING, encoding+1);
159
			if (id==MENU_ID_REOPEN)
160
				EventShowMenu(menu_reopen_x, #menu_reopen_list, MENU_ID_REOPEN, NULL);
5612 punk_joker 161
			break;
5915 punk_joker 162
 
7041 leency 163
		case evKey:
164
			GetKeys();
165
			if (key_modifier&KEY_LCTRL) || (key_modifier&KEY_RCTRL)
5612 punk_joker 166
			{
7041 leency 167
				if (key_scancode == SCAN_CODE_KEY_O) EventOpenFile();
5705 punk_joker 168
				break;
169
			}
7041 leency 170
			if (rows.ProcessKey(key_scancode)) DrawText();
5612 punk_joker 171
			break;
7041 leency 172
 
173
		 case evReDraw:
174
			if (menu.list.cur_y) {
175
				if (menu.list.cur_y == FILE_SUBMENU_ID_OPEN) EventOpenFile();
176
				if (menu.list.cur_y == FILE_SUBMENU_ID_CLOSE) EventCloseFile();
177
				if (menu.list.cur_y == FILE_SUBMENU_ID_PROPERTIES) EventShowFileProperties();
178
				if (menu.list.cur_y == FILE_SUBMENU_ID_EXIT) ExitProcess();
179
 
180
				if (menu.list.cur_y > MENU_ID_ENCODING) && (menu.list.cur_y < MENU_ID_ENCODING + 10)
181
					EventChangeEncoding(menu.list.cur_y - MENU_ID_ENCODING);
182
 
183
				if (menu.list.cur_y == FILE_SUBMENU_ID_TINYPAD) ReopenFileIn("/sys/tinypad");
184
				if (menu.list.cur_y == FILE_SUBMENU_ID_TEXTEDIT) ReopenFileIn("/sys/develop/t_edit");
185
				if (menu.list.cur_y == FILE_SUBMENU_ID_TEXTREAD) ReopenFileIn("/sys/txtread");
186
				if (menu.list.cur_y == FILE_SUBMENU_ID_WEBVIEW) ReopenFileIn("/sys/network/webview");
187
				if (menu.list.cur_y == FILE_SUBMENU_ID_FB2READ) ReopenFileIn("/sys/fb2read");
188
				if (menu.list.cur_y == FILE_SUBMENU_ID_HEXVIEW) ReopenFileIn("/sys/develop/heed");
189
 
190
 
191
				menu.list.cur_y = 0;
192
			};
5612 punk_joker 193
			draw_window();
194
			break;
7041 leency 195
	  }
5612 punk_joker 196
   }
197
}
198
 
7043 leency 199
void ReopenFileIn(dword _app)
7041 leency 200
{
7043 leency 201
	RunProgram(_app, #param);
7041 leency 202
}
5612 punk_joker 203
 
7043 leency 204
void EventShowMenu(dword _menu_item_x, _menu_list, _id, _selected)
205
{
206
	menu.selected = _selected;
207
	menu.show(
208
	Form.left+5 + _menu_item_x,
209
	Form.top+skin_height + TOPPANELH,
210
	140,
211
	_menu_list,
212
	_id);
213
}
214
 
7041 leency 215
void EventOpenFile()
216
{
217
	OpenDialog_start stdcall (#o_dialog);
7043 leency 218
	if (!o_dialog.status) return;
7041 leency 219
	OpenFile(#openfile_path);
220
	Prepare();
221
	draw_window();
222
}
223
 
224
void EventCloseFile()
225
{
7043 leency 226
	if (!bufpointer) return;
7041 leency 227
	strcpy(#win_title, TITLE);
228
	FreeBuf();
229
	draw_window();
230
}
231
 
232
void EventShowFileProperties()
233
{
234
char ss_param[4096];
235
	if (!bufpointer) return;
236
	sprintf(#ss_param, "-p %s", #param);
237
	RunProgram("/sys/File managers/Eolite", #ss_param);
238
}
239
 
240
void EventChangeEncoding(dword id)
241
{
242
	encoding = id;
243
	OpenFile(#openfile_path);
244
	Prepare();
245
	draw_window();
246
}
247
 
248
int DrawMenuButton(dword x,y,id,text)
249
{
250
	int textlen = strlen(text)*8;
251
	int padding = 12;
252
	DefineHiddenButton(x, y, textlen+padding+padding, TOPPANELH-2, id);
253
	WriteText(x+padding,y+4, 0x90, MixColors(system.color.work, system.color.work_text, 70), text);
254
	return textlen+padding+padding;
255
}
256
 
5612 punk_joker 257
void draw_window()
258
{
5674 pavelyakov 259
	system.color.get();
6746 leency 260
	DefineAndDrawWindow(GetScreenWidth()-WIN_W/2,GetScreenHeight()-WIN_H/2,WIN_W,WIN_H,0x73,0xFFFFFF,#win_title,0);
5612 punk_joker 261
	GetProcessInfo(#Form, SelfInfo);
7041 leency 262
	DrawBar(0, 0, Form.cwidth, TOPPANELH-1, system.color.work);
263
	DrawBar(0, TOPPANELH-1, Form.cwidth, 1, system.color.work_dark);
5674 pavelyakov 264
	DrawBar(0, Form.cheight-BOTPANELH, Form.cwidth, BOTPANELH, system.color.work);
5612 punk_joker 265
 
7041 leency 266
	menu_encoding_x = menu_file_x + DrawMenuButton(menu_file_x, 0, MENU_ID_FILE, MENU1);
267
	menu_reopen_x = menu_encoding_x + DrawMenuButton(menu_encoding_x, 0, MENU_ID_ENCODING, MENU2);
268
	DrawMenuButton(menu_reopen_x, 0, MENU_ID_REOPEN, MENU3);
269
 
5612 punk_joker 270
	if (old_width!=Form.width) || (old_height!=Form.height)
271
	{
272
		old_width = Form.width;
273
		old_height = Form.height;
5915 punk_joker 274
 
7041 leency 275
		rows.no_selection = true;
276
		rows.SetFont(8, 14, 0x90);
7043 leency 277
		rows.SetSizes(0, TOPPANELH, Form.cwidth - SCROLL_SIZE, Form.cheight - TOPPANELH - BOTPANELH, 20);
7041 leency 278
		rows.column_max = rows.w / rows.font_w;
279
 
280
		if (bufpointer) Prepare();
281
		rows.CheckDoesValuesOkey();
5612 punk_joker 282
	}
7043 leency 283
	DrawRectangle(rows.x+rows.w-1, rows.y, SCROLL_SIZE, rows.h-1, 0xEEEeee);
284
	DrawText();
5612 punk_joker 285
}
286
 
7041 leency 287
void OpenFile(dword _path)
5612 punk_joker 288
{
7041 leency 289
	strcpy(#param, _path);
290
	sprintf(#win_title, "%s - %s", TITLE, #param);
291
	rows.KeyHome();
292
	file_size stdcall (#param);
5612 punk_joker 293
	bufsize = EBX;
294
	if (bufsize)
295
	{
7041 leency 296
		bufpointer = mem_Free(bufpointer);
5612 punk_joker 297
		bufpointer = mem_Alloc(bufsize);
7041 leency 298
		if (ReadFile(0, bufsize, bufpointer, #param) != 0) {
299
			bufpointer = 0;
300
			notify("'Error opening file'-E");
301
		}
5612 punk_joker 302
	}
5915 punk_joker 303
	if (encoding!=CH_CP866) ChangeCharset(charsets[encoding], "CP866", bufpointer);
5612 punk_joker 304
}
305
 
306
void FreeBuf()
307
{
308
	int i;
7043 leency 309
	if (!bufpointer) return;
7041 leency 310
	for (i=0; i
5612 punk_joker 311
	{
312
		mem_Free(DSDWORD[i*4+draw_sruct]);
313
	}
7041 leency 314
	draw_sruct = mem_Free(draw_sruct);
315
	bufpointer = mem_Free(bufpointer);
5612 punk_joker 316
}
317
 
7041 leency 318
enum {
319
	PARSE_CALCULATE_ROWS_COUNT,
320
	PARSE_DRAW_PREPARE,
321
};
322
void Parse(int mode)
5612 punk_joker 323
{
7041 leency 324
	int pos=0;
325
	int sub_pos=0;
5612 punk_joker 326
	int len_str = 0;
7041 leency 327
	bool do_eof = false;
5612 punk_joker 328
	word bukva[2];
7041 leency 329
 
5612 punk_joker 330
	while(1)
331
	{
332
		while(1)
333
		{
5915 punk_joker 334
			bukva = DSBYTE[bufpointer+pos+len_str];
5612 punk_joker 335
			if (bukva=='\0')
336
			{
7041 leency 337
				do_eof = true;
5612 punk_joker 338
				break;
339
			}
340
			if (bukva==0x0a) break;
341
			else len_str++;
342
		}
7041 leency 343
		if (len_str<=rows.column_max)
5612 punk_joker 344
		{
7041 leency 345
			if (mode==PARSE_DRAW_PREPARE)
346
			{
347
				ESDWORD[sub_pos*4+draw_sruct] = mem_Alloc(len_str+1);
348
				strlcpy(DSDWORD[sub_pos*4+draw_sruct], bufpointer+pos, len_str); //-1 to do not show \n symbol
349
			}
350
			pos += len_str+1;
5612 punk_joker 351
		}
352
		else
353
		{
7041 leency 354
			if (mode==PARSE_DRAW_PREPARE)
5612 punk_joker 355
			{
7041 leency 356
				ESDWORD[sub_pos*4+draw_sruct] = mem_Alloc(len_str+1);
357
				strlcpy(DSDWORD[sub_pos*4+draw_sruct], bufpointer+pos, rows.column_max);
5612 punk_joker 358
			}
7041 leency 359
			pos += rows.column_max;
5612 punk_joker 360
		}
7041 leency 361
		sub_pos++;
362
		if (mode==PARSE_CALCULATE_ROWS_COUNT) if (do_eof) break;
363
		if (mode==PARSE_DRAW_PREPARE) if (pos>=bufsize-1) break;
5612 punk_joker 364
		len_str = 0;
365
	}
7041 leency 366
	if (mode == PARSE_CALCULATE_ROWS_COUNT)
367
	{
368
		rows.count = sub_pos;
369
		draw_sruct = mem_Free(draw_sruct);
370
		draw_sruct = mem_Alloc(rows.count*4);
371
		Parse(PARSE_DRAW_PREPARE);
372
 
373
	}
5612 punk_joker 374
}
375
 
7041 leency 376
void Prepare()
377
{
378
	Parse(PARSE_CALCULATE_ROWS_COUNT);
379
}
380
 
5612 punk_joker 381
void DrawText()
382
{
7043 leency 383
	int i=0, top;
7041 leency 384
 
385
	if (rows.count
5612 punk_joker 386
	else
387
	{
7041 leency 388
		if (rows.count-rows.first<=rows.visible) top = rows.count-rows.first-1;
389
		else top = rows.visible;
5612 punk_joker 390
	}
7041 leency 391
 
7043 leency 392
	if (bufpointer) for (i=0; i
5612 punk_joker 393
	{
7043 leency 394
		DrawBar(0, i*rows.item_h+TOPPANELH, rows.w, rows.item_h, 0xFFFFFF);
7041 leency 395
		WriteText(2, i*rows.item_h+TOPPANELH, 0x90, 0x000000, DSDWORD[i+rows.first*4+draw_sruct]);
5612 punk_joker 396
	}
7041 leency 397
	DrawBar(0, i*rows.item_h+rows.y, rows.w, -i*rows.item_h + rows.h, 0xFFFfff);
7043 leency 398
	DrawVerticalScroll();
5612 punk_joker 399
}
400
 
7043 leency 401
void DrawVerticalScroll()
402
{
403
	scroll_v.max_area = rows.count;
404
	scroll_v.cur_area = rows.visible;
405
	scroll_v.position = rows.first;
406
	scroll_v.start_y = rows.y;
407
	scroll_v.size_y = rows.h;
408
	scroll_v.start_x = rows.w + rows.x -1;
409
	scroll_v.all_redraw = 0;
410
	scrollbar_v_draw(#scroll_v);
411
}
412
 
7041 leency 413
stop: