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-08-05 23:45:56 -04:00
|
|
|
USING: accessors assocs hashtables arrays colors colors.constants fry
|
|
|
|
kernel math math.functions math.ranges math.rectangles math.order
|
|
|
|
math.vectors namespaces opengl sequences ui.gadgets
|
2009-05-16 20:49:27 -04:00
|
|
|
ui.gadgets.scrollers ui.gadgets.status-bar ui.gadgets.worlds
|
|
|
|
ui.gestures ui.render ui.pens.solid ui.text ui.commands ui.images
|
2009-08-05 23:45:56 -04:00
|
|
|
ui.gadgets.menus ui.gadgets.line-support models combinators
|
|
|
|
combinators.short-circuit fonts locals strings sets sorting ;
|
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 )
|
2009-02-16 05:25:15 -05:00
|
|
|
GENERIC: column-alignment ( renderer -- alignment )
|
|
|
|
GENERIC: filled-column ( renderer -- n )
|
|
|
|
GENERIC: column-titles ( renderer -- strings )
|
|
|
|
|
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
|
|
|
|
|
2009-02-11 05:55:33 -05:00
|
|
|
M: object prototype-row drop { "" } ;
|
2009-02-16 05:25:15 -05:00
|
|
|
M: object column-alignment drop f ;
|
|
|
|
M: object filled-column drop f ;
|
|
|
|
M: object column-titles drop f ;
|
|
|
|
|
|
|
|
M: trivial-renderer row-columns 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 ;
|
|
|
|
|
2009-02-15 05:01:57 -05:00
|
|
|
TUPLE: table < line-gadget
|
2009-02-11 05:55:33 -05:00
|
|
|
{ renderer initial: trivial-renderer }
|
|
|
|
{ action initial: [ drop ] }
|
|
|
|
single-click?
|
2009-02-18 22:00:31 -05:00
|
|
|
{ hook initial: [ drop ] }
|
2009-02-17 09:24:55 -05:00
|
|
|
{ gap initial: 2 }
|
2009-01-05 18:31:21 -05:00
|
|
|
column-widths total-width
|
2009-02-15 05:01:57 -05:00
|
|
|
focus-border-color
|
2009-02-11 05:55:33 -05:00
|
|
|
{ mouse-color initial: COLOR: black }
|
2009-02-19 17:54:27 -05:00
|
|
|
column-line-color
|
2009-02-11 05:55:33 -05:00
|
|
|
selection-required?
|
2009-08-05 17:24:56 -04:00
|
|
|
selection
|
|
|
|
selection-index
|
|
|
|
selected-indices
|
2009-01-05 18:31:21 -05:00
|
|
|
mouse-index
|
2009-02-17 09:24:55 -05:00
|
|
|
{ takes-focus? initial: t }
|
2009-05-13 17:10:04 -04:00
|
|
|
focused?
|
|
|
|
multiple-selection? ;
|
|
|
|
|
2009-08-05 17:24:56 -04:00
|
|
|
<PRIVATE
|
2009-05-13 17:10:04 -04:00
|
|
|
|
2009-08-05 23:45:56 -04:00
|
|
|
: add-selected-index ( table n -- table )
|
|
|
|
over selected-indices>> conjoin ;
|
|
|
|
|
|
|
|
: multiple>single ( values -- value/f ? )
|
2009-08-05 23:56:08 -04:00
|
|
|
dup assoc-empty? [ drop f f ] [ values first t ] if ;
|
2009-08-05 23:45:56 -04:00
|
|
|
|
|
|
|
: selected-index ( table -- n )
|
|
|
|
selected-indices>> multiple>single drop ;
|
|
|
|
|
|
|
|
: set-selected-index ( table n -- table )
|
|
|
|
dup associate >>selected-indices ;
|
|
|
|
|
2009-08-05 17:24:56 -04:00
|
|
|
PRIVATE>
|
2009-08-05 23:45:56 -04:00
|
|
|
|
|
|
|
: selected ( table -- index/indices )
|
|
|
|
[ selected-indices>> ] [ multiple-selection?>> ] bi
|
|
|
|
[ multiple>single drop ] unless ;
|
2008-12-19 03:37:40 -05:00
|
|
|
|
2009-04-29 15:19:30 -04:00
|
|
|
: new-table ( rows renderer class -- table )
|
2009-04-30 22:37:53 -04:00
|
|
|
new-line-gadget
|
|
|
|
swap >>renderer
|
|
|
|
swap >>model
|
|
|
|
sans-serif-font >>font
|
|
|
|
focus-border-color >>focus-border-color
|
2009-08-05 17:24:56 -04:00
|
|
|
transparent >>column-line-color
|
|
|
|
f <model> >>selection-index
|
2009-08-05 23:45:56 -04:00
|
|
|
f <model> >>selection
|
|
|
|
H{ } clone >>selected-indices ;
|
2009-04-29 15:19:30 -04:00
|
|
|
|
|
|
|
: <table> ( rows renderer -- table ) table new-table ;
|
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 )
|
2009-04-11 15:14:32 -04:00
|
|
|
GENERIC: cell-padding ( cell -- y )
|
2009-02-11 05:55:33 -05:00
|
|
|
GENERIC: draw-cell ( font cell -- )
|
|
|
|
|
|
|
|
M: string cell-width text-width ;
|
2009-02-20 21:52:33 -05:00
|
|
|
M: string cell-height text-height ceiling ;
|
2009-04-11 15:14:32 -04:00
|
|
|
M: string cell-padding drop 0 ;
|
2009-02-11 05:55:33 -05:00
|
|
|
M: string draw-cell draw-text ;
|
|
|
|
|
2009-04-11 15:14:32 -04:00
|
|
|
CONSTANT: image-padding 2
|
|
|
|
|
2009-02-11 05:55:33 -05:00
|
|
|
M: image-name cell-width nip image-dim first ;
|
|
|
|
M: image-name cell-height nip image-dim second ;
|
2009-04-11 15:14:32 -04:00
|
|
|
M: image-name cell-padding drop image-padding ;
|
2009-02-11 05:55:33 -05:00
|
|
|
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 ;
|
|
|
|
|
2009-02-16 05:25:15 -05:00
|
|
|
CONSTANT: column-title-background COLOR: light-gray
|
|
|
|
|
|
|
|
: column-title-font ( font -- font' )
|
|
|
|
column-title-background font-with-background t >>bold? ;
|
2009-02-11 05:55:33 -05:00
|
|
|
|
2009-02-16 05:25:15 -05:00
|
|
|
: initial-widths ( table rows -- widths )
|
|
|
|
over renderer>> column-titles dup
|
|
|
|
[ [ drop font>> ] dip [ text-width ] with map ]
|
|
|
|
[ drop nip first length 0 <repetition> ]
|
|
|
|
if ;
|
|
|
|
|
|
|
|
: row-column-widths ( table row -- widths )
|
2009-04-11 15:14:32 -04:00
|
|
|
[ font>> ] dip [ [ cell-width ] [ cell-padding ] bi + ] with map ;
|
2009-02-16 05:25:15 -05:00
|
|
|
|
|
|
|
: compute-total-width ( gap widths -- total )
|
|
|
|
swap [ column-offsets drop ] keep - ;
|
2009-02-11 05:55:33 -05:00
|
|
|
|
2009-02-16 05:25:15 -05:00
|
|
|
: compute-column-widths ( table -- total widths )
|
|
|
|
dup table-rows [ drop 0 { } ] [
|
|
|
|
[ drop gap>> ] [ initial-widths ] [ ] 2tri
|
2009-02-11 05:55:33 -05:00
|
|
|
[ row-column-widths vmax ] with each
|
2009-02-16 05:25:15 -05:00
|
|
|
[ compute-total-width ] keep
|
2008-12-19 03:37:40 -05:00
|
|
|
] if-empty ;
|
|
|
|
|
|
|
|
: 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 ]
|
2009-02-16 05:25:15 -05:00
|
|
|
[ renderer>> filled-column ]
|
2009-01-05 18:31:21 -05:00
|
|
|
[ 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-03-27 19:31:25 -04:00
|
|
|
: row-bounds ( table row -- loc dim )
|
|
|
|
row-rect rect-bounds ; inline
|
2009-01-12 23:16:57 -05:00
|
|
|
|
2009-05-13 17:10:04 -04:00
|
|
|
: draw-selected-rows ( table -- )
|
2009-02-09 01:23:47 -05:00
|
|
|
{
|
2009-08-05 23:45:56 -04:00
|
|
|
{ [ dup selected-indices>> assoc-empty? ] [ drop ] }
|
2009-02-09 01:23:47 -05:00
|
|
|
[
|
2009-08-05 23:45:56 -04:00
|
|
|
[ selected-indices>> keys ] [ selection-color>> gl-color ] [ ] tri
|
2009-05-13 17:10:04 -04:00
|
|
|
[ swap row-bounds gl-fill-rect ] curry each
|
2009-02-09 01:23:47 -05:00
|
|
|
]
|
|
|
|
} cond ;
|
|
|
|
|
|
|
|
: draw-focused-row ( table -- )
|
|
|
|
{
|
|
|
|
{ [ dup focused?>> not ] [ drop ] }
|
2009-08-05 17:24:56 -04:00
|
|
|
{ [ dup selected-index not ] [ drop ] }
|
2009-02-09 01:23:47 -05:00
|
|
|
[
|
2009-08-05 17:24:56 -04:00
|
|
|
[ ] [ selected-index ] [ focus-border-color>> gl-color ] tri
|
2009-03-27 19:31:25 -04:00
|
|
|
row-bounds gl-rect
|
2009-02-09 01:23:47 -05:00
|
|
|
]
|
|
|
|
} cond ;
|
|
|
|
|
|
|
|
: draw-moused-row ( table -- )
|
2009-01-12 23:16:57 -05:00
|
|
|
dup mouse-index>> dup [
|
2009-03-27 19:31:25 -04:00
|
|
|
over mouse-color>> gl-color
|
|
|
|
row-bounds gl-rect
|
2009-01-12 23:16:57 -05:00
|
|
|
] [ 2drop ] if ;
|
2008-12-19 03:37:40 -05:00
|
|
|
|
2009-02-16 02:10:21 -05:00
|
|
|
: column-line-offsets ( table -- xs )
|
|
|
|
[ column-widths>> ] [ gap>> ] bi
|
2009-02-11 05:55:33 -05:00
|
|
|
[ 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-16 02:10:21 -05:00
|
|
|
[ column-line-offsets ] [ dim>> second ] bi
|
2009-01-08 01:04:44 -05:00
|
|
|
'[ [ 0 2array ] [ _ 2array ] bi gl-line ] each
|
|
|
|
] bi ;
|
|
|
|
|
2009-04-11 15:14:32 -04:00
|
|
|
:: column-loc ( font column width align -- loc )
|
|
|
|
font column cell-width width swap - align * column cell-padding 2 / 1 align - * +
|
|
|
|
font column cell-height \ line-height get swap - 2 /
|
|
|
|
[ >integer ] bi@ 2array ;
|
2009-01-05 18:31:21 -05:00
|
|
|
|
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-16 05:25:15 -05:00
|
|
|
: table-column-alignment ( table -- seq )
|
|
|
|
dup renderer>> column-alignment
|
2009-02-05 23:14:35 -05:00
|
|
|
[ ] [ column-widths>> length 0 <repetition> ] ?if ;
|
|
|
|
|
2009-05-13 17:10:04 -04:00
|
|
|
:: row-font ( row ind table -- font )
|
2009-02-07 19:09:50 -05:00
|
|
|
table font>> clone
|
|
|
|
row table renderer>> row-color [ >>foreground ] when*
|
2009-08-05 23:45:56 -04:00
|
|
|
ind table selected-indices>> key?
|
|
|
|
[ table selection-color>> >>background ] when ;
|
2008-12-19 03:37:40 -05:00
|
|
|
|
2009-02-16 05:25:15 -05:00
|
|
|
: draw-columns ( columns widths alignment font gap -- )
|
|
|
|
'[ [ _ ] 3dip _ draw-column ] 3each ;
|
|
|
|
|
2009-02-07 19:09:50 -05:00
|
|
|
M: table draw-line ( row index table -- )
|
|
|
|
[
|
|
|
|
nip
|
|
|
|
[ renderer>> row-columns ]
|
|
|
|
[ column-widths>> ]
|
2009-02-16 05:25:15 -05:00
|
|
|
[ table-column-alignment ]
|
2009-02-07 19:09:50 -05:00
|
|
|
tri
|
2009-02-11 05:55:33 -05:00
|
|
|
]
|
|
|
|
[ row-font ]
|
|
|
|
[ 2nip gap>> ] 3tri
|
2009-02-16 05:25:15 -05:00
|
|
|
draw-columns ;
|
2008-12-19 03:37:40 -05:00
|
|
|
|
|
|
|
M: table draw-gadget*
|
|
|
|
dup control-value empty? [ drop ] [
|
2009-04-11 15:14:32 -04:00
|
|
|
dup line-height \ line-height [
|
|
|
|
{
|
2009-05-13 17:10:04 -04:00
|
|
|
[ draw-selected-rows ]
|
2009-04-11 15:14:32 -04:00
|
|
|
[ draw-lines ]
|
|
|
|
[ draw-column-lines ]
|
|
|
|
[ draw-focused-row ]
|
|
|
|
[ draw-moused-row ]
|
|
|
|
} cleave
|
|
|
|
] with-variable
|
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
|
2009-04-11 15:14:32 -04:00
|
|
|
[ [ cell-height ] [ cell-padding ] bi + ] with
|
|
|
|
[ max ] map-reduce ;
|
2009-02-11 05:55:33 -05:00
|
|
|
|
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>
|
|
|
|
|
2009-08-05 23:56:08 -04:00
|
|
|
: (selected-rows) ( table -- assoc )
|
2009-05-13 17:10:04 -04:00
|
|
|
[ selected-indices>> ] keep
|
2009-08-05 23:56:08 -04:00
|
|
|
'[ _ nth-row drop ] assoc-map ;
|
2009-05-13 17:10:04 -04:00
|
|
|
|
2009-08-05 23:56:08 -04:00
|
|
|
: selected-rows ( table -- assoc )
|
|
|
|
[ selected-indices>> ] [ ] [ renderer>> ] tri
|
|
|
|
'[ _ nth-row drop _ row-value ] assoc-map ;
|
2009-05-13 17:10:04 -04:00
|
|
|
|
2009-05-13 21:03:22 -04:00
|
|
|
: (selected-row) ( table -- value/f ? ) (selected-rows) multiple>single ;
|
2009-08-05 23:45:56 -04:00
|
|
|
|
2009-05-13 21:03:22 -04:00
|
|
|
: selected-row ( table -- value/f ? ) selected-rows multiple>single ;
|
2009-01-08 18:02:54 -05:00
|
|
|
|
|
|
|
<PRIVATE
|
2008-12-19 03:37:40 -05:00
|
|
|
|
2009-08-05 22:29:48 -04:00
|
|
|
: set-table-model ( model value multiple? -- )
|
2009-08-06 17:31:35 -04:00
|
|
|
[ values ] [ multiple>single drop ] if swap set-model ;
|
2009-08-05 17:24:56 -04:00
|
|
|
|
|
|
|
: update-selected ( table -- )
|
2009-08-05 22:29:48 -04:00
|
|
|
[
|
2009-08-05 23:45:56 -04:00
|
|
|
[ selection>> ]
|
|
|
|
[ selected-rows ]
|
|
|
|
[ multiple-selection?>> ] tri
|
|
|
|
set-table-model
|
|
|
|
]
|
|
|
|
[
|
|
|
|
[ selection-index>> ]
|
|
|
|
[ selected-indices>> ]
|
|
|
|
[ multiple-selection?>> ] tri
|
2009-08-05 22:29:48 -04:00
|
|
|
set-table-model
|
|
|
|
] bi ;
|
2008-12-19 03:37:40 -05:00
|
|
|
|
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 ;
|
|
|
|
|
2009-02-20 21:52:33 -05:00
|
|
|
: hide-mouse-help ( table -- )
|
|
|
|
f >>mouse-index [ hide-status ] [ relayout-1 ] bi ;
|
|
|
|
|
2009-04-24 02:14:02 -04:00
|
|
|
: find-row-index ( value table -- n/f )
|
2009-08-06 02:28:30 -04:00
|
|
|
[ model>> value>> ] [ renderer>> ] bi
|
|
|
|
'[ _ row-value eq? ] with find drop ;
|
2009-04-24 02:14:02 -04:00
|
|
|
|
2009-08-05 23:45:56 -04:00
|
|
|
: (update-selected-indices) ( table -- set )
|
2009-08-06 02:28:30 -04:00
|
|
|
[ selection>> value>> dup [ array? not ] [ ] bi and [ 1array ] when ] keep
|
|
|
|
'[ _ find-row-index ] map sift unique f assoc-like ;
|
2009-04-24 02:14:02 -04:00
|
|
|
|
2009-08-05 23:45:56 -04:00
|
|
|
: initial-selected-indices ( table -- set )
|
2009-04-24 02:14:02 -04:00
|
|
|
{
|
|
|
|
[ model>> value>> empty? not ]
|
|
|
|
[ selection-required?>> ]
|
2009-08-05 23:45:56 -04:00
|
|
|
[ drop { 0 } unique ]
|
2009-04-24 02:14:02 -04:00
|
|
|
} 1&& ;
|
|
|
|
|
2009-08-05 23:56:08 -04:00
|
|
|
: update-selected-indices ( table -- set )
|
2009-04-24 02:14:02 -04:00
|
|
|
{
|
2009-05-13 17:10:04 -04:00
|
|
|
[ (update-selected-indices) ]
|
|
|
|
[ initial-selected-indices ]
|
2009-04-24 02:14:02 -04:00
|
|
|
} 1|| ;
|
|
|
|
|
2008-12-19 03:37:40 -05:00
|
|
|
M: table model-changed
|
2009-08-05 23:45:56 -04:00
|
|
|
nip dup update-selected-indices {
|
2009-05-13 17:10:04 -04:00
|
|
|
[ >>selected-indices f >>mouse-index drop ]
|
2009-08-05 23:56:08 -04:00
|
|
|
[ multiple>single drop show-row-summary ]
|
2009-08-05 17:24:56 -04:00
|
|
|
[ drop update-selected ]
|
2009-01-15 01:52:05 -05:00
|
|
|
[ 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-05-13 17:10:04 -04:00
|
|
|
: scroll-to-row ( table n -- )
|
|
|
|
dup [ [ thin-row-rect ] [ drop ] 2bi scroll>rect ] [ 2drop ] if ;
|
|
|
|
|
|
|
|
: add-selected-row ( table n -- )
|
|
|
|
[ scroll-to-row ]
|
2009-08-05 23:45:56 -04:00
|
|
|
[ add-selected-index relayout-1 ] 2bi ;
|
2009-05-13 17:10:04 -04:00
|
|
|
|
2009-01-09 18:58:22 -05:00
|
|
|
: (select-row) ( table n -- )
|
2009-05-13 17:10:04 -04:00
|
|
|
[ scroll-to-row ]
|
2009-08-05 17:24:56 -04:00
|
|
|
[ set-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-05-13 17:10:04 -04:00
|
|
|
: if-mouse-row ( table true: ( mouse-index table -- ) false: ( table -- ) -- )
|
2009-04-03 20:30:07 -04:00
|
|
|
[ [ mouse-row ] keep 2dup valid-line? ]
|
|
|
|
[ ] [ '[ nip @ ] ] tri* if ; inline
|
|
|
|
|
2009-05-13 17:10:04 -04:00
|
|
|
: (table-button-down) ( quot table -- )
|
|
|
|
dup takes-focus?>> [ dup request-focus ] when swap
|
|
|
|
'[ swap [ >>mouse-index ] _ bi ] [ drop ] if-mouse-row ; inline
|
|
|
|
|
2009-08-05 23:45:56 -04:00
|
|
|
: table-button-down ( table -- )
|
|
|
|
[ (select-row) ] swap (table-button-down) ;
|
|
|
|
|
|
|
|
: continued-button-down ( table -- )
|
|
|
|
dup multiple-selection?>>
|
2009-08-05 22:29:48 -04:00
|
|
|
[ [ add-selected-row ] swap (table-button-down) ] [ table-button-down ] if ;
|
2009-08-05 23:45:56 -04:00
|
|
|
|
|
|
|
: thru-button-down ( table -- )
|
|
|
|
dup multiple-selection?>> [
|
2009-08-05 22:29:48 -04:00
|
|
|
[ 2dup over selected-index (a,b) swap
|
2009-08-05 23:45:56 -04:00
|
|
|
[ swap add-selected-index drop ] curry each add-selected-row ]
|
2009-08-05 22:29:48 -04:00
|
|
|
swap (table-button-down)
|
|
|
|
] [ table-button-down ] 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
|
|
|
: row-action ( table -- )
|
2009-05-13 23:15:33 -04:00
|
|
|
dup selected-row
|
2009-03-27 19:31:25 -04:00
|
|
|
[ swap [ action>> call( value -- ) ] [ dup hook>> call( table -- ) ] bi ]
|
2009-02-09 01:23:47 -05:00
|
|
|
[ 2drop ]
|
|
|
|
if ;
|
2009-01-06 17:52:12 -05:00
|
|
|
|
2009-04-03 20:30:07 -04:00
|
|
|
: row-action? ( table -- ? )
|
2009-07-07 00:19:26 -04:00
|
|
|
single-click?>> hand-click# get 2 = or ;
|
2009-04-03 20:30:07 -04:00
|
|
|
|
2009-01-13 17:22:07 -05:00
|
|
|
<PRIVATE
|
|
|
|
|
2009-01-06 17:52:12 -05:00
|
|
|
: table-button-up ( table -- )
|
2009-07-07 00:19:26 -04:00
|
|
|
dup [ mouse-row ] keep valid-line? [
|
2009-08-05 17:24:56 -04:00
|
|
|
dup row-action? [ row-action ] [ update-selected ] if
|
2009-07-07 00:19:26 -04:00
|
|
|
] [ drop ] if ;
|
2008-12-19 03:37:40 -05:00
|
|
|
|
2009-04-24 02:14:02 -04:00
|
|
|
PRIVATE>
|
|
|
|
|
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) ]
|
2009-08-05 17:24:56 -04:00
|
|
|
[ drop update-selected ]
|
2009-01-15 01:52:05 -05:00
|
|
|
[ show-row-summary ]
|
|
|
|
2tri ;
|
2008-12-19 03:37:40 -05:00
|
|
|
|
2009-04-24 02:14:02 -04:00
|
|
|
<PRIVATE
|
|
|
|
|
2009-01-22 02:19:15 -05:00
|
|
|
: prev/next-row ( table n -- )
|
2009-08-05 17:24:56 -04:00
|
|
|
[ dup selected-index ] dip '[ _ + ] [ 0 ] if* select-row ;
|
2009-01-22 02:19:15 -05:00
|
|
|
|
2009-02-16 02:10:21 -05:00
|
|
|
: previous-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 ;
|
|
|
|
|
2009-02-16 02:10:21 -05:00
|
|
|
: prev/next-page ( table n -- )
|
2009-02-17 07:10:02 -05:00
|
|
|
over visible-lines 1- * prev/next-row ;
|
2009-02-16 02:10:21 -05:00
|
|
|
|
|
|
|
: previous-page ( table -- )
|
|
|
|
-1 prev/next-page ;
|
|
|
|
|
|
|
|
: next-page ( table -- )
|
|
|
|
1 prev/next-page ;
|
|
|
|
|
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 ]
|
2009-02-18 22:00:31 -05:00
|
|
|
[ swap select-row ]
|
|
|
|
[
|
|
|
|
[ nth-row drop ]
|
|
|
|
[ renderer>> row-value ]
|
|
|
|
[ dup hook>> curry ]
|
|
|
|
tri
|
|
|
|
] 2tri
|
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
|
|
|
|
2009-04-24 02:14:02 -04:00
|
|
|
: focus-table ( table -- ) t >>focused? relayout-1 ;
|
2009-02-16 02:10:21 -05:00
|
|
|
|
2009-04-24 02:14:02 -04:00
|
|
|
: unfocus-table ( table -- ) f >>focused? relayout-1 ;
|
2009-02-16 02:10:21 -05:00
|
|
|
|
|
|
|
table "sundry" f {
|
|
|
|
{ mouse-enter show-mouse-help }
|
|
|
|
{ mouse-leave hide-mouse-help }
|
|
|
|
{ motion show-mouse-help }
|
2009-05-14 13:38:43 -04:00
|
|
|
{ T{ button-down f { S+ } 1 } thru-button-down }
|
2009-05-13 17:10:04 -04:00
|
|
|
{ T{ button-down f { A+ } 1 } continued-button-down }
|
2009-02-16 02:10:21 -05:00
|
|
|
{ T{ button-up } table-button-up }
|
2009-05-14 13:38:43 -04:00
|
|
|
{ T{ button-up f { S+ } } table-button-up }
|
2009-05-14 11:01:37 -04:00
|
|
|
{ T{ button-down } table-button-down }
|
2009-02-16 02:10:21 -05:00
|
|
|
{ gain-focus focus-table }
|
|
|
|
{ lose-focus unfocus-table }
|
|
|
|
{ T{ drag } table-button-down }
|
|
|
|
} define-command-map
|
|
|
|
|
|
|
|
table "row" f {
|
|
|
|
{ T{ button-down f f 3 } show-table-menu }
|
|
|
|
{ T{ key-down f f "RET" } row-action }
|
|
|
|
{ T{ key-down f f "UP" } previous-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 }
|
|
|
|
{ T{ key-down f f "PAGE_UP" } previous-page }
|
|
|
|
{ T{ key-down f f "PAGE_DOWN" } next-page }
|
|
|
|
} define-command-map
|
2009-01-05 18:31:21 -05:00
|
|
|
|
2009-02-16 05:25:15 -05:00
|
|
|
TUPLE: column-headers < gadget table ;
|
|
|
|
|
|
|
|
: <column-headers> ( table -- gadget )
|
|
|
|
column-headers new
|
|
|
|
swap >>table
|
|
|
|
column-title-background <solid> >>interior ;
|
|
|
|
|
|
|
|
: draw-column-titles ( table -- )
|
2009-04-11 15:14:32 -04:00
|
|
|
dup font>> font-metrics height>> \ line-height [
|
|
|
|
{
|
|
|
|
[ renderer>> column-titles ]
|
|
|
|
[ column-widths>> ]
|
|
|
|
[ table-column-alignment ]
|
|
|
|
[ font>> column-title-font ]
|
|
|
|
[ gap>> ]
|
|
|
|
} cleave
|
|
|
|
draw-columns
|
|
|
|
] with-variable ;
|
2009-02-16 05:25:15 -05:00
|
|
|
|
|
|
|
M: column-headers draw-gadget*
|
|
|
|
table>> draw-column-titles ;
|
|
|
|
|
|
|
|
M: column-headers pref-dim*
|
|
|
|
table>> [ pref-dim first ] [ font>> "" text-height ] bi 2array ;
|
|
|
|
|
|
|
|
M: table viewport-column-header
|
|
|
|
dup renderer>> column-titles
|
|
|
|
[ <column-headers> ] [ drop f ] if ;
|
|
|
|
|
2009-08-06 17:46:48 -04:00
|
|
|
PRIVATE>
|