lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]




Jérôme Vuarand wrote:
Can you send the prototype of the C function you are calling to
register a callback, along with the prototype of that callback ?
here's one of the examples that comes with the lib, it's just a basic example that pops up a message box with a single button on it:

----

#include "jm_gui/jm_gui.bi"

Dim Shared gQuit as integer = False

Function PopUp_BoxProc (ByVal box As UI_BOX_PTR, ByVal msgid As Integer, ByVal msgdata As UI_MSG_DATA_T Ptr) As Integer
   function = 1 '' Default not processed
   Select Case msgid
   Case UI_MSG_MOUSE_CLICK
       Select Case UI_GetBoxID(box)
       Case 1 '' Caption
           UI_SetCaption box, "Hello Again!"
       Case 2 '' Close
           gQuit = True
           function = 0
       End select
   End Select
End Function

Function PopUp_Create(byval owner As UI_BOX_PTR) As UI_BOX_PTR
   Dim As UI_BOX_PTR frm, btn, cap
frm = UI_CreateFrame(owner, 0, 640 \ 2 - 228 \ 2, 50, 228, 48, @PopUp_BoxProc, UI_BOX_FLAG_NORMAL or UI_BOX_FLAG_DRAG) cap = UI_CreateCaption(frm, 1, 228 \ 2 - 64 \ 2, 4, 64, 16, @PopUp_BoxProc, UI_BOX_FLAG_NORMAL, "Hello World!") btn = UI_CreateButton(frm, 2, 228 \ 2 - 64 \ 2, 24, 64, 16, @PopUp_BoxProc, UI_BOX_FLAG_NORMAL, "Close")
   Function = frm
End Function

Dim as UI_BOX_PTR gPopup
Dim as integer    mx,my,mw,mb,ret
Dim Shared fnt As JM_FONT_PTR    = 0


ScreenRes 640,480,16,2

UI_UpdateScreenInfo(640,480,16)
UI_SetSysColor UI_SYSCLR_FRAME_FACE, RGB(63,191,127)
UI_SetSysColor UI_SYSCLR_TEXT      , RGB(0,255,255)
ScreenSet 1,0

fnt = FONT_CreateFromBmp(6,8,"../data/6x8.bmp")
FONT_Set fnt

gPopup = PopUp_Create(NULL)

While gQuit = False
   GetMouse mx,my,mw,mb

   ret = UI_UpdateMouseInfo(NULL, mx, my, mw, mb)
   cls
   ret = UI_Draw(NULL, 0)
   flip
   sleep 1
Wend

FONT_Delete fnt

----

Basically, what I want to do is wrap all the UI_ functions so that they can be used in a lua script in almost exactly the same way as in FreeBasic. This is what I've been able to come up with for the function wrappers so far. I only started programming a few months ago so there's probably a better way than what I've been able to do

(this is FreeBasic, not C, but the lua api functions are the same)

----

' lua wrappers for jm_gui

