initial checkin
[org.ibex.widgets.git] / src / ibex / lib / text / block.t
1 <ibex xmlns:lib="ibex.lib">
2     // WARNING: more HACKs than a lumberjack
3
4     <org.ibex.theme.win2k.edit />
5     <ui:box shrink="true">
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
8
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);
13
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");
20
21             // give this box the remaining text
22             text = text.substring(pos);
23             ibex.log.info("split called, updated old box");
24
25             return newbox;
26         }
27     </ui:box>
28
29
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;
35
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));
39
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));
45                 guessch--;
46             }
47         } else if (pxpos > guesspx) {
48             while (pxpos > guesspx) {
49                 guessch++;
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));
53             }
54             guessch--;  // round down
55         }
56
57         return guessch;
58     }
59
60
61 </ibex>