EnableGadgetTransformation - change gadget at runtime

Share your advanced PureBasic knowledge/code with the community.
User avatar
STARGÅTE
Addict
Addict
Posts: 2067
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

EnableGadgetTransformation - change gadget at runtime

Post by STARGÅTE »

It's a simple idea, but I think it has potential:
With the help of "small canvas gadgets" EnableGadgetTransformation() allows the user to move or resize any gadget at the runtime of the program. DisableGadgetTransformation() disables this functionality again.

Anyone who wants to test it, can execute the code below and activate the functionality with the button "Enable transformation".
EnableGadgetTransformation() wrote:
  • Gadget: the gadget number
  • Flags: a combination of #GadgetTransformation_Position, #GadgetTransformation_Horizontally and #GadgetTransformation_Vertically, to define the allowed transformations.
  • Grid: the size of the grid to lock a transformation.

Code: Select all

;- Include: GadgetTransformation

DeclareModule GadgetTransformation
   
   EnumerationBinary 1
      #GadgetTransformation_Position
      #GadgetTransformation_Horizontally
      #GadgetTransformation_Vertically
   EndEnumeration
   
   #GadgetTransformation_Size = #GadgetTransformation_Horizontally|#GadgetTransformation_Vertically
   #GadgetTransformation_All  = #GadgetTransformation_Position|#GadgetTransformation_Horizontally|#GadgetTransformation_Vertically
   
   Declare DisableGadgetTransformation(Gadget.i)
   Declare EnableGadgetTransformation(Gadget.i, Flags.i=#GadgetTransformation_All, Grid.i=1)
   
EndDeclareModule

Module GadgetTransformation
   
   EnableExplicit
   
   #HandelSize = 5
   
   Structure GadgetTransformation
      Gadget.i
      Handle.i[10]
      Grid.i
   EndStructure
   
   Structure DataBuffer
      Handle.i[10]
   EndStructure
   
   Global NewList GadgetTransformation.GadgetTransformation()
   
   Procedure.i GridMatch(Value.i, Grid.i, Max.i=$7FFFFFFF)
      Value = Round(Value/Grid, #PB_Round_Nearest)*Grid
      If Value > Max
         ProcedureReturn Max
      Else
         ProcedureReturn Value
      EndIf
   EndProcedure
   
   Procedure GadgetTransformation_Callback()
      Static Selected.i, X.i, Y.i, OffsetX.i, OffsetY.i, GadgetX0.i, GadgetX1.i, GadgetY0.i, GadgetY1.i
      Protected *GadgetTransformation.GadgetTransformation = GetGadgetData(EventGadget())
      With *GadgetTransformation
         Select EventType()
            Case #PB_EventType_LeftButtonDown
               Selected = #True
               OffsetX = GetGadgetAttribute(EventGadget(), #PB_Canvas_MouseX)
               OffsetY = GetGadgetAttribute(EventGadget(), #PB_Canvas_MouseY)
               GadgetX0 = GadgetX(\Gadget)
               GadgetX1 = GadgetX0 + GadgetWidth(\Gadget)
               GadgetY0 = GadgetY(\Gadget)
               GadgetY1 = GadgetY0 + GadgetHeight(\Gadget)
            Case #PB_EventType_LeftButtonUp
               Selected = #False
            Case #PB_EventType_MouseMove
               If Selected
                  X = WindowMouseX(GetActiveWindow())-OffsetX
                  Y = WindowMouseY(GetActiveWindow())-OffsetY
                  Select EventGadget()
                     Case \Handle[1]
                        ResizeGadget(\Gadget, GridMatch(X+#HandelSize, \Grid, GadgetX1), #PB_Ignore, GadgetX1-GridMatch(X+#HandelSize, \Grid, GadgetX1), GridMatch(Y, \Grid)-GadgetY0)
                     Case \Handle[2]
                        ResizeGadget(\Gadget, #PB_Ignore, #PB_Ignore, #PB_Ignore, GridMatch(Y, \Grid)-GadgetY0)
                     Case \Handle[3]
                        ResizeGadget(\Gadget, #PB_Ignore, #PB_Ignore, GridMatch(X, \Grid)-GadgetX0, GridMatch(Y, \Grid)-GadgetY0)
                     Case \Handle[4]
                        ResizeGadget(\Gadget, GridMatch(X+#HandelSize, \Grid, GadgetX1), #PB_Ignore, GadgetX1-GridMatch(X+#HandelSize, \Grid, GadgetX1), #PB_Ignore)
                     Case \Handle[5]
                        ResizeGadget(\Gadget, GridMatch(X-#HandelSize, \Grid), GridMatch(Y+#HandelSize, \Grid), #PB_Ignore, #PB_Ignore)
                     Case \Handle[6]
                        ResizeGadget(\Gadget, #PB_Ignore, #PB_Ignore, GridMatch(X, \Grid)-GadgetX0, #PB_Ignore)
                     Case \Handle[7]
                        ResizeGadget(\Gadget, GridMatch(X+#HandelSize, \Grid, GadgetX1), GridMatch(Y+#HandelSize, \Grid, GadgetY1), GadgetX1-GridMatch(X+#HandelSize, \Grid, GadgetX1), GadgetY1-GridMatch(Y+#HandelSize, \Grid, GadgetY1))
                     Case \Handle[8]
                        ResizeGadget(\Gadget, #PB_Ignore, GridMatch(Y+#HandelSize, \Grid, GadgetY1), #PB_Ignore, GadgetY1-GridMatch(Y+#HandelSize, \Grid, GadgetY1))
                     Case \Handle[9]
                        ResizeGadget(\Gadget, #PB_Ignore, GridMatch(Y+#HandelSize, \Grid, GadgetY1), GridMatch(X, \Grid)-GadgetX0, GadgetY1-GridMatch(Y+#HandelSize, \Grid, GadgetY1))
                  EndSelect
                  If \Handle[1]
                     ResizeGadget(\Handle[1], GadgetX(\Gadget)-#HandelSize, GadgetY(\Gadget)+GadgetHeight(\Gadget), #PB_Ignore, #PB_Ignore)
                  EndIf
                  If \Handle[2]
                     ResizeGadget(\Handle[2], GadgetX(\Gadget)+(GadgetWidth(\Gadget)-#HandelSize)/2, GadgetY(\Gadget)+GadgetHeight(\Gadget), #PB_Ignore, #PB_Ignore)
                  EndIf
                  If \Handle[3]
                     ResizeGadget(\Handle[3], GadgetX(\Gadget)+GadgetWidth(\Gadget), GadgetY(\Gadget)+GadgetHeight(\Gadget), #PB_Ignore, #PB_Ignore)
                  EndIf
                  If \Handle[4]
                     ResizeGadget(\Handle[4], GadgetX(\Gadget)-#HandelSize, GadgetY(\Gadget)+(GadgetHeight(\Gadget)-#HandelSize)/2, #PB_Ignore, #PB_Ignore)
                  EndIf
                  If \Handle[5]
                     ResizeGadget(\Handle[5], GadgetX(\Gadget)+#HandelSize, GadgetY(\Gadget)-#HandelSize, #PB_Ignore, #PB_Ignore)
                  EndIf
                  If \Handle[6]
                     ResizeGadget(\Handle[6], GadgetX(\Gadget)+GadgetWidth(\Gadget), GadgetY(\Gadget)+(GadgetHeight(\Gadget)-#HandelSize)/2, #PB_Ignore, #PB_Ignore)
                  EndIf
                  If \Handle[7]
                     ResizeGadget(\Handle[7], GadgetX(\Gadget)-#HandelSize, GadgetY(\Gadget)-#HandelSize, #PB_Ignore, #PB_Ignore)
                  EndIf
                  If \Handle[8]
                     ResizeGadget(\Handle[8], GadgetX(\Gadget)+(GadgetWidth(\Gadget)-#HandelSize)/2, GadgetY(\Gadget)-#HandelSize, #PB_Ignore, #PB_Ignore)
                  EndIf
                  If \Handle[9]
                     ResizeGadget(\Handle[9], GadgetX(\Gadget)+GadgetWidth(\Gadget), GadgetY(\Gadget)-#HandelSize, #PB_Ignore, #PB_Ignore)
                  EndIf
               EndIf
         EndSelect
      EndWith
   EndProcedure
   
   Procedure DisableGadgetTransformation(Gadget.i)
      Protected I.i, *GadgetTransformation.GadgetTransformation
      ForEach GadgetTransformation()
         If GadgetTransformation()\Gadget = Gadget
            For I = 1 To 9
               If GadgetTransformation()\Handle[I]
                  FreeGadget(GadgetTransformation()\Handle[I])
               EndIf
            Next
            DeleteElement(GadgetTransformation())
         EndIf
      Next
   EndProcedure
   
   Procedure EnableGadgetTransformation(Gadget.i, Flags.i=#GadgetTransformation_All, Grid.i=1)
      Protected Handle.i, I.i
      Protected *GadgetTransformation.GadgetTransformation
      Protected *Cursors.DataBuffer = ?Cursors
      Protected *Flags.DataBuffer = ?Flags
      ForEach GadgetTransformation()
         If GadgetTransformation()\Gadget = Gadget
            For I = 1 To 9
               If GadgetTransformation()\Handle[I]
                  FreeGadget(GadgetTransformation()\Handle[I])
               EndIf
            Next
            DeleteElement(GadgetTransformation())
         EndIf
      Next
      *GadgetTransformation = AddElement(GadgetTransformation())
      *GadgetTransformation\Gadget = Gadget
      *GadgetTransformation\Grid = Grid
      For I = 1 To 9
         If Flags & *Flags\Handle[I] = *Flags\Handle[I]
            Select I
               Case 1
                  Handle = CanvasGadget(#PB_Any, GadgetX(Gadget)-#HandelSize, GadgetY(Gadget)+GadgetHeight(Gadget), #HandelSize, #HandelSize)
               Case 2
                  Handle = CanvasGadget(#PB_Any, GadgetX(Gadget)+(GadgetWidth(Gadget)-#HandelSize)/2, GadgetY(Gadget)+GadgetHeight(Gadget), #HandelSize, #HandelSize)
               Case 3
                  Handle = CanvasGadget(#PB_Any, GadgetX(Gadget)+GadgetWidth(Gadget), GadgetY(Gadget)+GadgetHeight(Gadget), #HandelSize, #HandelSize)
               Case 4
                  Handle = CanvasGadget(#PB_Any, GadgetX(Gadget)-#HandelSize, GadgetY(Gadget)+(GadgetHeight(Gadget)-#HandelSize)/2, #HandelSize, #HandelSize)
               Case 5
                  Handle = CanvasGadget(#PB_Any, GadgetX(Gadget)+#HandelSize, GadgetY(Gadget)-#HandelSize, 2*#HandelSize, #HandelSize)
               Case 6
                  Handle = CanvasGadget(#PB_Any, GadgetX(Gadget)+GadgetWidth(Gadget), GadgetY(Gadget)+(GadgetHeight(Gadget)-#HandelSize)/2, #HandelSize, #HandelSize)
               Case 7
                  Handle = CanvasGadget(#PB_Any, GadgetX(Gadget)-#HandelSize, GadgetY(Gadget)-#HandelSize, #HandelSize, #HandelSize)
               Case 8
                  Handle = CanvasGadget(#PB_Any, GadgetX(Gadget)+(GadgetWidth(Gadget)-#HandelSize)/2, GadgetY(Gadget)-#HandelSize, #HandelSize, #HandelSize)
               Case 9
                  Handle = CanvasGadget(#PB_Any, GadgetX(Gadget)+GadgetWidth(Gadget), GadgetY(Gadget)-#HandelSize, #HandelSize, #HandelSize)
            EndSelect
            *GadgetTransformation\Handle[I] = Handle
            SetGadgetData(Handle, *GadgetTransformation)
            SetGadgetAttribute(Handle, #PB_Canvas_Cursor, *Cursors\Handle[I])
            If StartDrawing(CanvasOutput(Handle))
               Box(0, 0, OutputWidth(), OutputHeight(), $000000)
               Box(1, 1, OutputWidth()-2, OutputHeight()-2, $FFFFFF)
               StopDrawing()
            EndIf
            BindGadgetEvent(Handle, @GadgetTransformation_Callback())
         EndIf
      Next
      DataSection
         Cursors:
         Data.i 0, #PB_Cursor_LeftDownRightUp, #PB_Cursor_UpDown, #PB_Cursor_LeftUpRightDown, #PB_Cursor_LeftRight
         Data.i #PB_Cursor_Arrows, #PB_Cursor_LeftRight, #PB_Cursor_LeftUpRightDown, #PB_Cursor_UpDown, #PB_Cursor_LeftDownRightUp
         Flags:
         Data.i 0, #GadgetTransformation_Size, #GadgetTransformation_Vertically, #GadgetTransformation_Size, #GadgetTransformation_Horizontally
         Data.i #GadgetTransformation_Position, #GadgetTransformation_Horizontally, #GadgetTransformation_Size, #GadgetTransformation_Vertically, #GadgetTransformation_Size
      EndDataSection
   EndProcedure
   
EndModule



;- Example code

UseModule GadgetTransformation

Enumeration
   #Window
   #GadgetTransformation
   #EditorGadget
   #ButtonGadget
   #TrackBarGadget
   #SpinGadget
EndEnumeration

OpenWindow(#Window, 0, 0, 600, 400, "WindowTitle", #PB_Window_MinimizeGadget|#PB_Window_ScreenCentered)
EditorGadget(#EditorGadget, 50, 100, 200, 50, #PB_Editor_WordWrap) : SetGadgetText(#EditorGadget, "Grumpy wizards make toxic brew for the evil Queen and Jack.")
ButtonGadget(#ButtonGadget, 50, 250, 200, 25, "Hallo Welt!", #PB_Button_MultiLine)
TrackBarGadget(#TrackBarGadget, 350, 100, 200, 25, 0, 100) : SetGadgetState(#TrackBarGadget, 70)
SpinGadget(#SpinGadget, 350, 250, 200, 25, 0, 100, #PB_Spin_Numeric) : SetGadgetState(#SpinGadget, 70)

ButtonGadget(#GadgetTransformation, 20, 20, 150, 25, "Enable Transformation", #PB_Button_Toggle)

Repeat
   
   Select WaitWindowEvent()
         
      Case #PB_Event_CloseWindow
         End
         
      Case #PB_Event_Gadget
         Select EventGadget()
            Case #GadgetTransformation
               Select GetGadgetState(#GadgetTransformation)
                  Case #False
                     SetGadgetText(#GadgetTransformation, "Enable Transformation")
                     DisableGadgetTransformation(#EditorGadget)
                     DisableGadgetTransformation(#ButtonGadget)
                     DisableGadgetTransformation(#TrackBarGadget)
                     DisableGadgetTransformation(#SpinGadget)
                  Case #True
                     SetGadgetText(#GadgetTransformation, "Disable Transformation")
                     EnableGadgetTransformation(#EditorGadget, #GadgetTransformation_All, 10)
                     EnableGadgetTransformation(#ButtonGadget, #GadgetTransformation_All)
                     EnableGadgetTransformation(#TrackBarGadget, #GadgetTransformation_Position|#GadgetTransformation_Horizontally)
                     EnableGadgetTransformation(#SpinGadget, #GadgetTransformation_Position)
               EndSelect
         EndSelect
         
         
   EndSelect
   
ForEver
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: EnableGadgetTransformation - change gadget at runtime

Post by infratec »

:thumbup

I don't no for what I can use it, but ....

Well done :!:

Bernd
User avatar
Kurzer
Enthusiast
Enthusiast
Posts: 664
Joined: Sun Jun 11, 2006 12:07 am
Location: Near Hamburg

Re: EnableGadgetTransformation - change gadget at runtime

Post by Kurzer »

Thats an awesome module, STARGÅTE! Great work. Image
Are you working on a new Formdesigner? :wink:
PB 6.02 x64, OS: Win 7 Pro x64 & Win 11 x64, Desktopscaling: 125%, CPU: I7 6500, RAM: 16 GB, GPU: Intel Graphics HD 520, User age in 2023: 56y
"Happiness is a pet." | "Never run a changing system!"
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: EnableGadgetTransformation - change gadget at runtime

Post by davido »

@STARGÅTE,
Brilliant idea.

I had the same thought as kurzer ...
It would be great if one could save the transformation; even better if one could dynamically add canvas based gadgets.

Thank you for sharing. :D

Tested on MacBook Pro i7.
DE AA EB
User avatar
STARGÅTE
Addict
Addict
Posts: 2067
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: EnableGadgetTransformation - change gadget at runtime

Post by STARGÅTE »

"save the transformation" ?
You can use GadgetX/Y() and GadgetWidth/Height() to get this informations.

"even better if one could dynamically add canvas based gadgets." ?
I don't want to create a new form designer. (also an answer for kurzer)
Adding gadgets dynamically is already possible.
You can use some think like a popup menu and the #PB_Any constant.
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: EnableGadgetTransformation - change gadget at runtime

Post by davido »

@STARGÅTE,

Thank you for your advice. It is much appreciated.
DE AA EB
User avatar
Kukulkan
Addict
Addict
Posts: 1352
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

Re: EnableGadgetTransformation - change gadget at runtime

Post by Kukulkan »

Cool! Nice code :D
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5342
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: EnableGadgetTransformation - change gadget at runtime

Post by Kwai chang caine »

Super code :shock:
Step by step, with this one and also Mestnyi codes, it's soon possible to create new designer like a puzzle :D
Thanks for sharing 8)
ImageThe happiness is a road...
Not a destination
User avatar
idle
Always Here
Always Here
Posts: 5040
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: EnableGadgetTransformation - change gadget at runtime

Post by idle »

That's a pretty neat solution for runtime changes, thanks for sharing.
Windows 11, Manjaro, Raspberry Pi OS
Image
Tranquil
Addict
Addict
Posts: 949
Joined: Mon Apr 28, 2003 2:22 pm
Location: Europe

Re: EnableGadgetTransformation - change gadget at runtime

Post by Tranquil »

Nice solution, without bloated API calls. Thanks for sharing it!
Tranquil
User avatar
Andre
PureBasic Team
PureBasic Team
Posts: 2056
Joined: Fri Apr 25, 2003 6:14 pm
Location: Germany (Saxony, Deutscheinsiedel)
Contact:

Re: EnableGadgetTransformation - change gadget at runtime

Post by Andre »

Very good, thank you!
Bye,
...André
(PureBasicTeam::Docs & Support - PureArea.net | Order:: PureBasic | PureVisionXP)
User avatar
Michael Vogel
Addict
Addict
Posts: 2666
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: EnableGadgetTransformation - change gadget at runtime

Post by Michael Vogel »

Thank you André for this great invention - it could mean a big change in developing dialogs!

It could even work in a complete multi-window program, I just added the following procedures to do so:

Code: Select all

#MaxGadgetID=999

Procedure SetGadgetTransformation(Gadget.i,Flags.i=#GadgetTransformation_All,Grid.i=1)
	If GadgetTransformationMode
		EnableGadgetTransformation(Gadget,Flags,Grid)
	Else
		DisableGadgetTransformation(Gadget.i)
	EndIf
EndProcedure
	
Procedure SwapGadgetTransformationMode()
		
	Protected i
	Protected ActiveWindow
		
	ActiveWindow=WindowID(GetActiveWindow())
	UseGadgetList(ActiveWindow)
		
	GadgetTransformationMode!1
		
	For i=1 To #MaxGadgetID
		If IsGadget(i)
			If GetParent_(GadgetID(i))=ActiveWindow
				SetGadgetTransformation(i,#GadgetTransformation_All,10)
			EndIf
		EndIf
	Next i
		
EndProcedure
Now you can also use SwapGadgetTransformationMode() within any event loop of your program to enable/disable the transformation for the active window.

Theoretically, the source code could be scanned to find the responsible line for each gadget and the size parameters adapted on the fly as well...

Code: Select all

CompilerIf #PB_Compiler_Debugger

		Global NumMax
		Global Dim Num.s(NumMax)
		Procedure InitGadgetSourceCode()

			Protected s.s

			If ReadFile(0,#PB_Compiler_File)
				While Eof(0)=0
					s=Trim(Trim(ReadString(0)),#TAB$)
					If Left(s,11)="Enumeration"
						n=0
						scan=1
					ElseIf Left(s,14)="EndEnumeration"
						scan=0
					ElseIf scan And PeekA(@s)='#'
						; Debug s+" = "+Str(n)
						If n>NumMax
							NumMax=n
							ReDim Num(NumMax)
						EndIf
						Num(n)+s
						n+1
					EndIf
				Wend
				CloseFile(0)
			EndIf
		EndProcedure
		Procedure GetGadgetSourceCode(n)

			Protected s.s,t.s
			Protected p,line

			If n<=NumMax
				If ReadFile(0,#PB_Compiler_File)
					While Eof(0)=0
						line+1
						s=Trim(Trim(ReadString(0)),#TAB$)
						p=FindString(s,"Gadget(")
						If p
							t=StringField(Mid(s,p+7),1,",")
							If PeekA(@t)='#' And t<>"#PB_Any"
								If FindString(Num(n)+"#",t+"#")
									Debug Str(line)+": "+s
									; ProcedureReturn line
								EndIf
							EndIf
						EndIf

					Wend
					CloseFile(0)
				EndIf
			EndIf

		EndProcedure

		InitGadgetSourceCode()
		GetGadgetSourceCode(1); Example to get source code line for Gadget #1
		
	CompilerEndIf
I think it would be great, if the PB IDE would allow to do something like this in debugger mode.


Just for an easy check, here's the merge of all sources (Andrés example):

Code: Select all

;- Include: GadgetTransformation

DeclareModule GadgetTransformation

	#GadgetStatus=999

	EnumerationBinary 1
		#GadgetTransformation_Position
		#GadgetTransformation_Horizontally
		#GadgetTransformation_Vertically
	EndEnumeration

	#GadgetTransformation_Size = #GadgetTransformation_Horizontally|#GadgetTransformation_Vertically
	#GadgetTransformation_All  = #GadgetTransformation_Position|#GadgetTransformation_Horizontally|#GadgetTransformation_Vertically

	Declare DisableGadgetTransformation(Gadget.i)
	Declare EnableGadgetTransformation(Gadget.i, Flags.i=#GadgetTransformation_All, Grid.i=1)
	Declare SetGadgetTransformation(Gadget.i, Flags.i=#GadgetTransformation_All, Grid.i=1)
	Declare SwapGadgetTransformationMode()

	Global GadgetTransformationMode.i

EndDeclareModule
Module GadgetTransformation

	EnableExplicit

	#HandelSize = 5

	Structure GadgetTransformation
		Gadget.i
		Handle.i[10]
		Grid.i
	EndStructure

	Structure DataBuffer
		Handle.i[10]
	EndStructure

	Global NewList GadgetTransformation.GadgetTransformation()


	Global NumMax
	Global Dim Num.s(NumMax)

	Procedure InitGadgetSourceCode()

		CompilerIf #PB_Compiler_Debugger

			Protected s.s
			Protected n,scan

			If ReadFile(0,#PB_Compiler_File)
				While Eof(0)=0
					s=Trim(Trim(ReadString(0)),#TAB$)
					If Left(s,11)="Enumeration"
						n=0
						scan=1
					ElseIf Left(s,14)="EndEnumeration"
						scan=0
					ElseIf scan And PeekA(@s)='#'
						; Debug s+" = "+Str(n)
						If n>NumMax
							NumMax=n
							ReDim Num(NumMax)
						EndIf
						Num(n)+s
						n+1
					EndIf
				Wend
				CloseFile(0)
			EndIf

		CompilerEndIf

	EndProcedure
	Procedure.s GetGadgetSourceCode(n)

		Protected s.s,t.s
		Protected p,line

		CompilerIf #PB_Compiler_Debugger

			If n<=NumMax
				If ReadFile(0,#PB_Compiler_File)
					While Eof(0)=0
						line+1
						s=Trim(Trim(ReadString(0)),#TAB$)
						p=FindString(s,"Gadget(")
						If p
							t=StringField(Mid(s,p+7),1,",")
							If PeekA(@t)='#' And t<>"#PB_Any"
								If FindString(Num(n)+"#",t+"#")
									ProcedureReturn " #"+Str(line)+": "+s
									; ProcedureReturn line
								EndIf
							EndIf
						EndIf

					Wend
					CloseFile(0)
				EndIf
			EndIf

		CompilerEndIf

		ProcedureReturn " :("

	EndProcedure

	InitGadgetSourceCode()

	Procedure.i GridMatch(Value.i, Grid.i, NumMax.i=$7FFFFFFF)
		Value = Round(Value/Grid, #PB_Round_Nearest)*Grid
		If Value > NumMax
			ProcedureReturn NumMax
		Else
			ProcedureReturn Value
		EndIf
	EndProcedure
	Procedure GadgetTransformation_Callback()

		Static Selected.i, X.i, Y.i, OffsetX.i, OffsetY.i, GadgetX0.i, GadgetX1.i, GadgetY0.i, GadgetY1.i
		Protected *GadgetTransformation.GadgetTransformation = GetGadgetData(EventGadget())
		Protected s.s

		With *GadgetTransformation
			
			s=GetGadgetSourceCode(\Gadget)
			x=FindString(s,",")
			y=FindString(s,",",FindString(s,",",FindString(s,",",FindString(s,",",x+1)+1)+1)+1)
SetGadgetText(#GadgetStatus,Left(s,x)+Str(GadgetX(\Gadget))+","+Str(GadgetY(\Gadget))+","+Str(GadgetWidth(\Gadget))+","+Str(GadgetHeight(\Gadget))+Mid(s,y))

			Select EventType()
			Case #PB_EventType_LeftButtonDown
				Selected = #True
				OffsetX = GetGadgetAttribute(EventGadget(), #PB_Canvas_MouseX)
				OffsetY = GetGadgetAttribute(EventGadget(), #PB_Canvas_MouseY)
				GadgetX0 = GadgetX(\Gadget)
				GadgetX1 = GadgetX0 + GadgetWidth(\Gadget)
				GadgetY0 = GadgetY(\Gadget)
				GadgetY1 = GadgetY0 + GadgetHeight(\Gadget)
			Case #PB_EventType_LeftButtonUp
				Selected = #False
			Case #PB_EventType_MouseMove
				If Selected
					X = WindowMouseX(GetActiveWindow())-OffsetX
					Y = WindowMouseY(GetActiveWindow())-OffsetY
					Select EventGadget()
					Case \Handle[1]
						ResizeGadget(\Gadget, GridMatch(X+#HandelSize, \Grid, GadgetX1), #PB_Ignore, GadgetX1-GridMatch(X+#HandelSize, \Grid, GadgetX1), GridMatch(Y, \Grid)-GadgetY0)
					Case \Handle[2]
						ResizeGadget(\Gadget, #PB_Ignore, #PB_Ignore, #PB_Ignore, GridMatch(Y, \Grid)-GadgetY0)
					Case \Handle[3]
						ResizeGadget(\Gadget, #PB_Ignore, #PB_Ignore, GridMatch(X, \Grid)-GadgetX0, GridMatch(Y, \Grid)-GadgetY0)
					Case \Handle[4]
						ResizeGadget(\Gadget, GridMatch(X+#HandelSize, \Grid, GadgetX1), #PB_Ignore, GadgetX1-GridMatch(X+#HandelSize, \Grid, GadgetX1), #PB_Ignore)
					Case \Handle[5]
						ResizeGadget(\Gadget, GridMatch(X-#HandelSize, \Grid), GridMatch(Y+#HandelSize, \Grid), #PB_Ignore, #PB_Ignore)
					Case \Handle[6]
						ResizeGadget(\Gadget, #PB_Ignore, #PB_Ignore, GridMatch(X, \Grid)-GadgetX0, #PB_Ignore)
					Case \Handle[7]
						ResizeGadget(\Gadget, GridMatch(X+#HandelSize, \Grid, GadgetX1), GridMatch(Y+#HandelSize, \Grid, GadgetY1), GadgetX1-GridMatch(X+#HandelSize, \Grid, GadgetX1), GadgetY1-GridMatch(Y+#HandelSize, \Grid, GadgetY1))
					Case \Handle[8]
						ResizeGadget(\Gadget, #PB_Ignore, GridMatch(Y+#HandelSize, \Grid, GadgetY1), #PB_Ignore, GadgetY1-GridMatch(Y+#HandelSize, \Grid, GadgetY1))
					Case \Handle[9]
						ResizeGadget(\Gadget, #PB_Ignore, GridMatch(Y+#HandelSize, \Grid, GadgetY1), GridMatch(X, \Grid)-GadgetX0, GadgetY1-GridMatch(Y+#HandelSize, \Grid, GadgetY1))
					EndSelect
					If \Handle[1]
						ResizeGadget(\Handle[1], GadgetX(\Gadget)-#HandelSize, GadgetY(\Gadget)+GadgetHeight(\Gadget), #PB_Ignore, #PB_Ignore)
					EndIf
					If \Handle[2]
						ResizeGadget(\Handle[2], GadgetX(\Gadget)+(GadgetWidth(\Gadget)-#HandelSize)/2, GadgetY(\Gadget)+GadgetHeight(\Gadget), #PB_Ignore, #PB_Ignore)
					EndIf
					If \Handle[3]
						ResizeGadget(\Handle[3], GadgetX(\Gadget)+GadgetWidth(\Gadget), GadgetY(\Gadget)+GadgetHeight(\Gadget), #PB_Ignore, #PB_Ignore)
					EndIf
					If \Handle[4]
						ResizeGadget(\Handle[4], GadgetX(\Gadget)-#HandelSize, GadgetY(\Gadget)+(GadgetHeight(\Gadget)-#HandelSize)/2, #PB_Ignore, #PB_Ignore)
					EndIf
					If \Handle[5]
						ResizeGadget(\Handle[5], GadgetX(\Gadget)+#HandelSize, GadgetY(\Gadget)-#HandelSize, #PB_Ignore, #PB_Ignore)
					EndIf
					If \Handle[6]
						ResizeGadget(\Handle[6], GadgetX(\Gadget)+GadgetWidth(\Gadget), GadgetY(\Gadget)+(GadgetHeight(\Gadget)-#HandelSize)/2, #PB_Ignore, #PB_Ignore)
					EndIf
					If \Handle[7]
						ResizeGadget(\Handle[7], GadgetX(\Gadget)-#HandelSize, GadgetY(\Gadget)-#HandelSize, #PB_Ignore, #PB_Ignore)
					EndIf
					If \Handle[8]
						ResizeGadget(\Handle[8], GadgetX(\Gadget)+(GadgetWidth(\Gadget)-#HandelSize)/2, GadgetY(\Gadget)-#HandelSize, #PB_Ignore, #PB_Ignore)
					EndIf
					If \Handle[9]
						ResizeGadget(\Handle[9], GadgetX(\Gadget)+GadgetWidth(\Gadget), GadgetY(\Gadget)-#HandelSize, #PB_Ignore, #PB_Ignore)
					EndIf
				EndIf
			EndSelect
		EndWith

	EndProcedure
	Procedure DisableGadgetTransformation(Gadget.i)
		Protected I.i, *GadgetTransformation.GadgetTransformation
		ForEach GadgetTransformation()
			If GadgetTransformation()\Gadget = Gadget
				For I = 1 To 9
					If GadgetTransformation()\Handle[I]
						FreeGadget(GadgetTransformation()\Handle[I])
					EndIf
				Next
				DeleteElement(GadgetTransformation())
			EndIf
		Next
	EndProcedure
	Procedure EnableGadgetTransformation(Gadget.i, Flags.i=#GadgetTransformation_All, Grid.i=1)

		Protected Handle.i, I.i
		Protected *GadgetTransformation.GadgetTransformation
		Protected *Cursors.DataBuffer = ?Cursors
		Protected *Flags.DataBuffer = ?Flags

		ForEach GadgetTransformation()
			If GadgetTransformation()\Gadget = Gadget
				For I = 1 To 9
					If GadgetTransformation()\Handle[I]
						FreeGadget(GadgetTransformation()\Handle[I])
					EndIf
				Next
				DeleteElement(GadgetTransformation())
			EndIf
		Next

		*GadgetTransformation = AddElement(GadgetTransformation())
		*GadgetTransformation\Gadget = Gadget
		*GadgetTransformation\Grid = Grid

		For I = 1 To 9
			If Flags & *Flags\Handle[I] = *Flags\Handle[I]
				Select I
				Case 1
					Handle = CanvasGadget(#PB_Any, GadgetX(Gadget)-#HandelSize, GadgetY(Gadget)+GadgetHeight(Gadget), #HandelSize, #HandelSize)
				Case 2
					Handle = CanvasGadget(#PB_Any, GadgetX(Gadget)+(GadgetWidth(Gadget)-#HandelSize)/2, GadgetY(Gadget)+GadgetHeight(Gadget), #HandelSize, #HandelSize)
				Case 3
					Handle = CanvasGadget(#PB_Any, GadgetX(Gadget)+GadgetWidth(Gadget), GadgetY(Gadget)+GadgetHeight(Gadget), #HandelSize, #HandelSize)
				Case 4
					Handle = CanvasGadget(#PB_Any, GadgetX(Gadget)-#HandelSize, GadgetY(Gadget)+(GadgetHeight(Gadget)-#HandelSize)/2, #HandelSize, #HandelSize)
				Case 5
					Handle = CanvasGadget(#PB_Any, GadgetX(Gadget)+#HandelSize, GadgetY(Gadget)-#HandelSize, 2*#HandelSize, #HandelSize)
				Case 6
					Handle = CanvasGadget(#PB_Any, GadgetX(Gadget)+GadgetWidth(Gadget), GadgetY(Gadget)+(GadgetHeight(Gadget)-#HandelSize)/2, #HandelSize, #HandelSize)
				Case 7
					Handle = CanvasGadget(#PB_Any, GadgetX(Gadget)-#HandelSize, GadgetY(Gadget)-#HandelSize, #HandelSize, #HandelSize)
				Case 8
					Handle = CanvasGadget(#PB_Any, GadgetX(Gadget)+(GadgetWidth(Gadget)-#HandelSize)/2, GadgetY(Gadget)-#HandelSize, #HandelSize, #HandelSize)
				Case 9
					Handle = CanvasGadget(#PB_Any, GadgetX(Gadget)+GadgetWidth(Gadget), GadgetY(Gadget)-#HandelSize, #HandelSize, #HandelSize)
				EndSelect

				*GadgetTransformation\Handle[I] = Handle
				SetGadgetData(Handle, *GadgetTransformation)
				SetGadgetAttribute(Handle, #PB_Canvas_Cursor, *Cursors\Handle[I])

				If StartDrawing(CanvasOutput(Handle))
					Box(0, 0, OutputWidth(), OutputHeight(), $000000)
					Box(1, 1, OutputWidth()-2, OutputHeight()-2, $FFFFFF)
					StopDrawing()
				EndIf

				BindGadgetEvent(Handle, @GadgetTransformation_Callback())

			EndIf
		Next

		DataSection
			Cursors:
			Data.i 0, #PB_Cursor_LeftDownRightUp, #PB_Cursor_UpDown, #PB_Cursor_LeftUpRightDown, #PB_Cursor_LeftRight
			Data.i #PB_Cursor_Arrows, #PB_Cursor_LeftRight, #PB_Cursor_LeftUpRightDown, #PB_Cursor_UpDown, #PB_Cursor_LeftDownRightUp
			Flags:
			Data.i 0, #GadgetTransformation_Size, #GadgetTransformation_Vertically, #GadgetTransformation_Size, #GadgetTransformation_Horizontally
			Data.i #GadgetTransformation_Position, #GadgetTransformation_Horizontally, #GadgetTransformation_Size, #GadgetTransformation_Vertically, #GadgetTransformation_Size
		EndDataSection

	EndProcedure

	#NumMaxGadgetID=999

	Procedure SetGadgetTransformation(Gadget.i,Flags.i=#GadgetTransformation_All,Grid.i=1)

		If GadgetTransformationMode
			EnableGadgetTransformation(Gadget,Flags,Grid)
		Else
			DisableGadgetTransformation(Gadget.i)
		EndIf

	EndProcedure
	Procedure SwapGadgetTransformationMode()

		Protected i
		Protected ActiveWindow

		ActiveWindow=WindowID(GetActiveWindow())
		UseGadgetList(ActiveWindow)

		GadgetTransformationMode!1

		For i=1 To #NumMaxGadgetID
			If IsGadget(i) And i<>#GadgetStatus
				If GetParent_(GadgetID(i))=ActiveWindow
					SetGadgetTransformation(i,#GadgetTransformation_All,10)
				EndIf
			EndIf
		Next i

	EndProcedure


EndModule

UseModule GadgetTransformation

	;- Example code


	Enumeration
		#Window
		#GadgetTransformation
		#EditorGadget
		#ButtonGadget
		#TrackBarGadget
		#SpinGadget
	EndEnumeration

	OpenWindow(#Window, 0, 0, 600, 400, "WindowTitle", #PB_Window_MinimizeGadget|#PB_Window_ScreenCentered)
	EditorGadget(#EditorGadget, 50, 100, 200, 50, #PB_Editor_WordWrap) : SetGadgetText(#EditorGadget, "Grumpy wizards make toxic brew for the evil Queen and Jack.")
	ButtonGadget(#ButtonGadget, 50, 250, 200, 25, "Hallo Welt!", #PB_Button_MultiLine)
	TrackBarGadget(#TrackBarGadget, 350, 100, 200, 25, 0, 100) : SetGadgetState(#TrackBarGadget, 70)
	SpinGadget(#SpinGadget, 350, 250, 200, 25, 0, 100, #PB_Spin_Numeric) : SetGadgetState(#SpinGadget, 70)
	TextGadget(#GadgetStatus,0,380,600,20,"")

	ButtonGadget(#GadgetTransformation, 20, 20, 150, 25, "Enable Transformation", #PB_Button_Toggle)

	Repeat

		Select WaitWindowEvent()

		Case #PB_Event_CloseWindow
			End

		Case #PB_Event_Gadget
			Select EventGadget()
			Case #GadgetTransformation
				SwapGadgetTransformationMode()
			EndSelect
		EndSelect

	ForEver

UnuseModule GadgetTransformation
User avatar
Andre
PureBasic Team
PureBasic Team
Posts: 2056
Joined: Fri Apr 25, 2003 6:14 pm
Location: Germany (Saxony, Deutscheinsiedel)
Contact:

Re: EnableGadgetTransformation - change gadget at runtime

Post by Andre »

@Michael:
The "flowers" must go to STARGÅTE for this code example! :mrgreen:
But thanks for further extending the initial code :D

I've also tested it successfully on MacOS 10.6.8 and PB5.42 beta1 - with one exception: I needed to comment out the line

Code: Select all

            ;If GetParent_(GadgetID(i))=ActiveWindow    ; TODO: don't work on MacOS, as it's WinAPI (really needed?)
in the SwapGadgetTransformationMode() procedure, as this is WinAPI. Maybe there is an MacOS equivalent, which can be used for the same function?
Bye,
...André
(PureBasicTeam::Docs & Support - PureArea.net | Order:: PureBasic | PureVisionXP)
User avatar
Michael Vogel
Addict
Addict
Posts: 2666
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: EnableGadgetTransformation - change gadget at runtime

Post by Michael Vogel »

Oy - sorry, Stargåte, thanks!

Meantime I am able to show a demo code (windows only) with some modifications, which demonstrates what could be the next step: a interpreter enhancement, which allows to modify all gadgets at runtime and the source code is kept updated in the background...

Remark: Please save the source in the interpreter before playing around, just copying it to "<New>" does not work.
Room for improvements: panel gadgets :evil: - I've no idea how to disable gadgets in hidden panels.

Code: Select all

; -----------------------------------------------------------------------------------------------------------------------------

; Define Transformer Kit

	EnableExplicit

	#TxGadgetStart=		1
	#TxGadgetStop=		999
	#TxDemo=			1
	#TxStatus=			#TxGadgetStop+1

	#TxSizeGrid=			10
	#TxSizeBorder=		5
	#TxSizeBox=			#TxSizeBorder+4
	#TxSizeTop=			15

	#TxColorActive=		#Gray
	#TxColorInactive=	$E0E0E0
	#TxColorBackground=	$F0F0F0
	#TxColorGrid=		$04EDA9

	#Undefined=			-1

	Enumeration
		#TxModeOff
		#TxModeOn
		#TxModeSave
	EndEnumeration

	Structure TxGadgetType
		Gadget.i
		Border.i
		Grid.i
		Box.i
		X.i
		Y.i
		W.i
		H.i
		Code.s
		Line.i
	EndStructure

	Global TxGadgetCount
	Global TxMode
	Global TxGrid
	Global TxSourceCount

	Global Dim TxGadget.TxGadgetType(0)
	Global Dim TxSource.s(0)

	#Tx_LeftUp=			5
	#Tx_Left=			7
	#Tx_LeftDown=		6
	#Tx_RightUp=		9
	#Tx_Right=			11
	#Tx_RightDown=		10
	#Tx_Up=			13
	#Tx_Down=			14
	#Tx_Move=			17
	#Tx_Grid=			18

; EndDefine
Procedure.i TxChoose(selector,one,zero)

	If selector
		ProcedureReturn one
	Else
		ProcedureReturn zero
	EndIf

EndProcedure
Procedure.i TxGrid(Value.i)

	If TxGrid
		ProcedureReturn Round(Value/#TxSizeGrid,#PB_Round_Nearest)*#TxSizeGrid
	Else
		ProcedureReturn Value
	EndIf

EndProcedure
Procedure TxSetSource(ID)

	Protected s.s,t.s
	Protected p,line

	CompilerIf #PB_Compiler_Debugger

		With TxGadget(ID)

			If \Gadget<=TxSourceCount
				\Line=0
				\Code=":("
				If ReadFile(0,#PB_Compiler_File,#PB_File_SharedRead)
					While Eof(0)=0
						line+1
						s=Trim(Trim(ReadString(0)),#TAB$)
						p=FindString(s,"Gadget(")
						If p
							t=StringField(Mid(s,p+7),1,",")
							If PeekA(@t)='#' And t<>"#PB_Any"
								If FindString(TxSource(\Gadget)+"#",t+"#")
									\Line=line
									\Code=s
									; Debug " #"+Str(line)+": "+s
									ProcedureReturn line
								EndIf
							EndIf
						EndIf

					Wend
					CloseFile(0)
				EndIf
			EndIf

		EndWith

	CompilerEndIf


EndProcedure
Procedure TxGetID(gadget)

	Protected i

	i=TxGadgetCount
	While i
		If TxGadget(i)\Border=gadget
			ProcedureReturn i
		EndIf
		i-1
	Wend

	ProcedureReturn #Null

EndProcedure
Procedure TxBorderUpdate(ID)

	Protected w,h,i,c

	If StartDrawing(CanvasOutput(TxGadget(ID)\Border))
		w=OutputWidth()
		h=OutputHeight()

		c=TxChoose(TxGadget(ID)\Box,#TxColorActive,#TxColorInactive)
		Box(0,0,w,h,#TxColorBackground)
		Box(0,0,#TxSizeBox,#TxSizeBox,c)
		Box(0,h-#TxSizeBox,#TxSizeBox,#TxSizeBox,c)
		Box(w-#TxSizeBox,0,#TxSizeBox,#TxSizeBox,c)
		Box(w-#TxSizeBox,h-#TxSizeBox,#TxSizeBox,#TxSizeBox,c)
		Box((w-#TxSizeBox)>>1,0,#TxSizeBox,#TxSizeBox,c)
		Box((w-#TxSizeBox)>>1,h-#TxSizeBox,#TxSizeBox,#TxSizeBox,c)
		Box(0,(h-#TxSizeBox)>>1,#TxSizeBox,#TxSizeBox,c)
		Box(w-#TxSizeBox,(h-#TxSizeBox)>>1,#TxSizeBox,#TxSizeBox,c)

		w-1
		h-1
		If TxGadget(ID)\Box
			i=w
			While i>#TxSizeBox
				Plot(i,0,c)
				Plot(i,h,c)
				i-2
			Wend
			i=h
			While i>#TxSizeBox
				Plot(0,i,c)
				Plot(w,i,c)
				i-2
			Wend

			Box(#TxSizeBox+1,h-#TxSizeBorder+1,#TxSizeTop,#TxSizeBorder,TxChoose(TxGrid,#Black,#Gray))
			Box(#TxSizeBox+2,h-#TxSizeBorder+2,#TxSizeTop-2,#TxSizeBorder-2,TxChoose(TxGrid,#TxColorGrid,#White))

		EndIf

		Box(#TxSizeBox+1,0,#TxSizeTop,#TxSizeBorder,TxChoose(TxGadget(ID)\Box,#Black,c))
		Box(#TxSizeBox+2,1,#TxSizeTop-2,#TxSizeBorder-2,TxChoose(TxGadget(ID)\Box,#Yellow,#TxColorBackground))

		StopDrawing()
	EndIf

EndProcedure
Procedure TxGadgetUpdate(ID,x,y,w,h)

	x=TxGrid(x) : y=TxGrid(y) : w=TxGrid(w) : h=TxGrid(h)

	With TxGadget(ID)
		ResizeGadget(\Gadget,x,y,w,h)
		ResizeGadget(\Border,x-#TxSizeBorder,y-#TxSizeBorder,w+#TxSizeBorder<<1,h+#TxSizeBorder<<1)
		TxBorderUpdate(ID)
	EndWith

EndProcedure
Procedure TxGadgetCallback()

	Static Selected
	Static mx,my

	Protected ID,x,y,z
	Protected s.s

	ID=TxGetID(EventGadget())
	If ID
		With TxGadget(ID)

			s=\Code
			x=FindString(s,",")
			y=FindString(s,",",FindString(s,",",FindString(s,",",x+1)+1)+1)
			z=FindString(s,",",y+1)
			If z=0 : z=FindString(s,")",y+1) : EndIf
			If z : \Code=Left(s,x)+Str(GadgetX(\Gadget))+","+Str(GadgetY(\Gadget))+","+Str(GadgetWidth(\Gadget))+","+Str(GadgetHeight(\Gadget))+Mid(s,z) : EndIf

			CompilerIf #TxDemo
				SetGadgetText(#TxStatus," Line "+Str(\Line)+" - "+\Code)
			CompilerEndIf

			Select EventType()
			Case #PB_EventType_LeftButtonDown
				Selected=#True
				If \Box=#Tx_Grid
					TxGrid!1
					TxBorderUpdate(ID)
				EndIf
				mx=TxGrid(GadgetX(\Border)+GetGadgetAttribute(\Border,#PB_Canvas_MouseX))
				my=TxGrid(GadgetY(\Border)+GetGadgetAttribute(\Border,#PB_Canvas_MouseY))

			Case #PB_EventType_LeftButtonUp
				\X=GadgetX(\Gadget)
				\Y=GadgetY(\Gadget)
				\W=GadgetWidth(\Gadget)
				\H=GadgetHeight(\Gadget)
				Selected = #False

			Case #PB_EventType_MouseLeave
				\Box=#Null
				TxBorderUpdate(ID)

			Case #PB_EventType_MouseMove

				If Selected
					X=TxGrid(WindowMouseX(GetActiveWindow())-mx)
					Y=TxGrid(WindowMouseY(GetActiveWindow())-my)

					Select \Box
					Case #Tx_Move
						TxGadgetUpdate(ID,\X+x,\Y+Y,\W,\H)
					Case #Tx_LeftUp
						TxGadgetUpdate(ID,\X+x,\Y+y,\W-x,\H-y)
					Case #Tx_Up
						TxGadgetUpdate(ID,\X,\Y+y,\W,\H-y)
					Case #Tx_RightUp
						TxGadgetUpdate(ID,\X,\Y+y,\W+x,\H-y)
					Case #Tx_Left
						TxGadgetUpdate(ID,\X+x,\Y,\W-x,\H)
					Case #Tx_Right
						TxGadgetUpdate(ID,\X,\Y,\W+x,\H)
					Case #Tx_LeftDown
						TxGadgetUpdate(ID,\X+x,\Y,\W-x,\H+y)
					Case #Tx_Down
						TxGadgetUpdate(ID,\X,\Y,\W,\H+y)
					Case #Tx_RightDown
						TxGadgetUpdate(ID,\X,\Y,\W+x,\H+y)
					EndSelect

				Else
					x=GetGadgetAttribute(EventGadget(),#PB_Canvas_MouseX)
					y=GetGadgetAttribute(EventGadget(),#PB_Canvas_MouseY)
					If y<=#TxSizeBox
						y=1
					ElseIf y>=GadgetHeight(EventGadget())-#TxSizeBox
						y=2
					ElseIf y>=(GadgetHeight(EventGadget())-#TxSizeBox)>>1 And y<=(GadgetHeight(EventGadget())+#TxSizeBox)>>1
						y=3
					Else
						y=#Undefined
					EndIf
					If x<=#TxSizeBox
						y+4
					ElseIf x>=GadgetWidth(EventGadget())-#TxSizeBox
						y+8
					ElseIf x>=(GadgetWidth(EventGadget())-#TxSizeBox)>>1 And x<=(GadgetWidth(EventGadget())+#TxSizeBox)>>1
						y+12
					ElseIf x<=#TxSizeBox+#TxSizeTop
						y+16
					EndIf

					Select y
					Case #Tx_LeftUp,#Tx_RightDown
						x=#PB_Cursor_LeftUpRightDown
					Case #Tx_LeftDown,#Tx_RightUp
						x=#PB_Cursor_LeftDownRightUp
					Case #Tx_Left,#Tx_Right
						x=#PB_Cursor_LeftRight
					Case #Tx_Up,#Tx_Down
						x=#PB_Cursor_UpDown
					Case #Tx_Move
						x=#PB_Cursor_Arrows
					Case #Tx_Grid
						x=#PB_Cursor_Hand
					Default
						x=#PB_Cursor_Default
						y=#Undefined
					EndSelect

					SetGadgetAttribute(\Border,#PB_Canvas_Cursor,x)
					If \Box<>y
						\Box=y
						TxBorderUpdate(ID)
					EndIf

				EndIf
			EndSelect
		EndWith
	EndIf

EndProcedure
Procedure TxGadgetAdd(Gadget.i)

	TxGadgetCount+1
	ReDim TxGadget(TxGadgetCount)

	With TxGadget(TxGadgetCount)
		\Gadget=Gadget
		\X=GadgetX(Gadget)
		\Y=GadgetY(Gadget)
		\W=GadgetWidth(Gadget)
		\H=GadgetHeight(Gadget)
		\Border=CanvasGadget(#PB_Any,\X-#TxSizeBorder,\Y-#TxSizeBorder,\W+#TxSizeBorder<<1,\H+#TxSizeBorder<<1)

		TxSetSource(TxGadgetCount)
		TxBorderUpdate(TxGadgetCount)
		BindGadgetEvent(\Border,@TxGadgetCallback())

		SetWindowLongPtr_(GadgetID(\Border),#GWL_STYLE,GetWindowLongPtr_(GadgetID(\Border),#GWL_STYLE)|#WS_CLIPSIBLINGS)
		SetWindowPos_(GadgetID(\Gadget),#HWND_TOP,-1,-1,-1,-1,#SWP_NOSIZE|#SWP_NOMOVE)
	EndWith

EndProcedure
Procedure TxGadgetMode(mode)

	Protected i
	Protected ThisWindow
	Protected GadgetWindow

	If mode=#TxModeSave

		Protected s.s,c.s
		Protected n,line

		CompilerIf #PB_Compiler_Debugger

			; Debug "SAVE - "+#PB_Compiler_File
			If TxGadgetCount
				If CopyFile(#PB_Compiler_File,#PB_Compiler_File+".tmp")
					If ReadFile(0,#PB_Compiler_File+".tmp")
						If CreateFile(1,#PB_Compiler_File)
							While Eof(0)=0
								line+1
								s=ReadString(0)
								n=0
								i=TxGadgetCount
								While i
									If TxGadget(i)\Line=line
										n=i
										i=1
									EndIf
									i-1
								Wend
								If n
									c=TxGadget(n)\Code
									n=FindString(s,Left(c,FindString(c,",")))
									s=Left(s,n-1)+c
								EndIf
								WriteStringN(1,s)
							Wend
							CloseFile(1)
						EndIf
						CloseFile(0)
					EndIf
				EndIf
			EndIf

		CompilerEndIf


	Else
		TxMode=mode

		; ThisWindow=WindowID(GetActiveWindow())
		; UseGadgetList(ThisWindow)

		If TxMode
			For i=#TxGadgetStart To #TxGadgetStop
				If IsGadget(i)
					GadgetWindow=GetParent_(GadgetID(i))
					If IsWindow_(GadgetWindow)
						If GadgetWindow<>ThisWindow
							ThisWindow=GadgetWindow
							UseGadgetList(ThisWindow)
						EndIf
						; If GadgetWindow=ThisWindow
						TxGadgetAdd(i)
						; EndIf
					EndIf
				EndIf
			Next i

		Else

			While TxGadgetCount
				With TxGadget(TxGadgetCount)
					UnbindGadgetEvent(\Border,@TxGadgetCallback())
					FreeGadget(\Border)
				EndWith
				TxGadgetCount-1
			Wend

		EndIf
	EndIf


EndProcedure
Procedure TxInit()

	Protected s.s
	Protected n,scan

	CompilerIf #PB_Compiler_Debugger

		If ReadFile(0,#PB_Compiler_File)
			While Eof(0)=0
				s=Trim(Trim(ReadString(0)),#TAB$)
				If Left(s,11)="Enumeration"
					n=0
					scan=1
				ElseIf Left(s,14)="EndEnumeration"
					scan=0
				ElseIf scan And PeekA(@s)='#'
					; Debug s+" = "+Str(n)
					If n>TxSourceCount
						TxSourceCount=n
						ReDim TxSource(TxSourceCount)
					EndIf
					TxSource(n)+s
					n+1
				EndIf
			Wend
			CloseFile(0)
		EndIf

	CompilerEndIf

	TxGrid=#True
	TxGadgetMode(#TxModeOn)

EndProcedure

; -----------------------------------------------------------------------------------------------------------------------------

Enumeration
	#Window1
	#Window2
	#TxGadgetDemo
	#TextGadget
	#EditorGadget
	#ButtonGadget
	#TrackBarGadget
	#CanvasGadget
	#ListGadget
	#FrameGadget
	#CheckboxGadget
	#SpinGadget
	#AnotherOne
	#Panel1
	#Panel2
	#PanButton1
	#PanButton2
EndEnumeration

OpenWindow(#Window1, 0, 0, 600, 400, "Window 1", #PB_Window_MinimizeGadget|#PB_Window_ScreenCentered)
EditorGadget(#EditorGadget,20,80,200,50, #PB_Editor_WordWrap) : SetGadgetText(#EditorGadget, "Grumpy wizards make toxic brew for the evil Queen and Jack.")
ButtonGadget(#ButtonGadget,20,150,200,30, "Hallo Welt!", #PB_Button_MultiLine)
TextGadget(#TextGadget,240,160,340,20,"The early bird gets the worm, but the early worm gets eaten.",#PB_Button_MultiLine)
TrackBarGadget(#TrackBarGadget,240,110,340,30, 0, 100) : SetGadgetState(#TrackBarGadget, 70)
CanvasGadget(#CanvasGadget,240,30,340,60)
CheckBoxGadget(#CheckboxGadget,20,250,560,20, "CheckBox checked"): SetGadgetState(#CheckboxGadget, #PB_Checkbox_Checked)
SpinGadget(#SpinGadget,20,200,560,30,0,100,#PB_Spin_Numeric) : SetGadgetState(#SpinGadget, 70)
ListIconGadget(#ListGadget,20,290,300,70,"Column 1",100,#PB_ListIcon_GridLines)
FrameGadget(#FrameGadget,340,290,240,70,"",#PB_Frame_Single)

ButtonGadget(#TxGadgetDemo,20,30,200,30,"SAVE GADGETS NOW!",#PB_Button_MultiLine)
TextGadget(#TxStatus,0,380,6000,20,"")

OpenWindow(#Window2,200,200,600,400,"Window 2",#PB_Window_MinimizeGadget)
EditorGadget(#AnotherOne,10,10,260,370, #PB_Editor_WordWrap) : SetGadgetText(#EditorGadget, "Grumpy wizards make toxic brew for the evil Queen and Jack.")
PanelGadget(#Panel1,280,20,310,360)
AddGadgetItem(#Panel1, -1, "Panel 1")
PanelGadget(#Panel2,5,5,290,166)
AddGadgetItem(#Panel2, -1, "Sub-Panel 1")
AddGadgetItem(#Panel2, -1, "Sub-Panel 2")
AddGadgetItem(#Panel2, -1, "Sub-Panel 3")
CloseGadgetList()
AddGadgetItem (#Panel1, -1,"Panel 2")
ButtonGadget(#PanButton1, 10, 15, 80, 24,"Button 1")
ButtonGadget(#PanButton2, 95, 15, 80, 24,"Button 2")
CloseGadgetList()

SetActiveWindow(#Window1)
TxInit()

Repeat

	Select WaitWindowEvent()

	Case #PB_Event_CloseWindow
		End

	Case #PB_Event_Gadget
		Select EventGadget()
		Case #TxGadgetDemo
			TxGadgetMode(#TxModeSave)
			TxGadgetMode(TxMode!1)
			SetGadgetText(#TxGadgetDemo,StringField("Enable Transformer.Save Gadget Location",TxMode+1,"."))
		EndSelect
	EndSelect

ForEver

; -----------------------------------------------------------------------------------------------------------------------------
mestnyi
Addict
Addict
Posts: 995
Joined: Mon Nov 25, 2013 6:41 am

Re: EnableGadgetTransformation - change gadget at runtime

Post by mestnyi »

What do you think?

Code: Select all

;- Include: Transformation
; http://www.purebasic.fr/english/viewtopic.php?f=12&t=64700

DeclareModule Transformation
  EnableExplicit
  
  EnumerationBinary 1
    #Anchor_Position
    #Anchor_Horizontally
    #Anchor_Vertically
  EndEnumeration
  
  #Anchor_Size = #Anchor_Horizontally|#Anchor_Vertically
  #Anchor_All  = #Anchor_Position|#Anchor_Horizontally|#Anchor_Vertically
  
  Declare Is(Gadget.i)
  Declare Disable(Gadget.i)
  Declare Enable(Gadget.i, Grid.i=1, Flags.i=#Anchor_All)
  
EndDeclareModule

Module Transformation
  Structure Transformation
    Gadget.i
    ID.i[10]
    Grid.i
    Pos.i
    Size.i
    Img.i
  EndStructure
  
  Structure DataBuffer
    ID.i[10]
  EndStructure
  
  Global NewList AnChor.Transformation()
  
  CompilerIf #PB_Compiler_OS = #PB_OS_Windows
    Procedure GadgetsClipCallBack( GadgetID, lParam )
      If GadgetID
        Protected Gadget = GetProp_( GadgetID, "PB_ID" )
        
        If GetWindowLongPtr_( GadgetID, #GWL_STYLE ) & #WS_CLIPSIBLINGS = #False 
          If IsGadget( Gadget ) 
            Select GadgetType( Gadget )
              Case #PB_GadgetType_ComboBox
                Protected Height = GadgetHeight( Gadget )
                
              Case #PB_GadgetType_Text
                If (GetWindowLongPtr_(GadgetID( Gadget ), #GWL_STYLE) & #SS_NOTIFY) = #False
                  SetWindowLongPtr_(GadgetID( Gadget ), #GWL_STYLE, GetWindowLongPtr_(GadgetID( Gadget ), #GWL_STYLE) | #SS_NOTIFY)
                EndIf
                
              Case #PB_GadgetType_Frame, #PB_GadgetType_Image
                If (GetWindowLongPtr_(GadgetID( Gadget ), #GWL_EXSTYLE) & #WS_EX_TRANSPARENT) = #False
                  SetWindowLongPtr_(GadgetID( Gadget ), #GWL_EXSTYLE, GetWindowLongPtr_(GadgetID( Gadget ), #GWL_EXSTYLE) | #WS_EX_TRANSPARENT)
                EndIf
                
                ; Из-за бага когда устанавливаешь фоновый рисунок (например точки на кантейнер)
              Case #PB_GadgetType_Container 
                SetGadgetColor( Gadget, #PB_Gadget_BackColor, GetSysColor_( #COLOR_BTNFACE ))
                
                ; Для панел гаджета темный фон убирать
              Case #PB_GadgetType_Panel 
                If Not IsGadget( Gadget ) And (GetWindowLongPtr_(GadgetID, #GWL_EXSTYLE) & #WS_EX_TRANSPARENT) = #False
                  SetWindowLongPtr_(GadgetID, #GWL_EXSTYLE, GetWindowLongPtr_(GadgetID, #GWL_EXSTYLE) | #WS_EX_TRANSPARENT)
                EndIf
                ; SetClassLongPtr_(GadgetID, #GCL_HBRBACKGROUND, GetStockObject_(#NULL_BRUSH))
                
            EndSelect
            
            ;             If (GetWindowLongPtr_(GadgetID( Gadget ), #GWL_EXSTYLE) & #WS_EX_TRANSPARENT) = #False
            ;               SetWindowLongPtr_(GadgetID( Gadget ), #GWL_EXSTYLE, GetWindowLongPtr_(GadgetID( Gadget ), #GWL_EXSTYLE) | #WS_EX_TRANSPARENT)
            ;             EndIf
          EndIf
          
          SetWindowLongPtr_( GadgetID, #GWL_STYLE, GetWindowLongPtr_( GadgetID, #GWL_STYLE ) | #WS_CLIPSIBLINGS | #WS_CLIPCHILDREN )
          
          If Height
            ResizeGadget( Gadget, #PB_Ignore, #PB_Ignore, #PB_Ignore, Height )
          EndIf
          
          SetWindowPos_( GadgetID, #GW_HWNDFIRST, 0,0,0,0, #SWP_NOMOVE|#SWP_NOSIZE )
        EndIf
        
      EndIf
      
      ProcedureReturn GadgetID
    EndProcedure
  CompilerEndIf
  
  Procedure ClipGadgets( WindowID )
    CompilerIf #PB_Compiler_OS = #PB_OS_Windows
      ; WindowID = GetAncestor_( WindowID, #GA_ROOT )
      SetWindowLongPtr_( WindowID, #GWL_STYLE, GetWindowLongPtr_( WindowID, #GWL_STYLE )|#WS_CLIPCHILDREN )
      EnumChildWindows_( WindowID, @GadgetsClipCallBack(), 0 )
    CompilerEndIf
  EndProcedure
  
  Macro MoveTransformation(This)
    ; Transformation resize
    If This\ID[5] : ResizeGadget(This\ID[5], GadgetX(This\Gadget)-This\Size+This\Pos, GadgetY(This\Gadget)+GadgetHeight(This\Gadget)-This\Pos, #PB_Ignore, #PB_Ignore) : EndIf
    If This\ID[2] : ResizeGadget(This\ID[2], GadgetX(This\Gadget)+(GadgetWidth(This\Gadget)-This\Size)/2, GadgetY(This\Gadget)+GadgetHeight(This\Gadget)-This\Pos, #PB_Ignore, #PB_Ignore) : EndIf
    If This\ID[3] : ResizeGadget(This\ID[3], GadgetX(This\Gadget)+GadgetWidth(This\Gadget)-This\Pos, GadgetY(This\Gadget)+GadgetHeight(This\Gadget)-This\Pos, #PB_Ignore, #PB_Ignore) : EndIf
    If This\ID[4] : ResizeGadget(This\ID[4], GadgetX(This\Gadget)-This\Size+This\Pos, GadgetY(This\Gadget)+(GadgetHeight(This\Gadget)-This\Size)/2, #PB_Ignore, #PB_Ignore) : EndIf
    If This\ID[1] : ResizeGadget(This\ID[1], GadgetX(This\Gadget), GadgetY(This\Gadget), GadgetWidth(This\Gadget), GadgetHeight(This\Gadget)) : EndIf
    If This\ID[6] : ResizeGadget(This\ID[6], GadgetX(This\Gadget)+GadgetWidth(This\Gadget)-This\Pos, GadgetY(This\Gadget)+(GadgetHeight(This\Gadget)-This\Size)/2, #PB_Ignore, #PB_Ignore) : EndIf
    If This\ID[7] : ResizeGadget(This\ID[7], GadgetX(This\Gadget)-This\Size+This\Pos, GadgetY(This\Gadget)-This\Size+This\Pos, #PB_Ignore, #PB_Ignore) : EndIf
    If This\ID[8] : ResizeGadget(This\ID[8], GadgetX(This\Gadget)+(GadgetWidth(This\Gadget)-This\Size)/2, GadgetY(This\Gadget)-This\Size+This\Pos, #PB_Ignore, #PB_Ignore) : EndIf
    If This\ID[9] : ResizeGadget(This\ID[9], GadgetX(This\Gadget)+GadgetWidth(This\Gadget)-This\Pos, GadgetY(This\Gadget)-This\Size+This\Pos, #PB_Ignore, #PB_Ignore) : EndIf
  EndMacro
  
  Procedure Is(Gadget.i)
    Protected I
    
    If ListSize(AnChor()) 
      ForEach AnChor()
        If AnChor() = GetGadgetData(Gadget)
         For I = 1 To 9
            If AnChor()\ID[I] = Gadget
              ProcedureReturn I
            EndIf
          Next
        EndIf
      Next
    EndIf
  EndProcedure
  
  Procedure.i GridMatch(Value.i, Grid.i, Max.i=$7FFFFFFF)
    Value = Round((Value/Grid), #PB_Round_Nearest) * Grid
    If (Value>Max) : Value=Max : EndIf
    ProcedureReturn Value
  EndProcedure
  
  Procedure Callback()
    Static Selected.i, X.i, Y.i, OffsetX.i, OffsetY.i, GadgetX0.i, GadgetX1.i, GadgetY0.i, GadgetY1.i
    Protected *Anchor.Transformation = GetGadgetData(EventGadget())
    
    With *Anchor
      Select EventType()
        Case #PB_EventType_LeftButtonDown
          Selected = #True
          GadgetX0 = GadgetX(\Gadget)
          GadgetY0 = GadgetY(\Gadget)
          GadgetX1 = GadgetX0 + GadgetWidth(\Gadget)
          GadgetY1 = GadgetY0 + GadgetHeight(\Gadget)
          OffsetX = GetGadgetAttribute(EventGadget(), #PB_Canvas_MouseX)
          OffsetY = GetGadgetAttribute(EventGadget(), #PB_Canvas_MouseY)
          
          If \ID[1] : HideGadget(\ID[1], #True) : EndIf
          
        Case #PB_EventType_LeftButtonUp
          Selected = #False
          
          If \ID[1] 
            If StartDrawing(WindowOutput(GetActiveWindow()))
              \Img = GrabDrawingImage(#PB_Any, 
                               GadgetX(\Gadget, #PB_Gadget_WindowCoordinate),
                               GadgetY(\Gadget, #PB_Gadget_WindowCoordinate), 
                               GadgetWidth(\Gadget), GadgetHeight(\Gadget))
              StopDrawing()
              
              If StartDrawing(CanvasOutput(\ID[1]))
                DrawImage(ImageID(\Img), 0,0)
                StopDrawing()
                HideGadget(\ID[1], #False) 
              EndIf
            EndIf
          EndIf
          
      
        Case #PB_EventType_MouseMove
          If Selected
            X = DesktopMouseX()-(GadgetX(\Gadget, #PB_Gadget_ScreenCoordinate)-GadgetX(\Gadget, #PB_Gadget_WindowCoordinate))-OffsetX
            Y = DesktopMouseY()-(GadgetY(\Gadget, #PB_Gadget_ScreenCoordinate)-GadgetY(\Gadget, #PB_Gadget_WindowCoordinate))-OffsetY
            
            ; gadget resize
            Select EventGadget()
              Case \ID[5] : ResizeGadget(\Gadget, GridMatch(X+\Size, \Grid, GadgetX1), #PB_Ignore, GadgetX1-GridMatch(X+\Size, \Grid, GadgetX1), GridMatch(Y, \Grid)-GadgetY0)
              Case \ID[2] : ResizeGadget(\Gadget, #PB_Ignore, #PB_Ignore, #PB_Ignore, GridMatch(Y, \Grid)-GadgetY0)
              Case \ID[3] : ResizeGadget(\Gadget, #PB_Ignore, #PB_Ignore, GridMatch(X, \Grid)-GadgetX0, GridMatch(Y, \Grid)-GadgetY0)
              Case \ID[4] : ResizeGadget(\Gadget, GridMatch(X+\Size, \Grid, GadgetX1), #PB_Ignore, GadgetX1-GridMatch(X+\Size, \Grid, GadgetX1), #PB_Ignore)
              Case \ID[1] : ResizeGadget(\Gadget, GridMatch(X-\Size, \Grid), GridMatch(Y+\Size-\Pos, \Grid), #PB_Ignore, #PB_Ignore)
              Case \ID[6] : ResizeGadget(\Gadget, #PB_Ignore, #PB_Ignore, GridMatch(X, \Grid)-GadgetX0, #PB_Ignore)
              Case \ID[7] : ResizeGadget(\Gadget, GridMatch(X+\Size, \Grid, GadgetX1), GridMatch(Y+\Size, \Grid, GadgetY1), GadgetX1-GridMatch(X+\Size, \Grid, GadgetX1), GadgetY1-GridMatch(Y+\Size, \Grid, GadgetY1))
              Case \ID[8] : ResizeGadget(\Gadget, #PB_Ignore, GridMatch(Y+\Size, \Grid, GadgetY1), #PB_Ignore, GadgetY1-GridMatch(Y+\Size, \Grid, GadgetY1))
              Case \ID[9] : ResizeGadget(\Gadget, #PB_Ignore, GridMatch(Y+\Size, \Grid, GadgetY1), GridMatch(X, \Grid)-GadgetX0, GadgetY1-GridMatch(Y+\Size, \Grid, GadgetY1))
            EndSelect
            
            MoveTransformation(*Anchor)
            
            CompilerIf #PB_Compiler_OS = #PB_OS_Windows
              UpdateWindow_(WindowID(GetActiveWindow()))
            CompilerEndIf 
          EndIf
      EndSelect
    EndWith
  EndProcedure
  
  Procedure Disable(Gadget.i)
    Protected I.i
    
    If ListSize(AnChor())
      ForEach AnChor()
        If AnChor()\Gadget = Gadget
          For I = 1 To 9
            If AnChor()\ID[I]
              FreeGadget(AnChor()\ID[I])
            EndIf
          Next
          
          DeleteElement(AnChor())
        EndIf
      Next
    EndIf
    
  EndProcedure
  
  Procedure Enable(Gadget.i, Grid.i=1, Flags.i=#Anchor_All)
    Protected ID.i, I.i
    Protected *Anchor.Transformation
    Protected *Cursors.DataBuffer = ?Cursors
    Protected *Flags.DataBuffer = ?Flags
    
    
      
    Disable(Gadget)
    
    *Anchor = AddElement(AnChor())
    *Anchor\Gadget = Gadget
    *Anchor\Grid = Grid
    *Anchor\Pos = 3
    *AnChor\Size = 5
    
    If StartDrawing(WindowOutput(GetActiveWindow()))
      *Anchor\Img = GrabDrawingImage(#PB_Any, GadgetX(*Anchor\Gadget, #PB_Gadget_WindowCoordinate),GadgetY(*Anchor\Gadget, #PB_Gadget_WindowCoordinate), GadgetWidth(*Anchor\Gadget), GadgetHeight(*Anchor\Gadget))
      StopDrawing()
    EndIf
        
    For I = 1 To 9
      If Flags & *Flags\ID[I] = *Flags\ID[I]
        If I=1
          ID = CanvasGadget(#PB_Any, 0,0, GadgetWidth(Gadget), GadgetHeight(Gadget))
        Else
          ID = CanvasGadget(#PB_Any, 0,0, *Anchor\Size, *Anchor\Size)
        EndIf
        
        *Anchor\ID[I] = ID
        SetGadgetData(ID, *Anchor)
        SetGadgetAttribute(ID, #PB_Canvas_Cursor, *Cursors\ID[I])
        
        If StartDrawing(CanvasOutput(ID))
          Box(0, 0, OutputWidth(), OutputHeight(), $000000)
          Box(1, 1, OutputWidth()-2, OutputHeight()-2, $FFFFFF)
          StopDrawing()
        EndIf
        
        BindGadgetEvent(ID, @Callback())
      EndIf
    Next
    
    MoveTransformation(*Anchor)
    ClipGadgets(UseGadgetList(0))
    
    If IsGadget(*Anchor\ID[1]) And StartDrawing(CanvasOutput(*Anchor\ID[1]))
      DrawImage(ImageID(*Anchor\Img), 0,0)
      StopDrawing()
    EndIf
    
    CompilerIf #PB_Compiler_OS = #PB_OS_Windows
      UpdateWindow_(UseGadgetList(0))
    CompilerEndIf 
    
    DataSection
      Cursors:
      Data.i 0, #PB_Cursor_Arrows, #PB_Cursor_UpDown, #PB_Cursor_LeftUpRightDown, #PB_Cursor_LeftRight
      Data.i #PB_Cursor_LeftDownRightUp, #PB_Cursor_LeftRight, #PB_Cursor_LeftUpRightDown, #PB_Cursor_UpDown, #PB_Cursor_LeftDownRightUp
      
      Flags:
      Data.i 0, #Anchor_Position, #Anchor_Vertically, #Anchor_Size, #Anchor_Horizontally
      Data.i #Anchor_Size, #Anchor_Horizontally, #Anchor_Size, #Anchor_Vertically, #Anchor_Size
    EndDataSection
  EndProcedure
  
EndModule



;- Example
CompilerIf #PB_Compiler_IsMainFile
  UseModule Transformation
  
  Enumeration
    #Window
    #Transformation
    #EditorGadget
    #ButtonGadget
    #TrackBarGadget
    #SpinGadget
    #CanvasGadget
  EndEnumeration
  
  OpenWindow(#Window, 0, 0, 600, 400, "WindowTitle", #PB_Window_MinimizeGadget|#PB_Window_ScreenCentered)
  EditorGadget(#EditorGadget, 50, 100, 200, 50, #PB_Editor_WordWrap) : SetGadgetText(#EditorGadget, "Grumpy wizards make toxic brew for the evil Queen and Jack.")
  ButtonGadget(#ButtonGadget, 50, 250, 200, 25, "Hallo Welt!", #PB_Button_MultiLine)
  TrackBarGadget(#TrackBarGadget, 350, 100, 200, 25, 0, 100) : SetGadgetState(#TrackBarGadget, 70)
  SpinGadget(#SpinGadget, 350, 180, 200, 25, 0, 100, #PB_Spin_Numeric) : SetGadgetState(#SpinGadget, 70)
  CanvasGadget(#CanvasGadget, 350, 250, 200, 25)
  
  ButtonGadget(#Transformation, 20, 20, 150, 25, "Enable Transformation", #PB_Button_Toggle)
  SetGadgetData(#CanvasGadget, 999)
  
  Repeat
    
    Select WaitWindowEvent()
        
      Case #PB_Event_CloseWindow
        End
        
      Case #PB_Event_Repaint
        
      Case #PB_Event_Gadget
        Select EventGadget()
          Case #Transformation
            Select GetGadgetState(#Transformation)
              Case #False
                SetGadgetText(#Transformation, "Enable Transformation")
                Disable(#EditorGadget)
                Disable(#ButtonGadget)
                Disable(#TrackBarGadget)
                Disable(#SpinGadget)
                Disable(#CanvasGadget)
              Case #True
                SetGadgetText(#Transformation, "Disable Transformation")
                Enable(#EditorGadget, 5, #Anchor_All)
                Enable(#ButtonGadget, 1, #Anchor_All)
                Enable(#TrackBarGadget, 1, #Anchor_Position|#Anchor_Horizontally)
                Enable(#SpinGadget, 1, #Anchor_Position)
                Enable(#CanvasGadget, 1, #Anchor_All)
            EndSelect
        EndSelect
        
    EndSelect
    
  ForEver
CompilerEndIf
In the second variant, how to do it, so that when you click (up) the anchor of the gadget's move, show the rest of the anchors of this gadget?

Code: Select all

;- Include: Transformation
; http://www.purebasic.fr/english/viewtopic.php?f=12&t=64700

DeclareModule Transformation
  EnableExplicit
  
  #Arrows = 9
  
  EnumerationBinary 1
    #Anchor_Position
    #Anchor_Horizontally
    #Anchor_Vertically
  EndEnumeration
  
  #Anchor_Size = #Anchor_Horizontally|#Anchor_Vertically
  #Anchor_All  = #Anchor_Position|#Anchor_Horizontally|#Anchor_Vertically
  
  Declare Is(Gadget.i)
  Declare Gadget()
  Declare Update(Gadget.i)
  Declare Disable(Gadget.i)
  Declare Enable(Gadget.i, Grid.i=1, Flags.i=#Anchor_All)
  
EndDeclareModule

Module Transformation
  Structure Transformation
    Gadget.i
    ID.i[10]
    Grid.i
    Pos.i
    Size.i
  EndStructure
  
  Structure DataBuffer
    ID.i[10]
  EndStructure
  
  Global ActiveGadget =- 1
  Global NewList AnChor.Transformation()
  
  CompilerIf #PB_Compiler_OS = #PB_OS_Windows
    Procedure GadgetsClipCallBack( GadgetID, lParam )
      If GadgetID
        Protected Gadget = GetProp_( GadgetID, "PB_ID" )
        
        If GetWindowLongPtr_( GadgetID, #GWL_STYLE ) & #WS_CLIPSIBLINGS = #False 
          If IsGadget( Gadget ) 
            Select GadgetType( Gadget )
              Case #PB_GadgetType_ComboBox
                Protected Height = GadgetHeight( Gadget )
                
              Case #PB_GadgetType_Text
                If (GetWindowLongPtr_(GadgetID( Gadget ), #GWL_STYLE) & #SS_NOTIFY) = #False
                  SetWindowLongPtr_(GadgetID( Gadget ), #GWL_STYLE, GetWindowLongPtr_(GadgetID( Gadget ), #GWL_STYLE) | #SS_NOTIFY)
                EndIf
                
              Case #PB_GadgetType_Frame, #PB_GadgetType_Image
                If (GetWindowLongPtr_(GadgetID( Gadget ), #GWL_EXSTYLE) & #WS_EX_TRANSPARENT) = #False
                  SetWindowLongPtr_(GadgetID( Gadget ), #GWL_EXSTYLE, GetWindowLongPtr_(GadgetID( Gadget ), #GWL_EXSTYLE) | #WS_EX_TRANSPARENT)
                EndIf
                
                ; Из-за бага когда устанавливаешь фоновый рисунок (например точки на кантейнер)
              Case #PB_GadgetType_Container 
                SetGadgetColor( Gadget, #PB_Gadget_BackColor, GetSysColor_( #COLOR_BTNFACE ))
                
                ; Для панел гаджета темный фон убирать
              Case #PB_GadgetType_Panel 
                If Not IsGadget( Gadget ) And (GetWindowLongPtr_(GadgetID, #GWL_EXSTYLE) & #WS_EX_TRANSPARENT) = #False
                  SetWindowLongPtr_(GadgetID, #GWL_EXSTYLE, GetWindowLongPtr_(GadgetID, #GWL_EXSTYLE) | #WS_EX_TRANSPARENT)
                EndIf
                ; SetClassLongPtr_(GadgetID, #GCL_HBRBACKGROUND, GetStockObject_(#NULL_BRUSH))
                
            EndSelect
            
            ;             If (GetWindowLongPtr_(GadgetID( Gadget ), #GWL_EXSTYLE) & #WS_EX_TRANSPARENT) = #False
            ;               SetWindowLongPtr_(GadgetID( Gadget ), #GWL_EXSTYLE, GetWindowLongPtr_(GadgetID( Gadget ), #GWL_EXSTYLE) | #WS_EX_TRANSPARENT)
            ;             EndIf
          EndIf
          
          SetWindowLongPtr_( GadgetID, #GWL_STYLE, GetWindowLongPtr_( GadgetID, #GWL_STYLE ) | #WS_CLIPSIBLINGS | #WS_CLIPCHILDREN )
          
          If Height
            ResizeGadget( Gadget, #PB_Ignore, #PB_Ignore, #PB_Ignore, Height )
          EndIf
          
          SetWindowPos_( GadgetID, #GW_HWNDFIRST, 0,0,0,0, #SWP_NOMOVE|#SWP_NOSIZE )
        EndIf
        
      EndIf
      
      ProcedureReturn GadgetID
    EndProcedure
  CompilerEndIf
  
  Procedure ClipGadgets( WindowID )
    CompilerIf #PB_Compiler_OS = #PB_OS_Windows
      WindowID = GetAncestor_( WindowID, #GA_ROOT )
      SetWindowLongPtr_( WindowID, #GWL_STYLE, GetWindowLongPtr_( WindowID, #GWL_STYLE )|#WS_CLIPCHILDREN )
      EnumChildWindows_( WindowID, @GadgetsClipCallBack(), 0 )
    CompilerEndIf
  EndProcedure
  
  Macro MoveTransformation(This)
    ; Transformation resize
    If This\ID[1] : ResizeGadget(This\ID[1], GadgetX(This\Gadget)-This\Size+This\Pos, GadgetY(This\Gadget)+(GadgetHeight(This\Gadget)-This\Size)/2, #PB_Ignore, #PB_Ignore) : EndIf
    If This\ID[2] : ResizeGadget(This\ID[2], GadgetX(This\Gadget)+(GadgetWidth(This\Gadget)-This\Size)/2, GadgetY(This\Gadget)-This\Size+This\Pos, #PB_Ignore, #PB_Ignore) : EndIf
    If This\ID[3] : ResizeGadget(This\ID[3], GadgetX(This\Gadget)+GadgetWidth(This\Gadget)-This\Pos, GadgetY(This\Gadget)+(GadgetHeight(This\Gadget)-This\Size)/2, #PB_Ignore, #PB_Ignore) : EndIf
    If This\ID[4] : ResizeGadget(This\ID[4], GadgetX(This\Gadget)+(GadgetWidth(This\Gadget)-This\Size)/2, GadgetY(This\Gadget)+GadgetHeight(This\Gadget)-This\Pos, #PB_Ignore, #PB_Ignore) : EndIf
    If This\ID[5] : ResizeGadget(This\ID[5], GadgetX(This\Gadget)-This\Size+This\Pos, GadgetY(This\Gadget)-This\Size+This\Pos, #PB_Ignore, #PB_Ignore) : EndIf
    If This\ID[6] : ResizeGadget(This\ID[6], GadgetX(This\Gadget)+GadgetWidth(This\Gadget)-This\Pos, GadgetY(This\Gadget)-This\Size+This\Pos, #PB_Ignore, #PB_Ignore) : EndIf
    If This\ID[7] : ResizeGadget(This\ID[7], GadgetX(This\Gadget)+GadgetWidth(This\Gadget)-This\Pos, GadgetY(This\Gadget)+GadgetHeight(This\Gadget)-This\Pos, #PB_Ignore, #PB_Ignore) : EndIf
    If This\ID[8] : ResizeGadget(This\ID[8], GadgetX(This\Gadget)-This\Size+This\Pos, GadgetY(This\Gadget)+GadgetHeight(This\Gadget)-This\Pos, #PB_Ignore, #PB_Ignore) : EndIf
    If This\ID[#Arrows] : ResizeGadget(This\ID[#Arrows], GadgetX(This\Gadget)+This\Size, GadgetY(This\Gadget)-This\Size+This\Pos, #PB_Ignore, #PB_Ignore) : EndIf
  EndMacro
;   Macro MoveTransformation(This)
;     ; Transformation resize
;     If This\ID[1] : ResizeGadget(This\ID[1], GadgetX(This\Gadget, #PB_Gadget_WindowCoordinate)-This\Size+This\Pos, GadgetY(This\Gadget, #PB_Gadget_WindowCoordinate)+(GadgetHeight(This\Gadget)-This\Size)/2, #PB_Ignore, #PB_Ignore) : EndIf
;     If This\ID[2] : ResizeGadget(This\ID[2], GadgetX(This\Gadget, #PB_Gadget_WindowCoordinate)+(GadgetWidth(This\Gadget)-This\Size)/2, GadgetY(This\Gadget, #PB_Gadget_WindowCoordinate)-This\Size+This\Pos, #PB_Ignore, #PB_Ignore) : EndIf
;     If This\ID[3] : ResizeGadget(This\ID[3], GadgetX(This\Gadget, #PB_Gadget_WindowCoordinate)+GadgetWidth(This\Gadget)-This\Pos, GadgetY(This\Gadget, #PB_Gadget_WindowCoordinate)+(GadgetHeight(This\Gadget)-This\Size)/2, #PB_Ignore, #PB_Ignore) : EndIf
;     If This\ID[4] : ResizeGadget(This\ID[4], GadgetX(This\Gadget, #PB_Gadget_WindowCoordinate)+(GadgetWidth(This\Gadget)-This\Size)/2, GadgetY(This\Gadget, #PB_Gadget_WindowCoordinate)+GadgetHeight(This\Gadget)-This\Pos, #PB_Ignore, #PB_Ignore) : EndIf
;     If This\ID[5] : ResizeGadget(This\ID[5], GadgetX(This\Gadget, #PB_Gadget_WindowCoordinate)-This\Size+This\Pos, GadgetY(This\Gadget, #PB_Gadget_WindowCoordinate)-This\Size+This\Pos, #PB_Ignore, #PB_Ignore) : EndIf
;     If This\ID[6] : ResizeGadget(This\ID[6], GadgetX(This\Gadget, #PB_Gadget_WindowCoordinate)+GadgetWidth(This\Gadget)-This\Pos, GadgetY(This\Gadget, #PB_Gadget_WindowCoordinate)-This\Size+This\Pos, #PB_Ignore, #PB_Ignore) : EndIf
;     If This\ID[7] : ResizeGadget(This\ID[7], GadgetX(This\Gadget, #PB_Gadget_WindowCoordinate)+GadgetWidth(This\Gadget)-This\Pos, GadgetY(This\Gadget, #PB_Gadget_WindowCoordinate)+GadgetHeight(This\Gadget)-This\Pos, #PB_Ignore, #PB_Ignore) : EndIf
;     If This\ID[8] : ResizeGadget(This\ID[8], GadgetX(This\Gadget, #PB_Gadget_WindowCoordinate)-This\Size+This\Pos, GadgetY(This\Gadget, #PB_Gadget_WindowCoordinate)+GadgetHeight(This\Gadget)-This\Pos, #PB_Ignore, #PB_Ignore) : EndIf
;     If This\ID[#Arrows] : ResizeGadget(This\ID[#Arrows], GadgetX(This\Gadget, #PB_Gadget_WindowCoordinate)+This\Size, GadgetY(This\Gadget, #PB_Gadget_WindowCoordinate)-This\Size+This\Pos, #PB_Ignore, #PB_Ignore) : EndIf
;   EndMacro
  
  Procedure Gadget()
    ProcedureReturn ActiveGadget
  EndProcedure
  
  Procedure Update(Gadget)
    ForEach AnChor()
      If AnChor()\Gadget = Gadget
        MoveTransformation(AnChor())
      EndIf
    Next
  EndProcedure
  
  Procedure Is(Gadget.i)
    Protected I
    
    If ListSize(AnChor()) 
      ForEach AnChor()
        If AnChor() = GetGadgetData(Gadget)
         For I = 1 To 9
            If AnChor()\ID[I] = Gadget
              ProcedureReturn I
            EndIf
          Next
        EndIf
      Next
    EndIf
  EndProcedure
  
  Procedure.i GridMatch(Value.i, Grid.i, Max.i=$7FFFFFFF)
    Value = Round((Value/Grid), #PB_Round_Nearest) * Grid
    If (Value>Max) : Value=Max : EndIf
    ProcedureReturn Value
  EndProcedure
  
  Procedure Callback()
    Static Selected.i, X.i, Y.i, OffsetX.i, OffsetY.i, GadgetX0.i, GadgetX1.i, GadgetY0.i, GadgetY1.i
    Protected *Anchor.Transformation = GetGadgetData(EventGadget())
    
    With *Anchor
      Select EventType()
        Case #PB_EventType_LeftButtonDown
          Selected = #True
          GadgetX0 = GadgetX(\Gadget)
          GadgetY0 = GadgetY(\Gadget)
          GadgetX1 = GadgetX0 + GadgetWidth(\Gadget)
          GadgetY1 = GadgetY0 + GadgetHeight(\Gadget)
          OffsetX = GetGadgetAttribute(EventGadget(), #PB_Canvas_MouseX)
          OffsetY = GetGadgetAttribute(EventGadget(), #PB_Canvas_MouseY)
          ActiveGadget = \Gadget
          
        Case #PB_EventType_LeftButtonUp
          Selected = #False
        Case #PB_EventType_MouseMove
          If Selected
            X = DesktopMouseX()-(GadgetX(\Gadget, #PB_Gadget_ScreenCoordinate)-GadgetX(\Gadget, #PB_Gadget_ContainerCoordinate))-OffsetX
            Y = DesktopMouseY()-(GadgetY(\Gadget, #PB_Gadget_ScreenCoordinate)-GadgetY(\Gadget, #PB_Gadget_ContainerCoordinate))-OffsetY
            
            ; gadget resize
            Select EventGadget()
              Case \ID[1] : ResizeGadget(\Gadget, GridMatch(X+(\Size-\Pos), \Grid, GadgetX1), #PB_Ignore, GadgetX1-GridMatch(X+(\Size-\Pos), \Grid, GadgetX1), #PB_Ignore)
              Case \ID[2] : ResizeGadget(\Gadget, #PB_Ignore, GridMatch(Y+(\Size-\Pos), \Grid, GadgetY1), #PB_Ignore, GadgetY1-GridMatch(Y+(\Size-\Pos), \Grid, GadgetY1))
              Case \ID[3] : ResizeGadget(\Gadget, #PB_Ignore, #PB_Ignore, GridMatch(X+(\Size-\Pos), \Grid)-GadgetX0, #PB_Ignore)
              Case \ID[4] : ResizeGadget(\Gadget, #PB_Ignore, #PB_Ignore, #PB_Ignore, GridMatch(Y+(\Size-\Pos), \Grid)-GadgetY0)
              Case \ID[5] : ResizeGadget(\Gadget, GridMatch(X+(\Size-\Pos), \Grid, GadgetX1), GridMatch(Y+(\Size-\Pos), \Grid, GadgetY1), GadgetX1-GridMatch(X+(\Size-\Pos), \Grid, GadgetX1), GadgetY1-GridMatch(Y+(\Size-\Pos), \Grid, GadgetY1))
              Case \ID[6] : ResizeGadget(\Gadget, #PB_Ignore, GridMatch(Y+(\Size-\Pos), \Grid, GadgetY1), GridMatch(X+(\Size-\Pos), \Grid)-GadgetX0, GadgetY1-GridMatch(Y+(\Size-\Pos), \Grid, GadgetY1))
              Case \ID[7] : ResizeGadget(\Gadget, #PB_Ignore, #PB_Ignore, GridMatch(X+(\Size-\Pos), \Grid)-GadgetX0, GridMatch(Y+(\Size-\Pos), \Grid)-GadgetY0)
              Case \ID[8] : ResizeGadget(\Gadget, GridMatch(X+(\Size-\Pos), \Grid, GadgetX1), #PB_Ignore, GadgetX1-GridMatch(X+(\Size-\Pos), \Grid, GadgetX1), GridMatch(Y+(\Size-\Pos), \Grid)-GadgetY0)
              Case \ID[#Arrows] : ResizeGadget(\Gadget, GridMatch(X-\Size, \Grid), GridMatch(Y+(\Size-\Pos), \Grid), #PB_Ignore, #PB_Ignore)
            EndSelect
            
            MoveTransformation(*Anchor)
            
            CompilerIf #PB_Compiler_OS = #PB_OS_Windows
              UpdateWindow_(WindowID(GetActiveWindow()))
            CompilerEndIf 
          EndIf
      EndSelect
    EndWith
  EndProcedure
  
  Procedure Disable(Gadget.i)
    Protected I.i
    
    If ListSize(AnChor())
      ForEach AnChor()
        If AnChor()\Gadget = Gadget
          For I = 1 To 9
            If AnChor()\ID[I]
              FreeGadget(AnChor()\ID[I])
            EndIf
          Next
          
          DeleteElement(AnChor())
        EndIf
      Next
    EndIf
    
  EndProcedure
  
  Procedure Enable(Gadget.i, Grid.i=1, Flags.i=#Anchor_All)
    Protected ID.i, I.i
    Protected *Anchor.Transformation
    Protected *Cursors.DataBuffer = ?Cursors
    Protected *Flags.DataBuffer = ?Flags
      
    Disable(Gadget)
    
    *Anchor = AddElement(AnChor())
    
    With *AnChor
      \Gadget = Gadget
      \Grid = Grid
      \Pos = 3
      \Size = 6
      
      CompilerIf #PB_Compiler_OS = #PB_OS_Windows
        Static open 
        Protected ParentID = GetParent_(GadgetID(Gadget))
        Protected Parent = GetProp_( ParentID, "PB_ID" )
        If ( IsGadget( Parent ) And GadgetID( Parent ) = ParentID )
          OpenGadgetList(Parent)
          open=1
        Else
          If open
            CloseGadgetList()
            open=0
          EndIf
          UseGadgetList(ParentID) ; WindowID(GetActiveWindow()))
        EndIf
      CompilerEndIf 
    
      For I = 1 To 9
        If Flags & *Flags\ID[I] = *Flags\ID[I]
          If (I=#Arrows)
            ID = CanvasGadget(#PB_Any, 0,0, \Size*2, \Size) 
          Else
            ID = CanvasGadget(#PB_Any, 0,0, \Size, \Size)
          EndIf
          
          \ID[I] = ID
          SetGadgetData(ID, *Anchor)
          SetGadgetAttribute(ID, #PB_Canvas_Cursor, *Cursors\ID[I])
          
          If StartDrawing(CanvasOutput(ID))
            Box(0, 0, OutputWidth(), OutputHeight(), $000000)
            Box(1, 1, OutputWidth()-2, OutputHeight()-2, $FFFFFF)
            StopDrawing()
          EndIf
          
          BindGadgetEvent(ID, @Callback())
        EndIf
      Next
      
      MoveTransformation(*Anchor)
      ClipGadgets(UseGadgetList(0))
      
      CompilerIf #PB_Compiler_OS = #PB_OS_Windows
        UpdateWindow_(UseGadgetList(0))
      CompilerEndIf 
    EndWith
    
    DataSection
      Cursors:
      Data.i 0
      Data.i #PB_Cursor_LeftRight
      Data.i #PB_Cursor_UpDown
      Data.i #PB_Cursor_LeftRight
      Data.i #PB_Cursor_UpDown
      Data.i #PB_Cursor_LeftUpRightDown
      Data.i #PB_Cursor_LeftDownRightUp
      Data.i #PB_Cursor_LeftUpRightDown
      Data.i #PB_Cursor_LeftDownRightUp
      Data.i #PB_Cursor_Arrows
      
      Flags:
      Data.i 0
      Data.i #Anchor_Horizontally
      Data.i #Anchor_Vertically
      Data.i #Anchor_Horizontally
      Data.i #Anchor_Vertically
      Data.i #Anchor_Size
      Data.i #Anchor_Size
      Data.i #Anchor_Size
      Data.i #Anchor_Size
      Data.i #Anchor_Position
    EndDataSection
  EndProcedure
  
EndModule



;- Example
CompilerIf #PB_Compiler_IsMainFile
  UseModule Transformation
  
  Enumeration
    #Window
    #Transformation
    #EditorGadget
    #ContainerGadget
    #ContainerGadget2
    #ButtonGadget
    #TrackBarGadget
    #SpinGadget
    #CanvasGadget
  EndEnumeration
  
  Global Gadget =- 1
  
  Procedure OpenWindow_0()
    OpenWindow(#Window, 0, 0, 600, 400, "WindowTitle", #PB_Window_MinimizeGadget|#PB_Window_ScreenCentered)
    EditorGadget(#EditorGadget, 50, 100, 200, 50, #PB_Editor_WordWrap) : SetGadgetText(#EditorGadget, "Grumpy wizards make toxic brew for the evil Queen and Jack.")
    
    ContainerGadget(#ContainerGadget, 50, 250, 200, 125, #PB_Container_Flat)
    ContainerGadget(#ContainerGadget2, 10, 10, 200, 125, #PB_Container_Flat)
    ButtonGadget(#ButtonGadget, 10, 10, 200, 25, "Hallo Welt!", #PB_Button_MultiLine)
    CloseGadgetList()
    CloseGadgetList()
    
    TrackBarGadget(#TrackBarGadget, 350, 100, 200, 25, 0, 100) : SetGadgetState(#TrackBarGadget, 70)
    SpinGadget(#SpinGadget, 350, 180, 200, 25, 0, 100, #PB_Spin_Numeric) : SetGadgetState(#SpinGadget, 70)
    CanvasGadget(#CanvasGadget, 350, 250, 200, 25)
    
    ButtonGadget(#Transformation, 20, 20, 150, 25, "Enable Transformation", #PB_Button_Toggle)
    SetGadgetData(#CanvasGadget, 999)
  EndProcedure
  
  OpenWindow_0()
  ;OpenGadgetList(#ContainerGadget)
  Repeat
    
    Select WaitWindowEvent()
        
      Case #PB_Event_CloseWindow
        End
        
      Case #PB_Event_Repaint
        
      Case #PB_Event_Gadget
        Select EventType()
          Case #PB_EventType_LeftButtonDown
            Debug "Is anchor "+Is(EventGadget())+" "+Gadget()
            Gadget = EventGadget()
            Define OffsetX = GetGadgetAttribute(EventGadget(), #PB_Canvas_MouseX)
            Define OffsetY = GetGadgetAttribute(EventGadget(), #PB_Canvas_MouseY)
          Case #PB_EventType_LeftButtonUp
            Gadget =- 1
          Case #PB_EventType_MouseMove
            If IsGadget(Gadget) And Not Is(Gadget)
              Define X = DesktopMouseX()-(GadgetX(Gadget, #PB_Gadget_ScreenCoordinate)-GadgetX(Gadget, #PB_Gadget_WindowCoordinate))-OffsetX
              Define Y = DesktopMouseY()-(GadgetY(Gadget, #PB_Gadget_ScreenCoordinate)-GadgetY(Gadget, #PB_Gadget_WindowCoordinate))-OffsetY
              ResizeGadget(Gadget,X,Y,#PB_Ignore,#PB_Ignore)
              Update(Gadget)
            EndIf               
        EndSelect
        
        Select EventGadget()
          Case #Transformation
            Select GetGadgetState(#Transformation)
              Case #False
                SetGadgetText(#Transformation, "Enable Transformation")
                Disable(#EditorGadget)
                Disable(#ButtonGadget)
                Disable(#TrackBarGadget)
                Disable(#SpinGadget)
                Disable(#CanvasGadget)
                Disable(#ContainerGadget)
                Disable(#ContainerGadget2)
              Case #True
                SetGadgetText(#Transformation, "Disable Transformation")
                Enable(#EditorGadget, 5, #Anchor_All)
                Enable(#ButtonGadget, 1, #Anchor_All)
                Enable(#TrackBarGadget, 1, #Anchor_Position|#Anchor_Horizontally)
                Enable(#SpinGadget, 1, #Anchor_Position)
                Enable(#CanvasGadget, 1, #Anchor_All)
                Enable(#ContainerGadget, 1, #Anchor_All)
                Enable(#ContainerGadget2, 10, #Anchor_All)
            EndSelect
        EndSelect
        
    EndSelect
    
  ForEver
CompilerEndIf
Post Reply