iup_array_opt.bi, iup_assert_opt.bi, iup_attrib_opt.bi, iup_childtree_opt.bi, iup_classbase_opt.bi, iup_class_opt.bi,
iup_dlglist_opt.bi, iup_draw_opt.bi, iup_drvfont_opt.bi, iup_drvinfo_opt.bi, iup_drv_opt.bi, iup_extra_opt.bi,
iup_focus_opt.bi, iup_image_opt.bi, iup_key_opt.bi, iup_mask_opt.bi, iup_object_opt.bi, iup_register_opt.bi, iup_table_opt.bi
We can create our own IUP classes now.
Joshy
my first try (IupUserClass.bas and iupuserclass.bi in the download)
Code: Select all
#ifndef __IUPOVAL_BI__
#define __IUPOVAL_BI__
' simple example of a user defined IUP class
#include once "inc/iup_object_opt.bi"
#include once "inc/iup_register_opt.bi"
#include once "inc/iup_draw_opt.bi"
declare function OvalNewClass cdecl () as Iclass ptr
' register the NewClass function
sub IupOvalOpen cdecl()
if (IupGetGlobal("_IUP_OVAL_OPEN")=0) then
iupRegisterClass(OvalNewClass())
IupSetGlobal("_IUP_OVAL_OPEN", "1")
end if
end sub
'
function IupOval cdecl ()as Ihandle ptr
return IupCreatev("oval",NULL)
end function
' our private oval data
type OVAL_DATA
as IDrawCanvas ptr dc
as integer nPoints
as integer ptr pPoints
end type
#define GETOVALDATA(v) dim as OVAL_DATA ptr v = cast(OVAL_DATA ptr,IupGetAttribute(ih, "_IUP_OVALDATA"))
function OvalResize_CB cdecl ( ih as Ihandle ptr, w as integer, h as integer) as integer
'? "OvalDefaultResize_CB " w & " x " & h
GETOVALDATA(pData)
if pData then
if pData->dc then
' a bug in IUP 3.5 need this
pData->dc->w=w+1
pData->dc->h=h+1
iupDrawUpdateSize(pData->dc)
end if
end if
return IUP_DEFAULT
end function
function OvalRedraw_CB cdecl (ih as Ihandle ptr, x as single, y as single) as integer
' ? "OvalRedraw_CB"
GETOVALDATA(pData)
if pData then
if pData->dc then
iupDrawParentBackground(pData->dc)
' get the oval attribute number of points
dim as integer nPoints=IupGetInt(ih, "NUMPOINTS")
if nPoints<3 then nPoints=3
' the value changes since creation ?
if (nPoints<>pData->nPoints) then
' reallocate the x,y points of the oval
pData->pPoints=reallocate(pData->pPoints,nPoints*2 * sizeof(integer))
pData->nPoints=nPoints
end if
' the size are changed so we recalculate the x,y coods
dim as single wstep = 6.14/pData->nPoints
dim as single rad
dim as single wr=(pData->dc->w-1)*0.5
dim as single hr=(pData->dc->h-1)*0.5
for i as integer = 0 to pData->nPoints-1
pData->pPoints[i*2+0]=cos(rad)*wr+wr
pData->pPoints[i*2+1]=sin(rad)*hr+hr
rad+=wstep
next
iupDrawPolygon(pData->dc, pData->pPoints,pData->nPoints, 255,0,0, IUP_DRAW_FILL)
iupDrawText(pData->dc,"OVAL",4, wr,hr, 0,0,255,"TIMES_BOLD_12")
' show the offscreen image
iupDrawFlush(pData->dc)
end if
end if
return IUP_DEFAULT
end function
function OvalCreateMethod cdecl(ih as Ihandle ptr, params as any ptr ptr) as integer
'? "OvalCreateMethod"
dim as OVAL_DATA ptr pData = callocate(sizeof(OVAL_DATA))
IupSetAttribute(ih, "_IUP_OVALDATA", cptr(zstring ptr,pData))
IupSetAttribute(ih, "NUMPOINTS", "32")
IupSetCallback(ih, "RESIZE_CB", cast(Icallback,@OvalResize_CB) )
IupSetCallback(ih, "ACTION" , cast(Icallback,@OvalRedraw_CB))
return IUP_NOERROR
end function
function OvalMapMethod cdecl (ih as Ihandle ptr) as integer
'? "OvalMapMethod"
GETOVALDATA(pData)
if pData then
if (pData->dc=NULL) then
' create offscreen image
pData->dc = iupDrawCreateCanvas(ih)
pData->nPoints = IupGetInt(ih, "NUMPOINTS")
if pData->nPoints<3 then
pData->nPoints=3
IupStoreAttribute(ih, "NUMPOINTS",str(pData->nPoints))
end if
pData->pPoints = allocate(pData->nPoints*2*sizeof(integer))
end if
end if
return IUP_NOERROR
end function
sub OvalUnMapMethod cdecl (ih as Ihandle ptr)
'? "OvalUnMapMethod"
end sub
sub OvalDestroyMethod cdecl(ih as Ihandle ptr)
'? "OvalUnMapMethod"
GETOVALDATA(pData)
if pData then
if (pData->dc) then
iupDrawKillCanvas(pData->dc)
pData->dc = NULL
end if
if (pData->pPoints) then
deallocate pData->pPoints: pData->pPoints=NULL
end if
deallocate pData
IupSetAttribute(ih, "_IUP_OVALDATA",NULL)
end if
end sub
' here we create a new IUP class
function OvalNewClass cdecl () as Iclass ptr
dim as Iclass ptr ic = iupClassNew(iupRegisterFindClass("canvas"))
ic->name = @"oval"
ic->format = NULL
ic->nativetype = IUP_TYPECANVAS
ic->childtype = IUP_CHILDNONE
ic->is_interactive = 1
ic->New_ = @OvalNewClass
ic->Create = @OvalCreateMethod
ic->Destroy = @OvalDestroyMethod
ic->Map = @OvalMapMethod
ic->UnMap = @OvalUnMapMethod
return ic
end function
#endif ' __IUPOVAL_BI__