File: Icon.cpp

package info (click to toggle)
storm-lang 0.7.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 52,028 kB
  • sloc: ansic: 261,471; cpp: 140,432; sh: 14,891; perl: 9,846; python: 2,525; lisp: 2,504; asm: 860; makefile: 678; pascal: 70; java: 52; xml: 37; awk: 12
file content (133 lines) | stat: -rw-r--r-- 3,442 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include "stdafx.h"
#include "Icon.h"
#include "Utils/Bitwise.h"
#include "Core/SystemError.h"

namespace gui {

#ifdef GUI_WIN32

	// Create the color bitmap.
	static HBITMAP createColorBitmap(Image *image) {
		Nat scanline = image->width() * 4;
		vector<byte> data(scanline * image->height(), 0);

		// Copy pixels, we need to shift R and B as well.
		for (Nat y = 0; y < image->height(); y++) {
			Nat start = y * scanline;
			for (Nat x = 0; x < image->width(); x++) {
				byte *src = image->buffer(x, y);
				data[start + x*4 + 0] = src[2];
				data[start + x*4 + 1] = src[1];
				data[start + x*4 + 2] = src[0];
				data[start + x*4 + 3] = src[3];
			}
		}

		return CreateBitmap(image->width(), image->height(), 1, 32, &data[0]);
	}

	// Create the mask bitmap.
	static HBITMAP createMaskBitmap(Image *image) {
		Nat scanline = roundUp((image->width() + 7) / 8, Nat(2));
		vector<byte> data(scanline * image->height(), 0);

		for (Nat y = 0; y < image->height(); y++) {
			Nat start = scanline * y;
			for (Nat x = 0; x < image->width(); x++) {
				byte *b = image->buffer(x, y);
				if (b[4] == 0) {
					Nat idx = start + x / 8;
					data[idx] |= 1 << (7 - (x % 8));
				}
			}
		}

		return CreateBitmap(image->width(), image->height(), 1, 1, &data[0]);
	}

	// Create a HICON of the desired size.
	static Handle createIcon(ImageSet *from, Nat width, Nat height) {
		Image *image = from->atLeast(width, height);
		// Note: DrawIcon seems to handle shrinking icons as needed.

		HBITMAP maskBitmap = createMaskBitmap(image);
		HBITMAP colorBitmap = createColorBitmap(image);
		// PVAR(systemErrorMessage(from->engine(), GetLastError()));
		// PVAR(colorBitmap);

		ICONINFO info;
		info.fIcon = TRUE;
		info.xHotspot = 0;
		info.yHotspot = 0;
		info.hbmMask = maskBitmap;
		info.hbmColor = colorBitmap;
		HICON created = CreateIconIndirect(&info);

		DeleteObject(colorBitmap);
		DeleteObject(maskBitmap);

		// HDC desktop = GetDC(NULL);
		// DrawIcon(desktop, 1000, 500, created);
		// ReleaseDC(NULL, desktop);

		return created;
	}

	Icon::Icon(ImageSet *from) {
		smallIcon = createIcon(from, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON));
		largeIcon = createIcon(from, GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON));
	}

	Icon::~Icon() {
		DestroyIcon(largeIcon.icon());
		DestroyIcon(smallIcon.icon());
	}

#endif

#ifdef GUI_GTK

	static GdkPixbuf *createPixbuf(Image *image) {
		GdkPixbuf *buf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, true, 8, image->width(), image->height());
		if (!buf)
			return null;

		guchar *dst = gdk_pixbuf_get_pixels(buf);
		int stride = gdk_pixbuf_get_rowstride(buf);

		for (Nat y = 0; y < image->height(); y++) {
			byte *src = image->buffer(0, y);
			memcpy(dst + int(y)*stride, src, image->width() * 4);
		}

		return buf;
	}

	Icon::Icon(ImageSet *from) {
		gtkImages = runtime::allocArray<GdkPixbuf *>(engine(), &pointerArrayType, from->count());
		for (Nat i = 0; i < from->count(); i++) {
			if (GdkPixbuf *buffer = createPixbuf(from->at(i)))
				gtkImages->v[gtkImages->filled++] = buffer;
		}
	}

	Icon::~Icon() {
		for (size_t i = 0; i < gtkImages->filled; i++) {
			if (gtkImages->v[i])
				g_object_unref(gtkImages->v[i]);
		}
	}

	GList *Icon::gtkList() const {
		GList *current = NULL;
		for (Nat i = gtkImages->filled; i > 0; i--) {
			GdkPixbuf *item = gtkImages->v[i - 1];
			current = g_list_prepend(current, item);
		}
		return current;
	}

#endif

}