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