File: editctrl.c

package info (click to toggle)
saoimage 1.19-5
  • links: PTS
  • area: main
  • in suites: slink
  • size: 3,256 kB
  • ctags: 3,610
  • sloc: ansic: 36,050; makefile: 215; sh: 11
file content (282 lines) | stat: -rw-r--r-- 9,347 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#ifndef lint
static char SccsId[] = "%W%  %G%";
#endif

/* Module:	editctrl.c (Editor Control)
 * Purpose:	Top level of text editing session for string input
 * Subroutine:	init_edit_popup()		returns: EditStruct *
 * Subroutine:	get_edit_input()		returns: int
 * Subroutine:	unmap_popwin()			returns: void
 * Xlib calls:	XCreateSimpleWindow(), XMapWindow(), XUnmapWindow()
 * Xlib calls:	XSelectInput(), XNextEvent(), XDrawImageString()
 * Copyright:	1989 Smithsonian Astrophysical Observatory
 *		You may do anything you like with this file except remove
 *		this copyright.  The Smithsonian Astrophysical Observatory
 *		makes no representations about the suitability of this
 *		software for any purpose.  It is provided "as is" without
 *		express or implied warranty.
 * Modified:	{0} Michael VanHilst	initial version		  7 July 1989
 *		{1} Jay Travisano (STScI)  VMS,IMTOOL changes     10 Nov 1989
 *              {2} MVH BSDonly strings.h compatability           19 Feb 1990
 *		{n} <who> -- <does what> -- <when>
 */

#include <stdio.h>		/* stderr, NULL, etc. */

#ifndef VMS
#ifdef SYSV
#include <string.h>		/* strlen, strcat, strcpy, strrchr */
#else
#include <strings.h>		/* strlen, etc. for unenlightened BSD's */
#endif
#else
#include <string.h>		/* strlen, strcat, strcpy, strrchr */
#endif

#include <X11/Xlib.h>		/* X window stuff */
#include <X11/Xutil.h>		/* X window manager stuff */
#include "hfiles/color.h"
#include "hfiles/window.h"
#include "hfiles/edit.h"

extern struct colorRec color;
extern struct windowRec btnbox;
extern struct windowRec desktop;
extern Window root;

#define XOFF 2
#define YOFF 1
#define BDRWDTH 2
#define WDTHLESS 12

struct popRec pop;
static int init_window = 1;

#if VMS && IMTOOL
extern void XZ_ast();
extern int  XZ_efn;
#endif

/*
 * Subroutine:	init_edit_popup
 * Purpose:	Get everything ready for running editor sessions
 */
EditStruct *init_edit_popup ( string, max_chars )
     char *string;
     int max_chars;
{
  EditStruct *edit;
  int len;
  EditStruct *get_edit_struct();
  void init_edit_struct(), load_edit_struct();
  static void init_popwin();

  if( init_window ) {
    init_popwin(color.gcset.menu.foreground, color.gcset.menu.background);
    init_window = 0;
  }
  edit = get_edit_struct (max_chars);
  init_edit_struct(pop.display, pop.ID, edit,
		    pop.fontstruct, pop.foreground, pop.background);
  if( (string != NULL) && ((len = strlen(string)) > 1) )
    load_edit_struct(edit, string, len);
  return( edit );
}

/*
 * Subroutine:	get_edit_input
 * Purpose:	Run popup editor session for text input
 * Xlib calls:	XNextEvent(), XDrawImageString(), XUnmapWindow()
 * Note:	All non-editor events are thrown out during session
 *		(but expose is fielded and configure is saved for end).
 * Returns:	1 if user returns with a response, 0 if user cancels session
 */
int get_edit_input ( edit, one_row, map, unmap, prompt )
     EditStruct *edit;
     int one_row;	/* i: put-edit-after-prompt-on-same-line */
     int map;		/* i: map-window-before-starting */
     int unmap;		/* i: unmap-window-before-return */
     char *prompt;	/* i: user prompt such as "enter file name:" */
{
  XEvent event, configure;
  int init, got_configure, answer;
  GC gc, set_edit_gc();
  int emacs_response();
  void draw_new_string(), redraw_edit_string();
  void adjust_desktop(), redraw_window(), unmap_popwin();
  static void map_popwin();

  map_popwin(edit, one_row, prompt, map);
  if( !map ) {
    /* window already up, don't wait for expose event */
    XClearWindow(pop.display, pop.ID);
    gc = set_edit_gc(pop.font, pop.foreground, pop.background);
    XDrawImageString(pop.display, pop.ID, gc, pop.prompt_x,
		     pop.prompt_y, prompt, strlen(prompt));
    draw_new_string(edit, 0);
    init = 0;
  } else
    init = 1;
  got_configure = 0;
  while( 1 ) {
    /* swallow all the events, except a few, from now until we are done */
    XNextEvent(pop.display, &event);
    switch( event.type ) {
    case KeyPress:
      /* grab key events from all application windows */
      if( (answer = emacs_response(&event.xkey, edit)) != 0 ) {
	if( got_configure )
	  adjust_desktop(&configure.xconfigure);
	if( unmap )
	  unmap_popwin();
	if( answer < 0 )
	  return( 0 );
	else
	  return( 1 );
      }
      break;
    case Expose:
      if( event.xexpose.count > 0 )
	break;
      if( event.xexpose.window == pop.ID ) {
	gc = set_edit_gc(pop.font, pop.foreground, pop.background);
	XDrawImageString(pop.display, pop.ID, gc, pop.prompt_x,
			 pop.prompt_y, prompt, strlen(prompt));
	if( init ) {
	  draw_new_string(edit, 0);
	  init = 0;
	} else
	  redraw_edit_string(edit);
      } else
	/* redraw entire window once (not parts of windows) */
	redraw_window(event.xexpose.window);
      break;
    case ConfigureNotify:
      /* if the main window is reconfigured, save this info */
      if( event.xconfigure.window == desktop.ID ) {
	bcopy((char *)&event, (char *)&configure, sizeof(XEvent));
	got_configure = 1;
      }
      break;
    default:
      break;
    }
  }
}

