> is "custom".. hehe
ups...
Code: Select all
;/ CustomButtons 3
;/ GPI - 28.07.2003
;/
;/ The black color (rgb(0,0,0)) is set as transparent
;/ so when you want a black boarder, use rgb(1,1,1) instead of rgb(0,0,0)
;/ IMPORTANT: All the images for a button must have the same area transparent, otherwise the background isn't erased complete
;/
;/ because the buttons ARE normal buttons, you can use
;/ DisableGadget()
;/ Also you can use the normal PB-Message-System with WaitWindowEvent() and #PB_Event_Button
;/
;/ SetGadgetText() won't work, use SetCustomButtonText()
;/ same for SetGadgetState() and GetGadgetState(), use SetCustomButtonState() and GetCustomButtonState()
;/ ResizeGadgetText() has problems with the background, so use ResizeCustomButton()
;/ same for SetGadgetFont(), use SetCustomButtonFont()
;/
;/ When you want a Custom background only for a specificy rectangle, use the
;/ CustomBack routines.
;/ IMPORTANT NOTE:
;/ CustomBacks are something like Container-Gadgets, but they are NO PB-Gadget!
;/ So you must use the given CustomBack-Routines!
;/ Also when you use a CustomBack-Procedure, you MUST activate the corresponding window with UseWindow() or they
;/ won't work.
;/ When the Brush is invalid, the function will also not work (it will crash...).
;/
;/ Original Custom Buttons by freedimension
Global OldButtonProc_
;-
#CustomBotton_Toggle=1
#CustomBotton_CheckBox=2
#CustomBotton_Option=4
Declare Callback(WindowID, Message, wParam, lParam)
Procedure CustomBackCallback(Window, Message, wParam, lParam)
Result= Callback(Window,Message,wParam,lParam)
If Result=#PB_ProcessPureBasicEvents
ProcedureReturn DefWindowProc_(Window, Message, wParam, lParam)
Else
ProcedureReturn Result
EndIf
EndProcedure
Global OldParentId_
Procedure CreateCustomBack(nr,x,y,w,h,Brush,ParentId)
OldParentId_=ParentId
WindowClass.s = "CustomBack_"+Str(nr)+"_"
wc.WNDCLASSEX
wc\cbSize = SizeOf(WNDCLASSEX)
wc\lpfnWndProc = @CustomBackCallback()
;wc\hCursor = LoadCursor_(0, #IDC_CROSS)
wc\hbrBackground = Brush
wc\lpszClassName = @WindowClass
RegisterClassEx_(@wc)
hWndMain = CreateWindowEx_(#WS_EX_TOOLWINDOW, WindowClass, "",#WS_CHILD|#WS_CLIPCHILDREN|#WS_VISIBLE , x, y, w, h, ParentId, 0, 0, 0)
;ShowWindow_(hWndMain, #SW_SHOWDEFAULT)
;UpdateWindow_(hWndMain);
UseGadgetList(hWndMain)
ProcedureReturn hWndMain
EndProcedure
Procedure CloseCustomBack()
If OldParentId_
UseGadgetList(OldParentId_)
OldParentId_=0
EndIf
EndProcedure
Procedure CustomBackId(nr)
WindowClass.s = "CustomBack_"+Str(nr)+"_"
ProcedureReturn FindWindowEx_(WindowID(),0,@WindowClass,0)
EndProcedure
Procedure FreeCustomBack(nr)
id=CustomBackId(nr)
If id
DestroyWindow_(id)
EndIf
EndProcedure
Procedure ResizeCustomBack(nr,x,y,w,h)
id=CustomBackId(nr)
If id
getwindowrect_(id,rect.RECT)
MapWindowPoints_(0,GetParent_(id),rect,2)
If x=-1
x=rect\left
EndIf
If y=-1
y=rect\top
EndIf
If w=-1
w=rect\right-rect\left
EndIf
If h=-1
h=rect\bottom-rect\top
EndIf
MoveWindow_(id,x,y,w,h,#True)
EndIf
EndProcedure
Procedure DisableCustomBackEnum_(id,flag)
EnableWindow_(id,flag)
ProcedureReturn #True
EndProcedure
Procedure DisableCustomBack(nr,flag)
If flag: flag=0 : Else : flag=1 : EndIf
id=CustomBackId(nr)
If id
EnumChildWindows_(id,@DisableCustomBackEnum_(),flag)
EnableWindow_(id,flag)
EndIf
EndProcedure
Procedure ButtonProc_(wnd,msg,wParam,lParam)
If msg=#WM_LBUTTONDBLCLK
msg=#wm_lbuttondown
EndIf
If msg=#wm_erasebkgnd
getwindowrect_(wnd,rect.RECT)
MapWindowPoints_(0,GetParent_(wnd),rect,2)
BackBrush=GetClassLong_(GetParent_(wnd), #GCL_HBRBACKGROUND)
SetBrushOrgEx_(wParam,-rect\left,-rect\top,oldpoint.POINT)
MapWindowPoints_(GetParent_(wnd),wnd,rect,2)
FillRect_(wParam,rect,BackBrush)
SetBrushOrgEx_(wParam,oldpoint\x,oldpoint\y,0)
ProcedureReturn -1
Else
ProcedureReturn CallWindowProc_(OldButtonProc_,wnd,msg,wParam,lParam)
EndIf
EndProcedure
Global CButton_OptionOffset_
Structure CustomButton
id.l
hwnd.l
text.s
NormalTColor.l
FocusTColor.l
PressedTcolor.l
DisabledTcolor.l
normal.l
focus.l
pressed.l
PFocus.l
disabled.l
state.l
Type.l
OptionOffset.l
EndStructure
NewList CButton_.CustomButton()
Procedure FreeCustomButton(id)
ResetList(CButton_())
ok=#False
Repeat
If NextElement(CButton_())
If CButton_()\id=id
FreeGadget(id)
CButton_()\text=""
DeleteElement(CButton_())
ok=#True
EndIf
Else
ok=#True
EndIf
Until ok
EndProcedure
Procedure FreeAllCustomButton()
While FirstElement(CButton_())
FreeCustomButton(CButton_()\id)
Wend
EndProcedure
Procedure CustomButton(id,x,y,w,h,text.s,NormalTColor,FocusTColor,PressedTcolor,DisabledTcolor,FontId,normal,focus,pressed,PFocus,disabled,Type); for tcolor and fontid you can use -1 (#pb_default) for Systemdefaults
ResetList(CButton_())
ok=#False
Repeat
If NextElement(CButton_())
If CButton_()\id=id
FreeCustomButton(id)
ok=#True
EndIf
Else
ok=#True
EndIf
Until ok
AddElement(CButton_())
CButton_()\id=id
CButton_()\text=text
CButton_()\NormalTColor=NormalTColor
CButton_()\FocusTColor=FocusTColor
CButton_()\PressedTcolor=PressedTcolor
CButton_()\DisabledTcolor=DisabledTcolor
CButton_()\normal =normal
CButton_()\focus =focus
CButton_()\pressed =pressed
CButton_()\PFocus =PFocus
CButton_()\disabled=disabled
CButton_()\Type =Type
CButton_()\OptionOffset=CButton_OptionOffset_
CButton_()\hwnd=ButtonGadget(id,x,y,w,h,"",0);bs_notify
SetGadgetFont(id,FontId)
SetWindowLong_(CButton_()\hwnd,#gwl_style,#BS_OWNERDRAW|#WS_CHILD|#ws_tabstop|#WS_VISIBLE)
OldButtonProc_=SetWindowLong_(CButton_()\hwnd,#GWL_WNDPROC,@ButtonProc_())
;Debug GetWindowLong_(CButton_()\hwnd,#gwl_exstyle)
ProcedureReturn CButton_()\hwnd
EndProcedure
Procedure SetCustomButtonImage(id,normal,focus,pressed,PFocus,disabled) ; -1=no change!
ResetList(CButton_())
ok=#False
Repeat
If NextElement(CButton_())
If CButton_()\id=id
If normal<>-1
CButton_()\normal = normal
EndIf
If pressed<>-1
CButton_()\pressed = pressed
EndIf
If focus<>-1
CButton_()\focus = focus
EndIf
If PFocus<>-1
CButton_()\PFocus = PFocus
EndIf
If disabled<>-1
CButton_()\disabled= disabled
EndIf
RedrawWindow_(CButton_()\hwnd, 0, 0,#rdw_erase|#RDW_INTERNALPAINT|#RDW_INVALIDATE|#RDW_ALLCHILDREN )
ok=#True
EndIf
Else
ok=#True
EndIf
Until ok
EndProcedure
Procedure SetCustomButtonFont(id,FontId)
SetGadgetFont(id,FontId)
RedrawWindow_(GadgetID(id), 0, 0,#rdw_erase|#RDW_INTERNALPAINT|#RDW_INVALIDATE|#RDW_ALLCHILDREN )
EndProcedure
Procedure SetCustomButtonText(id,text.s)
ResetList(CButton_())
ok=#False
Repeat
If NextElement(CButton_())
If CButton_()\id=id
CButton_()\text=text
RedrawWindow_(CButton_()\hwnd, 0, 0,#rdw_erase|#RDW_INTERNALPAINT|#RDW_INVALIDATE|#RDW_ALLCHILDREN )
ok=#True
EndIf
Else
ok=#True
EndIf
Until ok
EndProcedure
Procedure GetCustomButtonState(id)
Result=0
ResetList(CButton_())
ok=#False
Repeat
If NextElement(CButton_())
If CButton_()\id=id
Result=CButton_()\state
ok=#True
EndIf
Else
ok=#True
EndIf
Until ok
ProcedureReturn Result
EndProcedure
Procedure SetCustomButtonState(id,state)
If state: state=1 : EndIf
ResetList(CButton_())
ok=#False
Repeat
If NextElement(CButton_())
If CButton_()\id=id
CButton_()\state=state
RedrawWindow_(CButton_()\hwnd, 0, 0,#RDW_INTERNALPAINT|#RDW_INVALIDATE|#RDW_ALLCHILDREN )
ok=#True
EndIf
Else
ok=#True
EndIf
Until ok
EndProcedure
Procedure SetCustomButtonTextColor(id,NormalTColor,FocusTColor,PressedTcolor,DisabledTcolor); -1=Default: -2=nochange
ResetList(CButton_())
ok=#False
Repeat
If NextElement(CButton_())
If CButton_()\id=id
If NormalTColor>-2
CButton_()\NormalTColor=NormalTColor
EndIf
If FocusTColor>-2
CButton_()\FocusTColor=FocusTColor
EndIf
If PressedTcolor>-2
CButton_()\PressedTcolor=PressedTcolor
EndIf
If DisabledTcolor>-2
CButton_()\DisabledTcolor=DisabledTcolor
EndIf
RedrawWindow_(CButton_()\hwnd, 0, 0,#RDW_INTERNALPAINT|#RDW_INVALIDATE|#RDW_ALLCHILDREN )
ok=#True
EndIf
Else
ok=#True
EndIf
Until ok
EndProcedure
Procedure ResizeCustomButton(id,x,y,w,h)
If GetFocus_()=GadgetID(id)
ok=#True
EndIf
HideGadget(id,#True)
ResizeGadget(id,x,y,w,h)
HideGadget(id,#False)
If ok
SetFocus_(GadgetID(id))
EndIf
EndProcedure
Procedure StartCustomButtonOption()
CButton_OptionOffset_+1
EndProcedure
;-
Procedure RemoveLine(wParam,wnd,x1,y1,x2,y2)
getwindowrect_(wnd,rect.RECT)
MapWindowPoints_(0,GetParent_(wnd),rect,2)
BackBrush=GetClassLong_(GetParent_(wnd), #GCL_HBRBACKGROUND)
SetBrushOrgEx_(wParam,-rect\left,-rect\top,oldpoint.POINT)
; point.POINT\x=x1:point\y=y1
; MapWindowPoints_(wnd,GetParent_(wnd),point,1)
; BackBrush=GetClassLong_(GetParent_(wnd), #GCL_HBRBACKGROUND)
; SetBrushOrgEx_(wParam,-point\x,-point\y,oldpoint.POINT)
rect.RECT\left=x1:rect\right=x2+10
rect\top=y1:rect\bottom=y2+100
FillRect_(wParam,rect,BackBrush)
SetBrushOrgEx_(wParam,oldpoint\x,oldpoint\y,0)
EndProcedure
Procedure Callback(WindowID, Message, wParam, lParam)
Result = #PB_ProcessPureBasicEvents
If Message = #wm_command And wParam>>16&$ffff=#bn_clicked
ResetList(CButton_())
ok=#False
Repeat
If NextElement(CButton_())
If CButton_()\hwnd=lParam
If CButton_()\Type&#CustomBotton_Option
id =CButton_()\id
OptionOffset=CButton_()\OptionOffset
CButton_()\state=1
RedrawWindow_(CButton_()\hwnd, 0, 0,#RDW_INTERNALPAINT|#RDW_INVALIDATE|#RDW_ALLCHILDREN )
ResetList(CButton_())
While NextElement(CButton_())
If CButton_()\Type=#CustomBotton_Option And CButton_()\OptionOffset=OptionOffset And CButton_()\id<>id And CButton_()\state
CButton_()\state=0
RedrawWindow_(CButton_()\hwnd, 0, 0,#RDW_INTERNALPAINT|#RDW_INVALIDATE|#RDW_ALLCHILDREN )
EndIf
Wend
ElseIf CButton_()\Type&(#CustomBotton_Toggle|#CustomBotton_CheckBox)
CButton_()\state=1-CButton_()\state
RedrawWindow_(CButton_()\hwnd, 0, 0,#RDW_INTERNALPAINT|#RDW_INVALIDATE|#RDW_ALLCHILDREN )
EndIf
ok=#True
EndIf
Else
ok=#True
EndIf
Until ok
EndIf
If Message = #WM_DRAWITEM
*dis.DRAWITEMSTRUCT = lParam
If *dis\CtlType<>#ODT_BUTTON
Debug "hä"
Else
ResetList(CButton_())
ok=#False
Repeat
If NextElement(CButton_())
If CButton_()\hwnd=*dis\hwndItem
pic=CButton_()\normal
TColor=CButton_()\NormalTColor
If *dis\itemState & #ODS_Disabled
;{Disabled
pic=CButton_()\disabled
If CButton_()\DisabledTcolor>-1
TColor=CButton_()\DisabledTcolor
EndIf
;}
ElseIf CButton_()\Type=#CustomBotton_Toggle
;{Toggle-Button
state=CButton_()\state
If *dis\itemState & #ODS_SELECTED: state=1-state : EndIf
If state=0
If *dis\itemState & #ODS_FOCUS
pic=CButton_()\focus
If CButton_()\FocusTColor>-1
TColor=CButton_()\FocusTColor
EndIf
EndIf
Else
If *dis\itemState & #ODS_FOCUS
pic=CButton_()\PFocus
If CButton_()\FocusTColor>-1
TColor=CButton_()\FocusTColor
EndIf
Else
pic=CButton_()\pressed
If CButton_()\FocusTColor>-1
TColor=CButton_()\FocusTColor
EndIf
EndIf
EndIf
;}
ElseIf CButton_()\Type&(#CustomBotton_CheckBox|#CustomBotton_Option)
;{Checkbox
If *dis\itemState & #ODS_FOCUS
If CButton_()\FocusTColor>-1
TColor=CButton_()\FocusTColor
EndIf
EndIf
state=CButton_()\state
If state=0
If *dis\itemState & #ODS_SELECTED
pic=CButton_()\focus
EndIf
Else
If *dis\itemState & #ODS_SELECTED
pic=CButton_()\PFocus
Else
pic=CButton_()\pressed
EndIf
EndIf
;}
ElseIf *dis\itemState & #ODS_SELECTED
;{normal
pic=CButton_()\PFocus
If CButton_()\PressedTcolor>-1
TColor=CButton_()\PressedTcolor
EndIf
ElseIf *dis\itemState & #ODS_FOCUS
pic=CButton_()\focus
If CButton_()\FocusTColor>-1
TColor=CButton_()\FocusTColor
EndIf
;}
EndIf
oldmode=SetBkMode_(*dis\hDC, #TRANSPARENT)
GetObject_(pic,SizeOf(BITMAP),bmp.BITMAP)
If pic
offy=((*dis\rcItem\bottom-*dis\rcItem\top)-bmp\bmHeight)/2
imglist=ImageList_Create_(bmp\bmWidth,bmp\bmHeight,#ILC_COLORDDB|#ILC_MASK,1,0)
ImageList_AddMasked_(imglist,pic,0)
ImageList_Draw_(imglist,0,*dis\hDC,*dis\rcItem\left,*dis\rcItem\top+offy,#ILD_TRANSPARENT )
ImageList_Destroy_(imglist)
EndIf
GetTextExtentPoint32_(*dis\hDC,@CButton_()\text,Len(CButton_()\text),size.SIZE)
h=((*dis\rcItem\bottom-*dis\rcItem\top )-size\cy)/2
If CButton_()\Type&( #CustomBotton_CheckBox|#CustomBotton_Option)
w=bmp\bmWidth+2
x1=*dis\rcItem\left+w-1
y1=*dis\rcItem\top+h-1
x2=*dis\rcItem\left+w+size\cx+1
y2=*dis\rcItem\top+h+size\cy+1
If *dis\itemState & #ODS_FOCUS
hpen=createpen_(#PS_DOT,1,TColor)
SelectObject_(*dis\hDC,hpen)
movetoex_(*dis\hDC,x1,y1,oldpoint.POINT)
lineto_(*dis\hDC,x2,y1)
lineto_(*dis\hDC,x2,y2)
lineto_(*dis\hDC,x1,y2)
lineto_(*dis\hDC,x1,y1)
DeleteObject_(hpen)
Else
RemoveLine(*dis\hDC,*dis\hwndItem,x1,y1,x2,y1)
RemoveLine(*dis\hDC,*dis\hwndItem,x2,y1,x2,y2)
RemoveLine(*dis\hDC,*dis\hwndItem,x2,y2,x1,y2)
RemoveLine(*dis\hDC,*dis\hwndItem,x1,y2,x1,y1)
EndIf
Else
w=((*dis\rcItem\right -*dis\rcItem\left)-size\cx)/2
EndIf
If TColor>-1
SetTextColor_(*dis\hDC, TColor)
EndIf
TextOut_(*dis\hDC,*dis\rcItem\left+w,*dis\rcItem\top+h,@CButton_()\text,Len(CButton_()\text))
SetBkMode_(*dis\hDC,oldmode)
Result=#True
ok=#True
EndIf
Else
ok=#True
EndIf
Until ok
EndIf
EndIf
ProcedureReturn Result
EndProcedure
OpenWindow(1, 10, 150, 256, 256, #PB_Window_TitleBar|#PB_Window_SystemMenu, "Button OwnerDraw")
SetWindowCallback(@Callback())
normal = LoadImage(1, "lang-normal.bmp")
focus = LoadImage(2, "lang-normal-focus.bmp")
pressed = LoadImage(3, "lang-pressed.bmp")
disabled = LoadImage(4, "lang-disabled.bmp")
PFocus = LoadImage(5, "lang-pressed-focus.bmp")
back = LoadImage(6, "background2.bmp")
chk_normal = LoadImage( 7,"check-normal.bmp")
chk_Pnormal = LoadImage( 8,"check-normal-pressed.bmp")
chk_checked = LoadImage( 9,"check-checked.bmp")
chk_Pchecked= LoadImage(10,"check-checked-pressed.bmp")
chk_disabled= LoadImage(11,"check-disabled.bmp")
opt_normal = LoadImage(12,"option-normal.bmp")
opt_Pnormal = LoadImage(13,"option-normal-pressed.bmp")
opt_select = LoadImage(14,"option-select.bmp")
opt_Pselect = LoadImage(15,"option-select-pressed.bmp")
opt_disabled= LoadImage(16,"option-disabled.bmp")
back2=LoadImage(17,"background.bmp")
back3=LoadImage(18,"background3.bmp")
BackBrush = CreatePatternBrush_(back)
BackBrush2= CreatePatternBrush_(back2)
BackBrush3= CreatePatternBrush_(back3)
SetClassLong_(WindowID(), #GCL_HBRBACKGROUND, BackBrush)
CreateGadgetList(WindowID())
CustomButton(1, 5, 5,100, 20,"Button 1",RGB(255,255,255),#PB_Default ,#PB_Default ,#PB_Default ,LoadFont(1,"Courier New",10) , normal,focus ,pressed,PFocus,disabled,0)
CustomButton(2, 5,25*1+5,100, 20,"unpressed",RGB(255,255,255),#PB_Default ,RGB(0,0,0) ,#PB_Default , #PB_Default , normal,focus ,pressed,PFocus,disabled,#CustomBotton_Toggle)
CustomButton(3, 5,25*2+5,100, 20,"Disabled",RGB(255,255,255),RGB(255,255,0),RGB(128,128,0),RGB(0,0,0) , #PB_Default , normal,focus ,pressed,PFocus,disabled,Type)
CustomButton(4, 5,25*3+5,100, 20,"uncheck" ,RGB(255,255,255),RGB(255,255,0),RGB(255,255,0),RGB(128,128,128), #PB_Default ,chk_normal,chk_Pnormal,chk_checked,chk_Pchecked,chk_disabled,#CustomBotton_CheckBox)
CreateCustomBack(11, 5,25*4+5,200,25*2+20,BackBrush2,WindowID())
StartCustomButtonOption()
CustomButton(5, 5,25*0,100, 20,"Option 1",RGB(255,128,128),RGB(128,255,0),RGB(128,255,0),RGB(128,128,128), #PB_Default ,opt_normal,opt_Pnormal,opt_select ,opt_Pselect ,opt_disabled,#CustomBotton_Option)
CustomButton(6, 5,25*1,100, 20,"Option 1",RGB(255,128,128),RGB(128,255,0),RGB(128,255,0),RGB(128,128,128), #PB_Default ,opt_normal,opt_Pnormal,opt_select ,opt_Pselect ,opt_disabled,#CustomBotton_Option)
CustomButton(7, 5,25*2,100, 20,"Option 1",RGB(255,128,128),RGB(128,255,0),RGB(128,255,0),RGB(128,128,128), #PB_Default ,opt_normal,opt_Pnormal,opt_select ,opt_Pselect ,opt_disabled,#CustomBotton_Option)
CloseCustomBack()
CreateCustomBack(12, 5,25*7+5,200,25*2+20,BackBrush3,WindowID())
StartCustomButtonOption()
CustomButton(8, 5,0,100, 20,"Option 2",RGB(255,255,255),RGB(255,255,0),RGB(255,255,0),RGB(128,128,128), #PB_Default ,opt_normal,opt_Pnormal,opt_select ,opt_Pselect ,opt_disabled,#CustomBotton_Option)
CustomButton(9, 5,25*1,100, 20,"Option 2",RGB(255,255,255),RGB(255,255,0),RGB(255,255,0),RGB(128,128,128), #PB_Default ,opt_normal,opt_Pnormal,opt_select ,opt_Pselect ,opt_disabled,#CustomBotton_Option)
CustomButton(10, 5,25*2,100, 20,"Option 2",RGB(255,255,255),RGB(255,255,0),RGB(255,255,0),RGB(128,128,128), #PB_Default ,opt_normal,opt_Pnormal,opt_select ,opt_Pselect ,opt_disabled,#CustomBotton_Option)
CloseCustomBack()
RectRgn = CreateRoundRectRgn_(0, 0, 200,25*2+20, 20, 20)
SetWindowRgn_(CustomBackId(12), RectRgn, #True)
DisableGadget(3,#True)
SetCustomButtonState(5,#True)
SetCustomButtonState(8,#True)
DeleteObject_(RectRgn)
DeleteObject_(BackBrush)
DeleteObject_(BackBrush2)
DeleteObject_(BackBrush3)
state=0
Repeat
event = WaitWindowEvent()
If event=#PB_Event_Gadget
;Debug "Pressed"
If EventType()=#PB_EventType_LeftClick
Select EventGadgetID()
Case 1
state=1-state
DisableCustomBack(12,state)
;resizeCustomBack(12,Random(100),-1,-1,-1)
Case 2
SetCustomButtonText(2, StringField("unpressed pressed",GetCustomButtonState(2)+1," "))
Case 4
SetCustomButtonText(4, StringField("unchecked checked",GetCustomButtonState(4)+1," "))
EndSelect
EndIf
EndIf
Until event = #PB_Event_CloseWindow
FreeAllCustomButton()