File: pagesman.tcl

package info (click to toggle)
tklib 0.9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 23,156 kB
  • sloc: tcl: 105,088; sh: 2,573; ansic: 792; pascal: 359; makefile: 69; sed: 53; exp: 21
file content (851 lines) | stat: -rw-r--r-- 23,804 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
#==============================================================================
# Contains the implementation of the pagesman widget.
#
# Structure of the module:
#   - Namespace initialization
#   - Private procedure creating the default bindings
#   - Public procedure creating a new pagesman widget
#   - Private configuration procedures
#   - Private procedures implementing the pagesman widget command
#   - Private procedures used in bindings
#
# Copyright (c) 2021-2023  Csaba Nemethi (E-mail: csaba.nemethi@t-online.de)
#==============================================================================

#
# Namespace initialization
# ========================
#

namespace eval scrollutil::pm {
    #
    # The array configSpecs is used to handle configuration options.  The names
    # of its elements are the configuration options for the Pagesman class.
    # The value of an array element is either an alias name or a list
    # containing the database name and class as well as an indicator specifying
    # the widget to which the option applies: f stands for the frame and w for
    # the pagesman widget itself.
    #
    #	Command-Line Name	{Database Name		Database Class       W}
    #	-----------------------------------------------------------------------
    #
    variable configSpecs
    array set configSpecs {
	-background		{background		Background	     f}
	-bg			-background
	-borderwidth		{borderWidth		BorderWidth	     f}
	-bd			-borderwidth
	-cursor			{cursor			Cursor		     f}
	-forgetcommand		{forgetCommand		ForgetCommand	     w}
	-height			{height			Height		     f}
	-highlightbackground	{highlightBackground	HighlightBackground  f}
	-highlightcolor		{highlightColor		HighlightColor	     f}
	-highlightthickness	{highlightThickness	HighlightThickness   f}
	-leavecommand		{leaveCommand		LeaveCommand	     w}
	-relief			{relief			Relief		     f}
	-takefocus		{takeFocus		TakeFocus	     f}
	-width			{width			Width		     f}
    }

    #
    # Extend the elements of the array configSpecs
    #
    proc extendConfigSpecs {} {
	variable ::scrollutil::usingTile
	variable configSpecs

	if {$usingTile} {
	    foreach opt {-background -bg -highlightbackground -highlightcolor
			 -highlightthickness} {
		unset configSpecs($opt)
	    }
	} else {
	    set helpFrm .__helpFrm
	    for {set n 2} {[winfo exists $helpFrm]} {incr n} {
		set helpFrm .__helpFrm$n
	    }
	    tk::frame $helpFrm
	    foreach opt {-background -highlightbackground -highlightcolor
			 -highlightthickness} {
		set configSet [$helpFrm configure $opt]
		lappend configSpecs($opt) [lindex $configSet 3]
	    }
	    destroy $helpFrm
	}

	lappend configSpecs(-borderwidth)	0
	lappend configSpecs(-cursor)		""
	lappend configSpecs(-height)		0
	lappend configSpecs(-leavecommand)	""
	lappend configSpecs(-relief)		flat
	lappend configSpecs(-takefocus)		""
	lappend configSpecs(-width)		0
    }
    extendConfigSpecs

    variable configOpts [lsort [array names configSpecs]]

    #
    # The array pageConfigSpecs is used to handle page configuration options.
    # The names of its elements are the page configuration options for the
    # Pagesman widget class.  The value of an array element is a list
    # containing the database name and class.
    #
    #	Command-Line Name	{Database Name	Database Class	}
    #	---------------------------------------------------------
    #
    variable pageConfigSpecs
    array set pageConfigSpecs {
	-padding		{padding	Padding		}
	-sticky			{sticky		Sticky		}
    }

    #
    # Extend the elements of the array pageConfigSpecs
    #
    lappend pageConfigSpecs(-padding)	0
    lappend pageConfigSpecs(-sticky)	"nsew"

    #
    # Use a list to facilitate the handling of command options
    #
    variable cmdOpts [list add adjustsize attrib cget configure forget \
		      hasattrib haspageattrib index insert pageattrib \
		      pagecget pageconfigure pages select size unsetattrib \
		      unsetpageattrib window]
}

#
# Private procedure creating the default bindings
# ===============================================
#

