Subversion Repositories Kolibri OS

Rev

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

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