only clean out gs when last window goes
[nvi.git] / motif_l / m_ruler.c
blobfb0b0b89591a4709d64780211c598bcefb80e18a
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_ruler.c,v 8.4 1996/12/18 10:26:44 bostic Exp $ (Berkeley) $Date: 1996/12/18 10:26:44 $";
14 #endif /* not lint */
16 /* This module implements a dialog for the text ruler
18 * Interface:
19 * void __vi_show_text_ruler_dialog( Widget parent, String title )
20 * Pops up a text ruler dialog.
21 * We allow one per session. It is not modal.
23 * void __vi_clear_text_ruler_dialog( Widget parent, String title )
24 * Pops down the text ruler dialog.
26 * void __vi_set_text_ruler( int row, int col )
27 * Changes the displayed position
30 #include <sys/types.h>
31 #include <sys/queue.h>
33 #include <X11/X.h>
34 #include <X11/Intrinsic.h>
35 #include <X11/Shell.h>
36 #include <Xm/DrawingA.h>
37 #include <Xm/RowColumn.h>
38 #include <Xm/PushBG.h>
40 #include <bitstring.h>
41 #include <stdio.h>
43 #include "../common/common.h"
44 #include "../ipc/ip.h"
45 #include "m_motif.h"
46 #include "vi_mextern.h"
49 /* globals */
51 static Widget db_ruler = NULL;
53 static Boolean active = False;
55 static int ruler_border = 5,
56 ruler_asc;
58 static GC gc_ruler;
60 static XFontStruct *ruler_font;
62 static char text[256];
64 #if ! defined(SelfTest)
65 static XutResource resource[] = {
66 { "rulerFont", XutRKfont, &ruler_font },
67 { "rulerBorder", XutRKinteger, &ruler_border },
69 #endif
72 /* change the displayed position */
74 static void
75 set_ruler_text( row, col, h, w, asc )
76 int row, col, *h, *w, *asc;
78 int dir, des;
79 XCharStruct over;
81 /* format the data */
82 sprintf( text, "%9.d,%-9.d", row+1, col+1 );
84 /* how big will it be? */
85 XTextExtents( ruler_font, text, strlen(text), &dir, asc, &des, &over );
87 /* how big a window will we need? */
88 *h = 2*ruler_border + over.ascent + over.descent;
89 *w = 2*ruler_border + over.width;
93 static void
94 redraw_text()
96 XClearArea( XtDisplay(db_ruler), XtWindow(db_ruler), 0, 0, 0, 0, False );
97 XDrawString( XtDisplay(db_ruler),
98 XtWindow(db_ruler),
99 gc_ruler,
100 ruler_border, ruler_border + ruler_asc,
101 text,
102 strlen(text)
108 * PUBLIC: void __vi_set_text_ruler __P((int, int));
110 void
111 __vi_set_text_ruler( row, col )
112 int row, col;
114 int h, w;
116 if ( ! active ) return;
118 set_ruler_text( row, col, &h, &w, &ruler_asc );
120 redraw_text();
124 /* callbacks */
126 static void
127 cancel_cb()
129 #if defined(SelfTest)
130 puts( "cancelled" );
131 #endif
132 active = False;
136 static void destroyed()
138 #if defined(SelfTest)
139 puts( "destroyed" );
140 #endif
142 /* some window managers destroy us upon popdown */
143 db_ruler = NULL;
144 active = False;
149 /* Draw and display a dialog the describes nvi options */
151 #if defined(__STDC__)
152 static Widget create_text_ruler_dialog( Widget parent, String title )
153 #else
154 static Widget create_text_ruler_dialog( parent, title )
155 Widget parent;
156 String title;
157 #endif
159 Widget box;
160 int h, w, asc;
161 Pixel fg, bg;
163 /* already built? */
164 if ( db_ruler != NULL ) return db_ruler;
166 #if defined(SelfTest)
167 ruler_font = XLoadQueryFont( XtDisplay(parent), "9x15" );
168 #else
169 /* check the resource database for interesting resources */
170 __XutConvertResources( parent,
171 vi_progname,
172 resource,
173 XtNumber(resource)
175 #endif
177 gc_ruler = XCreateGC( XtDisplay(parent), XtWindow(parent), 0, NULL );
178 XSetFont( XtDisplay(parent), gc_ruler, ruler_font->fid );
180 box = XtVaCreatePopupShell( title,
181 transientShellWidgetClass,
182 parent,
183 XmNallowShellResize, False,
186 XtAddCallback( box, XmNpopdownCallback, cancel_cb, 0 );
187 XtAddCallback( box, XmNdestroyCallback, destroyed, 0 );
189 /* should be ok to use the font now */
190 active = True;
192 /* how big a window? */
193 set_ruler_text( 0, 0, &h, &w, &asc );
195 /* keep this global, we might destroy it later */
196 db_ruler = XtVaCreateManagedWidget( "Ruler",
197 xmDrawingAreaWidgetClass,
198 box,
199 XmNheight, h,
200 XmNwidth, w,
203 /* this callback is for when the drawing area is exposed */
204 XtAddCallback( db_ruler,
205 XmNexposeCallback,
206 redraw_text,
210 /* what colors are selected for the drawing area? */
211 XtVaGetValues( db_ruler,
212 XmNbackground, &bg,
213 XmNforeground, &fg,
216 XSetForeground( XtDisplay(db_ruler), gc_ruler, fg );
217 XSetBackground( XtDisplay(db_ruler), gc_ruler, bg );
219 /* done */
220 return db_ruler;
225 /* module entry point
226 * __vi_show_text_ruler_dialog( parent, title )
227 * __vi_clear_text_ruler_dialog( parent, title )
230 #if defined(__STDC__)
231 void __vi_show_text_ruler_dialog( Widget parent, String title )
232 #else
233 void __vi_show_text_ruler_dialog( parent, title )
234 Widget parent;
235 String title;
236 #endif
238 Widget db = create_text_ruler_dialog( parent, title ),
239 shell = XtParent(db);
240 Dimension height, width;
242 /* this guy does not resize */
243 XtVaGetValues( db,
244 XmNheight, &height,
245 XmNwidth, &width,
248 XtVaSetValues( shell,
249 XmNmaxWidth, width,
250 XmNminWidth, width,
251 XmNmaxHeight, height,
252 XmNminHeight, height,
256 XtManageChild( db );
258 /* leave this guy up */
259 XtPopup( shell, XtGrabNone );
261 active = True;
263 /* ask vi core for the current r,c now */
264 #if ! defined(SelfTest)
265 __vi_set_text_ruler( __vi_screen->cury, __vi_screen->curx );
266 #else
267 __vi_set_text_ruler( rand(), rand() );
268 #endif
272 #if defined(__STDC__)
273 void __vi_clear_text_ruler_dialog()
274 #else
275 void __vi_clear_text_ruler_dialog()
276 #endif
278 if ( active )
279 XtPopdown( XtParent(db_ruler) );
283 #if defined(SelfTest)
285 #if XtSpecificationRelease == 4
286 #define ArgcType Cardinal *
287 #else
288 #define ArgcType int *
289 #endif
291 static void change_pos( Widget w )
293 __vi_set_text_ruler( rand(), rand() );
296 #if defined(__STDC__)
297 static void show_text_ruler( Widget w, XtPointer data, XtPointer cbs )
298 #else
299 static void show_text_ruler( w, data, cbs )
300 Widget w;
301 XtPointer data;
302 XtPointer cbs;
303 #endif
305 __vi_show_text_ruler_dialog( data, "Ruler" );
308 main( int argc, char *argv[] )
310 XtAppContext ctx;
311 Widget top_level, rc, button;
312 extern exit();
314 /* create a top-level shell for the window manager */
315 top_level = XtVaAppInitialize( &ctx,
316 argv[0],
317 NULL, 0, /* options */
318 (ArgcType) &argc,
319 argv, /* might get modified */
320 NULL,
321 NULL
324 rc = XtVaCreateManagedWidget( "rc",
325 xmRowColumnWidgetClass,
326 top_level,
330 button = XtVaCreateManagedWidget( "Pop up text ruler dialog",
331 xmPushButtonGadgetClass,
335 XtAddCallback( button, XmNactivateCallback, show_text_ruler, rc );
337 button = XtVaCreateManagedWidget( "Change Position",
338 xmPushButtonGadgetClass,
342 XtAddCallback( button, XmNactivateCallback, change_pos, rc );
344 button = XtVaCreateManagedWidget( "Quit",
345 xmPushButtonGadgetClass,
349 XtAddCallback( button, XmNactivateCallback, exit, 0 );
351 XtRealizeWidget(top_level);
352 XtAppMainLoop(ctx);
354 #endif