1 /*
2  * Copyright (c) 2009 Mat Sutcliffe (oktal@gmx.co.uk)
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 "EXTERN.h"
27 #include "perl.h"
28 #include "XSUB.h"
29 
30 #include <agar/core.h>
31 #include <agar/gui.h>
32 #include "perl_agar.h"
33 
34 static const AP_FlagNames flagNames[] = {
35 	{ "homogenous",		AG_TOOLBAR_HOMOGENOUS },
36 	{ "sticky",		AG_TOOLBAR_STICKY },
37 	{ "multiSticky",	AG_TOOLBAR_MULTI_STICKY },
38 	{ NULL,			0 }
39 };
40 
41 MODULE = Agar::Toolbar		PACKAGE = Agar::Toolbar		PREFIX = AG_
42 PROTOTYPES: ENABLE
43 VERSIONCHECK: DISABLE
44 
45 Agar::Toolbar
46 newHoriz(package, parent, numRows, ...)
47 	const char * package
48 	Agar::Widget parent
49 	int numRows
50 PREINIT:
51 	Uint flags = 0, wflags = 0;
52 CODE:
53 	if ((items == 4 && SvTYPE(SvRV(ST(3))) != SVt_PVHV) || items > 4) {
54 		Perl_croak(aTHX_ "Usage: Agar::Toolbar->newHoriz(parent,rows,[{opts}])");
55 	}
56 	if (items == 4) {
57 		AP_MapHashToFlags(SvRV(ST(3)), flagNames, &flags);
58 		AP_MapHashToFlags(SvRV(ST(3)), AP_WidgetFlagNames, &wflags);
59 	}
60 	RETVAL = AG_ToolbarNew(parent, AG_TOOLBAR_HORIZ, numRows, flags);
61 	AGWIDGET(&(RETVAL->box))->flags |= wflags;
62 OUTPUT:
63 	RETVAL
64 
65 Agar::Toolbar
66 newVert(package, parent, numRows, ...)
67 	const char * package
68 	Agar::Widget parent
69 	int numRows
70 PREINIT:
71 	Uint flags = 0, wflags = 0;
72 CODE:
73 	if ((items == 4 && SvTYPE(SvRV(ST(3))) != SVt_PVHV) || items > 4) {
74 		Perl_croak(aTHX_ "Usage: Agar::Toolbar->newVert(parent,rows,[{opts}])");
75 	}
76 	if (items == 4) {
77 		AP_MapHashToFlags(SvRV(ST(3)), flagNames, &flags);
78 		AP_MapHashToFlags(SvRV(ST(3)), AP_WidgetFlagNames, &wflags);
79 	}
80 	RETVAL = AG_ToolbarNew(parent, AG_TOOLBAR_VERT, numRows, flags);
81 	AGWIDGET(&(RETVAL->box))->flags |= wflags;
82 OUTPUT:
83 	RETVAL
84 
85 void
86 setActiveRow(self, row)
87 	Agar::Toolbar self
88 	int row
89 CODE:
90 	AG_ToolbarRow(self, row);
91 
92 Agar::Button
93 addTextButton(self, text)
94 	Agar::Toolbar self
95 	const char * text
96 CODE:
97 	RETVAL = AG_ToolbarButton(self, text, 0, NULL, "");
98 OUTPUT:
99 	RETVAL
100 
101 Agar::Button
102 addIconButton(self, surface)
103 	Agar::Toolbar self
104 	Agar::Surface surface
105 CODE:
106 	RETVAL = AG_ToolbarButtonIcon(self, AG_SurfaceDup(surface), 0, NULL, "");
107 
108 void
109 select(self, button)
110 	Agar::Toolbar self
111 	Agar::Button button
112 CODE:
113 	AG_ToolbarSelect(self, button);
114 
115 void
116 deselect(self, button)
117 	Agar::Toolbar self
118 	Agar::Button button
119 CODE:
120 	AG_ToolbarDeselect(self, button);
121 
122 void
123 selectOnly(self, button)
124 	Agar::Toolbar self
125 	Agar::Button button
126 CODE:
127 	AG_ToolbarSelectOnly(self, button);
128 
129 void
130 selectAll(self)
131 	Agar::Toolbar self
132 CODE:
133 	AG_ToolbarSelectAll(self);
134 
135 void
136 deselectAll(self)
137 	Agar::Toolbar self
138 CODE:
139 	AG_ToolbarDeselectAll(self);
140 
141 Agar::Box
142 getRow(self, index)
143 	Agar::Toolbar self
144 	Uint index
145 CODE:
146 	if (index >= self->nRows) {
147 		Perl_croak(aTHX_ "Row index out of bounds");
148 	}
149 	RETVAL = self->rows[index];
150 OUTPUT:
151 	RETVAL
152 
153 void
154 setFlag(self, name)
155 	Agar::Toolbar self
156 	const char * name
157 CODE:
158 	if (AP_SetNamedFlag(name, flagNames, &(self->flags))) {
159 		AP_SetNamedFlag(name, AP_WidgetFlagNames, &(AGWIDGET(&self->box)->flags));
160 	}
161 
162 void
163 unsetFlag(self, name)
164 	Agar::Toolbar self
165 	const char * name
166 CODE:
167 	if (AP_UnsetNamedFlag(name, flagNames, &(self->flags))) {
168 		AP_UnsetNamedFlag(name, AP_WidgetFlagNames, &(AGWIDGET(&self->box)->flags));
169 	}
170 
171 Uint
172 getFlag(self, name)
173 	Agar::Toolbar self
174 	const char * name
175 CODE:
176 	if (AP_GetNamedFlag(name, flagNames, self->flags, &RETVAL)) {
177 		if (AP_GetNamedFlag(name, AP_WidgetFlagNames, AGWIDGET(&self->box)->flags,
178 			&RETVAL)) { XSRETURN_UNDEF; }
179 	}
180 OUTPUT:
181 	RETVAL
182 
183