1 /** DGui project file.
2 
3 Copyright: Trogu Antonio Davide 2011-2013
4 
5 License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0).
6 
7 Authors: Trogu Antonio Davide
8 */
9 module dguihub.resources;
10 
11 import dguihub.core.charset;
12 import dguihub.core.winapi;
13 import dguihub.core.geometry;
14 import dguihub.core.utils;
15 import dguihub.core.exception;
16 import dguihub.canvas;
17 
18 final class Resources {
19    private static Resources _rsrc;
20 
21    private this() {
22 
23    }
24 
25    public Icon getIcon(ushort id) {
26       return getIcon(id, nullSize);
27    }
28 
29    public Icon getIcon(ushort id, Size sz) {
30       HICON hIcon = loadImage(getHInstance(), cast(wchar*)id, IMAGE_ICON,
31             sz.width, sz.height, LR_LOADTRANSPARENT | (sz == nullSize ? LR_DEFAULTSIZE : 0));
32 
33       if (!hIcon) {
34          throwException!(GDIException)("Cannot load Icon: '%d'", id);
35       }
36 
37       return Icon.fromHICON(hIcon);
38    }
39 
40    public Bitmap getBitmap(ushort id) {
41       HBITMAP hBitmap = loadImage(getHInstance(), cast(wchar*)id,
42             IMAGE_BITMAP, 0, 0, LR_LOADTRANSPARENT | LR_DEFAULTSIZE);
43 
44       if (!hBitmap) {
45          throwException!(GDIException)("Cannot load Bitmap: '%d'", id);
46       }
47 
48       return Bitmap.fromHBITMAP(hBitmap);
49    }
50 
51    public T* getRaw(T)(ushort id, char* rt) {
52       HRSRC hRsrc = FindResourceW(null, MAKEINTRESOURCEW(id), rt);
53 
54       if (!hRsrc) {
55          throwException!(GDIException)("Cannot load Custom Resource: '%d'", id);
56       }
57 
58       return cast(T*)LockResource(LoadResource(null, hRsrc));
59    }
60 
61    @property public static Resources instance() {
62       if (!_rsrc) {
63          _rsrc = new Resources();
64       }
65 
66       return _rsrc;
67    }
68 }