Subversion Repositories Kolibri OS

Rev

Rev 7229 | 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
 
6805 leency 3
#include "../lib/kfont.h"
5886 pavelyakov 4
#include "../lib/io.h"
5819 leency 5
#include "../lib/gui.h"
6
#include "../lib/list_box.h"
6042 leency 7
#include "../lib/menu.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";
39
od_filter filter2 = {0,0};
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 "gui.h"
64
#include "prepare_page.h"
5819 leency 65
 
5980 leency 66
 
5923 leency 67
void InitDlls()
68
{
69
	load_dll(boxlib,    #box_lib_init,   0);
70
	load_dll(libio,     #libio_init,     1);
71
	load_dll(libimg,    #libimg_init,    1);
72
	load_dll(libini,    #lib_init,       1);
73
	load_dll(iconv_lib, #iconv_open,     0);
74
	load_dll(Proc_lib,  #OpenDialog_init,0);
75
}
76
 
77
 
5819 leency 78
void main()
5923 leency 79
{
5987 leency 80
	InitDlls();
5819 leency 81
	OpenDialog_init stdcall (#o_dialog);
6806 leency 82
	kfont.init(DEFAULT_FONT);
5819 leency 83
	Libimg_LoadImage(#skin, abspath("toolbar.png"));
84
	LoadIniSettings();
85
	OpenFile(#param);
86
	list.no_selection = true;
7227 leency 87
	SetEventMask(EVM_REDRAW + EVM_KEY + EVM_BUTTON + EVM_MOUSE + EVM_MOUSE_FILTER);
5819 leency 88
	loop()
89
	{
5923 leency 90
		switch(WaitEvent())
91
		{
92
			case evMouse:
93
				HandleMouseEvent();
5819 leency 94
				break;
5923 leency 95
			case evKey:
96
				HandleKeyEvent();
97
				break;
98
			case evButton:
99
				HandleButtonEvent();
100
				break;
101
			case evReDraw:
6042 leency 102
				if (menu.list.cur_y) {
103
					encoding = menu.list.cur_y - 10;
5923 leency 104
					OpenFile(#param);
105
					PreparePage();
6753 leency 106
					menu.list.cur_y = NULL;
5923 leency 107
				};
108
				draw_window();
109
		}
5819 leency 110
	}
111
}
112
 
5980 leency 113
 
114
void HandleButtonEvent()
5819 leency 115
{
5923 leency 116
 
5980 leency 117
	byte btn = GetButtonID();
118
	if (btn==1) {
119
		SaveIniSettings();
120
		ExitProcess();
121
	}
122
	btn-=10;
123
	switch(btn)
5923 leency 124
	{
5980 leency 125
		case OPEN_FILE:
126
			EventOpenFile();
127
			break;
7218 leency 128
		case SHOW_FILE_PROPERTIES:
129
			EventShowFileProperties();
130
			break;
5980 leency 131
		case MAGNIFY_PLUS:
132
			EventMagnifyPlus();
133
			break;
134
		case MAGNIFY_MINUS:
135
			EventMagnifyMinus();
136
			break;
137
		case CHANGE_ENCODING:
138
			EventChangeEncoding();
139
			break;
140
		case RUN_EDIT:
141
			EventRunEdit();
142
			break;
143
		case SHOW_INFO:
144
			EventShowInfo();
145
			break;
5923 leency 146
	}
5819 leency 147
}
148
 
149
 
5980 leency 150
void HandleKeyEvent()
5819 leency 151
{
5980 leency 152
	if (help_opened) {
153
		help_opened = false;
154
		DrawPage();
155
		return;
156
	}
157
	GetKeys();
7218 leency 158
	if (key_scancode == SCAN_CODE_F1) {
5980 leency 159
		EventShowInfo();
160
		return;
161
	}
162
	if (key_modifier & KEY_LCTRL) || (key_modifier & KEY_RCTRL) {
163
		switch (key_scancode)
164
		{
7218 leency 165
			case SCAN_CODE_KEY_O:
5980 leency 166
				EventOpenFile();
167
				break;
7218 leency 168
			case SCAN_CODE_KEY_I:
169
				EventShowFileProperties();
170
				break;
5980 leency 171
			case SCAN_CODE_UP:
172
				EventMagnifyPlus();
173
				break;
174
			case SCAN_CODE_DOWN:
175
				EventMagnifyMinus();
176
				break;
7218 leency 177
			case SCAN_CODE_KEY_E:
5980 leency 178
				EventRunEdit();
179
				break;
180
			case SCAN_CODE_TAB:
181
				EventChangeEncoding();
182
				break;
5819 leency 183
		}
5980 leency 184
		return;
5819 leency 185
	}
5980 leency 186
	if (list.ProcessKey(key_scancode))
187
		DrawPage();
188
}
5819 leency 189
 
190
 
5980 leency 191
void HandleMouseEvent()
192
{
193
	mouse.get();
194
	list.wheel_size = 7;
195
	if (list.MouseScroll(mouse.vert)) {
196
		DrawPage();
197
		return;
5819 leency 198
	}
5980 leency 199
	scrollbar_v_mouse (#scroll);
200
	if (list.first != scroll.position) {
201
		list.first = scroll.position;
202
		DrawPage();
203
	}
5819 leency 204
}
205
 
5980 leency 206
 
207
/* ----------------------------------------------------- */
208
 
209
void EventOpenFile()
5819 leency 210
{
5980 leency 211
	OpenDialog_start stdcall (#o_dialog);
212
	OpenFile(#openfile_path);
213
	PreparePage();
5819 leency 214
}
215
 
7218 leency 216
void EventShowFileProperties()
217
{
218
	char ss_param[4096];
219
	if (!param) return;
220
	sprintf(#ss_param, "-p %s", #param);
221
	io.run("/sys/File managers/Eolite", #ss_param);
222
}
223
 
5980 leency 224
void EventMagnifyPlus()
5819 leency 225
{
6806 leency 226
	kfont.size.pt++;
227
	if(!kfont.changeSIZE())
228
		kfont.size.pt--;
5980 leency 229
	else
230
		PreparePage();
5819 leency 231
}
232
 
5980 leency 233
void EventMagnifyMinus()
234
{
6806 leency 235
	kfont.size.pt--;
236
	if(!kfont.changeSIZE())
237
		kfont.size.pt++;
5980 leency 238
	else
239
		PreparePage();
240
}
241
 
242
void EventRunEdit()
243
{
244
	io.run(DEFAULT_EDITOR, #param);
245
}
246
 
247
void EventChangeEncoding()
248
{
6042 leency 249
	menu.selected = encoding + 1;
7218 leency 250
	menu.show(Form.left+5 + charsets_menu_left, Form.top+29+skin_height, 130,
251
		"UTF-8\nKOI8-RU\nCP1251\nCP1252\nISO8859-5\nCP866", 10);
5980 leency 252
}
253
 
254
void EventShowInfo() {
255
	help_opened = true;
256
	DrawBar(list.x, list.y, list.w, list.h, 0xFFFfff);
257
	WriteText(list.x + 10, list.y + 10, 10000001b, 0x555555, VERSION);
258
	WriteTextLines(list.x + 10, list.y+40, 10110000b, 0, ABOUT, 20);
259
}
260
 
261
/* ------------------------------------------- */
262
 
263
 
5819 leency 264
void OpenFile(dword f_path)
265
{
266
	int tmp;
267
	if (ESBYTE[f_path]) {
268
		strcpy(#param, f_path);
269
		io.read(#param);
270
		strcpy(#title, #param);
271
		strcat(#title, " - Text Reader");
272
	}
273
	else {
274
		if (list.count) return;
5923 leency 275
		io.buffer_data = INTRO_TEXT;
5819 leency 276
		strcpy(#title, "Text Reader");
277
	}
278
	if (encoding!=CH_CP866) ChangeCharset(charsets[encoding], "CP866", io.buffer_data);
279
	list.KeyHome();
280
	list.ClearList();
281
}
282
 
5980 leency 283
void draw_window()
284
{
7218 leency 285
	#define PADDING 6
286
	#define TOOLBAR_BUTTON_WIDTH 26
287
	incn x;
6746 leency 288
	DefineAndDrawWindow(Form.left,Form.top,Form.width,Form.height,0x73,0,#title,0);
5980 leency 289
	GetProcessInfo(#Form, SelfInfo);
290
	if (Form.status_window>2) return;
291
 
292
	if (Form.width  < 200) { MoveSize(OLD,OLD,200,OLD); return; }
293
	if (Form.height < 200) { MoveSize(OLD,OLD,OLD,200); return; }
294
 
295
	DrawBar(0, 0, Form.cwidth, TOOLBAR_H - 1, 0xe1e1e1);
296
	DrawBar(0, TOOLBAR_H - 1, Form.cwidth, 1, 0x7F7F7F);
297
 
7218 leency 298
	x.n = 0;
299
	DrawToolbarButton(OPEN_FILE,       x.inc(8));
300
	DrawToolbarButton(SHOW_FILE_PROPERTIES, x.inc(TOOLBAR_BUTTON_WIDTH + PADDING));
301
	DrawToolbarButton(MAGNIFY_PLUS,    x.inc(TOOLBAR_BUTTON_WIDTH + PADDING + PADDING));
302
	DrawToolbarButton(MAGNIFY_MINUS,   x.inc(TOOLBAR_BUTTON_WIDTH - 1));
303
	DrawToolbarButton(CHANGE_ENCODING, x.inc(TOOLBAR_BUTTON_WIDTH + PADDING + PADDING));
304
		charsets_menu_left = x.n;
305
	DrawToolbarButton(RUN_EDIT,        x.inc(TOOLBAR_BUTTON_WIDTH + PADDING));
5980 leency 306
	DrawToolbarButton(SHOW_INFO,       Form.cwidth - 34);
307
 
7043 leency 308
 
5980 leency 309
	if ((Form.cwidth-scroll.size_x-1 == list.w) &&
310
		(Form.cheight-TOOLBAR_H == list.h) &&
311
		(list.count)
312
	)
313
	{
314
		DrawPage();
315
	} else {
316
		PreparePage();
317
	}
318
	DrawRectangle(scroll.start_x, scroll.start_y, scroll.size_x, scroll.size_y-1, scroll.bckg_col);
5923 leency 319
}
5980 leency 320
 
321
void DrawPage()
322
{
6808 leency 323
	kfont.ShowBufferPart(list.x, list.y, list.w, list.h, list.first*list.item_h*list.w);
5980 leency 324
	DrawScroller();
325
}