Filter:   InfoImg
download initapp.c
Language: C
License: GPL
Copyright: (C) 1998 Gloria Bueno (C) 1998,1999,2000,2001,2002 Yohann Sulaiman. (C) 2002 Free Software Foundation, Inc.
LOC: 202
Project Info
MultiSlice RTP Environment(yhs)
Server: SourceForge
Type: cvs
...eForge\y\yhs\yhs\root\root\
   app_glob.c
   command_run.c
   command_slice.c
   ctomass.c
   edge.c
   editing_tools.c
   exit.c
   FILE_ID.DIZ
   file_viewer.c
   file2.raw
   filters.c
   glxdino.c
   glxmotif.c
   glxsimple.c
   gpl.html
   icon.h
   initapp.c
   inject.c
   label.c
   log.h
   logmen.h
   logo.h
   main.h
   makefile.32
   makefile.64
   makefile.f32
   measure.c
   menu.c
   multislice.c
   neighbour.c
   open.c
   open.old.c
   paperplane.c
   print.c
   program1.c
   region.c
   region_ops.c
   save.c
   slice.c
   split_merge.c
   textfun.c
   version.c
   viewslice.c
   wals.study
   watershed.c
   xbutton.c
   xmenu.c
   xwins.h
   xwinutil.c
   ys_ext.c
   zoom.c
   zoom2.c

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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
  /*

MultiSlice RTP Environment
(C) 1998,1999,2000,2001,2002 Yohann Sulaiman.
(C) 1998 Gloria Bueno
(C) 2002  Free Software Foundation, Inc. 


    This file is part of the source code of the MultiSlice RTP Environment.

    MultiSlice RTP Environment is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    MultiSlice RTP Environment  is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with MultiSlice RTP Environment; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

*/


/*-------------------------------------------------------------*/
/*  File: initapp.c
 *  
 *  Function to process command-line arguments, connect to the
 *  X display, and set up the main window of a generic
 *  X application.
 *
 */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xresource.h>

#define  DEF_APP_GLOB    /* so that globals get defined */
#include "app_glob.c"

#define max(x,y)     (((x) > (y)) ? (x) : (y))

#define DEFAULT_BGCOLOR         "white"
#define DEFAULT_FGCOLOR         "black"
#define DEFAULT_BDWIDTH         1
#define DEFAULT_FONT            "fixed"

typedef struct APP_PARAMS
{
    char *name;
    char *cname;
    char **p_value_string;
} APP_PARAMS;

/* Default parameter values (as strings) */
char *theBGcolor     = DEFAULT_BGCOLOR,
     *theFGcolor     = DEFAULT_FGCOLOR,
     *theFont        = DEFAULT_FONT,
     *theGeom_rsrc   = NULL,
     *display_name   = NULL,
     *theGeom_cline  = NULL,
     *theGeom        = NULL;

/* List of user-customizable resources */

APP_PARAMS app_resources[] =
{
    "background", "Background",   &theBGcolor,  
    "foreground", "Foreground",   &theFGcolor,
    "font",       "Font",         &theFont,
    "geometry",   "Geometry",     &theGeom_rsrc,
};

int num_resources = sizeof(app_resources) / sizeof(APP_PARAMS);

/* List of command-line options */

APP_PARAMS app_options[] =
{
    "-display",  "-d", &display_name,
    "-geometry", "-g", &theGeom_cline
};

int num_options = sizeof(app_options) / sizeof(APP_PARAMS);