#------------------------------------------------------------------------------
# scrollutil::pm::createBindings
#
# Creates the default bindings for the binding tag Pagesman.
#------------------------------------------------------------------------------
proc scrollutil::pm::createBindings {} {
    bind Pagesman <KeyPress> continue
    bind Pagesman <FocusIn> {
	if {[focus -lastfor %W] eq "%W"} {
	    if {[%W select] ne ""} {
		focus [%W select]
	    }
        }
    }
    bind Pagesman <Map> { scrollutil::pm::resizeWidgetDelayed %W 100 }
    bind Pagesman <Destroy> {
	namespace delete scrollutil::ns%W
	catch {rename %W ""}
    }
}

#
# Public procedure creating a new pagesman widget
# ===============================================
#

#------------------------------------------------------------------------------
# scrollutil::pagesman
#
# Creates a new pagesman widget whose name is specified as the first command-
# line argument, and configures it according to the options and their values
# given on the command line.  Returns the name of the newly created widget.
#------------------------------------------------------------------------------
proc scrollutil::pagesman args {
    variable usingTile
    variable pm::configSpecs
    variable pm::configOpts

    if {[llength $args] == 0} {
	mwutil::wrongNumArgs "pagesman pathName ?options?"
    }

    #
    # Create a frame of the class Pagesman
    #
    set win [lindex $args 0]
    if {[catch {
	if {$usingTile} {
	    ttk::frame $win -class Pagesman -padding 0
	} else {
	    tk::frame $win -class Pagesman -container 0
	    catch {$win configure -padx 0 -pady 0}
	}
    } result] != 0} {
	return -code error $result
    }

    grid rowconfigure    $win 0 -weight 1
    grid columnconfigure $win 0 -weight 1
    grid propagate $win 0

    #
    # Create a namespace within the current one to hold the data of the widget
    #
    namespace eval ns$win {
	#
	# The following array holds various data for this widget
	#
	variable data
	array set data {
	    pageList	{}
	    paddingList	{}
	    stickyList	{}
	    pageCount	0
	    currentPage	""
	    delay	0
	}

	#
	# The following array is used to hold arbitrary attributes
	# and their values for this widget and its pages
	#
	variable attribs
    }

    #
    # Initialize some further components of data
    #
    upvar ::scrollutil::ns${win}::data data
    foreach opt $configOpts {
	set data($opt) [lindex $configSpecs($opt) 3]
    }
    regsub -all "%" $win "%%" data(pathName)		;# needed in bindings

    #
    # Configure the widget according to the command-line
    # arguments and to the available database options
    #
    if {[catch {
	mwutil::configureWidget $win configSpecs scrollutil::pm::doConfig \
				scrollutil::pm::doCget [lrange $args 1 end] 1
    } result] != 0} {
	destroy $win
	return -code error $result
    }

    #
    # Move the original widget command into the namespace pm within the current
    # one and create an alias of the original name for a new widget procedure
    #
    rename ::$win pm::$win
    interp alias {} ::$win {} scrollutil::pm::pagesmanWidgetCmd $win

    return $win
}

#
# Private configuration procedures
# ================================
#

#------------------------------------------------------------------------------
# scrollutil::pm::doConfig
#
# Applies the value val of the configuration option opt to the pagesman widget
# win.
#------------------------------------------------------------------------------
proc scrollutil::pm::doConfig {win opt val} {
    variable configSpecs
    upvar ::scrollutil::ns${win}::data data

    #
    # Apply the value to the widget corresponding to the given option
    #
    switch [lindex $configSpecs($opt) 2] {
	f {
	    #
	    # Apply the value to the frame and save the
	    # properly formatted value of val in data($opt)
	    #
	    $win configure $opt $val
	    set data($opt) [$win cget $opt]
	}

	w { set data($opt) $val }
    }
}

#------------------------------------------------------------------------------
# scrollutil::pm::doCget
#
# Returns the value of the configuration option opt for the pagesman widget
# win.
#------------------------------------------------------------------------------
proc scrollutil::pm::doCget {win opt} {
    upvar ::scrollutil::ns${win}::data data
    return $data($opt)
}

