lua-users home
lua-l archive

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


Hi,

This problem has kept me awake for the whole weekend. I'm writing a tree
control that should display a lua table and tables within tables etc..
That works perfectly. Then I need to be able to manipulate the members
in the table/tree, and this is where its getting fishy.

What information do I need to save for each member to be able to
retrieve and modify the object later. My first try (And basicly all my
tries in different shapes) is releated to save the lua_Object for the
table and the index .. But this seems to go wrong.. Is the lua_Object a
safe value to save outside blocks ? Is there an easier way

If any of you helpfull people has any ideas on how to do this I would
appriciate it very much!

/Erik

p.s. This is the source code, it using the multistate version - therefor
the extra passed lua_state on all calls

// Function to insert a member in the Windows Tree Control
HTREEITEM AddTreeLine(HWND htree, HTREEITEM parent, char *Text,unsigned
ParentObj,unsigned long Object)
{ 
  TV_INSERTSTRUCT  str;
  TreeMember      *tm;

  str.hParent = parent;
  str.item.mask = TVIF_TEXT | TVIF_PARAM;
  str.hInsertAfter = TVI_LAST;
  str.item.pszText = Text;
  tm = malloc(sizeof(TreeMember));
  tm->Object = Object;
  tm->Parent = ParentObj;
  str.item.lParam = (unsigned long)tm;
  return TreeView_InsertItem(htree,&str);
} 

// Function to build the tree control - recursive
void TreeAddTable(lua_State *lua_state,ControlList *CL,TreeList
*Tree,lua_Object Table,HTREEITEM Parent)
{
  lua_Object LO;
  lua_Object LO2;
  TObject   *O;
  TObject   *O2;
  char       Text[512];
  HTREEITEM  NewParent;

  lua_beginblock(lua_state);
  
  lua_pushobject(lua_state,Table);
  lua_pushnil(lua_state);
  
  lua_call(lua_state,"next");
  LO = lua_getresult(lua_state,1);
  LO2 = lua_getresult(lua_state,2);
  while (!lua_isnil(lua_state,LO))
  {
    O = luaA_Address(lua_state,LO);
    O2 = luaA_Address(lua_state,LO2);
    if (ttype(O2) == LUA_T_ARRAY)
    {
      int d;
      strcpy(Text,lua_getstring(lua_state,LO));
      for (d = 0; d < strlen(Text); d++)
        if (Text[d] < 32)
          Text[d] = 32;
      lua_pushobject(lua_state,Table);    // Ref just for testing - will
mess up the garbage collection!
      lua_ref(lua_state,1);
      lua_pushobject(lua_state,LO);    
      lua_ref(lua_state,1);
      NewParent = AddTreeLine(CL->ControlHandle,Parent,Text,Table,LO);
      TreeAddTable(lua_state,CL,Tree,LO2,NewParent);
    }
    else
    {
      int d;
      strcpy(Text,lua_getstring(lua_state,LO));
      strcat(Text," : ");
      strcat(Text,lua_getstring(lua_state,LO2));
      for (d = 0; d < strlen(Text); d++)
        if (Text[d] < 32)
          Text[d] = 32;
      lua_pushobject(lua_state,Table);     // Ref just for testing -
will mess up the garbage collection!
      lua_ref(lua_state,1);
      lua_pushobject(lua_state,LO);    
      lua_ref(lua_state,1);
      NewParent = AddTreeLine(CL->ControlHandle,Parent,Text,Table,LO);
    }
    lua_pushobject(lua_state,Table);
    lua_pushobject(lua_state,LO);
    lua_call(lua_state,"next");
    LO = lua_getresult(lua_state,1);
    LO2 = lua_getresult(lua_state,2);
  } 
  lua_endblock(lua_state);
}


// This is the function that gets called when the user selects a member
in the tree.
void TreeControlHandling(WindowList *WL,ControlList *CL,LPARAM lParam)
{
  NM_TREEVIEW    *tv = (NM_TREEVIEW*)lParam;
  TreeList       *TC = (TreeList*)CL->ControlStruct;
  TreeMember     *TM;
  lua_Object      Value;
  
  switch (tv->hdr.code)
  {
    case TVN_SELCHANGED:
      TM = (TreeMember*)tv->itemNew.lParam;
      lua_pushobject(WL->lua_state,TM->Parent);
      lua_pushobject(WL->lua_state,TM->Object);
      Value = lua_gettable(WL->lua_state);
      printf("Data %s\n",lua_getstring(WL->lua_state,Value));
      break;
  }
}