55# - Namespace initialization
66# - Public utility procedures
77#
8- # Copyright (c) 2000-2020 Csaba Nemethi (E-mail: [email protected] )8+ # Copyright (c) 2000-2021 Csaba Nemethi (E-mail: [email protected] )99# ==============================================================================
1010
1111package require Tk 8
@@ -19,7 +19,7 @@ namespace eval mwutil {
1919 #
2020 # Public variables:
2121 #
22- variable version 2.17
22+ variable version 2.18
2323 variable library
2424 if {$::tcl_version >= 8.4} {
2525 set library [file dirname [file normalize [info script]]]
@@ -35,8 +35,8 @@ namespace eval mwutil {
3535 configureWidget fullConfigOpt fullOpt enumOpts \
3636 configureSubCmd attribSubCmd hasattribSubCmd \
3737 unsetattribSubCmd getScrollInfo getScrollInfo2 \
38- isScrollable hasFocus genMouseWheelEvent \
39- windowingSystem currentTheme
38+ isScrollable scrollByUnits genMouseWheelEvent \
39+ hasFocus windowingSystem currentTheme
4040
4141 #
4242 # Make modified versions of the procedures tk_focusNext and
@@ -507,7 +507,9 @@ proc mwutil::getScrollInfo argList {
507507 wrongNumArgs " scroll number units|pages"
508508 }
509509
510- set number [format " %d" [lindex $argList 1]]
510+ set number [lindex $argList 1]
511+ format " %f" $number ;# floating-point number check with error message
512+ set number [expr {int($number > 0 ? ceil($number ) : floor($number ))}]
511513 set what [lindex $argList 2]
512514 if {[string first $what " units" ] == 0} {
513515 return [list scroll $number units]
@@ -545,7 +547,9 @@ proc mwutil::getScrollInfo2 {cmd argList} {
545547 wrongNumArgs " $cmd scroll number units|pages"
546548 }
547549
548- set number [format " %d" [lindex $argList 1]]
550+ set number [lindex $argList 1]
551+ format " %f" $number ;# floating-point number check with error message
552+ set number [expr {int($number > 0 ? ceil($number ) : floor($number ))}]
549553 set what [lindex $argList 2]
550554 if {[string first $what " units" ] == 0} {
551555 return [list scroll $number units]
@@ -577,21 +581,16 @@ proc mwutil::isScrollable {w axis} {
577581}
578582
579583# ------------------------------------------------------------------------------
580- # mwutil::hasFocus
584+ # mwutil::scrollByUnits
581585#
582- # Returns a boolean value indicating whether the focus window is (a descendant
583- # of) the widget w and has the same toplevel.
584- # ------------------------------------------------------------------------------
585- proc mwutil::hasFocus w {
586- set focusWin [focus -displayof $w ]
587- if {[string length $focusWin ] == 0} {
588- return 0
589- }
590-
591- return [expr {
592- ([string compare $w " ." ] == 0 || [string first $w . $focusWin .] == 0) &&
593- [string compare [winfo toplevel $w ] [winfo toplevel $focusWin ]] == 0
594- }]
586+ # Scrolls the widget w along a given axis (x or y) by units. The number of
587+ # units is obtained by converting the fraction built from the last two
588+ # arguments to an integer, rounded away from 0.
589+ # ------------------------------------------------------------------------------
590+ proc mwutil::scrollByUnits {w axis delta divisor} {
591+ set number [expr {$delta /$divisor }]
592+ set number [expr {int($number > 0 ? ceil($number ) : floor($number ))}]
593+ $w ${axis} view scroll $number units
595594}
596595
597596# ------------------------------------------------------------------------------
@@ -617,6 +616,24 @@ proc mwutil::genMouseWheelEvent {w event rootX rootY delta} {
617616 }
618617}
619618
619+ # ------------------------------------------------------------------------------
620+ # mwutil::hasFocus
621+ #
622+ # Returns a boolean value indicating whether the focus window is (a descendant
623+ # of) the widget w and has the same toplevel.
624+ # ------------------------------------------------------------------------------
625+ proc mwutil::hasFocus w {
626+ set focusWin [focus -displayof $w ]
627+ if {[string length $focusWin ] == 0} {
628+ return 0
629+ }
630+
631+ return [expr {
632+ ([string compare $w " ." ] == 0 || [string first $w . $focusWin .] == 0) &&
633+ [string compare [winfo toplevel $w ] [winfo toplevel $focusWin ]] == 0
634+ }]
635+ }
636+
620637# ------------------------------------------------------------------------------
621638# mwutil::windowingSystem
622639#
0 commit comments