#macro UserdataStubs(HANDLE, DATATYPE)
Function To##HANDLE (Byval L As lua_State Ptr, Byval index As Integer) As DATATYPE Ptr
       var h = cptr(DATATYPE Ptr, lua_touserdata(L, index))
       If h = NULL Then luaL_typerror(L, index, #HANDLE)
       Return h
   End Function
   Function push##HANDLE (Byval L As lua_State Ptr) As DATATYPE Ptr
var newvalue = cptr(DATATYPE Ptr, lua_newuserdata(L, sizeof(DATATYPE)))
       luaL_getmetatable(L, #HANDLE)
       lua_pushvalue(L, -1)
       lua_setmetatable(L, -3)
       lua_pushstring(L, "__index")
       lua_pushstring(L, #HANDLE)
       lua_gettable(L, LUA_GLOBALSINDEX)
       lua_settable(L, -3)
       lua_pop(L, 1)
       Return newvalue
   End Function
#endmacro

#macro UserdataRegister(HANDLE, METHODS, METAMETHODS)
   Function HANDLE##_register(Byval L As lua_State Ptr) As Integer
luaL_newmetatable(L, #HANDLE) ' create New metatable For file handles
       lua_pushliteral(L, "__index")
lua_pushvalue(L, -2) ' push metatable lua_rawset(L, -3) ' metatable.__index = metatable luaL_openlib(L, 0, METAMETHODS, 0)
       luaL_openlib(L, #HANDLE, METHODS, 0)
lua_pushstring(L, #HANDLE)
       lua_gettable(L, LUA_GLOBALSINDEX)
       luaL_getmetatable(L, #HANDLE)
       lua_setmetatable(L, -2)
Return 1 End Function
#endmacro

#include once "jm_gui/jm_gui.bi"


UserdataStubs(Font, JM_FONT_PTR Ptr)

function load_jm_font Cdecl(Byval L As lua_State Ptr) As Integer
if (lua_gettop(L) < 1) or (lua_gettop(L) > 3) then return Lual_error(L, "[UI.FontLoad] takes 3 arguments")
   dim as string error_msg ', f_file
   'dim as integer f_w, f_h
   dim new_font as JM_FONT_PTR

   dim as string f_file = *lual_checkstring(L,1)
   dim as integer _
       f_w = lual_checkinteger(L,2),_
       f_h = lual_checkinteger(L,3)
if not FileExists(f_file) then return luaL_argerror(L, 3, "[UI.FontLoad] cannot find "&f_file) select case ucase(right(f_file, 3))
   case "BMP"
       new_font = FONT_CreateFromBMP(f_w, f_h, f_file)
   case else
       luaL_argerror(L, 1, "[UI.FontLoad] font format not recognized")
   end select
dim as JM_FONT_PTR ptr fnt = PushFont(L)
   *fnt = new_font
return 1
end function

function fnt_del Cdecl(Byval L As lua_State Ptr) As Integer
   var fnt = toFont(L,1)
   if *fnt then
       FONT_Delete *fnt
       *fnt = 0
       return 1
   else
if *fnt = 0 then return luaL_error(L, "[font.delete] font expected got nil")
   endif
end function

Static Shared As luaL_reg font_methods(2) = {_
   (@"load",    @load_jm_font),_
   (0,0)}

Static Shared As luaL_reg font_meta(1) = {(@"__gc", @fnt_del), (0,0)}

UserdataRegister(Font, @font_methods(0), @font_meta(0))
Font_Register(L)

function ui_scd Cdecl(Byval L As lua_State Ptr) As Integer
if lua_gettop(L) <> 1 then return lual_error(L, "[UI.SetColorDepth] takes one argument")
   dim as integer col_d = lual_checknumber(L,1)
   UI_SetColorDepth(col_d)
end function

function setfnt Cdecl(Byval L As lua_State Ptr) As Integer
if lua_gettop(L) <> 1 then return lual_error(L, "[UI.FontSet] takes one argument")
    if lua_type(L, 1) <> LUA_TUSERDATA then
var error_msg = "[UI.SetFont] font expected, got "& *lua_typename(L, lua_type(L,3))
       return lual_argerror(L, 1, error_msg)
    endif
Dim As JM_FONT_PTR Ptr pfnt = toFont(L,1)
   dim As JM_FONT_PTR fnt = *pfnt
   var a = FONT_set(fnt)
end function

function fnt_print Cdecl(Byval L As lua_State Ptr) As Integer
if (lua_gettop(L) < 5) or (lua_gettop(L) > 6) then return luaL_error(L, "[UI.FontPrint] takes either 5 or 6 args")
   dim as integer x = lual_checknumber(L,1), y = lual_checknumber(L,2)
dim as uinteger fc = lual_checknumber(L,4), bc = lual_checknumber(L,5), trans = 1
   dim as string text = *lual_checkstring(L,3)
   if lua_gettop(L) = 6 then trans = lual_checknumber(L,6)

   FONT_DrawString x, y, text, fc, bc, trans
end function

function f_GTW Cdecl(Byval L As lua_State Ptr) As Integer
   var text = *lual_checkstring(L,1)
   lua_pushnumber(L, FONT_GetTextWidth(text))
   return 1
end function

function f_GTH Cdecl(Byval L As lua_State Ptr) As Integer
   var text = *lual_checkstring(L,1)
   lua_pushnumber(L, FONT_GetTextHeight(text))
   return 1
end function

function ui_ssc Cdecl(Byval L As lua_State Ptr) As Integer
if lua_gettop(L) <> 2 then return luaL_error(L, "[UI.SetSysColor] takes 2 args")
   dim as uinteger _
       flag = lual_checknumber(L,1),_
       col = lual_checknumber(L,2)
   UI_SetSysColor flag, col
end function

function ui_cf Cdecl(Byval L As lua_State Ptr) As Integer
if lua_gettop(L) <> 8 then return lual_error(L, "[UI.CreateFrame] takes 8 args")
   dim ownerbox  as UI_BOX_PTR = 0
   dim As integer _
       id     = lual_checknumber(L, 2),  _
         x         = lual_checknumber(L, 3),  _
         y        = lual_checknumber(L, 4),  _
         w         = lual_checknumber(L, 5),  _
         h       = lual_checknumber(L, 6),  _
         flags = lual_checknumber(L, 8)
    dim as string func_string = *lual_checkstring(L,7)
var ret = UI_CreateFrame(ownerbox, id, x, y, w, h, @callback_function , flags) end function

Static Shared As luaL_reg ui_functions(7) = {_
   (@"SetColorDepth",        @ui_scd),_
   (@"FontSet",                @setfnt),_
   (@"FontPrint",                @fnt_print),_
   (@"FontGetTextWidth",    @f_GTW),_
   (@"FontGetTextHeight",    @f_GTH),_
   (@"SetSysColor",            @ui_ssc),_
   (@"CreateFrame",            @ui_cf),_
   (0,0)}

luaL_openlib(L, @"UI", @ui_functions(0),0)

----
In this line of the ui_cf function: var ret = UI_CreateFrame(ownerbox, id, x, y, w, h, @callback_function , flags) the argument '@callback_function' is the function that I need to create that will link to a lua function, but I don't really know where to begin

You can get the library I'm using from this page:

http://www.execulink.com/~coder/freebasic/jmgui.html