home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / TIMEXSRC.ZIP / UA_EVWIN.C < prev    next >
C/C++ Source or Header  |  1990-03-29  |  14KB  |  444 lines

  1. /* ua_evwin.c -- Event editing window control
  2.  
  3.     February 1990    Mark E. Mallett, Personal Workstation Magazine
  4.  
  5. This module contains the code controlling the event editing dialog
  6. window.
  7.  
  8. Included are the following routines:
  9.  
  10.     event_winproc        Event editing window procedure
  11.  
  12. */
  13.  
  14. #define    INCL_WIN
  15.  
  16. #include <stdio.h>
  17. #include <ctype.h>
  18. #include <malloc.h>
  19. #include <string.h>
  20. #include <os2.h>
  21.  
  22. #include "timex.h"
  23. #include "ua_timex.h"
  24. #include "ua_pm.h"
  25. #include "ua_baif.h"
  26. #include "evpack.h"
  27.  
  28.  
  29. /* Local definitions */
  30.  
  31.  
  32. /* External data referenced */
  33.  
  34. extern  EVENT   *CureventP;             /* Current event */
  35.  
  36. /* External routines used */
  37.  
  38. extern  int     atoi( char *strP );
  39. extern  void    get_events( void );
  40.  
  41. /* Local data publicly available */
  42.  
  43.  
  44. /* Local routines and forward declarations */
  45.  
  46. static  void    fill_evwin( void );
  47. static  int     proc_evdata( void );
  48. static  void    set_dayof( int id );
  49.  
  50. /* Private data */
  51.  
  52. static  HWND    EvwinH;                 /* Event window handle */
  53.  
  54.     /* Table describing the day-of elements */
  55. static struct {
  56.         int     do_id;                  /* Base ID */
  57.         int     do_elC;                 /* Number of elements */
  58.         }       Dotbl[] =  {
  59.         { EEL_DAYOFMON, 31 },
  60.         { EEL_DAYOFWEEK, 7 },
  61.         { 0, 0 }        } ;
  62. /*
  63.  
  64. *//* event_winproc( winH, msgcode, mp1, mp2 )
  65.  
  66.     Window procedure for the event editing window
  67.  
  68. Accepts :
  69.  
  70.     winH        Window handler
  71.     msgcode        Message code
  72.     mp1        First message parameter
  73.     mp2        Second window parameter
  74.  
  75. Returns :
  76.  
  77. */
  78.  
  79. MRESULT EXPENTRY
  80. event_winproc(
  81.     HWND        winH,        /* Window handle */
  82.     USHORT        msgcode,    /* Message code */
  83.     MPARAM        mp1,        /* First parm */
  84.     MPARAM        mp2        /* Second parm */
  85. ) {
  86.  
  87.         int             i;              /* Scratch */
  88.         int             yearX;          /* Index in year list */
  89.         HWND            yearH;          /* Handle for year list */
  90.         char            buf[50];        /* String buffer */
  91.  
  92.     /* Dispatch on the message code */
  93.     switch( msgcode ) {
  94.  
  95.         case    WM_INITDLG:             /* Dialog initialization */
  96.             EvwinH = winH;              /* Remember event window handle */
  97.             set_dayof( EEL_DAYOFMON ); /* Setup day-of dialog */
  98.  
  99.             /* If we're modifying an event, setup screen from it */
  100.             if ( CureventP != NULL )
  101.                 fill_evwin();
  102.  
  103.             break;
  104.  
  105.  
  106.     case    WM_COMMAND:        /* Command from a button */
  107.         switch ( COMMANDMSG( &msgcode )->cmd ) {
  108.  
  109.                 case EEL_YEARADD:       /* Add a year */
  110.                     /* Get the year to add */
  111.                     WinQueryDlgItemText( EvwinH, EEL_YEARENTER, 50, &buf[0] );
  112.                     if ( ( buf[0] == NUL ) || !isdigit( buf[0] ) )
  113.                         warning( EC_NOTOK, "Year must be entered." );
  114.                     else {
  115.                         /* Turn it into a 4-digit year */
  116.                         i = atoi( &buf[0] );
  117.                         sprintf( &buf[0], "%04d", i < 100 ? i + 1900 : i );
  118.  
  119.                         /* Add it to the list */
  120.                         yearH = WinWindowFromID( EvwinH, EEL_YEARLIST );
  121.                         WinSendMsg( yearH, LM_INSERTITEM,
  122.                                      MPFROM2SHORT( LIT_SORTASCENDING, 0 ),
  123.                                      MPFROMP( &buf[0] ) );
  124.                     }
  125.                     return( 0 );
  126.  
  127.                 case EEL_YEARDEL:       /* Delete selected year */
  128.                     yearH = WinWindowFromID( EvwinH, EEL_YEARLIST );
  129.                     yearX = (int)WinSendMsg( yearH, LM_QUERYSELECTION,
  130.                                         MPFROMSHORT( LIT_FIRST ), 0L );
  131.                     if ( yearX == LIT_NONE )
  132.                         warning( EC_OK, "No year selected." );
  133.                     else {
  134.                         /* Remove it from the list */
  135.                         WinSendMsg( yearH, LM_DELETEITEM,
  136.                                         MPFROMSHORT( yearX ), 0L );
  137.                     }
  138.                     return( 0 );
  139.  
  140.  
  141.         case EEL_DONE:
  142.                     /* Process the information */
  143.                     if ( !proc_evdata() )
  144.                         break;          /* Need more info */
  145.  
  146.                     /* Get new events list from database */
  147.                     get_events();
  148.  
  149.                     /* All set -- fall through to get rid of window */
  150.  
  151.                 case EEL_CANCEL:
  152.             WinDismissDlg( winH, TRUE );
  153.                     return( 0 );
  154.  
  155.                 case EEL_DAYOFWEEK+EEL_DAYOF:  /* Switch to day-of-month */
  156.                     set_dayof( EEL_DAYOFMON );
  157.                     return( 0 );
  158.  
  159.                 case EEL_DAYOFMON+EEL_DAYOF:   /* Switch to day-of-week */
  160.                     set_dayof( EEL_DAYOFWEEK );
  161.                     return( 0 );
  162.         }
  163.         return( 0 );
  164.  
  165.  
  166.     case WM_ERASEBACKGROUND:
  167.         return( 1 );
  168.     }
  169.  
  170.     /* Not handled here... give to parent handler */
  171.     return( WinDefDlgProc( winH, msgcode, mp1, mp2 ) );
  172. }
  173. /*
  174.  
  175. *//* fill_evwin()
  176.  
  177.         Fill the event window from the current event
  178.  
  179. */
  180.  
  181. static void
  182. fill_evwin( void ) {
  183.  
  184.         int             i;              /* Scratch */
  185.         int             y;              /* Year number */
  186.         HWND            ylistH;         /* Yearlist window handle */
  187.         char            buf[50];        /* String buffer */
  188.  
  189.     /* Event name */
  190.     WinSetDlgItemText( EvwinH, EEL_EVNAME, CureventP->ev_nameP );
  191.  
  192.     /* Action type */
  193.     WinSetDlgItemText( EvwinH, EEL_ACTYPE,
  194.                         CureventP->ev_acttype == ACTION_RUN ? "RUN" :
  195.                           "???" );
  196.  
  197.     /* Action string */
  198.     WinSetDlgItemText( EvwinH, EEL_ACTION, CureventP->ev_actargP );
  199.  
  200.     /* Priority class */
  201.     sprintf( &buf[0], "%d", CureventP->ev_priclass );
  202.     WinSetDlgItemText( EvwinH, EEL_PRICLASS, &buf[0] );
  203.  
  204.     /* Priority value */
  205.     sprintf( &buf[0], "%d", CureventP->ev_prival );
  206.     WinSetDlgItemText( EvwinH, EEL_PRIVAL, &buf[0] );
  207.  
  208.     /* Years */
  209.     ylistH = WinWindowFromID( EvwinH, EEL_YEARLIST );
  210.     WinSendMsg( ylistH, LM_DELETEALL, NULL, NULL );
  211.     for( i = 0; i < CureventP->ev_yearC; ++i ) {
  212.         for( y = CureventP->ev_yearP[i].y_first;
  213.                 y <= CureventP->ev_yearP[i].y_last;
  214.                    ++y ) {
  215.             sprintf( &buf[0], "%04d", y );
  216.             WinSendMsg( ylistH, LM_INSERTITEM,
  217.                            MPFROM2SHORT( LIT_SORTASCENDING, 0 ),
  218.                            MPFROMP( &buf[0] ) );
  219.         }
  220.     }
  221.  
  222.     /* Months */
  223.     for( i = 0; i < 12; ++i )
  224.         if ( ( CureventP->ev_months & ( 1 << i ) ) != 0 )
  225.             WinSendDlgItemMsg( EvwinH, EEL_MONTH+i, BM_SETCHECK,
  226.                                 MPFROMSHORT( 1 ), 0L );
  227.  
  228.  
  229.     /* Day-of */
  230.     if ( CureventP->ev_dayof == DAYOF_MONTH ) {
  231.         set_dayof( EEL_DAYOFMON );
  232.         for ( i = 0; i < 31; ++i )
  233.             if ( ( CureventP->ev_dayofs & ( 1L << i ) ) != 0 )
  234.                 WinSendDlgItemMsg( EvwinH, EEL_DAYOFMON+i, BM_SETCHECK,
  235.                                 MPFROMSHORT( 1 ), 0L );
  236.     }
  237.     else {
  238.         set_dayof( EEL_DAYOFWEEK );
  239.         for ( i = 0; i < 7; ++i )
  240.             if ( ( CureventP->ev_dayofs & ( 1L << i ) ) != 0 )
  241.                 WinSendDlgItemMsg( EvwinH, EEL_DAYOFWEEK+i, BM_SETCHECK,
  242.                                 MPFROMSHORT( 1 ), 0L );
  243.     }
  244.  
  245.     /* Hours */
  246.     for( i = 0; i < 24; ++i )
  247.         if ( ( CureventP->ev_hours & ( 1L << i ) ) != 0 )
  248.             WinSendDlgItemMsg( EvwinH, EEL_HOUR+i, BM_SETCHECK,
  249.                                 MPFROMSHORT( 1 ), 0L );
  250.  
  251.  
  252.     /* Minutes */
  253.     for( i = 0; i < 60; ++i )
  254.         if ( ( CureventP->ev_mins[i/32] & ( 1L << ( i%32 ) ) ) != 0 )
  255.             WinSendDlgItemMsg( EvwinH, EEL_MIN + (i/5)*5, BM_SETCHECK,
  256.                                 MPFROMSHORT( 1 ), 0L );
  257. }
  258. /*
  259.  
  260. *//* proc_evdata()
  261.  
  262.         Process the event data when it's done
  263.  
  264. Accepts :
  265.  
  266.  
  267. Returns :
  268.  
  269.         < value >       TRUE if processed,
  270.                         FALSE if not all fields are filled in.
  271.                         error return if error.
  272.  
  273. */
  274.  
  275. static int
  276. proc_evdata( void ) {
  277.  
  278.         int             status;
  279.         int             i;              /* Scratch */
  280.         int             n;              /* Another scratch */
  281.         int             priclass;       /* Priority class */
  282.         int             prival;         /* Priority value */
  283.         EVENT           *eventP;        /* Ptr to event */
  284.         HWND            ylistH;         /* Yearlist handle */
  285.         char            evname[100];    /* Name of event */
  286.         char            acttype[20];    /* Action type */
  287.         char            action[100];    /* Action string */
  288.         char            buf[50];        /* Scratch buffer */
  289.  
  290.     /* Get the info that is required */
  291.     WinQueryDlgItemText( EvwinH, EEL_EVNAME, 100, &evname[0] );
  292.     if ( evname[0] == NUL ) {
  293.         warning( EC_OK, "Event name is required." );
  294.         return( FALSE );
  295.     }
  296.  
  297.     WinQueryDlgItemText( EvwinH, EEL_ACTYPE, 20, &acttype[0] );
  298.     if ( acttype[0] == NUL ) {
  299.         warning( EC_OK, "Action Type is required." );
  300.         return( FALSE );
  301.     }
  302.  
  303.     WinQueryDlgItemText( EvwinH, EEL_ACTION, 100, &action[0] );
  304.     if ( action[0] == NUL ) {
  305.         warning( EC_OK, "Action string is required." );
  306.         return( FALSE );
  307.     }
  308.  
  309.  
  310.     /* Make event from the event data ... */
  311.     event_clear();
  312.  
  313.     event_name( &evname[0] );
  314.     if ( stricmp( &acttype[0], "Run" ) == 0 )
  315.         event_acttype( ACTION_RUN );
  316.     event_action( &action[0] );
  317.  
  318.     /* Priority */
  319.     WinQueryDlgItemText( EvwinH, EEL_PRICLASS, 50, &buf[0] );
  320.     if ( buf[0] != NUL )
  321.         priclass = atoi( &buf[0] );
  322.     else
  323.         priclass = -1;
  324.     WinQueryDlgItemText( EvwinH, EEL_PRIVAL, 50, &buf[0] );
  325.     if ( buf[0] != NUL )
  326.         prival = atoi( &buf[0] );
  327.     else
  328.         prival = -1;
  329.     event_pri( prival, priclass );
  330.  
  331.  
  332.     /* Get the year data */
  333.     ylistH = WinWindowFromID( EvwinH, EEL_YEARLIST );
  334.     n = (int)WinSendMsg( ylistH, LM_QUERYITEMCOUNT, NULL, NULL );
  335.     for( i = 0; i < n; ++i ) {
  336.         WinSendMsg( ylistH, LM_QUERYITEMTEXT,
  337.                         MPFROM2SHORT( i, 50 ),
  338.                         MPFROMP( &buf[0] ) );
  339.         event_year( atoi( &buf[0] ), atoi( &buf[0] ) );
  340.     }
  341.  
  342.     /* Get the months */
  343.     for( i = 0; i < 12; ++i )
  344.         if ( WinSendDlgItemMsg( EvwinH, EEL_MONTH+i, BM_QUERYCHECK,
  345.                                 NULL, NULL ) )
  346.             event_month( i );
  347.  
  348.     /* Day-of */
  349.  
  350.     /* Check for day-of-month */
  351.     if ( WinIsWindowEnabled( WinWindowFromID( EvwinH, EEL_DAYOFMON ) ) ) {
  352.         for( i = 0; i < 31; ++i )
  353.             if ( WinSendDlgItemMsg( EvwinH, EEL_DAYOFMON+i, BM_QUERYCHECK,
  354.                                         NULL, NULL ) )
  355.                 event_dayofmon( i+1 );
  356.     }
  357.     else {
  358.         /* Day of week */
  359.         for( i = 0; i < 7; ++i )
  360.             if ( WinSendDlgItemMsg( EvwinH, EEL_DAYOFWEEK+i,BM_QUERYCHECK,
  361.                                         NULL, NULL ) )
  362.                 event_dayofwk( i );
  363.     }
  364.  
  365.  
  366.     /* Get the hours */
  367.     for( i = 0; i < 24; ++i )
  368.         if ( WinSendDlgItemMsg( EvwinH, EEL_HOUR+i, BM_QUERYCHECK,
  369.                                 NULL, NULL ) )
  370.             event_hour( i );
  371.  
  372.     /* Minutes */
  373.     for( i = 0; i < 60; i += 5 )
  374.         if ( WinSendDlgItemMsg( EvwinH, EEL_MIN+i, BM_QUERYCHECK,
  375.                                 NULL, NULL ) )
  376.             event_min( i );
  377.     eventP = event_make();
  378.  
  379.  
  380.     /* Update the database. */
  381.     if ( CureventP == NULL )
  382.         status = ba_add( eventP );
  383.     else
  384.         status = ba_modify( CureventP->ev_nameP, eventP );
  385.  
  386.  
  387.     free( eventP );
  388.  
  389.     return( status );
  390. }
  391. /*
  392.  
  393. *//* set_dayof( id )
  394.  
  395.         Setup the "day of" dialog
  396.  
  397. Accepts :
  398.  
  399.         id              ID of the root of the dialog
  400.  
  401. Returns :
  402.  
  403.  
  404. */
  405.  
  406. static void
  407. set_dayof(
  408.         int             id              /* The ID to set */
  409. ) {
  410.         int             doX;            /* Index into day-of table */
  411.         int             elid;           /* Element id */
  412.         int             baseid;         /* Base ID of group */
  413.         int             maxid;          /* Maximum ID */
  414.         HWND            winH;           /* Subwindow handle */
  415.  
  416.     /* Adjust the passed ID to be the id of the surrounding box. */
  417.     --id;
  418.  
  419.     /* Start update */
  420.     WinEnableWindowUpdate( EvwinH, FALSE );
  421.  
  422.     /* Go through the day-of table */
  423.     for( doX = 0; ( baseid = Dotbl[doX].do_id ) != 0; ++doX ) {
  424.         --baseid;                       /* ID of box */
  425.         maxid = baseid + Dotbl[doX].do_elC;
  426.         for( elid = baseid; elid <= maxid; ++elid ) {
  427.             winH = WinWindowFromID( EvwinH, elid );
  428.  
  429.             WinShowWindow( winH, id == baseid );
  430.             WinEnableWindow( winH, id == baseid );
  431.         }
  432.  
  433.         /* Also do the switch box */
  434.         winH = WinWindowFromID( EvwinH, baseid+EEL_DAYOF+1 );
  435.         WinShowWindow( winH, id == baseid );
  436.         WinEnableWindow( winH, id == baseid );
  437.     }
  438.  
  439.     /* Update the screen now */
  440. /*    WinShowWindow( EvwinH, TRUE );  */
  441.     WinEnableWindowUpdate( EvwinH, TRUE );
  442. }
  443.  
  444.