שתשת  =  =  =  =  =  =  =  =  =  =;
; This example has really nothing to do with GadUtil.library. It just
; demonstrates a bug in GadTools.library v37 (that also appears when
; using GadUtil on a v37 system).
;
; GadTools autodocs says that the LISTVIEW_KIND gadget will _never_
; be higher than the specified value in gng_Height.
;
; If you create a STRING_KIND gadget to show the selected item in a
; ListView gadget, the height of the listview and the string gadget
; together becomes too high. If the built-in TEXT_KIND gadget is used
; to show the selected item, the height is ok.
;
; This bug is fixed on version 39 of GadTools.
;
; Another "bug" in GadTools is that a bevelbox gets "recessed" if the
; tag GTBB_Recessed, FALSE is used. According to the autodocs, it should
; be that way, but I think it's wrong. GadUtil makes a way around this
; "bug".
;

	incdir	'include:'
	include 'libraries/gadtools.i'
	include	'LVOs.i'

LVWIDTH:	equ	598	; ListView and string gadget must be exactly
STRWIDTH:	equ	598	;  the same width for GadTools
LVHEIGHT:	equ	76	; Height of LV and the button to compare with
SHOWSTR:	equ	1	; Flag for STRING_KIND showselected gadget
WHEIGHT:	equ	LVHEIGHT+120

MYGAD_VIEW:	equ	5
MYGAD_CYCLE:	equ	3

MYIDCMP:	equ	LISTVIEWIDCMP!CYCLEIDCMP!MXIDCMP!BUTTONIDCMP

Main:
	move.l	_AbsExecBase.w,a6
	lea.l	LibPtrs(pc),a2
	move.w	(a2)+,d2
.OpenLib:
	move.l	(a2),a1
	moveq.l	#37,d0
	CallLib	OpenLibrary
	move.l	d0,(a2)+
	dbeq	d2,.OpenLib
	beq.s	.CloseLibs	

	bsr.w	GadToolsWindow		; Start the program

.CloseLibs:
	move.l	_AbsExecBase.w,a6	; Close dos library
	lea.l	LibPtrs(pc),a2
	move.w	(a2)+,d2
.CloseLib:
	move.l	(a2)+,d0
	beq.s	.NotOpened
	move.l	d0,a1
	CallLib	CloseLibrary
	dbra	d2,.CloseLib
.NotOpened:
	moveq.l	#0,d0			; Returncode for DOS
	rts

;--------------------------------------------------------------------------
EmptyTag:	dc.l	TAG_DONE
Topaz80:	dc.l	topazname,$00080000

LibPtrs:
		dc.w	4-1
IntuiBase:	dc.l	IntuiName
GToolsBase:	dc.l	GToolsName
DosBase:	dc.l	DosName
UtilityBase:	dc.l	UtilityName

IntuiName:	dc.b	'intuition.library',0
GToolsName:	dc.b	'gadtools.library',0
DosName:	dc.b	'dos.library',0
UtilityName:	dc.b	'utility.library',0

topazname:	dc.b	'topaz.font',0
	even

makegadgets:
	move.w	(a3)+,d2
	subq.w	#1,d2
.MakeGadget:
	move.l	gad(pc),a0		; Previous gadget
	lea.l	ng(pc),a1		; NewGadget structure
	move.w	(a3)+,gng_LeftEdge(a1)
	move.w	(a3)+,gng_TopEdge(a1)
	move.w	(a3)+,gng_Width(a1)
	move.w	(a3)+,gng_Height(a1)
	move.w	(a3)+,gng_GadgetID(a1)
	move.l	(a3)+,d0		; Gadget type
	move.l	(a3)+,gng_GadgetText(a1)
	move.l	(a3)+,a2		; Tags
	CallLib	CreateGadgetA	
	move.l	d0,gad
	dbeq	d2,.MakeGadget
	rts

;--------------------------------------------------------------------------
	
GadToolsWindow:
	movem.l	d0-a6,-(sp)
	move.l	IntuiBase(pc),a6
	suba.l	a0,a0
	CallLib	LockPubScreen		; Lock default public screen
	move.l	d0,mysc
	beq.w	.NoScreen		; Failed to lock public screen

	move.l	d0,a0			; Screen pointer to A0
	lea.l	EmptyTag(pc),a1		; Pointer to an empty tag list
	move.l	GToolsBase(pc),a6
	CallLib	GetVisualInfoA
	move.l	d0,vi
	beq.w	.UnlockSc
	move.l	d0,beveltags+4

	lea.l	glist(pc),a0
	CallLib	CreateContext		; Create storage for context data
	move.l	d0,gad
	
	lea.l	ng(pc),a0		; Get pointer to the NewGadget struct
	move.l	#Topaz80,gng_TextAttr(a0)
	move.l	vi(pc),gng_VisualInfo(a0)

 ifne SHOWSTR
	lea.l	gadgetdatas1(pc),a3
	bsr.w	makegadgets
	beq.w	.FreeGads
	move.l	d0,LvTags+4
 endc
	
	lea.l	gadgetdatas(pc),a3
	bsr.w	makegadgets
	beq.w	.FreeGads		; Failed to create the gadgets

	suba.l	a0,a0			; No NewWindow structure
	lea.l	wintags(pc),a1		; Tag pointer
	move.l	IntuiBase(pc),a6
	CallLib	OpenWindowTagList	; Open the window
	move.l	d0,mywin
	beq.w	.FreeGads		; Window failed to open

	move.l	d0,a0			; Window pointer to A0
	suba.l	a1,a1
	move.l	GToolsBase(pc),a6
	CallLib	GT_RefreshWindow	; Refresh the gadgets

	move.l	mywin(pc),a0
	move.l	wd_RPort(a0),a0
	lea.l	beveltags(pc),a1
	moveq.l	#10,d0
	moveq.l	#100,d1
	move.w	#200,d2
	move.w	#60,d3
	CallLib	DrawBevelBoxA


	move.l	mywin(pc),a0
	bsr.w	process_window_events	
