ksh: fix up shipin for more modern systems WRT test and wc
[oweals/cde.git] / cde / examples / dtprint / AppSpecific.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /* $XConsortium: AppSpecific.c /main/4 1996/05/09 03:39:11 drk $ */
24 #include "PrintDemo.h"
25
26 /*
27  * ------------------------------------------------------------------------
28  * Name: AppObject_new
29  *
30  * Description:
31  *
32  *     Allocates a new AppObject data structure. If parent is non-NULL,
33  *     an application-specific main window work region is created.
34  *
35  * Return value:
36  *
37  *     A pointer to the new AppObject structure.
38  *
39  */
40 #define LABEL_FMTSTR "Filename: %s   Size: %d"
41
42 static char * 
43 ReadFile(char * filename, int * filesize)
44 {
45    char *buffer = NULL;
46    FILE * file;
47    *filesize = 0 ;
48
49    if ((file = fopen(filename, "r")) == NULL) return NULL ;
50
51    fseek(file, 0L, SEEK_END);
52    *filesize = ftell(file);
53    
54    rewind(file);
55    buffer = (char *) malloc(*filesize+1);
56    if (fread(buffer, 1, *filesize, file) == *filesize ) {
57       buffer[*filesize] = '\0';
58       return buffer;
59    }
60
61    free(buffer);
62    return NULL;
63 }
64
65 AppObject*
66 AppObject_new(
67               Widget parent,
68               String file_name)
69 {
70     AppObject* me = (AppObject*)XtCalloc(1, sizeof(AppObject));
71     int filesize ;
72
73     me->main_window = parent ;
74
75     if(file_name != (String)NULL)
76         me->file_name = XtNewString(file_name);
77     else
78         me->file_name = XtNewString("README.txt");
79
80     me->file_buffer = ReadFile(me->file_name, &filesize);
81     if (me->file_buffer == NULL)  {
82         me->file_buffer = XtNewString("abcdefghijklmnopqrstuvwxyz");
83         filesize = strlen("abcdefghijklmnopqrstuvwxyz");
84     }
85
86     if(parent != (Widget)NULL)
87     {
88         XmString label;
89         String buf;
90         
91         buf = XtCalloc(strlen(LABEL_FMTSTR)+strlen(me->file_name)+10,
92                        sizeof(char));
93         sprintf(buf, LABEL_FMTSTR, me->file_name, filesize);
94         label = XmStringCreateLocalized(buf);
95         XtFree(buf);
96         me->widget =
97             XtVaCreateManagedWidget("AppWorkArea",
98                                     xmLabelWidgetClass,
99                                     parent,
100                                     XmNlabelString, label,
101                                     NULL);
102         XmStringFree(label);
103     }
104
105     return me;
106 }
107
108 /*
109  * ------------------------------------------------------------------------
110  * Name: AppObject_customizePrintSetupBox
111  *
112  * Description:
113  *
114  *     Adds application specific items to the passed print setup box.
115  *
116  *     The document file name is presented in the top work area.
117  *
118  * Return value:
119  *
120  *     None.
121  *
122  */
123 void
124 AppObject_customizePrintSetupBox(
125                                  AppObject* me,
126                                  Widget print_dialog)
127 {
128     Widget row;
129     XmString label;
130     Widget w;
131     /*
132      * create the app-specific top work area
133      */
134     XtVaSetValues(print_dialog,
135                   DtNworkAreaLocation, DtWORK_AREA_TOP,
136                   NULL);
137     row = XtVaCreateManagedWidget(
138                                   "DocumentNameRow",
139                                   xmRowColumnWidgetClass,
140                                   print_dialog,
141                                   XmNorientation, XmHORIZONTAL,
142                                   NULL);
143     /*
144      * create the document name label
145      */
146     label = XmStringCreateLocalized("Document:");
147     w = XtVaCreateManagedWidget("DocumentNameLabel",
148                          xmLabelGadgetClass,
149                          row,
150                          XmNlabelString, label,
151                          NULL);
152     XmStringFree(label);
153     /*
154      * create the document name
155      */
156     label = XmStringCreateLocalized(me->file_name);
157     w = XtVaCreateManagedWidget("DocumentName",
158                          xmLabelGadgetClass,
159                          row,
160                          XmNlabelString, label,
161                          NULL);
162     XmStringFree(label);
163 }