#! /usr/dt/bin/dtksh
#  $XConsortium: DtWsTest1.src /main/3 1996/04/23 20:18:06 drk $

# #########################################################################
#   (c) Copyright 1993, 1994 Hewlett-Packard Company	
#   (c) Copyright 1993, 1994 International Business Machines Corp.
#   (c) Copyright 1993, 1994 Sun Microsystems, Inc.
#   (c) Copyright 1993, 1994 Unix System Labs, Inc., a subsidiary of
#       Novell, Inc.
# #########################################################################


# 
#  This sample shell script demonstrates how to interact with the workspace
#  manager.  It demonstrates the following capabilities:
# 
# 	1) How to query which workspaces the widgets currently reside it.
# 	2) How to set the current workspace.
# 	3) How to be notified when the current workspace changes.
# 


integer wsCount

#  Pushbutton Callback: This function asks the workspace manager to change
#                       to the workspace indicated by $1; $1 is an X atom
#                       which identifies the new workspace.  At some point
#                       after our request to the workspace manager, the
#                       workspace manager will activate our WsCB function,
#                       letting us know that the change has actually taken
#                       place.
SetWorkspace()
{
   echo
   if DtWsmSetCurrentWorkspace $TOPLEVEL $1; then
      echo "Changing to new workspace"
   else
      XmGetAtomName NAME $(XtDisplay "-" $TOPLEVEL) $1
      echo "Unable to Change to workspace " $NAME
   fi
}

#  Workspace Changed Callback: This function is invoked whenever the workspace
#                              manager changes workspaces.  It will simply
#                              query the 'name' of the new workspace, and
#                              echo it outl
WsCB()
{
   DtWsmGetCurrentWorkspace $(XtDisplay "-" $TOPLEVEL)                        $(XRootWindowOfScreen "-" $(XtScreen "-" $TOPLEVEL))                        NEW_ATOM


   XmGetAtomName NAME $(XtDisplay "-" $TOPLEVEL) $NEW_ATOM
   echo "Change to workspace complete " $NAME  "("$NEW_ATOM")"
}

# ###################### Create the Main UI ###############################

XtInitialize TOPLEVEL dtWsTest DtWsTest "$0" "$@"
XtSetValues $TOPLEVEL allowShellResize:True

XtCreateManagedWidget DA da XmDrawingArea $TOPLEVEL
XtSetValues $DA height:200 width:200

XtRealizeWidget $TOPLEVEL
XSync $(XtDisplay "-" $TOPLEVEL) False

XtCreateApplicationShell TOPLEVEL2 DtWsTesta TopLevelShell

XtCreateManagedWidget RC rc XmRowColumn $TOPLEVEL2          orientation:HORIZONTAL          packing:PACK_COLUMN 



#  Get a list of all of the workspaces, and create a pushbutton for each one.
#  When a pushbutton is activated, it will ask the workspace manager to
#  change to the indicated workspace.
oldIF=$IFS
if DtWsmGetWorkspaceList $(XtDisplay "-" $TOPLEVEL)                           $(XRootWindowOfScreen "-" $(XtScreen "-" $TOPLEVEL))                           WS_LIST; 


then
   IFS=,
   wsCount=0
   for item in $WS_LIST; 
   do
      XmGetAtomName NAME $(XtDisplay "-" $TOPLEVEL) $item
      label="Set Current Workspace to "$NAME
      XtCreateManagedWidget ITEM $item XmPushButton $RC 	     labelString:$label

      XtAddCallback $ITEM activateCallback "SetWorkspace $item"
      wsCount=$wsCount+1
   done
   IFS=$oldIFS
else
   echo "Unable to get workspace list"
   exit -1
fi

XtSetValues $RC numColumns:$wsCount
XtRealizeWidget $TOPLEVEL2
XSync $(XtDisplay "-" $TOPLEVEL) False

#  The following block queries the initial set of workspaces occupied by
#  this shell script; this list is printed out.  Next, it will ask the
#  workspace manager to move the shell script windows into all workspaces.
#  Lastly, it will again ask the workspace manager for the list of
#  workspaces occupied by the shell script windows, and will again print
#  out the list.
if DtWsmGetWorkspacesOccupied $(XtDisplay "-" $TOPLEVEL)                                $(XtWindow "-" $TOPLEVEL)                                CURRENT_WS_LIST ;


then
   echo "Initial workspaces occupied:"
   for item in $CURRENT_WS_LIST; 
   do
      XmGetAtomName NAME $(XtDisplay "-" $TOPLEVEL) $item
      echo "   "$NAME
   done

   DtWsmGetWorkspaceList $(XtDisplay "-" $TOPLEVEL)                           $(XRootWindowOfScreen "-" $(XtScreen "-" $TOPLEVEL))                           WS_LIST



   DtWsmSetWorkspacesOccupied $(XtDisplay "-" $TOPLEVEL)                                $(XtWindow "-" $TOPLEVEL)                                $WS_LIST



   DtWsmSetWorkspacesOccupied $(XtDisplay "-" $TOPLEVEL2)                                $(XtWindow "-" $TOPLEVEL2)                                $WS_LIST


else
   echo "Unable to get current list of occupied workspaces"
   echo -2
fi

XSync $(XtDisplay "-" $TOPLEVEL) False

#  Print the new list of workspaces occupied
DtWsmGetWorkspacesOccupied $(XtDisplay "-" $TOPLEVEL)                             $(XtWindow "-" $TOPLEVEL)                             CURRENT_WS_LIST



echo "After modification, workspaces occupied:"
IFS=,
for item in $CURRENT_WS_LIST; 
do
   XmGetAtomName NAME $(XtDisplay "-" $TOPLEVEL) $item
   echo "   "$NAME
done
IFS=$oldIFS
echo ""

#  Add a callback to be notified whenever the workspace changes.
DtWsmAddCurrentWorkspaceCallback HANDLE1 $TOPLEVEL WsCB

XtMainLoop
