1 <ibex xmlns:lib="ibex.lib">
2 // WARNING: more HACKs than a lumberjack
4 <org.ibex.theme.win2k.edit />
6 thisbox.splitable = true; // tells flow code that this object can be split or joined with another block
7 thisbox.block = true; // tells text code that this object exists inside a subfocus
9 thisbox.split = function(w) {
10 ibex.log.info("split called");
11 var pos = ibex..ibex.lib.text.edit..static.getpos(text, w, width, font, fontsize);
12 //var pos = static.getpos(text, w, width, font, fontsize);
14 // give new box requested text range
15 var newbox = ibex.box;
16 ibex.log.info("split called, box made");
17 ibex..ibex.lib.text.block(newbox);
18 newbox.text = text.substring(0, pos);
19 ibex.log.info("split called, applied block template and text given");
21 // give this box the remaining text
22 text = text.substring(pos);
23 ibex.log.info("split called, updated old box");
30 // TODO: share with ibex.lib.text.edit
31 static.getpos = function(t, pxpos, pxlen, tfont, tsize) {
32 // short circuit extremes
33 if (0 >= pxpos) return 0;
34 if (pxpos >= pxlen) return t.length;
36 // inital guess based on average character width
37 var guessch = ibex.math.min(t.length, ibex.math.floor(pxpos / (pxlen / t.length)));
38 var guesspx = ibex.ui.font.width(tfont, tsize, t.substring(0, guessch));
40 if (guesspx > pxpos) {
41 while (guesspx > pxpos) {
42 // textwidth of individual character must account for font kerning
43 guesspx -= ibex.ui.font.width(tfont, tsize, t.substring(guessch -1, guessch +1));
44 guesspx += ibex.ui.font.width(tfont, tsize, t.charAt(guessch));
47 } else if (pxpos > guesspx) {
48 while (pxpos > guesspx) {
50 if (guessch >= t.length) break;
51 guesspx += ibex.ui.font.width(tfont, tsize, t.substring(guessch -1, guessch+1));
52 guesspx -= ibex.ui.font.width(tfont, tsize, t.charAt(guessch));
54 guessch--; // round down