File: comment.c

package info (click to toggle)
fkiss 0.33a.patch-3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 804 kB
  • ctags: 1,074
  • sloc: ansic: 9,141; sh: 2,924; makefile: 98; sed: 14
file content (209 lines) | stat: -rw-r--r-- 5,277 bytes parent folder | download | duplicates (3)
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
/* $Header: /home/yav/catty/fkiss/RCS/comment.c,v 1.3 2000/08/24 02:10:48 yav Exp $
 * fkiss cel comment display
 * written by yav <yav@bigfoot.com>
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

char id_comment[] = "$Id: comment.c,v 1.3 2000/08/24 02:10:48 yav Exp $";

#include <X11/Xlib.h>
#include <stdio.h>

#include "config.h"

#include "headers.h"
#include "fkiss.h"
#include "work.h"

#define PUBLIC_COMMENT_C
#include "extern.h"

#define COMWIN_BORDER
#define COMWIN_XOFS 8
#define COMWIN_YOFS (-8)
#define COMWIN_LINE_SPACE 16
#define COMWIN_UPPER_MARGIN 4
#define COMWIN_LOWER_MARGIN 6	/* within font descent */
#define COMWIN_LEFT_MARGIN 4
#define COMWIN_RIGHT_MARGIN 4
#define COMWIN_NUMBER_WIDTH 40

static Bool comwin_created = False;
static int comment_cel = -1;

int expose_comment_line(d, n, x, y, mark, rw, rh)
     Drawable d;		/* Drawable */
     int n;			/* cel number */
     int x, y;			/* base point position (pixel) */
     int mark;			/* mark character */
     int *rw;			/* width return pointer */
     int *rh;			/* height return pointer */
{
  int i, r, w, h;
  char *p;
  char buf[256];		/* cnf line must be shorter than 256 bytes */
  
  if (comment_linenum) {
    /* with line number */
    sprintf(buf, "%4d%c", (cell + n)->line, mark);
    draw_string(d, x, y, buf);
    x += COMWIN_NUMBER_WIDTH;
  }
  r = w = h = 0;
  p = (cell + n)->comment;
  if (p != NULL) {
    while (*p) {
      for (i = 0; i < sizeof(buf)-1; i++) {
	if (*p == '\0')
	  break;
	if (*p == '\n') {
	  p++;
	  break;
	}
	buf[i] = *p++;
      }
      buf[i] = '\0';
      i = draw_string(d, x, y, buf);
      if (i > w)
	w = i;
      y += COMWIN_LINE_SPACE;
      h += COMWIN_LINE_SPACE;
      r++;
    }
  }
  if (comment_linenum) {
    w += COMWIN_NUMBER_WIDTH;
    if (!r) {
      r++;
      h += COMWIN_LINE_SPACE;
    }
  }
  if (rw != NULL)
    *rw = w;
  if (rh != NULL)
    *rh = h;
  return r;			/* line count */
}

/* draw comment to comment window
 * and return drawn area geometry
 */
int expose_comment_sub(d, celn, x, y, rw, rh)
     Drawable d;		/* drawing window or pixmap or None */
     int celn;			/* cel number */
     int x, y;			/* base point */
     int *rw;			/* drawing area width return */
     int *rh;			/* drawing area height return */
{
  int i, n, r, w, h;

  if (celn < 0)
    return 0;			/* illegal cel number, No line printed */
  r = 0;
  switch (comment_mode) {
  case 1:
    r = expose_comment_line(d, celn, x, y, 0, rw, rh);
    break;
  case 2:
    if (rw != NULL)
      *rw = 0;
    if (rh != NULL)
      *rh = 0;
    for (i = 0; i < celcnt; i++) {
      if ((cell + i)->width &&
	  ((cell + i)->obj == (cell + celn)->obj) &&
	  ((cell + i)->comment != NULL)) {
	n = expose_comment_line(d, i, x, y, (i == celn ? '*' : ' '), &w, &h);
	y += n * COMWIN_LINE_SPACE;
	r += n;
	if (rw != NULL && w > *rw)
	  *rw = w;
	if (rh != NULL)
	  *rh += h;
      }
    }
    break;
  }
  return r;			/* line count */
}

void expose_comment(ev)
     XExposeEvent *ev;
{
  if (!ev->count) {
    /* redraw at last event only */
    expose_comment_sub(comwin, comment_cel,
		       COMWIN_LEFT_MARGIN,
		       COMWIN_UPPER_MARGIN + COMWIN_LINE_SPACE,
		       NULL, NULL);
  }
}

void unmap_comment()
{
  if (comwin_created)
    XUnmapWindow(dsp, comwin);
}

void map_comment(ev)
     XButtonEvent *ev;
{
  int i, x, y, w, h;
  XSetWindowAttributes atr;
  Window child;

  if (!comment_mode)
    return;
  x = ev->x;
  y = ev->y;
  comment_cel = search_cell(x, y);
  if (expose_comment_sub(None, comment_cel, 0, 0, &w, &h) <= 0) {
    unmap_comment();
    return;
  }
  XTranslateCoordinates(dsp, ev->window, rootwin, x, y, &x, &y, &child);
  w += COMWIN_LEFT_MARGIN + COMWIN_RIGHT_MARGIN;
  h += COMWIN_UPPER_MARGIN + COMWIN_LOWER_MARGIN;
  x += COMWIN_XOFS;
  y += COMWIN_YOFS - h;
  if (x + w > DisplayWidth(dsp, scr))
    x = DisplayWidth(dsp, scr) - w;
  if (y + h > DisplayHeight(dsp, scr))
    y = DisplayHeight(dsp, scr) - h;
  if (x < 0)
    x = 0;
  if (y < 0)
    y = 0;
  if (comwin_created) {
    unmap_comment();		/* to send expose event */
    XMoveResizeWindow(dsp, comwin, x, y, w, h);
  } else {
    comwin = XCreateSimpleWindow(dsp, rootwin,
				 x, y, w, h, combw,
				 spx[SPX_FG], spx[SPX_BG]);
    XSelectInput(dsp, comwin, ExposureMask|ButtonPressMask
		 |EnterWindowMask|KeyPressMask);
    XSetTransientForHint(dsp, comwin, topwin);
    atr.override_redirect = True;
    atr.save_under = True;
    i = CWOverrideRedirect|CWSaveUnder;
    XChangeWindowAttributes(dsp, comwin, i, &atr);
    comwin_created = True;
  }
  XMapRaised(dsp, comwin);
}

/* End of file */