EvaLayoutManager.nut 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. /*
  2. EvaLayoutManager without third party dependencies.
  3. Originally derived from
  4. EvaLayout, Lay it be!
  5. (http://www.codeproject.com/Articles/13891/EvaLayout-Lay-it-be)
  6. Copyright (c) 2014, Domingo Alvarez Duarte - mingodad[at]gmail[dot]com
  7. Released under the MIT LICENSE or GNU LESSER GENERAL PUBLIC LICENSE Version 3
  8. at your choice, permission to release under this licenses was obtained from the
  9. original author Alejandro Xalabarder ales[at]elxala[dot]de
  10. */
  11. const HEADER_ADAPT = "A";
  12. const HEADER_EXPAND = "X";
  13. const EXPAND_HORIZONTAL = "-";
  14. const EXPAND_VERTICAL = "+";
  15. local function max(a, b)
  16. {
  17. return a >= b ? a : b;
  18. }
  19. class WidgetInfo
  20. {
  21. name = null; // name of the component in the layout array
  22. widgetId = null; // component window handle
  23. isLaidOut = null; // if the component has been found in the layout array
  24. // and therefore if indxPos is a valid calculated field
  25. indxPos = null; // place in the layout array mesured in array indices
  26. // left = column index
  27. // right = last column index
  28. // top = row index
  29. // right = last row index
  30. iniRect = null; // initial pos and size of the component
  31. constructor(wname, theHandle, originalRect)
  32. {
  33. name = wname;
  34. widgetId = theHandle;
  35. isLaidOut = false;
  36. iniRect = originalRect;
  37. indxPos = {left : 0, right : 0, top : 0, bottom : 0};
  38. }
  39. }
  40. class EvaLayoutManager
  41. {
  42. isPrecalculated = null;
  43. Hmargin = null;
  44. Vmargin = null;
  45. Hgap = null;
  46. Vgap = null;
  47. fijoH = null;
  48. fijoV = null;
  49. Hpos = null;
  50. Hdim = null;
  51. HextraPos = null;
  52. Vdim = null;
  53. Vpos = null;
  54. VextraPos = null;
  55. columnsReparto = null;
  56. rowsReparto = null;
  57. componentArray = null;
  58. componentArrayIdx = null;
  59. evaLayout = null;
  60. constructor()
  61. {
  62. isPrecalculated = false;
  63. Hmargin = 0;
  64. Vmargin = 0;
  65. Hgap = 0;
  66. Vgap = 0;
  67. fijoH = 0;
  68. fijoV = 0;
  69. Hpos = [];
  70. Hdim = [];
  71. HextraPos = [];
  72. Vdim = [];
  73. Vpos = [];
  74. VextraPos = [];
  75. columnsReparto = [];
  76. rowsReparto = [];
  77. componentArray = [];
  78. componentArrayIdx = {};
  79. evaLayout = null;
  80. }
  81. function setLayout (layoutInfo)
  82. {
  83. if(type(layoutInfo) == "string")
  84. {
  85. var linfo = [];
  86. var lines = layoutInfo.split('\n');
  87. var lines_len = lines.len();
  88. var maxcols = 0;
  89. for(var i=0; i < lines_len; ++i)
  90. {
  91. var rec = lines[i].split(',');
  92. var rec_len = rec.len();
  93. if(rec_len == 1) continue; //ignore blank lines
  94. for(var j=0; j < rec_len; ++j)
  95. {
  96. rec[j] = rec[j].strip();
  97. }
  98. if(rec_len > maxcols)
  99. {
  100. maxcols = rec_len;
  101. }
  102. linfo.push(rec);
  103. }
  104. for(var i=0, len=linfo.len(); i < len; ++i)
  105. {
  106. var rec = linfo[i];
  107. var rec_len = rec.len();
  108. if(rec_len < maxcols)
  109. {
  110. for(var j=rec_len; j < maxcols; ++j)
  111. {
  112. rec.push("");
  113. }
  114. }
  115. }
  116. evaLayout = linfo;
  117. //console.log(linfo);
  118. } else { //assume an array
  119. evaLayout = layoutInfo;
  120. }
  121. isPrecalculated = false;
  122. }
  123. function getComponentRect(compWinHandle, rect)
  124. {
  125. rect.left <- math.random(5, 380);
  126. rect.top <- math.random(5, 380);
  127. rect.right <- math.random(rect.left + 6, 360);
  128. rect.bottom <- rect.top + 20;
  129. }
  130. function addComponent (componentName, compWinHandle)
  131. {
  132. var rect = {};
  133. getComponentRect (compWinHandle, rect);
  134. // add the component to the arrayand index it
  135. componentArrayIdx[componentName] <- componentArray.len();
  136. // add the component to the array
  137. componentArray.push (new WidgetInfo(componentName, compWinHandle, rect));
  138. isPrecalculated = false;
  139. }
  140. function hideShowComponentsInLayout ()
  141. {
  142. var compIds = getLayoutWidgetIds ();
  143. for(var i=0, len=componentArray.len(); i < len; ++i)
  144. {
  145. var wi = componentArray[i];
  146. hideShowComponent (wi.widgetId, !compIds[wi.widgetId]);
  147. }
  148. };
  149. function hideShowComponent (theId, bHide)
  150. {
  151. };
  152. function removeComponents ()
  153. {
  154. componentArray.clear ();
  155. componentArrayIdx.clear();
  156. isPrecalculated = false;
  157. }
  158. function showComponent(cId, bShowHide)
  159. {
  160. print("show", cId, bShowHide);
  161. }
  162. function moveComponent (cId, x, y, dx, dy, bShowHide)
  163. {
  164. print("move", cId, x, y, dx, dy, bShowHide);
  165. }
  166. function distributeWeight (aryExtraPos, aryReparto, HVdim, totalReparto, nrcsize)
  167. {
  168. for (var ii = 0, len=aryExtraPos.len(); ii < len; ii ++) aryExtraPos[ii] = 0;
  169. var arsize = aryReparto.len();
  170. var totalWeight = 0;
  171. for (var ii = 0; ii < arsize; ii ++) totalWeight += aryReparto[ii][1];
  172. var repartHV = totalReparto / ((totalWeight == 0) ? 1 : totalWeight);
  173. for (var ii = 0; ii < arsize; ii ++)
  174. {
  175. var colAry = aryReparto[ii];
  176. var indx = colAry[0];
  177. var indxExpandHV = repartHV * colAry[1];
  178. HVdim[indx] = indxExpandHV;
  179. for (var res = indx+1; res < nrcsize; res ++)
  180. {
  181. aryExtraPos[res] += indxExpandHV;
  182. }
  183. }
  184. };
  185. function doLayout (totWidth, totHeight)
  186. {
  187. precalculateAll ();
  188. // repartir H
  189. distributeWeight(HextraPos, columnsReparto, Hdim, (totWidth - fijoH), nColumns());
  190. // repartir V
  191. distributeWeight(VextraPos, rowsReparto, Vdim, (totHeight - fijoV), nRows());
  192. for (var ii = 0, len = componentArray.len(); ii < len; ii ++)
  193. {
  194. var wi = componentArray[ii];
  195. showComponent (wi.widgetId, (wi.isLaidOut) ? true : false);
  196. if (! wi.isLaidOut) continue;
  197. var indxPos = wi.indxPos;
  198. var indxPosLeft = indxPos.left;
  199. var indxPosTop = indxPos.top;
  200. var indxPosRight = indxPos.right;
  201. var indxPosBottom = indxPos.bottom;
  202. var x = Hpos[indxPosLeft] + HextraPos[indxPosLeft];
  203. var y = Vpos[indxPosTop] + VextraPos[indxPosTop];
  204. var dx = 0;
  205. var dy = 0;
  206. var mm;
  207. for (mm = indxPosLeft; mm <= indxPosRight; mm ++)
  208. {
  209. if (mm != indxPosLeft) dx += Hgap;
  210. dx += Hdim[mm];
  211. }
  212. for (mm = indxPosTop; mm <= indxPosBottom; mm ++)
  213. {
  214. if (mm != indxPosTop) dy += Vgap;
  215. dy += Vdim[mm];
  216. }
  217. //here we have a problem when the number become negative
  218. //the widget is not managed
  219. if (x < 0 || y < 0 || dx < 0 || dy < 0) //continue;
  220. {
  221. var elmRect = {};
  222. var elm = getComponentRect(wi.widgetId, elmRect);
  223. if(x < 0) x = elmRect.left;
  224. if(dx < 0) dx = elmRect.right - elmRect.left;
  225. if(y < 0) y = elmRect.top;
  226. if(dy < 0) dy = elmRect.bottom - elmRect.top;
  227. }
  228. moveComponent (wi.widgetId, x, y, dx, dy, true);
  229. //InvalidateRect(hControl, NULL, TRUE);
  230. }
  231. };
  232. function getWidgets (vecNames)
  233. {
  234. var ncsize = nColumns();
  235. for (var rr = 0, rlen = nRows(); rr < rlen; rr ++)
  236. {
  237. for (var cc = 0; cc < ncsize; cc ++)
  238. {
  239. var name = widgetAt (rr, cc);
  240. if (name.len() > 0 && name != EXPAND_HORIZONTAL && name != EXPAND_VERTICAL)
  241. {
  242. vecNames.push (name);
  243. }
  244. }
  245. }
  246. }
  247. function nColumns ()
  248. {
  249. return max (0, evaLayout[1].len() - 1);
  250. }
  251. function nRows ()
  252. {
  253. return max (0, evaLayout.len() - 2);
  254. }
  255. function widgetAt (nrow, ncol)
  256. {
  257. var theRow = nrow+2;
  258. if(evaLayout.len() > theRow)
  259. {
  260. var theCol = ncol + 1;
  261. theRow = evaLayout[theRow];
  262. if(theRow.len() > theCol)
  263. {
  264. return theRow[theCol];
  265. }
  266. }
  267. return null;
  268. }
  269. //private
  270. function getLayoutWidgetIds ()
  271. {
  272. var widgetIds = {};
  273. var ncols = nColumns();
  274. for(var row=0, rlen=nRows(); row<rlen; ++row)
  275. {
  276. for(var col=0; col < ncols; ++col)
  277. {
  278. var wi = widgetAt(row, col);
  279. var found = widgetIds[wi.widgetId];
  280. widgetIds[wi.widgetId] = found ? found+1 : 1;
  281. }
  282. }
  283. return widgetIds;
  284. };
  285. //private
  286. function isExpandWeight (headStr, aryToStore, idxToStore)
  287. {
  288. var strLen = headStr.len() ;
  289. if (strLen && headStr[0] == HEADER_EXPAND[0])
  290. {
  291. var weight = 1;
  292. if(strLen > 1)
  293. {
  294. weight = headStr.slice(1).tointeger();
  295. }
  296. aryToStore.push ([idxToStore, weight]); // compute later
  297. return true;
  298. }
  299. return false;
  300. };
  301. function computeVHDim (nrcsize, getHeader, VHmargin, VHgap, VHdim,
  302. VHpos, VHextraPos, aryReparto, getMinOf)
  303. {
  304. var fijoHV = VHmargin;
  305. for (var rr = 0; rr < nrcsize; rr ++)
  306. {
  307. var heaStr = getHeader(rr);
  308. var gap = (rr == 0) ? 0 : VHgap;
  309. VHdim.push (0);
  310. VHpos.push (0);
  311. VHextraPos.push (0);
  312. if (!isExpandWeight(heaStr, aryReparto, rr))
  313. {
  314. if (heaStr == "" || heaStr == HEADER_ADAPT)
  315. {
  316. VHdim[rr] = getMinOf(rr); // maximum-minimum of the row
  317. }
  318. else
  319. {
  320. VHdim[rr] = heaStr.tointeger(); // indicated size
  321. }
  322. }
  323. VHpos[rr] = fijoHV + gap;
  324. fijoHV += VHdim[rr];
  325. fijoHV += gap;
  326. }
  327. return fijoHV + VHmargin;
  328. };
  329. function precalculateAll ()
  330. {
  331. if (isPrecalculated) return;
  332. var el_row = evaLayout[0];
  333. Hmargin = max(0, el_row[1].tointeger() );
  334. Vmargin = max(0, el_row[2].tointeger() );
  335. Hgap = max(0, el_row[3].tointeger() );
  336. Vgap = max(0, el_row[4].tointeger() );
  337. Hdim.clear ();
  338. Hpos.clear ();
  339. Vdim.clear ();
  340. Vpos.clear ();
  341. columnsReparto.clear ();
  342. rowsReparto.clear ();
  343. for (var ii = 0, len = componentArray.len(); ii < len; ii ++)
  344. {
  345. componentArray[ii].isLaidOut = false;
  346. }
  347. //var cc;
  348. //var rr;
  349. var nrsize = nRows();
  350. var ncsize = nColumns();
  351. // compute Vdim
  352. fijoV = computeVHDim(nrsize, rowHeader, Vmargin, Vgap, Vdim,
  353. Vpos, VextraPos, rowsReparto, minHeightOfRow);
  354. // compute Hdim
  355. fijoH = computeVHDim(ncsize, columnHeader, Hmargin, Hgap, Hdim,
  356. Hpos, HextraPos, columnsReparto, minWidthOfColumn);
  357. // finding all components in the layout array
  358. for (var cc = 0; cc < ncsize; cc ++)
  359. {
  360. for (var rr = 0; rr < nrsize; rr ++)
  361. {
  362. var name = widgetAt(rr, cc);
  363. var indx = indxComponent (name);
  364. if (indx == -1) continue;
  365. var wid = componentArray[indx];
  366. var indxPos = wid.indxPos;
  367. // set position x,y
  368. indxPos.left = cc;
  369. indxPos.top = rr;
  370. // set position x2,y2
  371. var ava = cc;
  372. while (ava+1 < ncsize && widgetAt(rr, ava+1) == EXPAND_HORIZONTAL) ava ++;
  373. indxPos.right = ava;
  374. ava = rr;
  375. while (ava+1 < nrsize && widgetAt(ava+1, cc) == EXPAND_VERTICAL) ava ++;
  376. indxPos.bottom = ava;
  377. wid.isLaidOut = true;
  378. }
  379. }
  380. isPrecalculated = true;
  381. };
  382. function columnHeader (ncol)
  383. {
  384. return evaLayout[1][ncol + 1];
  385. }
  386. function rowHeader (nrow)
  387. {
  388. return evaLayout[2 + nrow][0];
  389. }
  390. function minHeightOfRow (nrow)
  391. {
  392. // el componente mas alto de la columna
  393. var maxheight = 0;
  394. for (var cc = 0, len = nColumns(); cc < len; cc ++)
  395. {
  396. var name = widgetAt (nrow, cc);
  397. var indx = indxComponent (name);
  398. if (indx == -1) continue;
  399. // if the widget occupies more than one row do not compute it
  400. if (widgetAt (nrow+1, cc) == EXPAND_VERTICAL) continue;
  401. var wid = componentArray[indx];
  402. var height = wid.iniRect.bottom - wid.iniRect.top;
  403. maxheight = max (maxheight, height);
  404. }
  405. return maxheight;
  406. }
  407. function minWidthOfColumn (ncol)
  408. {
  409. // el componente mas ancho de la columna
  410. var maxwidth = 0;
  411. for (var rr = 0, len = nRows(); rr < len; rr ++)
  412. {
  413. var name = widgetAt (rr, ncol);
  414. var indx = indxComponent (name);
  415. if (indx == -1) continue;
  416. // if the widget occupies more than one column do not compute it
  417. if (widgetAt (rr, ncol+1) == EXPAND_HORIZONTAL) continue;
  418. var wid = componentArray[indx];
  419. var width = wid.iniRect.right - wid.iniRect.left;
  420. maxwidth = max (maxwidth, width);
  421. }
  422. return maxwidth;
  423. }
  424. function indxComponent (compName)
  425. {
  426. return table_rawget(componentArrayIdx, compName, -1);
  427. };
  428. }
  429. class FltkEvaLayoutManager extends EvaLayoutManager {
  430. function showComponent(cId, bShowHide)
  431. {
  432. //print("show", cId, bShowHide);
  433. }
  434. function getComponentRect(cId, rect)
  435. {
  436. rect.left <- cId.x();
  437. rect.top <- cId.y();
  438. rect.right <- cId.w();
  439. rect.bottom <- rect.top + cId.h();
  440. }
  441. function moveComponent (cId, x, y, dx, dy, bShowHide)
  442. {
  443. //print(format([==[document.getElementById("%s").setAttribute("style", "position:absolute;left:%dpx;top:%dpx;right:%dpx;bottom:%dpx;");]==], cId, x, y, dx, dy, bShowHide));
  444. cId.resize(x, y, dx, dy);
  445. }
  446. }
  447. var layInfo = [
  448. ["EvaLayout", "10", "10", "5", "5"],
  449. ["grid" , "75" , "X" , "A" ],
  450. [ "A" , "boton1" , "memo" , "-" ],
  451. [ "A" , "boton2" , "+" , "" ],
  452. [ "A" , "boton3" , "+" , "" ],
  453. [ "X" , "" , "+" , "" ],
  454. [ "A" , "edit1" , "-" , "boton4" ],
  455. ];
  456. var manager = new FltkEvaLayoutManager();
  457. manager.setLayout(layInfo);
  458. class EvaWindow extends Fl_Window {
  459. boton1 = null;
  460. boton2 = null;
  461. boton3 = null;
  462. boton4 = null;
  463. memo = null;
  464. edit1 = null;
  465. constructor() {
  466. base.constructor(10, 50, 300, 280, "EvaWindow");
  467. begin();
  468. boton1 = new Fl_Button(0, 0, 35, 25,"button1");
  469. boton1->labelsize(16);
  470. boton2 = new Fl_Button(0, 0, 35, 25,"button2");
  471. boton2->labelsize(16);
  472. boton3 = new Fl_Button(0, 0, 35, 25,"button3");
  473. boton3->labelsize(16);
  474. boton4 = new Fl_Button(0, 0, 105, 25,"button4");
  475. boton4->labelsize(16);
  476. memo = new Fl_Input(0, 0, 35, 25,"memo");
  477. memo->labelsize(16);
  478. edit1 = new Fl_Input(0, 0, 35, 25,"edit1");
  479. edit1->labelsize(16);
  480. end();
  481. }
  482. }
  483. local win = new EvaWindow();
  484. win->resizable(win);
  485. win->show_main();
  486. manager.addComponent("memo", win.memo);
  487. manager.addComponent("boton1", win.boton1);
  488. manager.addComponent("boton2", win.boton2);
  489. manager.addComponent("boton3", win.boton3);
  490. manager.addComponent("boton4", win.boton4);
  491. manager.addComponent("edit1", win.edit1);
  492. manager.doLayout(win.w(), win.h());
  493. //Fl:scheme("plastic");
  494. Fl.scheme("gtk+");
  495. //use partial match to find verdana font
  496. Fl.visual(FL_RGB);
  497. //allow arrow keys navigation
  498. Fl.option(Fl.OPTION_ARROW_FOCUS, true);
  499. Fl.run();
  500. /*
  501. manager.addComponent("memo", "memo");
  502. manager.addComponent("boton1", "boton1");
  503. manager.addComponent("boton2", "boton2");
  504. manager.addComponent("boton3", "boton3");
  505. manager.addComponent("boton4", "boton4");
  506. manager.addComponent("edit1", "edit1");
  507. print(manager.nRows(), manager.nColumns());
  508. manager.doLayout(300, 400);
  509. manager.doLayout(400, 300);
  510. manager.doLayout(500, 600);
  511. manager.doLayout(600, 500);
  512. */