source: trunk/tester/scripts/stringdiffs.tcl @ 4503

Last change on this file since 4503 was 3177, checked in by mmc, 8 years ago

Updated all of the copyright notices to reference the transfer to
the new HUBzero Foundation, LLC.

File size: 9.3 KB
Line 
1# ----------------------------------------------------------------------
2#  COMPONENT: stringdiffs - show diff info between two strings
3#
4#  This component is used when examining the details of a particular
5#  diff.  It shows two strings with either inline or side-by-side
6#  diffs.
7# ======================================================================
8#  AUTHOR:  Michael McLennan, Purdue University
9#  Copyright (c) 2004-2012  HUBzero Foundation, LLC
10#
11#  See the file "license.terms" for information on usage and
12#  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
13# ======================================================================
14package require Itk
15package require RapptureGUI
16
17namespace eval Rappture::Tester { # forward declaration }
18
19option add *StringDiffs.titleFont {Arial -12 bold} widgetDefault
20option add *StringDiffs.bodyFont {Courier -12} widgetDefault
21option add *StringDiffs.bodyBackground white widgetDefault
22
23itcl::class Rappture::Tester::StringDiffs {
24    inherit itk::Widget
25
26    itk_option define -title title Title ""
27    itk_option define -title1 title1 Title1 ""
28    itk_option define -title2 title2 Title2 ""
29
30    constructor {args} { # defined later }
31    method show {v1 v2}
32
33    protected method _yview {args}
34    protected method _ysbar {args}
35}
36
37# ----------------------------------------------------------------------
38# CONSTRUCTOR
39# ----------------------------------------------------------------------
40itcl::body Rappture::Tester::StringDiffs::constructor {args} {
41    # viewer for inline diffs
42    itk_component add inline {
43        frame $itk_interior.inline
44    }
45
46    itk_component add title {
47        label $itk_component(inline).title -justify left -anchor w
48    } {
49        usual
50        rename -font -titlefont titleFont Font
51    }
52    pack $itk_component(title) -side top -fill x
53
54    itk_component add scrl {
55        Rappture::Scroller $itk_component(inline).scrl \
56            -xscrollmode auto -yscrollmode auto
57    }
58    pack $itk_component(scrl) -expand yes -fill both
59
60    itk_component add body {
61        Rappture::Diffview $itk_component(scrl).diffs \
62            -highlightthickness 0 \
63            -diff 1->2 -layout inline
64    } {
65        keep -foreground -cursor
66        keep -addedbackground -addedforeground
67        keep -deletedbackground -deletedforeground -overstrike
68        keep -changedbackground -changedforeground
69        rename -background -bodybackground bodyBackground Background
70        rename -font -bodyfont bodyFont Font
71    }
72    $itk_component(scrl) contents $itk_component(body)
73
74    # viewer for side-by-side diffs
75    itk_component add sidebyside {
76        frame $itk_interior.sbys
77    }
78
79    itk_component add title1 {
80        label $itk_component(sidebyside).title1 -justify left -anchor w
81    } {
82        usual
83        rename -font -titlefont titleFont Font
84    }
85    itk_component add body1 {
86        Rappture::Diffview $itk_component(sidebyside).s1 \
87            -highlightthickness 0 \
88            -diff 2->1 -layout sidebyside
89    } {
90        keep -background -foreground -cursor
91        keep -addedbackground -addedforeground
92        keep -deletedbackground -deletedforeground -overstrike
93        keep -changedbackground -changedforeground
94        rename -background -bodybackground bodyBackground Background
95        rename -font -bodyfont bodyFont Font
96    }
97    itk_component add xsbar1 {
98        scrollbar $itk_component(sidebyside).xsbar1 -orient horizontal \
99            -command [list $itk_component(body1) xview]
100    }
101
102    itk_component add title2 {
103        label $itk_component(sidebyside).title2 -justify left -anchor w
104    } {
105        usual
106        rename -font -titlefont titleFont Font
107    }
108    itk_component add body2 {
109        Rappture::Diffview $itk_component(sidebyside).s2 \
110            -highlightthickness 0 \
111            -diff 1->2 -layout sidebyside
112    } {
113        keep -background -foreground -cursor
114        keep -addedbackground -addedforeground
115        keep -deletedbackground -deletedforeground -overstrike
116        keep -changedbackground -changedforeground
117        rename -background -bodybackground bodyBackground Background
118        rename -font -bodyfont bodyFont Font
119    }
120    itk_component add xsbar2 {
121        scrollbar $itk_component(sidebyside).xsbar2 -orient horizontal \
122            -command [list $itk_component(body2) xview]
123    }
124
125    itk_component add ysbar {
126        scrollbar $itk_component(sidebyside).ysbar -orient vertical \
127            -command [itcl::code $this _yview]
128    }
129    $itk_component(body1) configure \
130        -xscrollcommand [list $itk_component(xsbar1) set] \
131        -yscrollcommand [itcl::code $this _ysbar body1]
132    $itk_component(body2) configure \
133        -xscrollcommand [list $itk_component(xsbar2) set] \
134        -yscrollcommand [itcl::code $this _ysbar body2]
135
136    grid $itk_component(title1) -row 0 -column 0 -sticky nsew
137    grid $itk_component(body1) -row 1 -column 0 -sticky nsew
138    grid $itk_component(xsbar1) -row 2 -column 0 -sticky ew
139    grid $itk_component(ysbar) -row 1 -column 1 -sticky ns
140    grid $itk_component(title2) -row 0 -column 2 -sticky nsew
141    grid $itk_component(body2) -row 1 -column 2 -sticky nsew
142    grid $itk_component(xsbar2) -row 2 -column 2 -sticky ew
143    grid columnconfigure $itk_component(sidebyside) 0 -weight 1
144    grid columnconfigure $itk_component(sidebyside) 2 -weight 1
145    grid rowconfigure $itk_component(sidebyside) 1 -weight 1
146
147    eval itk_initialize $args
148}
149
150itk::usual StringDiffs {
151    keep -background -foreground -cursor
152    keep -titlefont -bodyfont
153}
154
155# ----------------------------------------------------------------------
156# USAGE: show <v1> <v2>
157#
158# Loads two values into the viewer and shows their differences.
159# If the strings are short, the diffs are shown inline.  Otherwise,
160# they are show with side-by-side viewers.
161# ----------------------------------------------------------------------
162itcl::body Rappture::Tester::StringDiffs::show {v1 v2} {
163    #
164    # HACK ALERT!  The Diffview widget doesn't handle tabs at all.
165    #   We'll convert all tabs to a few spaces until we get a chance
166    #   to fix it properly.
167    #
168    set v1 [string map [list \t "   "] $v1]
169    set v2 [string map [list \t "   "] $v2]
170
171    #
172    # Figure out whether to show inline diffs or side-by-side.
173    # If the strings are short, it looks better inline.  Otherwise,
174    # use side-by-side.
175    #
176    foreach w [pack slaves $itk_interior] {
177        pack forget $w
178    }
179    if {[llength [split $v1 \n]] > 10 || [llength [split $v2 \n]] > 10} {
180        set which "sidebyside"
181    } else {
182        set which "inline"
183    }
184    pack $itk_component($which) -expand yes -fill both
185
186    switch -- $which {
187        inline {
188            $itk_component(body) text 1 $v1
189            $itk_component(body) text 2 $v2
190            $itk_component(body1) text 1 ""
191            $itk_component(body1) text 2 ""
192            $itk_component(body2) text 1 ""
193            $itk_component(body2) text 2 ""
194        }
195        sidebyside {
196            $itk_component(body1) text 1 $v1
197            $itk_component(body1) text 2 $v2
198            $itk_component(body2) text 1 $v1
199            $itk_component(body2) text 2 $v2
200            $itk_component(body) text 1 ""
201            $itk_component(body) text 2 ""
202        }
203    }
204}
205
206# ----------------------------------------------------------------------
207# USAGE: _yview <arg> <arg>...
208#
209# Called whenever the scrollbar changes the y-view of the diffs.
210# Sends the new command along to both views so they are aligned.
211# ----------------------------------------------------------------------
212itcl::body Rappture::Tester::StringDiffs::_yview {args} {
213    eval $itk_component(body1) yview $args
214    eval $itk_component(body2) yview $args
215}
216
217# ----------------------------------------------------------------------
218# USAGE: _ysbar <whichChanged> <arg> <arg>...
219#
220# Called whenever the y-view of one widget changes.  Copies the
221# current view from the <whichChanged> widget to the other side,
222# and updates the bubble to display the correct view.
223# ----------------------------------------------------------------------
224itcl::body Rappture::Tester::StringDiffs::_ysbar {which args} {
225    switch -- $which {
226        body1 {
227            set pos [lindex [$itk_component(body1) yview] 0]
228            $itk_component(body2) yview moveto $pos
229        }
230        body2 {
231            set pos [lindex [$itk_component(body2) yview] 0]
232            $itk_component(body1) yview moveto $pos
233        }
234    }
235    eval $itk_component(ysbar) set $args
236}
237
238# ----------------------------------------------------------------------
239# CONFIGURATION OPTIONS: -title -title1 -title2
240# ----------------------------------------------------------------------
241itcl::configbody Rappture::Tester::StringDiffs::title {
242    # if there is no -title for inline diffs, use -title1
243    if {$itk_option(-title) eq ""} {
244        $itk_component(title) configure -text $itk_option(-title1)
245    } else {
246        $itk_component(title) configure -text $itk_option(-title)
247    }
248}
249
250itcl::configbody Rappture::Tester::StringDiffs::title1 {
251    # if there is no -title for inline diffs, use -title1
252    if {$itk_option(-title) eq ""} {
253        $itk_component(title) configure -text $itk_option(-title1)
254    }
255    $itk_component(title1) configure -text $itk_option(-title1)
256}
257
258itcl::configbody Rappture::Tester::StringDiffs::title2 {
259    $itk_component(title2) configure -text $itk_option(-title2)
260}
Note: See TracBrowser for help on using the repository browser.