#------------------------------------------------------------------------------
# scrollutil::pm::doPageConfig
#
# Applies the value val of the page configuration option opt to the page of
# index pageIdx of the pagesman widget win.
#------------------------------------------------------------------------------
proc scrollutil::pm::doPageConfig {pageIdx win opt val} {
    upvar ::scrollutil::ns${win}::data data
    set widget [lindex $data(pageList) $pageIdx]
    set isCurrent [expr {$widget eq $data(currentPage)}]

    switch -- $opt {
	-padding {
	    if {[catch {mwutil::parsePadding $win $val} result] != 0} {
		return -code error $result
	    }

	    set data(paddingList) \
		[lreplace $data(paddingList) $pageIdx $pageIdx $val]

	    if {$isCurrent} {
		foreach {l t r b} [mwutil::parsePadding $win $val] {}
		grid configure $widget -padx [list $l $r] -pady [list $t $b]
	    }

	    if {[winfo ismapped $win]} {
		resizeWidgetDelayed $win 100
	    }
	}

	-sticky {
	    if {![regexp {^[nsew]*$} $val]} {
		return -code error "bad -sticky specification \"$val\""
	    }

	    set data(stickyList) \
		[lreplace $data(stickyList) $pageIdx $pageIdx $val]

	    if {$isCurrent} {
		grid configure $widget -sticky $val
	    }
	}
    }
}

#------------------------------------------------------------------------------
# scrollutil::pm::doPageCget
#
# Returns the value of the page configuration option opt for the page of index
# pageIdx of the pagesman widget win.
#------------------------------------------------------------------------------
proc scrollutil::pm::doPageCget {pageIdx win opt} {
    upvar ::scrollutil::ns${win}::data data

    switch -- $opt {
	-padding { return [lindex $data(paddingList) $pageIdx] }
	-sticky  { return [lindex $data(stickyList)  $pageIdx] }
    }
}

#
# Private procedures implementing the pagesman widget command
# ===========================================================
#

