1
0

xuigrid.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. #include <precomp.h>
  2. #include "xuigrid.h"
  3. #include <tataki/canvas/canvas.h>
  4. // -----------------------------------------------------------------------
  5. const wchar_t GridXuiObjectStr[] = L"Grid"; // xml tag
  6. char GridXuiSvcName[] = "Grid xui object";
  7. XMLParamPair Grid::params[] = {
  8. { GRID_SETTOPLEFT, L"TOPLEFT"},
  9. { GRID_SETTOP, L"TOP"},
  10. { GRID_SETTOPRIGHT, L"TOPRIGHT"},
  11. { GRID_SETLEFT, L"LEFT"},
  12. { GRID_SETMIDDLE, L"MIDDLE"},
  13. { GRID_SETRIGHT, L"RIGHT"},
  14. { GRID_SETBOTTOMLEFT, L"BOTTOMLEFT"},
  15. { GRID_SETBOTTOM, L"BOTTOM"},
  16. { GRID_SETBOTTOMRIGHT, L"BOTTOMRIGHT"},
  17. };
  18. // -----------------------------------------------------------------------
  19. Grid::Grid() {
  20. setRectRgn(1);
  21. myxuihandle = newXuiHandle();
  22. CreateXMLParameters(myxuihandle);
  23. }
  24. void Grid::CreateXMLParameters(int master_handle)
  25. {
  26. //GRID_PARENT::CreateXMLParameters(master_handle);
  27. int numParams = sizeof(params) / sizeof(params[0]);
  28. hintNumberOfParams(myxuihandle, numParams);
  29. for (int i = 0;i < numParams;i++)
  30. addParam(myxuihandle, params[i], XUI_ATTRIBUTE_IMPLIED);
  31. }
  32. // -----------------------------------------------------------------------
  33. Grid::~Grid() {
  34. }
  35. // -----------------------------------------------------------------------
  36. int Grid::onInit() {
  37. GRID_PARENT::onInit();
  38. doPaint(NULL, 1); // computes the region
  39. invalidateWindowRegion();
  40. return 1;
  41. }
  42. // -----------------------------------------------------------------------
  43. int Grid::onResize() {
  44. GRID_PARENT::onResize();
  45. doPaint(NULL, 1);
  46. invalidateWindowRegion();
  47. return 1;
  48. }
  49. // -----------------------------------------------------------------------
  50. int Grid::setXuiParam(int xuihandle, int xmlattributeid, const wchar_t *xmlattributename, const wchar_t *value) {
  51. if (xuihandle != myxuihandle)
  52. return GRID_PARENT::setXuiParam(xuihandle, xmlattributeid, xmlattributename, value);
  53. switch (xmlattributeid) {
  54. case GRID_SETTOPLEFT:
  55. case GRID_SETTOP:
  56. case GRID_SETTOPRIGHT:
  57. case GRID_SETLEFT:
  58. case GRID_SETMIDDLE:
  59. case GRID_SETRIGHT:
  60. case GRID_SETBOTTOMLEFT:
  61. case GRID_SETBOTTOM:
  62. case GRID_SETBOTTOMRIGHT:
  63. setGridImage(value, xmlattributeid);
  64. break;
  65. default:
  66. return 0;
  67. }
  68. return 1;
  69. }
  70. // -----------------------------------------------------------------------
  71. void Grid::setGridImage(const wchar_t *elementname, int what) {
  72. switch (what) {
  73. case GRID_SETTOPLEFT: topleft = elementname; break;
  74. case GRID_SETTOP: top = elementname; break;
  75. case GRID_SETTOPRIGHT: topright = elementname; break;
  76. case GRID_SETLEFT: left = elementname; break;
  77. case GRID_SETMIDDLE: middle = elementname; break;
  78. case GRID_SETRIGHT: right = elementname; break;
  79. case GRID_SETBOTTOMLEFT: bottomleft = elementname; break;
  80. case GRID_SETBOTTOM: bottom = elementname; break;
  81. case GRID_SETBOTTOMRIGHT: bottomright = elementname; break;
  82. default: return;
  83. }
  84. if (isInited()) invalidate();
  85. }
  86. // -----------------------------------------------------------------------
  87. int Grid::onPaint(Canvas *canvas) {
  88. GRID_PARENT::onPaint(canvas);
  89. doPaint(canvas, 0);
  90. return 1;
  91. }
  92. void Grid::doPaint(Canvas *canvas, int dorgn) {
  93. RECT r;
  94. getGridRect(&r);
  95. SkinBitmap *left_bm = left.getBitmap();
  96. SkinBitmap *middle_bm = middle.getBitmap();
  97. SkinBitmap *right_bm = right.getBitmap();
  98. SkinBitmap *topleft_bm = topleft.getBitmap();
  99. SkinBitmap *top_bm = top.getBitmap();
  100. SkinBitmap *topright_bm = topright.getBitmap();
  101. SkinBitmap *bottomleft_bm = bottomleft.getBitmap();
  102. SkinBitmap *bottom_bm = bottom.getBitmap();
  103. SkinBitmap *bottomright_bm = bottomright.getBitmap();
  104. int left_w = left_bm ? left_bm->getWidth() : 0;
  105. int left_h = left_bm ? left_bm->getHeight() : 0;
  106. int top_h = top_bm ? top_bm->getHeight() : 0;
  107. int top_w = top_bm ? top_bm->getWidth() : 0;
  108. int topleft_h = topleft_bm ? topleft_bm->getHeight() : 0;
  109. int topright_h = topright_bm ? topright_bm->getHeight() : 0;
  110. int topleft_w = topleft_bm ? topleft_bm->getWidth() : 0;
  111. int topright_w = topright_bm ? topright_bm->getWidth() : 0;
  112. int right_w = right_bm ? right_bm->getWidth() : 0;
  113. int right_h = right_bm ? right_bm->getHeight() : 0;
  114. int bottom_h = bottom_bm ? bottom_bm->getHeight() : 0;
  115. int bottom_w = bottom_bm ? bottom_bm->getWidth() : 0;
  116. int bottomleft_h = bottomleft_bm ? bottom_bm->getHeight() : 0;
  117. int bottomright_h = bottomright_bm ? bottom_bm->getHeight() : 0;
  118. int bottomleft_w = bottomleft_bm ? bottomleft_bm->getWidth() : 0;
  119. int bottomright_w = bottomright_bm ? bottomright_bm->getWidth() : 0;
  120. int middle_w = middle_bm ? middle_bm->getWidth() : 0;
  121. int middle_h = middle_bm ? middle_bm->getHeight() : 0;
  122. RECT cell;
  123. if (dorgn) reg.empty();
  124. int paintingAlpha = 0;
  125. if (canvas)
  126. paintingAlpha = getPaintingAlpha();
  127. // topleft
  128. if (topleft_bm) {
  129. cell.left = r.left;
  130. cell.top = r.top;
  131. cell.right = cell.left + topleft_w;
  132. cell.bottom = r.top + topleft_h;
  133. if (canvas) topleft_bm->stretchToRectAlpha(canvas, &cell, paintingAlpha);
  134. if (dorgn) {
  135. RegionI _r(topleft);
  136. _r.offset(cell.left-r.left, cell.top-r.top);
  137. reg.addRegion(&_r);
  138. }
  139. }
  140. // top
  141. if (top_bm) {
  142. cell.left = r.left + topleft_w;
  143. cell.top = r.top;
  144. cell.right = r.right - topright_w;
  145. cell.bottom = r.top + top_h;
  146. if (canvas) top_bm->stretchToRectAlpha(canvas, &cell, paintingAlpha);
  147. if (dorgn && cell.left != cell.right) {
  148. RegionI _r(top);
  149. _r.scale((double)(cell.right-cell.left) / top_w, 1);
  150. _r.offset(cell.left-r.left, cell.top-r.top);
  151. reg.addRegion(&_r);
  152. }
  153. }
  154. // topright
  155. if (topright_bm) {
  156. cell.left = r.right - topright_w;
  157. cell.top = r.top;
  158. cell.right = r.right;
  159. cell.bottom = r.top + topright_h;
  160. if (canvas) topright_bm->stretchToRectAlpha(canvas, &cell, paintingAlpha);
  161. if (dorgn) {
  162. RegionI _r(topright);
  163. _r.offset(cell.left-r.left, cell.top-r.top);
  164. reg.addRegion(&_r);
  165. }
  166. }
  167. // left
  168. if (left_bm) {
  169. cell.left = r.left;
  170. cell.top = r.top + topleft_h;
  171. cell.right = r.left + left_w;
  172. cell.bottom = r.bottom - bottomleft_h;
  173. if (canvas) left_bm->stretchToRectAlpha(canvas, &cell, paintingAlpha);
  174. if (dorgn && cell.bottom != cell.top) {
  175. RegionI _r(left);
  176. _r.scale(1, (double)(cell.bottom-cell.top) / left_h);
  177. _r.offset(cell.left-r.left, cell.top-r.top);
  178. reg.addRegion(&_r);
  179. }
  180. }
  181. // middle
  182. if (middle_bm) {
  183. cell.left = r.left + left_w;
  184. cell.top = r.top + top_h;
  185. cell.right = r.right - right_w;
  186. cell.bottom = r.bottom - bottom_h;
  187. if (canvas) middle_bm->stretchToRectAlpha(canvas, &cell, paintingAlpha);
  188. if (dorgn && cell.left != cell.right && cell.bottom != cell.top) {
  189. RegionI _r(middle);
  190. _r.scale((double)(cell.right-cell.left) / middle_w, (double)(cell.bottom-cell.top) / middle_h);
  191. _r.offset(cell.left-r.left, cell.top-r.top);
  192. reg.addRegion(&_r);
  193. }
  194. }
  195. // right
  196. if (right_bm) {
  197. cell.left = r.right - right_w;
  198. cell.top = r.top + top_h;
  199. cell.right = r.right;
  200. cell.bottom = r.bottom - bottomright_h;
  201. if (canvas) right_bm->stretchToRectAlpha(canvas, &cell, paintingAlpha);
  202. if (dorgn && cell.bottom != cell.top) {
  203. RegionI _r(right);
  204. _r.scale(1, (double)(cell.bottom-cell.top) / right_h);
  205. _r.offset(cell.left-r.left, cell.top-r.top);
  206. reg.addRegion(&_r);
  207. }
  208. }
  209. // bottomleft
  210. if (bottomleft_bm) {
  211. cell.left = r.left;
  212. cell.top = r.bottom - bottomleft_h;
  213. cell.right = r.left + bottomleft_w;
  214. cell.bottom = r.bottom;
  215. if (canvas) bottomleft_bm->stretchToRectAlpha(canvas, &cell, paintingAlpha);
  216. if (dorgn) {
  217. RegionI _r(bottomleft);
  218. _r.offset(cell.left-r.left, cell.top-r.top);
  219. reg.addRegion(&_r);
  220. }
  221. }
  222. // bottom
  223. if (bottom_bm) {
  224. cell.left = r.left + bottomleft_w;
  225. cell.top = r.bottom - bottom_h;
  226. cell.right = r.right - bottomright_w;
  227. cell.bottom = r.bottom;
  228. if (canvas) bottom_bm->stretchToRectAlpha(canvas, &cell, paintingAlpha);
  229. if (dorgn && cell.right != cell.left) {
  230. RegionI _r(bottom);
  231. _r.scale((double)(cell.right-cell.left) / bottom_w, 1);
  232. _r.offset(cell.left-r.left, cell.top-r.top);
  233. reg.addRegion(&_r);
  234. }
  235. }
  236. // bottomright
  237. if (bottomright_bm) {
  238. cell.left = r.right - bottomright_w;
  239. cell.top = r.bottom - bottomright_h;
  240. cell.right = r.right;
  241. cell.bottom = r.bottom;
  242. if (canvas) bottomright_bm->stretchToRectAlpha(canvas, &cell, paintingAlpha);
  243. if (dorgn) {
  244. RegionI _r(bottomright);
  245. _r.offset(cell.left-r.left, cell.top-r.top);
  246. reg.addRegion(&_r);
  247. }
  248. }
  249. }