[PB 6.00] Another TabBarGadget [Source]
Posted: Tue Jan 03, 2023 6:49 pm
Another TabBarGadget
Maybe this is useful for someone
Its a more simplistic implementation of a TabBarGadget.
I did not use any platform specific code so this should also work on Linux and CheeseburgerOS.
If you look for something professional i suggest this:
http://forums.purebasic.com/english/vie ... f236ee0443
NEW Version (dev. 1.1):
http://forums.purebasic.com/english/vie ... 0e#p593390
Version (dev. 1.0):
http://forums.purebasic.com/english/vie ... 18#p593357
Version (dev. 0.5):
How it looks:

Example:
Code:
Download (sources + images):
https://www.dropbox.com/s/jt7gtkyoo057e ... b.zip?dl=0
Maybe this is useful for someone

Its a more simplistic implementation of a TabBarGadget.
I did not use any platform specific code so this should also work on Linux and CheeseburgerOS.
If you look for something professional i suggest this:
http://forums.purebasic.com/english/vie ... f236ee0443
NEW Version (dev. 1.1):
http://forums.purebasic.com/english/vie ... 0e#p593390
Version (dev. 1.0):
http://forums.purebasic.com/english/vie ... 18#p593357
Version (dev. 0.5):
How it looks:

Example:
Code: Select all
EnableExplicit
XIncludeFile "gadget_tab.pbi"
Procedure.i Main()
Protected flags.i
Protected btn.i
Protected tab.i
Protected cnt.i
flags|#PB_Window_SystemMenu|#PB_Window_ScreenCentered|#PB_Window_MinimizeGadget
flags|#PB_Window_MaximizeGadget|#PB_Window_SizeGadget
If OpenWindow(0,0,0,600,400,#Null$,flags)
btn = ButtonGadget(#PB_Any,200,100,200,30,"Add Tab")
tab = TabGadgetCreate(0,0,0)
TabGadgetAdd(tab,"Tab 1",#True,123)
TabGadgetAdd(tab,"Tab 2",#False,456)
TabGadgetAdd(tab,"Tab 3",#False,789)
Repeat
Select WaitWindowEvent()
Case #TAB_EVENT
Select TabGadgetEvent()
Case #TAB_EVENT_CLOSED
Debug "Closed: " + Str(TabGadgetEventData())
Case #TAB_EVENT_SELECTED
Debug "Selected: " + Str(TabGadgetEventData())
EndSelect
Case #PB_Event_Gadget
Select EventGadget()
Case tab
TabGadgetUpdate(tab)
Case btn
TabGadgetAdd(tab,"Hello " + Str(cnt),#False,cnt)
cnt + 1
EndSelect
Case #PB_Event_SizeWindow
TabGadgetResize(tab,WindowWidth(0))
Case #PB_Event_CloseWindow
Break
EndSelect
ForEver
CloseWindow(0)
EndIf
ProcedureReturn #Null
EndProcedure
Main()
End
Code:
Code: Select all
EnableExplicit
;TabGadget
;Version: dev 0.5
;Author: Mijikai
;Note:
;- Most things are hardcoded!
;- Tabs can be scrolled with the mouse wheel and the left right buttons
;The left and right buttons have two modes (mouse left click/right click) ;)
UsePNGImageDecoder()
Structure TAB_RECT_STRUCT
x.i
y.i
w.i
h.i
EndStructure
Structure TAB_IMG_STRUCT
id.i[7]
EndStructure
Structure TAB_ITEM_STRUCT
text.s
custom.i
rect.TAB_RECT_STRUCT
EndStructure
Structure TAB_STRUCT
window.i
font.i
canvas.i
color.i
*image.TAB_IMG_STRUCT
rect.TAB_RECT_STRUCT
*active.TAB_ITEM_STRUCT
start.i
stop.i
min.i
max.i
offset.i
List item.TAB_ITEM_STRUCT()
EndStructure
#TAB_EVENT = #PB_Event_FirstCustomValue
#TAB_EVENT_SELECTED = 1
#TAB_EVENT_CLOSED = 0
Procedure.i TabWindowColor(Window.i,*Color.Integer)
If StartDrawing(WindowOutput(Window))
*Color\i = Point(0,0)
StopDrawing()
ProcedureReturn #True
EndIf
ProcedureReturn #False
EndProcedure
Procedure.i TabGadgetCreate(Window.i,X.i,Y.i,Color.i = -1)
Static init.i
Static font.i
Static img.TAB_IMG_STRUCT
Protected *tab.TAB_STRUCT
Protected save.i
With *tab
If Not init
font = LoadFont(#PB_Any,"Consolas",10)
If font
img\id[0] = CatchImage(#PB_Any,?tab_img_0)
img\id[1] = CatchImage(#PB_Any,?tab_img_1)
img\id[2] = CatchImage(#PB_Any,?tab_img_2)
img\id[3] = CatchImage(#PB_Any,?tab_img_3)
img\id[4] = CatchImage(#PB_Any,?tab_img_4)
img\id[5] = CatchImage(#PB_Any,?tab_img_5)
img\id[6] = CatchImage(#PB_Any,?tab_img_6)
For save = 0 To 6
If img\id[save]
init + 1
EndIf
Next
init = Bool(init = 7)
EndIf
EndIf
If init And Window < 0
For save = 0 To 6
If \image\id[save]
FreeImage(\image\id[save])
EndIf
Next
If font
FreeFont(font)
EndIf
init = #False
EndIf
If IsWindow(Window) And init
save = UseGadgetList(WindowID(Window))
*tab = AllocateStructure(TAB_STRUCT)
If *tab
\window = Window
\image = @img
\font = font
\rect\x = X
\rect\y = Y
\rect\h = 30
\rect\w = WindowWidth(Window,#PB_Window_InnerCoordinate)
\canvas = CanvasGadget(#PB_Any,\rect\x,\rect\y,\rect\w,\rect\h)
If \canvas
\color = Color
If Color < 0
TabWindowColor(window,@\color)
EndIf
UseGadgetList(save)
SetGadgetData(\canvas,*tab)
ProcedureReturn \canvas
EndIf
FreeStructure(*tab)
UseGadgetList(save)
EndIf
EndIf
ProcedureReturn #Null
EndWith
EndProcedure
Procedure.i TabGadgetUpdate(Gadget.i)
Protected *tab.TAB_STRUCT
Protected mouse_click.i
Protected mouse_right.i
Protected mouse_wheel.i
Protected mouse_x.i
Protected mouse_y.i
Protected highlight.i
Protected offset.i
Protected close.i
With *tab
*tab = GetGadgetData(Gadget)
mouse_x = GetGadgetAttribute(\canvas,#PB_Canvas_MouseX)
mouse_y = GetGadgetAttribute(\canvas,#PB_Canvas_MouseY)
Select EventType()
Case #PB_EventType_LeftClick
mouse_click = #True
Case #PB_EventType_RightClick
mouse_right = #True
Case #PB_EventType_MouseLeave
mouse_x = 0
mouse_y = 0
EndSelect
mouse_wheel = GetGadgetAttribute(\canvas,#PB_Canvas_WheelDelta)
If mouse_wheel < 0
mouse_wheel = -128
ElseIf mouse_wheel > 0
mouse_wheel = 128
EndIf
\start + mouse_wheel
If \start < \min
\start = \min
ElseIf \start > \max
\start = \max
EndIf
\offset = \start + 1
If StartDrawing(CanvasOutput(\canvas))
DrawingFont(FontID(\font))
Box(0,0,OutputWidth(),OutputHeight(),\color)
ForEach \item()
DrawingMode(#PB_2DDrawing_AllChannels)
highlight = Bool(mouse_x > \offset And mouse_x < (\offset + 128) And mouse_y > 5 And mouse_x < (\rect\w - 64))
If highlight
\item()\rect\x = \offset + 128 - 21
\item()\rect\y = 11 - (highlight << 1)
\item()\rect\w = \item()\rect\x + 14
\item()\rect\h = \item()\rect\y + 14
If mouse_x > \item()\rect\x And mouse_x < \item()\rect\w And mouse_y > \item()\rect\y And mouse_y < \item()\rect\h And mouse_x < (\rect\w - 64)
close = 1 + mouse_click
ElseIf mouse_click
\active = @\item()
PostEvent(#TAB_EVENT,1,\item()\custom)
EndIf
ElseIf \active = @\item()
highlight = 1
EndIf
If close = 2
If \active = @\item()
\active = 0
EndIf
PostEvent(#TAB_EVENT,0,\item()\custom)
\stop - 128
\min = - (\stop - 128)
DeleteElement(\item())
mouse_click = #False
Else
offset = highlight << 1
DrawAlphaImage(ImageID(\image\id[0 + highlight + close]),\offset,5 - offset)
DrawingMode(#PB_2DDrawing_Transparent)
DrawText(\offset + 5,10 - offset,\item()\text,$666666)
\offset + 128
EndIf
close = 0
Next
DrawingMode(#PB_2DDrawing_AllChannels)
offset = 0
highlight = 0
If mouse_y > 2
If mouse_x > (\rect\w - 32) And mouse_x < \rect\w
offset = 1
If mouse_click
\start - 128
ElseIf mouse_right
\start = \min
EndIf
ElseIf mouse_x > (\rect\w - 64) And mouse_x < (\rect\w - 32)
highlight = 1
If mouse_click
\start + 128
ElseIf mouse_right
\start = \max
EndIf
EndIf
EndIf
DrawAlphaImage(ImageID(\image\id[3 + highlight]),\rect\w - 64,2)
DrawAlphaImage(ImageID(\image\id[5 + offset]),\rect\w - 32,2)
StopDrawing()
ProcedureReturn #True
EndIf
ProcedureReturn #False
EndWith
EndProcedure
Procedure.i TabGadgetRelease(Gadget.i)
Protected *tab.TAB_STRUCT
With *tab
*Tab = GetGadgetData(Gadget)
FreeGadget(\canvas)
FreeStructure(*Tab)
ProcedureReturn #Null
EndWith
EndProcedure
Procedure.i TabGadgetDestroy()
TabGadgetCreate(-1,#Null,#Null)
ProcedureReturn #Null
EndProcedure
Procedure.i TabGadgetEvent()
ProcedureReturn EventWindow()
EndProcedure
Procedure.i TabGadgetEventData()
ProcedureReturn EventGadget()
EndProcedure
Procedure.i TabGadgetAdd(Gadget.i,Text.s,Active.i = #False,Custom.i = #Null)
Protected *tab.TAB_STRUCT
With *tab
*tab = GetGadgetData(Gadget)
If AddElement(\item())
If Len(Text) > 14
Text = Left(Text,12) + ".."
EndIf
\item()\text = Text
\item()\custom = Custom
If Active
\active = @\item()
EndIf
\min = - \stop
\max = \rect\w - 128
\stop + 128
TabGadgetUpdate(Gadget)
ProcedureReturn @\item()
EndIf
ProcedureReturn #Null
EndWith
EndProcedure
Procedure.i TabGadgetResize(Gadget.i,Width.i)
Protected *tab.TAB_STRUCT
With *tab
*tab = GetGadgetData(Gadget)
ResizeGadget(\canvas,#PB_Ignore,#PB_Ignore,Width,#PB_Ignore)
\rect\w = Width
\max = \rect\w - 128
TabGadgetUpdate(Gadget)
ProcedureReturn #Null
EndWith
EndProcedure
DataSection
tab_img_0:
IncludeBinary "bt0.png"
tab_img_1:
IncludeBinary "bt1.png"
tab_img_2:
IncludeBinary "bt2.png"
tab_img_3:
IncludeBinary "bl0.png"
tab_img_4:
IncludeBinary "bl1.png"
tab_img_5:
IncludeBinary "br0.png"
tab_img_6:
IncludeBinary "br1.png"
EndDataSection
https://www.dropbox.com/s/jt7gtkyoo057e ... b.zip?dl=0