Subversion Repositories Kolibri OS

Rev

Rev 7288 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 7288 Rev 7428
1
#define MEMSIZE 4096*25
1
#define MEMSIZE 4096*25
2
 
2
 
3
#include "../lib/io.h"
3
#include "../lib/io.h"
4
#include "../lib/gui.h"
4
#include "../lib/gui.h"
5
#include "../lib/list_box.h"
5
#include "../lib/list_box.h"
6
#include "../lib/menu.h"
6
#include "../lib/menu.h"
7
#include "../lib/kfont.h"
7
#include "../lib/kfont.h"
8
 
8
 
9
#include "../lib/obj/box_lib.h"
9
#include "../lib/obj/box_lib.h"
10
#include "../lib/obj/libini.h"
10
#include "../lib/obj/libini.h"
11
#include "../lib/obj/libimg.h"
11
#include "../lib/obj/libimg.h"
12
#include "../lib/obj/iconv.h"
12
#include "../lib/obj/iconv.h"
13
#include "../lib/obj/proc_lib.h"
13
#include "../lib/obj/proc_lib.h"
14
 
14
 
15
#include "../lib/patterns/simple_open_dialog.h"
15
#include "../lib/patterns/simple_open_dialog.h"
16
 
16
 
17
#define TOOLBAR_H 34
17
#define TOOLBAR_H 34
18
#define TOOLBAR_ICON_WIDTH  26
18
#define TOOLBAR_ICON_WIDTH  26
19
#define TOOLBAR_ICON_HEIGHT 24
19
#define TOOLBAR_ICON_HEIGHT 24
20
 
20
 
21
#define DEFAULT_EDITOR "/sys/tinypad"
21
#define DEFAULT_EDITOR "/sys/tinypad"
22
 
22
 
23
#define INTRO_TEXT "This is a plain Text Reader.\nTry to open some text file."
23
#define INTRO_TEXT "This is a plain Text Reader.\nTry to open some text file."
24
#define VERSION "Text Reader v1.21"
24
#define VERSION "Text Reader v1.21a"
25
#define ABOUT "Idea: Leency, punk_joker
25
#define ABOUT "Idea: Leency, punk_joker
26
Code: Leency, Veliant, KolibriOS Team
26
Code: Leency, Veliant, KolibriOS Team
27
 
27
 
28
Hotkeys:
28
Hotkeys:
29
Ctrl+O - open file
29
Ctrl+O - open file
30
Ctrl+I - show file properties
30
Ctrl+I - show file properties
31
Ctrl+Up - bigger font
31
Ctrl+Up - bigger font
32
Ctrl+Down - smaller font
32
Ctrl+Down - smaller font
33
Ctrl+Tab - select charset
33
Ctrl+Tab - select charset
34
Ctrl+E - edit current document
34
Ctrl+E - edit current document
35
 
35
 
36
Press any key..."
36
Press any key..."
37
 
37
 
38
char default_dir[] = "/rd/1";
38
char default_dir[] = "/rd/1";
39
od_filter filter2 = { 8, "TXT\0\0" };
39
od_filter filter2 = { 8, "TXT\0\0" };
40
 
40
 
41
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};
41
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};
42
llist list;
42
llist list;
43
 
43
 
44
proc_info Form;
44
proc_info Form;
45
char title[4196];
45
char title[4196];
46
 
46
 
47
bool help_opened = false;
47
bool help_opened = false;
48
int charsets_menu_left = 0;
48
int charsets_menu_left = 0;
49
 
49
 
50
enum {
50
enum {
51
	OPEN_FILE,
51
	OPEN_FILE,
52
	MAGNIFY_MINUS,
52
	MAGNIFY_MINUS,
53
	MAGNIFY_PLUS,
53
	MAGNIFY_PLUS,
54
	CHANGE_ENCODING,
54
	CHANGE_ENCODING,
55
	RUN_EDIT,
55
	RUN_EDIT,
56
	SHOW_INFO,
56
	SHOW_INFO,
57
	SHOW_FILE_PROPERTIES
57
	SHOW_FILE_PROPERTIES
58
};
58
};
59
 
