summaryrefslogtreecommitdiff
path: root/blt/src/bltTreeViewStyle.c
blob: 94239e32314867f15484dec45752bb0d662392cc (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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/*
 * bltTreeViewStyle.c --
 *
 *	This module implements styles for treeview widget cells.
 *
 * Copyright 1998-1999 Lucent Technologies, Inc.
 *
 * Permission to use, copy, modify, and distribute this software and
 * its documentation for any purpose and without fee is hereby
 * granted, provided that the above copyright notice appear in all
 * copies and that both that the copyright notice and warranty
 * disclaimer appear in supporting documentation, and that the names
 * of Lucent Technologies or any of their entities not be used in
 * advertising or publicity pertaining to distribution of the software
 * without specific, written prior permission.
 *
 * Lucent Technologies disclaims all warranties with regard to this
 * software, including all implied warranties of merchantability and
 * fitness.  In no event shall Lucent Technologies be liable for any
 * special, indirect or consequential damages or any damages
 * whatsoever resulting from loss of use, data or profits, whether in
 * an action of contract, negligence or other tortuous action, arising
 * out of or in connection with the use or performance of this
 * software.
 *
 *	The "treeview" widget was created by George A. Howlett.
 */

#include "bltInt.h"

#ifndef NO_TREEVIEW

#include "bltTreeView.h"
#include "bltList.h"
#include <X11/Xutil.h>
#include <X11/Xatom.h>

#define STYLE_TEXT_ENTRY	0
#define STYLE_OPTION_MENU	1
#define STYLE_CHECK_BOX		2

#define DEF_TEXT_ENTRY_SIDE		"left"
#define DEF_TEXT_ENTRY_KEY		(char *)NULL
#define DEF_TEXT_ENTRY_ICON		(char *)NULL
#define DEF_TEXT_ENTRY_BACKGROUND	RGB_WHITE
#define DEF_TEXT_ENTRY_FG_COLOR		STD_COLOR_NORMAL_FG
#define DEF_TEXT_ENTRY_FG_MONO		STD_MONO_NORMAL_FG
#define DEF_TEXT_ENTRY_FONT		STD_FONT
#define DEF_TEXT_ENTRY_HIGHLIGHT_BG_COLOR STD_COLOR_NORMAL_BG
#define DEF_TEXT_ENTRY_HIGHLIGHT_FG_COLOR STD_COLOR_NORMAL_FG
#define DEF_TEXT_ENTRY_NORMAL_BG_COLOR 	STD_COLOR_NORMAL_BG
#define DEF_TEXT_ENTRY_NORMAL_FG_COLOR 	STD_COLOR_NORMAL_FG

extern Blt_CustomOption bltTreeViewImagesOption;

static StyleCreateProc CreateTextEntry, CreateCheckBox, CreateOptionMenu;
static StyleFreeProc FreeTextEntry, FreeCheckBox, FreeOptionMenu;
static StyleDrawProc DrawTextEntry, DrawCheckBox, DrawOptionMenu;
static StyleConfigProc ConfigTextEntry, ConfigCheckBox, ConfigOptionMenu;

typedef struct {
    int refCount;		/* Usage reference count. */
    unsigned int flags;
    char *name;			
    TreeViewStyleClass *classPtr; /* Class-specific routines to manage style. */
    Blt_HashEntry *hashPtr;

    XColor *fgColor;		/* Normal foreground color of cell. */
    XColor *bgColor;		/* Normal background color of cell. */
    XColor *highlightFgColor;	/* Foreground color of cell when
				 * highlighted. */
    XColor *highlightBgColor;	/* Background color of cell when
				 * highlighted. */
    Tk_Font font;	
    TreeViewImage icon;		/* If non-NULL, is a Tk_Image to be drawn
				 * in the cell. */
    int side;			/* Position of the text in relation to
				 * the icon.  */
    Blt_TreeKey key;		/* Actual data resides in this data value */
} TextEntry;

static Blt_ConfigSpec textEntrySpecs[] =
{
    {BLT_CONFIG_BORDER, "-background", "background", "Background",
	DEF_TEXT_ENTRY_NORMAL_BG_COLOR, Blt_Offset(TextEntry, bgColor), 0},
    {BLT_CONFIG_SYNONYM, "-bg", "background", (char *)NULL, (char *)NULL, 
	0, 0},
    {BLT_CONFIG_SYNONYM, "-fg", "foreground", (char *)NULL, (char *)NULL, 
	0, 0},
    {BLT_CONFIG_FONT, "-font", "font", "Font",
	DEF_TEXT_ENTRY_FONT, Blt_Offset(TextEntry, font), 0},
    {BLT_CONFIG_COLOR, "-foreground", "foreground", "Foreground",
	DEF_TEXT_ENTRY_NORMAL_FG_COLOR, Blt_Offset(TextEntry, fgColor),
	BLT_CONFIG_COLOR_ONLY},
    {BLT_CONFIG_COLOR, "-highlightbackground", "highlightBackground",
	"HighlightBackground", DEF_TEXT_ENTRY_HIGHLIGHT_BG_COLOR, 
        Blt_Offset(TextEntry, highlightBgColor), BLT_CONFIG_COLOR_ONLY},
    {BLT_CONFIG_COLOR, "-highlightforeground", "highlightForeground", 
	"HighlightForeground", DEF_TEXT_ENTRY_HIGHLIGHT_FG_COLOR, 
	 Blt_Offset(TextEntry, highlightFgColor), 0},
    {BLT_CONFIG_CUSTOM, "-icon", "icon", "Icon",
	DEF_TEXT_ENTRY_ICON, Blt_Offset(TextEntry, icon), 
	BLT_CONFIG_NULL_OK, &bltTreeViewImagesOption},
    {BLT_CONFIG_STRING, "-key", "key", "key",
	DEF_TEXT_ENTRY_KEY, Blt_Offset(TextEntry, key), 
	BLT_CONFIG_NULL_OK, 0},
    {BLT_CONFIG_SIDE, "-side", "side", "side",
	DEF_TEXT_ENTRY_SIDE, Tk_Offset(TextEntry, side),
	BLT_CONFIG_DONT_SET_DEFAULT},
    {BLT_CONFIG_END, (char *)NULL, (char *)NULL, (char *)NULL,
	(char *)NULL, 0, 0}
};

typedef struct {
    int refCount;		/* Usage reference count. */
    unsigned int flags;
    char *name;			
    TreeViewStyleClass *classPtr; /* Class-specific routines to manage style. */
    Blt_HashEntry *hashPtr;

    XColor *fgColor;		/* Text color of the cell. */
    Tk_3DBorder border;		/* Background color of the cell. */
    TreeViewImage icon;		/* If non-NULL, is a Tk_Image to be drawn
				 * in the cell. */
    int anchor;			/* Position of the text in relation to
				 * the icon.  */
    Blt_TreeKey dataKey;	/* Actual data resides in this data value */

    char *choices;		/* Choices possible. */
    char *defChoice;		/* Default choice. */
    int useScrollbar;
    int scrollWidth;
    int button;
} OptionMenu;

static Blt_ConfigSpec optionMenuSpecs[] =
{
    {BLT_CONFIG_END, (char *)NULL, (char *)NULL, (char *)NULL,
	(char *)NULL, 0, 0}
};

typedef struct {
    int refCount;		/* Usage reference count. */
    unsigned int flags;		/* Contains style type and update flags. */
    char *name;			/* Instance name. */
    TreeViewStyleClass *classPtr; /* Class-specific routines to manage style. */
    Blt_HashEntry *hashPtr;	/* If non-NULL, points to the hash
				 * table entry for the style.  A style
				 * that's been deleted, but still in
				 * use (non-zero reference count) will
				 * have no hash table entry.
				 */
    XColor *fgColor;		/* Text color of the cell. */
    Tk_3DBorder border;		/* Background color of the cell. */
    TreeViewImage icon;		/* If non-NULL, is a Tk_Image to be drawn
				 * in the cell. */
    int anchor;			/* Position of the text in relation to
				 * the icon.  */
    Blt_TreeKey dataKey;	/* Actual data resides in this data value */

    /* Checkbox specific fields. */
    char *onValue;
    char *offValue;
} CheckBox;

static Blt_ConfigSpec checkBoxSpecs[] =
{
    {BLT_CONFIG_END, (char *)NULL, (char *)NULL, (char *)NULL,
	(char *)NULL, 0, 0}
};

static TreeViewStyleClass textEntryClass = {
    textEntrySpecs,
    ConfigTextEntry,
    DrawTextEntry,
    FreeTextEntry,
};

static TreeViewStyleClass checkBoxClass = {
    checkBoxSpecs,
    ConfigCheckBox,
    DrawCheckBox,
    FreeCheckBox,
};

static TreeViewStyleClass optionMenuClass = {
    optionMenuSpecs,
    ConfigOptionMenu,
    DrawOptionMenu,
    FreeOptionMenu,
};

static TreeViewStyle *
CreateTextEntry(tvPtr)
    TreeView *tvPtr;
{
    TextEntry *textEntryPtr;

    textEntryPtr = Blt_Calloc(1, sizeof(TextEntry));
    assert(textEntryPtr);
    textEntryPtr->classPtr = &textEntryClass;
    textEntryPtr->side = SIDE_LEFT;
    return (TreeViewStyle *)textEntryPtr;
}

static void
ConfigTextEntry(tvPtr, stylePtr)
    TreeView *tvPtr;
    TreeViewStyle *stylePtr;
{
}

static void
DrawTextEntry(tvPtr, drawable, entryPtr, stylePtr, valuePtr, x, y)
    TreeView *tvPtr;
    Drawable drawable;
    TreeViewEntry *entryPtr;
    TreeViewStyle *stylePtr;
    TreeViewValue *valuePtr;
    int x, y;
{
    TreeViewColumn *columnPtr = valuePtr->columnPtr;

    if (valuePtr->image != NULL) {
	Tk_RedrawImage(TreeViewImageData(valuePtr->image), 0, 0, 
	       valuePtr->width, valuePtr->height, drawable, x, y);
    } else {
	TextStyle ts;
	XColor *color;

	if (entryPtr->color != NULL) {
	    XSetForeground(tvPtr->display, columnPtr->gc, 
			   entryPtr->color->pixel);
	    color = entryPtr->color;
	} else {
	    color = columnPtr->fgColor;
	}
	Blt_SetDrawTextStyle(&ts, columnPtr->font, columnPtr->gc, color, 
		tvPtr->selFgColor, entryPtr->shadow.color, 0.0, TK_ANCHOR_NW,
	    TK_JUSTIFY_LEFT, 0, entryPtr->shadow.offset);
	Blt_DrawTextLayout(tvPtr->tkwin, drawable, valuePtr->textPtr, 
		&ts, x, y);
	if (entryPtr->color != NULL) {
	    XSetForeground(tvPtr->display, valuePtr->columnPtr->gc, 
		  columnPtr->fgColor->pixel);
	}
    }
}

static void
FreeTextEntry(tvPtr, stylePtr)
    TreeView *tvPtr;
    TreeViewStyle *stylePtr;
{
}

static void
MeasureTextEntry(tvPtr, stylePtr, valuePtr, widthPtr, heightPtr)
    TreeView *tvPtr;
    TreeViewStyle *stylePtr;
    TreeViewValue *valuePtr;
    int *widthPtr, *heightPtr;
{
}

static TreeViewStyle *
CreateCheckBox(tvPtr)
     TreeView *tvPtr;
{
    CheckBox *checkBoxPtr;

    checkBoxPtr = Blt_Malloc(sizeof(CheckBox));
    checkBoxPtr->classPtr = &checkBoxClass;
    return (TreeViewStyle *)checkBoxPtr;
}

static void
ConfigCheckBox(tvPtr, stylePtr)
    TreeView *tvPtr;
    TreeViewStyle *stylePtr;
{
}

static void
DrawCheckBox(tvPtr, drawable, entryPtr, stylePtr, valuePtr, x, y)
    TreeView *tvPtr;
    Drawable drawable;
    TreeViewEntry *entryPtr;
    TreeViewStyle *stylePtr;
    TreeViewValue *valuePtr;
    int x, y;
{
}

static void
FreeCheckBox(tvPtr, stylePtr)
    TreeView *tvPtr;
    TreeViewStyle *stylePtr;
{
}

static void
MeasureCheckBox(tvPtr, stylePtr, valuePtr, widthPtr, heightPtr)
    TreeView *tvPtr;
    TreeViewStyle *stylePtr;
    TreeViewValue *valuePtr;
    int *widthPtr, *heightPtr;
{
}

static TreeViewStyle *
CreateOptionMenu(tvPtr)
     TreeView *tvPtr;
{
    OptionMenu *optionMenuPtr;

    optionMenuPtr = Blt_Malloc(sizeof(OptionMenu));
    optionMenuPtr->classPtr = &optionMenuClass;
    return (TreeViewStyle *)optionMenuPtr;
}

static void
ConfigOptionMenu(tvPtr, stylePtr)
    TreeView *tvPtr;
    TreeViewStyle *stylePtr;
{
}

static void
DrawOptionMenu(tvPtr, drawable, entryPtr, stylePtr, valuePtr, x, y)
    TreeView *tvPtr;
    Drawable drawable;
    TreeViewEntry *entryPtr;
    TreeViewStyle *stylePtr;
    TreeViewValue *valuePtr;
    int x, y;
{
}

static void
FreeOptionMenu(tvPtr, stylePtr)
    TreeView *tvPtr;
    TreeViewStyle *stylePtr;
{
}

static void
MeasureOptionMenu(tvPtr, stylePtr, valuePtr, widthPtr, heightPtr)
    TreeView *tvPtr;
    TreeViewStyle *stylePtr;
    TreeViewValue *valuePtr;
    int *widthPtr, *heightPtr;
{
}

TreeViewStyle *
Blt_TreeViewGetStyle(interp, tvPtr, objPtr, stylePtrPtr)
    Tcl_Interp *interp;
    TreeView *tvPtr;
    Tcl_Obj *objPtr;
    TreeViewStyle *stylePtrPtr;
{
    Blt_HashEntry *hPtr;
    char *string;
    
    string = Tcl_GetString(objPtr);
    hPtr = Blt_FindHashEntry(&tvPtr->styleTable, string);
    if (hPtr == NULL) {
	return NULL;
    }
    return Blt_GetHashValue(hPtr);
}

TreeViewStyle *
Blt_TreeViewCreateStyle(tvPtr, type, name)
     TreeView *tvPtr;		/* TreeView widget. */
     int type;			/* Type of style: either
				 * STYLE_TEXT_ENTRY,
				 * STYLE_OPTION_MENU, or
				 * STYLE_CHECK_BOX */
    char *name;			/* Name of the new style. */
{    
    TreeViewStyle *stylePtr;

    /* Create the new marker based upon the given type */
    switch (type) {
    case STYLE_TEXT_ENTRY:
	stylePtr = CreateTextEntry(tvPtr);
	break;
    case STYLE_OPTION_MENU:
	stylePtr = CreateOptionMenu(tvPtr);
	break;
    case STYLE_CHECK_BOX:
	stylePtr = CreateCheckBox(tvPtr);
	break;
    default:
	return NULL;
    }
    assert(stylePtr);
    stylePtr->name = Blt_Strdup(name);
    stylePtr->flags = type;
    stylePtr->refCount = 0;
    return stylePtr;
}

void
Blt_TreeViewFreeStyle(tvPtr, stylePtr)
    TreeView *tvPtr;
    TreeViewStyle *stylePtr;
{
    stylePtr->refCount--;
    /* Remove the style from the hash table so that it's name can be used.*/
    if (stylePtr->hashPtr != NULL) {
	Blt_DeleteHashEntry(&tvPtr->styleTable, stylePtr->hashPtr);
	stylePtr->hashPtr = NULL; 
    } 
    /* If no cell is using the style, remove it.*/
    if (stylePtr->refCount <= 0) {
	(*stylePtr->classPtr->freeProc)(tvPtr, stylePtr); 
    } 
}

#endif /* NO_TREEVIEW */