1 | /** @file
|
---|
2 | * helpers - Guest Additions Service helper functions
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2006-2007 Oracle Corporation
|
---|
7 | *
|
---|
8 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
9 | * available from http://www.virtualbox.org. This file is free software;
|
---|
10 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
11 | * General Public License (GPL) as published by the Free Software
|
---|
12 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
13 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
14 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
15 | */
|
---|
16 |
|
---|
17 | #include <malloc.h>
|
---|
18 | #include <windows.h>
|
---|
19 |
|
---|
20 | #include <iprt/string.h>
|
---|
21 | #include <VBox/VBoxGuestLib.h>
|
---|
22 |
|
---|
23 | #include <VBoxGuestInternal.h>
|
---|
24 |
|
---|
25 | #include "helpers.h"
|
---|
26 | #include "resource.h"
|
---|
27 |
|
---|
28 | static unsigned nextAdjacentRectXP (RECTL *paRects, unsigned nRects, unsigned iRect)
|
---|
29 | {
|
---|
30 | unsigned i;
|
---|
31 | for (i = 0; i < nRects; i++)
|
---|
32 | {
|
---|
33 | if (paRects[iRect].right == paRects[i].left)
|
---|
34 | {
|
---|
35 | return i;
|
---|
36 | }
|
---|
37 | }
|
---|
38 | return ~0;
|
---|
39 | }
|
---|
40 |
|
---|
41 | static unsigned nextAdjacentRectXN (RECTL *paRects, unsigned nRects, unsigned iRect)
|
---|
42 | {
|
---|
43 | unsigned i;
|
---|
44 | for (i = 0; i < nRects; i++)
|
---|
45 | {
|
---|
46 | if (paRects[iRect].left == paRects[i].right)
|
---|
47 | {
|
---|
48 | return i;
|
---|
49 | }
|
---|
50 | }
|
---|
51 | return ~0;
|
---|
52 | }
|
---|
53 |
|
---|
54 | static unsigned nextAdjacentRectYP (RECTL *paRects, unsigned nRects, unsigned iRect)
|
---|
55 | {
|
---|
56 | unsigned i;
|
---|
57 | for (i = 0; i < nRects; i++)
|
---|
58 | {
|
---|
59 | if (paRects[iRect].bottom == paRects[i].top)
|
---|
60 | {
|
---|
61 | return i;
|
---|
62 | }
|
---|
63 | }
|
---|
64 | return ~0;
|
---|
65 | }
|
---|
66 |
|
---|
67 | unsigned nextAdjacentRectYN (RECTL *paRects, unsigned nRects, unsigned iRect)
|
---|
68 | {
|
---|
69 | unsigned i;
|
---|
70 | for (i = 0; i < nRects; i++)
|
---|
71 | {
|
---|
72 | if (paRects[iRect].top == paRects[i].bottom)
|
---|
73 | {
|
---|
74 | return i;
|
---|
75 | }
|
---|
76 | }
|
---|
77 | return ~0;
|
---|
78 | }
|
---|
79 |
|
---|
80 | void resizeRect(RECTL *paRects, unsigned nRects, unsigned iPrimary, unsigned iResized, int NewWidth, int NewHeight)
|
---|
81 | {
|
---|
82 | DDCLOG(("nRects %d, iPrimary %d, iResized %d, NewWidth %d, NewHeight %d\n", nRects, iPrimary, iResized, NewWidth, NewHeight));
|
---|
83 |
|
---|
84 | RECTL *paNewRects = (RECTL *)alloca (sizeof (RECTL) * nRects);
|
---|
85 | memcpy (paNewRects, paRects, sizeof (RECTL) * nRects);
|
---|
86 | paNewRects[iResized].right += NewWidth - (paNewRects[iResized].right - paNewRects[iResized].left);
|
---|
87 | paNewRects[iResized].bottom += NewHeight - (paNewRects[iResized].bottom - paNewRects[iResized].top);
|
---|
88 |
|
---|
89 | /* Verify all pairs of originally adjacent rectangles for all 4 directions.
|
---|
90 | * If the pair has a "good" delta (that is the first rectangle intersects the second)
|
---|
91 | * at a direction and the second rectangle is not primary one (which can not be moved),
|
---|
92 | * move the second rectangle to make it adjacent to the first one.
|
---|
93 | */
|
---|
94 |
|
---|
95 | /* X positive. */
|
---|
96 | unsigned iRect;
|
---|
97 | for (iRect = 0; iRect < nRects; iRect++)
|
---|
98 | {
|
---|
99 | /* Find the next adjacent original rect in x positive direction. */
|
---|
100 | unsigned iNextRect = nextAdjacentRectXP (paRects, nRects, iRect);
|
---|
101 | DDCLOG(("next %d -> %d\n", iRect, iNextRect));
|
---|
102 |
|
---|
103 | if (iNextRect == ~0 || iNextRect == iPrimary)
|
---|
104 | {
|
---|
105 | continue;
|
---|
106 | }
|
---|
107 |
|
---|
108 | /* Check whether there is an X intesection between these adjacent rects in the new rectangles
|
---|
109 | * and fix the intersection if delta is "good".
|
---|
110 | */
|
---|
111 | int delta = paNewRects[iRect].right - paNewRects[iNextRect].left;
|
---|
112 |
|
---|
113 | if (delta != 0)
|
---|
114 | {
|
---|
115 | DDCLOG(("XP intersection right %d left %d, diff %d\n",
|
---|
116 | paNewRects[iRect].right, paNewRects[iNextRect].left,
|
---|
117 | delta));
|
---|
118 |
|
---|
119 | paNewRects[iNextRect].left += delta;
|
---|
120 | paNewRects[iNextRect].right += delta;
|
---|
121 | }
|
---|
122 | }
|
---|
123 |
|
---|
124 | /* X negative. */
|
---|
125 | for (iRect = 0; iRect < nRects; iRect++)
|
---|
126 | {
|
---|
127 | /* Find the next adjacent original rect in x negative direction. */
|
---|
128 | unsigned iNextRect = nextAdjacentRectXN (paRects, nRects, iRect);
|
---|
129 | DDCLOG(("next %d -> %d\n", iRect, iNextRect));
|
---|
130 |
|
---|
131 | if (iNextRect == ~0 || iNextRect == iPrimary)
|
---|
132 | {
|
---|
133 | continue;
|
---|
134 | }
|
---|
135 |
|
---|
136 | /* Check whether there is an X intesection between these adjacent rects in the new rectangles
|
---|
137 | * and fix the intersection if delta is "good".
|
---|
138 | */
|
---|
139 | int delta = paNewRects[iRect].left - paNewRects[iNextRect].right;
|
---|
140 |
|
---|
141 | if (delta != 0)
|
---|
142 | {
|
---|
143 | DDCLOG(("XN intersection left %d right %d, diff %d\n",
|
---|
144 | paNewRects[iRect].left, paNewRects[iNextRect].right,
|
---|
145 | delta));
|
---|
146 |
|
---|
147 | paNewRects[iNextRect].left += delta;
|
---|
148 | paNewRects[iNextRect].right += delta;
|
---|
149 | }
|
---|
150 | }
|
---|
151 |
|
---|
152 | /* Y positive (in the computer sence, top->down). */
|
---|
153 | for (iRect = 0; iRect < nRects; iRect++)
|
---|
154 | {
|
---|
155 | /* Find the next adjacent original rect in y positive direction. */
|
---|
156 | unsigned iNextRect = nextAdjacentRectYP (paRects, nRects, iRect);
|
---|
157 | DDCLOG(("next %d -> %d\n", iRect, iNextRect));
|
---|
158 |
|
---|
159 | if (iNextRect == ~0 || iNextRect == iPrimary)
|
---|
160 | {
|
---|
161 | continue;
|
---|
162 | }
|
---|
163 |
|
---|
164 | /* Check whether there is an Y intesection between these adjacent rects in the new rectangles
|
---|
165 | * and fix the intersection if delta is "good".
|
---|
166 | */
|
---|
167 | int delta = paNewRects[iRect].bottom - paNewRects[iNextRect].top;
|
---|
168 |
|
---|
169 | if (delta != 0)
|
---|
170 | {
|
---|
171 | DDCLOG(("YP intersection bottom %d top %d, diff %d\n",
|
---|
172 | paNewRects[iRect].bottom, paNewRects[iNextRect].top,
|
---|
173 | delta));
|
---|
174 |
|
---|
175 | paNewRects[iNextRect].top += delta;
|
---|
176 | paNewRects[iNextRect].bottom += delta;
|
---|
177 | }
|
---|
178 | }
|
---|
179 |
|
---|
180 | /* Y negative (in the computer sence, down->top). */
|
---|
181 | for (iRect = 0; iRect < nRects; iRect++)
|
---|
182 | {
|
---|
183 | /* Find the next adjacent original rect in x negative direction. */
|
---|
184 | unsigned iNextRect = nextAdjacentRectYN (paRects, nRects, iRect);
|
---|
185 | DDCLOG(("next %d -> %d\n", iRect, iNextRect));
|
---|
186 |
|
---|
187 | if (iNextRect == ~0 || iNextRect == iPrimary)
|
---|
188 | {
|
---|
189 | continue;
|
---|
190 | }
|
---|
191 |
|
---|
192 | /* Check whether there is an Y intesection between these adjacent rects in the new rectangles
|
---|
193 | * and fix the intersection if delta is "good".
|
---|
194 | */
|
---|
195 | int delta = paNewRects[iRect].top - paNewRects[iNextRect].bottom;
|
---|
196 |
|
---|
197 | if (delta != 0)
|
---|
198 | {
|
---|
199 | DDCLOG(("YN intersection top %d bottom %d, diff %d\n",
|
---|
200 | paNewRects[iRect].top, paNewRects[iNextRect].bottom,
|
---|
201 | delta));
|
---|
202 |
|
---|
203 | paNewRects[iNextRect].top += delta;
|
---|
204 | paNewRects[iNextRect].bottom += delta;
|
---|
205 | }
|
---|
206 | }
|
---|
207 |
|
---|
208 | memcpy (paRects, paNewRects, sizeof (RECTL) * nRects);
|
---|
209 | return;
|
---|
210 | }
|
---|
211 |
|
---|
212 | int showBalloonTip (HINSTANCE hInst, HWND hWnd, UINT uID, const char *pszMsg, const char *pszTitle, UINT uTimeout, DWORD dwInfoFlags)
|
---|
213 | {
|
---|
214 | NOTIFYICONDATA niData;
|
---|
215 | niData.cbSize = sizeof(NOTIFYICONDATA);
|
---|
216 | niData.uFlags = NIF_INFO;
|
---|
217 | niData.hWnd = hWnd;
|
---|
218 | niData.uID = uID;
|
---|
219 | niData.uTimeout = uTimeout;
|
---|
220 | if (dwInfoFlags == 0)
|
---|
221 | dwInfoFlags = NIIF_INFO;
|
---|
222 | niData.dwInfoFlags = dwInfoFlags;
|
---|
223 |
|
---|
224 | OSVERSIONINFO info;
|
---|
225 | DWORD dwMajorVersion = 5; /* default XP */
|
---|
226 |
|
---|
227 | info.dwOSVersionInfoSize = sizeof(info);
|
---|
228 | if (FALSE == GetVersionEx(&info))
|
---|
229 | return FALSE;
|
---|
230 |
|
---|
231 | if (info.dwMajorVersion >= 5)
|
---|
232 | {
|
---|
233 | niData.uFlags |= NIF_ICON;
|
---|
234 | if ( info.dwMajorVersion == 5
|
---|
235 | && info.dwMinorVersion == 1) /* WinXP */
|
---|
236 | {
|
---|
237 | //niData.dwInfoFlags = NIIF_USER; /* Use an own icon instead of the default one */
|
---|
238 | niData.hIcon = LoadIcon(hInst, MAKEINTRESOURCE(IDI_VIRTUALBOX));
|
---|
239 | }
|
---|
240 | #ifdef BALLOON_WITH_VISTA_TOOLTIP_ICON
|
---|
241 | else if (info.dwMajorVersion == 6) /* Vista and up */
|
---|
242 | {
|
---|
243 | niData.dwInfoFlags = NIIF_USER | NIIF_LARGE_ICON; /* Use an own icon instead of the default one */
|
---|
244 | niData.hBalloonIcon = LoadIcon(hInst, MAKEINTRESOURCE(IDI_VIRTUALBOX));
|
---|
245 | }
|
---|
246 | #endif
|
---|
247 | }
|
---|
248 |
|
---|
249 | strcpy(niData.szInfo, pszMsg ? pszMsg : "");
|
---|
250 | strcpy(niData.szInfoTitle, pszTitle ? pszTitle : "");
|
---|
251 |
|
---|
252 | if (!Shell_NotifyIcon(NIM_MODIFY, &niData))
|
---|
253 | return GetLastError();
|
---|
254 | return 0;
|
---|
255 | }
|
---|