59
 
60
int encoding;
60
int encoding;
61
 
61
 
62
#include "ini.h"
62
#include "ini.h"
63
#include "prepare_page.h"
63
#include "prepare_page.h"
64
 
64
 
65
 
65
 
66
void InitDlls()
66
void InitDlls()
67
{
67
{
68
	load_dll(boxlib,    #box_lib_init,   0);
68
	load_dll(boxlib,    #box_lib_init,   0);
69
	load_dll(libio,     #libio_init,     1);
69
	load_dll(libio,     #libio_init,     1);
70
	load_dll(libimg,    #libimg_init,    1);
70
	load_dll(libimg,    #libimg_init,    1);
71
	load_dll(libini,    #lib_init,       1);
71
	load_dll(libini,    #lib_init,       1);
72
	load_dll(iconv_lib, #iconv_open,     0);
72
	load_dll(iconv_lib, #iconv_open,     0);
73
	load_dll(Proc_lib,  #OpenDialog_init,0);
73
	load_dll(Proc_lib,  #OpenDialog_init,0);
74
}
74
}
75
 
75
 
76
 
76
 
77
void main()
77
void main()
78
{   	
78
{   	
79
	InitDlls();	
79
	InitDlls();	
80
	OpenDialog_init stdcall (#o_dialog);
80
	OpenDialog_init stdcall (#o_dialog);
81
	LoadIniSettings();
81
	LoadIniSettings();
82
	kfont.init(DEFAULT_FONT);
82
	kfont.init(DEFAULT_FONT);
83
	Libimg_LoadImage(#skin, abspath("toolbar.png"));
83
	Libimg_LoadImage(#skin, abspath("toolbar.png"));
84
	OpenFile(#param);
84
	OpenFile(#param);
85
	list.no_selection = true;
85
	list.no_selection = true;
86
	SetEventMask(EVM_REDRAW + EVM_KEY + EVM_BUTTON + EVM_MOUSE + EVM_MOUSE_FILTER);
86
	SetEventMask(EVM_REDRAW + EVM_KEY + EVM_BUTTON + EVM_MOUSE + EVM_MOUSE_FILTER);
87
	loop()
87
	loop()
88
	{
88
	{
89
		switch(WaitEvent())
89
		switch(WaitEvent())
90
		{
90
		{
91
			case evMouse:
91
			case evMouse:
92
				HandleMouseEvent();
92
				HandleMouseEvent();
93
				break;
93
				break;
94
			case evKey:
94
			case evKey:
95
				HandleKeyEvent();
95
				HandleKeyEvent();
96
				break;
96
				break;
97
			case evButton:
97
			case evButton:
98
				HandleButtonEvent();
98
				HandleButtonEvent();
99
				break;
99
				break;
100
			case evReDraw:
100
			case evReDraw:
101
				if (menu.list.cur_y) {
101
				if (menu.list.cur_y) {
102
					encoding = menu.list.cur_y - 10;
102
					encoding = menu.list.cur_y - 10;
103
					OpenFile(#param); 
103
					OpenFile(#param); 
104
					PreparePage();
104
					PreparePage();
105
					menu.list.cur_y = NULL;
105
					menu.list.cur_y = NULL;
106
				};
106
				};
107
				draw_window();
107
				draw_window();
108
		}
108
		}
109
	}
109
	}
110
}
110
}
111
 
111
 
112
 
112
 
113
void HandleButtonEvent()
113
void HandleButtonEvent()
114
{
114
{
115
	
115
	
116
	byte btn = GetButtonID();
116
	byte btn = GetButtonID();
117
	if (btn==1) {
117
	if (btn==1) {
118
		SaveIniSettings();
118
		SaveIniSettings();
119
		ExitProcess();
119
		ExitProcess();
120
	}
120
	}
121
	btn-=10;
121
	btn-=10;
122
	switch(btn)
122
	switch(btn)
123
	{
123
	{
124
		case OPEN_FILE:
124
		case OPEN_FILE:
125
			EventOpenFile();
125
			EventOpenFile();
126
			break;
126
			break;
127
		case SHOW_FILE_PROPERTIES:
127
		case SHOW_FILE_PROPERTIES:
128
			EventShowFileProperties();
128
			EventShowFileProperties();
129
			break;
129
			break;
130
		case MAGNIFY_PLUS:
130
		case MAGNIFY_PLUS:
131
			EventMagnifyPlus();
131
			EventMagnifyPlus();
132
			break;
132
			break;
133
		case MAGNIFY_MINUS:
133
		case MAGNIFY_MINUS:
134
			EventMagnifyMinus();
134
			EventMagnifyMinus();
135
			break;
135
			break;
136
		case CHANGE_ENCODING:
136
		case CHANGE_ENCODING:
137
			EventChangeEncoding();
137
			EventChangeEncoding();
138
			break;
138
			break;
139
		case RUN_EDIT:
139
		case RUN_EDIT:
140
			EventRunEdit();
140
			EventRunEdit();
141
			break;
141
			break;
142
		case SHOW_INFO:
142
		case SHOW_INFO:
143
			EventShowInfo();
143
			EventShowInfo();
144
			break;
144
			break;
145
	}
145
	}
146
}
146
}
147
 
147
 
148
 
148
 
149
void HandleKeyEvent()
149
void HandleKeyEvent()
150
{
150
{
151
	if (help_opened) {
151
	if (help_opened) {
152
		help_opened = false;
152
		help_opened = false;
153
		DrawPage();
153
		DrawPage();
154
		return; 
154
		return; 
155
	}
155
	}
156
	GetKeys();
156
	GetKeys();
157
	if (key_scancode == SCAN_CODE_F1) {
157
	if (key_scancode == SCAN_CODE_F1) {
158
		EventShowInfo();
158
		EventShowInfo();
159
		return;
159
		return;
160
	}
160
	}
161
	if (key_modifier & KEY_LCTRL) || (key_modifier & KEY_RCTRL) {
161
	if (key_modifier & KEY_LCTRL) || (key_modifier & KEY_RCTRL) {
162
		switch (key_scancode)
162
		switch (key_scancode)
163
		{
163
		{
164
			case SCAN_CODE_KEY_O:
164
			case SCAN_CODE_KEY_O:
165
				EventOpenFile();
165
				EventOpenFile();
166
				break;
166
				break;
167
			case SCAN_CODE_KEY_I:
167
			case SCAN_CODE_KEY_I:
168
				EventShowFileProperties();
168
				EventShowFileProperties();
169
				break;
169
				break;
170
			case SCAN_CODE_UP:
170
			case SCAN_CODE_UP:
171
				EventMagnifyPlus();
171
				EventMagnifyPlus();
172
				break;
172
				break;
173
			case SCAN_CODE_DOWN:
173
			case SCAN_CODE_DOWN:
174
				EventMagnifyMinus();
174
				EventMagnifyMinus();
175
				break;
175
				break;
176
			case SCAN_CODE_KEY_E:
176
			case SCAN_CODE_KEY_E:
177
				EventRunEdit();
177
				EventRunEdit();
178
				break;
178
				break;
179
			case SCAN_CODE_TAB:
179
			case SCAN_CODE_TAB:
180
				EventChangeEncoding();
180
				EventChangeEncoding();
181
				break;
181
				break;
182
		}
182
		}
183
		return;
183
		return;
184
	}
184
	}
185
	if (list.ProcessKey(key_scancode))
185
	if (list.ProcessKey(key_scancode))
186
		DrawPage();
186
		DrawPage();
187
}
187
}
188
 
188
 
189
 
189
 
190
void HandleMouseEvent()
190
void HandleMouseEvent()
191
{
191
{
192
	mouse.get();
192
	mouse.get();
193
	list.wheel_size = 7;
193
	list.wheel_size = 7;
194
	if (list.MouseScroll(mouse.vert)) {
194
	if (list.MouseScroll(mouse.vert)) {
195
		DrawPage(); 
195
		DrawPage(); 
196
		return; 
196
		return; 
197
	}
197
	}
198
	scrollbar_v_mouse (#scroll);
198
	scrollbar_v_mouse (#scroll);
199
	if (list.first != scroll.position) {
199
	if (list.first != scroll.position) {
200
		list.first = scroll.position;
200
		list.first = scroll.position;
201
		DrawPage(); 
201
		DrawPage(); 
202
	}
202
	}
203
}
203
}
204
 
204
 
205
 
205
 
206
/* ----------------------------------------------------- */
206
/* ----------------------------------------------------- */
207
 
207
 
208
void EventOpenFile()
208
void EventOpenFile()
209
{
209
{
210
	OpenDialog_start stdcall (#o_dialog);
210
	OpenDialog_start stdcall (#o_dialog);
211
	if (o_dialog.status) {
211
	if (o_dialog.status) {
212
		OpenFile(#openfile_path);
212
		OpenFile(#openfile_path);
213
		PreparePage();
213
		PreparePage();
214
	}
214
	}
215
}
215
}
216
 
216
 
217
void EventShowFileProperties()
217
void EventShowFileProperties()
218
{
218
{
219
	char ss_param[4096];
219
	char ss_param[4096];
220
	if (!param) return;
220
	if (!param) return;
221
	sprintf(#ss_param, "-p %s", #param);
221
	sprintf(#ss_param, "-p %s", #param);
222
	io.run("/sys/File managers/Eolite", #ss_param);
222
	io.run("/sys/File managers/Eolite", #ss_param);
223
}
223
}
224
 
224
 
225
void EventMagnifyPlus()
225
void EventMagnifyPlus()
226
{
226
{
227
	kfont.size.pt++;
227
	kfont.size.pt++;
228
	if(!kfont.changeSIZE())
228
	if(!kfont.changeSIZE())
229
		kfont.size.pt--;
229
		kfont.size.pt--;
230
	else
230
	else
231
		PreparePage();
231
		PreparePage();
232
}
232
}
233
 
233
 
234
void EventMagnifyMinus()
234
void EventMagnifyMinus()
235
{
235
{
236
	kfont.size.pt--;
236
	kfont.size.pt--;
237
	if(!kfont.changeSIZE())
237
	if(!kfont.changeSIZE())
238
		kfont.size.pt++;
238
		kfont.size.pt++;
239
	else
239
	else
240
		PreparePage();
240
		PreparePage();
241
}
241
}
242
 
242
 
243
void EventRunEdit()
243
void EventRunEdit()
244
{
244
{
245
	io.run(DEFAULT_EDITOR, #param);
245
	io.run(DEFAULT_EDITOR, #param);
246
}
246
}
247
 
247
 
248
void EventChangeEncoding()
248
void EventChangeEncoding()
249
{
249
{
250
	menu.selected = encoding + 1;
250
	menu.selected = encoding + 1;
251
	menu.show(Form.left+5 + charsets_menu_left, Form.top+29+skin_height, 130,
251
	menu.show(Form.left+5 + charsets_menu_left, Form.top+29+skin_height, 130,
252
		"UTF-8\nKOI8-RU\nCP1251\nCP1252\nISO8859-5\nCP866", 10);
252
		"UTF-8\nKOI8-RU\nCP1251\nCP1252\nISO8859-5\nCP866", 10);
253
}
253
}
254
 
254
 
255
void EventShowInfo() {
255
void EventShowInfo() {
256
	help_opened = true;
256
	help_opened = true;
257
	DrawBar(list.x, list.y, list.w, list.h, 0xFFFfff);
257
	DrawBar(list.x, list.y, list.w, list.h, 0xFFFfff);
258
	WriteText(list.x + 10, list.y + 10, 10000001b, 0x555555, VERSION);
258
	WriteText(list.x + 10, list.y + 10, 10000001b, 0x555555, VERSION);
259
	WriteTextLines(list.x + 10, list.y+40, 10110000b, 0, ABOUT, 20);
259
	WriteTextLines(list.x + 10, list.y+40, 10110000b, 0, ABOUT, 20);
260
}
260
}
261
 
261
 
262
/* ------------------------------------------- */
262
/* ------------------------------------------- */
263
 
263
 
264
 
264
 
265
void OpenFile(dword f_path) 
265
void OpenFile(dword f_path) 
266
{
266
{
267
	int tmp;
267
	int tmp;
268
	if (ESBYTE[f_path]) {
268
	if (ESBYTE[f_path]) {
269
		strcpy(#param, f_path);
269
		strcpy(#param, f_path);
270
		io.read(#param);
270
		io.read(#param);
271
		strcpy(#title, #param);
271
		strcpy(#title, #param);
272
		strcat(#title, " - Text Reader"); 
272
		strcat(#title, " - Text Reader"); 
273
	}
273
	}
274
	else {
274
	else {
275
		if (list.count) return;
275
		if (list.count) return;
276
		io.buffer_data = INTRO_TEXT;
276
		io.buffer_data = INTRO_TEXT;
277
		strcpy(#title, "Text Reader"); 
277
		strcpy(#title, "Text Reader"); 
278
	}
278
	}
279
	if (encoding!=CH_CP866) ChangeCharset(charsets[encoding], "CP866", io.buffer_data);
279
	if (encoding!=CH_CP866) ChangeCharset(charsets[encoding], "CP866", io.buffer_data);
280
	list.KeyHome();
280
	list.KeyHome();
281
	list.ClearList();
281
	list.ClearList();
282
}
282
}
283
 
283
 
284
void draw_window()
284
void draw_window()
285
{
285
{
286
	#define PADDING 6
286
	#define PADDING 6
287
	#define TOOLBAR_BUTTON_WIDTH 26
287
	#define TOOLBAR_BUTTON_WIDTH 26
288
	incn x;
288
	incn x;
289
	DefineAndDrawWindow(Form.left,Form.top,Form.width,Form.height,0x73,0,#title,0);
289
	DefineAndDrawWindow(Form.left,Form.top,Form.width,Form.height,0x73,0,#title,0);
290
	GetProcessInfo(#Form, SelfInfo);
290
	GetProcessInfo(#Form, SelfInfo);
291
	if (Form.status_window>2) return;
291
	if (Form.status_window>2) return;
292
 
292
 
293
	if (Form.width  < 200) { MoveSize(OLD,OLD,200,OLD); return; }
293
	if (Form.width  < 200) { MoveSize(OLD,OLD,200,OLD); return; }
294
	if (Form.height < 200) { MoveSize(OLD,OLD,OLD,200); return; }
294
	if (Form.height < 200) { MoveSize(OLD,OLD,OLD,200); return; }
295
	
295
	
296
	DrawBar(0, 0, Form.cwidth, TOOLBAR_H - 1, 0xe1e1e1);
296
	DrawBar(0, 0, Form.cwidth, TOOLBAR_H - 1, 0xe1e1e1);
297
	DrawBar(0, TOOLBAR_H - 1, Form.cwidth, 1, 0x7F7F7F);
297
	DrawBar(0, TOOLBAR_H - 1, Form.cwidth, 1, 0x7F7F7F);
298
	
298
	
299
	x.n = 0;
299
	x.n = 0;
300
	DrawToolbarButton(OPEN_FILE,       x.inc(8));
300
	DrawToolbarButton(OPEN_FILE,       x.inc(8));
301
	DrawToolbarButton(SHOW_FILE_PROPERTIES, x.inc(TOOLBAR_BUTTON_WIDTH + PADDING));
301
	DrawToolbarButton(SHOW_FILE_PROPERTIES, x.inc(TOOLBAR_BUTTON_WIDTH + PADDING));
302
	DrawToolbarButton(MAGNIFY_PLUS,    x.inc(TOOLBAR_BUTTON_WIDTH + PADDING + PADDING));
302
	DrawToolbarButton(MAGNIFY_MINUS,   x.inc(TOOLBAR_BUTTON_WIDTH + PADDING + PADDING));
303
	DrawToolbarButton(MAGNIFY_MINUS,   x.inc(TOOLBAR_BUTTON_WIDTH - 1));
303
	DrawToolbarButton(MAGNIFY_PLUS,    x.inc(TOOLBAR_BUTTON_WIDTH - 1));
304
	DrawToolbarButton(CHANGE_ENCODING, x.inc(TOOLBAR_BUTTON_WIDTH + PADDING + PADDING));
304
	DrawToolbarButton(CHANGE_ENCODING, x.inc(TOOLBAR_BUTTON_WIDTH + PADDING + PADDING));
305
		charsets_menu_left = x.n;
305
		charsets_menu_left = x.n;
306
	DrawToolbarButton(RUN_EDIT,        x.inc(TOOLBAR_BUTTON_WIDTH + PADDING));
306
	DrawToolbarButton(RUN_EDIT,        x.inc(TOOLBAR_BUTTON_WIDTH + PADDING));
307
	DrawToolbarButton(SHOW_INFO,       Form.cwidth - 34);
307
	DrawToolbarButton(SHOW_INFO,       Form.cwidth - 34);
308
	
308
	
309
	
309
	
310
	if ((Form.cwidth-scroll.size_x-1 == list.w) && 
310
	if ((Form.cwidth-scroll.size_x-1 == list.w) && 
311
		(Form.cheight-TOOLBAR_H == list.h) && 
311
		(Form.cheight-TOOLBAR_H == list.h) && 
312
		(list.count) 
312
		(list.count) 
313
	)
313
	)
314
	{
314
	{
315
		DrawPage(); 
315
		DrawPage(); 
316
	} else {
316
	} else {
317
		PreparePage();
317
		PreparePage();
318
	}
318
	}
319
	DrawRectangle(scroll.start_x, scroll.start_y, scroll.size_x, scroll.size_y-1, scroll.bckg_col);
319
	DrawRectangle(scroll.start_x, scroll.start_y, scroll.size_x, scroll.size_y-1, scroll.bckg_col);
320
}
320
}
321
 
321
 
322
void DrawPage()
322
void DrawPage()
323
{
323
{
324
	kfont.ShowBufferPart(list.x, list.y, list.w, list.h, list.first*list.item_h*list.w);
324
	kfont.ShowBufferPart(list.x, list.y, list.w, list.h, list.first*list.item_h*list.w);
325
	DrawScroller();
325
	DrawScroller();
326
}
326
}
327
 
327
 
328
void DrawToolbarButton(char image_id, int x)
328
void DrawToolbarButton(char image_id, int x)
329
{
329
{
330
	DefineButton(x+1, 6, TOOLBAR_ICON_WIDTH-2, TOOLBAR_ICON_HEIGHT-2, 10+image_id + BT_HIDE, 0);
330
	DefineButton(x+1, 6, TOOLBAR_ICON_WIDTH-2, TOOLBAR_ICON_HEIGHT-2, 10+image_id + BT_HIDE, 0);
331
	img_draw stdcall(skin.image, x, 5, TOOLBAR_ICON_WIDTH, TOOLBAR_ICON_HEIGHT, 0, image_id*TOOLBAR_ICON_HEIGHT);
331
	img_draw stdcall(skin.image, x, 5, TOOLBAR_ICON_WIDTH, TOOLBAR_ICON_HEIGHT, TOOLBAR_ICON_WIDTH-1*image_id, 0);
332
}
332
}
333
 
333
 
334
void DrawScroller()
334
void DrawScroller()
335
{
335
{
336
	scroll.max_area = list.count;
336
	scroll.max_area = list.count;
337
	scroll.cur_area = list.visible;
337
	scroll.cur_area = list.visible;
338
	scroll.position = list.first;
338
	scroll.position = list.first;
339
	scroll.all_redraw = 0;
339
	scroll.all_redraw = 0;
340
	scroll.start_x = list.x + list.w;
340
	scroll.start_x = list.x + list.w;
341
	scroll.start_y = list.y;
341
	scroll.start_y = list.y;
342
	scroll.size_y = list.h;
342
	scroll.size_y = list.h;
343
	scrollbar_v_draw(#scroll);
343
	scrollbar_v_draw(#scroll);
344
}
344
}