.NoFile:
	move.l	mywin(pc),a0		; Close our window
	move.l	IntuiBase(pc),a6
	CallLib	CloseWindow

.FreeGads:
	move.l	GToolsBase(pc),a6	; Deallocate mem for gadgets
	move.l	glist(pc),a0
	CallLib	FreeGadgets
.FreeVisual:
	move.l	vi(pc),a0
	CallLib	FreeVisualInfoA	
.UnlockSc:
	suba.l	a0,a0			; Unlock the default public screen
	move.l	mysc(pc),a1
	move.l	IntuiBase(pc),a6
	CallLib	UnlockPubScreen
.NoScreen:
	movem.l	(sp)+,d0-a6
	rts

;--------------------------------------------------------------------------
ng:	blk.b	30,0		; NewGadget structure for CreateGadget()
vi:	dc.l	0
gad:	dc.l	0
mywin:	dc.l	0

beveltags:	dc.l	GT_VisualInfo,	0
;		dc.l	GTBB_Recessed,	TRUE
		dc.l	TAG_DONE
wintags:
	dc.l	WA_Title,	wintitle
gl:	dc.l	WA_Gadgets,	0,		WA_AutoAdjust,	TRUE
	dc.l	WA_Width,	640,		WA_InnerHeight,	WHEIGHT
	dc.l	WA_DragBar,	TRUE,		WA_DepthGadget,	TRUE
	dc.l	WA_Activate,	TRUE,		WA_CloseGadget,	TRUE
	dc.l	WA_IDCMP,MYIDCMP!IDCMP_CLOSEWINDOW!IDCMP_REFRESHWINDOW
sc:	dc.l	WA_PubScreen,	0,		TAG_DONE	

glist:	equ	gl+4
mysc:	equ	sc+4

;----------------------------------------------------------------------------
gadgetdatas1:
	dc.w	1
	dc.w	10,	100,	STRWIDTH,	14,	-1
	dc.l	STRING_KIND,	0,	0
gadgetdatas:
	dc.w	2
	dc.w	610,	14,	20,	LVHEIGHT,	-1
	dc.l	BUTTON_KIND,	0,		0

	dc.w	10,	14,	LVWIDTH,LVHEIGHT,	MYGAD_VIEW
	dc.l	LISTVIEW_KIND,	0,		LvTags
	
LvTags:	dc.l	GTLV_ShowSelected,	NULL
SetLv:	dc.l	GTLV_Labels,	0,	TAG_DONE

wintitle:
	dc.b	"ListUtil v3.0 release 0.0000001 PrePrePrePreview",0
	even

;----------------------------------------------------------------------------
	
process_window_events:
	movem.l	d0-a6,-(sp)
	move.l	a0,-(sp)		; Save window pointer
.WaitEvent:
	move.l	(sp),a0
	move.l	wd_UserPort(a0),a0	; Window -> UserPort
	moveq.l	#1,d0
	moveq.l	#0,d1
	move.b	MP_SIGBIT(a0),d1	; -> mp_SigBit
	lsl.l	d1,d0			; Make signal mask
	move.l	_AbsExecBase,a6
	CallLib	Wait			; Wait for a message
.GetEvent:
	move.l	(sp),a0			; Get UserPort pointer
	move.l	wd_UserPort(a0),a0
	move.l	GToolsBase(pc),a6
	CallLib	GT_GetIMsg		; Remove message from UserPort
	tst.l	d0			; Check message for NULL
	beq.s	.WaitEvent		; Continue waiting if no message

	move.l	d0,a1			; IntuiMessage pointer to A1
	move.l	im_IAddress(a1),a2	; IAddress to A2
	move.w	im_Code(a1),d2		; Code to D2
	move.l	im_Class(a1),d3		; Class to D3

	CallLib	GT_ReplyIMsg		; Reply the message

	cmp.l	#IDCMP_CLOSEWINDOW,d3	; Close gadget pressed ?
	beq.b	.Exit			; Yep! Exit the event loop	

	cmp.l	#IDCMP_GADGETUP,d3	; Any gadget released ?
	bne.s	.GetEvent		; Nope! Check close gadget
	move.w	gg_GadgetID(a2),d1	; Get gadget ID
	cmp.w	#MYGAD_CYCLE,d1
	bne.s	.GetEvent

	bra.b	.GetEvent
.CheckRefr:
	cmp.l	#IDCMP_REFRESHWINDOW,d3	; Do window need to be refreshed ?
	bne.b	.GetEvent		; Nope! Check next message!
	move.l	GToolsBase(pc),a6
	move.l	(sp),a0
	CallLib	GT_BeginRefresh		; Start refreshing window
	move.l	(sp),a0
	moveq.l	#TRUE,d0
	CallLib	GT_EndRefresh		; Refreshing done
	bra.b	.GetEvent
.Exit
	move.l	(sp)+,a0
	movem.l	(sp)+,d0-a6
	rts

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