8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / tools / cscope-fast / mouse.c
blob1e29ad7ba2cecfce6cb4a454ad7dd36fe6066abf
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
20 * CDDL HEADER END
22 /* Copyright (c) 1988 AT&T */
23 /* All Rights Reserved */
27 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
28 * Use is subject to license terms.
31 #pragma ident "%Z%%M% %I% %E% SMI"
33 /* common mouse interface functions */
35 #include <stdio.h> /* NULL */
36 #include <stdlib.h> /* NULL */
37 #include <string.h> /* NULL */
38 #include <ctype.h> /* isdigit */
39 #include "global.h"
41 #define ctrl(x) (x & 037)
43 MOUSETYPE mouse;
45 static MOUSEMENU *loadedmenu;
46 static BOOL changemenu = YES;
48 /* see if there is a mouse interface */
50 void
51 initmouse(void)
53 char *s, *term;
55 if ((term = getenv("TERM")) == NULL) {
56 return;
58 if (strcmp(term, "emacsterm") == 0 || strcmp(term, "viterm") == 0) {
59 mouse = EMACSTERM;
60 } else if ((s = getenv("MOUSE")) != NULL && strcmp(s, "myx") == 0) {
62 * the MOUSE enviroment variable is for 5620 terminal
63 * programs that have mouse support but the TERM environment
64 * variable is the same as a terminal without a mouse, such
65 * as myx
67 mouse = MYX;
69 if ((s = getenv("MOUSEMENU")) != NULL && strcmp(s, "none") == 0) {
70 changemenu = NO;
72 initmenu();
75 /* reinitialize the mouse in case curses changed the attributes */
77 void
78 reinitmouse(void)
80 if (mouse == EMACSTERM) {
83 * enable the mouse click and sweep coordinate control
84 * sequence
86 (void) printf("\033{2");
87 if (changemenu) {
88 (void) printf("\033#2"); /* switch to menu 2 */
90 (void) fflush(stdout);
94 /* restore any original mouse attributes not handled by terminfo */
96 void
97 cleanupmouse(void)
99 int i;
101 if (mouse == MYX && loadedmenu != NULL) {
102 /* remove the mouse menu */
103 (void) printf("\033[6;0X\033[9;0X");
104 for (i = 0; loadedmenu[i].text != NULL; ++i) {
105 (void) printf("\033[0;0x");
107 loadedmenu = NULL;
111 /* download a mouse menu */
113 void
114 downloadmenu(MOUSEMENU *menu)
116 int i;
117 int len;
119 switch (mouse) {
120 case EMACSTERM:
121 reinitmouse();
122 (void) printf("\033V1"); /* display the scroll bar */
123 if (changemenu) {
124 (void) printf("\033M0@%s@%s@", menu[0].text,
125 menu[0].value);
126 for (i = 1; menu[i].text != NULL; ++i) {
127 (void) printf("\033M@%s@%s@", menu[i].text,
128 menu[i].value);
131 (void) fflush(stdout);
132 break;
133 case MYX:
134 if (changemenu) {
135 cleanupmouse();
136 (void) printf("\033[6;1X\033[9;1X");
137 for (i = 0; menu[i].text != NULL; ++i) {
138 len = strlen(menu[i].text);
139 (void) printf("\033[%d;%dx%s%s", len,
140 len + strlen(menu[i].value),
141 menu[i].text, menu[i].value);
143 (void) fflush(stdout);
144 loadedmenu = menu;
146 break;
147 case NONE:
148 case PC7300:
149 break;
153 /* draw the scroll bar */
155 void
156 drawscrollbar(int top, int bot, int total)
158 int p1, p2;
160 if (mouse == EMACSTERM) {
161 if (bot > top && total > 0) {
162 p1 = 16 + (top - 1) * 100 / total;
163 p2 = 16 + (bot - 1) * 100 / total;
164 if (p2 > 116) {
165 p2 = 116;
167 if (p1 < 16) {
168 p1 = 16;
171 * don't send ^S or ^Q to avoid hanging a layer using
172 * cu(1)
174 if (p1 == ctrl('Q') || p1 == ctrl('S')) {
175 ++p1;
177 if (p2 == ctrl('Q') || p2 == ctrl('S')) {
178 ++p2;
180 } else {
181 p1 = p2 = 16;
183 (void) printf("\033W%c%c", p1, p2);
187 /* translate a mouse click or sweep to a selection */
190 mouseselection(MOUSEEVENT *p, int offset, int maxselection)
192 int i;
194 i = p->y1 - offset;
195 if (i < 0) {
196 i = 0;
197 } else if (i >= maxselection) {
198 i = maxselection - 1;
200 return (i);
203 /* get the mouse event */
205 MOUSEEVENT *
206 getmouseevent(void)
208 static MOUSEEVENT m;
210 if (mouse == EMACSTERM) {
211 switch (mygetch()) {
212 case ctrl('_'): /* click */
213 if ((m.button = mygetch()) == '0') { /* if scroll bar */
214 m.percent = getpercent();
215 } else {
216 m.x1 = getcoordinate();
217 m.y1 = getcoordinate();
218 m.x2 = m.y2 = -1;
220 break;
222 case ctrl(']'): /* sweep */
223 m.button = mygetch();
224 m.x1 = getcoordinate();
225 m.y1 = getcoordinate();
226 m.x2 = getcoordinate();
227 m.y2 = getcoordinate();
228 break;
229 default:
230 return (NULL);
232 return (&m);
234 return (NULL);
237 /* get a row or column coordinate from a mouse button click or sweep */
240 getcoordinate(void)
242 int c, next;
244 c = mygetch();
245 next = 0;
246 if (c == ctrl('A')) {
247 next = 95;
248 c = mygetch();
250 if (c < ' ') {
251 return (0);
253 return (next + c - ' ');
256 /* get a percentage */
259 getpercent(void)
261 int c;
263 c = mygetch();
264 if (c < 16) {
265 return (0);
267 if (c > 120) {
268 return (100);
270 return (c - 16);
273 /* update the window label area */
276 labelarea(char *s)
278 static BOOL labelon;
280 switch (mouse) {
281 case EMACSTERM:
282 if (labelon == NO) {
283 labelon = YES;
284 (void) printf("\033L1"); /* force it on */
286 (void) printf("\033L!%s!", s);
287 return (1);
288 case MYX:
289 (void) printf("\033[?%dv%s", strlen(s), s);
290 return (1);
291 case NONE:
292 case PC7300:
293 default:
294 return (0); /* no label area */