home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 634.lha / lwb_v1.0 / lwb.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-08  |  10.8 KB  |  450 lines

  1. /* LWB.C Copyright 1991 by     
  2.                     Joerg Wesemann
  3.                     Auf der Heide 12
  4.                     W - 2807 Achim - Baden
  5.                     Germany
  6.                     e-mail: b15l@dhbrrz41.bitnet (prefered)
  7.                             joergw@informatik.uni-bremen.de
  8.  
  9.    Read LWB.DOC for distribution rules !
  10. */                
  11.  
  12.  
  13.  
  14.  
  15. #include <exec/types.h>
  16. #include <clib/exec_protos.h>
  17. #include <intuition/intuition.h>
  18. #include <clib/intuition_protos.h>
  19. #include <graphics/gfx.h>
  20. #include <graphics/gfxmacros.h>
  21. #include <clib/graphics_protos.h>
  22. #include <clib/gadtools_protos.h>
  23. #include <clib/dos_protos.h>
  24.  
  25. #include <stdlib.h>
  26. #include <stdio.h>
  27. #include <string.h>
  28.  
  29.  
  30. struct GfxBase *GfxBase=NULL;
  31. struct IntuitionBase *IntuitionBase=NULL;
  32. struct Library *GadToolsBase=NULL;
  33.  
  34. struct Window *win=NULL;        /* MiniWB-Window  */
  35. struct Screen *scrbuf = NULL;    /* Daten des Workbench-Screens  */
  36. struct Window *winptr = NULL;    /* Fuer die Windowliste des Screens  */
  37.  
  38. WORD screenw = 1500;            /* Groesse des WorkbenchScreen  */
  39. WORD screenh = 1000;            /*              -"-             */
  40.  
  41. WORD xoff = 0;                    /* Offsets des Workbench-Screens  */
  42. WORD yoff = 0;                    /*                -"-             */
  43.  
  44. BYTE wxoffset = 0;                /* Window Border offset  */
  45. BYTE wyoffset = 0;
  46.  
  47. WORD inw = 80;                    /* Innenweite des MiniWB-Windows   */        
  48. WORD inh = 0;                    /* zu errechnende Innenhoehe -"-   */
  49.  
  50. WORD winw = 0;                    /* Gesamtweite des MiniWB-Windows  */
  51. WORD winh = 0;                    /* Gesamthoehe         -"-         */
  52.  
  53. WORD vieww = 712;                /* Tatsaechlicher sichtbarer Ausschnitt  */
  54. WORD viewh = 512;                /*                  -"-                  */
  55. WORD correcting = 0;            /* Korrekturwert fuer andere Hertzzahlen */
  56.  
  57. double xmul = 0.0;                /* linearer Umrechnungsfaktor x  */
  58. double ymul = 0.0;                /* linearer Umrechnungsfaktor y  */
  59.  
  60. SHORT imsgMouseX = 0;            /* Intuition Message Mouse x Koordinate  */
  61. SHORT imsgMouseY = 0;            /* Intuition Message Mouse y Koordinate  */
  62.  
  63.  
  64.  
  65. /* Prototypes */
  66. static void cleanExit( int, char * );
  67. static void updateData( void );
  68. static void okPoint( SHORT *, SHORT *);
  69. static void showView( WORD, WORD, int);
  70. static void relFillRectangle( struct RastPort *, long, long, long, long);
  71. static void relRectangle( struct RastPort *, long, long, long, long);
  72.  
  73. #ifdef DEBUG
  74.     static void printScreenData(struct Screen *);
  75.     static void printWindowData(struct Window *);
  76. #endif
  77.  
  78.  
  79.  
  80.  
  81.  
  82. void updateData(void) 
  83. {   
  84.     double wbxy = 0.0; /* Verhaeltnis WB-Hoehe zu WB-Breite */
  85.     
  86.       if( ! GetScreenData(scrbuf, sizeof(struct Screen), WBENCHSCREEN,NULL) ) {
  87.         cleanExit(20,"Keine SCREEN-Daten zu bekommen");
  88.     }
  89.     
  90.     #ifdef DEBUG
  91.         printScreenData(scrbuf);
  92.     #endif
  93.  
  94.     wbxy = (double) screenh / (double) screenw;    
  95.     inh = (WORD) (wbxy * (double) inw);
  96.     
  97.     viewh = scrbuf->Height-correcting;
  98.     vieww = scrbuf->Width;    
  99.  
  100.     if(win) CloseWindow(win);         /*  Wenn offen, dann schliessen  */
  101.  
  102.     xoff = scrbuf->LeftEdge;
  103.     yoff = scrbuf->TopEdge;
  104.  
  105.     if(!(win=OpenWindowTags(NULL,            WA_InnerWidth,inw,
  106.                                             WA_InnerHeight,inh,
  107.                                             WA_Left, vieww-winw-xoff-1,
  108.                                             WA_Top, viewh-winh-yoff-2,
  109.                                             WA_Activate,FALSE,
  110.                                             WA_DragBar,TRUE,
  111.                                             WA_RMBTrap,TRUE,
  112.                                             WA_DepthGadget,TRUE,
  113.                                             WA_CloseGadget,TRUE,
  114.                                             WA_SizeGadget,FALSE,
  115.                                             WA_SmartRefresh,TRUE,
  116.                                             WA_ReportMouse,TRUE,
  117.                                             WA_IDCMP,IDCMP_CLOSEWINDOW|IDCMP_MOUSEBUTTONS|IDCMP_ACTIVEWINDOW|IDCMP_INACTIVEWINDOW,
  118.                                             WA_MinWidth,30,
  119.                                             WA_MinHeight,20,
  120.                                             WA_Title,"LWB",
  121.                                         TAG_DONE)))
  122.     cleanExit(20,"OpenWindow() failed");
  123.  
  124.  
  125.     winw = win->Width;
  126.     winh = win->Height;
  127.     wxoffset = win->BorderLeft+2;
  128.     wyoffset = win->BorderTop+2;
  129.  
  130.     MoveWindow(win,vieww - xoff - winw - win->LeftEdge-1 , 
  131.                    viewh - yoff - winh - win->TopEdge-2    );
  132.  
  133.     #ifdef DEBUG
  134.         printf("Window Move:  x = %d   y= %d\n",vieww - xoff - winw - win->LeftEdge,
  135.                                                 viewh - yoff - winh - win->TopEdge   );
  136.     #endif
  137.  
  138.       if( ! GetScreenData(scrbuf, sizeof(struct Screen), WBENCHSCREEN,NULL) ) {
  139.         cleanExit(20,"Keine SCREEN-Daten zu bekommen");
  140.     }
  141.  
  142.     xmul = ( (double) inw - 4) / (double) screenw;
  143.     ymul = ( (double) inh -4) / (double) screenh;
  144.     
  145.     winptr = scrbuf->FirstWindow;
  146.     while(winptr) {  
  147.         #ifdef DEBUG
  148.             printWindowData(winptr);
  149.         #endif
  150.         if(strcmp("LWB",winptr->Title) && winptr->Width!=screenw) {
  151.             relFillRectangle(win->RPort,winptr->LeftEdge,winptr->TopEdge, winptr->LeftEdge + winptr->Width,winptr->TopEdge+winptr->Height);
  152.         }
  153.         winptr = winptr->NextWindow;
  154.     }
  155.     showView(xoff,yoff,1);
  156. }
  157.  
  158. void main(int argc,char *argv[])
  159. {
  160.     int ende  = 0;
  161.     int ende2 = 0;
  162.     struct IntuiMessage *imsg;
  163.     ULONG imsgClass;
  164.     UWORD imsgCode,topborder;
  165.     SHORT oldmousex = 0;
  166.     SHORT oldmousey = 0;
  167.     WORD  oldxoff    = 0;
  168.     WORD  oldyoff     = 0;
  169.  
  170.     
  171.     if(argc!=3 && argc!=4) 
  172.         cleanExit(20,"Usage:  LWB <vitual_width> <virtual_height> [<correcting>]");
  173.  
  174.     screenw = (WORD) atoi(argv[1]);
  175.     screenh = (WORD) atoi(argv[2]);
  176.     if (argc==4)
  177.         correcting = atoi(argv[3]);
  178.     else
  179.         correcting = 0;
  180.  
  181.     if(screenw==0 || screenh==0) 
  182.         cleanExit(20,"Parameter nicht korrekt");
  183.  
  184.     if(!(GfxBase=(struct GfxBase *)OpenLibrary("graphics.library",36L)))
  185.         cleanExit(20,"Brauche V36 graphics.library");
  186.  
  187.     if(!(IntuitionBase=(struct IntuitionBase *)OpenLibrary("intuition.library",36L)))
  188.         cleanExit(20,"Brauche V36 intuition.library");
  189.  
  190.     if(!(GadToolsBase=(struct Library *)OpenLibrary("gadtools.library",36L)))
  191.         cleanExit(20,"Brauche V36 gadtools.library");
  192.  
  193.     if (!(scrbuf = (struct Screen *) malloc(sizeof(struct Screen)) ) )
  194.         cleanExit(20,"Kein temporärer Speicher für SCREEN-Daten");
  195.  
  196.     onbreak(abort);
  197.     updateData();
  198.     
  199.     while(!ende) {
  200.         chkabort();
  201.  
  202.         Delay(25);
  203.         
  204.           if( ! GetScreenData(scrbuf, sizeof(struct Screen), WBENCHSCREEN,NULL) ) {
  205.             cleanExit(20,"Keine SCREEN-Daten zu bekommen");
  206.         }
  207.                 
  208.         if (xoff!= scrbuf->LeftEdge || yoff!= scrbuf->TopEdge) {
  209.             updateData();
  210.         }
  211.  
  212.         while((!ende) && (imsg= (struct IntuiMessage *) GT_GetIMsg(win->UserPort)) )
  213.         {
  214.             imsgClass=imsg->Class;
  215.             imsgCode=imsg->Code;
  216.             imsgMouseX = imsg->MouseX - wxoffset;
  217.             imsgMouseY = imsg->MouseY - wyoffset;
  218.  
  219.             GT_ReplyIMsg(imsg);
  220.  
  221.             switch(imsgClass)
  222.             {    
  223.                 case IDCMP_ACTIVEWINDOW:
  224.                     WindowToFront(win);
  225.                     break;
  226.                 case IDCMP_INACTIVEWINDOW:
  227.                     break;
  228.                 case IDCMP_CLOSEWINDOW:
  229.                     ende = 1;
  230.                     break;
  231.                 case IDCMP_MOUSEBUTTONS:
  232.                     if( imsgCode == MENUDOWN ) {
  233.                         updateData();
  234.                     }
  235.                     else if( imsgCode == SELECTDOWN ) {
  236.                         showView(xoff,yoff,0); /* altes uebermalen  */
  237.                         ModifyIDCMP(win,IDCMP_MOUSEBUTTONS|IDCMP_MOUSEMOVE);
  238.                         Wait(1 << win->UserPort->mp_SigBit);
  239.                         ende2 = 0;
  240.                         oldmousex = imsgMouseX; /* Werte sichern  */
  241.                         oldmousey = imsgMouseY;
  242.                         while((!ende2)) {
  243.                             while( imsg=(struct IntuiMessage *) GT_GetIMsg(win->UserPort)) {
  244.                                 imsgClass=imsg->Class;
  245.                                 imsgCode=imsg->Code;
  246.                                 imsgMouseX = imsg->MouseX - wxoffset;
  247.                                 imsgMouseY = imsg->MouseY - wyoffset;
  248.  
  249.                                 GT_ReplyIMsg(imsg);
  250.  
  251.                                 switch(imsgClass) {
  252.                                     case IDCMP_MOUSEBUTTONS:
  253.                                         if(imsgCode==SELECTUP) {
  254.                                             okPoint(&imsgMouseX,&imsgMouseY);
  255.                                             MoveScreen(win->WScreen,imsgMouseX-xoff,imsgMouseY-yoff); 
  256.                                             ende2 = 1;
  257.                                         }
  258.                                         break;
  259.                                     case IDCMP_MOUSEMOVE:
  260.                                         showView(xoff - (WORD) ( (double) ( imsgMouseX - oldmousex ) / xmul ), 
  261.                                                  yoff - (WORD) ( (double) ( imsgMouseY - oldmousey ) / ymul ), 0 );
  262.                                 
  263.                                         break;
  264.                                 }
  265.                             } /* of while GetMsg() */
  266.                         } /* while(!ende2) */ 
  267.                         updateData();
  268.                     }
  269.                     break;
  270.                 default:
  271.                     break;    
  272.             }
  273.         }
  274.     }
  275.   
  276.     cleanExit(0,NULL);
  277. }
  278.  
  279.  
  280. void cleanExit(int rc,char *msg)
  281. {
  282.     if(win)
  283.         CloseWindow(win);
  284.         win = NULL;
  285.     if(scrbuf)
  286.         free(scrbuf);
  287.         scrbuf = NULL;
  288.  
  289.     if(IntuitionBase)
  290.         CloseLibrary(IntuitionBase);
  291.         IntuitionBase = NULL;
  292.  
  293.     if(GfxBase)
  294.         CloseLibrary(GfxBase);
  295.         GfxBase= NULL;
  296.  
  297.     if(rc)
  298.         puts(msg);
  299.     exit(rc);    
  300. }
  301.  
  302.  
  303. static void relFillRectangle( struct RastPort *rp, long xl, long yo, long xr, long yu)
  304. {
  305.     xl = (long) (xmul * (double) xl ) + wxoffset;
  306.     yo = (long) (ymul * (double) yo ) + wyoffset;
  307.     xr = (long) (xmul * (double) xr ) + wxoffset;
  308.     yu = (long) (ymul * (double) yu ) + wyoffset;
  309.     
  310.     SetAPen(rp,2);
  311.     RectFill(rp,xl,yo,xr,yu);
  312.     SetAPen(rp,1);
  313.     Move(rp,xl,yo);
  314.     Draw(rp,xr,yo);
  315.     Draw(rp,xr,yu);
  316.     Draw(rp,xl,yu);
  317.     Draw(rp,xl,yo);    
  318. }
  319.  
  320.  
  321. static void relRectangle( struct RastPort *rp, long xl, long yo, long xr, long yu)
  322. {
  323.     xl = (long) (xmul * (double) xl ) + wxoffset;
  324.     yo = (long) (ymul * (double) yo ) + wyoffset;
  325.     xr = (long) (xmul * (double) xr ) + wxoffset;
  326.     yu = (long) (ymul * (double) yu ) + wyoffset;
  327.     
  328.     SetAPen(rp,1);
  329.     Move(rp,xl,yo);
  330.     Draw(rp,xr,yo);
  331.     Draw(rp,xr,yu);
  332.     Draw(rp,xl,yu);
  333.     Draw(rp,xl,yo);    
  334. }
  335.  
  336.  
  337. /* DICE spezifische Abbruchfunktion  */
  338. void abort(void)
  339. {
  340.     cleanExit(1,"CTRL_C gedrueckt");
  341. }
  342.  
  343.  
  344. static void okPoint(SHORT *x , SHORT *y)
  345.     /* erhaelt den geklickten Punkt und verwandelt diesen in die 
  346.        korrekte Start-Koordinate des neuen Fensters
  347.     */
  348. {
  349.     SHORT dxl = 0;
  350.     SHORT dxr = 0;
  351.     SHORT dxu = 0;
  352.     SHORT dxo = 0;
  353.     
  354.  
  355.     /* nach Workbench-Window transformieren  */
  356.     
  357.     *x = (SHORT) ( (double) *x / xmul );
  358.     *y = (SHORT) ( (double) *y / ymul );    
  359.  
  360.     dxl = *x - vieww / 2;
  361.     dxr = *x + vieww / 2;
  362.     dxu = *y + viewh / 2;
  363.     dxo = *y - viewh / 2;
  364.  
  365.     /* Horizontaler Test  */
  366.     if( dxl < 0 ) {
  367.         *x -= dxl;
  368.     }
  369.     else if (dxr > screenw) {
  370.         *x -= (dxr - screenw);
  371.     }
  372.     /* Vertikaler Test   */
  373.     if( dxo < 0) {
  374.         *y -= dxo;
  375.     }
  376.     else if (dxu > screenh) {
  377.         *y -= (dxu - screenh);
  378.     }
  379.     
  380.     *x -= vieww / 2;
  381.     *y -= viewh / 2;
  382.  
  383.     *x *= -1;
  384.     *y *= -1;
  385.  
  386. }
  387.  
  388.  
  389. static void showView(WORD xoff, WORD yoff, int first)
  390. {    
  391.     static WORD xold = 0;        /* alter xoffset  */
  392.     static WORD yold = 0;        /* alter yoffset  */
  393.  
  394.     if(xoff>0) {
  395.         xoff = 0;
  396.     }
  397.     else if (xoff < -screenw + vieww) {
  398.         xoff = -screenw + vieww;
  399.     }
  400.  
  401.     if(yoff>0) {
  402.         yoff = 0;
  403.     }
  404.     else if (yoff < -screenh + viewh) {
  405.         yoff = -screenh + viewh;
  406.     }
  407.     
  408.     SetDrPt(win->RPort,0x5555);    
  409.     SetAPen(win->RPort,1);
  410.  
  411.     if(!first) {                /* altes loeschen  */
  412.         SetDrMd(win->RPort,COMPLEMENT);
  413.  
  414.         relRectangle(win->RPort,-xold,-yold,-xold+vieww,-yold+viewh);
  415.         SetDrMd(win->RPort,JAM1);
  416.     }
  417.     
  418.     SetDrMd(win->RPort,COMPLEMENT);
  419.  
  420.     relRectangle(win->RPort,-xoff,-yoff,-xoff+vieww,-yoff+viewh);
  421.  
  422.     SetDrMd(win->RPort,JAM1);
  423.     SetDrPt(win->RPort,0xffff);
  424.  
  425.     xold = xoff;
  426.     yold = yoff;
  427. }
  428.  
  429.  
  430. #ifdef DEBUG
  431.  
  432. static void printScreenData(struct Screen *s)
  433. {
  434.     printf("\nScreen:\nNextScreen: %d  FirstWindow: %d\n",s->NextScreen,s->FirstWindow);
  435.     printf("LeftEdge: %d    TopEdge: %d    Width: %d    Height: %d\n",s->LeftEdge,s->TopEdge,s->Width,s->Height);
  436.     printf("Title: %s   DefaultTitle: %s\n",s->Title,s->DefaultTitle);
  437.     printf("\n");
  438. }
  439.  
  440. static void printWindowData(struct Window *w)
  441. {
  442.     printf("\nWindow:\nScreen: %d   NextWindow: %d\n",w->WScreen,w->NextWindow);
  443.     printf("LeftEdge:  %d    TopEdge:   %d    Width: %d    Height: %d\n",w->LeftEdge,w->TopEdge,w->Width,w->Height);
  444.     printf("MinWidth:  %d    MaxWidth:  %d\n",w->MinWidth,w->MaxWidth);
  445.     printf("MinHeight: %d    MaxHeight: %d\n",w->MinHeight,w->MaxHeight);
  446.     printf("Title: %s \n",w->Title);
  447.     printf("\n");
  448. }
  449. #endif
  450.