~ubuntu-branches/ubuntu/trusty/libcdk5/trusty

« back to all changes in this revision

Viewing changes to examples/alphalist_ex.c

  • Committer: Bazaar Package Importer
  • Author(s): John Goerzen
  • Date: 2007-06-06 03:54:31 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20070606035431-ba4gdvw0h6ybffsu
Tags: 5.0.20060507-1
* New upstream release.
* Fixed header patching.  Patch from Robert Schiele.
  Closes: #402978, #416336.
* Update widget count in description.  Closes: #294709.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* $Id: alphalist_ex.c,v 1.13 2004/08/28 01:02:30 tom Exp $ */
 
1
/* $Id: alphalist_ex.c,v 1.22 2006/05/07 23:15:34 tom Exp $ */
2
2
 
3
 
#include <cdk.h>
 
3
#include <cdk_test.h>
4
4
 
5
5
#ifdef HAVE_XCURSES
6
 
char *XCursesProgramName="alphalist_ex";
 
6
char *XCursesProgramName = "alphalist_ex";
7
7
#endif
8
8
 
9
9
/*
10
10
 * This program demonstrates the Cdk alphalist widget.
 
11
 *
 
12
 * Options (in addition to normal CLI parameters):
 
13
 *      -c      create the data after the widget
11
14
 */
 
15
static CDKSCREEN *cdkscreen = 0;
 
16
static char **myUserList = 0;
 
17
static int userSize;
 
18
 
 
19
typedef struct
 
20
{
 
21
   int deleted;                 /* index in current list which is deleted */
 
22
   int original;                /* index in myUserList[] of deleted item */
 
23
   int position;                /* position before delete */
 
24
   int topline;                 /* top-line before delete */
 
25
} UNDO;
 
26
 
 
27
static UNDO *myUndoList;
 
28
static int undoSize;
12
29
 
13
30
/*
14
31
 * This reads the passwd file and retrieves user information.
19
36
   int x = 0;
20
37
   unsigned used = 0;
21
38
 
22
 
   while ( (ent = getpwent ()) != 0)
 
39
   while ((ent = getpwent ()) != 0)
23
40
   {
24
 
      used = CDKallocStrings(list, ent->pw_name, x++, used);
 
41
      used = CDKallocStrings (list, ent->pw_name, x++, used);
25
42
   }
 
43
   endpwent ();
26
44
   return x;
27
45
}
28
46
 
29
 
int main(int argc, char **argv)
30
 
{
31
 
   /* Declare variables. */
32
 
   CDKSCREEN *cdkscreen         = 0;
 
47
#define CB_PARAMS EObjectType cdktype GCC_UNUSED, void* object GCC_UNUSED, void* clientdata GCC_UNUSED, chtype key GCC_UNUSED
 
48
 
 
49
static void fill_undo (CDKALPHALIST *widget, int deleted, char *data)
 
50
{
 
51
   int top = getCDKScrollCurrentTop (widget->scrollField);
 
52
   int item = getCDKAlphalistCurrentItem (widget);
 
53
   int n;
 
54
 
 
55
   myUndoList[undoSize].deleted = deleted;
 
56
   myUndoList[undoSize].topline = top;
 
57
   myUndoList[undoSize].original = -1;
 
58
   myUndoList[undoSize].position = item;
 
59
   for (n = 0; n < userSize; ++n)
 
60
   {
 
61
      if (!strcmp (myUserList[n], data))
 
62
      {
 
63
         myUndoList[undoSize].original = n;
 
64
         break;
 
65
      }
 
66
   }
 
67
   ++undoSize;
 
68
}
 
69
 
 
70
static int do_delete (CB_PARAMS)
 
71
{
 
72
   CDKALPHALIST *widget = (CDKALPHALIST *)clientdata;
 
73
   int size;
 
74
   char **list = getCDKAlphalistContents (widget, &size);
 
75
   int result = FALSE;
 
76
 
 
77
   if (size)
 
78
   {
 
79
      int save = getCDKScrollCurrentTop (widget->scrollField);
 
80
      int first = getCDKAlphalistCurrentItem (widget);
 
81
      int n;
 
82
 
 
83
      fill_undo (widget, first, list[first]);
 
84
      for (n = first; n < size; ++n)
 
85
         list[n] = list[n + 1];
 
86
      setCDKAlphalistContents (widget, list, size - 1);
 
87
      setCDKScrollCurrentTop (widget->scrollField, save);
 
88
      setCDKAlphalistCurrentItem (widget, first);
 
89
      drawCDKAlphalist (widget, BorderOf (widget));
 
90
      result = TRUE;
 
91
   }
 
92
   return result;
 
93
}
 