/*
 * Subroutine:	unmap_popwin
 * Purpose:	Unmap the popwin: called by routines which control map and
 *		unmap themselves because they use the window for multiple
 *		queries (single query can have auto-unmap)
 */
void unmap_popwin ( )
{
  if( pop.pointer_x >= 0 )
    /* if pointer position was noted, return pointer to continue from where
       event processing was interrupted */
    XWarpPointer(pop.display, None, root, 0, 0, 0, 0,
		 pop.pointer_x, pop.pointer_y);
  XUnmapWindow(pop.display, pop.ID);
}

/*
 * Subroutine:	init_popwin
 * Purpose:	Set the popup parameters and create the popup window
 * Xlib calls:	XCreateSimpleWindow(), XSelectInput();
 */
static void init_popwin ( foreground, background )
     unsigned long foreground, background;
{
  int text_yoff;
  XWMHints wmhints;
  XFontStruct *get_fontstruct();

  pop.foreground = foreground;
  pop.background = background;
  /* use the "large" font */
  pop.fontstruct = get_fontstruct (2);
  pop.font = pop.fontstruct->fid;
  pop.font_height = pop.fontstruct->ascent + pop.fontstruct->descent;
  pop.ref_width = btnbox.width;
  pop.ref_height = btnbox.height;
  pop.width = pop.ref_width - (XOFF + XOFF + BDRWDTH + BDRWDTH);
  pop.height = pop.ref_height - (YOFF + YOFF + BDRWDTH + BDRWDTH);
  text_yoff = (pop.height - (pop.font_height + pop.font_height)) / 3;
  pop.two_y1 = pop.font_height + text_yoff - pop.fontstruct->descent;
  pop.two_y2 = pop.two_y1 + pop.font_height + text_yoff;
  pop.one_y = (pop.height + pop.fontstruct->ascent) / 2;
  pop.prompt_x = pop.two_y1;
  pop.display = btnbox.display;
  pop.ID =
    XCreateSimpleWindow(btnbox.display, btnbox.ID, XOFF, YOFF,
			(unsigned int)pop.width, (unsigned int)pop.height,
			BDRWDTH, pop.foreground, pop.background);
#if VMS && IMTOOL
  XSelectAsyncInput(pop.display, pop.ID, ExposureMask | KeyPressMask,
	  	XZ_ast, XZ_efn);
#endif
  XSelectInput(pop.display, pop.ID, ExposureMask | KeyPressMask);
  /* Tell window manager to handle focus for us */
  wmhints.input = True;
  wmhints.flags = InputHint;
  XSetWMHints(btnbox.display, pop.ID, &wmhints);
}

/*
 * Subroutine:	map_popwin
 * Purpose:	map the popwindow, noting its current size and setting params
 * Xlib calls:	XMapWindow()
 */
static void map_popwin ( edit, one_row, prompt, map )
     EditStruct *edit;
     int one_row;	/* i: put-edit-after-prompt-on-same-line */
     char *prompt;	/* i: prompt string such as "Enter file name:" */
     int map;		/* i: map-the-window-when-ready */
{
  int text_yoff;
  Window root_ret, child_ret;
  int x_ret, y_ret;
  unsigned int mask_ret;

  if( (pop.ref_width != btnbox.width) || (pop.ref_height != btnbox.height) ) {
    pop.ref_width = btnbox.width;
    pop.ref_height = btnbox.height;
    pop.width = pop.ref_width - (XOFF + XOFF + BDRWDTH + BDRWDTH);
    pop.height = pop.ref_height - (YOFF + YOFF + BDRWDTH + BDRWDTH);
    XResizeWindow(btnbox.display, pop.ID,
		  (unsigned int)pop.width, (unsigned int)pop.height);
    text_yoff = (pop.height - (pop.font_height + pop.font_height)) / 3;
    pop.two_y1 = pop.font_height + text_yoff;
    pop.two_y2 = pop.two_y1 + pop.font_height + text_yoff;
    pop.one_y = (pop.height + pop.fontstruct->ascent) / 2;
    pop.prompt_x = pop.two_y1;
  }
  /* we don't know how current this edit's coords are, always update them */
  if( one_row ) {
    pop.prompt_y = pop.one_y;
    edit->y = pop.one_y;
    edit->x =
      pop.prompt_x + XTextWidth(pop.fontstruct, prompt, strlen(prompt)) + 10;
  } else {
    pop.prompt_y = pop.two_y1;
    edit->y = pop.two_y2;
    edit->x = pop.prompt_x;
  }
  edit->max_pixlen = pop.width - (edit->x + pop.prompt_x);
  edit->area_y = edit->y - edit->fontstruct->max_bounds.ascent;
  if( map ) {
    if( XQueryPointer(pop.display, root, &root_ret, &child_ret, &x_ret, &y_ret,
		      &pop.pointer_x, &pop.pointer_y, &mask_ret) == False )
      pop.pointer_x = -1;
    XMapWindow (btnbox.display, pop.ID);
  }
}