Expand PMF_FN_* macros.
[netbsd-mini2440.git] / dist / nvi / motif / m_main.c
blob732a1378e55fe10d603d1d4b5ea84a0dafb6a653
1 /* $NetBSD$ */
3 /*-
4 * Copyright (c) 1996
5 * Rob Zimmermann. All rights reserved.
6 * Copyright (c) 1996
7 * Keith Bostic. All rights reserved.
9 * See the LICENSE file for redistribution information.
12 #include "config.h"
14 #ifndef lint
15 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";
16 #endif /* not lint */
18 #include <sys/types.h>
19 #include <sys/queue.h>
21 #include <X11/Intrinsic.h>
22 #include <X11/StringDefs.h>
23 #include <Xm/MainW.h>
25 #include <bitstring.h>
26 #include <signal.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
31 #undef LOCK_SUCCESS
32 #include "../common/common.h"
33 #include "../ipc/ip.h"
34 #include "../motif_l/m_motif.h"
35 #include "../motif_l/vi_mextern.h"
36 #include "extern.h"
38 int vi_ifd = -1;
39 int vi_ofd = -1;
40 IPVIWIN *ipvi_motif;
42 #if XtSpecificationRelease == 4
43 #define ArgcType Cardinal *
44 #else
45 #define ArgcType int *
46 #endif
48 #if defined(ColorIcon)
49 #if XT_REVISION >= 6
50 #include <X11/xpm.h>
51 #else
52 #include "xpm.h"
53 #endif
55 #include "nvi.xpm" /* Icon pixmap. */
56 #else
57 #include "nvi.xbm" /* Icon bitmap. */
58 #endif
60 static pid_t pid;
61 static Pixel icon_fg,
62 icon_bg;
63 static Pixmap icon_pm;
64 static Widget top_level;
65 static XtAppContext ctx;
67 static void XutInstallColormap __P((String, Widget));
68 static void XutSetIcon __P((Widget, int, int, Pixmap));
69 static void onchld __P((int));
70 static void onexit __P((void));
72 #if ! defined(ColorIcon)
73 static XutResource resource[] = {
74 { "iconForeground", XutRKpixel, &icon_fg },
75 { "iconBackground", XutRKpixel, &icon_bg },
77 #endif
80 /* resources for the vi widgets unless the user overrides them */
81 String fallback_rsrcs[] = {
83 "*font: -*-*-*-r-*--14-*-*-*-m-*-*-*",
84 "*text*fontList: -*-*-*-r-*--14-*-*-*-m-*-*-*",
85 "*Menu*fontList: -*-helvetica-bold-r-normal--14-*-*-*-*-*-*-*",
86 "*fontList: -*-helvetica-medium-r-normal--14-*-*-*-*-*-*-*",
87 "*pointerShape: xterm",
88 "*busyShape: watch",
89 "*iconName: vi",
91 #if ! defined(ColorIcon)
92 /* coloring for the icons */
93 "*iconForeground: XtDefaultForeground",
94 "*iconBackground: XtDefaultBackground",
95 #endif
97 /* layout for the tag stack dialog */
98 "*Tags*visibleItemCount: 5",
100 /* for the text ruler */
101 "*rulerFont: -*-helvetica-medium-r-normal--14-*-*-*-*-*-*-*",
102 "*rulerBorder: 5",
104 /* layout for the new, temporary preferences page */
105 "*toggleOptions.numColumns: 6", /* also used by Find */
106 "*Preferences*tabWidthPercentage: 0",
107 "*Preferences*tabs.shadowThickness: 2",
108 "*Preferences*tabs.font: -*-helvetica-bold-r-normal--14-*-*-*-*-*-*-*",
110 /* --------------------------------------------------------------------- *
111 * anything below this point is only defined when we are not running CDE *
112 * --------------------------------------------------------------------- */
114 /* Do not define default colors when running under CDE
115 * (e.g. VUE on HPUX). The result is that you don't look
116 * like a normal desktop application
118 "?background: gray75",
119 "?screen.background: wheat",
120 "?highlightColor: red",
121 "?Preferences*options.background: gray90",
124 #if defined(__STDC__)
125 static String *get_fallback_rsrcs( String name )
126 #else
127 static String *get_fallback_rsrcs( name )
128 String name;
129 #endif
131 String *copy = (String *) malloc( (1+XtNumber(fallback_rsrcs))*sizeof(String) );
132 int i, running_cde;
133 Display *d;
135 /* connect to server and see if the CDE atoms are present */
136 d = XOpenDisplay(0);
137 running_cde = is_cde( d );
138 XCloseDisplay(d);
140 for ( i=0; i<XtNumber(fallback_rsrcs); i++ ) {
142 /* stop here if running CDE */
143 if ( fallback_rsrcs[i][0] == '?' ) {
144 if ( running_cde ) break;
145 fallback_rsrcs[i] = strdup(fallback_rsrcs[i]);
146 fallback_rsrcs[i][0] = '*';
149 copy[i] = malloc( strlen(name) + strlen(fallback_rsrcs[i]) + 1 );
150 strcpy( copy[i], name );
151 strcat( copy[i], fallback_rsrcs[i] );
154 copy[i] = NULL;
155 return copy;
159 /* create the shell widgetry */
161 #if defined(__STDC__)
162 static void create_top_level_shell( int *argc, char **argv )
163 #else
164 static void create_top_level_shell( argc, argv )
165 int *argc;
166 char **argv;
167 #endif
169 char *ptr;
170 Widget main_w, editor;
171 Display *display;
173 /* X gets quite upset if the program name is not simple */
174 if (( ptr = strrchr( argv[0], '/' )) != NULL ) argv[0] = ++ptr;
175 vi_progname = argv[0];
177 /* create a top-level shell for the window manager */
178 top_level = XtVaAppInitialize( &ctx,
179 vi_progname,
180 NULL, 0, /* options */
181 (ArgcType) argc,
182 argv, /* might get modified */
183 get_fallback_rsrcs( argv[0] ),
184 NULL
186 display = XtDisplay(top_level);
188 /* might need to go technicolor... */
189 XutInstallColormap( argv[0], top_level );
191 /* create our icon
192 * do this *before* realizing the shell widget in case the -iconic
193 * option was specified.
196 #if defined(ColorIcon)
197 int nvi_width, nvi_height;
198 XpmAttributes attr;
200 attr.valuemask = 0;
201 XpmCreatePixmapFromData( display,
202 DefaultRootWindow(display),
203 nvi_xpm,
204 &icon_pm,
205 NULL,
206 &attr
208 nvi_width = attr.width;
209 nvi_height = attr.height;
210 #else
211 /* check the resource database for interesting resources */
212 __XutConvertResources( top_level,
213 vi_progname,
214 resource,
215 XtNumber(resource)
218 icon_pm = XCreatePixmapFromBitmapData(
219 display,
220 DefaultRootWindow(display),
221 (char *) nvi_bits,
222 nvi_width,
223 nvi_height,
224 icon_fg,
225 icon_bg,
226 DefaultDepth( display, DefaultScreen(display) )
228 #endif
229 XutSetIcon( top_level, nvi_height, nvi_width, icon_pm );
232 /* in the shell, we will stack a menubar an editor */
233 main_w = XtVaCreateManagedWidget( "main",
234 xmMainWindowWidgetClass,
235 top_level,
236 NULL
239 /* create the menubar */
240 XtManageChild( (Widget) vi_create_menubar( main_w ) );
242 /* add the VI widget from the library */
243 editor = vi_create_editor( "editor", main_w, onexit );
245 /* put it up */
246 XtRealizeWidget( top_level );
248 /* We *may* want all keyboard events to go to the editing screen.
249 * If the editor is the only widget in the shell that accepts
250 * keyboard input, then the user will expect that he can type when
251 * the pointer is over the scrollbar (for example). This call
252 * causes that to happen.
254 XtSetKeyboardFocus( top_level, XtNameToWidget( editor, "*screen" ) );
259 main(int argc, char **argv)
261 IPVI* ipvi;
263 * Initialize the X widgetry. We must do this before picking off
264 * arguments as well-behaved X programs have common argument lists
265 * (e.g. -rv for reverse video).
267 create_top_level_shell(&argc, argv);
269 /* We need to know if the child process goes away. */
270 (void)signal(SIGCHLD, onchld);
272 vi_create(&ipvi, 0);
273 (void)ipvi->run(ipvi, argc, argv);
274 ipvi->new_window(ipvi,&ipvi_motif,-1);
275 ipvi_motif->set_ops(ipvi_motif, &ipsi_ops_motif);
276 /* Run vi: the parent returns, the child is the vi process. */
277 vi_ifd = ipvi_motif->ifd;
278 vi_ofd = ipvi_motif->ofd;
279 pid = ipvi->pid;
281 /* Tell X that we are interested in input on the pipe. */
282 XtAppAddInput(ctx, vi_ifd,
283 (XtPointer)XtInputReadMask, vi_input_func, NULL);
285 /* Main loop. */
286 XtAppMainLoop(ctx);
288 /* NOTREACHED */
289 abort();
292 static void
293 XutSetIcon(Widget wid, int height, int width, Pixmap p)
295 Display *display = XtDisplay(wid);
296 Window win;
298 /* best bet is to set the icon window */
299 XtVaGetValues( wid, XtNiconWindow, &win, 0 );
301 if ( win == None ) {
302 win = XCreateSimpleWindow( display,
303 RootWindow( display,
304 DefaultScreen( display ) ),
305 0, 0,
306 width, height,
308 CopyFromParent,
309 CopyFromParent
313 if ( win != None ) {
314 XtVaSetValues( wid, XtNiconWindow, win, 0 );
315 XSetWindowBackgroundPixmap( display, win, p );
318 else {
319 /* do it the old fashioned way */
320 XtVaSetValues( wid, XtNiconPixmap, p, 0 );
324 /* Support for multiple colormaps
326 * XutInstallColormap( String name, Widget wid )
327 * The first time called, this routine checks to see if the
328 * resource "name*installColormap" is "True". If so, the
329 * widget is assigned a newly allocated colormap.
331 * Subsequent calls ignore the "name" parameter and use the
332 * same colormap.
334 * Future versions of this routine may handle multiple colormaps
335 * by name.
337 static enum { cmap_look, cmap_use, cmap_ignore } cmap_state = cmap_look;
339 static Boolean use_colormap = False;
341 static XutResource colormap_resources[] = {
342 { "installColormap", XutRKboolean, &use_colormap }
345 static void
346 XutInstallColormap(String name, Widget wid)
348 static Colormap cmap = 0;
349 static Display *cmap_display = 0;
350 Display *display = XtDisplay(wid);
352 /* what is the current finite state? */
353 if ( cmap_state == cmap_look ) {
355 /* what does the resource say? */
356 __XutConvertResources( wid,
357 name,
358 colormap_resources,
359 XtNumber(colormap_resources)
362 /* was the result "True"? */
363 if ( ! use_colormap ) {
364 cmap_state = cmap_ignore;
365 return;
368 /* yes it was */
369 cmap_state = cmap_use;
370 cmap_display = display;
371 cmap = XCopyColormapAndFree( display,
372 DefaultColormap( display,
373 DefaultScreen( display )
378 /* use the private colormap? */
379 if ( cmap_state == cmap_use ) {
380 XtVaSetValues( wid, XtNcolormap, cmap, 0 );
385 * onchld --
386 * Handle SIGCHLD.
388 static void
389 onchld(int signo)
391 /* If the vi process goes away, we exit as well. */
392 if (kill(pid, 0))
393 vi_fatal_message(top_level, "The vi process died. Exiting.");
397 * onexit --
398 * Function called when the editor "quits".
400 static void
401 onexit(void)
403 exit (0);