summaryrefslogtreecommitdiff
path: root/Master/tlpkg/tlperl/lib/Tk/Adjuster.pm
blob: bbe44f33dc8784368330b83d882a2235b1bd38ed (plain)
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
package Tk::Adjuster;

use vars qw($VERSION);
$VERSION = '4.008'; # $Id: //depot/Tkutf8/Tk/Adjuster.pm#7 $

use base  qw(Tk::Frame);

# We cannot do this :

# Construct Tk::Widget 'packAdjust';

# because if managed object is Derived (e.g. a Scrolled) then our 'new'
# will be delegated and hierachy gets turned inside-out
# So packAdjust is autoloaded in Widget.pm


Construct Tk::Widget qw(Adjuster);

{package Tk::Adjuster::Item;

use strict;
use base  qw(Tk::Frame);

sub ClassInit
{
 my ($class,$mw) = @_;
 $mw->bind($class,'<1>',['BDown', 1]);
 $mw->bind($class,'<Shift-1>',['BDown', 0]);
 $mw->bind($class,'<B1-Motion>',['Motion',1]);
 $mw->bind($class,'<Shift-B1-Motion>',['Motion',0]);
 $mw->bind($class,'<ButtonRelease-1>',['Motion',0]);
 return $class;
}

sub BDown
{
 my($w, $delay_mask) = @_;
 $w->{'start_x'} = $w->XEvent->x;
 $w->{'start_y'} = $w->XEvent->y;
 my $adj  = $w->Parent;
 delete $adj->{'lin_info'};
 my $delay = $delay_mask && $adj->cget('-delay');
 if ($delay)
  {
    $adj->vert ? $adj->delta_width_bar(0) : $adj->delta_height_bar(0);
  }
}

sub Motion
{
 my($w, $delay_mask) = @_;
 my $ev = $w->XEvent;
 my $adj  = $w->Parent;

 my $delay = $delay_mask && $adj->cget('-delay');
 if ($adj->vert)
  {
    my $dx = $ev->x - $w->{'start_x'};
    $delay ?  $adj->delta_width_bar($dx) : $adj->delta_width($dx);
  }
 else
  {
    my $dy = $ev->y - $w->{'start_y'};
    $delay ? $adj->delta_height_bar($dy) : $adj->delta_height($dy);
  }
}

}



sub packAfter
{
 my ($w,$s,%args) = @_;
 my $side = $args{'-side'} ? $args{'-side'} : 'top';
 $w->configure(-side   => $side, -widget => $s);
 $w->packed($s, %args);
}

sub packForget
{
 my ($w,$forget_slave) = @_;
 $w->Tk::Widget::packForget;
 $w->slave->packForget if $forget_slave;
}

# Called by Tk::Widget::packAdjust. It was here before packAfter was added
sub packed
{
 my ($w,$s,%args) = @_;
 delete $args{'-before'};
 delete $args{'-in'};
 $args{'-expand'} = 0;
 $args{'-after'} = $s;
 $args{'-fill'} = (($w->vert) ? 'y' : 'x');
 $w->pack(%args);
}

sub gridded
{
 my ($w,$s,%args) = @_;
 # delete $args{'-before'};
 # $args{'-expand'} = 0;
 # $args{'-after'} = $s;
 # $args{'-fill'} = (($w->vert) ? 'y' : 'x');
 $w->grid(%args);
}

sub ClassInit
{
 my ($class,$mw) = @_;
 $mw->bind($class,'<Configure>','SizeChange');
 $mw->bind($class,'<Unmap>','Restore');
 $mw->bind($class,'<Map>','Mapped');
 return $class;
}

sub SizeChange
{
 my $w = shift;
 # reqwidth/height of Adjuster is stored here. If it is partially pushed out
 # of the window, then $w->width/height returns that of the visible part.
 if ($w->vert)
  {
   my $sx = ($w->Width - $w->{'sep'}->Width)/2;
   $w->{'but'}->place('-x' => 0, '-y' => $w->Height-18);
   $w->{'sep'}->place('-x' => $sx, '-y' => 0,  -relheight => 1);
   $w->configure(-width => $w->{'but'}->ReqWidth);
   $w->{'reqwidth'} = $w->reqwidth;
  }
 else
  {
   my $sy = ($w->Height - $w->{'sep'}->Height)/2;
   $w->{'but'}->place('-x' => $w->Width-18, '-y' => 0);
   $w->{'sep'}->place('-x' => 0, '-y' => $sy,  -relwidth => 1);
   $w->configure(-height => $w->{'but'}->ReqHeight);
   $w->{'reqheight'} = $w->reqheight;
  }
 # Turn off geometry propagation in the slave. Do only if necessary, as this
 # causes repacking.
 my $s = $w->slave;
 $s->packPropagate('0') if $s->packSlaves && $s->packPropagate();
 $s->gridPropagate('0') if $s->gridSlaves && $s->gridPropagate();
}

