xpm.h isn't standard until X11R6
[nvi.git] / motif / m_main.c
blobfd311a4f06216920230308f9331c870b98da756a
1 /*-
2 * Copyright (c) 1996
3 * Rob Zimmermann. All rights reserved.
4 * Copyright (c) 1996
5 * Keith Bostic. All rights reserved.
7 * See the LICENSE file for redistribution information.
8 */
10 #include "config.h"
12 #ifndef lint
13 static const char sccsid[] = "$Id: m_main.c,v 8.34 1996/12/18 14:49:49 bostic Exp $ (Berkeley) $Date: 1996/12/18 14:49:49 $";
14 #endif /* not lint */
16 #include <sys/types.h>
17 #include <sys/queue.h>
19 #include <X11/Intrinsic.h>
20 #include <X11/StringDefs.h>
21 #include <Xm/MainW.h>
23 #include <bitstring.h>
24 #include <signal.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
29 #include "../common/common.h"
30 #include "../ipc/ip.h"
31 #include "../motif_l/m_motif.h"
32 #include "../motif_l/vi_mextern.h"
33 #include "extern.h"
35 #if XtSpecificationRelease == 4
36 #define ArgcType Cardinal *
37 #else
38 #define ArgcType int *
39 #endif
41 #if defined(ColorIcon)
42 #if XT_REVISION >= 6
43 #include <X11/xpm.h>
44 #else
45 #include "xpm.h"
46 #endif
48 #include "nvi.xpm" /* Icon pixmap. */
49 #else
50 #include "nvi.xbm" /* Icon bitmap. */
51 #endif
53 static pid_t pid;
54 static Pixel icon_fg,
55 icon_bg;
56 static Pixmap icon_pm;
57 static Widget top_level;
58 static XtAppContext ctx;
60 static void XutInstallColormap __P((String, Widget));
61 static void XutSetIcon __P((Widget, int, int, Pixmap));
62 static void onchld __P((int));
63 static void onexit __P((void));
65 #if ! defined(ColorIcon)
66 static XutResource resource[] = {
67 { "iconForeground", XutRKpixel, &icon_fg },
68 { "iconBackground", XutRKpixel, &icon_bg },
70 #endif
73 /* resources for the vi widgets unless the user overrides them */
74 String fallback_rsrcs[] = {
76 "*font: -*-*-*-r-*--14-*-*-*-m-*-*-*",
77 "*text*fontList: -*-*-*-r-*--14-*-*-*-m-*-*-*",
78 "*Menu*fontList: -*-helvetica-bold-r-normal--14-*-*-*-*-*-*-*",
79 "*fontList: -*-helvetica-medium-r-normal--14-*-*-*-*-*-*-*",
80 "*pointerShape: xterm",
81 "*busyShape: watch",
82 "*iconName: vi",
84 #if ! defined(ColorIcon)
85 /* coloring for the icons */
86 "*iconForeground: XtDefaultForeground",
87 "*iconBackground: XtDefaultBackground",
88 #endif
90 /* layout for the tag stack dialog */
91 "*Tags*visibleItemCount: 5",
93 /* for the text ruler */
94 "*rulerFont: -*-helvetica-medium-r-normal--14-*-*-*-*-*-*-*",
95 "*rulerBorder: 5",
97 /* layout for the new, temporary preferences page */
98 "*toggleOptions.numColumns: 6", /* also used by Find */
99 "*Preferences*tabWidthPercentage: 0",
100 "*Preferences*tabs.shadowThickness: 2",
101 "*Preferences*tabs.font: -*-helvetica-bold-r-normal--14-*-*-*-*-*-*-*",
103 /* --------------------------------------------------------------------- *
104 * anything below this point is only defined when we are not running CDE *
105 * --------------------------------------------------------------------- */
107 /* Do not define default colors when running under CDE
108 * (e.g. VUE on HPUX). The result is that you don't look
109 * like a normal desktop application
111 "?background: gray75",
112 "?screen.background: wheat",
113 "?highlightColor: red",
114 "?Preferences*options.background: gray90",
117 #if defined(__STDC__)
118 static String *get_fallback_rsrcs( String name )
119 #else
120 static String *get_fallback_rsrcs( name )
121 String name;
122 #endif
124 String *copy = (String *) malloc( (1+XtNumber(fallback_rsrcs))*sizeof(String) );
125 int i, running_cde;
126 Display *d;
128 /* connect to server and see if the CDE atoms are present */
129 d = XOpenDisplay(0);
130 running_cde = is_cde( d );
131 XCloseDisplay(d);
133 for ( i=0; i<XtNumber(fallback_rsrcs); i++ ) {
135 /* stop here if running CDE */
136 if ( fallback_rsrcs[i][0] == '?' ) {
137 if ( running_cde ) break;
138 fallback_rsrcs[i] = strdup(fallback_rsrcs[i]);
139 fallback_rsrcs[i][0] = '*';
142 copy[i] = malloc( strlen(name) + strlen(fallback_rsrcs[i]) + 1 );
143 strcpy( copy[i], name );
144 strcat( copy[i], fallback_rsrcs[i] );
147 copy[i] = NULL;
148 return copy;
152 /* create the shell widgetry */
154 #if defined(__STDC__)
155 static void create_top_level_shell( int *argc, char **argv )
156 #else
157 static void create_top_level_shell( argc, argv )
158 int *argc;
159 char **argv;
160 #endif
162 char *ptr;
163 Widget main_w, editor;
164 Display *display;
166 /* X gets quite upset if the program name is not simple */
167 if (( ptr = strrchr( argv[0], '/' )) != NULL ) argv[0] = ++ptr;
168 vi_progname = argv[0];
170 /* create a top-level shell for the window manager */
171 top_level = XtVaAppInitialize( &ctx,
172 vi_progname,
173 NULL, 0, /* options */
174 (ArgcType) argc,
175 argv, /* might get modified */
176 get_fallback_rsrcs( argv[0] ),
177 NULL
179 display = XtDisplay(top_level);
181 /* might need to go technicolor... */
182 XutInstallColormap( argv[0], top_level );
184 /* create our icon
185 * do this *before* realizing the shell widget in case the -iconic
186 * option was specified.
189 #if defined(ColorIcon)
190 int nvi_width, nvi_height;
191 XpmAttributes attr;
193 attr.valuemask = 0;
194 XpmCreatePixmapFromData( display,
195 DefaultRootWindow(display),
196 nvi_xpm,
197 &icon_pm,
198 NULL,
199 &attr
201 nvi_width = attr.width;
202 nvi_height = attr.height;
203 #else
204 /* check the resource database for interesting resources */
205 __XutConvertResources( top_level,
206 vi_progname,
207 resource,
208 XtNumber(resource)
211 icon_pm = XCreatePixmapFromBitmapData(
212 display,
213 DefaultRootWindow(display),
214 (char *) nvi_bits,
215 nvi_width,
216 nvi_height,
217 icon_fg,
218 icon_bg,
219 DefaultDepth( display, DefaultScreen(display) )
221 #endif
222 XutSetIcon( top_level, nvi_height, nvi_width, icon_pm );
225 /* in the shell, we will stack a menubar an editor */
226 main_w = XtVaCreateManagedWidget( "main",
227 xmMainWindowWidgetClass,
228 top_level,
229 NULL
232 /* create the menubar */
233 XtManageChild( (Widget) vi_create_menubar( main_w ) );
235 /* add the VI widget from the library */
236 editor = vi_create_editor( "editor", main_w, onexit );
238 /* put it up */
239 XtRealizeWidget( top_level );
241 /* We *may* want all keyboard events to go to the editing screen.
242 * If the editor is the only widget in the shell that accepts
243 * keyboard input, then the user will expect that he can type when
244 * the pointer is over the scrollbar (for example). This call
245 * causes that to happen.
247 XtSetKeyboardFocus( top_level, XtNameToWidget( editor, "*screen" ) );
252 main(argc, argv)
253 int argc;
254 char *argv[];
256 int i_fd;
259 * Initialize the X widgetry. We must do this before picking off
260 * arguments as well-behaved X programs have common argument lists
261 * (e.g. -rv for reverse video).
263 create_top_level_shell(&argc, argv);
265 /* We need to know if the child process goes away. */
266 (void)signal(SIGCHLD, onchld);
268 /* Run vi: the parent returns, the child is the vi process. */
269 (void)vi_run(argc, argv, &i_fd, &vi_ofd, &pid);
271 /* Tell X that we are interested in input on the pipe. */
272 XtAppAddInput(ctx, i_fd,
273 (XtPointer)XtInputReadMask, vi_input_func, NULL);
275 /* Main loop. */
276 XtAppMainLoop(ctx);
278 /* NOTREACHED */
279 abort();
282 static void
283 XutSetIcon(wid, height, width, p)
284 Widget wid;
285 int height, width;
286 Pixmap p;
288 Display *display = XtDisplay(wid);
289 Window win;
291 /* best bet is to set the icon window */
292 XtVaGetValues( wid, XtNiconWindow, &win, 0 );
294 if ( win == None ) {
295 win = XCreateSimpleWindow( display,
296 RootWindow( display,
297 DefaultScreen( display ) ),
298 0, 0,
299 width, height,
301 CopyFromParent,
302 CopyFromParent
306 if ( win != None ) {
307 XtVaSetValues( wid, XtNiconWindow, win, 0 );
308 XSetWindowBackgroundPixmap( display, win, p );
311 else {
312 /* do it the old fashioned way */
313 XtVaSetValues( wid, XtNiconPixmap, p, 0 );
317 /* Support for multiple colormaps
319 * XutInstallColormap( String name, Widget wid )
320 * The first time called, this routine checks to see if the
321 * resource "name*installColormap" is "True". If so, the
322 * widget is assigned a newly allocated colormap.
324 * Subsequent calls ignore the "name" parameter and use the
325 * same colormap.
327 * Future versions of this routine may handle multiple colormaps
328 * by name.
330 static enum { cmap_look, cmap_use, cmap_ignore } cmap_state = cmap_look;
332 static Boolean use_colormap = False;
334 static XutResource colormap_resources[] = {
335 { "installColormap", XutRKboolean, &use_colormap }
338 static void
339 XutInstallColormap(name, wid)
340 String name;
341 Widget wid;
343 static Colormap cmap = 0;
344 static Display *cmap_display = 0;
345 Display *display = XtDisplay(wid);
347 /* what is the current finite state? */
348 if ( cmap_state == cmap_look ) {
350 /* what does the resource say? */
351 __XutConvertResources( wid,
352 name,
353 colormap_resources,
354 XtNumber(colormap_resources)
357 /* was the result "True"? */
358 if ( ! use_colormap ) {
359 cmap_state = cmap_ignore;
360 return;
363 /* yes it was */
364 cmap_state = cmap_use;
365 cmap_display = display;
366 cmap = XCopyColormapAndFree( display,
367 DefaultColormap( display,
368 DefaultScreen( display )
373 /* use the private colormap? */
374 if ( cmap_state == cmap_use ) {
375 XtVaSetValues( wid, XtNcolormap, cmap, 0 );
380 * onchld --
381 * Handle SIGCHLD.
383 static void
384 onchld(signo)
385 int signo;
387 /* If the vi process goes away, we exit as well. */
388 if (kill(pid, 0))
389 vi_fatal_message(top_level, "The vi process died. Exiting.");
393 * onexit --
394 * Function called when the editor "quits".
396 static void
397 onexit()
399 exit (0);