-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbdScroll.m
201 lines (170 loc) · 8.12 KB
/
bdScroll.m
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
classdef bdScroll < handle
%bdScroll - A scrolling uipanel for the Brain Dynamics Toolbox.
%
%EXAMPLE
% fig = figure('Units','pixels');
% scr = bdScroll(fig,300,300);
% ax = axes('Parent',scr.panel);
% imagesc(rand(100,100),'Parent',ax);
%
%AUTHORS
% Stewart Heitmann (2017b,2017c)
% Copyright (C) 2016-2019 QIMR Berghofer Medical Research Institute
% All rights reserved.
%
% Redistribution and use in source and binary forms, with or without
% modification, are permitted provided that the following conditions
% are met:
%
% 1. Redistributions of source code must retain the above copyright
% notice, this list of conditions and the following disclaimer.
%
% 2. Redistributions in binary form must reproduce the above copyright
% notice, this list of conditions and the following disclaimer in
% the documentation and/or other materials provided with the
% distribution.
%
% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
% "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
% LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
% FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
% COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
% INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
% BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
% LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
% CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
% LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
% ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
% POSSIBILITY OF SUCH DAMAGE.
properties (Access = public)
panel % uipanel for user-defined graphics objects
vpanel % viewport panel
end
properties (Access = private)
cpanel % container panel
vscroll % vertical scrollbar
hscroll % horizontal scrollbar
end
methods
% Constructor
function this = bdScroll(parent,panelw,panelh,varargin)
% Construct a container panel at the given position within the parent
this.cpanel = uipanel('Parent',parent, 'BorderType','none');
% construct view panel within the container (approximate position only)
this.vpanel = uipanel('Parent',this.cpanel, ...
'Units','pixels', ...
'Position',[0 20 80 80], ...
varargin{:});
% construct the vertical scrollbar within the container (approximate position only)
this.vscroll = uicontrol('Style','slider', ...
'Value',1, ...
'Parent',this.cpanel, ...
'Units','pixels', ...
'Position', [20,20,80,80] );
% Slider callbacks are only executed when the user releases the
% slider thumb. But we also want it to execute when the thumb
% is being dragged. So we use a listener instead.
addlistener(this.vscroll,'Value','PostSet',@(~,~) this.vscrollCallback());
% construct the horizontal scrollbar within the container (approximate position only)
this.hscroll = uicontrol('Style','slider', ...
'Parent',this.cpanel, ...
'Units','pixels', ...
'Position', [0 0 80,20] );
% Slider callbacks are only executed when the user releases the
% slider thumb. But we also want it to execute when the thumb
% is being dragged. So we use a listener instead.
addlistener(this.hscroll,'Value','PostSet',@(~,~) this.hscrollCallback());
% construct the users panel within the view panel.
this.panel = uipanel('parent',this.vpanel, ...
'Units','pixels', ...
'Position',[2 2 panelw panelh], ...
'BorderType','none');
% resize the widgets within the container (to their accurate positions)
this.SizeChanged();
% register the SizeChanged callback for the container panel
this.cpanel.SizeChangedFcn = @(src,evnt) this.SizeChanged();
% register the SizeChanged callback for the user panel
this.panel.SizeChangedFcn = @(src,evnt) this.SizeChangedUser(src);
end
end
methods (Access = private)
% Callback for changes to the size of the container panel.
function SizeChanged(this)
%disp('bdScroll.SizeChanged');
% set container units to pixels
cpanelunits = this.cpanel.Units;
this.cpanel.Units = 'pixels';
% get geometry of container panel (in pixels)
cpanelw = this.cpanel.Position(3);
cpanelh = this.cpanel.Position(4);
% get geometry of our user panel
panelw = this.panel.Position(3);
panelh = this.panel.Position(4);
% compute our widget geometries (assuming both scrollbars active)
vpanelpos = [2,23,max(cpanelw-21,0),max(cpanelh-22,0)];
vscrollpos = [cpanelw-22,22,20,max(cpanelh-22,0)];
hscrollpos = [3,1,max(cpanelw-23,0),20];
% if the user panel is narrower than our container panel
% then we don't need the horizontal scrollbar
if (panelw < cpanelw)
this.hscroll.Visible='off';
this.hscroll.Value=0;
vpanelpos(2) = 2;
vpanelpos(4) = cpanelh-1;
else
this.hscroll.Visible='on';
end
% if the user panel is shorter than our container panel
% then we don't need the vertical scrollbar
if (panelh < cpanelh)
this.vscroll.Visible='off';
this.vscroll.Value=1;
vpanelpos(1) = 2;
vpanelpos(3) = cpanelw-2;
else
this.vscroll.Visible='on';
end
% Apply the new widget geometries
this.vpanel.Position = vpanelpos;
this.vscroll.Position = vscrollpos;
this.hscroll.Position = hscrollpos;
% Apply the existing scrollbar settings
% to the new panel geometry
this.vscrollCallback();
this.hscrollCallback();
% restore the parent units
this.cpanel.Units = cpanelunits;
end
% Callback for changes to the size of the user panel.
% This callback is invoked when either the width or height of the
% user panel is altered but not when the x or y positions are altered.
% The SizeChanged callback of the enclosing container panel will
% also be invoked (after this function).
function SizeChangedUser(this,src)
%disp('bdScroll.SizeChangedUser');
% re-align the user panel to the top of the viewport.
vpanely = this.vpanel.Position(2);
vpanelh = this.vpanel.Position(4);
panelh = this.panel.Position(4);
this.panel.Position(2) = vpanely + vpanelh - panelh;
% adjust the scrollbars using the container panel callback
this.SizeChanged();
end
function vscrollCallback(this)
%disp('bdScroll.vscrollCallback');
vpanelh = this.vpanel.Position(4);
panelh = this.panel.Position(4);
shifth = panelh-vpanelh;
offsety = this.vscroll.Value * shifth;
this.panel.Position(2) = -offsety;
end
function hscrollCallback(this)
%disp('bdScroll.hscrollCallback');
vpanelw = this.vpanel.Position(3);
panelw = this.panel.Position(3);
shiftw = max(panelw-vpanelw,0);
offsetx = this.hscroll.Value * shiftw;
this.panel.Position(1) = -offsetx;
end
end
end