1 /*
2 * Copyright (c) 2005-2010 Hypertriton, Inc. <http://hypertriton.com/>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
18 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
21 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
23 * USE OF THIS SOFTWARE EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26 #include <agar/core/core.h>
27 #include <agar/gui/separator.h>
28 #include <agar/gui/window.h>
29 #include <agar/gui/primitive.h>
30
31 AG_Separator *
AG_SeparatorNew(void * parent,enum ag_separator_type type)32 AG_SeparatorNew(void *parent, enum ag_separator_type type)
33 {
34 AG_Separator *sep;
35
36 sep = Malloc(sizeof(AG_Separator));
37 AG_ObjectInit(sep, &agSeparatorClass);
38 sep->type = type;
39 sep->visible = 1;
40
41 if (type == AG_SEPARATOR_HORIZ) {
42 AG_ExpandHoriz(sep);
43 } else {
44 AG_ExpandVert(sep);
45 }
46 AG_ObjectAttach(parent, sep);
47 return (sep);
48 }
49
50 AG_Separator *
AG_SpacerNew(void * parent,enum ag_separator_type type)51 AG_SpacerNew(void *parent, enum ag_separator_type type)
52 {
53 AG_Separator *sep;
54
55 sep = Malloc(sizeof(AG_Separator));
56 AG_ObjectInit(sep, &agSeparatorClass);
57 sep->type = type;
58 sep->visible = 0;
59
60 if (type == AG_SEPARATOR_HORIZ) {
61 AG_ExpandHoriz(sep);
62 } else {
63 AG_ExpandVert(sep);
64 }
65 AG_ObjectAttach(parent, sep);
66 return (sep);
67 }
68
69 static void
Init(void * obj)70 Init(void *obj)
71 {
72 AG_Separator *sep = obj;
73
74 sep->type = AG_SEPARATOR_HORIZ;
75 sep->padding = 4;
76 sep->visible = 1;
77 }
78
79 static void
SizeRequest(void * obj,AG_SizeReq * r)80 SizeRequest(void *obj, AG_SizeReq *r)
81 {
82 AG_Separator *sep = obj;
83
84 r->w = sep->padding*2 + 2;
85 r->h = sep->padding*2 + 2;
86 }
87
88 static int
SizeAllocate(void * obj,const AG_SizeAlloc * a)89 SizeAllocate(void *obj, const AG_SizeAlloc *a)
90 {
91 AG_Separator *sep = obj;
92
93 if (a->w < sep->padding*2 + 2 ||
94 a->h < sep->padding*2 + 2) {
95 return (-1);
96 }
97 return (0);
98 }
99
100 static void
Draw(void * obj)101 Draw(void *obj)
102 {
103 AG_Separator *sep = obj;
104 AG_Color c[2];
105
106 if (!sep->visible)
107 return;
108
109 c[0] = AG_ColorShift(WCOLOR(sep,0), agLowColorShift);
110 c[1] = AG_ColorShift(WCOLOR(sep,0), agHighColorShift);
111
112 switch (sep->type) {
113 case AG_SEPARATOR_HORIZ:
114 AG_DrawLineH(sep, 0, WIDTH(sep), sep->padding, c[0]);
115 AG_DrawLineH(sep, 0, WIDTH(sep), sep->padding+1, c[1]);
116 break;
117 case AG_SEPARATOR_VERT:
118 AG_DrawLineV(sep, sep->padding, 0, HEIGHT(sep), c[0]);
119 AG_DrawLineV(sep, sep->padding+1, 0, HEIGHT(sep), c[1]);
120 break;
121 }
122 }
123
124 void
AG_SeparatorSetPadding(AG_Separator * sep,Uint pixels)125 AG_SeparatorSetPadding(AG_Separator *sep, Uint pixels)
126 {
127 AG_ObjectLock(sep);
128 sep->padding = pixels;
129 AG_ObjectUnlock(sep);
130 AG_Redraw(sep);
131 }
132
133 AG_WidgetClass agSeparatorClass = {
134 {
135 "Agar(Widget:Separator)",
136 sizeof(AG_Separator),
137 { 0,0 },
138 Init,
139 NULL, /* free */
140 NULL, /* destroy */
141 NULL, /* load */
142 NULL, /* save */
143 NULL /* edit */
144 },
145 Draw,
146 SizeRequest,
147 SizeAllocate
148 };
149