Download this source

I take no responsibility for this source. It's freeware, do whatever you like with it.

This source shows how to open a screen with the same properties as Workbench, make it public and not close it until it's safe.


#include <exec/types.h>
#include <exec/memory.h>
#include <intuition/intuition.h>
#include <intuition/screens.h>

#include <clib/exec_protos.h>
#include <clib/intuition_protos.h>
#include <clib/graphics_protos.h>

#include <stdlib.h>
#include <stdio.h>

#define OK 0
#define ERROR 10
#define FAILURE 20

struct Screen *myscreen;
struct Window *mywindow;
BYTE mysignal=-1;

void cleanup(int code, char *string)
{
 if(mywindow)
  CloseWindow(mywindow);

 if(myscreen)
  while(!CloseScreen(myscreen))     /* try to close the screen  */
   Wait(1L << mysignal);      /* Every time a window is closed I get a signal */

 if(mysignal != -1)
  FreeSignal(mysignal);

 if(string)
  printf("%s\n",string);

 exit(code);
}

struct Screen *clonescreen(void)
{
 struct Screen *pubscreen, *newscreen;
 struct DrawInfo *drawinfo;
 ULONG modeid;

 if(pubscreen = LockPubScreen(NULL))
 {
  if(drawinfo = GetScreenDrawInfo(pubscreen))
  {
   modeid = GetVPModeID(&(pubscreen->ViewPort));  /* Get the modeid from Workbench screen */
   if(modeid != INVALID_ID)
   {
    newscreen = OpenScreenTags(NULL,
     SA_Width, pubscreen->Width,
     SA_Height, pubscreen->Height,
     SA_Depth, drawinfo->dri_Depth,
     SA_AutoScroll, TRUE,
     SA_Pens, (ULONG) (drawinfo->dri_Pens),
     SA_DisplayID, modeid,
     SA_Title, "Testprogram",
     SA_PubName, "Testscreen",  /* Note that this must be before SA_Pubtask */
     SA_PubTask, FindTask(NULL),   /* So we can close the screen safer */
     SA_PubSig, mysignal,
     TAG_DONE);
   }
   else
    printf("Wrong ModeId\n");
   FreeScreenDrawInfo(pubscreen, drawinfo);
  }
  else
   printf("Couldn't get drawinfo.\n");
  UnlockPubScreen(NULL,pubscreen);
 }
 else
  printf("Couldn't lock the screen\n");
 return(newscreen);
}

int main(void)
{
  BOOL keepgoing=TRUE;

 struct IntuiMessage *intmess;

 if((mysignal = AllocSignal(-1)) == -1)
  cleanup(ERROR,"Couldn't allocate the signal for the screen\n");

 if(!(myscreen = clonescreen()))
  cleanup(ERROR,"Couldn't open the screen\n");

 if(!(mywindow = OpenWindowTags(NULL,
  WA_Top, 10,
  WA_Height, 100,
  WA_Width, 200,
  WA_IDCMP, IDCMP_CLOSEWINDOW,
  WA_Flags, WFLG_CLOSEGADGET,
  WA_ScreenTitle, "Window on the screen",
  WA_PubScreen, myscreen,
  TAG_DONE)))
 cleanup(ERROR,"Couldn't open the window\n");

   /* According the autodoc it's enough to open the screen with SA_PubName
      to make a screen public. Unfortunately it doesn't work here... */
 PubScreenStatus(myscreen,0);

  while(keepgoing)
  {
   WaitPort(mywindow->UserPort);
   while(intmess = GT_GetIMsg(mywindow->UserPort))
   {
     if(intmess->Class == IDCMP_CLOSEWINDOW)
       keepgoing = FALSE;
     GT_ReplyIMsg(intmess);
    }
  }
 cleanup(OK,NULL);
}