xref: /haiku/src/apps/icon-o-matic/generic/support/support_ui.cpp (revision 1acbe440b8dd798953bec31d18ee589aa3f71b73)
1 /*
2  * Copyright 2006, Haiku.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Stephan Aßmus <superstippi@gmx.de>
7  */
8 
9 #include "support_ui.h"
10 
11 #include <stdio.h>
12 #include <string.h>
13 
14 #include <Bitmap.h>
15 #include <DataIO.h>
16 #include <Directory.h>
17 #include <File.h>
18 #include <FindDirectory.h>
19 #include <Screen.h>
20 #include <String.h>
21 #include <Path.h>
22 #include <View.h>
23 
24 // stroke_frame
25 void
26 stroke_frame(BView* v, BRect r, rgb_color left, rgb_color top,
27 			 rgb_color right, rgb_color bottom)
28 {
29 	if (v && r.IsValid()) {
30 		v->BeginLineArray(4);
31 			v->AddLine(BPoint(r.left, r.bottom),
32 					   BPoint(r.left, r.top), left);
33 			v->AddLine(BPoint(r.left + 1.0, r.top),
34 					   BPoint(r.right, r.top), top);
35 			v->AddLine(BPoint(r.right, r.top + 1.0),
36 					   BPoint(r.right, r.bottom), right);
37 			v->AddLine(BPoint(r.right - 1.0, r.bottom),
38 					   BPoint(r.left + 1.0, r.bottom), bottom);
39 		v->EndLineArray();
40 	}
41 }
42 
43 // store_color_in_message
44 status_t
45 store_color_in_message(BMessage* message, rgb_color color)
46 {
47 	status_t ret = B_BAD_VALUE;
48 	if (message) {
49 		ret = message->AddData("RGBColor", B_RGB_COLOR_TYPE,
50 							   (void*)&color, sizeof(rgb_color));
51 	}
52 	return ret;
53 }
54 
55 // restore_color_from_message
56 status_t
57 restore_color_from_message(const BMessage* message, rgb_color& color, int32 index)
58 {
59 	status_t ret = B_BAD_VALUE;
60 	if (message) {
61 			const void* colorPointer;
62 			ssize_t size = sizeof(rgb_color);
63 			ret = message->FindData("RGBColor", B_RGB_COLOR_TYPE, index,
64 									&colorPointer, &size);
65 			if (ret >= B_OK)
66 				color = *(const rgb_color*)colorPointer;
67 	}
68 	return ret;
69 }
70 
71 // make_color_drop_message
72 BMessage
73 make_color_drop_message(rgb_color color, BBitmap* bitmap)
74 {
75 	// prepare message
76 	BMessage message(B_PASTE);
77 	char hexstr[7];
78 	sprintf(hexstr, "#%.2X%.2X%.2X", color.red, color.green, color.blue);
79 	message.AddData("text/plain", B_MIME_TYPE, &hexstr, sizeof(hexstr));
80 	message.AddData("RGBColor", B_RGB_COLOR_TYPE, &color, sizeof(color));
81 	// prepare bitmap
82 	if (bitmap && bitmap->IsValid()
83 		&& (bitmap->ColorSpace() == B_RGB32
84 			|| bitmap->ColorSpace() == B_RGBA32)) {
85 		uint8* bits = (uint8*)bitmap->Bits();
86 		uint32 bpr = bitmap->BytesPerRow();
87 		uint32 width = bitmap->Bounds().IntegerWidth() + 1;
88 		uint32 height = bitmap->Bounds().IntegerHeight() + 1;
89 		for (uint32 y = 0; y < height; y++) {
90 			uint8* bitsHandle = bits;
91 			for (uint32 x = 0; x < width; x++) {
92 				if (x == 0 || y == 0 ) {
93 					// top or left border
94 					bitsHandle[0] = (uint8)min_c(255, color.blue * 1.2 + 40);
95 					bitsHandle[1] = (uint8)min_c(255, color.green * 1.2 + 40);
96 					bitsHandle[2] = (uint8)min_c(255, color.red * 1.2 + 40);
97 					bitsHandle[3] = 180;
98 				} else if ((x == width - 2 || y == height - 2)
99 						   && !(x == width - 1 || y == height - 1)) {
100 					// bottom or right border
101 					bitsHandle[0] = (uint8)(color.blue * 0.8);
102 					bitsHandle[1] = (uint8)(color.green * 0.8);
103 					bitsHandle[2] = (uint8)(color.red * 0.8);
104 					bitsHandle[3] = 180;
105 				} else if (x == width - 1 || y == height - 1) {
106 					// shadow
107 					bitsHandle[0] = 0;
108 					bitsHandle[1] = 0;
109 					bitsHandle[2] = 0;
110 					bitsHandle[3] = 100;
111 				} else {
112 					// color
113 					bitsHandle[0] = color.blue;
114 					bitsHandle[1] = color.green;
115 					bitsHandle[2] = color.red;
116 					bitsHandle[3] = 180;
117 				}
118 				if ((x == 0 && y == height - 1) || (y == 0 && x == width - 1)) {
119 					// spare pixels of shadow
120 					bitsHandle[0] = 0;
121 					bitsHandle[1] = 0;
122 					bitsHandle[2] = 0;
123 					bitsHandle[3] = 50;
124 				}
125 				bitsHandle += 4;
126 			}
127 			bits += bpr;
128 		}
129 	}
130 	return message;
131 }
132 
133 // make_sure_frame_is_on_screen
134 void
135 make_sure_frame_is_on_screen(BRect& frame, BWindow* window)
136 {
137 	if (!frame.IsValid())
138 		return;
139 
140 	BRect screenFrame;
141 	if (window) {
142 		BScreen screen(window);
143 		if (!screen.IsValid())
144 			return;
145 		screenFrame = screen.Frame();
146 	} else {
147 		BScreen screen(B_MAIN_SCREEN_ID);
148 		if (!screen.IsValid())
149 			return;
150 		screenFrame = screen.Frame();
151 	}
152 	if (!screenFrame.Contains(frame)) {
153 		// make sure frame fits in the screen
154 		if (frame.Width() > screenFrame.Width())
155 			frame.right -= frame.Width() - screenFrame.Width() + 10.0;
156 		if (frame.Height() > screenFrame.Height())
157 			frame.bottom -= frame.Height() - screenFrame.Height() + 30.0;
158 		// frame is now at the most the size of the screen
159 		if (frame.right > screenFrame.right)
160 			frame.OffsetBy(-(frame.right - screenFrame.right), 0.0);
161 		if (frame.bottom > screenFrame.bottom)
162 			frame.OffsetBy(0.0, -(frame.bottom - screenFrame.bottom));
163 		if (frame.left < screenFrame.left)
164 			frame.OffsetBy((screenFrame.left - frame.left), 0.0);
165 		if (frame.top < screenFrame.top)
166 			frame.OffsetBy(0.0, (screenFrame.top - frame.top));
167 	}
168 }
169 
170 // print_modifiers
171 void
172 print_modifiers()
173 {
174 	uint32 mods = modifiers();
175 	if (mods & B_SHIFT_KEY)
176 		printf("B_SHIFT_KEY\n");
177 	if (mods & B_COMMAND_KEY)
178 		printf("B_COMMAND_KEY\n");
179 	if (mods & B_CONTROL_KEY)
180 		printf("B_CONTROL_KEY\n");
181 	if (mods & B_CAPS_LOCK)
182 		printf("B_CAPS_LOCK\n");
183 	if (mods & B_SCROLL_LOCK)
184 		printf("B_SCROLL_LOCK\n");
185 	if (mods & B_NUM_LOCK)
186 		printf("B_NUM_LOCK\n");
187 	if (mods & B_OPTION_KEY)
188 		printf("B_OPTION_KEY\n");
189 	if (mods & B_MENU_KEY)
190 		printf("B_MENU_KEY\n");
191 	if (mods & B_LEFT_SHIFT_KEY)
192 		printf("B_LEFT_SHIFT_KEY\n");
193 	if (mods & B_RIGHT_SHIFT_KEY)
194 		printf("B_RIGHT_SHIFT_KEY\n");
195 	if (mods & B_LEFT_COMMAND_KEY)
196 		printf("B_LEFT_COMMAND_KEY\n");
197 	if (mods & B_RIGHT_COMMAND_KEY)
198 		printf("B_RIGHT_COMMAND_KEY\n");
199 	if (mods & B_LEFT_CONTROL_KEY)
200 		printf("B_LEFT_CONTROL_KEY\n");
201 	if (mods & B_RIGHT_CONTROL_KEY)
202 		printf("B_RIGHT_CONTROL_KEY\n");
203 	if (mods & B_LEFT_OPTION_KEY)
204 		printf("B_LEFT_OPTION_KEY\n");
205 	if (mods & B_RIGHT_OPTION_KEY)
206 		printf("B_RIGHT_OPTION_KEY\n");
207 }
208 
209 /*
210 // convert_cap_mode
211 agg::line_cap_e
212 convert_cap_mode(uint32 mode)
213 {
214 	agg::line_cap_e aggMode = agg::butt_cap;
215 	switch (mode) {
216 		case CAP_MODE_BUTT:
217 			aggMode = agg::butt_cap;
218 			break;
219 		case CAP_MODE_SQUARE:
220 			aggMode = agg::square_cap;
221 			break;
222 		case CAP_MODE_ROUND:
223 			aggMode = agg::round_cap;
224 			break;
225 	}
226 	return aggMode;
227 }
228 
229 // convert_cap_mode
230 agg::line_join_e
231 convert_join_mode(uint32 mode)
232 {
233 	agg::line_join_e aggMode = agg::miter_join;
234 	switch (mode) {
235 		case JOIN_MODE_MITER:
236 			aggMode = agg::miter_join;
237 			break;
238 		case JOIN_MODE_ROUND:
239 			aggMode = agg::round_join;
240 			break;
241 		case JOIN_MODE_BEVEL:
242 			aggMode = agg::bevel_join;
243 			break;
244 	}
245 	return aggMode;
246 }
247 */
248 
249 // string_for_color_space
250 const char*
251 string_for_color_space(color_space format)
252 {
253 	const char* name = "<unkown format>";
254 	switch (format) {
255 		case B_RGB32:
256 			name = "B_RGB32";
257 			break;
258 		case B_RGBA32:
259 			name = "B_RGBA32";
260 			break;
261 		case B_RGB32_BIG:
262 			name = "B_RGB32_BIG";
263 			break;
264 		case B_RGBA32_BIG:
265 			name = "B_RGBA32_BIG";
266 			break;
267 		case B_RGB24:
268 			name = "B_RGB24";
269 			break;
270 		case B_RGB24_BIG:
271 			name = "B_RGB24_BIG";
272 			break;
273 		case B_CMAP8:
274 			name = "B_CMAP8";
275 			break;
276 		case B_GRAY8:
277 			name = "B_GRAY8";
278 			break;
279 		case B_GRAY1:
280 			name = "B_GRAY1";
281 			break;
282 
283 		// YCbCr
284 		case B_YCbCr422:
285 			name = "B_YCbCr422";
286 			break;
287 		case B_YCbCr411:
288 			name = "B_YCbCr411";
289 			break;
290 		case B_YCbCr444:
291 			name = "B_YCbCr444";
292 			break;
293 		case B_YCbCr420:
294 			name = "B_YCbCr420";
295 			break;
296 
297 		// YUV
298 		case B_YUV422:
299 			name = "B_YUV422";
300 			break;
301 		case B_YUV411:
302 			name = "B_YUV411";
303 			break;
304 		case B_YUV444:
305 			name = "B_YUV444";
306 			break;
307 		case B_YUV420:
308 			name = "B_YUV420";
309 			break;
310 
311 		case B_YUV9:
312 			name = "B_YUV9";
313 			break;
314 		case B_YUV12:
315 			name = "B_YUV12";
316 			break;
317 
318 		default:
319 			break;
320 	}
321 	return name;
322 }
323 
324 // print_color_space
325 void
326 print_color_space(color_space format)
327 {
328 	printf("%s\n", string_for_color_space(format));
329 }
330 
331