[paging control fixes
ronw@ee.columbia.edu**20081223003559
 
  - added plot_pages function to make figure paging functionality
    available outside of plotall.m
  - consolidated paging controls inside a uipanel and moved them to the
    top left corner of the figure.
 
] {
addfile ./plottools/plot_pages.m
hunk ./plottools/plot_pages.m 1
+function plot_pages(page_funs)
+% plot_pages({page1_fun, page2_fun, page3_fun, ...})
+% 
+% Sets up a figure with controls that allow browsing across number of
+% "pages" drawn up by the given function handles.
+%
+% Each entry of the {page_funi} cell array should be a function handle
+% to a function that takes no arguments and draws the contents of a
+% particular page.
+%
+% E.g.  plot_pages({@() plot(x, y), @() imagesc(rand(100))})
+%
+% will setup a figure containing plot(x,y) and controls that allow the
+% user to switch to the next page.  When the "Next" button is clicked,
+% the figure will be redrawn to contain a random 100x100 pixel image.
+%
+% Note that the pagei_fun is called every time the user switches to
+% page i.  So in the previous example, the random image will change
+% every time page 2 is redrawn (by interacting with the paging
+% controls, not by underlying GUI updates).
+%
+% 2008-12-22 ronw@ee.columbia.edu
+
+% Copyright (C) 2008 Ron J. Weiss (ronw@ee.columbia.edu)
+%
+% This program is free software: you can redistribute it and/or modify
+% it under the terms of the GNU General Public License as published by
+% the Free Software Foundation, either version 3 of the License, or
+% (at your option) any later version.
+%
+% This program is distributed in the hope that it will be useful,
+% but WITHOUT ANY WARRANTY; without even the implied warranty of
+% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+% GNU General Public License for more details.
+%
+% You should have received a copy of the GNU General Public License
+% along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+draw_page(page_funs, 1);
+
+
+function draw_page(page_funs, page_num)
+npages = length(page_funs);
+
+if nargin(page_funs{page_num}) > 0
+  warning(sprintf('Cannot draw to page %d. Invalid drawing function: %s.', ...
+      page_num, func2str(page_funs{page_num})))
+  return
+end
+
+clf
+feval(page_funs{page_num});
+
+if npages == 1
+  return
+end
+
+if page_num == 1
+  enable_prev_button = 'off';
+else
+  enable_prev_button = 'on';
+end
+
+if page_num == npages
+  enable_next_button = 'off';
+else
+  enable_next_button = 'on';  
+end
+
+h_panel = uipanel('Units', 'normalized', 'Position', [0 1.0 1.0 1.0], ...
+    'Tag', 'plot_pages_controls');
+pos = [0 0 40 20];
+uicontrol('Parent', h_panel, 'Style', 'pushbutton', 'String', 'First', ...
+    'Units', 'pixels', 'Position', pos, ...
+    'Callback', @(a,b) draw_page(page_funs, 1));
+pos(1) = pos(1) + pos(3);
+uicontrol('Parent', h_panel, 'Style', 'pushbutton', 'String', 'Prev', ...
+    'Units', 'pixels', 'Position', pos, ...
+    'Callback', @(a,b) draw_page(page_funs, page_num - 1), ...
+    'Enable', enable_prev_button);
+pos(1) = pos(1) + pos(3);
+pos(3) = 50;
+uicontrol('Parent', h_panel, 'Style', 'edit', 'String', page_num, ...
+    'Units', 'pixels', 'Position', pos, ...
+    'Callback', @(a,b) draw_page(page_funs, ...
+    max(min(str2num(get(a, 'String')), npages), 1)));
+pos(1) = pos(1) + pos(3);
+uicontrol('Parent', h_panel, 'Style', 'text', ...
+    'String', sprintf(' / %d', npages), 'Position', pos);
+pos(1) = pos(1) + pos(3);
+pos(3) = 40;
+uicontrol('Parent', h_panel, 'Style', 'pushbutton', 'String', 'Next', ...
+    'Units', 'pixels', 'Position', pos, ...
+    'Callback', @(a,b) draw_page(page_funs, page_num + 1), ...
+    'Enable', enable_next_button);
+pos(1) = pos(1) + pos(3);
+uicontrol('Parent', h_panel, 'Style', 'pushbutton', 'String', 'Last', ...
+    'Units', 'pixels', 'Position', pos, ...
+    'Callback', @(a,b) draw_page(page_funs, npages));
+
+width_px = pos(1) + pos(3);
+height_px = pos(4);
+position_pager_controls(h_panel, width_px, height_px)
+set(h_panel, 'ResizeFcn', ...
+    @(a,b) position_pager_controls(h_panel, width_px, height_px))
+
+
+function position_pager_controls(h_panel, width_px, height_px)
+set(h_panel, 'Units', 'pixels', 'Position', [0 0 width_px height_px])
+set(h_panel, 'Units', 'normalized')
+pos = get(h_panel, 'Position');
+pos(1) = 0;
+pos(2) = 1.0 - pos(4);
+set(h_panel, 'Position', pos);
+
hunk ./plottools/plotall.m 83
-plot_data(data, properties, 1);
+plot_page(data, properties, 1)
hunk ./plottools/plotall.m 180
-all_axes = zeros(nplot, 1);
+subplots = cell(1, nplot);
hunk ./plottools/plotall.m 182
-  all_axes(x) = subplot(props.subplot(1), props.subplot(2), x);
+  subplots{x} = {props.subplot(1), props.subplot(2), x};
hunk ./plottools/plotall.m 184
-props.all_axes = all_axes;
+props.subplots = subplots;
hunk ./plottools/plotall.m 198
-props.axes = all_axes(plot_num);
+props.subplots = subplots(plot_num);
hunk ./plottools/plotall.m 202
-function plot_data(data, properties, curr_page)
+function plot_page(data, properties, curr_page)
hunk ./plottools/plotall.m 208
-
-% Pass 0: Hide all subplots.  The ones we use are made visible
-% automatically.
-for ax = properties.all_axes(:)'
-  set(ax, 'Visible', 'off')
-  children = get(ax, 'Children');
-  set(children, 'Visible', 'off')
-end
-
hunk ./plottools/plotall.m 209
+clf
hunk ./plottools/plotall.m 219
-  curr_axes = properties.axes(x);
-  axes(curr_axes)
+  curr_axes = subplot(properties.subplots{x}{:});
hunk ./plottools/plotall.m 240
-  if x < 1 || x > length(data)
+  if x < 1 || x > ndat;
hunk ./plottools/plotall.m 243
-  curr_axes = properties.axes(x);
+  curr_axes = all_axes(x - plots(1) + 1);
hunk ./plottools/plotall.m 296
-    add_pager_buttons(data, properties, curr_page)
+    add_pager_controls(data, properties, curr_page)
hunk ./plottools/plotall.m 305
-function add_pager_buttons(data, properties, curr_page)
+function add_pager_controls(data, properties, curr_page)
hunk ./plottools/plotall.m 319
-  enable_next_button = 'on';  
+  enable_next_button = 'on';
hunk ./plottools/plotall.m 322
-pos = [20 20 40 20];
-uicontrol('Style', 'pushbutton', 'String', 'First', 'Position', pos, ...
-    'Callback', @(a,b) plot_data(data, properties, 1));
+h_panel = uipanel('Units', 'normalized', 'Position', [0 1.0 1.0 1.0], ...
+    'Tag', 'plot_pages_controls');
+pos = [0 0 40 20];
+uicontrol('Parent', h_panel, 'Style', 'pushbutton', 'String', 'First', ...
+    'Units', 'pixels', 'Position', pos, ...
+    'Callback', @(a,b) plot_page(data, properties, 1));
hunk ./plottools/plotall.m 329
-uicontrol('Style', 'pushbutton', 'String', 'Prev', 'Position', pos, ...
-    'Callback', @(a,b) plot_data(data, properties, curr_page - 1), ...
+uicontrol('Parent', h_panel, 'Style', 'pushbutton', 'String', 'Prev', ...
+    'Units', 'pixels', 'Position', pos, ...
+    'Callback', @(a,b) plot_page(data, properties, curr_page - 1), ...
hunk ./plottools/plotall.m 335
-uicontrol('Style', 'edit', 'String', curr_page, 'Position', pos, ...
-    'Callback', @(a,b) plot_data(data, properties, ...
+uicontrol('Parent', h_panel, 'Style', 'edit', 'String', curr_page, ...
+    'Units', 'pixels', 'Position', pos, ...
+    'Callback', @(a,b) plot_page(data, properties, ...
hunk ./plottools/plotall.m 340
-uicontrol('Style', 'text', 'String', sprintf(' / %d', npages), ...
+uicontrol('Parent', h_panel, 'Style', 'text', 'String', sprintf(' / %d', npages), ...
hunk ./plottools/plotall.m 344
-uicontrol('Style', 'pushbutton', 'String', 'Next', 'Position', pos, ...
-    'Callback', @(a,b) plot_data(data, properties, curr_page + 1), ...
+uicontrol('Parent', h_panel, 'Style', 'pushbutton', 'String', 'Next', ...
+    'Units', 'pixels', 'Position', pos, ...
+    'Callback', @(a,b) plot_page(data, properties, curr_page + 1), ...
hunk ./plottools/plotall.m 349
-uicontrol('Style', 'pushbutton', 'String', 'Last', 'Position', pos, ...
-    'Callback', @(a,b) plot_data(data, properties, npages));
+uicontrol('Parent', h_panel, 'Style', 'pushbutton', 'String', 'Last', ...
+    'Units', 'pixels', 'Position', pos, ...
+    'Callback', @(a,b) plot_page(data, properties, npages));
+
+width_px = pos(1) + pos(3);
+height_px = pos(4);
+position_pager_controls(h_panel, width_px, height_px)
+set(h_panel, 'ResizeFcn', ...
+    @(a,b) position_pager_controls(h_panel, width_px, height_px))
+
+
+
+function position_pager_controls(h_panel, width_px, height_px)
+set(h_panel, 'Units', 'pixels', 'Position', [0 0 width_px height_px])
+set(h_panel, 'Units', 'normalized')
+pos = get(h_panel, 'Position');
+pos(1) = 0;
+pos(2) = 1.0 - pos(4);
+set(h_panel, 'Position', pos);
+
}
