Subversion Repositories Kolibri OS

Rev

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

Rev Author Line No. Line
9396 leency 1
#define MEMSIZE 1024*100
2
 
3
#include "../lib/gui.h"
4
#include "../lib/random.h"
5
#include "../lib/mem.h"
6
#include "../lib/cursor.h"
7
#include "../lib/list_box.h"
8
#include "../lib/events.h"
9
 
10
#include "../lib/obj/libimg.h"
11
#include "../lib/obj/box_lib.h"
12
#include "../lib/obj/proc_lib.h"
13
 
14
#include "../lib/patterns/rgb.h"
15
#include "../lib/patterns/toolbar_button.h"
16
#include "../lib/patterns/simple_open_dialog.h"
17
 
18
//===================================================//
19
//                                                   //
20
//                       DATA                        //
21
//                                                   //
22
//===================================================//
23
 
24
#define PAD 12
25
#define TOOLBAR_ITEM_H PAD+PAD
26
#define TOOLBAR_W 110
27
#define ISIZE 18
28
 
29
block canvas = { TOOLBAR_W + PAD + PAD, 0, NULL, NULL };
30
 
31
EVENTS button;
32
EVENTS key;
33
 
34
proc_info Form;
35
dword semi_white;
36
bool bg_dark=false;
37
 
38
char default_dir[4096] = "/rd/1";
39
od_filter filter2 = { 69, "BMP\0GIF\0ICO\0CUR\0JPEG\0JPG\0PNG\0PNM\0TGA\0TIFF\0TIF\0WBMP\0XBM\0XCF\Z80\0\0" };
40
 
41
libimg_image icons18;
42
libimg_image main_image;
43
 
44
char win_title[256] = "ImageEdit";
45
 
46
scroll_bar scroll_v = { 15,NULL,NULL,NULL,15,2,NULL,0,0,0xeeeeee,0xBBBbbb,0xeeeeee};
47
scroll_bar scroll_h = { NULL,NULL,15,NULL,15,2,NULL,0,0,0xeeeeee,0xBBBbbb,0xeeeeee};
48
 
49
/*
50
struct scroll_bar
51
{
52
	word size_x, start_x, size_y, start_y;
53
	dword btn_height, type, max_area, cur_area, position,
54
	bckg_col, frnt_col, line_col, redraw;
55
	word delta, delta2, r_size_x, r_start_x, r_size_y, r_start_y;
56
	dword m_pos, m_pos_2, m_keys, run_size, position2, work_size, all_redraw, ar_offset;
57
};
58
*/
59
 
60
//===================================================//
61
//                                                   //
62
//                       CODE                        //
63
//                                                   //
64
//===================================================//
65
 
66
void init_ui()
67
{
68
	sc.get();
69
	semi_white = MixColors(sc.work, 0xFFFfff, bg_dark*90 + 96);
70
	bg_dark = skin_is_dark();
71
	icons18.load("/sys/icons16.png");
72
	icons18.replace_color(0xffFFFfff, semi_white);
73
	icons18.replace_color(0xffCACBD6, MixColors(sc.work, 0, 200));
74
}
75
 