sub Mapped
{
 my $w = shift;
 $w->idletasks;
 my $m = $w->manager;
 if ($m =~ /^(?:pack|grid)$/)
  {
   my %info = $w->$m('info');
   my $master = $info{'-in'};
   $master->$m('propagate',0);
   $w->{'master'} = $master;
  }
 $w->slave_expand_off;
}

sub Populate
{
 my ($w,$args) = @_;
 $w->SUPER::Populate($args);
 $w->{'sep'} = Tk::Adjuster::Item->new($w,-bd => 1, -relief => 'sunken');
 $w->{'but'} = Tk::Adjuster::Item->new($w,-bd => 1, -width => 8, -height => 8, -relief => 'raised');

 # Force creation of Frame in widget's Toplevel. This makes a difference
 # where the widget's Toplevel has Delegates('Construct') set.
 # Need to explicitly set frame width to 0 for Win32
 my $l = $w->{'lin'} = Tk::Frame->new($w->toplevel, -bd => 0);
 # my $l = $w->{'lin'} = $w->toplevel->Frame(-bd => 0);

 my $cs = $w->ConfigSpecs(-widget => ['PASSIVE','widget','Widget',$w->Parent],
                 -side       => ['METHOD','side','Side','top'],
                 -delay      => ['PASSIVE','delay','Delay', 1],
                 -background => [['SELF',$w->{'sep'},$w->{'but'}],'background','Background',undef],
                 -foreground => [Tk::Configure->new($w->{'lin'},'-background'),'foreground','Foreground','black'],
		 -restore    => ['PASSIVE','restore', 'Restore', 1],
                );
 $w->_OnDestroy(qw(sep but lin master));
}

sub side
{
 my ($w,$val) = @_;
 if (@_ > 1)
  {
   $w->{'side'} = $val;
   my $cursor;
   if ($w->vert)
    {
     $cursor = 'sb_h_double_arrow';
     $w->{'sep'}->configure(-width => 2, -height => 10000);
    }
   else
    {
     $cursor = 'sb_v_double_arrow';
     $w->{'sep'}->configure(-height => 2, -width => 10000);
    }
   my $x;
   foreach $x ($w->{'sep'},$w->{'but'})
    {
     $x->configure(-cursor => $cursor);
    }
  }
 return $w->{'side'};
}

sub slave
{
 my $w = shift;
 my $s = $w->cget('-widget');
 return $s;
}

sub vert
{
 my $w = shift;
 my $side = $w->cget('-side');
 return  1 if $side eq 'left';
 return -1 if $side eq 'right';
 return  0;
}

# If the Adjuster gets unmapped, it attempts to restore itself. If its
# slave is mapped, then it reduces the size of the slave so that there is
# then room in the master for the Adjuster widget.
sub Restore
{
 my $w = shift;
 return if ! $w->toplevel->IsMapped ||
 	   ! $w->slave->IsMapped ||
	   ! $w->cget('-restore');
 $w->vert ? $w->delta_width(0) : $w->delta_height(0);
}

sub delta_width_bar
{
 my ($w,$dx) = @_;
 my $l = $w->{'lin'};
 my $r = $w->{'sep'};
 my $t = $w->toplevel;
 my $m = $w->{'master'};
 my $s = $w->slave;
 my ($min_rootx, $max_rootx, $t_border);
 if (! $w->{'lin_info'})
  {
   my $m_border = $m->cget('-bd') + $m->cget('-highlightthickness');
   $t_border    = $t->cget('-bd') + $t->cget('-highlightthickness');
   if ($w->cget('-side') eq 'right')
    {
     $min_rootx = $m->rootx + $m_border;
     $max_rootx = $s->rootx + $s->width - 1;
    }
   else
    {
     $min_rootx = $s->rootx;
     $max_rootx = $m->rootx + $m->width - $m_border - 1;
    }
   $w->{'lin_info'} = [$min_rootx, $max_rootx, $t_border];
  }
  else
   {
    ($min_rootx, $max_rootx, $t_border) = @{$w->{'lin_info'}};
   }
 $l->configure(-width => 1, -height => $w->height) unless $l->IsMapped;

 my $new_rootx = $w->rootx + $w->{'reqwidth'}/2 + $dx;
 $new_rootx = $min_rootx if $new_rootx < $min_rootx;
 $new_rootx = $max_rootx if $new_rootx > $max_rootx;
 my $placex = $new_rootx - $t->rootx - $t_border;
 my $placey = $w->rooty  - $t->rooty - $t_border;
 $l->place(-in => $t, -anchor => 'n', '-x' => $placex, '-y' => $placey);
 my $this = $w->containing($new_rootx, $w->rooty + 1);
 $l->raise($this) if $this && $this ne $t;
}

