Subversion Repositories Kolibri OS

Rev

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