Expand PMF_FN_* macros.
[netbsd-mini2440.git] / dist / nvi / motif_l / m_ruler.c
blob9aa81e6957fcfe62637a05ca88f3022e1f092960
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_ruler.c,v 8.6 2003/11/05 17:10:00 skimo Exp (Berkeley) Date: 2003/11/05 17:10:00";
16 #endif /* not lint */
18 /* This module implements a dialog for the text ruler
20 * Interface:
21 * void __vi_show_text_ruler_dialog( Widget parent, String title )
22 * Pops up a text ruler dialog.
23 * We allow one per session. It is not modal.
25 * void __vi_clear_text_ruler_dialog( Widget parent, String title )
26 * Pops down the text ruler dialog.
28 * void __vi_set_text_ruler( int row, int col )
29 * Changes the displayed position
32 #include <sys/types.h>
33 #include <sys/queue.h>
35 #include <X11/X.h>
36 #include <X11/Intrinsic.h>
37 #include <X11/Shell.h>
38 #include <Xm/DrawingA.h>
39 #include <Xm/RowColumn.h>
40 #include <Xm/PushBG.h>
42 #include <bitstring.h>
43 #include <stdio.h>
45 #undef LOCK_SUCCESS
46 #include "../common/common.h"
47 #include "../ipc/ip.h"
48 #include "m_motif.h"
49 #include "vi_mextern.h"
52 /* globals */
54 static Widget db_ruler = NULL;
56 static Boolean active = False;
58 static int ruler_border = 5,
59 ruler_asc;
61 static GC gc_ruler;
63 static XFontStruct *ruler_font;
65 static char text[256];
67 #if ! defined(SelfTest)
68 static XutResource resource[] = {
69 { "rulerFont", XutRKfont, &ruler_font },
70 { "rulerBorder", XutRKinteger, &ruler_border },
72 #endif
75 /* change the displayed position */
77 static void
78 set_ruler_text(int row, int col, int *h, int *w, int *asc)
80 int dir, des;
81 XCharStruct over;
83 /* format the data */
84 sprintf( text, "%9.d,%-9.d", row+1, col+1 );
86 /* how big will it be? */
87 XTextExtents( ruler_font, text, strlen(text), &dir, asc, &des, &over );
89 /* how big a window will we need? */
90 *h = 2*ruler_border + over.ascent + over.descent;
91 *w = 2*ruler_border + over.width;
95 static void
96 redraw_text(void)
98 XClearArea( XtDisplay(db_ruler), XtWindow(db_ruler), 0, 0, 0, 0, False );
99 XDrawString( XtDisplay(db_ruler),
100 XtWindow(db_ruler),
101 gc_ruler,
102 ruler_border, ruler_border + ruler_asc,
103 text,
104 strlen(text)
110 * PUBLIC: void __vi_set_text_ruler __P((int, int));
112 void
113 __vi_set_text_ruler(int row, int col)
115 int h, w;
117 if ( ! active ) return;
119 set_ruler_text( row, col, &h, &w, &ruler_asc );
121 redraw_text();
125 /* callbacks */
127 static void
128 cancel_cb(void)
130 #if defined(SelfTest)
131 puts( "cancelled" );
132 #endif
133 active = False;
137 static void destroyed(void)
139 #if defined(SelfTest)
140 puts( "destroyed" );
141 #endif
143 /* some window managers destroy us upon popdown */
144 db_ruler = NULL;
145 active = False;
150 /* Draw and display a dialog the describes nvi options */
152 #if defined(__STDC__)
153 static Widget create_text_ruler_dialog( Widget parent, String title )
154 #else
155 static Widget create_text_ruler_dialog( parent, title )
156 Widget parent;
157 String title;
158 #endif
160 Widget box;
161 int h, w, asc;
162 Pixel fg, bg;
164 /* already built? */
165 if ( db_ruler != NULL ) return db_ruler;
167 #if defined(SelfTest)
168 ruler_font = XLoadQueryFont( XtDisplay(parent), "9x15" );
169 #else
170 /* check the resource database for interesting resources */
171 __XutConvertResources( parent,
172 vi_progname,
173 resource,
174 XtNumber(resource)
176 #endif
178 gc_ruler = XCreateGC( XtDisplay(parent), XtWindow(parent), 0, NULL );
179 XSetFont( XtDisplay(parent), gc_ruler, ruler_font->fid );
181 box = XtVaCreatePopupShell( title,
182 transientShellWidgetClass,
183 parent,
184 XmNallowShellResize, False,
187 XtAddCallback( box, XmNpopdownCallback, cancel_cb, 0 );
188 XtAddCallback( box, XmNdestroyCallback, destroyed, 0 );
190 /* should be ok to use the font now */
191 active = True;
193 /* how big a window? */
194 set_ruler_text( 0, 0, &h, &w, &asc );
196 /* keep this global, we might destroy it later */
197 db_ruler = XtVaCreateManagedWidget( "Ruler",
198 xmDrawingAreaWidgetClass,
199 box,
200 XmNheight, h,
201 XmNwidth, w,
204 /* this callback is for when the drawing area is exposed */
205 XtAddCallback( db_ruler,
206 XmNexposeCallback,
207 redraw_text,
211 /* what colors are selected for the drawing area? */
212 XtVaGetValues( db_ruler,
213 XmNbackground, &bg,
214 XmNforeground, &fg,
217 XSetForeground( XtDisplay(db_ruler), gc_ruler, fg );
218 XSetBackground( XtDisplay(db_ruler), gc_ruler, bg );
220 /* done */
221 return db_ruler;
226 /* module entry point
227 * __vi_show_text_ruler_dialog( parent, title )
228 * __vi_clear_text_ruler_dialog( parent, title )
231 #if defined(__STDC__)
232 void __vi_show_text_ruler_dialog( Widget parent, String title )
233 #else
234 void __vi_show_text_ruler_dialog( parent, title )
235 Widget parent;
236 String title;
237 #endif
239 Widget db = create_text_ruler_dialog( parent, title ),
240 shell = XtParent(db);
241 Dimension height, width;
243 /* this guy does not resize */
244 XtVaGetValues( db,
245 XmNheight, &height,
246 XmNwidth, &width,
249 XtVaSetValues( shell,
250 XmNmaxWidth, width,
251 XmNminWidth, width,
252 XmNmaxHeight, height,
253 XmNminHeight, height,
257 XtManageChild( db );
259 /* leave this guy up */
260 XtPopup( shell, XtGrabNone );
262 active = True;
264 /* ask vi core for the current r,c now */
265 #if ! defined(SelfTest)
266 __vi_set_text_ruler( __vi_screen->cury, __vi_screen->curx );
267 #else
268 __vi_set_text_ruler( rand(), rand() );
269 #endif
273 #if defined(__STDC__)
274 void __vi_clear_text_ruler_dialog()
275 #else
276 void __vi_clear_text_ruler_dialog(void)
277 #endif
279 if ( active )
280 XtPopdown( XtParent(db_ruler) );
284 #if defined(SelfTest)
286 #if XtSpecificationRelease == 4
287 #define ArgcType Cardinal *
288 #else
289 #define ArgcType int *
290 #endif
292 static void change_pos( Widget w )
294 __vi_set_text_ruler( rand(), rand() );
297 #if defined(__STDC__)
298 static void show_text_ruler( Widget w, XtPointer data, XtPointer cbs )
299 #else
300 static void show_text_ruler( w, data, cbs )
301 Widget w;
302 XtPointer data;
303 XtPointer cbs;
304 #endif
306 __vi_show_text_ruler_dialog( data, "Ruler" );
309 main( int argc, char *argv[] )
311 XtAppContext ctx;
312 Widget top_level, rc, button;
313 extern exit();
315 /* create a top-level shell for the window manager */
316 top_level = XtVaAppInitialize( &ctx,
317 argv[0],
318 NULL, 0, /* options */
319 (ArgcType) &argc,
320 argv, /* might get modified */
321 NULL,
322 NULL
325 rc = XtVaCreateManagedWidget( "rc",
326 xmRowColumnWidgetClass,
327 top_level,
331 button = XtVaCreateManagedWidget( "Pop up text ruler dialog",
332 xmPushButtonGadgetClass,
336 XtAddCallback( button, XmNactivateCallback, show_text_ruler, rc );
338 button = XtVaCreateManagedWidget( "Change Position",
339 xmPushButtonGadgetClass,
343 XtAddCallback( button, XmNactivateCallback, change_pos, rc );
345 button = XtVaCreateManagedWidget( "Quit",
346 xmPushButtonGadgetClass,
350 XtAddCallback( button, XmNactivateCallback, exit, 0 );
352 XtRealizeWidget(top_level);
353 XtAppMainLoop(ctx);
355 #endif