-
Notifications
You must be signed in to change notification settings - Fork 2
/
SIDES.C
221 lines (195 loc) · 5.05 KB
/
SIDES.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
#include "stdafx.h"
#include "SIDES.H"
#include <windows.h>
TCHAR * g_side_names[]={
_T("NO SIDE"),
_T("LEFT"),
_T("RIGHT"),
_T("TOP"),
_T("BOTTOM")};
const enum SIDE OPPOSITE_SIDES[]={
SIDE_NONE,
SIDE_RIGHT, SIDE_LEFT,
SIDE_BOTTOM,SIDE_TOP
};
const enum SIDE ADJACENT_SIDES[]={
SIDE_NONE,
SIDE_TOP,
SIDE_BOTTOM,
SIDE_LEFT,
SIDE_RIGHT
};
enum SIDE OppositeSide(enum SIDE what_side){
chASSERT(what_side != SIDE_NONE);
return OPPOSITE_SIDES[what_side];
}
enum SIDE AdjacentSide(enum SIDE what_side){
chASSERT(what_side != SIDE_NONE);
return ADJACENT_SIDES[what_side];
}
// get the top,bottom,left or right value of a rect, deppending on
// what SIDE we ask for
int GetSideOfRect(enum SIDE what_side, const LPCRECT pRect)
{
switch(what_side)
{
case SIDE_LEFT:
return pRect->left;
case SIDE_RIGHT:
return pRect->right;
case SIDE_TOP:
return pRect->top;
case SIDE_BOTTOM:
return pRect->bottom;
default:
chFAIL(_T("not a valid side"));
return 0;
}
}
TCHAR * SideToString(enum SIDE which_side){
chASSERT(chINRANGE(SIDE_NONE,which_side,SIDE_BOTTOM));
return g_side_names[(int)which_side];
}
//Set the given SIDE of a rect structure
void SetSideOfRect(enum SIDE what_side, int new_val, LPRECT pRect)
{
switch(what_side)
{
case SIDE_LEFT:
pRect->left = new_val;
break;
case SIDE_RIGHT:
pRect->right = new_val;
break;
case SIDE_TOP:
pRect->top = new_val;
break;
case SIDE_BOTTOM:
pRect->bottom = new_val;
break;
default:
chFAIL(_T("not a valid side"));
}
}
//GetSideSign
//return the direction moving out from side
//would move the screen coords
int GetSideSign(enum SIDE side)
{
switch(side){
case SIDE_LEFT: //fallthrough
case SIDE_TOP:
return -1;
case SIDE_RIGHT: //fallthrough
case SIDE_BOTTOM:
return 1;
default:
chFAIL(_T("not a valid side"));
return 0; //never gets here?
}
}
void ExtendOut(RECT * p_rect,enum SIDE side,int new_width){
int new_val = GetSideOfRect(OppositeSide(side),p_rect) +
(GetSideSign(side) * new_width);
SetSideOfRect(side,new_val,p_rect);
}
void AlignToSide
(
LPRECT my_rect,
enum SIDE align_side,
int position
)
{ // side_val
// ^
// ---------------- |
// - my_rect - |
// ---------------- |
// ^ ^
// opp_side_Val position
// ^
// |
// ---------------+
// --> - my_rect +
// ---------------+
int side_val = GetSideOfRect(align_side,my_rect);
int opp_side_val = GetSideOfRect(OppositeSide(align_side),my_rect);
int size = opp_side_val - side_val; // may be negative
int aligned_side_val = position;
int aligned_opp_side_val = aligned_side_val + size; //may be negative
SetSideOfRect(align_side, aligned_side_val ,my_rect);
SetSideOfRect(OppositeSide(align_side), aligned_opp_side_val,my_rect);
}
//Align a SIDE of the Alignee rect with a SIDE of the Aligner rect
//
void AlignToRect
(
LPCRECT Aligner,
LPRECT Alignee,
enum SIDE AlignerSide,
enum SIDE AligneeSide
)
{ // aligneeSideVal
// ^
// ++++++++++++++++ ---------------
// + Alignee + | Aligner |
// ++++++++++++++++ ---------------
// ^ ^
// aligneeOpSideVal alignedSideVal
// ^
// |
// ++++++++++++++++--------------
// --> + Alignee + Aligner |
// ++++++++++++++++--------------
//Can't align vertical SIDE with a horizontal SIDE or vice versa
int aligneeSideVal = GetSideOfRect(AligneeSide,Alignee);
int aligneeOpSideVal = GetSideOfRect(OppositeSide(AligneeSide),Alignee);
int size = aligneeOpSideVal - aligneeSideVal; // may be negative
int alignedSideVal = GetSideOfRect(AlignerSide,Aligner);
int alignedOpSideVal = alignedSideVal + size; //may be negative
chASSERT((AlignerSide == OppositeSide(AligneeSide)) || (AlignerSide == AligneeSide) );
SetSideOfRect(AligneeSide,alignedSideVal,Alignee);
SetSideOfRect(OppositeSide(AligneeSide),alignedOpSideVal,Alignee);
}
//Take WPARAM from a WM_SIZING message (which is what edge is being sized)
//and split it into two SIDE variables
//
// ??turn into table deal like thing??
//
void split_edge(WPARAM which_edge, enum SIDE * vert_side, enum SIDE * horz_side){
switch(which_edge){
case WMSZ_BOTTOM:
*vert_side = SIDE_BOTTOM;
*horz_side = SIDE_NONE;
return;
case WMSZ_BOTTOMLEFT:
*vert_side = SIDE_BOTTOM;
*horz_side = SIDE_LEFT;
return;
case WMSZ_BOTTOMRIGHT:
*vert_side = SIDE_BOTTOM;
*horz_side = SIDE_RIGHT;
return;
case WMSZ_LEFT:
*vert_side = SIDE_NONE;
*horz_side = SIDE_LEFT;
return;
case WMSZ_RIGHT:
*vert_side = SIDE_NONE;
*horz_side = SIDE_RIGHT;
return;
case WMSZ_TOP:
*vert_side = SIDE_TOP;
*horz_side = SIDE_NONE;
return;
case WMSZ_TOPLEFT:
*vert_side = SIDE_TOP;
*horz_side = SIDE_LEFT;
return;
case WMSZ_TOPRIGHT:
*vert_side = SIDE_TOP;
*horz_side = SIDE_RIGHT;
return;
default:
chFAIL(_T("WM_SIZING contains bad value"));
}
}