1 /* $Id: viewer_ex.c,v 1.22 2016/12/04 15:22:16 tom Exp $ */
2
3 #include <cdk_test.h>
4
5 #ifdef HAVE_XCURSES
6 char *XCursesProgramName = "viewer_ex";
7 #endif
8
9 /*
10 * This program demonstrates the file selector and the viewer widget.
11 */
main(int argc,char ** argv)12 int main (int argc, char **argv)
13 {
14 /* *INDENT-EQLS* */
15 CDKSCREEN *cdkscreen = 0;
16 CDKVIEWER *example = 0;
17 CDKFSELECT *fSelect = 0;
18 char **info = 0;
19 const char *button[5];
20 char vTitle[256];
21 const char *mesg[4];
22 char temp[256];
23 int selected, lines;
24
25 CDK_PARAMS params;
26 char *filename; /* specify filename, bypassing fselect */
27 char *directory; /* specify starting directory for fselect */
28 int interp_it; /* interpret embedded markup */
29 int link_it; /* load file via embedded link */
30
31 CDKparseParams (argc, argv, ¶ms, "f:d:il" CDK_CLI_PARAMS);
32 /* *INDENT-EQLS* */
33 filename = CDKparamString (¶ms, 'f');
34 directory = CDKparamString2 (¶ms, 'd', ".");
35 interp_it = CDKparamNumber2 (¶ms, 'i', FALSE);
36 link_it = CDKparamNumber2 (¶ms, 'l', FALSE);
37
38 /* Create the viewer buttons. */
39 button[0] = "</5><OK><!5>";
40 button[1] = "</5><Cancel><!5>";
41
42 cdkscreen = initCDKScreen (NULL);
43
44 /* Start color. */
45 initCDKColor ();
46
47 /* Get the filename. */
48 if (filename == 0)
49 {
50 const char *title = "<C>Pick\n<C>A\n<C>File";
51 const char *label = "File: ";
52
53 fSelect = newCDKFselect (cdkscreen,
54 CDKparamValue (¶ms, 'X', CENTER),
55 CDKparamValue (¶ms, 'Y', CENTER),
56 CDKparamValue (¶ms, 'H', 20),
57 CDKparamValue (¶ms, 'W', 65),
58 title, label, A_NORMAL, '_', A_REVERSE,
59 "</5>", "</48>", "</N>", "</N>",
60 CDKparamValue (¶ms, 'N', TRUE),
61 CDKparamValue (¶ms, 'S', FALSE));
62 if (fSelect == 0)
63 {
64 destroyCDKScreen (cdkscreen);
65 endCDK ();
66
67 fprintf (stderr, "Cannot create fselect-widget\n");
68 ExitProgram (EXIT_FAILURE);
69 }
70
71 /*
72 * Set the starting directory. This is not necessary because when
73 * the file selector starts it uses the present directory as a default.
74 */
75 setCDKFselect (fSelect, directory, A_NORMAL, '.', A_REVERSE,
76 "</5>", "</48>", "</N>", "</N>", ObjOf (fSelect)->box);
77
78 /* Activate the file selector. */
79 filename = activateCDKFselect (fSelect, 0);
80
81 /* Check how the person exited from the widget. */
82 if (fSelect->exitType == vESCAPE_HIT)
83 {
84 /* Pop up a message for the user. */
85 mesg[0] = "<C>Escape hit. No file selected.";
86 mesg[1] = "";
87 mesg[2] = "<C>Press any key to continue.";
88 popupLabel (cdkscreen, (CDK_CSTRING2)mesg, 3);
89
90 /* Exit CDK. */
91 destroyCDKFselect (fSelect);
92 destroyCDKScreen (cdkscreen);
93 endCDK ();
94 ExitProgram (EXIT_SUCCESS);
95 }
96 }
97
98 /* Create the file viewer to view the file selected. */
99 example = newCDKViewer (cdkscreen,
100 CDKparamValue (¶ms, 'X', CENTER),
101 CDKparamValue (¶ms, 'Y', CENTER),
102 CDKparamValue (¶ms, 'H', 20),
103 CDKparamValue (¶ms, 'W', -2),
104 (CDK_CSTRING2)button, 2, A_REVERSE,
105 CDKparamValue (¶ms, 'N', TRUE),
106 CDKparamValue (¶ms, 'S', FALSE));
107
108 /* Could we create the viewer widget? */
109 if (example == 0)
110 {
111 /* Exit CDK. */
112 destroyCDKFselect (fSelect);
113 destroyCDKScreen (cdkscreen);
114 endCDK ();
115
116 printf ("Cannot create viewer. Is the window too small?\n");
117 ExitProgram (EXIT_SUCCESS);
118 }
119
120 if (link_it)
121 {
122 info = (char **)calloc (2, sizeof (char *));
123 info[0] = (char *)malloc (5 + strlen (filename));
124 sprintf (info[0], "<F=%s>", filename);
125 lines = -1;
126 interp_it = TRUE;
127 }
128 else
129 {
130 setCDKViewer (example, "reading...", 0, 0, A_REVERSE, TRUE, TRUE, TRUE);
131 /* Open the file and read the contents. */
132 lines = CDKreadFile (filename, &info);
133 if (lines == -1)
134 {
135 endCDK ();
136 printf ("Could not open \"%s\"\n", filename);
137 ExitProgram (EXIT_FAILURE);
138 }
139 }
140
141 /* Set up the viewer title, and the contents to the widget. */
142 sprintf (vTitle, "<C></B/21>Filename:<!21></22>%20s<!22!B>", filename);
143 setCDKViewer (example, vTitle,
144 (CDK_CSTRING2)info, lines,
145 A_REVERSE, interp_it, TRUE, TRUE);
146
147 CDKfreeStrings (info);
148
149 /* Destroy the file selector widget. */
150 destroyCDKFselect (fSelect);
151
152 /* Activate the viewer widget. */
153 selected = activateCDKViewer (example, 0);
154
155 /* Check how the person exited from the widget. */
156 if (example->exitType == vESCAPE_HIT)
157 {
158 mesg[0] = "<C>Escape hit. No Button selected.";
159 mesg[1] = "";
160 mesg[2] = "<C>Press any key to continue.";
161 popupLabel (cdkscreen, (CDK_CSTRING2)mesg, 3);
162 }
163 else if (example->exitType == vNORMAL)
164 {
165 sprintf (temp, "<C>You selected button %d", selected);
166 mesg[0] = temp;
167 mesg[1] = "";
168 mesg[2] = "<C>Press any key to continue.";
169 popupLabel (cdkscreen, (CDK_CSTRING2)mesg, 3);
170 }
171
172 /* Clean up. */
173 destroyCDKViewer (example);
174 destroyCDKScreen (cdkscreen);
175 endCDK ();
176 ExitProgram (EXIT_SUCCESS);
177 }
178