summaryrefslogtreecommitdiff
path: root/tix/generic/tixGrSel.c
blob: 9f45de685079947c0b05ccb627b6ea35afbea5a4 (plain)
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/* 
 * tixGrSel.c --
 *
 *	This module handles the selection
 *
 * Copyright (c) 1996, Expert Interface Technologies
 *
 * See the file "license.terms" for information on usage and redistribution
 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
 *
 */

#include <tixPort.h>
#include <tixInt.h>
#include <tixDef.h>
#include <tixGrid.h>

EXTERN TIX_DECLARE_SUBCMD(Tix_GrSelection);
static TIX_DECLARE_SUBCMD(Tix_GrSelIncludes);
static TIX_DECLARE_SUBCMD(Tix_GrSelModify);

static int		Tix_GrSelIncludes _ANSI_ARGS_((ClientData clientData,
			    Tcl_Interp *interp, int argc, char **argv));
static void		Tix_GrAdjustSelection _ANSI_ARGS_((
			    WidgetPtr wPtr, SelectBlock * sbPtr));
static void		Tix_GrMergeSelection _ANSI_ARGS_((
			    WidgetPtr wPtr, SelectBlock * sbPtr));
static int 		Intersect _ANSI_ARGS_((SelectBlock * s1,
			    SelectBlock * s2));
static int 		Include _ANSI_ARGS_((SelectBlock * s1,
			    SelectBlock * s2));

int
Tix_GrSelection(clientData, interp, argc, argv)
    ClientData clientData;
    Tcl_Interp *interp;		/* Current interpreter. */
    int argc;			/* Number of arguments. */
    char **argv;		/* Argument strings. */
{
    static Tix_SubCmdInfo subCmdInfo[] = {
	{TIX_DEFAULT_LEN, "adjust",   2, 4, Tix_GrSelModify,
	   "x1 y1 ?x2 y2?"},
	{TIX_DEFAULT_LEN, "clear",    2, 4, Tix_GrSelModify,
	   "x1 y1 ?x2 y2?"},
	{TIX_DEFAULT_LEN, "includes", 2, 4, Tix_GrSelIncludes,
	   "x1 y1 ?x2 y2?"},
	{TIX_DEFAULT_LEN, "set",      2, 4, Tix_GrSelModify,
	   "x1 y1 ?x2 y2?"},
	{TIX_DEFAULT_LEN, "toggle",   2, 4, Tix_GrSelModify,
	   "x1 y1 ?x2 y2?"},
    };
    static Tix_CmdInfo cmdInfo = {
	Tix_ArraySize(subCmdInfo), 1, TIX_VAR_ARGS, "?option? ?arg ...?",
    };

    return Tix_HandleSubCmds(&cmdInfo, subCmdInfo, clientData,
	interp, argc+1, argv-1);
}

static int
Tix_GrSelIncludes(clientData, interp, argc, argv)
    ClientData clientData;
    Tcl_Interp *interp;		/* Current interpreter. */
    int argc;			/* Number of arguments. */
    char **argv;		/* Argument strings. */
{
#if 0
    WidgetPtr wPtr = (WidgetPtr) clientData;
#endif
    return TCL_OK;
}

static int Intersect(s1, s2)
    SelectBlock * s1;
    SelectBlock * s2;
{
    return 0;
}

static int Include(s1, s2)
    SelectBlock * s1;
    SelectBlock * s2;
{
    return 0;
}

static void
Tix_GrMergeSelection(wPtr, sbPtr)
    WidgetPtr wPtr;
    SelectBlock * sbPtr;
{
    Tix_ListIterator li;

    switch (sbPtr->type) {
      case TIX_GR_SET:
      case TIX_GR_CLEAR:
	if (sbPtr->range[0][0] == 0 &&
	    sbPtr->range[1][0] == 0 &&
	    sbPtr->range[0][1] == TIX_GR_MAX &&
	    sbPtr->range[1][1] == TIX_GR_MAX) {

	    /* clear everything else from the list
	     */
	    Tix_SimpleListIteratorInit(&li);

	    for (Tix_SimpleListStart(&wPtr->selList, &li);
		 !Tix_SimpleListDone(&li);
		 Tix_SimpleListNext (&wPtr->selList, &li)) {

		SelectBlock *ptr = (SelectBlock *)li.curr;
		Tix_SimpleListDelete(&wPtr->selList, &li);
		ckfree((char*)ptr);
	    }
	}
	if (sbPtr->type == TIX_GR_SET) {
	    Tix_SimpleListAppend(&wPtr->selList, (char*)sbPtr, 0);
	}
	goto done;
    }

#if 0

    switch (sbPtr->type) {
        case TIX_GR_TOGGLE: {
        }
	break;
        case TIX_GR_SET: {
	  Tix_SimpleListAppend(&wPtr->selList, (char*)sbPtr, 0);
        }
        break;
        case TIX_GR_CLEAR: {
	    Tix_SimpleListIteratorInit(&li);

	    for (Tix_SimpleListStart(&wPtr->selList, &li);
		 !Tix_SimpleListDone(&li);
		 Tix_SimpleListNext (&wPtr->selList, &li)) {
	    }
        }
	  
    }
#else

    Tix_SimpleListAppend(&wPtr->selList, (char*)sbPtr, 0);

#endif

  done:
    Tix_GrAddChangedRect(wPtr, sbPtr->range, 0);
}

