1 /*--License:
2 	Kyra Sprite Engine
3 	Copyright Lee Thomason (Grinning Lizard Software) 2001-2005
4 	www.grinninglizard.com/kyra
5 	www.sourceforge.net/projects/kyra
6 
7 	Kyra is provided under the LGPL.
8 
9 	I kindly request you display a splash screen (provided in the HTML documentation)
10 	to promote Kyra and acknowledge the software and everyone who has contributed to it,
11 	but it is not required by the license.
12 
13 --- LGPL License --
14 
15     This library is free software; you can redistribute it and/or
16     modify it under the terms of the GNU Lesser General Public
17     License as published by the Free Software Foundation; either
18     version 2.1 of the License, or (at your option) any later version.
19 
20     This library is distributed in the hope that it will be useful,
21     but WITHOUT ANY WARRANTY; without even the implied warranty of
22     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23     Lesser General Public License for more details.
24 
25     You should have received a copy of the GNU Lesser General Public
26     License along with this library; if not, write to the Free Software
27     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
28 
29 	The full text of the license can be found in lgpl.txt
30 */
31 
32 #include "eventmanager.h"
33 #include "widget.h"
34 #include "../engine/engine.h"
35 #include "../engine/boxresource.h"
36 #include "../engine/box.h"
37 
38 
KrScheme(KrFontResource * _font)39 KrScheme::KrScheme( KrFontResource* _font )
40 {
41 	primary.Set( 170, 170, 170 );
42 	cursor.Set( 255, 255, 255 );
43 	secondary.Set(	200, 0,
44 					255, 0,
45 					200, 0,
46 					255 );
47 	font = _font;
48 }
49 
50 
CalcBrightLine() const51 KrRGBA KrScheme::CalcBrightLine() const
52 {
53 	KrRGBA lightGrey = primary;
54 
55 	KrColorTransform xform;
56 	xform.Brighten( BRIGHT );
57 	xform.ApplyTransform( &lightGrey );
58 
59 	return lightGrey;
60 }
61 
62 
CalcShadowLine() const63 KrRGBA KrScheme::CalcShadowLine() const
64 {
65 	KrRGBA darkGrey = primary;
66 
67 	KrColorTransform xform;
68 	xform.Darken( DARK );
69 	xform.ApplyTransform( &darkGrey );
70 
71 	return darkGrey;
72 }
73 
74 
CalcHiPrimary() const75 KrColorTransform KrScheme::CalcHiPrimary() const
76 {
77 	KrColorTransform light;
78 	light.Brighten( BRIGHT );
79 	return light;
80 }
81 
82 
CalcHiSec() const83 KrColorTransform KrScheme::CalcHiSec() const
84 {
85 	return secondary;
86 }
87 
88 
CalcDark() const89 KrColorTransform KrScheme::CalcDark() const
90 {
91 	KrColorTransform dark;
92 	dark.Darken( DARK );
93 	return dark;
94 }
95 
96 
CalcDarkSec() const97 KrColorTransform KrScheme::CalcDarkSec() const
98 {
99 	KrColorTransform xform = secondary;
100 	KrColorTransform dark;
101 	dark.Darken( DARK );
102 
103 	xform.Composite( dark );
104 	return xform;
105 }
106 
107 
KrBevelElement(int w,int h,const KrScheme & scheme)108 KrBevelElement::KrBevelElement( int w, int h, const KrScheme& scheme )
109 {
110 	width = w;
111 	height = h;
112 
113 	horD = horL = vertD = vertL = 0;
114 
115 	KrRGBA light = scheme.CalcBrightLine();
116 	KrRGBA dark  = scheme.CalcShadowLine();
117 
118 	horDR	= new KrBoxResource( "KrBevelElement",	width, 1,	&dark, 1,  KrBoxResource::FILL );
119 	horLR	= new KrBoxResource( "KrBevelElement",  width, 1,	&light,	1, KrBoxResource::FILL );
120 	vertDR	= new KrBoxResource( "KrBevelElement",	1, height,	&dark, 1,  KrBoxResource::FILL );
121 	vertLR	= new KrBoxResource( "KrBevelElement",	1, height,	&light,	1, KrBoxResource::FILL );
122 };
123 
124 
~KrBevelElement()125 KrBevelElement::~KrBevelElement()
126 {
127 	// Children should be deleted from the tree.
128 	delete horDR;
129 	delete horLR;
130 	delete vertDR;
131 	delete vertLR;
132 }
133 
134 
AddToTree(KrEngine * e,KrImNode * p)135 void KrBevelElement::AddToTree( KrEngine* e, KrImNode* p )
136 {
137 	horD = new KrBox( horDR );
138 	e->Tree()->AddNode( p, horD );
139 	horL = new KrBox( horLR );
140 	e->Tree()->AddNode( p, horL );
141 	vertD = new KrBox( vertDR );
142 	e->Tree()->AddNode( p, vertD );
143 	vertL = new KrBox( vertLR );
144 	e->Tree()->AddNode( p, vertL );
145 }
146 
147 
DrawIn()148 void KrBevelElement::DrawIn()
149 {
150 	horD->SetPos( 0, 0 );
151 	horL->SetPos( 0, height-1 );
152 	vertD->SetPos( 0, 0 );
153 	vertL->SetPos( width-1, 0 );
154 }
155 
DrawOut()156 void KrBevelElement::DrawOut()
157 {
158 	horD->SetPos( 0, height-1 );
159 	horL->SetPos( 0, 0 );
160 	vertD->SetPos( width-1, 0 );
161 	vertL->SetPos( 0, 0 );
162 }
163 
164 
KrWidget(const KrScheme & _scheme)165 KrWidget::KrWidget( const KrScheme& _scheme ) : scheme( _scheme )
166 {
167 	groupId = 0;
168 }
169 
170 
~KrWidget()171 KrWidget::~KrWidget()
172 {
173 	KrEventManager::Instance()->RemoveListener( this );
174 }
175 
176 
177 //void KrWidget::AddListener( IKrWidgetListener* listener )
178 //{
179 //	if ( eventList.Find( listener ) == eventList.NotFound() )
180 //		eventList.PushBack( listener );
181 //}
182 //
183 //
184 //void KrWidget::RemoveListener( IKrWidgetListener* listener )
185 //{
186 //	// Don't actually change the list, in case this is recursively
187 //	// called! The Publish can clean the list of extras later.
188 //
189 //	unsigned i = eventList.Find( listener );
190 //	if ( i != eventList.NotFound() )
191 //		eventList[ i ] = 0;
192 //}
193 
194 
195 //void KrWidget::PublishEvent( U32 event, U32 data, const SDL_Event* sdlEvent, const char* command, const char* arg )
196 //{
197 //	bool foundNull = false;
198 //
199 //	for( unsigned currentNode = 0; currentNode < eventList.Count(); ++currentNode )
200 //	{
201 //		if ( eventList[ currentNode ] )
202 //		{
203 //			// Note: this may decrement currentNode if we self-delete
204 //			eventList[currentNode]->HandleWidgetEvent( this, event, data, sdlEvent, command, arg );
205 //		}
206 //		else
207 //		{
208 //			foundNull = true;
209 //		}
210 //	}
211 //
212 //	if ( foundNull )
213 //	{
214 //		unsigned i=0;
215 //		while ( i < eventList.Count() )
216 //		{
217 //			if ( eventList[i] )
218 //				++i;
219 //			else
220 //				eventList.Remove( i );
221 //		}
222 //	}
223 //}
224 
225 
226 //void KrWidget::PublishTaggedEvent( U32 event, const SDL_Event* sdlEvent, const char* command, const char* arg, IKrWidgetListener* special )
227 //{
228 //
229 //	for( unsigned currentNode = 0; currentNode < eventList.Count(); ++currentNode )
230 //	{
231 //		if ( eventList[ currentNode ] )
232 //		{
233 //			// Note: this may decrement currentNode if we self-delete
234 //			eventList[currentNode]->HandleWidgetEvent(	this,
235 //														event,
236 //														special == eventList[currentNode] ? 1 : 0,
237 //														sdlEvent,
238 //														command, arg );
239 //		}
240 //	}
241 //}
242 //
243 
SetAccelerator(int keymod,int keysym)244 void KrWidget::SetAccelerator( int keymod, int keysym )
245 {
246 	KrEventManager::Instance()->SetAccelerator( keymod, keysym, this );
247 }
248 
249 
ParentWidget()250 KrWidget* KrWidget::ParentWidget()
251 {
252 	KrImNode* node;
253 	for( node = this->Parent(); node; node=node->Parent() )
254 	{
255 		if ( node->ToWidget() )
256 			return node->ToWidget();
257 	}
258 	return 0;
259 }
260 
261