Advertisement
Guest User

Untitled

a guest
May 29th, 2015
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.63 KB | None | 0 0
  1. /* === shoes_place_decide() ===
  2. place : actual element being calculated - child
  3. c : parent canvas
  4. attr : actual element attributes - child attributes
  5. dw : parent->place.iw, imw (real image width), canvas->width, PATTERN width, defaul control width
  6. dh : same but for height
  7. rel : a flag to sort elements (bakground,border : 4, image : 10 (8+2), slots : 2, widgets : 2
  8. slot with :attach => Shoes::Window : 1)
  9. padded : a boolean, controls are padded (SETUP_CONTROL), images if REL_CANVAS [means all ?] (SETUP)
  10.  
  11.  
  12. - 2 rounds (gathering info + real computation ?) for each element being evaluated
  13. - for slots, images, image blocks, backgrounds, borders, widgets
  14. shapes are using 'shoes_place_exact', textblock 'shoes_textblock_draw' (pango)
  15. - image 4 rounds (when alone ?)
  16. - app alone : nothing to place !
  17.  
  18.  
  19. in app (top level canvas) coordinates :
  20. x, y => coords
  21. ix, iy => margin + left, top, right, bottom
  22. dx, dy => displacement
  23. iw, ih => real dimension
  24. w, h => dimension + margin
  25. ox, oy => original placement
  26. cx, cy => coords of next possible placement
  27.  
  28. endx, endy => coords of next possible placement (for parent canvas)
  29.  
  30. */
  31. void
  32. shoes_place_decide(shoes_place *place, VALUE c, VALUE attr, int dw, int dh,
  33. unsigned char rel, int padded)
  34. {
  35. shoes_canvas *canvas = NULL;
  36. if (!NIL_P(c)) Data_Get_Struct(c, shoes_canvas, canvas);
  37. VALUE ck = rb_obj_class(c);
  38. VALUE stuck = ATTR(attr, attach);
  39.  
  40. // for image : we want to scale the image, given only one attribute :width or :height
  41. // get dw and dh, set width or height
  42. if (REL_FLAGS(rel) & REL_SCALE) { // 8
  43. VALUE rw = ATTR(attr, width), rh = ATTR(attr, height);
  44.  
  45. if (NIL_P(rw) && !NIL_P(rh)) { // we have height
  46. // fetch height in pixels whatever the input (string, float, positive/negative int)
  47. int spx = shoes_px(rh, dh, CPH(canvas), 1);
  48. // compute width with image aspect ratio [(dh == dw) means a square ]
  49. dw = (dh == dw) ? spx : ROUND(((dh * 1.) / dw) * spx);
  50. dh = spx; // now re-init 'dh' for next calculations
  51. ATTRSET(attr, width, INT2NUM(dw)); // set calculated width
  52. }
  53. else if (NIL_P(rh) && !NIL_P(rw)) {
  54. int spx = shoes_px(rw, dw, CPW(canvas), 1);
  55. dh = (dh == dw) ? spx : ROUND(((dh * 1.) / dw) * spx);
  56. dw = spx;
  57. ATTRSET(attr, height, INT2NUM(dh));
  58. }
  59. }
  60.  
  61. // get margins
  62. ATTR_MARGINS(attr, 0, canvas);
  63. if (padded || dh == 0) dh += tmargin + bmargin;
  64. if (padded || dw == 0) dw += lmargin + rmargin;
  65.  
  66. int testw = dw;
  67. if (testw == 0) testw = lmargin + 1 + rmargin;
  68.  
  69. // for displacements
  70. place->dx = place->dy = 0;
  71.  
  72. // Do we have sticky elements ?
  73. if (!NIL_P(stuck)) {
  74. if (stuck == cShoesWindow)
  75. rel = REL_FLAGS(rel) | REL_WINDOW; // 1
  76. else if (stuck == cMouse)
  77. rel = REL_FLAGS(rel) | REL_CURSOR; // 3
  78. else
  79. rel = REL_FLAGS(rel) | REL_STICKY; // 5
  80. }
  81. place->flags = rel;
  82.  
  83.  
  84. // MAIN WORK
  85. if (canvas == NULL) { // no parent ?
  86. place->ix = place->x = 0;
  87. place->iy = place->y = 0;
  88. place->iw = place->w = dw;
  89. place->ih = place->h = dh;
  90. }
  91. else {
  92. // ox, oy original x, y -- cx, cy computed ?
  93. int cx, cy, ox, oy, tw = dw, th = dh;
  94.  
  95. // compute cx, cy, ox, oy depending on kind of element being evaluated
  96. // also testw, dh if REF_TILE (background, border, ?)
  97. switch (REL_COORDS(rel)) { // rel & 0x07
  98. case REL_WINDOW: // 1
  99. cx = 0; cy = 0;
  100. ox = 0; oy = canvas->slot->scrolly;
  101. break;
  102.  
  103. case REL_CANVAS: // 2
  104. cx = canvas->cx - CPX(canvas);
  105. cy = canvas->cy - CPY(canvas);
  106. ox = CPX(canvas);
  107. oy = CPY(canvas);
  108. break;
  109.  
  110. case REL_CURSOR: // 3
  111. cx = 0; cy = 0;
  112. ox = canvas->app->mousex; oy = canvas->app->mousey;
  113. break;
  114.  
  115. case REL_TILE: // 4
  116. cx = 0; cy = 0;
  117. ox = CPX(canvas);
  118. oy = CPY(canvas);
  119. testw = dw = CPW(canvas);
  120. //dh = max(canvas->height, CPH(canvas));
  121. dh = max(canvas->height, (canvas->fully - CPB(canvas)) - CPY(canvas));
  122. break;
  123.  
  124. default:
  125. cx = 0; cy = 0;
  126. ox = canvas->cx; oy = canvas->cy;
  127. if ((REL_COORDS(rel) & REL_STICKY) && shoes_is_element(stuck))
  128. {
  129. shoes_element *element;
  130. Data_Get_Struct(stuck, shoes_element, element);
  131. ox = element->place.x;
  132. oy = element->place.y;
  133. }
  134. break;
  135. }
  136.  
  137.  
  138. /****** compute width and height of child*/
  139. // testw is now a default value for width
  140. place->w = PX(attr, width, testw, CPW(canvas)); // calculate width of child
  141.  
  142. // endx, endy where is next avalaible room in the canvas
  143. // where is next possible placement
  144. if (dw == 0 && place->w + (int)canvas->cx > canvas->place.iw) {
  145. canvas->cx = canvas->endx = CPX(canvas);
  146. canvas->cy = canvas->endy;
  147. place->w = canvas->place.iw;
  148. }
  149.  
  150. place->h = PX(attr, height, dh, CPH(canvas)); // calculate height of child
  151.  
  152. if (REL_COORDS(rel) != REL_TILE) { // not a background or a border
  153. tw = place->w; // tw = dw otherwise
  154. th = place->h; // th = dh otherwise
  155. }
  156.  
  157. /****** compute x and y of child*/
  158. // calculate placement of child in regards of eventual left, right, top, bottom attributes
  159. place->x = PX2(attr, left, right, cx, tw, canvas->place.iw) + ox;
  160. place->y = PX2(attr, top, bottom, cy, th,
  161. ORIGIN(canvas->place) ? canvas->height : canvas->fully) + oy;
  162.  
  163. if (!ORIGIN(canvas->place)) { // ??
  164. place->dx = canvas->place.dx;
  165. place->dy = canvas->place.dy;
  166. }
  167.  
  168. // calculate displacement of child
  169. place->dx += PXN(attr, displace_left, 0, CPW(canvas));
  170. place->dy += PXN(attr, displace_top, 0, CPH(canvas));
  171.  
  172.  
  173. /* set absolute placement flags */
  174. place->flags |= NIL_P(ATTR(attr, left)) && NIL_P(ATTR(attr, right)) ? 0 : FLAG_ABSX;
  175. place->flags |= NIL_P(ATTR(attr, top)) && NIL_P(ATTR(attr, bottom)) ? 0 : FLAG_ABSY;
  176.  
  177. // if we are in a flow with not enough width for next element
  178. // or in a Stack
  179. // not a background or border, not absolute positioned
  180. if (REL_COORDS(rel) != REL_TILE && ABSY(*place) == 0 && (ck == cStack || place->x + place->w > CPX(canvas) + canvas->place.iw))
  181. {
  182. rb_warn("ck: %s\n", RSTRING_PTR(rb_inspect(ck)));
  183. canvas->cx = place->x = CPX(canvas);
  184. canvas->cy = place->y = canvas->endy;
  185. }
  186.  
  187. } // MAIN
  188.  
  189. /* take care of margins */
  190. place->ix = place->x + lmargin;
  191. place->iy = place->y + tmargin;
  192. place->iw = place->w - (lmargin + rmargin);
  193. if (place->iw < 0) place->iw = 0;
  194. place->ih = place->h - (tmargin + bmargin);
  195. if (place->ih < 0) place->ih = 0;
  196.  
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement