Subversion Repositories Kolibri OS

Rev

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

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