94
 
 
95
static int do_delete1 (CB_PARAMS)
 
96
{
 
97
   CDKALPHALIST *widget = (CDKALPHALIST *)clientdata;
 
98
   int size;
 
99
   char **list = getCDKAlphalistContents (widget, &size);
 
100
   int result = FALSE;
 
101
 
 
102
   if (size)
 
103
   {
 
104
      int save = getCDKScrollCurrentTop (widget->scrollField);
 
105
      int first = getCDKAlphalistCurrentItem (widget);
 
106
 
 
107
      if (first-- > 0)
 
108
      {
 
109
         int n;
 
110
 
 
111
         fill_undo (widget, first, list[first]);
 
112
         for (n = first; n < size; ++n)
 
113
            list[n] = list[n + 1];
 
114
         setCDKAlphalistContents (widget, list, size - 1);
 
115
         setCDKScrollCurrentTop (widget->scrollField, save);
 
116
         setCDKAlphalistCurrentItem (widget, first);
 
117
         drawCDKAlphalist (widget, BorderOf (widget));
 
118
         result = TRUE;
 
119
      }
 
120
   }
 
121
   return result;
 
122
}
 
123
 
 
124
static int do_help (CB_PARAMS)
 
125
{
 
126
   static char *message[] =
 
127
   {
 
128
      "Alpha List tests:",
 
129
      "",
 
130
      "F1 = help (this message)",
 
131
      "F2 = delete current item",
 
132
      "F3 = delete previous item",
 
133
      "F4 = reload all items",
 
134
      "F5 = undo deletion",
 
135
      0
 
136
   };
 
137
   popupLabel (cdkscreen, message, CDKcountStrings (message));
 
138
   return TRUE;
 
139
}
 
140
 
 
141
static int do_reload (CB_PARAMS)
 
142
{
 
143
   int result = FALSE;
 
144
 
 
145
   if (userSize)
 
146
   {
 
147
      CDKALPHALIST *widget = (CDKALPHALIST *)clientdata;
 
148
      setCDKAlphalistContents (widget, myUserList, userSize);
 
149
      setCDKAlphalistCurrentItem (widget, 0);
 
150
      drawCDKAlphalist (widget, BorderOf (widget));
 
151
      result = TRUE;
 
152
   }
 
153
   return result;
 
154
}
 
155
 
 
156
static int do_undo (CB_PARAMS)
 
157
{
 
158
   int result = FALSE;
 
159
 
 
160
   if (undoSize > 0)
 
161
   {
 
162
      CDKALPHALIST *widget = (CDKALPHALIST *)clientdata;
 
163
      int size;
 
164
      int n;
 
165
      char **oldlist = getCDKAlphalistContents (widget, &size);
 
166
      char **newlist = (char **)malloc ((++size + 1) * sizeof (char *));
 
167
 
 
168
      --undoSize;
 
169
      newlist[size] = 0;
 
170
      for (n = size - 1; n > myUndoList[undoSize].deleted; --n)
 
171
      {
 
172
         newlist[n] = copyChar (oldlist[n - 1]);
 
173
      }
 
174
      newlist[n--] = copyChar (myUserList[myUndoList[undoSize].original]);
 
175
      while (n >= 0)
 
176
      {
 
177
         newlist[n] = copyChar (oldlist[n]);
 
178
         --n;
 
179
      }
 
180
      setCDKAlphalistContents (widget, newlist, size);
 
181
      setCDKScrollCurrentTop (widget->scrollField, myUndoList[undoSize].topline);
 
182
      setCDKAlphalistCurrentItem (widget, myUndoList[undoSize].position);
 
183
      drawCDKAlphalist (widget, BorderOf (widget));
 
184
      result = TRUE;
 
185
   }
 
186
   return result;
 
187
}
 
188
 
 
189
int main (int argc, char **argv)
 
