
/*******************************************************************
 $CRT 30 Jan 1997 : hb

 $AUT Holger Burkarth
 $DAT >>ClipWinReg.c<<   30 Jan 1997    13:50:00 - (C) ProDAD
*******************************************************************/

//##ex mcpp:cppc -gs -o pos:pos/Ex/ClipWinReg p:pLib/StartCode.o p:/pOS_RKRM/pLayer/ClipWinReg.c p:pLib/StdIO.o -l pOSStub -l pOS

/***********************************************************
  pOS programing example - Copyright (C) 1995-97 proDAD

  This code was written as an easy to understand example,
  how to program pOS features. It is provided 'as-is',
  without any express or implied warranty.

  Permission is hereby granted to use, copy and modify
  this source code for any purpose, without fee, subject
  to the following conditions:

    (1) This notice may not be removed or altered from any
        source distribution.

    (2) Altered source versions must be plainly marked as
        such, and must not be misrepresented as being
        the original source code.

    (3) If only executable code is distributed, then the
        accompanying documentation have to state that
        "this software is based in part on examples of
        the pOS developer packet".

    (4) Permission for use of this code is granted only
        if the user accepts full responsibility for any
        undesirable consequences. proDAD accept NO LIABILITY
        for damages of any kind.

  ©proDAD
***********************************************************/

/*\
*** Example:
***
\*/


#define __COMPUTER_AMIGA 1
#define NOMYDEBUG

#include "p:pExec/Types.h"
#include "p:pDOS/ArgTags.h"
#include "p:pDOS/DosSig.h"
#include "p:pDOS/DosErrors.h"
#include "p:pGFX/Text.h"
#include "p:pGFX/Color.h"
#include "p:pLayer/Region.h"
#include "p:pLayer/Clip.h"
#include "p:pScreen/ScrTags.h"
#include "p:pScreen/Window.h"
#include "p:pScreen/Screen.h"
#include "p:pIntui/IntuMsg.h"
#include "p:pIntui/Tags.h"
#include "p:pIntui/WinFlags.h"
#include "p:pProto/pLibExt.h"
#include "p:pProto/pExec2.h"
#include "p:pProto/pDOS2.h"
#include "p:pProto/pIntui2.h"
#include "p:pProto/pGFX2.h"
#include "p:pProto/pLayer2.h"

#ifdef _____ME_____
  #include "grund/inc_string.h"
  #include "grund/inc_stdio.h"
#else
 #ifdef __cplusplus
 extern "C" {
 #endif
  #include <string.h>
  #include <stdio.h>
 #ifdef __cplusplus
 }
 #endif
#endif


const CHAR *HelpText=
""
;

const CHAR *PrgHeader=
"";

const CHAR *PrgVerText=
"$VER: 1.0 ("__DATE2__") (Copyright 1996-97 by proDAD) (Created by Holger Burkarth)";


struct pOS_IntuiDevice *gb_IntuiBase;
struct pOS_GfxBase     *gb_GfxBase;
struct pOS_LayerBase   *gb_LayerBase;


BOOL CalcRegion(struct pOS_Window *win,struct pOS_Region* reg);
VOID DrawGfx(struct pOS_Window *win,ULONG pen);




/*----------------------------------
-----------------------------------*/
#ifdef __cplusplus
extern "C"
#endif