76
void main()
77
{
78
	int pressed_button_id;
79
	load_dll(libimg, #libimg_init, 1);
80
	load_dll(boxlib, #box_lib_init,0);
81
	load_dll(Proc_lib, #OpenDialog_init,0);
82
	OpenDialog_init stdcall (#o_dialog);
83
 
84
	init_ui();
85
	open_image("/sys/home.png");
86
 
87
	SetEventMask(EVM_REDRAW+EVM_KEY+EVM_BUTTON+EVM_MOUSE+EVM_MOUSE_FILTER);
88
	loop() switch(WaitEvent())
89
	{
90
		case evMouse:
91
			mouse.get();
92
			scrollbar_v_mouse stdcall(#scroll_v);
93
			scrollbar_h_mouse stdcall(#scroll_h);
94
			if (scroll_v.delta) || (scroll_h.delta) draw_canvas();
95
			break;
96
 
97
		case evButton:
98
			pressed_button_id = GetButtonID();
99
			if (pressed_button_id==1) ExitProcess();
100
			button.press(pressed_button_id);
101
			break;
102
 
103
		case evKey:
104
			GetKeys();
105
			if (key_scancode == SCAN_CODE_DOWN) {
106
				scroll_v.position = math.min(scroll_v.position+25, scroll_v.max_area - scroll_v.cur_area);
107
				draw_canvas();
108
			}
109
			if (key_scancode == SCAN_CODE_UP) {
110
				scroll_v.position = math.max(scroll_v.position-25, 0);
111
				draw_canvas();
112
			}
113
			break;
114
 
115
		case evReDraw:
116
			draw_window();
117
			break;
118
	}
119
}
120
 
121
void draw_window()
122
{
123
	incn tx;
124
	DefineAndDrawWindow(random(100)+40, 40+random(100), screen.width/3*2, screen.height/3*2, 0x73, NULL, #win_title, 0);
125
	GetProcessInfo(#Form, SelfInfo);
126
	if (Form.status_window&ROLLED_UP) return;
127
	if (Form.width  < 560) { MoveSize(OLD,OLD,560,OLD); return; }
128
	if (Form.height < 430) { MoveSize(OLD,OLD,OLD,430); return; }
129
	button.init(40);
130
	key.init(40);
131
 
132
	DrawBar(0, 0, canvas.x, Form.cheight, sc.work);
133
 
134
	canvas.w = Form.cwidth - canvas.x;
135
	canvas.h = Form.cheight;
136
	if (main_image.h > canvas.h) canvas.w -= scroll_v.size_x + 1;
137
	if (main_image.w > canvas.w) canvas.h -= scroll_h.size_y + 1;
138
 
139
	DrawBar(canvas.x, 0, 1, canvas.h, sc.work_graph);
140
	if (main_image.h > canvas.h) && (main_image.w > canvas.w) {
141
		DrawBar(canvas.x+canvas.w, canvas.y+canvas.h, scroll_v.size_x+1, scroll_h.size_y+1, sc.work);
142
	}
143
 
144
	scroll_v.all_redraw = scroll_h.all_redraw = 1;
145
	scroll_v.bckg_col = scroll_h.bckg_col = MixColors(sc.work, 0xBBBbbb, 80);
146
	scroll_v.frnt_col = scroll_h.frnt_col = MixColors(sc.work,0xFFFfff,120);
147
	scroll_v.line_col = scroll_h.line_col = sc.work_graph;
148
 
149
	#define GAP_S 24+7
150
	#define GAP_B 24+23
151
	tx.set(PAD-GAP_S);
152
	//draw_icon(10, ECTRL + SCAN_CODE_KEY_N, PAD, tx.inc(GAP_S), 02, "Create image");
153
	draw_icon(#event_open, ECTRL + SCAN_CODE_KEY_O, PAD, tx.inc(GAP_S), 00, "Open image");
154
	//draw_icon(13, ECTRL + SCAN_CODE_LEFT,  PAD, tx.inc(GAP_B), 30);
155
	//draw_icon(14, ECTRL + SCAN_CODE_RIGHT, PAD, tx.inc(GAP_S), 31);
156
	//draw_icon(15, ECTRL + SCAN_CODE_UP,    PAD, tx.inc(GAP_S), 32);
157
	//draw_icon(16, ECTRL + SCAN_CODE_DOWN,  PAD, tx.inc(GAP_S), 33);
158
 
159
 	//draw_icon(#event_save,     ECTRL + SCAN_CODE_KEY_S, PAD, tx.inc(GAP_B), 05, "Save file");
160
 	//draw_icon(12, 0,                                    PAD, tx.inc(GAP_B), 05, "PNG");
161
	//draw_icon(12, 0,                                    PAD, tx.inc(GAP_S), 05, "BMP");
162
	//draw_icon(12, 0,                                    PAD, tx.inc(GAP_S), 05, "RAW");
163
	draw_icon(#event_save,     ECTRL + SCAN_CODE_KEY_S, PAD, tx.inc(GAP_S), 05, "Save as PNG");
164
 
165
 	draw_icon(0, 0,                       PAD, tx.inc(GAP_B), 46, "Crop");
166
	draw_icon(0, 0,                       PAD, tx.inc(GAP_S), 06, "Resize");
167
	draw_icon(0, 0,                       PAD, tx.inc(GAP_S), 52, "Color depth");
168
	draw_icon(#event_flip_hor, ECTRL + SCAN_CODE_KEY_H, PAD,     tx.inc(GAP_S), 34, NULL);
169
	draw_icon(#event_flip_ver, ECTRL + SCAN_CODE_KEY_V, PAD*4+3, tx.n,          35, NULL);
170
	draw_icon(#event_rotate,   ECTRL + SCAN_CODE_KEY_R, PAD*7+6, tx.n,          36, NULL);
171
 
172
	draw_image_info(tx.inc(GAP_B));
173
	draw_canvas();
174
}
175
 
176
char* libimg_bpp[] = { "8 pal", "24", "32", "15", "16",
177
"1 mono", "8 gray", "2 pal", "4 pal", "8 pal" };
178
 
179
void draw_image_info(int _y)
180
{
181
	WriteText(PAD, _y, 0x90, sc.work_text, "Properties");
182
	DrawBar(PAD, _y+14, TOOLBAR_W, 1, sc.work_graph);
183
	WriteText(PAD, _y+22, 0x90, sc.work_text, "Width:");
184
	WriteText(PAD, _y+42, 0x90, sc.work_text, "Heigh:");
185
	WriteText(PAD, _y+62, 0x90, sc.work_text, "Depth:");
186
 
187
	WriteText(PAD+60, _y+22, 0x90, sc.work_text, itoa(main_image.w));
188
	WriteText(PAD+60, _y+42, 0x90, sc.work_text, itoa(main_image.h));
189
	WriteText(PAD+60, _y+62, 0x90, sc.work_text, libimg_bpp[main_image.type-1]);
190
}
191
 
192
void draw_icon(dword _event, _key, _x, _y, _icon_n, _text)
193
{
194
	int w;
195
	if (_text) w = TOOLBAR_W; else w = PAD + PAD + 8;
196
	DrawBar(_x, _y, w, TOOLBAR_ITEM_H+1, semi_white);
197
	PutPixel(_x,_y,sc.work);
198
	PutPixel(_x,_y+TOOLBAR_ITEM_H,sc.work);
199
	PutPixel(_x+w-1,_y,sc.work);
200
	PutPixel(_x+w-1,_y+TOOLBAR_ITEM_H,sc.work);
201
	if (_event) DefineHiddenButton(_x, _y, w, TOOLBAR_ITEM_H, button.add(_event));
202
	if (_text) WriteText(_x+PAD+ISIZE+2, _y+9, 0x80, sc.work_text, _text);
203
	img_draw stdcall(icons18.image, _x+7, _y+3, ISIZE, ISIZE, 0, _icon_n*ISIZE);
204
}
205
 
206
void draw_scroll_v()
207
{
208
	scroll_v.max_area = main_image.h;
209
	scroll_v.cur_area = scroll_v.size_y = canvas.h;
210
	scroll_v.start_x = canvas.x + canvas.w;
211
	scroll_v.start_y = 0;
212
	if (main_image.h > canvas.h) scrollbar_v_draw stdcall (#scroll_v);
213
}
214
 
215
void draw_scroll_h()
216
{
217
	scroll_h.max_area = main_image.w;
218
	scroll_h.cur_area = scroll_h.size_x = canvas.w;
219
	scroll_h.start_x = canvas.x;
220
	scroll_h.start_y = canvas.y + canvas.h;
221
	if (main_image.w > canvas.w) scrollbar_h_draw stdcall (#scroll_h);
222
}
223
 
224
void draw_canvas()
225
{
226
	int content_w, content_h;
227
	content_w = math.min(main_image.w, canvas.w-1);
228
	content_h = math.min(main_image.h, canvas.h);
229
 
230
	if (main_image.image) {
231
		img_draw stdcall(main_image.image, canvas.x+1, canvas.y,
232
		content_w, content_h, scroll_h.position, scroll_v.position);
233
 	}
234
 
235
	DrawBar(canvas.x+1+content_w, canvas.y, canvas.w - content_w - 1, content_h, 0xBFCAD2);
236
	DrawBar(canvas.x+1, canvas.y+content_h, canvas.w - 1, canvas.h - content_h, 0xBFCAD2);
237
 
238
	draw_scroll_v();
239
	draw_scroll_h();
240
}
241
 
242
void update_title(char* _new_title)
243
{
244
	strcpy(#win_title+9, " - ");
245
	strlcpy(#win_title+12, _new_title, sizeof(win_title));
246
	DrawTitle(#win_title);
247
}
248
 
249
void open_image(char* _path)
250
{
251
	main_image.load(_path);
252
	update_title(_path);
253
	scroll_v.position = 0;
254
	scroll_h.position = 0;
255
}
256
 
257
//===================================================//
258
//                                                   //
259
//                      EVENTS                       //
260
//                                                   //
261
//===================================================//
262
 
263
void event_open()
264
{
265
	o_dialog.type = 0; //open file
266
	OpenDialog_start stdcall (#o_dialog);
267
	if (o_dialog.status) {
268
		open_image(#openfile_path);
269
		draw_canvas();
270
	}
271
}
272
 
273
void event_flip_hor()
274
{
275
	img_flip stdcall (main_image.image, FLIP_HORIZONTAL);
276
	draw_canvas();
277
}
278
 
279
void event_flip_ver()
280
{
281
	img_flip stdcall (main_image.image, FLIP_VERTICAL);
282
	draw_canvas();
283
}
284
 
285
void event_rotate()
286
{
287
	img_rotate stdcall (main_image.image, ROTATE_90_CW);
288
	main_image.w >< main_image.h;
289
	draw_window();
290
}
291
 
292
void event_save()
293
{
294
	o_dialog.type = 1; //save file
295
	strcpy(#filename_area, "image.png");
296
	OpenDialog_start stdcall (#o_dialog);
297
	if (o_dialog.status) {
298
		update_title(#openfile_path);
299
		img_to_rgb stdcall (main_image.image);
300
		save_image(main_image.imgsrc, main_image.w, main_image.h, #openfile_path);
301
	}
302
}
303
 
304
stop:
305