forked from RamonUnch/AltSnap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
snap.c
288 lines (274 loc) · 9.22 KB
/
snap.c
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* (C) Raymond GILLIBERT *
* Functions to handle Snap/restore informations AltSnap *
* Snapping informations set with Set/GetProp. *
* Window database is used as fallback. *
* General Public License Version 3 or later (Free Software Foundation) *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#define SNAPPED 1
#define ROLLED 2
#define SNLEFT (1<<2)
#define SNRIGHT (1<<3)
#define SNTOP (1<<4)
#define SNBOTTOM (1<<5)
#define SNMAXH (1<<6)
#define SNMAXW (1<<7)
#define SNCLEAR (1<<8) // to clear the flag at init movement.
#define TORESIZE (1<<9)
#define SNTHENROLLED (1<<10)
#define SNZONE (1<<11)
#define SNMIN (1<<12)
#define SNTOPLEFT (SNTOP|SNLEFT)
#define SNTOPRIGHT (SNTOP|SNRIGHT)
#define SNBOTTOMLEFT (SNBOTTOM|SNLEFT)
#define SNBOTTOMRIGHT (SNBOTTOM|SNRIGHT)
#define SNAPPEDSIDE (SNTOPLEFT|SNBOTTOMRIGHT)
#define PureLeft(flag) ( (flag&SNLEFT) && !(flag&(SNTOP|SNBOTTOM)) )
#define PureRight(flag) ( (flag&SNRIGHT) && !(flag&(SNTOP|SNBOTTOM)) )
#define PureTop(flag) ( (flag&SNTOP) && !(flag&(SNLEFT|SNRIGHT)) )
#define PureBottom(flag) ( (flag&SNBOTTOM) && !(flag&(SNLEFT|SNRIGHT)) )
/////////////////////////////////////////////////////////////////////////////
#define NUMWNDDB 16
struct wnddata {
unsigned restore;
HWND hwnd;
int width;
int height;
// int rolledh;
// UINT odpi
};
static struct wnddata wnddb[NUMWNDDB];
/////////////////////////////////////////////////////////////////////////////
// Database functions: used as fallback if SetProp fails
// Zero-out the database to be called in Load()
static void ResetDB()
{
int i;
for (i=0; i < NUMWNDDB; i++) {
wnddb[i].hwnd = NULL;
wnddb[i].restore = 0;
}
}
// Return the entry if it was already in db. Or NULL otherwise
static pure int GetWindowInDB(HWND hwndd)
{
// Check if window is already in the wnddb database
// And set it in the current state
unsigned i;
for (i=0; i < NUMWNDDB; i++) {
if (wnddb[i].hwnd == hwndd) {
return i;
}
}
return -1;
}
static void AddWindowToDB(HWND hwndd, int width, int height, unsigned flag)
{
int idx = GetWindowInDB(hwndd);
// Find a nice place in wnddb if not already present
if (idx < 0) {
int i;
for (i=0; i < NUMWNDDB && wnddb[i].hwnd; i++);
if (i >= NUMWNDDB) return;
idx = i;
}
wnddb[idx].hwnd = hwndd;
wnddb[idx].width = width;
wnddb[idx].height = height;
wnddb[idx].restore = flag;
}
static void DelWindowFromDB(HWND hwnd)
{
unsigned i;
for (i=0; i < NUMWNDDB; i++) {
if (wnddb[i].hwnd == hwnd) {
wnddb[i].hwnd = NULL;
wnddb[i].restore = 0;
}
}
}
/////////////////////////////////////////////////////////////////////////////
// Windows restore data are saved in properties
// In 32b mode width and height are shrunk to 16b WORDS
// In 64b mode width and height are stored on 32b DWORDS
// I 64b mode we also check for FancyZone info that are stored the same way!
// There is a lot of cast because of annoying warnings...
// There is also a fallback to a database for some spetial windows.
static unsigned GetRestoreData(HWND hwnd, int *width, int *height)
{
DorQWORD WH = (DorQWORD)GetProp(hwnd, APP_PROPPT);
if (WH) {
*width = (int)LOWORDPTR(WH);
*height = (int)HIWORDPTR(WH);
UINT dpi = GetDpiForWindow(hwnd);
UINT odpi;
if (dpi && (odpi = (UINT)(DorQWORD)GetProp(hwnd, APP_PRODPI)) && odpi != dpi) {
// The current dpi is different for the window, We must scale the content.
*width = MulDiv(*width, dpi, odpi);
*height = MulDiv(*height, dpi, odpi);
}
return (unsigned)(DorQWORD)GetProp(hwnd, APP_PROPFL);;
# ifdef WIN64 // Try fancy zone flag only in 64bit mode!
} else if (conf.FancyZone && (WH = (DorQWORD)GetProp(hwnd, FZ_PROPPT))) {
// It seems FancyZones stores size info in points, not pixels.
*width = (int)LOWORDPTR(WH);
*height = (int)HIWORDPTR(WH);
if (conf.FancyZone != 2) {
int dpi = GetDpiForWindow(hwnd);
if (dpi) {
// Scale bcak to current dpi from 96
*width = MulDiv(*width, dpi, 96);
*height = MulDiv(*height, dpi, 96);
}
}
return SNAPPED|SNZONE;
# endif
} else { // fallback to database
int idx = GetWindowInDB(hwnd);
if (idx >= 0) {
*width = wnddb[idx].width;
*height = wnddb[idx].height;
return wnddb[idx].restore;
}
}
// Unable to find the window in the db
*width = *height = 0;
return 0;
}
static void ClearRestoreData(HWND hwnd)
{
RemoveProp(hwnd, APP_PROPPT);
RemoveProp(hwnd, APP_PROPFL);
// RemoveProp(hwnd, APP_PROPOFFSET);
# ifdef WIN64
if(conf.FancyZone) {
RemoveProp(hwnd, FZ_PROPPT);
RemoveProp(hwnd, FZ_PROPZONES);
}
# endif
DelWindowFromDB(hwnd);
}
// Sets width, height and restore flag in a hwnd
static void SetRestoreData(HWND hwnd, int width, int height, unsigned restore)
{
BOOL ret;
ret = SetProp(hwnd, APP_PROPFL, (HANDLE)(DorQWORD)restore);
ret &= SetProp(hwnd, APP_PROPPT, (HANDLE)MAKELONGPTR(width, height));
UINT dpi=0;
if ( (dpi = GetDpiForWindow(hwnd)) )
ret &= SetProp(hwnd, APP_PRODPI, (HANDLE)(DorQWORD)dpi);
if (!ret) AddWindowToDB(hwnd, width, height, restore);
}
static unsigned GetRestoreFlag(HWND hwnd)
{
unsigned flag = (DorQWORD)GetProp(hwnd, APP_PROPFL);
# ifdef WIN64
if(conf.FancyZone && GetProp(hwnd, FZ_PROPPT))
flag |= SNAPPED|SNZONE;
# endif
int idx; // fallback to db.
if (!flag && ((idx = GetWindowInDB(hwnd)) >=0)) {
return wnddb[idx].restore;
}
return flag;
}
static void SetRestoreFlag(HWND hwnd, unsigned flag)
{
BOOL ret = SetProp(hwnd, APP_PROPFL, (HANDLE)(DorQWORD)flag);
int idx;
if (!ret && ((idx = GetWindowInDB(hwnd)) >=0)) {
wnddb[idx].restore = flag;
}
}
static int IsAnySnapped(HWND hwnd)
{
// Any kind of snapping, including maximization.
// In short whenever the window will be restored.
unsigned flg;
return IsZoomed(hwnd)
||( !(conf.SmartAero&4) && SNAPPED & (flg=GetRestoreFlag(hwnd)) && !(flg&SNCLEAR) )
|| IsWindowSnapped(hwnd);
}
/////////////////////////////////////////////////////////////////////////////
// borderless flag (saving old GWL_STYLE)
static void SetBorderlessFlag(HWND hwnd, LONG_PTR flag)
{
SetProp(hwnd, APP_PRBDLESS,(HANDLE)flag);
}
static LONG_PTR GetBorderlessFlag(HWND hwnd)
{
return (LONG_PTR)GetProp(hwnd, APP_PRBDLESS);
}
static LONG_PTR ClearBorderlessFlag(HWND hwnd)
{
return (LONG_PTR)RemoveProp(hwnd, APP_PRBDLESS);
}
static void SetSquareCorners(HWND hwnd)
{
int cornerPref = DWMWCP_DEFAULT;
HRESULT ok = DwmGetWindowAttributeL(hwnd, DWMWA_WINDOW_CORNER_PREFERENCE, &cornerPref, sizeof(cornerPref));
if (ok) {
SetProp(hwnd, APP_OWDMCP, (HANDLE)(UINT_PTR)(cornerPref+1)); // Do not store Zero.
cornerPref = DWMWCP_DONOTROUND;
DwmSetWindowAttributeL(hwnd, DWMWA_WINDOW_CORNER_PREFERENCE, &cornerPref, sizeof(cornerPref));
}
}
static void RestoreOldCorners(HWND hwnd)
{
int cp = (int)(UINT_PTR)GetProp(hwnd, APP_OWDMCP);
RemoveProp(hwnd, APP_OWDMCP);
if (cp) {
cp--;
DwmSetWindowAttributeL(hwnd, DWMWA_WINDOW_CORNER_PREFERENCE, &cp, sizeof(cp));
}
}
///////////////////////////////////////////////////////////////////////////////
//// Roll unroll stuff
//static int GetRolledHeight(HWND hwnd)
//{
// int ret = (int)GetProp(hwnd, APP_ROLLED);
// int idx;
// if (!ret && ((idx = GetWindowInDB(hwnd)) >=0)) {
// ret = wnddb[idx].rolledh;
// }
// return ret;
//}
//static int ClearRolledHeight(HWND hwnd)
//{
// int ret = (int)RemoveProp(hwnd, APP_ROLLED);
//
// int idx;
// if (!ret && ((idx = GetWindowInDB(hwnd)) >=0)) {
// ret = wnddb[idx].rolledh;
// wnddb[idx].rolledh = 0;
// }
// return ret;
//}
//static void SetRolledHeight(HWND hwnd, int rolledh)
//{
// BOOL ret = SetProp(hwnd, APP_ROLLED, (HANDLE)(DorQWORD)rolledh);
// if (ret) return;
//
// int idx;
// if (((idx = GetWindowInDB(hwnd)) >=0)) {
// wnddb[idx].rolledh = rolledh;
// } else {
// int i;
// for (i=0; i < NUMWNDDB && wnddb[i].hwnd; i++);
// if (i >= NUMWNDDB) return;
// idx = i;
// wnddb[idx].hwnd = hwnd;
// wnddb[idx].rolledh = rolledh;
// wnddb[idx].restore = 0;
// }
//
//}
//static void AddToFlag(HWND hwnd, unsigned flag)
//{
// SetRestoreFlag(hwnd, flag|GetRestoreFlag(hwnd));
//}
//
//static void RemoveToFlag(HWND hwnd, unsigned flag)
//{
// SetRestoreFlag(hwnd, flag & (~GetRestoreFlag(hwnd)));
//}