sub delta_width
{
 my ($w,$dx) = @_;
 my $l = $w->{'lin'};
 $l->placeForget;
 my $s = $w->slave;
 if ($s)
  {
   my $m = $w->{'master'};
   my $m_border = $m->cget('-bd') + $m->cget('-highlightthickness');
   my $w_width = $w->{'reqwidth'};
   my $m_width = $m->width;
   my $s_width = $s->width;
   my $max_width = $m_width - $w_width;
   my $max_s_width;
   if ($w->cget('-side') eq 'right')
    {
     $dx = -$dx;
     $max_s_width = $max_width -
		      ($m->rootx + $m_width - ($s->rootx+$s_width)) - $m_border;
    }
   else
    {
     $max_s_width = $max_width - ($s->rootx - $m->rootx) - $m_border;
    }
   my $new_width = $s_width+$dx;
   $new_width = $max_s_width if $new_width > $max_s_width;
   $new_width = 0 if $new_width < 0;
   $s->GeometryRequest($new_width, $s->height);
  }
}

sub delta_height_bar
{
 my ($w,$dy) = @_;
 my $l = $w->{'lin'};
 my $r = $w->{'sep'};
 my $t = $w->toplevel;
 my $m = $w->{'master'};
 my $s = $w->slave;
 my ($min_rooty, $max_rooty, $t_border);
 if (! $w->{'lin_info'})
  {
   my $m_border = $m->cget('-bd') + $m->cget('-highlightthickness');
   $t_border    = $t->cget('-bd') + $t->cget('-highlightthickness');
   if ($w->cget('-side') eq 'bottom')
    {
     $min_rooty = $m->rooty + $m_border;
     $max_rooty = $s->rooty + $s->height - 1;
    }
   else
    {
     $min_rooty = $s->rooty;
     $max_rooty = $m->rooty + $m->height - $m_border - 1;
    }
   $w->{'lin_info'} = [$min_rooty, $max_rooty, $t_border];
  }
 else
  {
   ($min_rooty, $max_rooty, $t_border) = @{$w->{'lin_info'}};
  }
 $l->configure(-height => 1, -width => $w->width) unless $l->IsMapped;

 my $new_rooty = $w->rooty + $w->{'reqheight'}/2 + $dy;
 $new_rooty = $min_rooty if $new_rooty < $min_rooty;
 $new_rooty = $max_rooty if $new_rooty > $max_rooty;
 my $placey = $new_rooty - $t->rooty - $t_border;
 my $placex = $w->rootx  - $t->rootx - $t_border;
 $l->place(-in => $t, -anchor => 'w', '-x' => $placex, '-y' => $placey);
 my $this = $w->containing($w->rootx + 1, $new_rooty);
 $l->raise($this) if $this && $this ne $t;
}

sub delta_height
{
 my ($w,$dy) = @_;
 my $l = $w->{'lin'};
 $l->placeForget;
 my $s = $w->slave;
 if ($s)
  {
   my $m = $w->{'master'};
   my $m_border = $m->cget('-bd') + $m->cget('-highlightthickness');
   my $w_height = $w->{'reqheight'};
   my $m_height = $m->height;
   my $s_height = $s->height;
   my $max_height = $m_height - $w_height;
   my $max_s_height;
   if ($w->cget('-side') eq 'bottom')
    {
     $dy = -$dy;
     $max_s_height = $max_height -
		    ($m->rooty + $m_height - ($s->rooty+$s_height)) - $m_border;
    }
   else
    {
     $max_s_height = $max_height - ($s->rooty - $m->rooty) - $m_border;
    }
   my $new_height = $s_height+$dy;

   $new_height = $max_s_height if $new_height > $max_s_height;
   $new_height = 0 if $new_height < 0;
   $s->GeometryRequest($s->width, $new_height);
  }
}

# Turn off expansion in the slave.
# This is done only if necessary, as calls to pack/gridConfigure cause
# repacking.
# Before call to pack/gridConfigure, the reqwidth/reqheight is set to the
# current width/height. This is because the geometry managers use
# the requested values, not the actual, to calculate the new geometry.
sub slave_expand_off
{
 my $w = shift;
 my $s = $w->slave;
 return if ! $s;

 my $manager = $s->manager;
 if ($manager eq 'pack')
  {
   my %info = $s->packInfo;
   my $expand = $info{'-expand'};
   if ($expand)
    {
     $s->GeometryRequest($s->width, $s->height);
     $s->packConfigure(-expand => 0);
    }
  }
 elsif ($manager eq 'grid')
  {
   my %info = $s->gridInfo;
   my $master = $info{'-in'};
   if ($w->vert)
    {
     my $col = $info{'-column'};
     my $expand = $master->gridColumnconfigure($col, '-weight');
     if ($expand)
      {
       $s->GeometryRequest($s->width, $s->height);
       $master->gridColumnconfigure($col, -weight => 0);
      }
    }
   else
    {
     my $row = $info{'-row'};
     my $expand = $master->gridRowconfigure($row, '-weight');
     if ($expand)
      {
       $s->GeometryRequest($s->width, $s->height);
       $master->gridRowconfigure($row, -weight => 0);
      }
    }
  }
}

1;

__END__