Subversion Repositories Kolibri OS

Rev

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