#include "DropObjClass.h"
      
ULONG DropObj_DragQuery(struct IClass *cl,Object *obj,struct MUIP_DragQuery *msg)
{
  return(MUIV_DragQuery_Accept);
}

ULONG DropObj_DragDrop(struct IClass *cl,Object *obj,struct MUIP_DragDrop *msg)
{ Object *group, *new, *win, *app;
  int type, foo;
        
  get(msg->obj,MUIA_UserData,&type);
  get(obj,MUIA_WindowObject,&win);
    
  /* We are dropping an object from the toolbox, add it */
  if(type<MUIGroup && type != NULL)
  
  { new=CreateDropObj(type);

    if(new)
    { get(obj,MUIA_Parent,&group);

      if(group!=NULL)
      { get(obj,MUIA_UserData,&type);

        if(type == MUIGroup)
        /* Insert before or add to this group */
        { foo = MUI_Request(a->App,win,0,"VB request","*_Before|_In",
                "Insert the new object before\nor in this group?");

          if(foo==0)
            Group_Append(new,obj);
          else
            Group_Insert(new,obj,group);
        }
        else
          Group_Insert(new,obj,group);
      }
    }
  }
  else
  /* We dropped an existing object, move it */
  { 
    get(obj,MUIA_Parent,&group);

    /* Don't drop on ourselves or on a child*/
    if(obj!=msg->obj && group!=msg->obj)
    { if(group!=NULL)
      { get(obj,MUIA_UserData,&type);
        if(type == MUIGroup)
        { foo = MUI_Request(a->App,win,0,"VB request","*_Before|_In",
                "Move this object before\nor in this group?");
          Group_Remove(msg->obj);
          if(foo==0)
            Group_Append(msg->obj,obj);
          else
            Group_Insert(msg->obj,obj,group);
        }
        else
          Group_Move(msg->obj,obj);
      }
    }
    else
    { /* We've been dropped on ourselves, update the objects window */ 
      if(obj==msg->obj)
      { 
        get(msg->obj,MUIA_WindowObject,&win);
        get(win,MUIA_ApplicationObject,&app);
       
        set(win,MUIA_Window_ActiveObject,msg->obj);
        set(app,MUIA_UserData,msg->obj);

        UpdateObjectsWindow(obj);
      }
    }
  }
  
  return(0);
}

SAVEDS ASM ULONG DropObj_Dispatcher(REG(a0) struct IClass *cl,REG(a2) Object *obj,REG(a1) Msg msg)
{
  switch (msg->MethodID)
  {
    case MUIM_DragQuery: return(DropObj_DragQuery(cl,obj,(APTR)msg));
    case MUIM_DragDrop : return(DropObj_DragDrop (cl,obj,(APTR)msg));
  }
  return(DoSuperMethodA(cl,obj,msg));
}

