You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RegionHelper.h 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  2. *
  3. * This is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This software is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this software; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  16. * USA.
  17. */
  18. #ifndef __REGIONHELPER_H__
  19. #define __REGIONHELPER_H__
  20. // RegionHelper is a class which helps in using X server regions by
  21. // automatically freeing them in the destructor. It also fixes a problem with
  22. // REGION_INIT when given an empty rectangle.
  23. // REGION_NULL was introduced in the Xorg tree as the way to initialise an
  24. // empty region. If it's not already defined do it the old way. Note that the
  25. // old way causes a segfault in the new tree...
  26. #ifndef REGION_NULL
  27. #define REGION_NULL(pScreen,pReg) REGION_INIT(pScreen,pReg,NullBox,0)
  28. #endif
  29. class RegionHelper {
  30. public:
  31. // constructor from a single rect
  32. RegionHelper(ScreenPtr pScreen_, BoxPtr rect, int size)
  33. : pScreen(pScreen_), reg(0)
  34. {
  35. init(rect, size);
  36. }
  37. // constructor from an existing X server region
  38. RegionHelper(ScreenPtr pScreen_, RegionPtr pRegion)
  39. : pScreen(pScreen_), reg(&regRec)
  40. {
  41. REGION_NULL(pScreen, reg);
  42. REGION_COPY(pScreen, reg, pRegion);
  43. }
  44. // constructor from an array of rectangles
  45. RegionHelper(ScreenPtr pScreen_, int nrects, xRectanglePtr rects,
  46. int ctype=CT_NONE)
  47. : pScreen(pScreen_)
  48. {
  49. reg = RECTS_TO_REGION(pScreen, nrects, rects, ctype);
  50. }
  51. // constructor for calling init() later
  52. RegionHelper(ScreenPtr pScreen_) : pScreen(pScreen_), reg(0) {
  53. }
  54. void init(BoxPtr rect, int size) {
  55. reg = &regRec;
  56. if (!rect || (rect && (rect->x2 == rect->x1 || rect->y2 == rect->y1))) {
  57. REGION_NULL(pScreen, reg);
  58. } else {
  59. REGION_INIT(pScreen, reg, rect, size);
  60. }
  61. }
  62. // destructor frees as appropriate
  63. ~RegionHelper() {
  64. if (reg == &regRec) {
  65. REGION_UNINIT(pScreen, reg);
  66. } else if (reg) {
  67. REGION_DESTROY(pScreen, reg);
  68. }
  69. }
  70. ScreenPtr pScreen;
  71. RegionRec regRec;
  72. RegionPtr reg;
  73. };
  74. #endif