home *** CD-ROM | disk | FTP | other *** search
/ Vectronix 2 / VECTRONIX2.iso / FILES_01 / PC_V11_B.LZH / DEMO_PC / CLOCKACC.C < prev    next >
C/C++ Source or Header  |  1993-07-03  |  15KB  |  372 lines

  1.  
  2. #include <aes.h>
  3. #include <vdi.h>
  4. #include <stdio.h>
  5. #include <time.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8.  
  9. /* -------------------------------------------------------------------- */
  10. /*       Makros.                                                        */
  11. /* -------------------------------------------------------------------- */
  12.  
  13. #define min(a, b)             ((a) < (b) ? (a) : (b))
  14. #define max(a, b)             ((a) > (b) ? (a) : (b))
  15.  
  16. /* -------------------------------------------------------------------- */
  17. /*       Extern definierte globale Variablen.                           */
  18. /* -------------------------------------------------------------------- */
  19.                                     /* Mittels dieser Variablen kann    */
  20. extern int _app;                    /* das Programm feststellen, ob es  */
  21.                                     /* als Accessory oder normale App-  */
  22.                                     /* likation gestartet wurde.        */
  23. /* -------------------------------------------------------------------- */
  24. /*       Globale Variablen.                                             */
  25. /* -------------------------------------------------------------------- */
  26.  
  27. int    whandle;                     /* Handle für geöffnetes Fenster.   */
  28. char   title[] = " TONY Clock ";  /* Titelzeile des Fensters.         */
  29. int    gl_wchar,                    /* Grö₧e und Breite eines Buchsta-  */
  30.        gl_hchar,                    /* ben (wichtig falls mit unter-    */
  31.        gl_wbox,                     /* schiedlichen Bildschirmauflö-    */
  32.        gl_hbox;                     /* sungen gearbeitet wird) bzw.     */
  33.                                     /* einer Box.                       */
  34. int    phys_handle,                 /* Handles für GEM und VDI.         */
  35.        handle;
  36. int    max_x,                       /* Maximale Grö₧e der Arbeitsfläche */
  37.        max_y;
  38. int    appl_id,                     /* Identifikationsnummer des Prog.  */
  39.        menu_id;                     /* Id.-nummer im Menü 'Desk'.       */
  40.  
  41. time_t Tim = 0;                     /* Minutenzähler.                   */
  42.  
  43. char Time_pattern[40] = "  %H:%M - %a %d.%b %Y";
  44.  
  45. /* -------------------------------------------------------------------- */
  46. /*       Funktionsprototypen.                                           */
  47. /* -------------------------------------------------------------------- */
  48.  
  49. void open_window( void );
  50. int  rc_intersect( GRECT *r1, GRECT *r2 );
  51. void mouse_on( void );
  52. void mouse_off( void );
  53. void redraw_window( int all );
  54. int  handle_message( int pipe[8] );
  55. void event_loop( void );
  56.  
  57. /* -------------------------------------------------------------------- */
  58. /*       void main( void );                                             */
  59. /*                                                                      */
  60. /*       Kernstück des Programms.                                       */
  61. /*                                                                      */
  62. /*       ->                      Nichts.                                */
  63. /*                                                                      */
  64. /*       <-                      Nichts.                                */
  65. /* -------------------------------------------------------------------- */
  66.  
  67. void main( void )
  68. {
  69.    int i;
  70.    int work_in[11];
  71.    int work_out[57];
  72.  
  73.                                  /* ----------------------------------- */
  74.                                  /* Programminitialisierung:            */
  75.                                  /* ----------------------------------- */
  76.    appl_id = appl_init( );
  77.    if ( appl_id != -1 )
  78.    {
  79.       for ( i = 0; i < 20; i++ )
  80.          work_in[i]  = 1;
  81.       work_in[10] = 2;
  82.       phys_handle = graf_handle( &gl_wchar, &gl_hchar, &gl_wbox, &gl_hbox );
  83.       handle = phys_handle;
  84.       v_opnvwk( work_in, &handle, work_out );
  85.       if ( handle != 0 )
  86.       {
  87.          max_x = work_out[0];
  88.          max_y = work_out[1];
  89.          if ( !_app )             /* Falls das Programm als Accessory   */
  90.                                   /* gestartet wurde, hat _app den      */
  91.                                   /* null, sonst eins.                  */
  92.  
  93.             menu_id = menu_register( appl_id, "  Pure C Clock" );
  94.          else
  95.          {
  96.             graf_mouse( 4, (void*)0 );
  97.             open_window( );
  98.          }
  99.  
  100.                                  /* ----------------------------------- */
  101.                                  /* Event Loop                          */
  102.                                  /* ----------------------------------- */
  103.          event_loop( );
  104.  
  105.                                  /* ----------------------------------- */
  106.                                  /* Deinitialisierung                   */
  107.                                  /* ----------------------------------- */
  108.  
  109.          v_clsvwk( handle );
  110.       }
  111.       appl_exit( );
  112.    }
  113.    exit( 0 );
  114. }
  115.  
  116. /* -------------------------------------------------------------------- */
  117. /*       void open_window( void );                                      */
  118. /*                                                                      */
  119. /*       Öffnen eines Fensters für die Zeitanzeige.                     */
  120. /*                                                                      */
  121. /*       ->                      Nichts.                                */
  122. /*                                                                      */
  123. /*       <-                      Nichts.                                */
  124. /* -------------------------------------------------------------------- */
  125.  
  126. void open_window( void )
  127. {
  128.    if ( whandle <= 0 )
  129.    {
  130.       whandle = wind_create( NAME|CLOSER|MOVER, 0, 0, max_x + 1, max_y + 1 );
  131.       if ( whandle <= 0 )
  132.          return;
  133.  
  134.       wind_set( whandle, WF_NAME, title );
  135.       wind_open( whandle, max_x / 10, max_y / 10, max_x / 3 * 2, max_y / 5 );
  136.    }
  137.    else
  138.       wind_set( whandle, WF_TOP );
  139. }
  140.  
  141. /* -------------------------------------------------------------------- */
  142. /*       boolean rc_intersect( GRECT *r1, GRECT *r2 );                  */
  143. /*                                                                      */
  144. /*       Berechnung der Schnittfläche zweier Rechtecke.                 */
  145. /*                                                                      */
  146. /*       -> r1, r2               Pointer auf Rechteckstruktur.          */
  147. /*                                                                      */
  148. /*       <-                      == 0  falls sich die Rechtecke nicht   */
  149. /*                                     schneiden,                       */
  150. /*                               != 0  sonst.                           */
  151. /* -------------------------------------------------------------------- */
  152.  
  153. int rc_intersect( GRECT *r1, GRECT *r2 )
  154. {
  155.    int x, y, w, h;
  156.  
  157.    x = max( r2->g_x, r1->g_x );
  158.    y = max( r2->g_y, r1->g_y );
  159.    w = min( r2->g_x + r2->g_w, r1->g_x + r1->g_w );
  160.    h = min( r2->g_y + r2->g_h, r1->g_y + r1->g_h );
  161.  
  162.    r2->g_x = x;
  163.    r2->g_y = y;
  164.    r2->g_w = w - x;
  165.    r2->g_h = h - y;
  166.  
  167.    return ( ((w > x) && (h > y) ) );
  168. }
  169.  
  170. /* -------------------------------------------------------------------- */
  171. /*       void mouse_on( void );                                         */
  172. /*                                                                      */
  173. /*       Mauszeiger anschalten.                                         */
  174. /*                                                                      */
  175. /*       ->                      Nichts.                                */
  176. /*                                                                      */
  177. /*       <-                      Nichts.                                */
  178. /* -------------------------------------------------------------------- */
  179.  
  180. void mouse_on( void )
  181.  
  182. {
  183.    graf_mouse( M_ON, (void *)0 );
  184. }
  185.  
  186. /* -------------------------------------------------------------------- */
  187. /*       void mouse_off( void );                                        */
  188. /*                                                                      */
  189. /*       Mauszeiger ausschalten.                                        */
  190. /*                                                                      */
  191. /*       ->                      Nichts.                                */
  192. /*                                                                      */
  193. /*       <-                      Nichts.                                */
  194. /* -------------------------------------------------------------------- */
  195.  
  196. void mouse_off( void )
  197. {
  198.    graf_mouse( M_OFF, (void *)0 );
  199. }
  200.  
  201. /* -------------------------------------------------------------------- */
  202. /*       void redraw_window( int all );                                 */
  203. /*                                                                      */
  204. /*       Fensterinhalt neu zeichnen, nachdem er zuvor aus irgendeinem   */
  205. /*       Grunde zerstört wurde, oder weil das Fenster neu geöffnet      */
  206. /*       wurde.                                                         */
  207. /*                                                                      */
  208. /*       -> all                  == 0  Nur Datum und Uhrzeit neu,       */
  209. /*                               != 0  gesamten Fensterinhalt neu       */
  210. /*                                     schreiben                        */
  211. /*                                                                      */
  212. /*       <-                      Nichts.                                */
  213. /* -------------------------------------------------------------------- */
  214.  
  215. void redraw_window( int all )
  216. {
  217.    GRECT     box,
  218.              work;
  219.    int       clip[4];
  220.    char      s[40];                    /* Erhält Datum und Uhrzeit      */
  221.    time_t    tim;
  222.  
  223.    if( whandle <= 0 )                  /* Wenn kein Fenster auf ist,    */
  224.       return;                          /* brauch auch nicht gezeichnet  */
  225.                                        /* zu werden.                    */
  226.  
  227.                                        /* Wieder eine Minute vergangen? */
  228.    if ( ((tim = time( &tim ) / 60 ) > Tim ) || all )
  229.    {
  230.       Tim = tim;                       /* Ja, dann Minuten merken.      */
  231.       tim *= 60;
  232.  
  233.       strftime( s, 40, Time_pattern, localtime( &tim ));
  234.  
  235.       mouse_off( );
  236.  
  237.       vsf_color( handle, 0 );                       /* set white fill   */
  238.       vswr_mode( handle, 1 );                       /* set replace mode */
  239.  
  240.       wind_get( whandle, WF_WORKXYWH, &work.g_x, &work.g_y, &work.g_w, &work.g_h );
  241.       wind_get( whandle, WF_FIRSTXYWH, &box.g_x, &box.g_y, &box.g_w, &box.g_h );
  242.       work.g_w = min( work.g_w, max_x - work.g_x + 1 );
  243.       work.g_h = min( work.g_h, max_y - work.g_y + 1 );
  244.  
  245.       while ( box.g_w > 0 && box.g_h > 0 )
  246.       {
  247.          if( rc_intersect( &work, &box ) )
  248.          {
  249.             clip[0] = box.g_x;
  250.             clip[1] = box.g_y;
  251.             clip[2] = box.g_x + box.g_w - 1;
  252.             clip[3] = box.g_y + box.g_h - 1;
  253.  
  254.             vs_clip( handle, 1, clip );
  255.             if ( all )
  256.                vr_recfl( handle, clip );              /* fill rectangle */
  257.             v_gtext( handle, (work.g_x + work.g_w / 2) - (int)strlen( s ) * gl_wchar / 2,
  258.                      work.g_y + max_y / 10, s );
  259.          }
  260.          wind_get( whandle, WF_NEXTXYWH, &box.g_x, &box.g_y, &box.g_w, &box.g_h );
  261.       }
  262.       mouse_on( );
  263.    }
  264. }
  265.  
  266. /* -------------------------------------------------------------------- */
  267. /*       int handle_message( int pipe[8] );                             */
  268. /*                                                                      */
  269. /*       Auswertung der Ereignisse des Multi-Events bezüglich des       */
  270. /*       Messagebuffers.                                                */
  271. /*                                                                      */
  272. /*       ->                      Zeiger auf den Inhalt des Message-     */
  273. /*                               buffers.                               */
  274. /*                                                                      */
  275. /*       <-                      Falls das Fenster geschlossen wird     */
  276. /*                               und das Programm nicht als Accessory   */
  277. /*                               gestartet wurde wird eine 1 zurückge-  */
  278. /*                               geben zum Zeichen der Programmbeendi-  */
  279. /*                               gung, sonst immer eine 0.              */
  280. /* -------------------------------------------------------------------- */
  281.  
  282. int handle_message( int pipe[8] )
  283. {
  284.    switch ( pipe[0] )
  285.    {
  286.       case WM_REDRAW:
  287.          redraw_window( 1 );
  288.       break;
  289.  
  290.       case WM_TOPPED:
  291.          wind_set( whandle, WF_TOP );
  292.       break;
  293.  
  294.       case WM_CLOSED:
  295.          if ( pipe[3] == whandle )
  296.          {
  297.             wind_close( whandle );
  298.             wind_delete( whandle );
  299.             whandle = 0;
  300.          }
  301.          if ( _app )
  302.             return ( 1 );
  303.       break;
  304.  
  305.       case WM_MOVED:
  306.       case WM_SIZED:
  307.          if ( pipe[3] == whandle )
  308.             wind_set( whandle, WF_CURRXYWH,  pipe[4], pipe[5], pipe[6], pipe[7] );
  309.      break;
  310.  
  311.       case AC_OPEN:
  312.          if ( pipe[4] == menu_id )
  313.             open_window( );
  314.       break;
  315.  
  316.       case AC_CLOSE:
  317.          if ( pipe[3] == menu_id )
  318.             whandle = 0;
  319.       break;
  320.    }
  321.    return ( 0 );
  322. }
  323.  
  324. /* -------------------------------------------------------------------- */
  325. /*    event_loop()                                                      */
  326. /*                                                                      */
  327. /*    Die Multi-Event-Schleife.                                         */
  328. /*                                                                      */
  329. /*       ->                      Nichts.                                */
  330. /*                                                                      */
  331. /*       <-                      Nichts.                                */
  332. /* -------------------------------------------------------------------- */
  333.  
  334. void event_loop( void )
  335. {
  336.    int x, y,
  337.        kstate,
  338.        key,
  339.        clicks,
  340.        event,
  341.        state;
  342.    int pipe[8];
  343.    int quit;
  344.  
  345.    quit = 0;
  346.    do
  347.    {
  348.       event = evnt_multi( MU_MESAG | MU_TIMER,
  349.                           2, 0x1, 1,
  350.                           0, 0, 0, 0, 0,
  351.                           0, 0, 0, 0, 0,
  352.                           pipe,
  353.                           1000, 0,
  354.                           &x, &y, &state, &kstate, &key, &clicks );
  355.  
  356.       wind_update( BEG_UPDATE );
  357.  
  358.       if ( event & MU_MESAG )
  359.          quit = handle_message( pipe );
  360.  
  361.       if ( event & MU_TIMER )
  362.          redraw_window( 0 );
  363.  
  364.       wind_update( END_UPDATE );
  365.    }
  366.    while ( !quit );
  367. }
  368.  
  369. /* -------------------------------------------------------------------- */
  370. /*       End von CLOCKACC.C                                             */
  371. /* -------------------------------------------------------------------- */
  372.