break up the options into the new classes Bob and I agreed on
[nvi.git] / motif / m_main.c
blobb4594d2d8257da1c9201ae72962197f4254b65da
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.32 1996/12/17 17:03:36 bostic Exp $ (Berkeley) $Date: 1996/12/17 17:03:36 $";
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 "../motif_l/m_motif.h"
31 #include "../motif_l/vi_mextern.h"
32 #include "extern.h"
34 #if XtSpecificationRelease == 4
35 #define ArgcType Cardinal *
36 #else
37 #define ArgcType int *
38 #endif
40 #if defined(ColorIcon)
41 #include <X11/xpm.h>
42 #include "nvi.xpm" /* Icon pixmap. */
43 #else
44 #include "nvi.xbm" /* Icon bitmap. */
45 #endif
47 static pid_t pid;
48 static Pixel icon_fg,
49 icon_bg;
50 static Pixmap icon_pm;
51 static Widget top_level;
52 static XtAppContext ctx;
54 static void XutInstallColormap __P((String, Widget));
55 static void XutSetIcon __P((Widget, int, int, Pixmap));
56 static void onchld __P((int));
57 static void onexit __P((void));
59 #if ! defined(ColorIcon)
60 static XutResource resource[] = {
61 { "iconForeground", XutRKpixel, &icon_fg },
62 { "iconBackground", XutRKpixel, &icon_bg },
64 #endif
67 /* resources for the vi widgets unless the user overrides them */
68 String fallback_rsrcs[] = {
70 "*font: -*-*-*-r-*--14-*-*-*-m-*-*-*",
71 "*text*fontList: -*-*-*-r-*--14-*-*-*-m-*-*-*",
72 "*Menu*fontList: -*-helvetica-bold-r-normal--14-*-*-*-*-*-*-*",
73 "*fontList: -*-helvetica-medium-r-normal--14-*-*-*-*-*-*-*",
74 "*pointerShape: xterm",
75 "*busyShape: watch",
76 "*iconName: vi",
78 #if ! defined(ColorIcon)
79 /* coloring for the icons */
80 "*iconForeground: XtDefaultForeground",
81 "*iconBackground: XtDefaultBackground",
82 #endif
84 /* layout for the tag stack dialog */
85 "*Tags*visibleItemCount: 5",
87 /* for the text ruler */
88 "*rulerFont: -*-helvetica-medium-r-normal--14-*-*-*-*-*-*-*",
89 "*rulerBorder: 5",
91 /* layout for the new, temporary preferences page */
92 "*toggleOptions.numColumns: 6", /* also used by Find */
93 "*Preferences*tabWidthPercentage: 0",
94 "*Preferences*tabs.shadowThickness: 2",
95 "*Preferences*tabs.font: -*-helvetica-bold-r-normal--14-*-*-*-*-*-*-*",
97 /* --------------------------------------------------------------------- *
98 * anything below this point is only defined when we are not running CDE *
99 * --------------------------------------------------------------------- */
101 /* Do not define default colors when running under CDE
102 * (e.g. VUE on HPUX). The result is that you don't look
103 * like a normal desktop application
105 "?background: gray75",
106 "?screen.background: wheat",
107 "?highlightColor: red",
108 "?Preferences*options.background: gray90",
111 #if defined(__STDC__)
112 static String *get_fallback_rsrcs( String name )
113 #else
114 static String *get_fallback_rsrcs( name )
115 String name;
116 #endif
118 String *copy = (String *) malloc( (1+XtNumber(fallback_rsrcs))*sizeof(String) );
119 int i, running_cde;
120 Display *d;
122 /* connect to server and see if the CDE atoms are present */
123 d = XOpenDisplay(0);
124 running_cde = is_cde( d );
125 XCloseDisplay(d);
127 for ( i=0; i<XtNumber(fallback_rsrcs); i++ ) {
129 /* stop here if running CDE */
130 if ( fallback_rsrcs[i][0] == '?' ) {
131 if ( running_cde ) break;
132 fallback_rsrcs[i] = strdup(fallback_rsrcs[i]);
133 fallback_rsrcs[i][0] = '*';
136 copy[i] = malloc( strlen(name) + strlen(fallback_rsrcs[i]) + 1 );
137 strcpy( copy[i], name );
138 strcat( copy[i], fallback_rsrcs[i] );
141 copy[i] = NULL;
142 return copy;
146 /* create the shell widgetry */
148 #if defined(__STDC__)
149 static void create_top_level_shell( int *argc, char **argv )
150 #else
151 static void create_top_level_shell( argc, argv )
152 int *argc;
153 char **argv;
154 #endif
156 char *ptr;
157 Widget main_w, editor;
158 Display *display;
160 /* X gets quite upset if the program name is not simple */
161 if (( ptr = strrchr( argv[0], '/' )) != NULL ) argv[0] = ++ptr;
162 vi_progname = argv[0];
164 /* create a top-level shell for the window manager */
165 top_level = XtVaAppInitialize( &ctx,
166 vi_progname,
167 NULL, 0, /* options */
168 (ArgcType) argc,
169 argv, /* might get modified */
170 get_fallback_rsrcs( argv[0] ),
171 NULL
173 display = XtDisplay(top_level);
175 /* might need to go technicolor... */
176 XutInstallColormap( argv[0], top_level );
178 /* create our icon
179 * do this *before* realizing the shell widget in case the -iconic
180 * option was specified.
183 #if defined(ColorIcon)
184 int nvi_width, nvi_height;
185 XpmAttributes attr;
187 attr.valuemask = 0;
188 XpmCreatePixmapFromData( display,
189 DefaultRootWindow(display),
190 nvi_xpm,
191 &icon_pm,
192 NULL,
193 &attr
195 nvi_width = attr.width;
196 nvi_height = attr.height;
197 #else
198 /* check the resource database for interesting resources */
199 __XutConvertResources( top_level,
200 vi_progname,
201 resource,
202 XtNumber(resource)
205 icon_pm = XCreatePixmapFromBitmapData(
206 display,
207 DefaultRootWindow(display),
208 (char *) nvi_bits,
209 nvi_width,
210 nvi_height,
211 icon_fg,
212 icon_bg,
213 DefaultDepth( display, DefaultScreen(display) )
215 #endif
216 XutSetIcon( top_level, nvi_height, nvi_width, icon_pm );
219 /* in the shell, we will stack a menubar an editor */
220 main_w = XtVaCreateManagedWidget( "main",
221 xmMainWindowWidgetClass,
222 top_level,
223 NULL
226 /* create the menubar */
227 XtManageChild( (Widget) vi_create_menubar( main_w ) );
229 /* add the VI widget from the library */
230 editor = vi_create_editor( "editor", main_w, onexit );
232 /* put it up */
233 XtRealizeWidget( top_level );
235 /* We *may* want all keyboard events to go to the editing screen.
236 * If the editor is the only widget in the shell that accepts
237 * keyboard input, then the user will expect that he can type when
238 * the pointer is over the scrollbar (for example). This call
239 * causes that to happen.
241 XtSetKeyboardFocus( top_level, XtNameToWidget( editor, "*screen" ) );
246 main(argc, argv)
247 int argc;
248 char *argv[];
250 int i_fd;
253 * Initialize the X widgetry. We must do this before picking off
254 * arguments as well-behaved X programs have common argument lists
255 * (e.g. -rv for reverse video).
257 create_top_level_shell(&argc, argv);
259 /* We need to know if the child process goes away. */
260 (void)signal(SIGCHLD, onchld);
262 /* Run vi: the parent returns, the child is the vi process. */
263 (void)vi_run(argc, argv, &i_fd, &vi_ofd, &pid);
265 /* Tell X that we are interested in input on the pipe. */
266 XtAppAddInput(ctx, i_fd,
267 (XtPointer)XtInputReadMask, vi_input_func, NULL);
269 /* Main loop. */
270 XtAppMainLoop(ctx);
272 /* NOTREACHED */
273 abort();
276 static void
277 XutSetIcon(wid, height, width, p)
278 Widget wid;
279 int height, width;
280 Pixmap p;
282 Display *display = XtDisplay(wid);
283 Window win;
285 /* best bet is to set the icon window */
286 XtVaGetValues( wid, XtNiconWindow, &win, 0 );
288 if ( win == None ) {
289 win = XCreateSimpleWindow( display,
290 RootWindow( display,
291 DefaultScreen( display ) ),
292 0, 0,
293 width, height,
295 CopyFromParent,
296 CopyFromParent
300 if ( win != None ) {
301 XtVaSetValues( wid, XtNiconWindow, win, 0 );
302 XSetWindowBackgroundPixmap( display, win, p );
305 else {
306 /* do it the old fashioned way */
307 XtVaSetValues( wid, XtNiconPixmap, p, 0 );
311 /* Support for multiple colormaps
313 * XutInstallColormap( String name, Widget wid )
314 * The first time called, this routine checks to see if the
315 * resource "name*installColormap" is "True". If so, the
316 * widget is assigned a newly allocated colormap.
318 * Subsequent calls ignore the "name" parameter and use the
319 * same colormap.
321 * Future versions of this routine may handle multiple colormaps
322 * by name.
324 static enum { cmap_look, cmap_use, cmap_ignore } cmap_state = cmap_look;
326 static Boolean use_colormap = False;
328 static XutResource colormap_resources[] = {
329 { "installColormap", XutRKboolean, &use_colormap }
332 static void
333 XutInstallColormap(name, wid)
334 String name;
335 Widget wid;
337 static Colormap cmap = 0;
338 static Display *cmap_display = 0;
339 Display *display = XtDisplay(wid);
341 /* what is the current finite state? */
342 if ( cmap_state == cmap_look ) {
344 /* what does the resource say? */
345 __XutConvertResources( wid,
346 name,
347 colormap_resources,
348 XtNumber(colormap_resources)
351 /* was the result "True"? */
352 if ( ! use_colormap ) {
353 cmap_state = cmap_ignore;
354 return;
357 /* yes it was */
358 cmap_state = cmap_use;
359 cmap_display = display;
360 cmap = XCopyColormapAndFree( display,
361 DefaultColormap( display,
362 DefaultScreen( display )
367 /* use the private colormap? */
368 if ( cmap_state == cmap_use ) {
369 XtVaSetValues( wid, XtNcolormap, cmap, 0 );
374 * onchld --
375 * Handle SIGCHLD.
377 static void
378 onchld(signo)
379 int signo;
381 /* If the vi process goes away, we exit as well. */
382 if (kill(pid, 0))
383 vi_fatal_message(top_level, "The vi process died. Exiting.");
387 * onexit --
388 * Function called when the editor "quits".
390 static void
391 onexit()
393 exit (0);