Home > Archive > Matlab > July 2005 > Mouse drag callback
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Mouse drag callback
|
|
| Fijoy George 2005-07-28, 9:12 am |
| Hi all, Does anyone know if we can define a Matlab GUI callback function
which is invoked when a user clicks and drags the mouse over a figure? That
is, a function similarly to WindowButtonMotionFcn.
-Fijoy
| |
| Steve Simon 2005-07-28, 5:05 pm |
| Fijoy George wrote:
> Hi all, Does anyone know if we can define a Matlab GUI callback function
> which is invoked when a user clicks and drags the mouse over a figure? That
> is, a function similarly to WindowButtonMotionFcn.
>
> -Fijoy
>
>
You need to use a combination of the WindowButtonDownFcn,
WindowButtonMotionFcn, and WindowButtonUpFcn to do this.
The WindowButtonDownFcn should be used to do the setup, so when you
click on the figure it will:
1) Get and save the current WindowButtonMotionFcn and WindowButtonUpFcn
properties.
2) Set the WindowButtonMotionFcn and WindowButtonUpFcn properties to new
values.
The WindowButtonMotionFcn should be set to execute whatever code you
want while you are dragging.
The WindowButtonUpFcn should be used to restore the previous property
values.
Here's a basic example:
function test
% test gui function
% create figure
fig = figure;
% set the figure's WindowButtonDownFcn
set(fig,'WindowButtonDownFcn',{@wbd});
% ---------------------------
function wbd(h,evd)
disp('down')
% get the values and store them in the figure's appdata
props.WindowButtonMotionFcn = get(h,'WindowButtonMotionFcn');
props.WindowButtonUpFcn = get(h,'WindowButtonUpFcn');
setappdata(h,'TestGuiCallbacks',props);
% set the new values for the WindowButtonMotionFcn and
% WindowButtonUpFcn
set(h,'WindowButtonMotionFcn',{@wbm})
set(h,'WindowButtonUpFcn',{@wbu})
% ---------------------------
function wbm(h,evd)
% executes while the mouse moves
disp('motion')
% ---------------------------
function wbu(h,evd)
% executes when the mouse button is released
disp('up')
% get the properties and restore them
props = getappdata(h,'TestGuiCallbacks');
set(h,props);
-Steve Simon
| |
| Fijoy George 2005-07-28, 5:05 pm |
| Thanks Steve. But, I actually want the dragging to work for a particular
'patch' (or 'axes'). Basically, I have two seperate 'patch'es displayed in
the same 'figure' using subplot() command. I want to have a callback
function which is called when the mouse is dragged in one of these patches
(or axes). Unfortunately, I see that patch (or axes) can have only
ButtonDownFnc, but no functions for ButtonUp or ButtonMotion.
Can I still produce the dragging effect?
-Fijoy
"Steve Simon" <ssimon@mathworks.com> wrote in message
news:dcarpn$hhh$1@fred.mathworks.com...
> Fijoy George wrote:
>
> You need to use a combination of the WindowButtonDownFcn,
> WindowButtonMotionFcn, and WindowButtonUpFcn to do this.
>
> The WindowButtonDownFcn should be used to do the setup, so when you click
> on the figure it will:
>
> 1) Get and save the current WindowButtonMotionFcn and WindowButtonUpFcn
> properties.
>
> 2) Set the WindowButtonMotionFcn and WindowButtonUpFcn properties to new
> values.
>
>
>
>
> The WindowButtonMotionFcn should be set to execute whatever code you want
> while you are dragging.
>
>
> The WindowButtonUpFcn should be used to restore the previous property
> values.
>
>
>
> Here's a basic example:
>
>
>
> function test
> % test gui function
>
> % create figure
> fig = figure;
>
> % set the figure's WindowButtonDownFcn
> set(fig,'WindowButtonDownFcn',{@wbd});
>
>
>
> % ---------------------------
> function wbd(h,evd)
>
> disp('down')
>
> % get the values and store them in the figure's appdata
> props.WindowButtonMotionFcn = get(h,'WindowButtonMotionFcn');
> props.WindowButtonUpFcn = get(h,'WindowButtonUpFcn');
> setappdata(h,'TestGuiCallbacks',props);
>
> % set the new values for the WindowButtonMotionFcn and
> % WindowButtonUpFcn
> set(h,'WindowButtonMotionFcn',{@wbm})
> set(h,'WindowButtonUpFcn',{@wbu})
>
>
> % ---------------------------
> function wbm(h,evd)
> % executes while the mouse moves
>
> disp('motion')
>
> % ---------------------------
> function wbu(h,evd)
> % executes when the mouse button is released
>
> disp('up')
>
> % get the properties and restore them
> props = getappdata(h,'TestGuiCallbacks');
> set(h,props);
>
>
>
>
>
>
>
>
> -Steve Simon
| |
| Steve Simon 2005-07-29, 9:07 am |
| Fijoy George wrote:
> Thanks Steve. But, I actually want the dragging to work for a particular
> 'patch' (or 'axes'). Basically, I have two seperate 'patch'es displayed in
> the same 'figure' using subplot() command. I want to have a callback
> function which is called when the mouse is dragged in one of these patches
> (or axes). Unfortunately, I see that patch (or axes) can have only
> ButtonDownFnc, but no functions for ButtonUp or ButtonMotion.
>
> Can I still produce the dragging effect?
>
> -Fijoy
>
>
> "Steve Simon" <ssimon@mathworks.com> wrote in message
> news:dcarpn$hhh$1@fred.mathworks.com...
>
>
>
>
You would still do something similar, but with the patch's ButtonDownFcn
instead of the figure's WindowButtonDownFcn. Since there is only one
callback property for motion and button ups, you have to share it, so
you must be careful about setting/restoring the WindowButtonMotionFcn
and WindowButtonUpFcn:
function test
fig = figure;
ax1 = subplot(2,1,1);
p1 = patch;
set(p1,'ButtonDownFcn',{@bd,1})
ax2 = subplot(2,1,2);
p2 = patch;
set(p2,'ButtonDownFcn',{@bd,2})
% ---------------------------
function bd(h,evd,n)
disp('down')
fig = ancestor(h,'figure');
% get the values and store them in the figure's appdata
props.WindowButtonMotionFcn = get(fig,'WindowButtonMotionFcn');
props.WindowButtonUpFcn = get(fig,'WindowButtonUpFcn');
setappdata(fig,'TestGuiCallbacks',props)
;
set(fig,'WindowButtonMotionFcn',{@wbm,n}
)
set(fig,'WindowButtonUpFcn',{@wbu})
% ---------------------------
function wbm(h,evd,n)
disp(['motion: patch ' num2str(n)])
% ---------------------------
function wbu(h,evd)
disp('up')
fig = ancestor(h,'figure');
props = getappdata(fig,'TestGuiCallbacks');
set(fig,props);
setappdata(fig,'TestGuiCallbacks',[]);
-Steve Simon
|
|
|
|
|