Retrieving coordinates with ScrollAreaGadget()

Just starting out? Need help? Post your questions and find answers here.
tOnGAs
User
User
Posts: 12
Joined: Tue Aug 31, 2004 12:08 pm
Location: France

Retrieving coordinates with ScrollAreaGadget()

Post by tOnGAs »

Could somebody please help me ?
I'm trying to find a way to get the coordinates of the mouse pointer when I left click somewhere in a ScrollAreaGadget().
This may not be the best way to do it. But after extensive searching I'm getting a bit clueless... :roll:

Anyway, here is what I managed to do this far.

Code: Select all

Procedure Max(n1, n2)
  If n1 >= n2
    max = n1
  Else
    max = n2
  EndIf
  ProcedureReturn max
EndProcedure

UseJPEGImageDecoder()

; Load BIG a picture made of 2 parts (left & right)
LoadImage(0, "H:\FondsDePage\bigimage_left.jpg")
img1width.l = ImageWidth()
img1height.l = ImageHeight()
LoadImage(1, "H:\FondsDePage\bigimage_right.jpg")
img2width.l = ImageWidth()
img2height.l = ImageHeight()

; Display the whole thing using a ScrollAreaGadget()
OpenWindow(0, 0, 0, 800, 600, #PB_Window_SystemMenu | #PB_Window_ScreenCentered, "Nouveau projet")
CreateGadgetList(WindowID(0))
ScrollAreaGadget(0, 0, 0, 800, 600, img1width + img2width, max(img1height, img2height), 10)
ImageGadget(1, 0, 0, img1width, img1height ,UseImage(0))
ImageGadget(2, img1width, 0, img2width, img2height, UseImage(1))
CloseGadgetList()

; How do I...
; ...find the coordinates (in pixels) of the mouse when somebody clicks somewhere in the image ?
;
Quit.l = 0

Repeat

  EventID.l = WaitWindowEvent()
  
  If EventID = #PB_Event_CloseWindow
    Quit = 1
  EndIf

Until Quit = 1

End
I understand ScrollAreaGadget() and ImageGadget() do not react to any user-input but there may be some API-magic to do it anyway ?

Thanks for any help or advice.
Last edited by tOnGAs on Tue Oct 05, 2004 10:09 am, edited 1 time in total.
Sparkie
PureBatMan Forever
PureBatMan Forever
Posts: 2307
Joined: Tue Feb 10, 2004 3:07 am
Location: Ohio, USA

Post by Sparkie »

There should be at least two different ways to do this. Here's one way. If I get time, I'll post another method. :)

Code: Select all

Procedure Max(n1, n2) 
  If n1 >= n2 
    max = n1 
  Else 
    max = n2 
  EndIf 
  ProcedureReturn max 
EndProcedure 

UseJPEGImageDecoder() 

; Load BIG a picture made of 2 parts (left & right) 
LoadImage(0, "H:\FondsDePage\bigimage_left.jpg") 
img1width.l = ImageWidth() 
img1height.l = ImageHeight() 
LoadImage(1, "H:\FondsDePage\bigimage_right.jpg") 
img2width.l = ImageWidth() 
img2height.l = ImageHeight() 

