lint
[nvi.git] / motif_l / m_ruler.c
blob7d7b4b626fbd85cb45e29408d7ac945b331fe2d5
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.2 1996/12/16 17:24:03 bostic Exp $ (Berkeley) $Date: 1996/12/16 17:24:03 $";
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 "m_util.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)
107 void
108 __vi_set_text_ruler( row, col )
109 int row, col;
111 int h, w;
113 if ( ! active ) return;
115 set_ruler_text( row, col, &h, &w, &ruler_asc );
117 redraw_text();
121 /* callbacks */
123 static void
124 cancel_cb()
126 #if defined(SelfTest)
127 puts( "cancelled" );
128 #endif
129 active = False;
133 static void destroyed()
135 #if defined(SelfTest)
136 puts( "destroyed" );
137 #endif
139 /* some window managers destroy us upon popdown */
140 db_ruler = NULL;
141 active = False;
146 /* Draw and display a dialog the describes nvi options */
148 #if defined(__STDC__)
149 static Widget create_text_ruler_dialog( Widget parent, String title )
150 #else
151 static Widget create_text_ruler_dialog( parent, title )
152 Widget parent;
153 String title;
154 #endif
156 Widget box;
157 int h, w, asc;
158 Pixel fg, bg;
160 /* already built? */
161 if ( db_ruler != NULL ) return db_ruler;
163 #if defined(SelfTest)
164 ruler_font = XLoadQueryFont( XtDisplay(parent), "9x15" );
165 #else
166 /* check the resource database for interesting resources */
167 XutConvertResources( parent,
168 vi_progname,
169 resource,
170 XtNumber(resource)
172 #endif
174 gc_ruler = XCreateGC( XtDisplay(parent), XtWindow(parent), 0, NULL );
175 XSetFont( XtDisplay(parent), gc_ruler, ruler_font->fid );
177 box = XtVaCreatePopupShell( title,
178 transientShellWidgetClass,
179 parent,
180 XmNallowShellResize, False,
183 XtAddCallback( box, XmNpopdownCallback, cancel_cb, 0 );
184 XtAddCallback( box, XmNdestroyCallback, destroyed, 0 );
186 /* should be ok to use the font now */
187 active = True;
189 /* how big a window? */
190 set_ruler_text( 0, 0, &h, &w, &asc );
192 /* keep this global, we might destroy it later */
193 db_ruler = XtVaCreateManagedWidget( "Ruler",
194 xmDrawingAreaWidgetClass,
195 box,
196 XmNheight, h,
197 XmNwidth, w,
200 /* this callback is for when the drawing area is exposed */
201 XtAddCallback( db_ruler,
202 XmNexposeCallback,
203 redraw_text,
207 /* what colors are selected for the drawing area? */
208 XtVaGetValues( db_ruler,
209 XmNbackground, &bg,
210 XmNforeground, &fg,
213 XSetForeground( XtDisplay(db_ruler), gc_ruler, fg );
214 XSetBackground( XtDisplay(db_ruler), gc_ruler, bg );
216 /* done */
217 return db_ruler;
222 /* module entry point
223 * __vi_show_text_ruler_dialog( parent, title )
224 * __vi_clear_text_ruler_dialog( parent, title )
227 #if defined(__STDC__)
228 void __vi_show_text_ruler_dialog( Widget parent, String title )
229 #else
230 void __vi_show_text_ruler_dialog( parent, title )
231 Widget parent;
232 String title;
233 #endif
235 int row, col;
236 Widget db = create_text_ruler_dialog( parent, title ),
237 shell = XtParent(db);
238 Dimension height, width;
240 /* this guy does not resize */
241 XtVaGetValues( db,
242 XmNheight, &height,
243 XmNwidth, &width,
246 XtVaSetValues( shell,
247 XmNmaxWidth, width,
248 XmNminWidth, width,
249 XmNmaxHeight, height,
250 XmNminHeight, height,
254 XtManageChild( db );
256 /* leave this guy up */
257 XtPopup( shell, XtGrabNone );
259 active = True;
261 /* ask vi core for the current r,c now */
262 #if ! defined(SelfTest)
263 __vi_set_text_ruler( __vi_screen->cury, __vi_screen->curx );
264 #else
265 __vi_set_text_ruler( rand(), rand() );
266 #endif
270 #if defined(__STDC__)
271 void __vi_clear_text_ruler_dialog()
272 #else
273 void __vi_clear_text_ruler_dialog()
274 #endif
276 if ( active )
277 XtPopdown( XtParent(db_ruler) );
281 #if defined(SelfTest)
283 #if XtSpecificationRelease == 4
284 #define ArgcType Cardinal *
285 #else
286 #define ArgcType int *
287 #endif
289 static void change_pos( Widget w )
291 __vi_set_text_ruler( rand(), rand() );
294 #if defined(__STDC__)
295 static void show_text_ruler( Widget w, XtPointer data, XtPointer cbs )
296 #else
297 static void show_text_ruler( w, data, cbs )
298 Widget w;
299 XtPointer data;
300 XtPointer cbs;
301 #endif
303 __vi_show_text_ruler_dialog( data, "Ruler" );
306 main( int argc, char *argv[] )
308 XtAppContext ctx;
309 Widget top_level, rc, button;
310 extern exit();
312 /* create a top-level shell for the window manager */
313 top_level = XtVaAppInitialize( &ctx,
314 argv[0],
315 NULL, 0, /* options */
316 (ArgcType) &argc,
317 argv, /* might get modified */
318 NULL,
319 NULL
322 rc = XtVaCreateManagedWidget( "rc",
323 xmRowColumnWidgetClass,
324 top_level,
328 button = XtVaCreateManagedWidget( "Pop up text ruler dialog",
329 xmPushButtonGadgetClass,
333 XtAddCallback( button, XmNactivateCallback, show_text_ruler, rc );
335 button = XtVaCreateManagedWidget( "Change Position",
336 xmPushButtonGadgetClass,
340 XtAddCallback( button, XmNactivateCallback, change_pos, rc );
342 button = XtVaCreateManagedWidget( "Quit",
343 xmPushButtonGadgetClass,
347 XtAddCallback( button, XmNactivateCallback, exit, 0 );
349 XtRealizeWidget(top_level);
350 XtAppMainLoop(ctx);
352 #endif