static void
Tix_GrAdjustSelection(wPtr, sbPtr)
    WidgetPtr wPtr;
    SelectBlock * sbPtr;
{
    int changed[2][2];
    SelectBlock * current;

    current = (SelectBlock*)wPtr->selList.tail;

    /*
     * The changed region is the union of the area of the current selection
     * and the adjusted selection.
     */
    changed[TIX_X][0] = sbPtr->range[TIX_X][0];
    changed[TIX_X][1] = sbPtr->range[TIX_X][1];
    changed[TIX_Y][0] = sbPtr->range[TIX_Y][0];
    changed[TIX_Y][1] = sbPtr->range[TIX_Y][1];

    if (changed[TIX_X][0] > current->range[TIX_X][0]) {
	changed[TIX_X][0] = current->range[TIX_X][0];
    }
    if (changed[TIX_X][1] < current->range[TIX_X][1]) {
	changed[TIX_X][1] = current->range[TIX_X][1];
    }
    if (changed[TIX_Y][0] > current->range[TIX_Y][0]) {
	changed[TIX_Y][0] = current->range[TIX_Y][0];
    }
    if (changed[TIX_Y][1] < current->range[TIX_Y][1]) {
	changed[TIX_Y][1] = current->range[TIX_Y][1];
    }

    /* Adjust the current selection according to sbPtr */
    current->range[TIX_X][0] = sbPtr->range[TIX_X][0];
    current->range[TIX_X][1] = sbPtr->range[TIX_X][1];
    current->range[TIX_Y][0] = sbPtr->range[TIX_Y][0];
    current->range[TIX_Y][1] = sbPtr->range[TIX_Y][1];

    /* Set the changed area */
    Tix_GrAddChangedRect(wPtr, changed, 0);

    /* sbPtr is no longer needed */
    ckfree((char*)sbPtr);
}

static int
Tix_GrSelModify(clientData, interp, argc, argv)
    ClientData clientData;
    Tcl_Interp *interp;		/* Current interpreter. */
    int argc;			/* Number of arguments. */
    char **argv;		/* Argument strings. */
{
    WidgetPtr wPtr = (WidgetPtr) clientData;
    int type, adjust = 0;
    SelectBlock * sbPtr = NULL;
    int tmp;

    if (argc != 2 && argc != 4) {
	return Tix_ArgcError(interp, argc+2, argv-2, 2, "x1 y1 ?x2 y2?");
    }

    /*
     * (1) find out the type of operation.
     */
    if (argv[-1][0] == 'a') {
	if (wPtr->selList.numItems <= 0) {
	    /*
	     * There is nothing in the selection list to adjust!
	     */
	    Tcl_AppendResult(interp, "selection list is empty", NULL);
	    return TCL_ERROR;
	}
	adjust = 1;
    }
    else if (argv[-1][0] == 'c') {
	type = TIX_GR_CLEAR;
    }
    else if (argv[-1][0] == 's') {
	type = TIX_GR_SET;
    }
    else {
	type = TIX_GR_TOGGLE;
    }

    sbPtr = (SelectBlock*)ckalloc(sizeof(SelectBlock));
    sbPtr->type = type;

    if (Tcl_GetInt(interp, argv[0], &sbPtr->range[0][0]) != TCL_OK) {
	goto error;
    }
    if (Tcl_GetInt(interp, argv[1], &sbPtr->range[1][0]) != TCL_OK) {
	goto error;
    }
    if (argc == 4) {
	if (Tcl_GetInt(interp, argv[2], &sbPtr->range[0][1]) != TCL_OK) {
	    if (strcmp(argv[2], "max") == 0) {
		Tcl_ResetResult(interp);
		sbPtr->range[0][1] = TIX_GR_MAX;
	    } else {
		goto error;
	    }
	}
	if (Tcl_GetInt(interp, argv[3], &sbPtr->range[1][1]) != TCL_OK) {
	    if (strcmp(argv[3], "max") == 0) {
		Tcl_ResetResult(interp);
		sbPtr->range[1][1] = TIX_GR_MAX;
	    } else {
		goto error;
	    }
	}
    } else {
	sbPtr->range[0][1] = sbPtr->range[0][0];
	sbPtr->range[1][1] = sbPtr->range[1][0];
    }

    if (wPtr->selectUnit != tixRowUid) {
	if (sbPtr->range[0][0] > sbPtr->range[0][1]) {
	    tmp = sbPtr->range[0][1];
	    sbPtr->range[0][1] = sbPtr->range[0][0]; 
	    sbPtr->range[0][0] = tmp;
	}
    } else {
	sbPtr->range[0][0] = 0;
	sbPtr->range[0][1] = TIX_GR_MAX;
    }

    if (wPtr->selectUnit != tixColumnUid) {
	if (sbPtr->range[1][0] > sbPtr->range[1][1]) {
	    tmp = sbPtr->range[1][1];
	    sbPtr->range[1][1] = sbPtr->range[1][0]; 
	    sbPtr->range[1][0] = tmp;
	}
    } else {
	sbPtr->range[1][0] = 0;
	sbPtr->range[1][1] = TIX_GR_MAX;
    }

    if (adjust) {
	Tix_GrAdjustSelection(wPtr, sbPtr);
	sbPtr = NULL;
    } else {
	Tix_GrMergeSelection(wPtr, sbPtr);
    }
    wPtr->toComputeSel = 1;
    return TCL_OK;

  error:
    if (sbPtr) {
	ckfree((char*)sbPtr);
    }
    return TCL_ERROR;
}