VOID main()
{
  struct pOS_DosArgs* Args;
  UWORD Err=0;
  ULONG Ops[1]={0};

  gb_IntuiBase=(struct pOS_IntuiDevice*)pOS_OpenLibrary("pintui.library",0);
  gb_GfxBase=(struct pOS_GfxBase*)pOS_OpenLibrary("pgraphics.library",0);
  gb_LayerBase=(struct pOS_LayerBase*)pOS_OpenLibrary("pLayer.library",0);

  Args=pOS_ReadDosArgs(
"REFRESH_MODE/N",
Ops,sizeof(Ops)/sizeof(ULONG),

    ARGTAG_PrgHeaderText, (ULONG)PrgHeader,    /* kurze Programm-Beschreibung */
    ARGTAG_HelpText,      (ULONG)HelpText,     /* Help-Texte */
    ARGTAG_PrgVerText,    (ULONG)PrgVerText,   /* VER-String */
    TAG_END);


  if(Args) {
    struct pOS_Window   *Win;
    struct pOS_Screen   *Scr;
    struct pOS_Region   *Region;
    struct pOS_ColorMap *ColorMap;
    struct pOS_RColor   RC;
    ULONG Ticks=0,Pen,ActIdx=0;
    const ULONG RMode=Ops[0] ? *((ULONG*)Ops[0]) : 0;

    static const ULONG ColTab[][3]={
      {0,	0,	0},
      {255,	0,	0},
      {0,	255,	0},
      {0,	0,	255},
    };



    RC.rc_U.rc_E.rcue_A=255;

    Region=pOS_CreateRegion();

    if(Region) {
      if(Scr=pOS_LockPubScreen(NULL)) {
        const struct pOS_DrawInfo *const Dri=Scr->scr_DrawInfo;

        Win=pOS_OpenWindow(
            SCRTAG_Screen,     (ULONG)Scr,
            SCRTAG_Flags,      WINFLGF_DepthGadget | WINFLGF_SimpleRefresh |
                               WINFLGF_Activate | WINFLGF_CloseGadget | WINFLGF_Dragbar,
            SCRTAG_IDCMP,      IDCMP_CloseWindow | IDCMP_RefreshWindow | IDCMP_IntuiTicks,
            SCRTAG_Width,      256,
            SCRTAG_Height,     256,
            SCRTAG_UnderMouse, TRUE,
            SCRTAG_AutoAdjust, TRUE,
            TAG_DONE);

        if(Win) {
          struct pOS_IntuiMessage *Msg;
          BOOL Ende=FALSE;

          ColorMap=pOS_GetIColorMap(NULL,Win);
          CalcRegion(Win,Region);

          RC.rc_U.rc_E.rcue_R=ColTab[ActIdx][0];
          RC.rc_U.rc_E.rcue_G=ColTab[ActIdx][1];
          RC.rc_U.rc_E.rcue_B=ColTab[ActIdx][2];

          Pen=pOS_ObtainColorPen(ColorMap,~0,&RC,
                  OTCOLPF_Shared | OTCOLPF_Force | OTCOLPF_Flow | OTCOLPF_Display);

          while(!Ende) {
            if(DOSSIGF_CTRL_C & pOS_WaitSignal((1<<Win->win_UserPort->mp_SigBit) | DOSSIGF_CTRL_C)) break;

            while(Msg=(struct pOS_IntuiMessage*)pOS_GetMsg(Win->win_UserPort)) {

              switch(Msg->im_Class) {
                case IDCMP_CloseWindow:  Ende=TRUE; break;

                case IDCMP_RefreshWindow:
                  if(pOS_BeginRefresh(Win,&Region->rg_List)) {
                    DrawGfx(Win,Pen);
                    pOS_EndRefresh(Win,TRUE);
                  }
                  pOS_ReplyMsg(&Msg->im_Message);
                  Msg=NULL;
                  break;


                case IDCMP_IntuiTicks:
                  if(++Ticks>=10) {
                    Ticks=0;
                    pOS_ReleaseColorPen(ColorMap,Pen);
                    if(++ActIdx>=4) ActIdx=0;
                    RC.rc_U.rc_E.rcue_R=ColTab[ActIdx][0];
                    RC.rc_U.rc_E.rcue_G=ColTab[ActIdx][1];
                    RC.rc_U.rc_E.rcue_B=ColTab[ActIdx][2];
                    Pen=pOS_ObtainColorPen(ColorMap,~0,&RC,
                       OTCOLPF_Shared | OTCOLPF_Force | OTCOLPF_Flow | OTCOLPF_Display);

/*\
*** Das System kann jederzeit den Zeichenvorgang unterbrechen.
\*/
                    if(RMode==0) {
                      struct pOS_Region *Reg;

                      Reg=pOS_InstallClipRegion(Win->win_Layer,Region);
                      DrawGfx(Win,Pen);
                      pOS_InstallClipRegion(Win->win_Layer,Reg);
                    }

/*\
*** Einfachste Variante. :-)
\*/
                    else if(RMode==1) {
                      pOS_InvalidWindowRect(Win,NULL,INVALWINF_CreateMsg);
                    }

/*\
*** Schnellste Clipping-Variante, das System kann den Vorgang nicht unterbrechen.
*** => darf nur kurze Zeit gelockt sein
\*/
                    else if(RMode==2) {
                      pOS_LayerClip Clip;
                      if(pOS_LockClipLayer(Win->win_Layer,&Clip,&Region->rg_List,0)) {
                        DrawGfx(Win,Pen);
                        pOS_UnlockClipLayer(Win->win_Layer,&Clip);
                      }
                    }
                  }
                  break;

              }

              if(Msg) { if(pOS_SysIMessage(Msg)) Msg=NULL; }
              if(Msg) pOS_ReplyMsg(&Msg->im_Message);
            }
          }
          pOS_CloseWindow(Win);
          pOS_ReleaseColorPen(ColorMap,Pen);
        }
        else printf("Cannot open window\n");

        pOS_UnlockPubScreen(Scr);
      }
      else printf("Cannot lock PubScreen\n");
      pOS_DeleteRegion(Region);
    }
    else printf("Cannot create Region\n");
    pOS_DeleteDosArgs(Args);  /* Args freigeben */
  }
  else Err=DOSFAIL_FAIL;  /* vollkommen fehlgeschlagen */

  pOS_SetShellFail(Err);

  pOS_CloseLibrary((pOS_Library*)gb_IntuiBase);
  pOS_CloseLibrary((pOS_Library*)gb_GfxBase);
  pOS_CloseLibrary((pOS_Library*)gb_LayerBase);
}



/*----------------------------------
-----------------------------------*/
BOOL CalcRegion(struct pOS_Window *win,struct pOS_Region* reg)
{
  pOS_Rectangle Rect={30,30,win->win_Width-60,win->win_Height-60};

  pOS_ClearRegion(reg,&reg->rg_List); // *** erase all region datas

  return( pOS_OrRectRegion(reg,&Rect) );
}

/*----------------------------------
-----------------------------------*/
VOID DrawGfx(struct pOS_Window *win,ULONG pen)
{
  struct pOS_RastPort *const RP=win->win_RastPort;
  ULONG i;

  pOS_SetAPen(RP,pen);
  for(i=0; i<win->win_Height; i+=5) {
    pOS_SetPosition(RP,0,i);
    pOS_DrawLine(RP,8192,i);
  }
}








