Code: Select all
; First steps with BindEvent() and BindGadgetEvent()
EnableExplicit
Declare CountListiconColumn(GadNum, maxcolumn, keyword.s)
Declare SetGadgetResize(WinNum,GadNum,FlagX,FlagY,FlagWidth,FlagHeight,AdjustCol=#False)
Declare EventHandler_WinResize()
Declare EventHandler_ScrollBar()
Global NumReSizeGads.l,no.l
Procedure CountListiconColumn(GadNum, maxcolumn, keyword.s)
If IsGadget(GadNum)
AddGadgetColumn(GadNum, maxcolumn, keyword, 0)
For no=0 To maxcolumn
If GetGadgetItemText(GadNum, -1, no)=keyword
Break
EndIf
Next
RemoveGadgetColumn(GadNum, no)
ProcedureReturn no
EndIf
EndProcedure
Procedure SetGadgetResize(WinNum,GadNum,FlagX,FlagY,FlagWidth,FlagHeight,AdjustCol=#False)
Protected Attrib.l,ColW.l,ColCount.w,Win.i
Static *P,*M,ColTableSize.l = 1024
; Memory to hold number of columns and original sizes.
; NumCol.w,Wid0.w,Wid1.w,Wid2.w,Wid3.w,....
If *P = 0 : *M = AllocateMemory(ColTableSize) : *P = *M : EndIf
Structure RESIZER
WinNum.l
OldWindowHeight.w
OldWindowWidth.w
GadNum.l
OriginalGadgetWidth.w
OriginalGadgetHeight.w
FlagX.w
FlagY.w
FlagW.w
FlagH.w
SpecGadData.i
EndStructure
; Create structure to hold gadget data
If NumReSizeGads = 0
NumReSizeGads = 1
Global Dim ReSize.RESIZER(1)
Else
NumReSizeGads + 1
ReDim ReSize(NumReSizeGads)
EndIf
; Add a gadget to the list of gadgets that are to be re-sized.
With ReSize(NumReSizeGads)
\WinNum = WinNum
\OldWindowHeight = WindowHeight(WinNum)
\OldWindowWidth = WindowWidth(WinNum)
\GadNum = GadNum
\OriginalGadgetWidth = GadgetWidth(\GadNum)
\OriginalGadgetHeight = GadgetHeight(\GadNum)
\FlagX = FlagX & 1
\FlagY = FlagY & 1
\FlagW = FlagWidth & 1
\FlagH = FlagHeight & 1
; Flag gadgets with columns that will need width adjustment
Attrib = 0
If AdjustCol & 1
Select GadgetType(GadNum)
Case #PB_GadgetType_ListIcon : Attrib = #PB_ListIcon_ColumnWidth
Case #PB_GadgetType_ExplorerList : Attrib = #PB_Explorer_ColumnWidth
EndSelect
EndIf
If Attrib
\SpecGadData = *P ; Start of data area for this gadget.
ColCount = 0
ColW = GetGadgetItemAttribute(\GadNum,0,Attrib,ColCount) ; First column width
CountListiconColumn(\GadNum, 32000, "RichardL Column No")
While ColCount < no ; If a real column...
PokeW((*P+2) + (ColCount*2),ColW) ; keep it's width
ColCount + 1 ; count the columns
ColW = GetGadgetItemAttribute(\GadNum,0,Attrib,ColCount) ; get next width.
Wend
PokeW(\SpecGadData,ColCount) ; Keep the number of columns.
*P + ((ColCount+1)*2) ; Update data area pointer for next gadget
ColTableSize + ((ColCount+1)*2)
*M = ReAllocateMemory(*M,ColTableSize)
Debug ColTableSize
Else
\SpecGadData = 0
EndIf
EndWith
EndProcedure
Procedure EventHandler_WinResize()
Protected dx.w,dy.w,n.w,NewX.w,NewY.w,NewW.w,NewH.w,Win.i,DoOnce.l,f.f,Col.w,ColCount.w,*P
Win = EventWindow()
DoOnce = #False
For n = 1 To NumReSizeGads
If ReSize(n)\WinNum = Win ; A window is being resized, is it one we are monitoring?
With ReSize(n)
If DoOnce = #False
; How much has window size changed?
dy = WindowHeight(ReSize(n)\WinNum) - ReSize(n)\OldWindowHeight
dx = WindowWidth(ReSize(n)\WinNum) - ReSize(n)\OldWindowWidth
; Save the new window size.
\OldWindowHeight = WindowHeight(ReSize(n)\WinNum)
\OldWindowWidth = WindowWidth(ReSize(n)\WinNum)
DoOnce = #True
EndIf
; Adjust gadget sizes and positions
NewX = (dx * \FlagX) + GadgetX(\GadNum)
NewY = (dy * \FlagY) + GadgetY(\GadNum)
NewW = (dx * \FlagW) + GadgetWidth(\GadNum)
NewH = (dy * \FlagH) + GadgetHeight(\GadNum)
ResizeGadget(\GadNum,NewX,NewY,NewW,NewH)
; Adjust column widths of ListIconGadget() and ExplorerListGadget()
If \SpecGadData And dx<>0
f.f = GadgetWidth(\GadNum) / \OriginalGadgetWidth
ColCount = PeekW(\SpecGadData)
*P = \SpecGadData + 2
For Col = 0 To ColCount-1
SetGadgetItemAttribute(\GadNum,0,#PB_ListIcon_ColumnWidth,PeekW(*P)*f,Col)
*P + 2
Next
EndIf
; For WINDOWS
CompilerIf #PB_OS_Windows
InvalidateRect_(WindowID(\WinNum),0,0) ; ***** Fixes broken gadgets
While WindowEvent() : Wend ; ***** Improves rendering of EditorGadget()
CompilerEndIf
EndWith
EndIf
Next
EndProcedure
Procedure EventHandler_ScrollBar()
Select EventGadget()
Case 7
SetGadgetText(6,Str(GetGadgetState(7)))
Case 8
SetGadgetText(9,Str(GetGadgetState(8)))
EndSelect
EndProcedure
DisableExplicit
; Test code
; Window 1
OpenWindow(1,100,100,200,90,"Re-Size my height",#PB_Window_SystemMenu|#PB_Window_SizeGadget)
SmartWindowRefresh(1,1)
SetWindowColor(1,RGB(255,255,0))
WindowBounds(1,200,90,200,400)
EditorGadget(1,5,5,190,20) : SetGadgetResize(1,1,0,0,0,1)
ButtonGadget(2,5,40,190,20,"Button 2") : SetGadgetResize(1,2,0,1,0,0)
; Window 2
OpenWindow(2,420,120,425,110,"Re-Size Me 2 ways",#PB_Window_SystemMenu|#PB_Window_SizeGadget)
SetWindowColor(2,RGB(0,255,255))
WindowBounds(2,425,110,800,400)
EditorGadget(3,5,5,190,20) : SetGadgetResize(2,3,0,0,0,1)
For n = 1 To 50
AddGadgetItem(3,-1,Str(Random(1000000)))
Next
ListIconGadget(4,200,5,195,50,"Title",60,#PB_ListIcon_GridLines) ; Create the LIG()
For x = 0 To 9 ; Add some columns
AddGadgetColumn(4,60+(x*20),Str(x),20+x)
Next
SetGadgetResize(2,4,0,0,1,1,#True) ; Specify resizing AFTER adding the columns!
For y = 0 To 9
AddGadgetItem(4,-1,"")
For x = 0 To 10
SetGadgetItemText(4,y,"X"+Str(x)+" Y"+Str(y),x)
Next
Next
ButtonGadget(5,5,40,190,40,"Button 4") : SetGadgetResize(2,5,0,1,0,0)
StringGadget(6,340,85,50,20,"---") : SetGadgetResize(2,6,1,1,0,0)
ScrollBarGadget(7,400,5,21,80,0,1023,1, #PB_ScrollBar_Vertical) : SetGadgetResize(2,7,1,0,0,1)
SetGadgetState(7,512)
ScrollBarGadget(8,200,60,195,20,0,2048,1): SetGadgetResize(2,8,0,1,1,0)
SetGadgetState(8,1024)
StringGadget(9,200,85,50,20,"---") : SetGadgetResize(2,9,0,1,0,0)
; Bind various items to specific event handlers
BindEvent(#PB_Event_SizeWindow,@EventHandler_WinResize())
BindGadgetEvent(7,@EventHandler_ScrollBar())
BindGadgetEvent(8,@EventHandler_ScrollBar())
Repeat
; Your event management here
Until WaitWindowEvent() = #PB_Event_CloseWindow