Subversion Repositories Kolibri OS

Rev

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

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