add vi_send(), vi_translate() to list of exported functions
[nvi.git] / motif / m_main.c
blob3c82cdbe9c411187580b9c2cb4e7586006c6c554
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.33 1996/12/18 10:27:26 bostic Exp $ (Berkeley) $Date: 1996/12/18 10:27:26 $";
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 #include <X11/xpm.h>
43 #include "nvi.xpm" /* Icon pixmap. */
44 #else
45 #include "nvi.xbm" /* Icon bitmap. */
46 #endif
48 static pid_t pid;
49 static Pixel icon_fg,
50 icon_bg;
51 static Pixmap icon_pm;
52 static Widget top_level;
53 static XtAppContext ctx;
55 static void XutInstallColormap __P((String, Widget));
56 static void XutSetIcon __P((Widget, int, int, Pixmap));
57 static void onchld __P((int));
58 static void onexit __P((void));
60 #if ! defined(ColorIcon)
61 static XutResource resource[] = {
62 { "iconForeground", XutRKpixel, &icon_fg },
63 { "iconBackground", XutRKpixel, &icon_bg },
65 #endif
68 /* resources for the vi widgets unless the user overrides them */
69 String fallback_rsrcs[] = {
71 "*font: -*-*-*-r-*--14-*-*-*-m-*-*-*",
72 "*text*fontList: -*-*-*-r-*--14-*-*-*-m-*-*-*",
73 "*Menu*fontList: -*-helvetica-bold-r-normal--14-*-*-*-*-*-*-*",
74 "*fontList: -*-helvetica-medium-r-normal--14-*-*-*-*-*-*-*",
75 "*pointerShape: xterm",
76 "*busyShape: watch",
77 "*iconName: vi",
79 #if ! defined(ColorIcon)
80 /* coloring for the icons */
81 "*iconForeground: XtDefaultForeground",
82 "*iconBackground: XtDefaultBackground",
83 #endif
85 /* layout for the tag stack dialog */
86 "*Tags*visibleItemCount: 5",
88 /* for the text ruler */
89 "*rulerFont: -*-helvetica-medium-r-normal--14-*-*-*-*-*-*-*",
90 "*rulerBorder: 5",
92 /* layout for the new, temporary preferences page */
93 "*toggleOptions.numColumns: 6", /* also used by Find */
94 "*Preferences*tabWidthPercentage: 0",
95 "*Preferences*tabs.shadowThickness: 2",
96 "*Preferences*tabs.font: -*-helvetica-bold-r-normal--14-*-*-*-*-*-*-*",
98 /* --------------------------------------------------------------------- *
99 * anything below this point is only defined when we are not running CDE *
100 * --------------------------------------------------------------------- */
102 /* Do not define default colors when running under CDE
103 * (e.g. VUE on HPUX). The result is that you don't look
104 * like a normal desktop application
106 "?background: gray75",
107 "?screen.background: wheat",
108 "?highlightColor: red",
109 "?Preferences*options.background: gray90",
112 #if defined(__STDC__)
113 static String *get_fallback_rsrcs( String name )
114 #else
115 static String *get_fallback_rsrcs( name )
116 String name;
117 #endif
119 String *copy = (String *) malloc( (1+XtNumber(fallback_rsrcs))*sizeof(String) );
120 int i, running_cde;
121 Display *d;
123 /* connect to server and see if the CDE atoms are present */
124 d = XOpenDisplay(0);
125 running_cde = is_cde( d );
126 XCloseDisplay(d);
128 for ( i=0; i<XtNumber(fallback_rsrcs); i++ ) {
130 /* stop here if running CDE */
131 if ( fallback_rsrcs[i][0] == '?' ) {
132 if ( running_cde ) break;
133 fallback_rsrcs[i] = strdup(fallback_rsrcs[i]);
134 fallback_rsrcs[i][0] = '*';
137 copy[i] = malloc( strlen(name) + strlen(fallback_rsrcs[i]) + 1 );
138 strcpy( copy[i], name );
139 strcat( copy[i], fallback_rsrcs[i] );
142 copy[i] = NULL;
143 return copy;
147 /* create the shell widgetry */
149 #if defined(__STDC__)
150 static void create_top_level_shell( int *argc, char **argv )
151 #else
152 static void create_top_level_shell( argc, argv )
153 int *argc;
154 char **argv;
155 #endif
157 char *ptr;
158 Widget main_w, editor;
159 Display *display;
161 /* X gets quite upset if the program name is not simple */
162 if (( ptr = strrchr( argv[0], '/' )) != NULL ) argv[0] = ++ptr;
163 vi_progname = argv[0];
165 /* create a top-level shell for the window manager */
166 top_level = XtVaAppInitialize( &ctx,
167 vi_progname,
168 NULL, 0, /* options */
169 (ArgcType) argc,
170 argv, /* might get modified */
171 get_fallback_rsrcs( argv[0] ),
172 NULL
174 display = XtDisplay(top_level);
176 /* might need to go technicolor... */
177 XutInstallColormap( argv[0], top_level );
179 /* create our icon
180 * do this *before* realizing the shell widget in case the -iconic
181 * option was specified.
184 #if defined(ColorIcon)
185 int nvi_width, nvi_height;
186 XpmAttributes attr;
188 attr.valuemask = 0;
189 XpmCreatePixmapFromData( display,
190 DefaultRootWindow(display),
191 nvi_xpm,
192 &icon_pm,
193 NULL,
194 &attr
196 nvi_width = attr.width;
197 nvi_height = attr.height;
198 #else
199 /* check the resource database for interesting resources */
200 __XutConvertResources( top_level,
201 vi_progname,
202 resource,
203 XtNumber(resource)
206 icon_pm = XCreatePixmapFromBitmapData(
207 display,
208 DefaultRootWindow(display),
209 (char *) nvi_bits,
210 nvi_width,
211 nvi_height,
212 icon_fg,
213 icon_bg,
214 DefaultDepth( display, DefaultScreen(display) )
216 #endif
217 XutSetIcon( top_level, nvi_height, nvi_width, icon_pm );
220 /* in the shell, we will stack a menubar an editor */
221 main_w = XtVaCreateManagedWidget( "main",
222 xmMainWindowWidgetClass,
223 top_level,
224 NULL
227 /* create the menubar */
228 XtManageChild( (Widget) vi_create_menubar( main_w ) );
230 /* add the VI widget from the library */
231 editor = vi_create_editor( "editor", main_w, onexit );
233 /* put it up */
234 XtRealizeWidget( top_level );
236 /* We *may* want all keyboard events to go to the editing screen.
237 * If the editor is the only widget in the shell that accepts
238 * keyboard input, then the user will expect that he can type when
239 * the pointer is over the scrollbar (for example). This call
240 * causes that to happen.
242 XtSetKeyboardFocus( top_level, XtNameToWidget( editor, "*screen" ) );
247 main(argc, argv)
248 int argc;
249 char *argv[];
251 int i_fd;
254 * Initialize the X widgetry. We must do this before picking off
255 * arguments as well-behaved X programs have common argument lists
256 * (e.g. -rv for reverse video).
258 create_top_level_shell(&argc, argv);
260 /* We need to know if the child process goes away. */
261 (void)signal(SIGCHLD, onchld);
263 /* Run vi: the parent returns, the child is the vi process. */
264 (void)vi_run(argc, argv, &i_fd, &vi_ofd, &pid);
266 /* Tell X that we are interested in input on the pipe. */
267 XtAppAddInput(ctx, i_fd,
268 (XtPointer)XtInputReadMask, vi_input_func, NULL);
270 /* Main loop. */
271 XtAppMainLoop(ctx);
273 /* NOTREACHED */
274 abort();
277 static void
278 XutSetIcon(wid, height, width, p)
279 Widget wid;
280 int height, width;
281 Pixmap p;
283 Display *display = XtDisplay(wid);
284 Window win;
286 /* best bet is to set the icon window */
287 XtVaGetValues( wid, XtNiconWindow, &win, 0 );
289 if ( win == None ) {
290 win = XCreateSimpleWindow( display,
291 RootWindow( display,
292 DefaultScreen( display ) ),
293 0, 0,
294 width, height,
296 CopyFromParent,
297 CopyFromParent
301 if ( win != None ) {
302 XtVaSetValues( wid, XtNiconWindow, win, 0 );
303 XSetWindowBackgroundPixmap( display, win, p );
306 else {
307 /* do it the old fashioned way */
308 XtVaSetValues( wid, XtNiconPixmap, p, 0 );
312 /* Support for multiple colormaps
314 * XutInstallColormap( String name, Widget wid )
315 * The first time called, this routine checks to see if the
316 * resource "name*installColormap" is "True". If so, the
317 * widget is assigned a newly allocated colormap.
319 * Subsequent calls ignore the "name" parameter and use the
320 * same colormap.
322 * Future versions of this routine may handle multiple colormaps
323 * by name.
325 static enum { cmap_look, cmap_use, cmap_ignore } cmap_state = cmap_look;
327 static Boolean use_colormap = False;
329 static XutResource colormap_resources[] = {
330 { "installColormap", XutRKboolean, &use_colormap }
333 static void
334 XutInstallColormap(name, wid)
335 String name;
336 Widget wid;
338 static Colormap cmap = 0;
339 static Display *cmap_display = 0;
340 Display *display = XtDisplay(wid);
342 /* what is the current finite state? */
343 if ( cmap_state == cmap_look ) {
345 /* what does the resource say? */
346 __XutConvertResources( wid,
347 name,
348 colormap_resources,
349 XtNumber(colormap_resources)
352 /* was the result "True"? */
353 if ( ! use_colormap ) {
354 cmap_state = cmap_ignore;
355 return;
358 /* yes it was */
359 cmap_state = cmap_use;
360 cmap_display = display;
361 cmap = XCopyColormapAndFree( display,
362 DefaultColormap( display,
363 DefaultScreen( display )
368 /* use the private colormap? */
369 if ( cmap_state == cmap_use ) {
370 XtVaSetValues( wid, XtNcolormap, cmap, 0 );
375 * onchld --
376 * Handle SIGCHLD.
378 static void
379 onchld(signo)
380 int signo;
382 /* If the vi process goes away, we exit as well. */
383 if (kill(pid, 0))
384 vi_fatal_message(top_level, "The vi process died. Exiting.");
388 * onexit --
389 * Function called when the editor "quits".
391 static void
392 onexit()
394 exit (0);