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 dgui.core.utils;
10 
11 import std.path;
12 import dgui.core.winapi;
13 import dgui.core.charset;
14 
15 enum WindowsVersion
16 {
17 	unknown       = 0,
18 	windows2000  = 1,
19 	windowsXP    = 2,
20 	windowsVista = 4,
21 	windows7     = 8,
22 }
23 
24 T winCast(T)(Object o)
25 {
26 	return cast(T)(cast(void*)o);
27 }
28 
29 T winCast(T)(size_t st)
30 {
31 	return cast(T)(cast(void*)st);
32 }
33 
34 HINSTANCE getHInstance()
35 {
36 	static HINSTANCE hInst = null;
37 
38 	if(!hInst)
39 	{
40 		hInst = GetModuleHandleW(null);
41 	}
42 
43 	return hInst;
44 }
45 
46 string getExecutablePath()
47 {
48 	static string exePath;
49 
50 	if(!exePath.length)
51 	{
52 		exePath = getModuleFileName(null);
53 	}
54 
55 	return exePath;
56 }
57 
58 string getStartupPath()
59 {
60 	static string startPath;
61 
62 	if(!startPath.length)
63 	{
64 		startPath = std.path.dirName(getExecutablePath());
65 	}
66 
67 	return startPath;
68 }
69 
70 string getTempPath()
71 {
72 	static string tempPath;
73 
74 	if(!tempPath.length)
75 	{
76 		dgui.core.charset.getTempPath(tempPath);
77 	}
78 
79 	return tempPath;
80 }
81 
82 string makeFilter(string userFilter)
83 {
84 	char[] newFilter = cast(char[])userFilter;
85 
86 	foreach(ref char ch; newFilter)
87 	{
88 		if(ch == '|')
89 		{
90 			ch = '\0';
91 		}
92 	}
93 
94 	newFilter ~= '\0';
95 	return newFilter.idup;
96 }
97 
98 public WindowsVersion getWindowsVersion()
99 {
100 	static WindowsVersion ver = WindowsVersion.unknown;
101 	static WindowsVersion[uint][uint] versions;
102 
103 	if(ver is WindowsVersion.unknown)
104 	{
105 		if(!versions.length)
106 		{
107 			versions[5][0] = WindowsVersion.windows2000;
108 			versions[5][1] = WindowsVersion.windowsXP;
109 			versions[6][0] = WindowsVersion.windowsVista;
110 			versions[6][1] = WindowsVersion.windows7;
111 		}
112 
113 		OSVERSIONINFOW ovi;
114 		ovi.dwOSVersionInfoSize = OSVERSIONINFOW.sizeof;
115 
116 		GetVersionExW(&ovi);
117 
118 		WindowsVersion[uint]* pMajVer = (ovi.dwMajorVersion in versions);
119 
120 		if(pMajVer)
121 		{
122 			WindowsVersion* pMinVer = (ovi.dwMinorVersion in *pMajVer);
123 
124 			if(pMinVer)
125 			{
126 				ver = versions[ovi.dwMajorVersion][ovi.dwMinorVersion];
127 			}
128 		}
129 	}
130 
131 	return ver;
132 }