아래의 스크립트는 텍스트 위젯에 라인넘버 기능을 넣고, wrap 된 라인에 wrap이미지를 보여주는 스크립트입니다. 간단한 텍스트 편집기를 만들고자 하시는 분들께 도움이 되리라 생각이 듭니다.
package require Tk 8.5
image create bitmap wrap_bit -foreground #aac -data {
#define wrap_width 8
#define wrap_height 8
static unsigned char wrap_bits[] = {
0x02, 0x02, 0x02, 0x22, 0x62, 0xfc, 0x60, 0x20
};
}
proc main {} {
text .text \
-wrap word \
-borderwidth 0 \
-yscrollcommand [list .vsb set]
canvas .canvas \
-width 20 \
-highlightthickness 0 \
-background white
scrollbar .vsb \
-borderwidth 1 \
-command [list .text yview]
pack .vsb -side right -fill y
pack .canvas -side left -fill y
pack .text -side left -fill both -expand true
# Arrange for line numbers to be redrawn when just about anything
# happens to the text widget. This runs much faster than you might
# think.
trace add execution .text leave [list traceCallback .text .canvas]
bind .text <Configure> [list traceCallback .text .canvas]
set f [open [info script] r]
set data [read $f]
close $f
.text insert end $data
}
proc traceCallback {text canvas args} {
if {[lindex $args 0 1] in {insert delete mark}} {
scan [$text index insert] %d.%d ::line ::column
puts "L: $::line C: $::column"
}
# only redraw if args are null (meaning we were called by a binding)
# or called by the trace and the command could potentially change
# the size of a line.
set benign {
mark bbox cget compare count debug dlineinfo
dump get index mark peer search
}
if {[llength $args] == 0 ||
[lindex $args 0 1] ni $benign} {
$canvas delete all
set i [$text index @0,0]
while true {
set dline [$text dlineinfo $i]
if {[llength $dline] == 0} break
set height [lindex $dline 3]
set y [lindex $dline 1]
set cy [expr {$y + int($height/2.0)}]
set linenum [lindex [split $i .] 0]
$canvas create text 0 $y -anchor nw -text $linenum
if {[$text count -ypixels $i "$i lineend"]} {
$canvas create image 0 [expr {$y + [lindex $dline 4] + 8}] -image wrap_bit -anchor w
}
set i [$text index "$i + 1 line"]
}
}
}
main
'Tcl & Tk > 팁 (Tip)' 카테고리의 다른 글
그라디언트 구현하기 (0) | 2025.03.26 |
---|---|
Tcl로 구현한 텔넷 서버 (0) | 2025.03.26 |
popen 구현 하기 (0) | 2025.03.26 |
C로 만드는 base64 디코드 커맨드 (0) | 2025.03.26 |
try/catch/finally 커맨드 (0) | 2025.03.26 |