2009-01-05 18:31:21 -05:00
|
|
|
! Copyright (C) 2008, 2009 Slava Pestov.
|
2008-12-19 03:37:40 -05:00
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2009-02-05 23:14:35 -05:00
|
|
|
USING: accessors arrays colors colors.constants fry kernel math
|
2009-02-07 19:09:50 -05:00
|
|
|
math.rectangles math.order math.vectors namespaces opengl sequences
|
|
|
|
ui.gadgets ui.gadgets.scrollers ui.gadgets.status-bar
|
2009-02-14 20:50:22 -05:00
|
|
|
ui.gadgets.worlds ui.gestures ui.render ui.text
|
2009-02-11 05:55:33 -05:00
|
|
|
ui.images ui.gadgets.menus ui.gadgets.line-support math.rectangles
|
|
|
|
models math.ranges sequences combinators fonts locals strings ;
|
2008-12-19 03:37:40 -05:00
|
|
|
IN: ui.gadgets.tables
|
|
|
|
|
|
|
|
! Row rendererer protocol
|
2009-02-11 05:55:33 -05:00
|
|
|
GENERIC: prototype-row ( renderer -- columns )
|
2008-12-19 03:37:40 -05:00
|
|
|
GENERIC: row-columns ( row renderer -- columns )
|
2009-01-07 13:18:42 -05:00
|
|
|
GENERIC: row-value ( row renderer -- object )
|
2009-02-05 23:14:35 -05:00
|
|
|
GENERIC: row-color ( row renderer -- color )
|
2008-12-19 03:37:40 -05:00
|
|
|
|
|
|
|
SINGLETON: trivial-renderer
|
|
|
|
|
|
|
|
M: trivial-renderer row-columns drop ;
|
2009-02-11 05:55:33 -05:00
|
|
|
M: object prototype-row drop { "" } ;
|
2009-01-07 13:18:42 -05:00
|
|
|
M: object row-value drop ;
|
2009-02-05 23:14:35 -05:00
|
|
|
M: object row-color 2drop f ;
|
|
|
|
|
2008-12-19 03:37:40 -05:00
|
|
|
TUPLE: table < gadget
|
2009-02-11 05:55:33 -05:00
|
|
|
{ renderer initial: trivial-renderer }
|
|
|
|
filled-column column-alignment
|
|
|
|
{ action initial: [ drop ] }
|
|
|
|
single-click?
|
|
|
|
{ hook initial: [ ] }
|
|
|
|
{ gap initial: 6 }
|
2009-01-05 18:31:21 -05:00
|
|
|
column-widths total-width
|
2009-02-07 19:09:50 -05:00
|
|
|
font selection-color focus-border-color
|
2009-02-11 05:55:33 -05:00
|
|
|
{ mouse-color initial: COLOR: black }
|
|
|
|
{ column-line-color initial: COLOR: dark-gray }
|
|
|
|
selection-required?
|
2009-01-05 18:31:21 -05:00
|
|
|
selected-index selected-value
|
|
|
|
mouse-index
|
|
|
|
focused? ;
|
2008-12-19 03:37:40 -05:00
|
|
|
|
|
|
|
: <table> ( rows -- table )
|
|
|
|
table new-gadget
|
|
|
|
swap >>model
|
|
|
|
f <model> >>selected-value
|
2009-01-06 17:52:12 -05:00
|
|
|
sans-serif-font >>font
|
|
|
|
selection-color >>selection-color
|
2009-02-11 05:55:33 -05:00
|
|
|
focus-border-color >>focus-border-color ;
|
2008-12-19 03:37:40 -05:00
|
|
|
|
2009-01-05 18:31:21 -05:00
|
|
|
<PRIVATE
|
|
|
|
|
2009-02-11 05:55:33 -05:00
|
|
|
GENERIC: cell-width ( font cell -- x )
|
|
|
|
GENERIC: cell-height ( font cell -- y )
|
|
|
|
GENERIC: draw-cell ( font cell -- )
|
|
|
|
|
|
|
|
M: string cell-width text-width ;
|
|
|
|
M: string cell-height text-height ;
|
|
|
|
M: string draw-cell draw-text ;
|
|
|
|
|
|
|
|
M: image-name cell-width nip image-dim first ;
|
|
|
|
M: image-name cell-height nip image-dim second ;
|
|
|
|
M: image-name draw-cell nip draw-image ;
|
2008-12-19 03:37:40 -05:00
|
|
|
|
|
|
|
: table-rows ( table -- rows )
|
|
|
|
[ control-value ] [ renderer>> ] bi '[ _ row-columns ] map ;
|
|
|
|
|
2009-02-11 05:55:33 -05:00
|
|
|
: column-offsets ( widths gap -- x xs )
|
|
|
|
[ 0 ] dip '[ _ + + ] accumulate ;
|
|
|
|
|
|
|
|
: initial-widths ( rows -- widths )
|
|
|
|
first length 0 <repetition> ;
|
|
|
|
|
|
|
|
: row-column-widths ( font row -- widths )
|
|
|
|
[ cell-width ] with map ;
|
|
|
|
|
|
|
|
: (compute-column-widths) ( gap font rows -- total widths )
|
|
|
|
[ 2drop 0 { } ] [
|
|
|
|
[ nip initial-widths ] 2keep
|
|
|
|
[ row-column-widths vmax ] with each
|
|
|
|
[ swap [ column-offsets drop ] keep - ] keep
|
2008-12-19 03:37:40 -05:00
|
|
|
] if-empty ;
|
|
|
|
|
2009-01-05 18:31:21 -05:00
|
|
|
: compute-column-widths ( table -- total-width column-widths )
|
2009-02-11 05:55:33 -05:00
|
|
|
[ gap>> ] [ font>> ] [ table-rows ] tri (compute-column-widths) ;
|
2009-01-05 18:31:21 -05:00
|
|
|
|
2008-12-19 03:37:40 -05:00
|
|
|
: update-cached-widths ( table -- )
|
2009-01-05 18:31:21 -05:00
|
|
|
dup compute-column-widths
|
|
|
|
[ >>total-width ] [ >>column-widths ] bi*
|
|
|
|
drop ;
|
|
|
|
|
|
|
|
: filled-column-width ( table -- n )
|
|
|
|
[ dim>> first ] [ total-width>> ] bi [-] ;
|
|
|
|
|
|
|
|
: update-filled-column ( table -- )
|
|
|
|
[ filled-column-width ]
|
|
|
|
[ filled-column>> ]
|
|
|
|
[ column-widths>> ] tri
|
|
|
|
2dup empty? not and
|
|
|
|
[ [ + ] change-nth ] [ 3drop ] if ;
|
2008-12-19 03:37:40 -05:00
|
|
|
|
2009-01-05 18:31:21 -05:00
|
|
|
M: table layout*
|
|
|
|
[ update-cached-widths ] [ update-filled-column ] bi ;
|
2008-12-19 03:37:40 -05:00
|
|
|
|
|
|
|
: row-rect ( table row -- rect )
|
|
|
|
[ [ line-height ] dip * 0 swap 2array ]
|
|
|
|
[ drop [ dim>> first ] [ line-height ] bi 2array ] 2bi <rect> ;
|
|
|
|
|
2009-01-12 23:16:57 -05:00
|
|
|
: highlight-row ( table row color quot -- )
|
|
|
|
[ [ row-rect rect-bounds ] dip gl-color ] dip
|
|
|
|
'[ _ @ ] with-translation ; inline
|
|
|
|
|
2009-02-09 01:23:47 -05:00
|
|
|
: draw-selected-row ( table -- )
|
|
|
|
{
|
|
|
|
{ [ dup selected-index>> not ] [ drop ] }
|
|
|
|
[
|
|
|
|
[ ] [ selected-index>> ] [ selection-color>> ] tri
|
|
|
|
[ gl-fill-rect ] highlight-row
|
|
|
|
]
|
|
|
|
} cond ;
|
|
|
|
|
|
|
|
: draw-focused-row ( table -- )
|
|
|
|
{
|
|
|
|
{ [ dup focused?>> not ] [ drop ] }
|
|
|
|
{ [ dup selected-index>> not ] [ drop ] }
|
|
|
|
[
|
|
|
|
[ ] [ selected-index>> ] [ focus-border-color>> ] tri
|
|
|
|
[ gl-rect ] highlight-row
|
|
|
|
]
|
|
|
|
} cond ;
|
|
|
|
|
|
|
|
: draw-moused-row ( table -- )
|
2009-01-12 23:16:57 -05:00
|
|
|
dup mouse-index>> dup [
|
|
|
|
over mouse-color>> [ gl-rect ] highlight-row
|
|
|
|
] [ 2drop ] if ;
|
2008-12-19 03:37:40 -05:00
|
|
|
|
2009-02-11 05:55:33 -05:00
|
|
|
: column-line-offsets ( widths gap -- xs )
|
|
|
|
[ column-offsets nip [ f ] ]
|
|
|
|
[ 2/ '[ rest-slice [ _ - ] map ] ]
|
|
|
|
bi if-empty ;
|
2009-01-12 20:32:10 -05:00
|
|
|
|
2009-02-11 05:55:33 -05:00
|
|
|
: draw-column-lines ( table -- )
|
2009-01-08 01:04:44 -05:00
|
|
|
[ column-line-color>> gl-color ]
|
|
|
|
[
|
2009-02-11 05:55:33 -05:00
|
|
|
[ [ column-widths>> ] [ gap>> ] bi column-line-offsets ] [ dim>> second ] bi
|
2009-01-08 01:04:44 -05:00
|
|
|
'[ [ 0 2array ] [ _ 2array ] bi gl-line ] each
|
|
|
|
] bi ;
|
|
|
|
|
2009-01-05 18:31:21 -05:00
|
|
|
: column-loc ( font column width align -- loc )
|
2009-02-11 05:55:33 -05:00
|
|
|
[ [ cell-width ] dip swap - ] dip
|
2009-01-05 18:31:21 -05:00
|
|
|
* 0 2array ;
|
|
|
|
|
2009-02-11 05:55:33 -05:00
|
|
|
: translate-column ( width gap -- )
|
|
|
|
+ 0 2array gl-translate ;
|
|
|
|
|
|
|
|
: draw-column ( font column width align gap -- )
|
|
|
|
[
|
|
|
|
over [
|
|
|
|
[ 2dup ] 2dip column-loc
|
|
|
|
[ draw-cell ] with-translation
|
|
|
|
] dip
|
|
|
|
] dip translate-column ;
|
2009-01-05 18:31:21 -05:00
|
|
|
|
2009-02-05 23:14:35 -05:00
|
|
|
: column-alignment ( table -- seq )
|
|
|
|
dup column-alignment>>
|
|
|
|
[ ] [ column-widths>> length 0 <repetition> ] ?if ;
|
|
|
|
|
2009-02-07 19:09:50 -05:00
|
|
|
:: row-font ( row index table -- font )
|
|
|
|
table font>> clone
|
|
|
|
row table renderer>> row-color [ >>foreground ] when*
|
|
|
|
index table selected-index>> = [ table selection-color>> >>background ] when ;
|
2008-12-19 03:37:40 -05:00
|
|
|
|
2009-02-07 19:09:50 -05:00
|
|
|
M: table draw-line ( row index table -- )
|
|
|
|
[
|
|
|
|
nip
|
|
|
|
[ renderer>> row-columns ]
|
|
|
|
[ column-widths>> ]
|
|
|
|
[ column-alignment ]
|
|
|
|
tri
|
2009-02-11 05:55:33 -05:00
|
|
|
]
|
|
|
|
[ row-font ]
|
|
|
|
[ 2nip gap>> ] 3tri
|
|
|
|
'[ [ _ ] 3dip _ draw-column ] 3each ;
|
2008-12-19 03:37:40 -05:00
|
|
|
|
|
|
|
M: table draw-gadget*
|
|
|
|
dup control-value empty? [ drop ] [
|
2009-02-10 19:47:34 -05:00
|
|
|
{
|
|
|
|
[ draw-selected-row ]
|
|
|
|
[ draw-lines ]
|
2009-02-11 05:55:33 -05:00
|
|
|
[ draw-column-lines ]
|
2009-02-10 19:47:34 -05:00
|
|
|
[ draw-focused-row ]
|
|
|
|
[ draw-moused-row ]
|
|
|
|
} cleave
|
2008-12-19 03:37:40 -05:00
|
|
|
] if ;
|
|
|
|
|
2009-02-11 05:55:33 -05:00
|
|
|
M: table line-height ( table -- y )
|
|
|
|
[ font>> ] [ renderer>> prototype-row ] bi
|
|
|
|
[ cell-height ] with [ max ] map-reduce ;
|
|
|
|
|
2008-12-19 03:37:40 -05:00
|
|
|
M: table pref-dim*
|
2009-01-05 18:31:21 -05:00
|
|
|
[ compute-column-widths drop ] keep
|
2009-02-11 05:55:33 -05:00
|
|
|
[ line-height ] [ control-value length ] bi * 2array ;
|
2008-12-19 03:37:40 -05:00
|
|
|
|
2009-01-08 18:02:54 -05:00
|
|
|
: nth-row ( row table -- value/f ? )
|
|
|
|
over [ control-value nth t ] [ 2drop f f ] if ;
|
2008-12-19 03:37:40 -05:00
|
|
|
|
2009-01-08 18:02:54 -05:00
|
|
|
PRIVATE>
|
|
|
|
|
|
|
|
: (selected-row) ( table -- value/f ? )
|
|
|
|
[ selected-index>> ] keep nth-row ;
|
|
|
|
|
|
|
|
: selected-row ( table -- value/f ? )
|
|
|
|
[ (selected-row) ] keep
|
|
|
|
swap [ renderer>> row-value t ] [ 2drop f f ] if ;
|
|
|
|
|
|
|
|
<PRIVATE
|
2008-12-19 03:37:40 -05:00
|
|
|
|
|
|
|
: update-selected-value ( table -- )
|
2009-01-08 18:02:54 -05:00
|
|
|
[ selected-row drop ] [ selected-value>> ] bi set-model ;
|
2008-12-19 03:37:40 -05:00
|
|
|
|
2009-01-13 21:47:08 -05:00
|
|
|
: initial-selected-index ( model table -- n/f )
|
|
|
|
[ value>> length 1 >= ] [ selection-required?>> ] bi* and 0 f ? ;
|
|
|
|
|
2009-01-15 01:52:05 -05:00
|
|
|
: show-row-summary ( table n -- )
|
|
|
|
over nth-row
|
|
|
|
[ swap [ renderer>> row-value ] keep show-summary ]
|
|
|
|
[ 2drop ]
|
|
|
|
if ;
|
|
|
|
|
2008-12-19 03:37:40 -05:00
|
|
|
M: table model-changed
|
2009-01-25 18:55:27 -05:00
|
|
|
[ nip ] [ initial-selected-index ] 2bi {
|
2009-01-15 01:52:05 -05:00
|
|
|
[ >>selected-index drop ]
|
|
|
|
[ show-row-summary ]
|
|
|
|
[ drop update-selected-value ]
|
|
|
|
[ drop relayout ]
|
|
|
|
} 2cleave ;
|
2008-12-19 03:37:40 -05:00
|
|
|
|
|
|
|
: thin-row-rect ( table row -- rect )
|
|
|
|
row-rect [ { 0 1 } v* ] change-dim ;
|
|
|
|
|
2009-01-09 18:58:22 -05:00
|
|
|
: (select-row) ( table n -- )
|
2009-01-22 02:19:15 -05:00
|
|
|
[ dup [ [ thin-row-rect ] [ drop ] 2bi scroll>rect ] [ 2drop ] if ]
|
2009-01-06 17:52:12 -05:00
|
|
|
[ >>selected-index relayout-1 ]
|
2008-12-19 03:37:40 -05:00
|
|
|
2bi ;
|
|
|
|
|
|
|
|
: mouse-row ( table -- n )
|
2009-02-07 19:09:50 -05:00
|
|
|
[ hand-rel second ] keep y>line ;
|
2008-12-19 03:37:40 -05:00
|
|
|
|
2009-01-06 17:52:12 -05:00
|
|
|
: table-button-down ( table -- )
|
2008-12-19 03:37:40 -05:00
|
|
|
dup request-focus
|
2009-01-06 17:52:12 -05:00
|
|
|
dup control-value empty? [ drop ] [
|
2009-02-07 19:09:50 -05:00
|
|
|
dup [ mouse-row ] keep validate-line
|
2009-01-06 17:52:12 -05:00
|
|
|
[ >>mouse-index ] [ (select-row) ] bi
|
|
|
|
] if ;
|
|
|
|
|
2009-01-13 17:22:07 -05:00
|
|
|
PRIVATE>
|
|
|
|
|
2009-01-06 17:52:12 -05:00
|
|
|
: row-action ( table -- )
|
2009-02-09 01:23:47 -05:00
|
|
|
dup selected-row
|
|
|
|
[ swap [ action>> call ] [ hook>> call ] bi ]
|
|
|
|
[ 2drop ]
|
|
|
|
if ;
|
2009-01-06 17:52:12 -05:00
|
|
|
|
2009-01-13 17:22:07 -05:00
|
|
|
<PRIVATE
|
|
|
|
|
2009-01-06 17:52:12 -05:00
|
|
|
: table-button-up ( table -- )
|
2009-02-09 01:23:47 -05:00
|
|
|
dup single-click?>> hand-click# get 2 = or
|
2009-01-06 17:52:12 -05:00
|
|
|
[ row-action ] [ update-selected-value ] if ;
|
2008-12-19 03:37:40 -05:00
|
|
|
|
2009-01-09 18:58:22 -05:00
|
|
|
: select-row ( table n -- )
|
2009-02-07 19:09:50 -05:00
|
|
|
over validate-line
|
2009-01-09 18:58:22 -05:00
|
|
|
[ (select-row) ]
|
|
|
|
[ drop update-selected-value ]
|
2009-01-15 01:52:05 -05:00
|
|
|
[ show-row-summary ]
|
|
|
|
2tri ;
|
2008-12-19 03:37:40 -05:00
|
|
|
|
2009-01-22 02:19:15 -05:00
|
|
|
: prev/next-row ( table n -- )
|
|
|
|
[ dup selected-index>> ] dip '[ _ + ] [ 0 ] if* select-row ;
|
|
|
|
|
2008-12-19 03:37:40 -05:00
|
|
|
: prev-row ( table -- )
|
2009-01-22 02:19:15 -05:00
|
|
|
-1 prev/next-row ;
|
2008-12-19 03:37:40 -05:00
|
|
|
|
|
|
|
: next-row ( table -- )
|
2009-01-22 02:19:15 -05:00
|
|
|
1 prev/next-row ;
|
2008-12-19 03:37:40 -05:00
|
|
|
|
|
|
|
: first-row ( table -- )
|
|
|
|
0 select-row ;
|
|
|
|
|
|
|
|
: last-row ( table -- )
|
|
|
|
dup control-value length 1- select-row ;
|
|
|
|
|
|
|
|
: hide-mouse-help ( table -- )
|
|
|
|
f >>mouse-index [ hide-status ] [ relayout-1 ] bi ;
|
|
|
|
|
2009-01-07 13:18:42 -05:00
|
|
|
: valid-row? ( row table -- ? )
|
|
|
|
control-value length 1- 0 swap between? ;
|
|
|
|
|
|
|
|
: if-mouse-row ( table true false -- )
|
|
|
|
[ [ mouse-row ] keep 2dup valid-row? ]
|
|
|
|
[ ] [ '[ nip @ ] ] tri* if ; inline
|
|
|
|
|
2008-12-19 03:37:40 -05:00
|
|
|
: show-mouse-help ( table -- )
|
2009-01-07 13:18:42 -05:00
|
|
|
[
|
2009-01-15 01:52:05 -05:00
|
|
|
swap
|
|
|
|
[ >>mouse-index relayout-1 ]
|
|
|
|
[ show-row-summary ]
|
|
|
|
2bi
|
2009-01-07 13:18:42 -05:00
|
|
|
] [ hide-mouse-help ] if-mouse-row ;
|
|
|
|
|
2009-01-16 17:39:32 -05:00
|
|
|
: show-table-menu ( table -- )
|
2009-01-07 13:18:42 -05:00
|
|
|
[
|
2009-02-02 14:43:54 -05:00
|
|
|
[ nip ]
|
|
|
|
[ [ nth-row drop ] [ renderer>> row-value ] [ hook>> ] tri ] 2bi
|
2009-01-16 17:39:32 -05:00
|
|
|
show-operations-menu
|
2009-01-07 13:18:42 -05:00
|
|
|
] [ drop ] if-mouse-row ;
|
2008-12-19 03:37:40 -05:00
|
|
|
|
|
|
|
table H{
|
2009-01-28 01:30:57 -05:00
|
|
|
{ mouse-enter [ show-mouse-help ] }
|
|
|
|
{ mouse-leave [ hide-mouse-help ] }
|
|
|
|
{ motion [ show-mouse-help ] }
|
2009-01-06 17:52:12 -05:00
|
|
|
{ T{ button-down } [ table-button-down ] }
|
2009-01-16 17:39:32 -05:00
|
|
|
{ T{ button-down f f 3 } [ show-table-menu ] }
|
2009-01-06 17:52:12 -05:00
|
|
|
{ T{ button-up } [ table-button-up ] }
|
2009-01-28 01:30:57 -05:00
|
|
|
{ gain-focus [ t >>focused? drop ] }
|
|
|
|
{ lose-focus [ f >>focused? drop ] }
|
2009-01-06 17:52:12 -05:00
|
|
|
{ T{ drag } [ table-button-down ] }
|
2009-01-06 17:53:01 -05:00
|
|
|
{ T{ key-down f f "RET" } [ row-action ] }
|
2008-12-19 03:37:40 -05:00
|
|
|
{ T{ key-down f f "UP" } [ prev-row ] }
|
|
|
|
{ T{ key-down f f "DOWN" } [ next-row ] }
|
|
|
|
{ T{ key-down f f "HOME" } [ first-row ] }
|
|
|
|
{ T{ key-down f f "END" } [ last-row ] }
|
|
|
|
} set-gestures
|
2009-01-05 18:31:21 -05:00
|
|
|
|
|
|
|
PRIVATE>
|