distrib: run libtoolize
[nvi.git] / motif / m_main.c
blob3d60b4319b0c22b56934bc1fcff047b066d2d8c2
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.40 2003/11/05 17:09:58 skimo Exp $ (Berkeley) $Date: 2003/11/05 17:09:58 $";
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 #undef LOCK_SUCCESS
30 #include "../common/common.h"
31 #include "../ipc/ip.h"
32 #include "../motif_l/m_motif.h"
33 #include "../motif_l/vi_mextern.h"
34 #include "extern.h"
36 int vi_ifd = -1;
37 int vi_ofd = -1;
38 IPVIWIN *ipvi_motif;
40 #if XtSpecificationRelease == 4
41 #define ArgcType Cardinal *
42 #else
43 #define ArgcType int *
44 #endif
46 #if defined(ColorIcon)
47 #if XT_REVISION >= 6
48 #include <X11/xpm.h>
49 #else
50 #include "xpm.h"
51 #endif
53 #include "nvi.xpm" /* Icon pixmap. */
54 #else
55 #include "nvi.xbm" /* Icon bitmap. */
56 #endif
58 static pid_t pid;
59 static Pixel icon_fg,
60 icon_bg;
61 static Pixmap icon_pm;
62 static Widget top_level;
63 static XtAppContext ctx;
65 static void XutInstallColormap __P((String, Widget));
66 static void XutSetIcon __P((Widget, int, int, Pixmap));
67 static void onchld __P((int));
68 static void onexit __P((void));
70 #if ! defined(ColorIcon)
71 static XutResource resource[] = {
72 { "iconForeground", XutRKpixel, &icon_fg },
73 { "iconBackground", XutRKpixel, &icon_bg },
75 #endif
78 /* resources for the vi widgets unless the user overrides them */
79 String fallback_rsrcs[] = {
81 "*font: -*-*-*-r-*--14-*-*-*-m-*-*-*",
82 "*text*fontList: -*-*-*-r-*--14-*-*-*-m-*-*-*",
83 "*Menu*fontList: -*-helvetica-bold-r-normal--14-*-*-*-*-*-*-*",
84 "*fontList: -*-helvetica-medium-r-normal--14-*-*-*-*-*-*-*",
85 "*pointerShape: xterm",
86 "*busyShape: watch",
87 "*iconName: vi",
89 #if ! defined(ColorIcon)
90 /* coloring for the icons */
91 "*iconForeground: XtDefaultForeground",
92 "*iconBackground: XtDefaultBackground",
93 #endif
95 /* layout for the tag stack dialog */
96 "*Tags*visibleItemCount: 5",
98 /* for the text ruler */
99 "*rulerFont: -*-helvetica-medium-r-normal--14-*-*-*-*-*-*-*",
100 "*rulerBorder: 5",
102 /* layout for the new, temporary preferences page */
103 "*toggleOptions.numColumns: 6", /* also used by Find */
104 "*Preferences*tabWidthPercentage: 0",
105 "*Preferences*tabs.shadowThickness: 2",
106 "*Preferences*tabs.font: -*-helvetica-bold-r-normal--14-*-*-*-*-*-*-*",
108 /* --------------------------------------------------------------------- *
109 * anything below this point is only defined when we are not running CDE *
110 * --------------------------------------------------------------------- */
112 /* Do not define default colors when running under CDE
113 * (e.g. VUE on HPUX). The result is that you don't look
114 * like a normal desktop application
116 "?background: gray75",
117 "?screen.background: wheat",
118 "?highlightColor: red",
119 "?Preferences*options.background: gray90",
122 #if defined(__STDC__)
123 static String *get_fallback_rsrcs( String name )
124 #else
125 static String *get_fallback_rsrcs( name )
126 String name;
127 #endif
129 String *copy = (String *) malloc( (1+XtNumber(fallback_rsrcs))*sizeof(String) );
130 int i, running_cde;
131 Display *d;
133 /* connect to server and see if the CDE atoms are present */
134 d = XOpenDisplay(0);
135 running_cde = is_cde( d );
136 XCloseDisplay(d);
138 for ( i=0; i<XtNumber(fallback_rsrcs); i++ ) {
140 /* stop here if running CDE */
141 if ( fallback_rsrcs[i][0] == '?' ) {
142 if ( running_cde ) break;
143 fallback_rsrcs[i] = strdup(fallback_rsrcs[i]);
144 fallback_rsrcs[i][0] = '*';
147 copy[i] = malloc( strlen(name) + strlen(fallback_rsrcs[i]) + 1 );
148 strcpy( copy[i], name );
149 strcat( copy[i], fallback_rsrcs[i] );
152 copy[i] = NULL;
153 return copy;
157 /* create the shell widgetry */
159 #if defined(__STDC__)
160 static void create_top_level_shell( int *argc, char **argv )
161 #else
162 static void create_top_level_shell( argc, argv )
163 int *argc;
164 char **argv;
165 #endif
167 char *ptr;
168 Widget main_w, editor;
169 Display *display;
171 /* X gets quite upset if the program name is not simple */
172 if (( ptr = strrchr( argv[0], '/' )) != NULL ) argv[0] = ++ptr;
173 vi_progname = argv[0];
175 /* create a top-level shell for the window manager */
176 top_level = XtVaAppInitialize( &ctx,
177 vi_progname,
178 NULL, 0, /* options */
179 (ArgcType) argc,
180 argv, /* might get modified */
181 get_fallback_rsrcs( argv[0] ),
182 NULL
184 display = XtDisplay(top_level);
186 /* might need to go technicolor... */
187 XutInstallColormap( argv[0], top_level );
189 /* create our icon
190 * do this *before* realizing the shell widget in case the -iconic
191 * option was specified.
194 #if defined(ColorIcon)
195 int nvi_width, nvi_height;
196 XpmAttributes attr;
198 attr.valuemask = 0;
199 XpmCreatePixmapFromData( display,
200 DefaultRootWindow(display),
201 nvi_xpm,
202 &icon_pm,
203 NULL,
204 &attr
206 nvi_width = attr.width;
207 nvi_height = attr.height;
208 #else
209 /* check the resource database for interesting resources */
210 __XutConvertResources( top_level,
211 vi_progname,
212 resource,
213 XtNumber(resource)
216 icon_pm = XCreatePixmapFromBitmapData(
217 display,
218 DefaultRootWindow(display),
219 (char *) nvi_bits,
220 nvi_width,
221 nvi_height,
222 icon_fg,
223 icon_bg,
224 DefaultDepth( display, DefaultScreen(display) )
226 #endif
227 XutSetIcon( top_level, nvi_height, nvi_width, icon_pm );
230 /* in the shell, we will stack a menubar an editor */
231 main_w = XtVaCreateManagedWidget( "main",
232 xmMainWindowWidgetClass,
233 top_level,
234 NULL
237 /* create the menubar */
238 XtManageChild( (Widget) vi_create_menubar( main_w ) );
240 /* add the VI widget from the library */
241 editor = vi_create_editor( "editor", main_w, onexit );
243 /* put it up */
244 XtRealizeWidget( top_level );
246 /* We *may* want all keyboard events to go to the editing screen.
247 * If the editor is the only widget in the shell that accepts
248 * keyboard input, then the user will expect that he can type when
249 * the pointer is over the scrollbar (for example). This call
250 * causes that to happen.
252 XtSetKeyboardFocus( top_level, XtNameToWidget( editor, "*screen" ) );
257 main(int argc, char **argv)
259 IPVI* ipvi;
261 * Initialize the X widgetry. We must do this before picking off
262 * arguments as well-behaved X programs have common argument lists
263 * (e.g. -rv for reverse video).
265 create_top_level_shell(&argc, argv);
267 /* We need to know if the child process goes away. */
268 (void)signal(SIGCHLD, onchld);
270 vi_create(&ipvi, 0);
271 (void)ipvi->run(ipvi, argc, argv);
272 ipvi->new_window(ipvi,&ipvi_motif,-1);
273 ipvi_motif->set_ops(ipvi_motif, &ipsi_ops_motif);
274 /* Run vi: the parent returns, the child is the vi process. */
275 vi_ifd = ipvi_motif->ifd;
276 vi_ofd = ipvi_motif->ofd;
277 pid = ipvi->pid;
279 /* Tell X that we are interested in input on the pipe. */
280 XtAppAddInput(ctx, vi_ifd,
281 (XtPointer)XtInputReadMask, vi_input_func, NULL);
283 /* Main loop. */
284 XtAppMainLoop(ctx);
286 /* NOTREACHED */
287 abort();
290 static void
291 XutSetIcon(Widget wid, int height, int width, Pixmap p)
293 Display *display = XtDisplay(wid);
294 Window win;
296 /* best bet is to set the icon window */
297 XtVaGetValues( wid, XtNiconWindow, &win, 0 );
299 if ( win == None ) {
300 win = XCreateSimpleWindow( display,
301 RootWindow( display,
302 DefaultScreen( display ) ),
303 0, 0,
304 width, height,
306 CopyFromParent,
307 CopyFromParent
311 if ( win != None ) {
312 XtVaSetValues( wid, XtNiconWindow, win, 0 );
313 XSetWindowBackgroundPixmap( display, win, p );
316 else {
317 /* do it the old fashioned way */
318 XtVaSetValues( wid, XtNiconPixmap, p, 0 );
322 /* Support for multiple colormaps
324 * XutInstallColormap( String name, Widget wid )
325 * The first time called, this routine checks to see if the
326 * resource "name*installColormap" is "True". If so, the
327 * widget is assigned a newly allocated colormap.
329 * Subsequent calls ignore the "name" parameter and use the
330 * same colormap.
332 * Future versions of this routine may handle multiple colormaps
333 * by name.
335 static enum { cmap_look, cmap_use, cmap_ignore } cmap_state = cmap_look;
337 static Boolean use_colormap = False;
339 static XutResource colormap_resources[] = {
340 { "installColormap", XutRKboolean, &use_colormap }
343 static void
344 XutInstallColormap(String name, Widget wid)
346 static Colormap cmap = 0;
347 static Display *cmap_display = 0;
348 Display *display = XtDisplay(wid);
350 /* what is the current finite state? */
351 if ( cmap_state == cmap_look ) {
353 /* what does the resource say? */
354 __XutConvertResources( wid,
355 name,
356 colormap_resources,
357 XtNumber(colormap_resources)
360 /* was the result "True"? */
361 if ( ! use_colormap ) {
362 cmap_state = cmap_ignore;
363 return;
366 /* yes it was */
367 cmap_state = cmap_use;
368 cmap_display = display;
369 cmap = XCopyColormapAndFree( display,
370 DefaultColormap( display,
371 DefaultScreen( display )
376 /* use the private colormap? */
377 if ( cmap_state == cmap_use ) {
378 XtVaSetValues( wid, XtNcolormap, cmap, 0 );
383 * onchld --
384 * Handle SIGCHLD.
386 static void
387 onchld(int signo)
389 /* If the vi process goes away, we exit as well. */
390 if (kill(pid, 0))
391 vi_fatal_message(top_level, "The vi process died. Exiting.");
395 * onexit --
396 * Function called when the editor "quits".
398 static void
399 onexit(void)
401 exit (0);