rect.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /*
  2. * Rectangle-related functions
  3. *
  4. * Copyright 1993, 1996 Alexandre Julliard
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #include <stdarg.h>
  21. #include "windef.h"
  22. #include "winbase.h"
  23. #include "wingdi.h"
  24. #include "wine/winuser16.h"
  25. #include "winuser.h"
  26. /***********************************************************************
  27. * SetRect (USER.72)
  28. */
  29. void WINAPI SetRect16( LPRECT16 rect, INT16 left, INT16 top,
  30. INT16 right, INT16 bottom )
  31. {
  32. rect->left = left;
  33. rect->right = right;
  34. rect->top = top;
  35. rect->bottom = bottom;
  36. }
  37. /***********************************************************************
  38. * SetRect (USER32.@)
  39. */
  40. BOOL WINAPI SetRect( LPRECT rect, INT left, INT top,
  41. INT right, INT bottom )
  42. {
  43. if (!rect) return FALSE;
  44. rect->left = left;
  45. rect->right = right;
  46. rect->top = top;
  47. rect->bottom = bottom;
  48. return TRUE;
  49. }
  50. /***********************************************************************
  51. * SetRectEmpty (USER.73)
  52. */
  53. void WINAPI SetRectEmpty16( LPRECT16 rect )
  54. {
  55. rect->left = rect->right = rect->top = rect->bottom = 0;
  56. }
  57. /***********************************************************************
  58. * SetRectEmpty (USER32.@)
  59. */
  60. BOOL WINAPI SetRectEmpty( LPRECT rect )
  61. {
  62. if (!rect) return FALSE;
  63. rect->left = rect->right = rect->top = rect->bottom = 0;
  64. return TRUE;
  65. }
  66. /***********************************************************************
  67. * CopyRect (USER.74)
  68. */
  69. BOOL16 WINAPI CopyRect16( RECT16 *dest, const RECT16 *src )
  70. {
  71. *dest = *src;
  72. return TRUE;
  73. }
  74. /***********************************************************************
  75. * CopyRect (USER32.@)
  76. */
  77. BOOL WINAPI CopyRect( RECT *dest, const RECT *src )
  78. {
  79. if (!dest || !src)
  80. return FALSE;
  81. *dest = *src;
  82. return TRUE;
  83. }
  84. /***********************************************************************
  85. * IsRectEmpty (USER.75)
  86. *
  87. * Bug compat: Windows checks for 0 or negative width/height.
  88. */
  89. BOOL16 WINAPI IsRectEmpty16( const RECT16 *rect )
  90. {
  91. return ((rect->left >= rect->right) || (rect->top >= rect->bottom));
  92. }
  93. /***********************************************************************
  94. * IsRectEmpty (USER32.@)
  95. *
  96. * Bug compat: Windows checks for 0 or negative width/height.
  97. */
  98. BOOL WINAPI IsRectEmpty( const RECT *rect )
  99. {
  100. if (!rect) return TRUE;
  101. return ((rect->left >= rect->right) || (rect->top >= rect->bottom));
  102. }
  103. /***********************************************************************
  104. * PtInRect (USER.76)
  105. */
  106. BOOL16 WINAPI PtInRect16( const RECT16 *rect, POINT16 pt )
  107. {
  108. return ((pt.x >= rect->left) && (pt.x < rect->right) &&
  109. (pt.y >= rect->top) && (pt.y < rect->bottom));
  110. }
  111. /***********************************************************************
  112. * PtInRect (USER32.@)
  113. */
  114. BOOL WINAPI PtInRect( const RECT *rect, POINT pt )
  115. {
  116. if (!rect) return FALSE;
  117. return ((pt.x >= rect->left) && (pt.x < rect->right) &&
  118. (pt.y >= rect->top) && (pt.y < rect->bottom));
  119. }
  120. /***********************************************************************
  121. * OffsetRect (USER.77)
  122. */
  123. void WINAPI OffsetRect16( LPRECT16 rect, INT16 x, INT16 y )
  124. {
  125. rect->left += x;
  126. rect->right += x;
  127. rect->top += y;
  128. rect->bottom += y;
  129. }
  130. /***********************************************************************
  131. * OffsetRect (USER32.@)
  132. */
  133. BOOL WINAPI OffsetRect( LPRECT rect, INT x, INT y )
  134. {
  135. if (!rect) return FALSE;
  136. rect->left += x;
  137. rect->right += x;
  138. rect->top += y;
  139. rect->bottom += y;
  140. return TRUE;
  141. }
  142. /***********************************************************************
  143. * InflateRect (USER.78)
  144. */
  145. void WINAPI InflateRect16( LPRECT16 rect, INT16 x, INT16 y )
  146. {
  147. rect->left -= x;
  148. rect->top -= y;
  149. rect->right += x;
  150. rect->bottom += y;
  151. }
  152. /***********************************************************************
  153. * InflateRect (USER32.@)
  154. */
  155. BOOL WINAPI InflateRect( LPRECT rect, INT x, INT y )
  156. {
  157. if (!rect) return FALSE;
  158. rect->left -= x;
  159. rect->top -= y;
  160. rect->right += x;
  161. rect->bottom += y;
  162. return TRUE;
  163. }
  164. /***********************************************************************
  165. * IntersectRect (USER.79)
  166. */
  167. BOOL16 WINAPI IntersectRect16( LPRECT16 dest, const RECT16 *src1,
  168. const RECT16 *src2 )
  169. {
  170. if (IsRectEmpty16(src1) || IsRectEmpty16(src2) ||
  171. (src1->left >= src2->right) || (src2->left >= src1->right) ||
  172. (src1->top >= src2->bottom) || (src2->top >= src1->bottom))
  173. {
  174. SetRectEmpty16( dest );
  175. return FALSE;
  176. }
  177. dest->left = max( src1->left, src2->left );
  178. dest->right = min( src1->right, src2->right );
  179. dest->top = max( src1->top, src2->top );
  180. dest->bottom = min( src1->bottom, src2->bottom );
  181. return TRUE;
  182. }
  183. /***********************************************************************
  184. * IntersectRect (USER32.@)
  185. */
  186. BOOL WINAPI IntersectRect( LPRECT dest, const RECT *src1,
  187. const RECT *src2 )
  188. {
  189. if (!dest || !src1 || !src2) return FALSE;
  190. if (IsRectEmpty(src1) || IsRectEmpty(src2) ||
  191. (src1->left >= src2->right) || (src2->left >= src1->right) ||
  192. (src1->top >= src2->bottom) || (src2->top >= src1->bottom))
  193. {
  194. SetRectEmpty( dest );
  195. return FALSE;
  196. }
  197. dest->left = max( src1->left, src2->left );
  198. dest->right = min( src1->right, src2->right );
  199. dest->top = max( src1->top, src2->top );
  200. dest->bottom = min( src1->bottom, src2->bottom );
  201. return TRUE;
  202. }
  203. /***********************************************************************
  204. * UnionRect (USER.80)
  205. */
  206. BOOL16 WINAPI UnionRect16( LPRECT16 dest, const RECT16 *src1,
  207. const RECT16 *src2 )
  208. {
  209. if (IsRectEmpty16(src1))
  210. {
  211. if (IsRectEmpty16(src2))
  212. {
  213. SetRectEmpty16( dest );
  214. return FALSE;
  215. }
  216. else *dest = *src2;
  217. }
  218. else
  219. {
  220. if (IsRectEmpty16(src2)) *dest = *src1;
  221. else
  222. {
  223. dest->left = min( src1->left, src2->left );
  224. dest->right = max( src1->right, src2->right );
  225. dest->top = min( src1->top, src2->top );
  226. dest->bottom = max( src1->bottom, src2->bottom );
  227. }
  228. }
  229. return TRUE;
  230. }
  231. /***********************************************************************
  232. * UnionRect (USER32.@)
  233. */
  234. BOOL WINAPI UnionRect( LPRECT dest, const RECT *src1,
  235. const RECT *src2 )
  236. {
  237. if (!dest) return FALSE;
  238. if (IsRectEmpty(src1))
  239. {
  240. if (IsRectEmpty(src2))
  241. {
  242. SetRectEmpty( dest );
  243. return FALSE;
  244. }
  245. else *dest = *src2;
  246. }
  247. else
  248. {
  249. if (IsRectEmpty(src2)) *dest = *src1;
  250. else
  251. {
  252. dest->left = min( src1->left, src2->left );
  253. dest->right = max( src1->right, src2->right );
  254. dest->top = min( src1->top, src2->top );
  255. dest->bottom = max( src1->bottom, src2->bottom );
  256. }
  257. }
  258. return TRUE;
  259. }
  260. /***********************************************************************
  261. * EqualRect (USER.244)
  262. */
  263. BOOL16 WINAPI EqualRect16( const RECT16* rect1, const RECT16* rect2 )
  264. {
  265. return ((rect1->left == rect2->left) && (rect1->right == rect2->right) &&
  266. (rect1->top == rect2->top) && (rect1->bottom == rect2->bottom));
  267. }
  268. /***********************************************************************
  269. * EqualRect (USER32.@)
  270. */
  271. BOOL WINAPI EqualRect( const RECT* rect1, const RECT* rect2 )
  272. {
  273. if (!rect1 || !rect2) return FALSE;
  274. return ((rect1->left == rect2->left) && (rect1->right == rect2->right) &&
  275. (rect1->top == rect2->top) && (rect1->bottom == rect2->bottom));
  276. }
  277. /***********************************************************************
  278. * SubtractRect (USER.373)
  279. */
  280. BOOL16 WINAPI SubtractRect16( LPRECT16 dest, const RECT16 *src1,
  281. const RECT16 *src2 )
  282. {
  283. RECT16 tmp;
  284. if (IsRectEmpty16( src1 ))
  285. {
  286. SetRectEmpty16( dest );
  287. return FALSE;
  288. }
  289. *dest = *src1;
  290. if (IntersectRect16( &tmp, src1, src2 ))
  291. {
  292. if (EqualRect16( &tmp, dest ))
  293. {
  294. SetRectEmpty16( dest );
  295. return FALSE;
  296. }
  297. if ((tmp.top == dest->top) && (tmp.bottom == dest->bottom))
  298. {
  299. if (tmp.left == dest->left) dest->left = tmp.right;
  300. else if (tmp.right == dest->right) dest->right = tmp.left;
  301. }
  302. else if ((tmp.left == dest->left) && (tmp.right == dest->right))
  303. {
  304. if (tmp.top == dest->top) dest->top = tmp.bottom;
  305. else if (tmp.bottom == dest->bottom) dest->bottom = tmp.top;
  306. }
  307. }
  308. return TRUE;
  309. }
  310. /***********************************************************************
  311. * SubtractRect (USER32.@)
  312. */
  313. BOOL WINAPI SubtractRect( LPRECT dest, const RECT *src1,
  314. const RECT *src2 )
  315. {
  316. RECT tmp;
  317. if (!dest) return FALSE;
  318. if (IsRectEmpty( src1 ))
  319. {
  320. SetRectEmpty( dest );
  321. return FALSE;
  322. }
  323. *dest = *src1;
  324. if (IntersectRect( &tmp, src1, src2 ))
  325. {
  326. if (EqualRect( &tmp, dest ))
  327. {
  328. SetRectEmpty( dest );
  329. return FALSE;
  330. }
  331. if ((tmp.top == dest->top) && (tmp.bottom == dest->bottom))
  332. {
  333. if (tmp.left == dest->left) dest->left = tmp.right;
  334. else if (tmp.right == dest->right) dest->right = tmp.left;
  335. }
  336. else if ((tmp.left == dest->left) && (tmp.right == dest->right))
  337. {
  338. if (tmp.top == dest->top) dest->top = tmp.bottom;
  339. else if (tmp.bottom == dest->bottom) dest->bottom = tmp.top;
  340. }
  341. }
  342. return TRUE;
  343. }