190
{
33
191
   CDKALPHALIST *alphaList      = 0;
34
192
   WINDOW *cursesWin            = 0;
35
193
   char *title                  = "<C></B/24>Alpha List\n<C>Title";
36
194
   char *label                  = "</B>Account: ";
37
195
   char *word                   = 0;
38
 
   char **info                  = 0;
 
196
   char **userList              = 0;
39
197
   char *mesg[5], temp[256];
40
 
   int count;
41
198
 
42
199
   CDK_PARAMS params;
43
200
 
44
 
   CDKparseParams(argc, argv, &params, CDK_CLI_PARAMS);
 
201
   CDKparseParams (argc, argv, &params, "c" CDK_CLI_PARAMS);
 
202
 
 
203
   /* Get the user list. */
 
204
   userSize = getUserList (&userList);
 
205
   if (userSize <= 0)
 
206
   {
 
207
      fprintf (stderr, "Cannot get user list\n");
 
208
      ExitProgram (EXIT_FAILURE);
 
209
   }
 
210
   myUserList = copyCharList (userList);
 
211
   myUndoList = (UNDO *) malloc (userSize * sizeof (UNDO));
 
212
   undoSize = 0;
45
213
 
46
214
   /* Set up CDK. */
47
 
   cursesWin = initscr();
 
215
   cursesWin = initscr ();
48
216
   cdkscreen = initCDKScreen (cursesWin);
49
217
 
50
218
   /* Start color. */
51
 
   initCDKColor();
52
 
 
53
 
   /* Get the user list. */
54
 
   count = getUserList (&info);
 
219
   initCDKColor ();
55
220
 
56
221
   /* Create the alpha list widget. */
57
222
   alphaList = newCDKAlphalist (cdkscreen,
58
 
                                CDKparamValue(&params, 'X', CENTER),
59
 
                                CDKparamValue(&params, 'Y', CENTER),
60
 
                                CDKparamValue(&params, 'H', 0),
61
 
                                CDKparamValue(&params, 'W', 0),
 
223
                                CDKparamValue (&params, 'X', CENTER),
 
224
                                CDKparamValue (&params, 'Y', CENTER),
 
225
                                CDKparamValue (&params, 'H', 0),
 
226
                                CDKparamValue (&params, 'W', 0),
62
227
                                title, label,
63
 
                                info, count,
 
228
                                CDKparamNumber (&params, 'c') ? 0 : userList,
 
229
                                CDKparamNumber (&params, 'c') ? 0 : userSize,
64
230
                                '_', A_REVERSE,
65
 
                                CDKparamValue(&params, 'N', TRUE),
66
 
                                CDKparamValue(&params, 'S', FALSE));
67
 
   CDKfreeStrings(info);
 
231
                                CDKparamValue (&params, 'N', TRUE),
 
232
                                CDKparamValue (&params, 'S', FALSE));
 
233
   if (alphaList == 0)
 
234
   {
 
235
      destroyCDKScreen (cdkscreen);
 
236
      endCDK ();
 
237
 
 
238
      fprintf (stderr, "Cannot create widget\n");
 
239
      ExitProgram (EXIT_FAILURE);
 
240
   }
 
241
 
 
242
   bindCDKObject (vALPHALIST, alphaList, '?', do_help, NULL);
 
243
   bindCDKObject (vALPHALIST, alphaList, KEY_F1, do_help, NULL);
 
244
   bindCDKObject (vALPHALIST, alphaList, KEY_F2, do_delete, alphaList);
 
245
   bindCDKObject (vALPHALIST, alphaList, KEY_F3, do_delete1, alphaList);
 
246
   bindCDKObject (vALPHALIST, alphaList, KEY_F4, do_reload, alphaList);
 
247
   bindCDKObject (vALPHALIST, alphaList, KEY_F5, do_undo, alphaList);
 
248
 
 
249
   if (CDKparamNumber (&params, 'c'))
 
250
   {
 
251
      setCDKAlphalistContents (alphaList, userList, userSize);
 
252
   }
68
253
 
69
254
   /* Let them play with the alpha list. */
70
255
   word = activateCDKAlphalist (alphaList, 0);
73
258
   if (alphaList->exitType == vESCAPE_HIT)
74
259
   {
75
260
      mesg[0] = "<C>You hit escape. No word was selected.";
76
 
      mesg[1] = "",
 
261
      mesg[1] = "";
77
262
      mesg[2] = "<C>Press any key to continue.";
78
263
      popupLabel (cdkscreen, mesg, 3);
79
264
   }
80
265
   else if (alphaList->exitType == vNORMAL)
81
266
   {
82
267
      mesg[0] = "<C>You selected the following";
83
 
      sprintf (temp, "<C>(%.*s)", (int)(sizeof(temp) - 10), word);
 
268
      sprintf (temp, "<C>(%.*s)", (int)(sizeof (temp) - 10), word);
84
269
      mesg[1] = temp;
85
270
      mesg[2] = "";
86
271
      mesg[3] = "<C>Press any key to continue.";
87
272
      popupLabel (cdkscreen, mesg, 4);
88
273
   }
89
274
 
90
 
   /* Clean up. */
 
275
   freeCharList (myUserList, userSize);
 
276
   free (myUserList);
 
277
 
91
278
   destroyCDKAlphalist (alphaList);
92
279
   destroyCDKScreen (cdkscreen);
93
 
   endCDK();
94
 
   exit (EXIT_SUCCESS);
 
280
   endCDK ();
 
281
 
 
282
   ExitProgram (EXIT_SUCCESS);
95
283
}