vi/v_txt.c: txt_dent: introduce sequence point between decrement and access
[nvi.git] / motif_l / m_ruler.c
blob4f005900911f267a14d7f09b558b35d16183b14d
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.6 2003/11/05 17:10:00 skimo Exp $ (Berkeley) $Date: 2003/11/05 17:10:00 $";
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 #undef LOCK_SUCCESS
44 #include "../common/common.h"
45 #include "../ipc/ip.h"
46 #include "m_motif.h"
47 #include "vi_mextern.h"
50 /* globals */
52 static Widget db_ruler = NULL;
54 static Boolean active = False;
56 static int ruler_border = 5,
57 ruler_asc;
59 static GC gc_ruler;
61 static XFontStruct *ruler_font;
63 static char text[256];
65 #if ! defined(SelfTest)
66 static XutResource resource[] = {
67 { "rulerFont", XutRKfont, &ruler_font },
68 { "rulerBorder", XutRKinteger, &ruler_border },
70 #endif
73 /* change the displayed position */
75 static void
76 set_ruler_text(int row, int col, int *h, int *w, int *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(void)
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(int row, int col)
113 int h, w;
115 if ( ! active ) return;
117 set_ruler_text( row, col, &h, &w, &ruler_asc );
119 redraw_text();
123 /* callbacks */
125 static void
126 cancel_cb(void)
128 #if defined(SelfTest)
129 puts( "cancelled" );
130 #endif
131 active = False;
135 static void destroyed(void)
137 #if defined(SelfTest)
138 puts( "destroyed" );
139 #endif
141 /* some window managers destroy us upon popdown */
142 db_ruler = NULL;
143 active = False;
148 /* Draw and display a dialog the describes nvi options */
150 #if defined(__STDC__)
151 static Widget create_text_ruler_dialog( Widget parent, String title )
152 #else
153 static Widget create_text_ruler_dialog( parent, title )
154 Widget parent;
155 String title;
156 #endif
158 Widget box;
159 int h, w, asc;
160 Pixel fg, bg;
162 /* already built? */
163 if ( db_ruler != NULL ) return db_ruler;
165 #if defined(SelfTest)
166 ruler_font = XLoadQueryFont( XtDisplay(parent), "9x15" );
167 #else
168 /* check the resource database for interesting resources */
169 __XutConvertResources( parent,
170 vi_progname,
171 resource,
172 XtNumber(resource)
174 #endif
176 gc_ruler = XCreateGC( XtDisplay(parent), XtWindow(parent), 0, NULL );
177 XSetFont( XtDisplay(parent), gc_ruler, ruler_font->fid );
179 box = XtVaCreatePopupShell( title,
180 transientShellWidgetClass,
181 parent,
182 XmNallowShellResize, False,
185 XtAddCallback( box, XmNpopdownCallback, cancel_cb, 0 );
186 XtAddCallback( box, XmNdestroyCallback, destroyed, 0 );
188 /* should be ok to use the font now */
189 active = True;
191 /* how big a window? */
192 set_ruler_text( 0, 0, &h, &w, &asc );
194 /* keep this global, we might destroy it later */
195 db_ruler = XtVaCreateManagedWidget( "Ruler",
196 xmDrawingAreaWidgetClass,
197 box,
198 XmNheight, h,
199 XmNwidth, w,
202 /* this callback is for when the drawing area is exposed */
203 XtAddCallback( db_ruler,
204 XmNexposeCallback,
205 redraw_text,
209 /* what colors are selected for the drawing area? */
210 XtVaGetValues( db_ruler,
211 XmNbackground, &bg,
212 XmNforeground, &fg,
215 XSetForeground( XtDisplay(db_ruler), gc_ruler, fg );
216 XSetBackground( XtDisplay(db_ruler), gc_ruler, bg );
218 /* done */
219 return db_ruler;
224 /* module entry point
225 * __vi_show_text_ruler_dialog( parent, title )
226 * __vi_clear_text_ruler_dialog( parent, title )
229 #if defined(__STDC__)
230 void __vi_show_text_ruler_dialog( Widget parent, String title )
231 #else
232 void __vi_show_text_ruler_dialog( parent, title )
233 Widget parent;
234 String title;
235 #endif
237 Widget db = create_text_ruler_dialog( parent, title ),
238 shell = XtParent(db);
239 Dimension height, width;
241 /* this guy does not resize */
242 XtVaGetValues( db,
243 XmNheight, &height,
244 XmNwidth, &width,
247 XtVaSetValues( shell,
248 XmNmaxWidth, width,
249 XmNminWidth, width,
250 XmNmaxHeight, height,
251 XmNminHeight, height,
255 XtManageChild( db );
257 /* leave this guy up */
258 XtPopup( shell, XtGrabNone );
260 active = True;
262 /* ask vi core for the current r,c now */
263 #if ! defined(SelfTest)
264 __vi_set_text_ruler( __vi_screen->cury, __vi_screen->curx );
265 #else
266 __vi_set_text_ruler( rand(), rand() );
267 #endif
271 #if defined(__STDC__)
272 void __vi_clear_text_ruler_dialog()
273 #else
274 void __vi_clear_text_ruler_dialog(void)
275 #endif
277 if ( active )
278 XtPopdown( XtParent(db_ruler) );
282 #if defined(SelfTest)
284 #if XtSpecificationRelease == 4
285 #define ArgcType Cardinal *
286 #else
287 #define ArgcType int *
288 #endif
290 static void change_pos( Widget w )
292 __vi_set_text_ruler( rand(), rand() );
295 #if defined(__STDC__)
296 static void show_text_ruler( Widget w, XtPointer data, XtPointer cbs )
297 #else
298 static void show_text_ruler( w, data, cbs )
299 Widget w;
300 XtPointer data;
301 XtPointer cbs;
302 #endif
304 __vi_show_text_ruler_dialog( data, "Ruler" );
307 main( int argc, char *argv[] )
309 XtAppContext ctx;
310 Widget top_level, rc, button;
311 extern exit();
313 /* create a top-level shell for the window manager */
314 top_level = XtVaAppInitialize( &ctx,
315 argv[0],
316 NULL, 0, /* options */
317 (ArgcType) &argc,
318 argv, /* might get modified */
319 NULL,
320 NULL
323 rc = XtVaCreateManagedWidget( "rc",
324 xmRowColumnWidgetClass,
325 top_level,
329 button = XtVaCreateManagedWidget( "Pop up text ruler dialog",
330 xmPushButtonGadgetClass,
334 XtAddCallback( button, XmNactivateCallback, show_text_ruler, rc );
336 button = XtVaCreateManagedWidget( "Change Position",
337 xmPushButtonGadgetClass,
341 XtAddCallback( button, XmNactivateCallback, change_pos, rc );
343 button = XtVaCreateManagedWidget( "Quit",
344 xmPushButtonGadgetClass,
348 XtAddCallback( button, XmNactivateCallback, exit, 0 );
350 XtRealizeWidget(top_level);
351 XtAppMainLoop(ctx);
353 #endif