void usage();
static char rname[80];
/*-------------------------------------------------------------*/
void initapp(argc, argv, x, y, width, height)
int argc, x, y;
char **argv; 
unsigned width,  height;
{
    int                  i, j;
    char                 *tmpstr;
    Colormap             default_cmap;
    XColor               color;
    int                  bitmask;
    XGCValues            gcv;
    XSetWindowAttributes xswa;
    char                 default_geometry[80];
    char                 rfilename[80];
    char                 *type;
    XrmValue             value;
    XrmDatabase          rdb;

    theAppName = argv[0];
    AppDone = 0;
    
/* Parse command-line options. Since each option has a value,
 * they are processed in pairs.
 */
    for (i = 1; i < argc; i += 2) 
    {
        for(j = 0; j < num_options; j++)
        {
            if(strcmp(app_options[j].name, argv[i]) == 0 ||
               strcmp(app_options[j].cname, argv[i]) == 0)
            {
                *app_options[j].p_value_string = argv[i+1];
                break;
            }
        }
        if(j >= num_options)
            usage();
    }

/*  Open connection to display selected by user */

    if ((theDisplay = XOpenDisplay(display_name)) == NULL) 
    {
        fprintf(stderr, "%s: can't open display named %s\n", 
                argv[0], XDisplayName(display_name));
        exit(1);
    }

/* Get resources from the resource file */
    XrmInitialize();
    strcpy(rfilename, getenv("HOME"));
    strcat(rfilename, "/.Xdefaults");
    rdb = XrmGetFileDatabase(rfilename);
    if(rdb != NULL) 
    {
        for (i = 0; i < num_resources; i++)
        {
/* Construct a complete resource name by appending the
 * resource name to the application's name.
 */
            strcpy(rname, theAppName);
            strcat(rname, "*");
            strcat(rname, app_resources[i].name);

            if(XrmGetResource(rdb, rname, rname,
                              &type, &value))
            {
                *app_resources[i].p_value_string = value.addr;
	    }
        }
    }

/* Set up colors and fonts */

    if ((theFontStruct = XLoadQueryFont(theDisplay, 
                                           theFont)) == NULL) 
    {
        fprintf(stderr, "%s: display %s cannot load font %s\n",
                theAppName, DisplayString(theDisplay), theFont);
        exit(1);
    }

/* Now select the colors using the default colormap */

    default_cmap = DefaultColormap(theDisplay, 
                                   DefaultScreen(theDisplay));

/* Main window's background color */
    if (XParseColor(theDisplay, default_cmap, theBGcolor, 
                    &color) == 0 ||
        XAllocColor(theDisplay, default_cmap, &color) == 0)

/* Use white background in case of failure */
        theBGpix = WhitePixel(theDisplay, 
                              DefaultScreen(theDisplay));
    else
        theBGpix = color.pixel;

/* Main window's foreground color */
    if (XParseColor(theDisplay, default_cmap, theFGcolor, 
                    &color) == 0 ||
        XAllocColor(theDisplay, default_cmap, &color) == 0)
/* Use black foreground in case of failure */
        theFGpix = BlackPixel(theDisplay, 
                              DefaultScreen(theDisplay));
    else
        theFGpix = color.pixel;

/* Fill out a XsizeHints structure to inform the window manager
 * of desired size and location of main window.
 */
    if((p_XSH = XAllocSizeHints()) == NULL)
    {
        fprintf(stderr, "Error allocating size hints!\n");
        exit(1);
    }
    p_XSH->flags = (PPosition | PSize | PMinSize);
    p_XSH->height = height;
    p_XSH->min_height = p_XSH->height;
    p_XSH->width = width;
    p_XSH->min_width = p_XSH->width;
    p_XSH->x = x;
    p_XSH->y = y;

/* Construct a default geometry string */
    sprintf(default_geometry, "%dx%d+%d+%d", p_XSH->width,
            p_XSH->height, p_XSH->x, p_XSH->y);
    theGeom = default_geometry;

/* Override the geometry, if necessary */
    if(theGeom_cline != NULL) theGeom = theGeom_cline;
    else
        if(theGeom_rsrc != NULL) theGeom = theGeom_rsrc;

/* Process the geometry specification */
    bitmask =  XGeometry(theDisplay, DefaultScreen(theDisplay), 
                   theGeom, default_geometry, DEFAULT_BDWIDTH, 
                   1, 1, 0, 0, &(p_XSH->x), &(p_XSH->y),
                   &(p_XSH->width), &(p_XSH->height));
        
/* Check bitmask and set flags in XSizeHints structure */
    if (bitmask & (XValue | YValue)) p_XSH->flags |= USPosition;
    if (bitmask & (WidthValue | HeightValue))
                                     p_XSH->flags |= USSize;

/* Create the main window using the position and size 
 * information derived above. For border color, use the 
 * foreground color.
 */
    theMain = XCreateSimpleWindow(theDisplay, 
                    DefaultRootWindow(theDisplay), 
                    p_XSH->x, p_XSH->y, p_XSH->width, p_XSH->height, 
                    DEFAULT_BDWIDTH, theFGpix, theBGpix);

/* Set up class hint */
    if((p_CH = XAllocClassHint()) == NULL)
    {
        fprintf(stderr, "Error allocating class hint!\n");
        exit(1);
    }
    p_CH->res_name = theAppName;
    p_CH->res_class = theAppName;

/* Set up XTextProperty for window name and icon name */
    if(XStringListToTextProperty(&theAppName, 1, &WName) == 0)
    {
        fprintf(stderr, "Error creating XTextProperty!\n");
        exit(1);
    }
    if(XStringListToTextProperty(&theAppName, 1, &IName) == 0)
    {
        fprintf(stderr, "Error creating XTextProperty!\n");
        exit(1);
    }

    if((p_XWMH = XAllocWMHints()) == NULL)
    {
        fprintf(stderr, "Error allocating Window Manager hints!\n");
        exit(1);
    }
    p_XWMH->flags = (InputHint|StateHint);
    p_XWMH->input = False;
    p_XWMH->initial_state = NormalState;
    XSetWMProperties(theDisplay, theMain, &WName, &IName, argv, argc,
                     p_XSH, p_XWMH, p_CH);

/* Finally, create a graphics context for the main window */    

    gcv.font = theFontStruct->fid;
    gcv.foreground = theFGpix;
    gcv.background = theBGpix;
    theGC = XCreateGC(theDisplay, theMain, 
                (GCFont | GCForeground | GCBackground), &gcv);

/* Set main window's attributes (colormap, bit_gravity) */

    xswa.colormap = DefaultColormap(theDisplay, 
                                    DefaultScreen(theDisplay));
    xswa.bit_gravity = NorthWestGravity;
    XChangeWindowAttributes(theDisplay, theMain, (CWColormap | 
                            CWBitGravity), &xswa);

/* Select Exposure events for the main window */

    XSelectInput(theDisplay, theMain, ExposureMask);

/* Map the main window */

    XMapWindow(theDisplay, theMain);
    XFlush(theDisplay);
    if(XGetWindowAttributes(theDisplay, theMain, &MainXWA) == 0)
    {
        fprintf(stderr, "Error getting attributes of Main.\n");
        exit(2);
    }
}
/*-------------------------------------------------------------*/
void usage ()
{
    fprintf (stderr, "%s [-display name] [-geometry geom]\n",
             theAppName);
/*    exit (1); */
}