Permalink
Browse files

remove the Callback hack, closures can do this better.

  • Loading branch information...
1 parent b747b72 commit 58a00b0bcb9454d5655ee0c7f447a44d6c787d5c @mbalmer mbalmer committed Apr 7, 2012
Showing with 11 additions and 57 deletions.
  1. +11 −46 luamotif.c
  2. +0 −1 luamotif.h
  3. +0 −10 widgets.c
View
@@ -208,6 +208,15 @@ lm_Insert(lua_State *L)
return 0;
}
+lm_GetInsertionPosition(lua_State *L)
+{
+ Widget widget;
+
+ widget = lm_GetWidget(L, 1);
+ lua_pushinteger(L, XmTextGetInsertionPosition(widget));
+ return 1;
+}
+
lm_GetLastPosition(lua_State *L)
{
Widget widget;
@@ -272,15 +281,10 @@ static void
lm_Callback(Widget widget, XtPointer client_data, XtPointer call_data)
{
struct cb_data *cbd = (struct cb_data *)client_data;
- int nargs = 1;
lua_rawgeti(cbd->L, LUA_REGISTRYINDEX, cbd->ref);
lua_rawgeti(cbd->L, LUA_REGISTRYINDEX, cbd->obj);
- if (cbd->data != -1) {
- lua_rawgeti(cbd->L, LUA_REGISTRYINDEX, cbd->data);
- nargs++;
- }
- lua_pcall(cbd->L, nargs, 0, 0);
+ lua_pcall(cbd->L, 1, 0, 0);
}
static void
@@ -290,8 +294,6 @@ lm_DestroyCallback(Widget widget, XtPointer client_data, XtPointer call_data)
luaL_unref(cbd->L, LUA_REGISTRYINDEX, cbd->ref);
luaL_unref(cbd->L, LUA_REGISTRYINDEX, cbd->obj);
- if (cbd->data != -1)
- luaL_unref(cbd->L, LUA_REGISTRYINDEX, cbd->data);
free(cbd);
}
@@ -316,11 +318,6 @@ lm_AddCallback(lua_State *L)
cbd->L = L;
- /* reference any data, if present */
- if (lua_gettop(L) == 4)
- cbd->data = luaL_ref(L, LUA_REGISTRYINDEX);
- else
- cbd->data = -1;
/* reference the function */
cbd->ref = luaL_ref(L, LUA_REGISTRYINDEX);
cb = lua_tostring(L, -1);
@@ -810,29 +807,6 @@ lm_newindex(lua_State *L)
#define MAXARGS 64
-static int
-lm_SetupCallback(lua_State *L, Widget w, const char *name)
-{
- struct cb_data *cbd;
-
- cbd = malloc(sizeof(struct cb_data));
- if (cbd == NULL)
- luaL_error(L, "memory error");
- cbd->L = L;
-
- lua_rawgeti(L, -1, 1);
- cbd->ref = luaL_ref(L, LUA_REGISTRYINDEX);
- lua_pushvalue(L, lua_gettop(L));
- cbd->obj = luaL_ref(L, LUA_REGISTRYINDEX);
- lua_rawgeti(L, -1, 2);
- cbd->data = luaL_ref(L, LUA_REGISTRYINDEX);
-
- XtAddCallback(w, name, lm_Callback, cbd);
- XtAddCallback(w, XmNdestroyCallback, lm_DestroyCallback, cbd);
-
- return 0;
-}
-
static Widget
lm_CreateWidgetHierarchy(lua_State *L, int parentObj, Widget parent,
const char *name)
@@ -861,15 +835,6 @@ lm_CreateWidgetHierarchy(lua_State *L, int parentObj, Widget parent,
lua_pushstring(L, "__widgetClass");
lua_rawget(L, -2);
- if (lua_isstring(L, -1)) {
- if (!strcmp("lmCallback", lua_tostring(L, -1))) {
- lua_pop(L, 1);
- lm_SetupCallback(L, parent, name);
- } else
- lua_pop(L, 1);
- return parent;
- }
-
class = (WidgetClass)lua_topointer(L, -1);
lua_pop(L, 1);
@@ -973,7 +938,6 @@ lm_CreateWidgetHierarchy(lua_State *L, int parentObj, Widget parent,
if (cbd == NULL)
luaL_error(L, "memory error");
cbd->L = L;
- cbd->data = -1;
lua_pushvalue(L, -1);
cbd->ref = luaL_ref(L, LUA_REGISTRYINDEX);
lua_pushvalue(L, t);
@@ -1120,6 +1084,7 @@ luaopen_motif(lua_State *L)
{ "SetString", lm_SetString },
{ "Remove", lm_Remove },
{ "Insert", lm_Insert },
+ { "GetInsertionPosition", lm_GetInsertionPosition },
{ "GetLastPosition", lm_GetLastPosition },
{ "SetInsertionPosition", lm_SetInsertionPosition },
{ "SetMaxLenght", lm_SetMaxLength },
View
@@ -36,7 +36,6 @@ typedef struct cb_data {
lua_State *L;
int ref; /* The function to be called */
int obj; /* The Lua object to pass, usually the widget */
- int data; /* Arbitrary data to pass to the callback */
} lm_callbackdata;
struct str_constant {
View
@@ -60,15 +60,6 @@ lm_##W(lua_State *L) \
return 1; \
}
-/* Pseudo widget class to setupa a callback with parameters */
-static int
-lm_Callback(lua_State *L)
-{
- lua_pushstring(L, "lmCallback");
- lua_setfield(L, -2, "__widgetClass");
- return 1;
-}
-
GadgetConstructor(ArrowButton)
GadgetConstructor(Label)
GadgetConstructor(PushButton)
@@ -128,7 +119,6 @@ struct luaL_reg lm_widgetConstructors[] = {
{ "ArrowButton", lm_ArrowButton },
{ "BulletinBoard", lm_BulletinBoard },
{ "ButtonBox", lm_ButtonBox },
- { "Callback", lm_Callback }, /* pseudo widget */
{ "CascadeButton", lm_CascadeButton },
{ "Column", lm_Column },
{ "ComboBox", lm_ComboBox },

0 comments on commit 58a00b0

Please sign in to comment.