File: colorEdit.c

package info (click to toggle)
xpaint 2.6.2-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,520 kB
  • ctags: 2,552
  • sloc: ansic: 26,927; makefile: 43; sh: 23
file content (140 lines) | stat: -rw-r--r-- 3,994 bytes parent folder | download | duplicates (4)
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
/* +-------------------------------------------------------------------+ */
/* | Copyright 1993, David Koblas (koblas@netcom.com)		       | */
/* | Copyright 1995, 1996 Torsten Martinsen (bullestock@dk-online.dk)  | */
/* |								       | */
/* | Permission to use, copy, modify, and to distribute this software  | */
/* | and its documentation for any purpose is hereby granted without   | */
/* | fee, provided that the above copyright notice appear in all       | */
/* | copies and that both that copyright notice and this permission    | */
/* | notice appear in supporting documentation.	 There is no	       | */
/* | representations about the suitability of this software for	       | */
/* | any purpose.  this software is provided "as is" without express   | */
/* | or implied warranty.					       | */
/* |								       | */
/* +-------------------------------------------------------------------+ */

/* $Id: colorEdit.c,v 1.4 1997/09/03 19:31:07 torsten Exp $ */

#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include <X11/Shell.h>
#include <X11/Xaw/Form.h>
#include <X11/Xaw/Command.h>
#include "misc.h"
#include "palette.h"
#include "color.h"
#include "protocol.h"

typedef struct {
    XtCallbackProc okProc;
    XtPointer closure;
    Widget wIn;

    Widget shell, pick;
    Palette *map;
    Boolean allowWrite;
    XColor origColor;
} LocalInfo;

static void 
commonCB(LocalInfo * info, XColor * col)
{
    Widget cw = info->wIn;
    XtCallbackProc proc = info->okProc;
    XtPointer closure = info->closure;

    XtDestroyWidget(info->shell);
    XtFree((XtPointer) info);

    proc(cw, closure, (XtPointer) col);
}

static void 
changeBgCancel(Widget w, LocalInfo * info, XtPointer junk2)
{
    if (info->allowWrite)
	PaletteSetPixel(info->map, ColorPickerGetPixel(info->pick),
			&info->origColor);
    commonCB(info, NULL);
}
static void 
changeBgOk(Widget w, LocalInfo * info, XtPointer junk2)
{
    XColor xcol;

    xcol = *ColorPickerGetXColor(info->pick);

    commonCB(info, &xcol);
}

void 
ColorEditor(Widget w, Pixel pixel, Palette * map, Boolean allowWrite,
	    XtCallbackProc okProc, XtPointer closure)
{
    Position x, y;
    Widget shell, form, ok, cancel, pick;
    Pixel pix;
    LocalInfo *info = XtNew(LocalInfo);
    Arg args[6];
    int nargs = 0;
    
    info->okProc = okProc;
    info->closure = closure;
    info->wIn = w;
    info->allowWrite = allowWrite;

    XtVaGetValues(GetShell(w), XtNx, &x, XtNy, &y, NULL);

    XtSetArg(args[nargs], XtNcolormap, map->cmap); nargs++; 
    XtSetArg(args[nargs], XtNx, x+24); nargs++;
    XtSetArg(args[nargs], XtNy, y+24); nargs++;

    shell = XtVisCreatePopupShell("colorEditDialog",
				  transientShellWidgetClass, GetShell(w),
				  args, nargs);

    PaletteAddUser(map, shell);

    form = XtVaCreateManagedWidget("form", formWidgetClass, shell,
				   NULL);

    if (allowWrite) {
	pix = pixel;
    } else {
	pix = PaletteGetUnused(map);
    }
    pick = ColorPickerPalette(form, map, &pix);
    info->origColor = *PaletteLookup(map, pixel);
    ColorPickerSetXColor(pick, &info->origColor);

    ok = XtVaCreateManagedWidget("ok",
				 commandWidgetClass, form,
				 XtNfromVert, pick,
				 XtNtop, XtChainBottom,
				 XtNbottom, XtChainBottom,
				 XtNleft, XtChainLeft,
				 XtNright, XtChainLeft,
				 NULL);

    cancel = XtVaCreateManagedWidget("cancel",
				     commandWidgetClass, form,
				     XtNfromVert, pick,
				     XtNfromHoriz, ok,
				     XtNtop, XtChainBottom,
				     XtNbottom, XtChainBottom,
				     XtNleft, XtChainLeft,
				     XtNright, XtChainLeft,
				     NULL);
    XtAddCallback(cancel, XtNcallback,
		  (XtCallbackProc) changeBgCancel, (XtPointer) info);
    XtAddCallback(ok, XtNcallback,
		  (XtCallbackProc) changeBgOk, (XtPointer) info);
    AddDestroyCallback(shell,
		       (DestroyCallbackFunc) changeBgCancel, (XtPointer) info);

    info->shell = shell;
    info->pick = pick;
    info->map = map;

    XtPopup(shell, XtGrabNone);
}