#------------------------------------------------------------------------------
# scrollutil::pm::pagesmanWidgetCmd
#
# Processes the Tcl command corresponding to a pagesman widget.
#------------------------------------------------------------------------------
proc scrollutil::pm::pagesmanWidgetCmd {win args} {
    set argCount [llength $args]
    if {$argCount == 0} {
	mwutil::wrongNumArgs "$win option ?arg arg ...?"
    }

    upvar ::scrollutil::ns${win}::data data

    variable cmdOpts
    set cmd [mwutil::fullOpt "command" [lindex $args 0] $cmdOpts]

    switch $cmd {
	add {
	    if {$argCount < 2} {
		mwutil::wrongNumArgs \
		    "$win $cmd window ?option value option value ...?"
	    }

	    set widget [lindex $args 1]
	    if {![winfo exists $widget]} {
		return -code error "bad window path name \"$widget\""
	    }

	    set padding 0
	    set sticky  "nsew"
	    getPageOpts $win [lrange $args 2 end] padding sticky

	    if {[set idx [lsearch -exact $data(pageList) $widget]] < 0} {
		lappend data(pageList)    $widget
		lappend data(paddingList) $padding
		lappend data(stickyList)  $sticky
		incr data(pageCount)

		bind $widget <Map> +[list scrollutil::pm::resizeWidgetDelayed \
				     $data(pathName) 100]
	    } else {
		set data(paddingList) \
		    [lreplace $data(paddingList) $idx $idx $padding]
		set data(stickyList) \
		    [lreplace $data(stickyList) $idx $idx $sticky]

		if {$widget eq $data(currentPage)} {
		    foreach {l t r b} [mwutil::parsePadding $win $padding] {}
		    grid configure $widget \
			-padx [list $l $r] -pady [list $t $b] -sticky $sticky
		}
	    }

	    if {[winfo ismapped $win]} {
		resizeWidgetDelayed $win 100
	    }

	    return ""
	}

	adjustsize {
	    if {$argCount != 1} {
		mwutil::wrongNumArgs "$win $cmd"
	    }

	    return [resizeWidget $win 1]
	}

	attrib {
	    return [::scrollutil::attribSubCmd $win "widget" \
		    [lrange $args 1 end]]
	}

	cget {
	    if {$argCount != 2} {
		mwutil::wrongNumArgs "$win $cmd option"
	    }

	    #
	    # Return the value of the specified configuration option
	    #
	    variable configSpecs
	    set opt [mwutil::fullConfigOpt [lindex $args 1] configSpecs]
	    return $data($opt)
	}

	configure {
	    variable configSpecs
	    return [mwutil::configureSubCmd $win configSpecs \
		    scrollutil::pm::doConfig scrollutil::pm::doCget \
		    [lrange $args 1 end]]
	}

	forget {
	    if {$argCount != 2} {
		mwutil::wrongNumArgs "$win $cmd pageIndex"
	    }

	    set pageIdx [lindex $args 1]
	    ##nagelfar ignore
	    set pageIdx [format "%d" $pageIdx]	;# integer check with error msg
	    if {$pageIdx < 0 || $pageIdx >= $data(pageCount)} {
		return -code error "page index $pageIdx out of bounds"
	    }

	    set widget [lindex $data(pageList) $pageIdx]
	    if {[set forgetCmd $data(-forgetcommand)] ne "" &&
		![uplevel #0 $forgetCmd [list $win $widget]]} {
		return 0
	    }

	    set data(pageList) [lreplace $data(pageList) $pageIdx $pageIdx]
	    set data(paddingList) \
		[lreplace $data(paddingList) $pageIdx $pageIdx]
	    set data(stickyList) [lreplace $data(stickyList) $pageIdx $pageIdx]
	    incr data(pageCount) -1

	    upvar ::scrollutil::ns${win}::attribs attribs
	    foreach name [array names attribs $widget-*] {
		unset attribs($name)
	    }

	    if {$widget eq $data(currentPage)} {
		#
		# Select the next/previous page
		#
		if {$pageIdx < $data(pageCount)} {
		    ::$win select $pageIdx
		} elseif {[incr pageIdx -1] >= 0} {
		    ::$win select $pageIdx
		} else {
		    grid forget $widget
		    set data(currentPage) ""
		    event generate $win <<PagesmanPageChanged>>
		}
	    }

	    if {[winfo ismapped $win]} {
		resizeWidgetDelayed $win 100
	    }

	    return 1
	}

	hasattrib -
	unsetattrib {
	    if {$argCount != 2} {
		mwutil::wrongNumArgs "$win $cmd name"
	    }

	    return [::scrollutil::${cmd}SubCmd $win "widget" [lindex $args 1]]
	}

	haspageattrib -
	unsetpageattrib {
	    if {$argCount != 3} {
		mwutil::wrongNumArgs "$win $cmd pageIndex name"
	    }

	    set pageIdx [lindex $args 1]
	    ##nagelfar ignore
	    set pageIdx [format "%d" $pageIdx]	;# integer check with error msg
	    if {$pageIdx < 0 || $pageIdx >= $data(pageCount)} {
		return -code error "page index $pageIdx out of bounds"
	    }

	    set first [string first "page" $cmd]
	    set last [expr {$first + 3}]
	    set cmd [string replace $cmd $first $last]	;# (has|unset)attrib
	    set widget [lindex $data(pageList) $pageIdx]
	    return [::scrollutil::${cmd}SubCmd $win $widget [lindex $args 2]]
	}

	index {
	    if {$argCount != 2} {
		mwutil::wrongNumArgs "$win $cmd window"
	    }

	    set widget [lindex $args 1]
	    if {![winfo exists $widget]} {
		return -code error "bad window path name \"$widget\""
	    }

	    if {[set idx [lsearch -exact $data(pageList) $widget]] < 0} {
		return -code error "window \"$widget\" is not managed by $win"
	    }

	    return $idx
	}

	insert {
	    if {$argCount < 3} {
		mwutil::wrongNumArgs \
		    "$win $cmd pageIndex window ?option value option value ...?"
	    }

	    set pageIdx [lindex $args 1]
	    ##nagelfar ignore
	    set pageIdx [format "%d" $pageIdx]	;# integer check with error msg

	    set widget [lindex $args 2]
	    if {![winfo exists $widget]} {
		return -code error "bad window path name \"$widget\""
	    }

	    if {[set idx [lsearch -exact $data(pageList) $widget]] >= 0} {
		set padding [lindex $data(paddingList) $idx]
		set sticky  [lindex $data(stickyList)  $idx]
	    } else {
		set padding 0
		set sticky  "nsew"
	    }
	    getPageOpts $win [lrange $args 3 end] padding sticky

	    if {$idx >= 0} {
		set data(pageList)    [lreplace $data(pageList)    $idx $idx]
		set data(paddingList) [lreplace $data(paddingList) $idx $idx]
		set data(stickyList)  [lreplace $data(stickyList)  $idx $idx]
	    }

	    set data(pageList)    [linsert $data(pageList)    $pageIdx $widget]
	    set data(paddingList) [linsert $data(paddingList) $pageIdx $padding]
	    set data(stickyList)  [linsert $data(stickyList)  $pageIdx $sticky]

	    if {$idx < 0} {
		incr data(pageCount)

		bind $widget <Map> +[list scrollutil::pm::resizeWidgetDelayed \
				     $data(pathName) 100]
	    } else {
		if {$widget eq $data(currentPage)} {
		    foreach {l t r b} [mwutil::parsePadding $win $padding] {}
		    grid configure $widget \
			-padx [list $l $r] -pady [list $t $b] -sticky $sticky
		}
	    }

	    if {[winfo ismapped $win]} {
		resizeWidgetDelayed $win 100
	    }

	    return ""
	}

	pageattrib {
	    if {$argCount < 2} {
		mwutil::wrongNumArgs \
		    "$win $cmd pageIndex ?name ?value name value ...??"
	    }

	    set pageIdx [lindex $args 1]
	    ##nagelfar ignore
	    set pageIdx [format "%d" $pageIdx]	;# integer check with error msg
	    if {$pageIdx < 0 || $pageIdx >= $data(pageCount)} {
		return -code error "page index $pageIdx out of bounds"
	    }

	    set widget [lindex $data(pageList) $pageIdx]
	    return [::scrollutil::attribSubCmd $win $widget \
		    [lrange $args 2 end]]
	}

	pagecget {
	    if {$argCount != 3} {
		mwutil::wrongNumArgs "$win $cmd pageIndex option"
	    }

	    set pageIdx [lindex $args 1]
	    ##nagelfar ignore
	    set pageIdx [format "%d" $pageIdx]	;# integer check with error msg
	    if {$pageIdx < 0 || $pageIdx >= $data(pageCount)} {
		return -code error "page index $pageIdx out of bounds"
	    }

	    variable pageConfigSpecs
	    set opt [mwutil::fullConfigOpt [lindex $args 2] pageConfigSpecs]
	    return [doPageCget $pageIdx $win $opt]
	}

	pageconfigure {
	    if {$argCount < 2} {
		mwutil::wrongNumArgs \
		    "$win $cmd pageIndex ?option ?value option value ...??"
	    }

	    set pageIdx [lindex $args 1]
	    ##nagelfar ignore
	    set pageIdx [format "%d" $pageIdx]	;# integer check with error msg
	    if {$pageIdx < 0 || $pageIdx >= $data(pageCount)} {
		return -code error "page index $pageIdx out of bounds"
	    }

	    variable pageConfigSpecs
	    return [mwutil::configureSubCmd $win pageConfigSpecs \
		    "scrollutil::pm::doPageConfig $pageIdx" \
		    "scrollutil::pm::doPageCget $pageIdx" \
		    [lrange $args 2 end]]
	}

	pages {
	    if {$argCount != 1} {
		mwutil::wrongNumArgs "$win $cmd"
	    }

	    return $data(pageList)
	}

	select {
	    if {$argCount > 2} {
		mwutil::wrongNumArgs "$win $cmd ?pageIndex?"
	    }

	    if {$argCount == 1} {
		return $data(currentPage)
	    }

	    set pageIdx [lindex $args 1]
	    ##nagelfar ignore
	    set pageIdx [format "%d" $pageIdx]	;# integer check with error msg
	    if {$pageIdx < 0 || $pageIdx >= $data(pageCount)} {
		return -code error "page index $pageIdx out of bounds"
	    }

	    set widget [lindex $data(pageList) $pageIdx]
	    if {[lsearch -exact $data(pageList) $data(currentPage)] >= 0} {
		if {$widget ne $data(currentPage) &&
		    [set cmd $data(-leavecommand)] ne "" &&
		    ![uplevel #0 $cmd [list $win $data(currentPage)]]} {
		    return 0
		} else {
		    grid forget $data(currentPage)
		}
	    }

	    set padding [lindex $data(paddingList) $pageIdx]
	    set sticky  [lindex $data(stickyList)  $pageIdx]
	    foreach {l t r b} [mwutil::parsePadding $win $padding] {}
	    grid $widget -padx [list $l $r] -pady [list $t $b] -sticky $sticky
	    set data(currentPage) $widget

	    if {$widget ne $data(currentPage)} {
		event generate $win <<PagesmanPageChanged>>
	    }

	    return 1
	}

	size {
	    if {$argCount != 1} {
		mwutil::wrongNumArgs "$win $cmd"
	    }

	    return $data(pageCount)
	}

	window {
	    if {$argCount != 2} {
		mwutil::wrongNumArgs "$win $cmd pageIndex"
	    }

	    set pageIdx [lindex $args 1]
	    ##nagelfar ignore
	    set pageIdx [format "%d" $pageIdx]	;# integer check with error msg
	    if {$pageIdx < 0 || $pageIdx >= $data(pageCount)} {
		return -code error "page index $pageIdx out of bounds"
	    }

	    return [lindex $data(pageList) $pageIdx]
	}
    }
}

#------------------------------------------------------------------------------
# scrollutil::pm::getPageOpts
#
# Gets the page options from a given list of option-value pairs.
#------------------------------------------------------------------------------
proc scrollutil::pm::getPageOpts {win optValPairs paddingName stickyName} {
    upvar $paddingName padding $stickyName sticky

    set count [llength $optValPairs]
    variable pageConfigSpecs
    foreach {opt val} $optValPairs {
	set opt [mwutil::fullConfigOpt $opt pageConfigSpecs]
	if {$count == 1} {
	    return -code error "value for \"$opt\" missing"
	}

	switch -- $opt {
	    -padding {
		if {[catch {mwutil::parsePadding $win $val} result] != 0} {
		    return -code error $result
		}
		set padding $val
	    }

	    -sticky {
		if {![regexp {^[nsew]*$} $val]} {
		    return -code error "bad -sticky specification \"$val\""
		}
		set sticky $val
	    }
	}

	incr count -2
    }
}

#
# Private procedures used in bindings
# ===================================
#

#------------------------------------------------------------------------------
# scrollutil::pm::resizeWidgetDelayed
#------------------------------------------------------------------------------
proc scrollutil::pm::resizeWidgetDelayed {win ms {force 0}} {
    upvar ::scrollutil::ns${win}::data data
    if {![info exists data(afterId)]} {
	set data(afterId) \
	    [after $ms [list scrollutil::pm::resizeWidget $win $force]]
	set data(delay) $ms
    }
}

#------------------------------------------------------------------------------
# scrollutil::pm::resizeWidget
#------------------------------------------------------------------------------
proc scrollutil::pm::resizeWidget {win {force 0}} {
    if {![winfo exists $win] || [winfo class $win] ne "Pagesman"} {
	return ""
    }

    upvar ::scrollutil::ns${win}::data data
    catch {unset data(afterId)}

    set computeWidth  [expr {$data(-width)  <= 0 || $force}]
    set computeHeight [expr {$data(-height) <= 0 || $force}]
    if {$computeWidth || $computeHeight} {
	set bd [expr {2 * $data(-borderwidth)}]
	variable ::scrollutil::usingTile
	if {!$usingTile} {
	    incr bd [expr {2 * $data(-highlightthickness)}]
	}
    }

    if {$computeWidth} {
	set maxWidth 0
	foreach widget $data(pageList) padding $data(paddingList) {
	    foreach {l t r b} [mwutil::parsePadding $win $padding] {}
	    set reqWidth [expr {[winfo reqwidth $widget] + $l + $r}]
	    if {$reqWidth > $maxWidth} {
		set maxWidth $reqWidth
	    }
	}

	incr maxWidth $bd
	$win configure -width $maxWidth
	if {$force} {
	    set data(-width) $maxWidth
	}
    }

    if {$computeHeight} {
	if {$computeWidth} {
	    update idletasks
	    if {![winfo exists $win] || [winfo class $win] ne "Pagesman"} {
		return ""
	    }
	}

	set maxHeight 0
	foreach widget $data(pageList) padding $data(paddingList) {
	    foreach {l t r b} [mwutil::parsePadding $win $padding] {}
	    set reqHeight [expr {[winfo reqheight $widget] + $t + $b}]
	    if {$reqHeight > $maxHeight} {
		set maxHeight $reqHeight
	    }
	}

	incr maxHeight $bd
	$win configure -height $maxHeight
	if {$force} {
	    set data(-height) $maxHeight
	}
    }

    if {$computeWidth || $computeHeight} {
	if {$data(delay) == 100} {
	    #
	    # Resize the widget again 210 ms later because dynamic scrollbars
	    # within a scrollarea might need up to 300 ms for being mapped
	    #
	    resizeWidgetDelayed $win 210 $force
	} else {
	    set data(delay) 0
	}
    }

    if {$data(pageCount) != 0 && $data(currentPage) eq ""} {
	::$win select 0
    }

    return ""
}