xref: /minix3/external/bsd/nvi/dist/motif_l/m_ruler.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
184d9c625SLionel Sambuc /*-
284d9c625SLionel Sambuc  * Copyright (c) 1996
384d9c625SLionel Sambuc  *	Rob Zimmermann.  All rights reserved.
484d9c625SLionel Sambuc  * Copyright (c) 1996
584d9c625SLionel Sambuc  *	Keith Bostic.  All rights reserved.
684d9c625SLionel Sambuc  *
784d9c625SLionel Sambuc  * See the LICENSE file for redistribution information.
884d9c625SLionel Sambuc  */
984d9c625SLionel Sambuc 
1084d9c625SLionel Sambuc #include "config.h"
1184d9c625SLionel Sambuc 
12*0a6a1f1dSLionel Sambuc #include <sys/cdefs.h>
13*0a6a1f1dSLionel Sambuc #if 0
1484d9c625SLionel Sambuc #ifndef lint
1584d9c625SLionel Sambuc 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 ";
1684d9c625SLionel Sambuc #endif /* not lint */
17*0a6a1f1dSLionel Sambuc #else
18*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: m_ruler.c,v 1.3 2014/01/26 21:43:45 christos Exp $");
19*0a6a1f1dSLionel Sambuc #endif
2084d9c625SLionel Sambuc 
2184d9c625SLionel Sambuc /* This module implements a dialog for the text ruler
2284d9c625SLionel Sambuc  *
2384d9c625SLionel Sambuc  * Interface:
2484d9c625SLionel Sambuc  * void	__vi_show_text_ruler_dialog( Widget parent, String title )
2584d9c625SLionel Sambuc  *	Pops up a text ruler dialog.
2684d9c625SLionel Sambuc  *	We allow one per session.  It is not modal.
2784d9c625SLionel Sambuc  *
2884d9c625SLionel Sambuc  * void	__vi_clear_text_ruler_dialog( Widget parent, String title )
2984d9c625SLionel Sambuc  *	Pops down the text ruler dialog.
3084d9c625SLionel Sambuc  *
3184d9c625SLionel Sambuc  * void	__vi_set_text_ruler( int row, int col )
3284d9c625SLionel Sambuc  *	Changes the displayed position
3384d9c625SLionel Sambuc  */
3484d9c625SLionel Sambuc 
3584d9c625SLionel Sambuc #include <sys/types.h>
3684d9c625SLionel Sambuc #include <sys/queue.h>
3784d9c625SLionel Sambuc 
3884d9c625SLionel Sambuc #include <X11/X.h>
3984d9c625SLionel Sambuc #include <X11/Intrinsic.h>
4084d9c625SLionel Sambuc #include <X11/Shell.h>
4184d9c625SLionel Sambuc #include <Xm/DrawingA.h>
4284d9c625SLionel Sambuc #include <Xm/RowColumn.h>
4384d9c625SLionel Sambuc #include <Xm/PushBG.h>
4484d9c625SLionel Sambuc 
4584d9c625SLionel Sambuc #include <bitstring.h>
4684d9c625SLionel Sambuc #include <stdio.h>
4784d9c625SLionel Sambuc 
4884d9c625SLionel Sambuc #undef LOCK_SUCCESS
4984d9c625SLionel Sambuc #include "../common/common.h"
5084d9c625SLionel Sambuc #include "../ipc/ip.h"
5184d9c625SLionel Sambuc #include "m_motif.h"
5284d9c625SLionel Sambuc #include "vi_mextern.h"
5384d9c625SLionel Sambuc 
5484d9c625SLionel Sambuc 
5584d9c625SLionel Sambuc /* globals */
5684d9c625SLionel Sambuc 
5784d9c625SLionel Sambuc static	Widget		db_ruler = NULL;
5884d9c625SLionel Sambuc 
5984d9c625SLionel Sambuc static	Boolean		active = False;
6084d9c625SLionel Sambuc 
6184d9c625SLionel Sambuc static	int		ruler_border = 5,
6284d9c625SLionel Sambuc 			ruler_asc;
6384d9c625SLionel Sambuc 
6484d9c625SLionel Sambuc static	GC		gc_ruler;
6584d9c625SLionel Sambuc 
6684d9c625SLionel Sambuc static	XFontStruct	*ruler_font;
6784d9c625SLionel Sambuc 
6884d9c625SLionel Sambuc static	char		text[256];
6984d9c625SLionel Sambuc 
7084d9c625SLionel Sambuc #if ! defined(SelfTest)
7184d9c625SLionel Sambuc static  XutResource resource[] = {
7284d9c625SLionel Sambuc     { 	"rulerFont",	XutRKfont,	&ruler_font	},
7384d9c625SLionel Sambuc     { 	"rulerBorder",	XutRKinteger,	&ruler_border	},
7484d9c625SLionel Sambuc };
7584d9c625SLionel Sambuc #endif
7684d9c625SLionel Sambuc 
7784d9c625SLionel Sambuc 
7884d9c625SLionel Sambuc /* change the displayed position */
7984d9c625SLionel Sambuc 
8084d9c625SLionel Sambuc static void
set_ruler_text(int row,int col,int * h,int * w,int * asc)8184d9c625SLionel Sambuc set_ruler_text(int row, int col, int *h, int *w, int *asc)
8284d9c625SLionel Sambuc {
8384d9c625SLionel Sambuc     int		dir, des;
8484d9c625SLionel Sambuc     XCharStruct	over;
8584d9c625SLionel Sambuc 
8684d9c625SLionel Sambuc     /* format the data */
8784d9c625SLionel Sambuc     sprintf( text, "%9.d,%-9.d", row+1, col+1 );
8884d9c625SLionel Sambuc 
8984d9c625SLionel Sambuc     /* how big will it be? */
9084d9c625SLionel Sambuc     XTextExtents( ruler_font, text, strlen(text), &dir, asc, &des, &over );
9184d9c625SLionel Sambuc 
9284d9c625SLionel Sambuc     /* how big a window will we need? */
9384d9c625SLionel Sambuc     *h = 2*ruler_border + over.ascent + over.descent;
9484d9c625SLionel Sambuc     *w = 2*ruler_border + over.width;
9584d9c625SLionel Sambuc }
9684d9c625SLionel Sambuc 
9784d9c625SLionel Sambuc 
9884d9c625SLionel Sambuc static void
redraw_text(void)9984d9c625SLionel Sambuc redraw_text(void)
10084d9c625SLionel Sambuc {
10184d9c625SLionel Sambuc     XClearArea( XtDisplay(db_ruler), XtWindow(db_ruler), 0, 0, 0, 0, False );
10284d9c625SLionel Sambuc     XDrawString( XtDisplay(db_ruler),
10384d9c625SLionel Sambuc 		 XtWindow(db_ruler),
10484d9c625SLionel Sambuc 		 gc_ruler,
10584d9c625SLionel Sambuc 		 ruler_border, ruler_border + ruler_asc,
10684d9c625SLionel Sambuc 		 text,
10784d9c625SLionel Sambuc 		 strlen(text)
10884d9c625SLionel Sambuc 		 );
10984d9c625SLionel Sambuc }
11084d9c625SLionel Sambuc 
11184d9c625SLionel Sambuc 
11284d9c625SLionel Sambuc /*
11384d9c625SLionel Sambuc  * PUBLIC: void __vi_set_text_ruler __P((int, int));
11484d9c625SLionel Sambuc  */
11584d9c625SLionel Sambuc void
__vi_set_text_ruler(int row,int col)11684d9c625SLionel Sambuc __vi_set_text_ruler(int row, int col)
11784d9c625SLionel Sambuc {
11884d9c625SLionel Sambuc     int h, w;
11984d9c625SLionel Sambuc 
12084d9c625SLionel Sambuc     if ( ! active ) return;
12184d9c625SLionel Sambuc 
12284d9c625SLionel Sambuc     set_ruler_text( row, col, &h, &w, &ruler_asc );
12384d9c625SLionel Sambuc 
12484d9c625SLionel Sambuc     redraw_text();
12584d9c625SLionel Sambuc }
12684d9c625SLionel Sambuc 
12784d9c625SLionel Sambuc 
12884d9c625SLionel Sambuc /* callbacks */
12984d9c625SLionel Sambuc 
13084d9c625SLionel Sambuc static void
cancel_cb(void)13184d9c625SLionel Sambuc cancel_cb(void)
13284d9c625SLionel Sambuc {
13384d9c625SLionel Sambuc #if defined(SelfTest)
13484d9c625SLionel Sambuc     puts( "cancelled" );
13584d9c625SLionel Sambuc #endif
13684d9c625SLionel Sambuc     active = False;
13784d9c625SLionel Sambuc }
13884d9c625SLionel Sambuc 
13984d9c625SLionel Sambuc 
destroyed(void)14084d9c625SLionel Sambuc static	void destroyed(void)
14184d9c625SLionel Sambuc {
14284d9c625SLionel Sambuc #if defined(SelfTest)
14384d9c625SLionel Sambuc     puts( "destroyed" );
14484d9c625SLionel Sambuc #endif
14584d9c625SLionel Sambuc 
14684d9c625SLionel Sambuc     /* some window managers destroy us upon popdown */
14784d9c625SLionel Sambuc     db_ruler = NULL;
14884d9c625SLionel Sambuc     active   = False;
14984d9c625SLionel Sambuc }
15084d9c625SLionel Sambuc 
15184d9c625SLionel Sambuc 
15284d9c625SLionel Sambuc 
15384d9c625SLionel Sambuc /* Draw and display a dialog the describes nvi options */
15484d9c625SLionel Sambuc 
15584d9c625SLionel Sambuc #if defined(__STDC__)
create_text_ruler_dialog(Widget parent,String title)15684d9c625SLionel Sambuc static	Widget	create_text_ruler_dialog( Widget parent, String title )
15784d9c625SLionel Sambuc #else
15884d9c625SLionel Sambuc static	Widget	create_text_ruler_dialog( parent, title )
15984d9c625SLionel Sambuc 	Widget	parent;
16084d9c625SLionel Sambuc 	String	title;
16184d9c625SLionel Sambuc #endif
16284d9c625SLionel Sambuc {
16384d9c625SLionel Sambuc     Widget	box;
16484d9c625SLionel Sambuc     int		h, w, asc;
16584d9c625SLionel Sambuc     Pixel	fg, bg;
16684d9c625SLionel Sambuc 
16784d9c625SLionel Sambuc     /* already built? */
16884d9c625SLionel Sambuc     if ( db_ruler != NULL ) return db_ruler;
16984d9c625SLionel Sambuc 
17084d9c625SLionel Sambuc #if defined(SelfTest)
17184d9c625SLionel Sambuc     ruler_font = XLoadQueryFont( XtDisplay(parent), "9x15" );
17284d9c625SLionel Sambuc #else
17384d9c625SLionel Sambuc     /* check the resource database for interesting resources */
17484d9c625SLionel Sambuc     __XutConvertResources( parent,
17584d9c625SLionel Sambuc 			 vi_progname,
17684d9c625SLionel Sambuc 			 resource,
17784d9c625SLionel Sambuc 			 XtNumber(resource)
17884d9c625SLionel Sambuc 			 );
17984d9c625SLionel Sambuc #endif
18084d9c625SLionel Sambuc 
18184d9c625SLionel Sambuc     gc_ruler = XCreateGC( XtDisplay(parent), XtWindow(parent), 0, NULL );
18284d9c625SLionel Sambuc     XSetFont( XtDisplay(parent), gc_ruler, ruler_font->fid );
18384d9c625SLionel Sambuc 
18484d9c625SLionel Sambuc     box = XtVaCreatePopupShell( title,
18584d9c625SLionel Sambuc 				transientShellWidgetClass,
18684d9c625SLionel Sambuc 				parent,
18784d9c625SLionel Sambuc 				XmNallowShellResize,	False,
18884d9c625SLionel Sambuc 				0
18984d9c625SLionel Sambuc 				);
19084d9c625SLionel Sambuc     XtAddCallback( box, XmNpopdownCallback, cancel_cb, 0 );
19184d9c625SLionel Sambuc     XtAddCallback( box, XmNdestroyCallback, destroyed, 0 );
19284d9c625SLionel Sambuc 
19384d9c625SLionel Sambuc     /* should be ok to use the font now */
19484d9c625SLionel Sambuc     active = True;
19584d9c625SLionel Sambuc 
19684d9c625SLionel Sambuc     /* how big a window? */
19784d9c625SLionel Sambuc     set_ruler_text( 0, 0, &h, &w, &asc );
19884d9c625SLionel Sambuc 
19984d9c625SLionel Sambuc     /* keep this global, we might destroy it later */
20084d9c625SLionel Sambuc     db_ruler = XtVaCreateManagedWidget( "Ruler",
20184d9c625SLionel Sambuc 					xmDrawingAreaWidgetClass,
20284d9c625SLionel Sambuc 					box,
20384d9c625SLionel Sambuc 					XmNheight,	h,
20484d9c625SLionel Sambuc 					XmNwidth,	w,
20584d9c625SLionel Sambuc 					0
20684d9c625SLionel Sambuc 					);
20784d9c625SLionel Sambuc     /* this callback is for when the drawing area is exposed */
20884d9c625SLionel Sambuc     XtAddCallback( db_ruler,
20984d9c625SLionel Sambuc 		   XmNexposeCallback,
21084d9c625SLionel Sambuc 		   redraw_text,
21184d9c625SLionel Sambuc 		   0
21284d9c625SLionel Sambuc 		   );
21384d9c625SLionel Sambuc 
21484d9c625SLionel Sambuc     /* what colors are selected for the drawing area? */
21584d9c625SLionel Sambuc     XtVaGetValues( db_ruler,
21684d9c625SLionel Sambuc 		   XmNbackground,		&bg,
21784d9c625SLionel Sambuc 		   XmNforeground,		&fg,
21884d9c625SLionel Sambuc 		   0
21984d9c625SLionel Sambuc 		   );
22084d9c625SLionel Sambuc     XSetForeground( XtDisplay(db_ruler), gc_ruler, fg );
22184d9c625SLionel Sambuc     XSetBackground( XtDisplay(db_ruler), gc_ruler, bg );
22284d9c625SLionel Sambuc 
22384d9c625SLionel Sambuc     /* done */
22484d9c625SLionel Sambuc     return db_ruler;
22584d9c625SLionel Sambuc }
22684d9c625SLionel Sambuc 
22784d9c625SLionel Sambuc 
22884d9c625SLionel Sambuc 
22984d9c625SLionel Sambuc /* module entry point
23084d9c625SLionel Sambuc  *	__vi_show_text_ruler_dialog( parent, title )
23184d9c625SLionel Sambuc  *	__vi_clear_text_ruler_dialog( parent, title )
23284d9c625SLionel Sambuc  */
23384d9c625SLionel Sambuc 
23484d9c625SLionel Sambuc #if defined(__STDC__)
__vi_show_text_ruler_dialog(Widget parent,String title)23584d9c625SLionel Sambuc void	__vi_show_text_ruler_dialog( Widget parent, String title )
23684d9c625SLionel Sambuc #else
23784d9c625SLionel Sambuc void	__vi_show_text_ruler_dialog( parent, title )
23884d9c625SLionel Sambuc Widget	parent;
23984d9c625SLionel Sambuc String	title;
24084d9c625SLionel Sambuc #endif
24184d9c625SLionel Sambuc {
24284d9c625SLionel Sambuc     Widget 	db = create_text_ruler_dialog( parent, title ),
24384d9c625SLionel Sambuc 		shell = XtParent(db);
24484d9c625SLionel Sambuc     Dimension	height, width;
24584d9c625SLionel Sambuc 
24684d9c625SLionel Sambuc     /* this guy does not resize */
24784d9c625SLionel Sambuc     XtVaGetValues( db,
24884d9c625SLionel Sambuc 		   XmNheight,	&height,
24984d9c625SLionel Sambuc 		   XmNwidth,	&width,
25084d9c625SLionel Sambuc 		   0
25184d9c625SLionel Sambuc 		   );
25284d9c625SLionel Sambuc     XtVaSetValues( shell,
25384d9c625SLionel Sambuc 		   XmNmaxWidth,		width,
25484d9c625SLionel Sambuc 		   XmNminWidth,		width,
25584d9c625SLionel Sambuc 		   XmNmaxHeight,	height,
25684d9c625SLionel Sambuc 		   XmNminHeight,	height,
25784d9c625SLionel Sambuc 		   0
25884d9c625SLionel Sambuc 		   );
25984d9c625SLionel Sambuc 
26084d9c625SLionel Sambuc     XtManageChild( db );
26184d9c625SLionel Sambuc 
26284d9c625SLionel Sambuc     /* leave this guy up */
26384d9c625SLionel Sambuc     XtPopup( shell, XtGrabNone );
26484d9c625SLionel Sambuc 
26584d9c625SLionel Sambuc     active = True;
26684d9c625SLionel Sambuc 
26784d9c625SLionel Sambuc     /* ask vi core for the current r,c now */
26884d9c625SLionel Sambuc #if ! defined(SelfTest)
26984d9c625SLionel Sambuc     __vi_set_text_ruler( __vi_screen->cury, __vi_screen->curx );
27084d9c625SLionel Sambuc #else
27184d9c625SLionel Sambuc     __vi_set_text_ruler( rand(), rand() );
27284d9c625SLionel Sambuc #endif
27384d9c625SLionel Sambuc }
27484d9c625SLionel Sambuc 
27584d9c625SLionel Sambuc 
27684d9c625SLionel Sambuc #if defined(__STDC__)
__vi_clear_text_ruler_dialog()27784d9c625SLionel Sambuc void	__vi_clear_text_ruler_dialog()
27884d9c625SLionel Sambuc #else
27984d9c625SLionel Sambuc void	__vi_clear_text_ruler_dialog(void)
28084d9c625SLionel Sambuc #endif
28184d9c625SLionel Sambuc {
28284d9c625SLionel Sambuc     if ( active )
28384d9c625SLionel Sambuc 	XtPopdown( XtParent(db_ruler) );
28484d9c625SLionel Sambuc }
28584d9c625SLionel Sambuc 
28684d9c625SLionel Sambuc 
28784d9c625SLionel Sambuc #if defined(SelfTest)
28884d9c625SLionel Sambuc 
28984d9c625SLionel Sambuc #if XtSpecificationRelease == 4
29084d9c625SLionel Sambuc #define	ArgcType	Cardinal *
29184d9c625SLionel Sambuc #else
29284d9c625SLionel Sambuc #define	ArgcType	int *
29384d9c625SLionel Sambuc #endif
29484d9c625SLionel Sambuc 
change_pos(Widget w)29584d9c625SLionel Sambuc static	void	change_pos( Widget w )
29684d9c625SLionel Sambuc {
29784d9c625SLionel Sambuc     __vi_set_text_ruler( rand(), rand() );
29884d9c625SLionel Sambuc }
29984d9c625SLionel Sambuc 
30084d9c625SLionel Sambuc #if defined(__STDC__)
show_text_ruler(Widget w,XtPointer data,XtPointer cbs)30184d9c625SLionel Sambuc static void show_text_ruler( Widget w, XtPointer data, XtPointer cbs )
30284d9c625SLionel Sambuc #else
30384d9c625SLionel Sambuc static void show_text_ruler( w, data, cbs )
30484d9c625SLionel Sambuc Widget w;
30584d9c625SLionel Sambuc XtPointer	data;
30684d9c625SLionel Sambuc XtPointer	cbs;
30784d9c625SLionel Sambuc #endif
30884d9c625SLionel Sambuc {
30984d9c625SLionel Sambuc     __vi_show_text_ruler_dialog( data, "Ruler" );
31084d9c625SLionel Sambuc }
31184d9c625SLionel Sambuc 
main(int argc,char * argv[])31284d9c625SLionel Sambuc main( int argc, char *argv[] )
31384d9c625SLionel Sambuc {
31484d9c625SLionel Sambuc     XtAppContext	ctx;
31584d9c625SLionel Sambuc     Widget		top_level, rc, button;
31684d9c625SLionel Sambuc 
31784d9c625SLionel Sambuc     /* create a top-level shell for the window manager */
31884d9c625SLionel Sambuc     top_level = XtVaAppInitialize( &ctx,
31984d9c625SLionel Sambuc 				   argv[0],
32084d9c625SLionel Sambuc 				   NULL, 0,	/* options */
32184d9c625SLionel Sambuc 				   (ArgcType) &argc,
32284d9c625SLionel Sambuc 				   argv,	/* might get modified */
32384d9c625SLionel Sambuc 				   NULL,
32484d9c625SLionel Sambuc 				   NULL
32584d9c625SLionel Sambuc 				   );
32684d9c625SLionel Sambuc 
32784d9c625SLionel Sambuc     rc = XtVaCreateManagedWidget( "rc",
32884d9c625SLionel Sambuc 				  xmRowColumnWidgetClass,
32984d9c625SLionel Sambuc 				  top_level,
33084d9c625SLionel Sambuc 				  0
33184d9c625SLionel Sambuc 				  );
33284d9c625SLionel Sambuc 
33384d9c625SLionel Sambuc     button = XtVaCreateManagedWidget( "Pop up text ruler dialog",
33484d9c625SLionel Sambuc 				      xmPushButtonGadgetClass,
33584d9c625SLionel Sambuc 				      rc,
33684d9c625SLionel Sambuc 				      0
33784d9c625SLionel Sambuc 				      );
33884d9c625SLionel Sambuc     XtAddCallback( button, XmNactivateCallback, show_text_ruler, rc );
33984d9c625SLionel Sambuc 
34084d9c625SLionel Sambuc     button = XtVaCreateManagedWidget( "Change Position",
34184d9c625SLionel Sambuc 				      xmPushButtonGadgetClass,
34284d9c625SLionel Sambuc 				      rc,
34384d9c625SLionel Sambuc 				      0
34484d9c625SLionel Sambuc 				      );
34584d9c625SLionel Sambuc     XtAddCallback( button, XmNactivateCallback, change_pos, rc );
34684d9c625SLionel Sambuc 
34784d9c625SLionel Sambuc     button = XtVaCreateManagedWidget( "Quit",
34884d9c625SLionel Sambuc 				      xmPushButtonGadgetClass,
34984d9c625SLionel Sambuc 				      rc,
35084d9c625SLionel Sambuc 				      0
35184d9c625SLionel Sambuc 				      );
35284d9c625SLionel Sambuc     XtAddCallback( button, XmNactivateCallback, exit, 0 );
35384d9c625SLionel Sambuc 
35484d9c625SLionel Sambuc     XtRealizeWidget(top_level);
35584d9c625SLionel Sambuc     XtAppMainLoop(ctx);
35684d9c625SLionel Sambuc }
35784d9c625SLionel Sambuc #endif
358