function manipulate_movie (themovie)
% manipulate_movie (movie)

global ah
global fnh
global frame
global nframes
global mymovie

mymovie = themovie;

nframes = length (mymovie);

% XXX check nframes > 0

% get a new figure handle
fh = figure ('Name', 'Movie manipulator', 'MenuBar', 'none', 'Toolbar', 'none');
%ph = uipanel ('Parent', fh, 'Title', 'Movie', 'Position', [.1, .1, .6, .8]);
ah = axes ('Parent', fh, 'Position', [.1, .1, .6, .8]);

frame = 1;
image (mymovie(1).cdata, 'Parent', ah);

fwdh = uicontrol (fh, 'Style', 'pushbutton', 'String', 'Fwd', 'Units', 'normalized', 'Position', [0.75, 0.6, 0.2, 0.1], 'Callback', @fwd_pushed);
rewh = uicontrol (fh, 'Style', 'pushbutton', 'String', 'Rew', 'Units', 'normalized', 'Position', [0.75, 0.4, 0.2, 0.1], 'Callback', @rew_pushed);
fnh = uicontrol (fh, 'Style', 'text', 'String', 'frame: 1', 'Units', 'normalized', 'Position', [0.75, 0.8, 0.2, 0.05]);

set (fh, 'KeyReleaseFcn', @keypress);

end

function keypress (hObject, eventdata)

switch eventdata.Key
    case 'leftarrow', rew_pushed (hObject, []);
    case 'rightarrow', fwd_pushed (hObject, []);
end

end

function fwd_pushed (hObject, eventdata)

global ah
global fnh
global frame
global nframes
global mymovie

frame = frame + 1;
if frame > nframes
    frame = 1;
end

image (mymovie(frame).cdata, 'Parent', ah);

framestr = num2str (frame);
set (fnh, 'String', ['frame: ' framestr]);

end

function rew_pushed (hObject, eventdata)

global ah
global fnh
global frame
global nframes
global mymovie

frame = frame - 1;
if frame < 1
    frame = nframes;
end

image (mymovie(frame).cdata, 'Parent', ah);

framestr = num2str (frame);
set (fnh, 'String', ['frame: ' framestr]);

end