1 /*
2  * tixGrid.h --
3  *
4  *	Defines main data structures for tixGrid
5  *
6  * Copyright (c) 1993-1999 Ioi Kim Lam.
7  * Copyright (c) 2000      Tix Project Group.
8  *
9  * See the file "license.terms" for information on usage and redistribution
10  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
11  *
12  * $Id: tixGrid.h,v 1.3 2004/03/28 02:44:56 hobbs Exp $
13  */
14 
15 #ifndef _TIX_GRID_H_
16 #define _TIX_GRID_H_
17 
18 #ifndef _TIX_GRID_DATA_H_
19 #include "tixGrData.h"
20 #endif
21 
22 #define TIX_X 0
23 #define TIX_Y 1
24 
25 
26 #define TIX_S_MARGIN 0
27 #define TIX_X_MARGIN 1
28 #define TIX_Y_MARGIN 2
29 #define TIX_MAIN     3
30 
31 #define TIX_SITE_NONE -1
32 
33 typedef struct TixGrEntry {
34     Tix_DItem * iPtr;
35     Tcl_HashEntry * entryPtr[2];	/* The index of this entry in the
36 					 * row/col tables */
37 } TixGrEntry;
38 
39 /*----------------------------------------------------------------------
40  * 			Render Block
41  *
42  * Before the Grid is rendered, information is filled into a pseudo 2D
43  * array of RenderBlockElem's:
44  *
45  *	(1) entries are placed in the appropriate (x,y) locations
46  *	(2) background and borders are formatted according
47  *	(3) highlights are formatted.
48  *
49  * The widget is redrawn using the render-block. This saves reformatting
50  * the next time the widget is exposed.
51  *----------------------------------------------------------------------
52  */
53 typedef struct RenderBlockElem {
54     TixGrEntry * chPtr;		/* not allocated, don't need to free */
55     int borderW[2][2];
56     int index[2];
57 
58     unsigned int selected : 1;
59     unsigned int filled : 1;
60 } RenderBlockElem;
61 
62 
63 /* ElmDispSize --
64  *
65  *	This structure stores the size information of the visible
66  *	rows (RenderBlock.dispSize[0][...]) and columns
67  *	(RenderBlock.dispSize[1][...])
68  */
69 typedef struct ElmDispSize {
70     int preBorder;
71     int size;
72     int postBorder;
73 
74     int total;		/* simple the sum of the above */
75 } ElmDispSize;
76 
77 typedef struct RenderBlock {
78     int size[2];		/* num of rows and cols in the render block */
79 
80     RenderBlockElem **elms;   	/* An Malloc'ed pseudo 2D array (you can do
81 				 * things like elms[0][0]), Used for the
82 				 * main body of the Grid.
83 				 */
84     ElmDispSize *dispSize[2];	/* (dispSizes[0][x], dispSizes[1][y])
85 				 * will be the dimension of the element (x,y)
86 				 * displayed on the screen (may be bigger
87 				 * or smaller than its desired size). */
88     int visArea[2];		/* visible area (width times height) of
89 				 * the visible cells on the screen */
90 }  RenderBlock;
91 
92 /*----------------------------------------------------------------------
93  * 			RenderInfo
94  *
95  * This stores information for rendering from the RB into an X drawable.
96  *
97  *----------------------------------------------------------------------
98  */
99 typedef struct RenderInfo {
100     Drawable drawable;
101     int origin[2];
102     int offset[2];
103     int size[2];		/* width and height of the area to draw
104 				 * (number of pixels starting from the offset)
105 				 * if offset = (2,2) and size = (5,5) we have
106 				 * to draw the rectangle ((2,2), (6,6));
107 				 */
108     struct {			/* the current valid grid area for the */
109 	int x1, x2, y1, y2;	/* "format" command */
110 	int whichArea;
111     } fmt;
112 } RenderInfo;
113 
114 typedef struct ExposedArea {
115     int x1, y1, x2, y2;
116 } ExposedArea, Rect;
117 
118 /*----------------------------------------------------------------------
119  * 			ColorInfo
120  *
121  * These colors are used by the format commands. They must be saved
122  * or otherwise the colormap may be changed ..
123  *----------------------------------------------------------------------
124  */
125 typedef struct ColorInfo {
126     struct ColorInfo * next;
127     int counter;
128     int type;			/* TK_CONFIG_BORDER or TK_CONFIG_COLOR */
129     long pixel;
130     Tk_3DBorder border;
131     XColor * color;
132 } ColorInfo;
133 
134 /*----------------------------------------------------------------------
135  * 			SelectBlock
136  *
137  * These structures are arranged in a list and are used to determine
138  * where a cell is selected.
139  *----------------------------------------------------------------------
140  */
141 #define TIX_GR_CLEAR		1
142 #define TIX_GR_SET		2
143 #define TIX_GR_TOGGLE		3
144 
145 #define TIX_GR_MAX		0x7fffffff
146 
147 #define TIX_GR_RESIZE		1
148 #define TIX_GR_REDRAW		2
149 
150 
151 typedef struct SelectBlock {
152     struct SelectBlock * next;
153     int range[2][2];		/* the top left and bottom right corners */
154     int type;			/* TIX_GR_CLEAR, TIX_GR_SET,
155 				 * TIX_GR_TOGGLE
156 				 *
157 				 * If several SelectBlock covers the same
158 				 * cell, the last block in the wPtr->selList
159 				 * determines whether this cell is selected
160 				 * or not */
161 } SelectBlock;
162 
163 /*----------------------------------------------------------------------
164  * 			GrSortItem
165  *
166  * Used to sort the items in the grid
167  *----------------------------------------------------------------------
168  */
169 typedef struct Tix_GrSortItem {
170     char * data;			/* is usually a string, but
171 					 * can be a pointer to an
172 					 * arbitrary data in C API */
173     int index;				/* row or column */
174 } Tix_GrSortItem;
175 
176 /*----------------------------------------------------------------------
177  * Data structure for iterating the cells inside the grid.
178  *
179  *----------------------------------------------------------------------
180  */
181 
182 typedef struct Tix_GrDataRowSearch {
183     struct TixGridRowCol * row;
184     Tcl_HashSearch hashSearch;
185     Tcl_HashEntry *hashPtr;
186 } Tix_GrDataRowSearch;
187 
188 typedef struct Tix_GrDataCellSearch {
189     char * data;
190     Tcl_HashSearch hashSearch;
191     Tcl_HashEntry *hashPtr;
192 } Tix_GrDataCellSearch;
193 
194 /*----------------------------------------------------------------------
195  *
196  *	        Main data structure of the grid widget.
197  *
198  *----------------------------------------------------------------------
199  */
200 typedef struct Tix_GridScrollInfo {
201     char * command;
202 
203     int max;		/* total size (width or height) of the widget*/
204     int offset;		/* The top/left side of the scrolled widget */
205     int unit;		/* How much should we scroll when the user */
206 
207     double window;	/* visible size, percentage of the total */
208 }Tix_GridScrollInfo;
209 
210 
211 typedef struct GridStruct {
212     Tix_DispData dispData;
213 
214     Tcl_Command widgetCmd;	/* Token for button's widget command. */
215 
216     /*
217      * Information used when displaying widget:
218      */
219     int reqSize[2];		/* For app programmer to request size */
220 
221     /*
222      * Information used when displaying widget:
223      */
224 
225     /* Border and general drawing */
226     int borderWidth;		/* Width of 3-D borders. */
227     int selBorderWidth;		/* Width of 3-D borders for selected items */
228     int relief;			/* Indicates whether window as a whole is
229 				 * raised, sunken, or flat. */
230     Tk_3DBorder border;		/* Used for drawing the 3d border. */
231     Tk_3DBorder selectBorder;	/* Used for selected background. */
232     XColor *normalFg;		/* Normal foreground for text. */
233     XColor *normalBg;		/* Normal background for  text. */
234     XColor *selectFg;		/* Color for drawing selected text. */
235 
236     Tk_Uid state;		/* State can only be normal or disabled. */
237 
238        /* GC and stuff */
239     GC backgroundGC;		/* GC for drawing background. */
240     GC selectGC;		/* GC for drawing selected background. */
241     GC anchorGC;		/* GC for drawing dotted anchor highlight. */
242     TixFont font;		/* Default font used by the DItems. */
243 
244     /* Text drawing */
245     Cursor cursor;		/* Current cursor for window, or None. */
246 
247     /* For highlights */
248     int highlightWidth;		/* Width in pixels of highlight to draw
249 				 * around widget when it has the focus.
250 				 * <= 0 means don't draw a highlight. */
251     int bdPad;			/* = highlightWidth + borderWidth */
252     XColor *highlightColorPtr;	/* Color for drawing traversal highlight. */
253     GC highlightGC;		/* For drawing traversal highlight. */
254 
255     /*
256      * default pad and gap values
257      */
258     int padX, padY;
259 
260     Tk_Uid selectMode;		/* Selection style: single, browse, multiple,
261 				 * or extended.  This value isn't used in C
262 				 * code, but the Tcl bindings use it. */
263     Tk_Uid selectUnit;		/* Selection unit: cell, row or column.
264 				 * This value isn't used in C
265 				 * code, but the Tcl bindings use it. */
266 
267     /*
268      * The following three sites are used according to the -selectunit.
269      * if selectunit is: "cell", [0] and [1] are used; "row", only [0]
270      * is used; "column", only [1] is used
271      */
272     int anchor[2];		/* The current anchor unit */
273     int dropSite[2];		/* The current drop site */
274     int dragSite[2];		/* The current drop site */
275 
276     /*
277      * Callback commands.
278      */
279     char *command;		/* The command when user double-clicks */
280     char *browseCmd;		/* The command to call when the selection
281 				 * changes. */
282     char *editNotifyCmd;	/* The command to call to determine whether
283 				 * a cell is editable. */
284     char *editDoneCmd;		/* The command to call when an entry has
285 				 * been edited by the user.*/
286     char *formatCmd;		/* The command to call when the Grid widget
287 				 * needs to be reformatted (e.g, Exposure
288 				 * events or when contents have been
289 				 * changed). */
290     char *sizeCmd;		/* The command to call when the size of
291 				 * the listbox changes. E.g., when the user
292 				 * add/deletes elements. Useful for auto-
293 				 * scrollbar geometry managers */
294 
295     /*
296      * Info for lay-out
297      */
298     char *takeFocus;		/* Value of -takefocus option;  not used in
299 				 * the C code, but used by keyboard traversal
300 				 * scripts.  Malloc'ed, but may be NULL. */
301 
302     int serial;			/* this number is incremented before each time
303 				 * the widget is redisplayed */
304 
305     TixGridDataSet * dataSet;
306     RenderBlock * mainRB;	/* Malloc'ed */
307 
308     int hdrSize[2];		/* number of rows (height of x header, index
309 				 * [0]) and columns (width of y header, index
310 				 * [1]) */
311     int floatRange[2];		/* Are the num of columns and rows floated?
312 				 * (if floated, you can scroll past the max
313 				 * element).*/
314     int gridSize[2];		/* the size of the grid where there is data */
315     Tix_DItemInfo * diTypePtr;	/* Default item type */
316     ExposedArea expArea;
317 
318     RenderInfo * renderInfo;	/* only points to stuff in stack */
319     Tix_GridScrollInfo scrollInfo[2];
320     int fontSize[2];		/* size of the "0" char of the -font option
321 				 */
322     TixGridSize defSize[2];
323     Tix_LinkList colorInfo;
324     Tix_LinkList selList;
325     Tix_LinkList mappedWindows;
326     int colorInfoCounter;
327 
328     unsigned int hasFocus  : 1;
329 
330     unsigned int idleEvent : 1;
331     unsigned int toResize  : 1;		/* idle event */
332     unsigned int toRedraw : 1;		/* idle event */
333 
334     unsigned int toResetRB  : 1; /* Do we need to reset the render block */
335     unsigned int toComputeSel  : 1;
336     unsigned int toRedrawHighlight : 1;
337 } Grid;
338 
339 typedef Grid   WidgetRecord;
340 typedef Grid * WidgetPtr;
341 
342 /*
343  * common functions
344  */
345 
346 EXTERN void		Tix_GrAddChangedRect _ANSI_ARGS_((
347 			    WidgetPtr wPtr, int changedRect[2][2],
348 			    int isSite));
349 EXTERN int		Tix_GrConfigSize _ANSI_ARGS_((Tcl_Interp *interp,
350 			    WidgetPtr wPtr, int argc, CONST84 char **argv,
351 			    TixGridSize *sizePtr, CONST84 char * argcErrorMsg,
352 			    int *changed_ret));
353 EXTERN void		Tix_GrDoWhenIdle _ANSI_ARGS_((WidgetPtr wPtr,
354 			    int type));
355 EXTERN void		Tix_GrCancelDoWhenIdle _ANSI_ARGS_((WidgetPtr wPtr));
356 EXTERN void		Tix_GrFreeElem _ANSI_ARGS_((TixGrEntry * chPtr));
357 EXTERN void		Tix_GrFreeUnusedColors _ANSI_ARGS_((WidgetPtr wPtr,
358 			    int freeAll));
359 EXTERN void		Tix_GrScrollPage _ANSI_ARGS_((WidgetPtr wPtr,
360 			    int count, int axis));
361 
362 /*
363  * The dataset functions
364  */
365 
366 EXTERN int		TixGridDataConfigRowColSize _ANSI_ARGS_((
367 			    Tcl_Interp * interp, WidgetPtr wPtr,
368 			    TixGridDataSet * dataSet, int which, int index,
369 			    int argc, CONST84 char ** argv, CONST84 char * argcErrorMsg,
370 			    int *changed_ret));
371 EXTERN CONST84 char *	TixGridDataCreateEntry _ANSI_ARGS_((
372 			    TixGridDataSet * dataSet, int x, int y,
373 			    CONST84 char * defaultEntry));
374 EXTERN int		TixGridDataDeleteEntry _ANSI_ARGS_((
375 			    TixGridDataSet * dataSet, int x, int y));
376 EXTERN void		TixGridDataDeleteRange _ANSI_ARGS_((WidgetPtr wPtr,
377 			    TixGridDataSet * dataSet, int which,
378 			    int from, int to));
379 EXTERN void 		TixGridDataDeleteSearchedEntry _ANSI_ARGS_((
380 			    Tix_GrDataCellSearch * cellSearchPtr));
381 EXTERN char *		TixGridDataFindEntry _ANSI_ARGS_((
382 			    TixGridDataSet * dataSet, int x, int y));
383 EXTERN int		TixGrDataFirstCell _ANSI_ARGS_((
384 			    Tix_GrDataRowSearch * rowSearchPtr,
385 			    Tix_GrDataCellSearch * cellSearchPtr));
386 EXTERN int		TixGrDataFirstRow _ANSI_ARGS_((
387 			    TixGridDataSet* dataSet,
388 			    Tix_GrDataRowSearch * rowSearchPtr));
389 EXTERN int		TixGridDataGetRowColSize _ANSI_ARGS_((
390 			    WidgetPtr wPtr, TixGridDataSet * dataSet,
391 			    int which, int index, TixGridSize * defSize,
392 			    int *pad0, int * pad1));
393 EXTERN void		TixGridDataGetGridSize _ANSI_ARGS_((
394 			    TixGridDataSet * dataSet, int *width_ret,
395 			    int *height_ret));
396 EXTERN int		TixGridDataGetIndex _ANSI_ARGS_((
397 			    Tcl_Interp * interp, WidgetPtr wPtr,
398 			    CONST84 char * xStr, CONST84 char * yStr,
399 			    int * xPtr, int * yPtr));
400 EXTERN void 		TixGridDataInsert _ANSI_ARGS_((
401 			    TixGridDataSet * dataSet,
402 			    int x, int y, ClientData data));
403 EXTERN void		TixGridDataMoveRange _ANSI_ARGS_((WidgetPtr wPtr,
404 			    TixGridDataSet * dataSet, int which,
405 			    int from, int to, int by));
406 EXTERN int		TixGrDataNextCell _ANSI_ARGS_((
407 			    Tix_GrDataCellSearch * cellSearchPtr));
408 EXTERN int		TixGrDataNextRow _ANSI_ARGS_((
409 			    Tix_GrDataRowSearch * rowSearchPtr));
410 EXTERN TixGridDataSet*	TixGridDataSetInit _ANSI_ARGS_((void));
411 EXTERN void		TixGridDataSetFree _ANSI_ARGS_((
412 			    TixGridDataSet* dataSet));
413 EXTERN int		TixGridDataUpdateSort _ANSI_ARGS_((
414 			    TixGridDataSet * dataSet, int axis,
415 			    int start, int end, Tix_GrSortItem *items));
416 
417 #endif /*_TIX_GRID_H_*/
418