; Display the whole thing using a ScrollAreaGadget() 
OpenWindow(0, 0, 0, 800, 600, #PB_Window_SystemMenu | #PB_Window_ScreenCentered, "Nouveau projet") 
CreateGadgetList(WindowID(0)) 
ScrollAreaGadget(0, 0, 0, 800, 600, img1width + img2width, Max(img1height, img2height), 10) 
ImageGadget(1, 0, 0, img1width, img1height ,UseImage(0)) 
ImageGadget(2, img1width, 0, img2width, img2height, UseImage(1)) 
CloseGadgetList() 

; --> this gets the parent of ImageGadgets in the ScrollAreaGadget
hChild = FindWindowEx_(GadgetID(0), 0, "PureScrollAreaChild", 0)

; How do I... 
; ...find the coordinates (in pixels) of the mouse when somebody clicks somewhere in the image ? 
; 
Quit.l = 0 


Repeat 
  
  EventID.l = WaitWindowEvent() 
  
  If EventID = #WM_LBUTTONDOWN
    GetCursorPos_(@cPos.POINT)
    ScreenToClient_(hChild, cPos)
    imageIs = ChildWindowFromPoint_(hChild, cPos\x, cPos\y)
    GetCursorPos_(@cPos.POINT)
    ScreenToClient_(imageIs, cPos)
    gadgetIs = GetDlgCtrlID_(imageIs)
    If gadgetIs = 1 Or gadgetIs = 2
      MessageRequester("Clicked ImageGadget#" + Str(gadgetis),  "Coordinate=  x " + Str(cPos\x) + "  y " + Str(cPos\y))
    EndIf
  EndIf
  
  If EventID = #PB_Event_CloseWindow 
    Quit = 1 
  EndIf 
  
Until Quit = 1 

End 
Last edited by Sparkie on Tue Oct 05, 2004 3:27 pm, edited 1 time in total.
What goes around comes around.

PB 5.21 LTS (x86) - Windows 8.1
Sparkie
PureBatMan Forever
PureBatMan Forever
Posts: 2307
Joined: Tue Feb 10, 2004 3:07 am
Location: Ohio, USA

Post by Sparkie »

Here's another method using a WindowCallback. :)

Code: Select all

#STN_CLICKED = 0
#SS_NOTIFY = $100

Procedure Max(n1, n2) 
  If n1 >= n2 
    max = n1 
  Else 
    max = n2 
  EndIf 
  ProcedureReturn max 
EndProcedure 

UseJPEGImageDecoder() 

; --> our WindowCallback to get ImageGadget mouse clicks
Procedure myWindowCallback(hWnd, Msg, wParam, lParam)
  result = #PB_ProcessPureBasicEvents
  
  Select Msg
    Case #WM_COMMAND
      gadgetIs = wParam &$FFFF
      nMessage = wParam<<16 &$FFFF
      
      If nMessage = #STN_CLICKED
        GetCursorPos_(@cPos.POINT)
        GetCursorPos_(@cPos.POINT)
        ScreenToClient_(lParam, cPos)
        If wParam &$FFFF = 1 Or wParam &$FFFF = 2
          MessageRequester("Clicked ImageGadget#" + Str(gadgetIs),  "Coordinate=  x " + Str(cPos\x) + "  y " + Str(cPos\y))
        EndIf
      EndIf
  EndSelect
  
  ProcedureReturn result
EndProcedure
; <--

; Load BIG a picture made of 2 parts (left & right) 
LoadImage(0, "H:\FondsDePage\bigimage_left.jpg") 
img1width.l = ImageWidth() 
img1height.l = ImageHeight() 
LoadImage(1, "H:\FondsDePage\bigimage_right.jpg") 
img2width.l = ImageWidth() 
img2height.l = ImageHeight() 

; Display the whole thing using a ScrollAreaGadget() 
OpenWindow(0, 0, 0, 800, 600, #PB_Window_SystemMenu | #PB_Window_ScreenCentered, "Nouveau projet") 
SetWindowCallback(@myWindowCallback())
CreateGadgetList(WindowID(0)) 
ScrollAreaGadget(0, 0, 0, 800, 600, img1width + img2width, Max(img1height, img2height), 10) 
ImageGadget(1, 0, 0, img1width, img1height ,UseImage(0)) 
ImageGadget(2, img1width, 0, img2width, img2height, UseImage(1)) 
CloseGadgetList() 

; --> ImageGadgets will need the #SS_NOTIFY flag for mouse click
style = GetWindowLong_(GadgetID(1), #GWL_STYLE)
newstyle = style |#SS_NOTIFY
SetWindowLong_(GadgetID(1), #GWL_STYLE, newstyle)
SetWindowLong_(GadgetID(2), #GWL_STYLE, newstyle)
; <--

; How do I... 
; ...find the coordinates (in pixels) of the mouse when somebody clicks somewhere in the image ? 
; 
Quit.l = 0 


Repeat 
  
  EventID.l = WaitWindowEvent() 
  
  If EventID = #PB_Event_CloseWindow 
    Quit = 1 
  EndIf 
  
Until Quit = 1 

End 
Last edited by Sparkie on Tue Oct 05, 2004 3:28 pm, edited 1 time in total.
What goes around comes around.

PB 5.21 LTS (x86) - Windows 8.1
tOnGAs
User
User
Posts: 12
Joined: Tue Aug 31, 2004 12:08 pm
Location: France

Post by tOnGAs »

Thanks Sparkie. I really like the second method using a WindowCallback.
Impressive display of API-knowledge !

I read this in a recent post from Fred :
#SS_NOTIFY is now added to the ImageGadget() in standard.
So I probably wont need these lines anymore ?

Code: Select all

; --> ImageGadgets will need the #SS_NOTIFY flag for mouse click
style = GetWindowLong_(GadgetID(1), #GWL_STYLE)
newstyle = style |#SS_NOTIFY
SetWindowLong_(GadgetID(1), #GWL_STYLE, newstyle)
SetWindowLong_(GadgetID(2), #GWL_STYLE, newstyle) 
I'm so happy ! Thanks again ! :P
PB 3.93 Registered User
pnd
User
User
Posts: 10
Joined: Mon Feb 14, 2022 9:33 pm

Re: Retrieving coordinates with ScrollAreaGadget()

Post by pnd »

Sparkie

I modified your windowcallback code listed above. Works great. Thanks, don't think I could have done it on my own. I would like to further modify it to leave a mark on the image being scrolled at the point where the mouse click takes place. I have not had any success adding this capability. Do you have any guidance on this? Thanks in advance.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Retrieving coordinates with ScrollAreaGadget()

Post by netmaestro »

To use the modern PB way, you can drop the callback and API calls, use canvas gadgets instead of image gadgets and place the image on the canvas using SetGadgetAttribute(). Then you can test the mouse position at any time using GetGadgetAttribute() as shown in the doc:
Further information about the current event can be received with the GetGadgetAttribute() function. This information is only available if the current event received by WaitWindowEvent() or WindowEvent() is an event for this gadget. The following attributes can be used:

#PB_Canvas_MouseX, #PB_Canvas_MouseY
So first look for the gadget event #PB_EventType_LeftClick and then use GetGadgetAttribute() as shown above to get the x and y coordinates.

To make a mark on the image you would StartDrawing(CanvasOutput(canvas)), use whatever you like to draw, Plot() or LineXY() or Box(), Circle() are options. You can also draw another little image on there like an arrow, a sign of some sort, etc. then StopDrawing().

In the modern PureBasic it's so much easier and it's all native. No API or callback needed anymore.
BERESHEIT
pnd
User
User
Posts: 10
Joined: Mon Feb 14, 2022 9:33 pm

Re: Retrieving coordinates with ScrollAreaGadget()

Post by pnd »

Netmaestro

Thanks for the quick reply. I'll try as you directed. I'm new to this language, but like it so far. I'm an old Clipper programmer trying to move my applications to a modern environment. My younger engineers don't have a grasp of MSDOS.
pnd
User
User
Posts: 10
Joined: Mon Feb 14, 2022 9:33 pm

Re: Retrieving coordinates with ScrollAreaGadget()

Post by pnd »

Netmaestro

I have the cursor coordinate portion of the application working. I am having a problem getting the image into the scrollareagadget. If it would not be too much trouble, could you provide a snipet showing how this is done? Any help appreciated.

Regards
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Retrieving coordinates with ScrollAreaGadget()

Post by netmaestro »

Ok, give this a try. Any questions, shoot.

Code: Select all

Declare Canvas_Handler()

CreateImage(0,400,400,24,#Cyan)
CreateImage(1,400,400,24,#Yellow)
OpenWindow(0,0,0,400,422,"",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)
CreateStatusBar(0, WindowID(0))

AddStatusBarField(50)
AddStatusBarField(20)
AddStatusBarField(20)
AddStatusBarField(40)
AddStatusBarField(20)
AddStatusBarField(40)

StatusBarText(0,0, "  Image:")
StatusBarText(0,2, "X:")
StatusBarText(0,4, "Y:")

ScrollAreaGadget(0,0,0,420,400,800,400)
  CanvasGadget(1,0,0,400,400)
  CanvasGadget(2,400,0,400,400)
CloseGadgetList()
  
SetGadgetAttribute(1, #PB_Canvas_Image, ImageID(0))
SetGadgetAttribute(2, #PB_Canvas_Image, ImageID(1))

BindGadgetEvent(1, @Canvas_Handler())
BindGadgetEvent(2, @Canvas_Handler())

Repeat:Until WaitWindowEvent() = #PB_Event_CloseWindow

Procedure Canvas_Handler()
  Select EventType()
      
    Case #PB_EventType_MouseEnter
      StatusBarText(0,1,Str(EventGadget()))
      
    Case #PB_EventType_MouseLeave
      StatusBarText(0,3,"")
      StatusBarText(0,5,"")
      
    Case #PB_EventType_MouseMove
      StatusBarText(0,3,Str(GetGadgetAttribute(EventGadget(),#PB_Canvas_MouseX)))
      StatusBarText(0,5,Str(GetGadgetAttribute(EventGadget(),#PB_Canvas_MouseY)))
      
    Case #PB_EventType_LeftButtonDown
      x=GetGadgetAttribute(EventGadget(),#PB_Canvas_MouseX)
      y=GetGadgetAttribute(EventGadget(),#PB_Canvas_MouseY)
      StartDrawing(CanvasOutput(EventGadget()))
        Box(x-2,y-2,4,4,#Red)
      StopDrawing()

  EndSelect
EndProcedure

BERESHEIT
pnd
User
User
Posts: 10
Joined: Mon Feb 14, 2022 9:33 pm

Re: Retrieving coordinates with ScrollAreaGadget()

Post by pnd »

Thanks Very Helpful

I added an openfilerequester statement. I'm trying to scroll around a single 5 MP image, but I keep getting an image not initialized error I'm including my mark-up below.

;#############################################################
;MASK Program Note uses the Current Version of PureBasic
;Code provided by Netmaestro by way of the Purebasic Forum
;#############################################################
Declare Canvas_Handler()

;CreateImage(0,400,400,24,#Cyan)
;CreateImage(1,400,400,24,#Yellow)
base.s= OpenFileRequester("Select Base Image:", "", "PNG |*.png", 0)
LoadImage(0,base.s)
OpenWindow(0,0,0,400,422,"",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)
CreateStatusBar(0, WindowID(0))

AddStatusBarField(50)
AddStatusBarField(20)
AddStatusBarField(20)
AddStatusBarField(40)
AddStatusBarField(20)
AddStatusBarField(40)

StatusBarText(0,0, " Image:")
StatusBarText(0,2, "X:")
StatusBarText(0,4, "Y:")

ScrollAreaGadget(0,0,0,420,400,800,400)
CanvasGadget(1,0,0,400,400)
;CanvasGadget(2,400,0,400,400)
CloseGadgetList()
Debug ImageID(1)
SetGadgetAttribute(1, #PB_Canvas_Image, ImageID(0))
;SetGadgetAttribute(2, #PB_Canvas_Image, ImageID(1))

BindGadgetEvent(1, @Canvas_Handler())
BindGadgetEvent(2, @Canvas_Handler())

Repeat:Until WaitWindowEvent() = #PB_Event_CloseWindow

Procedure Canvas_Handler()
Select EventType()

Case #PB_EventType_MouseEnter
StatusBarText(0,1,Str(EventGadget()))

Case #PB_EventType_MouseLeave
StatusBarText(0,3,"")
StatusBarText(0,5,"")

Case #PB_EventType_MouseMove
StatusBarText(0,3,Str(GetGadgetAttribute(EventGadget(),#PB_Canvas_MouseX)))
StatusBarText(0,5,Str(GetGadgetAttribute(EventGadget(),#PB_Canvas_MouseY)))

Case #PB_EventType_LeftButtonDown
x=GetGadgetAttribute(EventGadget(),#PB_Canvas_MouseX)
y=GetGadgetAttribute(EventGadget(),#PB_Canvas_MouseY)
StartDrawing(CanvasOutput(EventGadget()))
Box(x-2,y-2,4,4,#Red)
StopDrawing()

EndSelect
EndProcedure
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Retrieving coordinates with ScrollAreaGadget()

Post by netmaestro »

Did you employ the applicable decoder? e.g. UseJPEGImageDecoder()?
BERESHEIT
pnd
User
User
Posts: 10
Joined: Mon Feb 14, 2022 9:33 pm

Re: Retrieving coordinates with ScrollAreaGadget()

Post by pnd »

Embarrassed to say I forgot it, and saw it after I sent my last reply Everything is working now except I can't move the x axis scroll bar. Not sure why I can't pay outside of the window
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Retrieving coordinates with ScrollAreaGadget()

Post by netmaestro »

Possibly a sizing error somewhere? Gadget too large for the window?
BERESHEIT
pnd
User
User
Posts: 10
Joined: Mon Feb 14, 2022 9:33 pm

Re: Retrieving coordinates with ScrollAreaGadget()

Post by pnd »

Netmaestro

I'm new to the forum. Please tell me the correct way to list code when communicating with others on the forum. I don't think the last time I sent you some code was correct.
pnd
User
User
Posts: 10
Joined: Mon Feb 14, 2022 9:33 pm

Re: Retrieving coordinates with ScrollAreaGadget()

Post by pnd »

Netmaestro

Been over this several times and don't see where I went wrong would you take a look and tell me where the error is. I can't use the x axis scroll bar. The image I'm using is 5MP

Regards
#################################################################################
Declare Canvas_Handler()
UsePNGImageDecoder()
base.s= OpenFileRequester("Select Base Image:", "", "PNG |*.png", 0)
LoadImage(0,base.s)
OpenWindow(0,0,0,1200,700,"",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)
CreateStatusBar(0, WindowID(0))

AddStatusBarField(50)
AddStatusBarField(20)
AddStatusBarField(20)
AddStatusBarField(40)
AddStatusBarField(20)
AddStatusBarField(40)

StatusBarText(0,0, " Image:")
StatusBarText(0,2, "X:")
StatusBarText(0,4, "Y:")
Debug ImageWidth(0)
Debug ImageHeight(0)
iw1.i=ImageWidth(0)
ih1.i=ImageHeight(0)
Debug iw1.i
Debug ih1.i
ScrollAreaGadget(0,0,0,1200,700,iw1.i,ih1.i,10)
CanvasGadget(1,0,0,1200,700)
CloseGadgetList()

SetGadgetAttribute(1, #PB_Canvas_Image, ImageID(0))
BindGadgetEvent(1, @Canvas_Handler())
Repeat:Until WaitWindowEvent() = #PB_Event_CloseWindow

Procedure Canvas_Handler()
Select EventType()

Case #PB_EventType_MouseEnter
StatusBarText(0,1,Str(EventGadget()))

Case #PB_EventType_MouseLeave
StatusBarText(0,3,"")
StatusBarText(0,5,"")

Case #PB_EventType_MouseMove
StatusBarText(0,3,Str(GetGadgetAttribute(EventGadget(),#PB_Canvas_MouseX)))
StatusBarText(0,5,Str(GetGadgetAttribute(EventGadget(),#PB_Canvas_MouseY)))

Case #PB_EventType_LeftButtonDown
x=GetGadgetAttribute(EventGadget(),#PB_Canvas_MouseX)
y=GetGadgetAttribute(EventGadget(),#PB_Canvas_MouseY)
StartDrawing(CanvasOutput(EventGadget()))
Box(x-3,y-3,6,6,#Red)
Debug x
Debug y
StopDrawing()

EndSelect
EndProcedure
Post Reply