Debian Codegraph @

generic/bltCutbuffer.c

raw symbols SCIP
1 /*
2 * bltCutbuffer.c --
3 *
4 * Copyright 1993-1998 Lucent Technologies, Inc.
5 *
6 * Permission to use, copy, modify, and distribute this software and
7 * its documentation for any purpose and without fee is hereby
8 * granted, provided that the above copyright notice appear in all
9 * copies and that both that the copyright notice and warranty
10 * disclaimer appear in supporting documentation, and that the names
11 * of Lucent Technologies any of their entities not be used in
12 * advertising or publicity pertaining to distribution of the software
13 * without specific, written prior permission.
14 *
15 * Lucent Technologies disclaims all warranties with regard to this
16 * software, including all implied warranties of merchantability and
17 * fitness. In no event shall Lucent Technologies be liable for any
18 * special, indirect or consequential damages or any damages
19 * whatsoever resulting from loss of use, data or profits, whether in
20 * an action of contract, negligence or other tortuous action, arising
21 * out of or in connection with the use or performance of this
22 * software.
23 */
24
25 #include "bltInt.h"
26
27 #ifndef NO_CUTBUFFER
28
29 #ifndef WIN32
30 #include <X11/Xproto.h>
31 #endif
32
33 static int
34 GetCutNumber(interp, string, bufferPtr)
35 Tcl_Interp *interp;
36 char *string;
37 int *bufferPtr;
38 {
39 int number;
40
41 if (Tcl_GetInt(interp, string, &number) != TCL_OK) {
42 return TCL_ERROR;
43 }
44 if ((number < 0) || (number > 7)) {
45 Tcl_AppendResult(interp, "bad buffer # \"", string, "\"", (char *)NULL);
46 return TCL_ERROR;
47 }
48 *bufferPtr = number;
49 return TCL_OK;
50 }
51
52 /* ARGSUSED */
53 static int
54 RotateErrorProc(clientData, errEventPtr)
55 ClientData clientData;
56 XErrorEvent *errEventPtr;
57 {
58 int *errorPtr = clientData;
59
60 *errorPtr = TCL_ERROR;
61 return 0;
62 }
63
64 static int
65 GetOp(interp, tkwin, argc, argv)
66 Tcl_Interp *interp;
67 Tk_Window tkwin;
68 int argc;
69 char **argv;
70 {
71 char *string;
72 int buffer;
73 int nBytes;
74
75 buffer = 0;
76 if (argc == 3) {
77 if (GetCutNumber(interp, argv[2], &buffer) != TCL_OK) {
78 return TCL_ERROR;
79 }
80 }
81 string = XFetchBuffer(Tk_Display(tkwin), &nBytes, buffer);
82 if (string != NULL) {
83 int limit;
84 register char *p;
85 register int i;
86 int c;
87
88 if (string[nBytes - 1] == '\0') {
89 limit = nBytes - 1;
90 } else {
91 limit = nBytes;
92 }
93 for (p = string, i = 0; i < limit; i++, p++) {
94 c = (unsigned char)*p;
95 if (c == 0) {
96 *p = ' '; /* Convert embedded NUL bytes */
97 }
98 }
99 if (limit == nBytes) {
100 char *newPtr;
101
102 /*
103 * Need to copy the string into a bigger buffer so we can
104 * add a NUL byte on the end.
105 */
106 newPtr = Blt_Malloc(nBytes + 1);
107 assert(newPtr);
108 memcpy(newPtr, string, nBytes);
109 newPtr[nBytes] = '\0';
110 Blt_Free(string);
111 string = newPtr;
112 }
113 Tcl_SetResult(interp, string, TCL_DYNAMIC);
114 }
115 return TCL_OK;
116 }
117
118 static int
119 RotateOp(interp, tkwin, argc, argv)
120 Tcl_Interp *interp;
121 Tk_Window tkwin;
122 int argc;
123 char **argv;
124 {
125 int count;
126 int result;
127 Tk_ErrorHandler handler;
128
129 count = 1; /* Default: rotate one position */
130 if (argc == 3) {
131 if (Tcl_GetInt(interp, argv[2], &count) != TCL_OK) {
132 return TCL_ERROR;
133 }
134 if ((count < 0) || (count > 8)) {
135 Tcl_AppendResult(interp, "bad rotate count \"", argv[2], "\"",
136 (char *)NULL);
137 return TCL_ERROR;
138 }
139 }
140 result = TCL_OK;
141 handler = Tk_CreateErrorHandler(Tk_Display(tkwin), BadMatch,
142 X_RotateProperties, -1, RotateErrorProc, &result);
143 XRotateBuffers(Tk_Display(tkwin), count);
144 Tk_DeleteErrorHandler(handler);
145 XSync(Tk_Display(tkwin), False);
146 if (result != TCL_OK) {
147 Tcl_AppendResult(interp, "can't rotate cutbuffers unless all are set",
148 (char *)NULL);
149 return TCL_ERROR;
150 }
151 return TCL_OK;
152 }
153
154
155 static int
156 SetOp(interp, tkwin, argc, argv)
157 Tcl_Interp *interp;
158 Tk_Window tkwin;
159 int argc;
160 char **argv;
161 {
162 int buffer;
163
164 buffer = 0;
165 if (argc == 4) {
166 if (GetCutNumber(interp, argv[3], &buffer) != TCL_OK) {
167 return TCL_ERROR;
168 }
169 }
170 XStoreBuffer(Tk_Display(tkwin), argv[2], strlen(argv[2]) + 1, buffer);
171 return TCL_OK;
172 }
173
174 /*
175 *--------------------------------------------------------------
176 *
177 * BLT Sub-command specification:
178 *
179 * - Name of the sub-command.
180 * - Minimum number of characters needed to unambiguously
181 * recognize the sub-command.
182 * - Pointer to the function to be called for the sub-command.
183 * - Minimum number of arguments accepted.
184 * - Maximum number of arguments accepted.
185 * - String to be displayed for usage.
186 *
187 *--------------------------------------------------------------
188 */
189 static Blt_OpSpec cbOps[] =
190 {
191 {"get", 1, (Blt_Op)GetOp, 2, 3, "?buffer?",},
192 {"rotate", 1, (Blt_Op)RotateOp, 2, 3, "?count?",},
193 {"set", 1, (Blt_Op)SetOp, 3, 4, "value ?buffer?",},
194 };
195 static int numCbOps = sizeof(cbOps) / sizeof(Blt_OpSpec);
196
197
198 /*
199 *----------------------------------------------------------------------
200 *
201 * CutBufferCmd --
202 *
203 * This procedure is invoked to process the "cutbuffer" Tcl
204 * command. See the user documentation for details on what it does.
205 *
206 * Results:
207 * A standard Tcl result.
208 *
209 * Side effects:
210 * None.
211 *
212 *----------------------------------------------------------------------
213 */
214 /* ARGSUSED */
215 static int
216 CutbufferCmd(clientData, interp, argc, argv)
217 ClientData clientData; /* Main window associated with
218 * interpreter.*/
219 Tcl_Interp *interp; /* Current interpreter. */
220 int argc; /* Number of arguments. */
221 char **argv; /* Argument strings. */
222 {
223 Tk_Window tkwin;
224 Blt_Op proc;
225 int result;
226
227 proc = Blt_GetOp(interp, numCbOps, cbOps, BLT_OP_ARG1, argc, argv, 0);
228 if (proc == NULL) {
229 return TCL_ERROR;
230 }
231 tkwin = Tk_MainWindow(interp);
232 result = (*proc) (interp, tkwin, argc, argv);
233 return result;
234 }
235
236 /*
237 *----------------------------------------------------------------------
238 *
239 * Blt_CutbufferInit --
240 *
241 * This procedure is invoked to initialize the "cutbuffer" Tcl
242 * command. See the user documentation for details on what it does.
243 *
244 * Results:
245 * A standard Tcl result.
246 *
247 * Side effects:
248 * None.
249 *
250 *----------------------------------------------------------------------
251 */
252 int
253 Blt_CutbufferInit(interp)
254 Tcl_Interp *interp;
255 {
256 static Blt_CmdSpec cmdSpec =
257 {"cutbuffer", CutbufferCmd,};
258
259 if (Blt_InitCmd(interp, "blt", &cmdSpec) == NULL) {
260 return TCL_ERROR;
261 }
262 return TCL_OK;
263 }
264
265 #endif /* NO_CUTBUFFER */