File: calendar_ex.c

package info (click to toggle)
libcdk5 5.0.20050424-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 2,504 kB
  • ctags: 2,620
  • sloc: ansic: 29,645; sh: 4,283; makefile: 634; cpp: 42; sed: 40
file content (147 lines) | stat: -rw-r--r-- 4,223 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/* $Id: calendar_ex.c,v 1.12 2004/08/28 01:02:30 tom Exp $ */

#include <cdk.h>

#ifdef HAVE_XCURSES
char *XCursesProgramName="calendar_ex";
#endif

static BINDFN_PROTO(createCalendarMarkCB);
static BINDFN_PROTO(removeCalendarMarkCB);

/*
 * This program demonstrates the Cdk calendar widget.
 */
int main (int argc, char **argv)
{
   /* Declare variables. */
   CDKSCREEN *cdkscreen		= 0;
   CDKCALENDAR *calendar	= 0;
   WINDOW *cursesWin		= 0;
   char *mesg[5], temp[256];
   struct tm *dateInfo;
   time_t clck, retVal;

   CDK_PARAMS params;
   char *title;
   int day;
   int month;
   int year;

   /*
    * Get the current dates and set the default values for
    * the day/month/year values for the calendar.
    */
   time (&clck);
   dateInfo	= localtime (&clck);

   CDKparseParams(argc, argv, &params, "d:m:y:t:" CDK_MIN_PARAMS);
   day   = CDKparamNumber2(&params, 'd', dateInfo->tm_mday);
   month = CDKparamNumber2(&params, 'm', dateInfo->tm_mon + 1);
   year  = CDKparamNumber2(&params, 'y', dateInfo->tm_year + 1900);
   title = CDKparamString2(&params, 't', "<C></U>CDK Calendar Widget\n<C>Demo");

   /* Set up CDK. */
   cursesWin = initscr();
   cdkscreen = initCDKScreen (cursesWin);

   /* Start CDK Colors. */
   initCDKColor();

   /* Create the calendar widget. */
   calendar = newCDKCalendar (cdkscreen,
			      CDKparamValue(&params, 'X', CENTER),
			      CDKparamValue(&params, 'Y', CENTER),
			      title, day, month, year,
			      COLOR_PAIR(16)|A_BOLD,
			      COLOR_PAIR(24)|A_BOLD,
			      COLOR_PAIR(32)|A_BOLD,
			      COLOR_PAIR(40)|A_REVERSE,
			      CDKparamValue(&params, 'N', TRUE),
			      CDKparamValue(&params, 'S', FALSE));

   /* Is the widget null? */
   if (calendar == 0)
   {
      /* Clean up the memory. */
      destroyCDKScreen (cdkscreen);

      /* End curses... */
      endCDK();

      /* Spit out a message. */
      printf ("Oops. Can't seem to create the calendar. Is the window too small?\n");
      exit (EXIT_FAILURE);
   }

   /* Create a key binding to mark days on the calendar. */
   bindCDKObject (vCALENDAR, calendar, 'm', createCalendarMarkCB, calendar);
   bindCDKObject (vCALENDAR, calendar, 'M', createCalendarMarkCB, calendar);
   bindCDKObject (vCALENDAR, calendar, 'r', removeCalendarMarkCB, calendar);
   bindCDKObject (vCALENDAR, calendar, 'R', removeCalendarMarkCB, calendar);

   /* Draw the calendar widget. */
   drawCDKCalendar (calendar, ObjOf(calendar)->box);

   /* Let the user play with the widget. */
   retVal = activateCDKCalendar (calendar, 0);

   /* Check which day they selected. */
   if (calendar->exitType == vESCAPE_HIT)
   {
      mesg[0] = "<C>You hit escape. No date selected.";
      mesg[1] = "",
      mesg[2] = "<C>Press any key to continue.";
      popupLabel (cdkscreen, mesg, 3);
   }
   else if (calendar->exitType == vNORMAL)
   {
      mesg[0] = "You selected the following date";
      sprintf (temp, "<C></B/16>%02d/%02d/%d (dd/mm/yyyy)", calendar->day, calendar->month, calendar->year);
      mesg[1] = copyChar (temp);
      mesg[2] = "<C>Press any key to continue.";
      popupLabel (cdkscreen, mesg, 3);
      freeChar (mesg[1]);
   }

   /* Clean up and exit. */
   destroyCDKCalendar (calendar);
   destroyCDKScreen (cdkscreen);
   endCDK();
   fflush (stdout);
   printf ("Selected Time: %s\n", ctime(&retVal));
   exit (EXIT_SUCCESS);
}

/*
 * This adds a marker to the calendar.
 */
static int createCalendarMarkCB (EObjectType objectType GCC_UNUSED, void *object, void *clientData GCC_UNUSED, chtype key GCC_UNUSED)
{
   CDKCALENDAR *calendar = (CDKCALENDAR *)object;

   setCDKCalendarMarker (calendar,
				calendar->day,
				calendar->month,
				calendar->year,
				COLOR_PAIR (5) | A_REVERSE);

   drawCDKCalendar (calendar, ObjOf(calendar)->box);
   return (FALSE);
}

/*
 * This removes a marker from the calendar.
 */
static int removeCalendarMarkCB (EObjectType objectType GCC_UNUSED, void *object, void *clientData GCC_UNUSED, chtype key GCC_UNUSED)
{
   CDKCALENDAR *calendar = (CDKCALENDAR *)object;

   removeCDKCalendarMarker (calendar,
				calendar->day,
				calendar->month,
				calendar->year);

   drawCDKCalendar (calendar, ObjOf(calendar)->box);
   return (FALSE);
}