Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
5819 leency 1
#define MEMSIZE 4096*20
2
 
3
#include "../lib/font.h"
4
#include "../lib/gui.h"
5
#include "../lib/list_box.h"
6
#include "../lib/obj/box_lib.h"
7
#include "../lib/obj/libini.h"
8
#include "../lib/obj/iconv.h"
9
#include "../lib/obj/proc_lib.h"
10
#include "../lib/patterns/libimg_load_skin.h"
11
#include "../lib/patterns/simple_open_dialog.h"
12
 
13
#define TOOLBAR_H 34
14
 
15
char default_dir[] = "/rd/1";
16
od_filter filter2 = {0,0};
17
 
18
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};
19
llist list;
20
 
21
proc_info Form;
22
char title[4196];
23
 
24
byte help_opened = false;
25
 
26
enum {
27
	OPEN_FILE,
28
	MAGNIFY_MINUS,
29
	MAGNIFY_PLUS,
30
	CHANGE_ENCODING,
31
	RUN_EDIT,
32
	SHOW_INFO,
33
};
34
 
35
#include "ini.h"
36
#include "menu.h"
37
 
38
void main()
39
{
40
	byte btn;
41
	load_dll(boxlib, #box_lib_init,0);
42
	load_dll(libio, #libio_init,1);
43
	load_dll(libimg, #libimg_init,1);
44
	load_dll(libini, #lib_init,1);
45
	load_dll(iconv_lib, #iconv_open,0);
46
	load_dll(Proc_lib, #OpenDialog_init,0);
47
	OpenDialog_init stdcall (#o_dialog);
48
	font.no_bg_copy = true; font.color = 0; font.bg_color = 0xFFFFFF;
49
	font.load("/sys/fonts/Tahoma.kf"); if (!font.data) { io.run("/sys/@notify","'Error: Font is not loaded.' -E"); ExitProcess(); }
50
	Libimg_LoadImage(#skin, abspath("toolbar.png"));
51
	LoadIniSettings();
52
	OpenFile(#param);
53
	list.no_selection = true;
54
	SetEventMask(10000000000000000000000001100111b);
55
	loop()
56
	{
57
	switch(WaitEvent())
58
	{
59
	   	case evMouse:
60
      		mouse.get();
61
      		list.wheel_size = 7;
62
			if (list.MouseScroll(mouse.vert)) { DrawPage(); break; }
63
			scrollbar_v_mouse (#scroll); if (list.first != scroll.position) { list.first = scroll.position; DrawPage(); }
64
			break;
65
		case evKey:
66
			if (help_opened) { help_opened=false; DrawPage(); break; }
67
			GetKeys();
68
			if (key_scancode==059) goto _SHOW_INFO;
69
			if (key_modifier&KEY_LCTRL) || (key_modifier&KEY_RCTRL) {
70
				if (key_scancode==024) goto _OPEN_FILE;
71
				if (key_scancode==SCAN_CODE_UP) goto _MAGNIFY_PLUS;
72
				if (key_scancode==SCAN_CODE_DOWN) goto _MAGNIFY_MINUS;
73
				if (key_scancode==018) goto _RUN_EDIT;
74
				if (key_scancode==SCAN_CODE_TAB) goto _CHANGE_ENCODING;
75
				break;
76
			}
77
			if (list.ProcessKey(key_scancode)) DrawPage();
78
			break;
79
		case evButton:
80
			btn = GetButtonID();
81
			if (btn==1) { SaveIniSettings(); ExitProcess(); }
82
			btn-=10;
83
			if (btn==OPEN_FILE) { _OPEN_FILE: OpenDialog_start stdcall (#o_dialog); OpenFile(#openfile_path); PreparePage(); }
84
			else if (btn==MAGNIFY_PLUS)  { _MAGNIFY_PLUS: font.size.text++; if(!font.changeSIZE())font.size.text--; else PreparePage(); }
85
			else if (btn==MAGNIFY_MINUS) { _MAGNIFY_MINUS: font.size.text--; if(!font.changeSIZE())font.size.text++; else PreparePage(); }
86
			else if (btn==CHANGE_ENCODING) { _CHANGE_ENCODING: CreateThread(#menu_rmb,#stak+4092); break; }
87
			else if (btn==RUN_EDIT) { _RUN_EDIT: io.run("/sys/tinypad",#param); }
88
			else if (btn==SHOW_INFO) { _SHOW_INFO: ShowAbout(); }
89
			break;
90
		case evReDraw:
91
			if (action_buf) {
92
				OpenFile(#param);
93
				PreparePage();
94
				action_buf = false;
95
			};
96
			draw_window();
97
	  }
98
	}
99
}
100
 
101
void draw_window()
102
{
103
	DefineAndDrawWindow(Form.left,Form.top,Form.width,Form.height,0x73,0,#title);
104
	GetProcessInfo(#Form, SelfInfo);
105
	if (Form.status_window>2) return;
106
	if (Form.width  < 200) { MoveSize(OLD,OLD,200,OLD); return; }
107
	if (Form.height < 200) { MoveSize(OLD,OLD,OLD,200); return; }
108
	DrawBar(0, 0, Form.cwidth, TOOLBAR_H - 1, 0xe1e1e1);
109
	DrawBar(0, TOOLBAR_H - 1, Form.cwidth, 1, 0x7F7F7F);
110
	DrawToolbarButton(OPEN_FILE, 8);
111
	DrawToolbarButton(MAGNIFY_PLUS, 42);
112
	DrawToolbarButton(MAGNIFY_MINUS, 67);
113
	DrawToolbarButton(CHANGE_ENCODING, 101);
114
	DrawToolbarButton(RUN_EDIT, 135);
115
	DrawToolbarButton(SHOW_INFO, Form.cwidth - 34);
116
	if (Form.cwidth-scroll.size_x-1 == list.w) && (Form.cheight-TOOLBAR_H == list.h) && (list.count) DrawPage(); else PreparePage();
117
	DrawRectangle(scroll.start_x, scroll.start_y, scroll.size_x, scroll.size_y-1, scroll.bckg_col);
118
}
119
 
120
void DrawPage()
121
{
122
	_PutImage(list.x,list.y,list.w,list.h,list.first*list.line_h*list.w*3 + font.buffer);
123
	DrawScroller();
124
}
125
 
126
void PreparePage()
127
{
128
	char line[4096]=0;
129
	dword line_start;
130
	byte ch;
131
	dword bufoff;
132
	dword line_length=30;
133
	dword stroka_y = 5;
134
	dword stroka=0;
135
	char ch_width[255];
136
	int i, srch_pos;
137
	font.changeSIZE();
138
	list.w = Form.cwidth-scroll.size_x-1;
139
	//get font chars width, need to increase performance
140
	for (i=0; i<256; i++) ch_width[i] = font.symbol_size(i);
141
	//get font buffer height
142
	for (bufoff=io.buffer_data; ESBYTE[bufoff]; bufoff++)
143
	{
144
		ch = ESBYTE[bufoff];
145
		line_length += ch_width[ch];
146
		if (line_length>=list.w) || (ch==10) {
147
			srch_pos = bufoff;
148
			loop()
149
			{
150
				if (__isWhite(ESBYTE[srch_pos])) { bufoff=srch_pos+1; break; } //normal word-break
151
				if (srch_pos == line_start) break; //no white space found in whole line
152
				srch_pos--;
153
			}
154
			line_start = bufoff;
155
			line_length = 30;
156
			stroka++;
157
		}
158
	}
159
	//draw text in buffer
160
	list.count = stroka+2;
161
	list.SetSizes(0, TOOLBAR_H, list.w, Form.cheight-TOOLBAR_H, font.size.text+1);
162
	if (list.count < list.visible) list.count = list.visible;
163
 
164
	font.size.height = list.count+1*list.line_h;
165
	font.buffer_size = 0;
166
 
167
	line_length = 30;
168
	line_start = io.buffer_data;
169
	for (bufoff=io.buffer_data; ESBYTE[bufoff]; bufoff++)
170
	{
171
		ch = ESBYTE[bufoff];
172
		line_length += ch_width[ch];
173
		if (line_length>=list.w) || (ch==10)
174
		{
175
			//set word break
176
			srch_pos = bufoff;
177
			loop()
178
			{
179
				if (__isWhite(ESBYTE[srch_pos])) { bufoff=srch_pos+1; break; } //normal word-break
180
				if (srch_pos == line_start) break; //no white space found in whole line
181
				srch_pos--;
182
			}
183
			i = bufoff-line_start;
184
			strlcpy(#line, line_start, i);
185
			font.prepare_buf(8,stroka_y,list.w,font.size.height, #line);
186
			stroka_y += list.line_h;
187
			line_start = bufoff;
188
			line_length = 30;
189
		}
190
	}
191
	font.prepare_buf(8,stroka_y,list.w,font.size.height, line_start);
192
	SmoothFont(font.buffer, font.size.width, font.size.height);
193
	DrawPage();
194
}
195
 
196
void DrawToolbarButton(char image_id, int x)
197
{
198
	DefineButton(x, 5, 26-1, 24-1, 10+image_id + BT_HIDE, 0);
199
	img_draw stdcall(skin.image, x, 5, 26, 24, 0, image_id*24);
200
}
201
 
202
void DrawScroller()
203
{
204
	scroll.max_area = list.count;
205
	scroll.cur_area = list.visible;
206
	scroll.position = list.first;
207
	scroll.all_redraw = 0;
208
	scroll.start_x = list.x + list.w;
209
	scroll.start_y = list.y;
210
	scroll.size_y = list.h;
211
	scroll.start_x = list.x + list.w;
212
	scrollbar_v_draw(#scroll);
213
}
214
 
215
void OpenFile(dword f_path)
216
{
217
	int tmp;
218
	if (ESBYTE[f_path]) {
219
		strcpy(#param, f_path);
220
		io.read(#param);
221
		strcpy(#title, #param);
222
		strcat(#title, " - Text Reader");
223
	}
224
	else {
225
		if (list.count) return;
226
		io.buffer_data = "This is a plain text reader.\nTry to open some text file.";
227
		strcpy(#title, "Text Reader");
228
	}
229
	if (encoding!=CH_CP866) ChangeCharset(charsets[encoding], "CP866", io.buffer_data);
230
	list.KeyHome();
231
	list.ClearList();
232
}
233
 
234
 
235
 
236
char *about[] = {
237
	"Text Reader v1.0",
238
	"Idea: Leency, punk_joker",
239
	"Code: Leency, KolibriOS Team",
240
	" ",
241
	"Hotkeys:",
242
	"Ctrl+O - open file",
243
	"Ctrl+Up - bigger font",
244
	"Ctrl+Down - smaller font",
245
	"Ctrl+Tab - select charset",
246
	"Ctrl+E - edit current document",
247
	" ",
248
	"Press any key...",
249
 
250
};
251
 
252
ShowAbout() {
253
	int i;
254
	help_opened = true;
255
	DrawBar(list.x, list.y, list.w, list.h, 0xFFFfff);
256
	WriteText(list.x + 10, list.y + 10, 10000001b, 0x555555, about[0]);
257
	for (i=1; about[i]; i++) WriteText(list.x + 10, i+1*20 + list.y, 10110000b, 0, about[i]);
258
}