PEBL 2.2
Psychology Experiment Building Language - Cross-platform psychological experiment development system
PWidget.cpp
Go to the documentation of this file.
1//* -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*- */
3// Name: src/objects/PWidget.cpp
4// Purpose: Contains methods for primary GUI element
5// Author: Shane T. Mueller, Ph.D.
6// Copyright: (c) 2003-2026 Shane T. Mueller <smueller@obereed.net>
7// License: GPL 2
8//
9//
10//
11// This file is part of the PEBL project.
12//
13// PEBL is free software; you can redistribute it and/or modify
14// it under the terms of the GNU General Public License as published by
15// the Free Software Foundation; either version 2 of the License, or
16// (at your option) any later version.
17//
18// PEBL is distributed in the hope that it will be useful,
19// but WITHOUT ANY WARRANTY; without even the implied warranty of
20// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21// GNU General Public License for more details.
22//
23// You should have received a copy of the GNU General Public License
24// along with PEBL; if not, write to the Free Software
25// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27
28#include "PWidget.h"
29#include "../base/PComplexData.h"
30#include "../utility/PError.h"
31#include <list>
32#include <iostream>
33
34using std::cout;
35using std::endl;
36
38 mX(0),
39 mY(0),
40 mDrawX(0),
41 mDrawY(0),
42 mWidth(0),
43 mHeight(0),
44 mZoomX(1),
45 mZoomY(1),
46 mRotation(0),
47 mBackgroundColor(PColor(0,0,0,0)),
48 mIsVisible(true),
49 mParent(NULL)
50{
51 // InitializeProperty("NAME",Variant(""));
60 InitializeProperty("PARENT",Variant(0));
61
62}
63
64PWidget::PWidget(pInt x, pInt y, pInt width, pInt height, bool visible):
65 mX(x),
66 mY(y),
67 mDrawX(x),
68 mDrawY(y),
69 mWidth(width),
70 mHeight(height),
71 mZoomX(1.0),
72 mZoomY(1.0),
73 mRotation(0),
74 mBackgroundColor(PColor(0,0,0,0)),
75 mIsVisible(visible),
76 mParent(NULL)
77{
78 // InitializeProperty("NAME",Variant(""));
87 InitializeProperty("PARENT",Variant(0));
88
89}
90
92 PWidget(pw.GetX(),
93 pw.GetY(),
94 pw.GetWidth(),
95 pw.GetHeight(),
96 pw.IsVisible())
97{
98 // InitializeProperty("NAME",Variant(""));
107 InitializeProperty("PARENT",Variant(0));
108
109}
110
112{
113
114
115
116}
117
118bool PWidget::RotoZoom(pDouble angle, pDouble zoomx, pDouble zoomy, pInt smooth)
119{
120
121 return false;
122}
123
124
126{
127 //This should go straight to PlatforwWidget::SetPoint
128
129 return false;
130}
131
132
134{
135 //This should go straight to PlatforwWidget::GetPixel
136
137 return false;
138}
139
140
141
142bool PWidget::SetProperty(std::string name, Variant v)
143{
145 if(err == OVE_SUCCESS)
146 {
147 if (name == "X") SetPosition(v,mY);
148 else if (name == "Y") SetPosition(mX,v);
149 else if (name == "ZOOMX") SetZoomX(( pDouble)v);
150 else if (name == "ZOOMY") SetZoomY(( pDouble)v);
151 else if (name == "ROTATION") SetRotation(( pDouble)v);
152 else if (name == "WIDTH") SetWidth((pInt)v);
153 else if (name == "HEIGHT") SetHeight((pInt)v);
154 else if (name == "BGCOLOR")
155 {
156 PColor bgcolor = *((PColor*)(v.GetComplexData()->GetObject().get()));
157 SetBackgroundColor(bgcolor);
158 }
159 else if (name == "VISIBLE")
160 {
161 if(v.GetInteger())
162 Show();
163 else
164 Hide();
165 }else if (name=="PARENT")
166 {
167
168 PEBLObjectBase::SetProperty("PARENT",v);
169 }
170 else return false;
171 }
172 else
173 {
174 PError::SignalFatalError("Failing to set invalid property: ["+ name +"]" );
175 return false;
176 }
177 return false;
178}
179
180
181Variant PWidget::GetProperty(std::string name)const
182{
183 return PEBLObjectBase::GetProperty(name);
184}
185
186
187
189{
190 return ValidateProperty(name);
191}
192
193
195{
196
197
198 //This only returns valid for properties that are valid to widgets
199 //in general--subclasses need to check on their own.
200 if(name == "X" ||
201 name == "Y" ||
202 name == "VISIBLE" ||
203 name == "WIDTH" ||
204 name == "HEIGHT" ||
205 name == "ZOOMX" ||
206 name == "ZOOMY" ||
207 name == "BGCOLOR" ||
208 name == "ROTATION" ||
209 name == "NAME"||
210 name=="PARENT"
211 )
212 return OVE_VALID;
213 else
215}
216
217
219
221{
222
223
224 mX = x;
225 mY = y;
226 mDrawX = x;
227 mDrawY = y;
228
231}
232
233
234
237{
238
239 mZoomX = x;
241 SetProperty("X",mX); //reset X as it matters for images/labels
242 //SetProperty("WIDTH",(int)(mWidth*mZoomX));
243}
244
245
248{
249
250 mZoomY = y;
252 SetProperty("Y",mY);//reset y as it matters for images/labels
253 //SetProperty("HEIGHT",(int)(mHeight*mZoomY));
254}
255
256
257
259{
260 mHeight = h;
262
263}
264
266{
267 mWidth = w;
269
270}
271
274{
275 mRotation = x;
276 PEBLObjectBase::SetProperty("ROTATION",x);
277}
278
279//Inherited from PComplexObject->PEBLObjectBase
280std::ostream & PWidget::SendToStream(std::ostream& out) const
281{
282 out << "<Unknown PWidget>" << std::flush;
283 return out;
284}
285
286
288{
289
290 PColor * pc = new PColor(color);
292 PComplexData * pcd = new PComplexData(pob);
293 Variant col = Variant(pcd);
294
295
296 PEBLObjectBase::SetProperty("BGCOLOR",col);
297
298 PColor pc2 = PColor(color.GetRed(),color.GetGreen(),color.GetBlue(),color.GetAlpha());
299 //PColor(col);
300
305
306
307 // InitializeProperty("BGCOLOR", col);
308
309}
310
311
312
313// This will set the parent of *this to widget
314//
316{
317
318 PEBLObjectBase::SetProperty("PARENT",widget);
319 mParent = widget;
320}
321
322
323bool PWidget::IsParent(PWidget * testparent)
324{
325
326 return testparent == GetParent();
327}
328
329
331{
332
333
334 //remove the subwidget from wherever it was:
335 PWidget* p = widget->GetParent();
336 if(p)
337 {
338
339 p->RemoveSubWidget(widget);
340 }
341
342 widget->SetParent(NULL);
343
344
345
346 //now, add it back on:
347 mSubWidgets.push_front(widget);
348 widget->SetParent(this);
349
350 return true;
351}
352
354
355{
356
357 if(mSubWidgets.size()>0)
358 {
359
360 std::list<PWidget*>::iterator i = mSubWidgets.begin();
361 while(i != mSubWidgets.end())
362 {
363
364 (*i)->SetParent(NULL);
365 mSubWidgets.pop_front();
366 i=mSubWidgets.begin(); //readjust so it points to the start of the list.
367 }
368
369 mSubWidgets.clear();
370 }
371 return true;
372}
373
374
376{
377
378
379
380
381 mSubWidgets.remove(widget);
382 mSubWidgets.remove(dynamic_cast<PlatformWidget*>(widget));
383 widget->SetParent(NULL);
384
385 return true;
386}
387
389{
390
391 mSubWidgets.pop_front();
392 return true;
393}
394
395
397{
398 mIsVisible = true;
400
401}
403{
404 mIsVisible = false;
406}
407
408
#define NULL
Definition BinReloc.cpp:317
#define pInt
Definition Defs.h:8
#define pDouble
Definition Defs.h:7
ObjectValidationError
Definition PEBLObject.h:37
@ OVE_SUCCESS
Definition PEBLObject.h:38
@ OVE_INVALID_PROPERTY_NAME
Definition PEBLObject.h:40
@ OVE_VALID
Definition PEBLObject.h:39
void SetRed(int color)
Definition PColor.cpp:197
int GetRed() const
Definition PColor.cpp:226
int GetAlpha() const
Definition PColor.cpp:229
int GetBlue() const
Definition PColor.cpp:228
int GetGreen() const
Definition PColor.cpp:227
void SetGreen(int color)
Definition PColor.cpp:204
void SetBlue(int color)
Definition PColor.cpp:211
void SetAlpha(int color)
Definition PColor.cpp:218
virtual bool InitializeProperty(std::string name, Variant v)
virtual bool SetProperty(std::string name, Variant v)
Variant GetProperty(std::string) const
pInt mDrawX
Definition PWidget.h:129
virtual ObjectValidationError ValidateProperty(std::string, Variant v) const
Definition PWidget.cpp:188
virtual Variant GetProperty(std::string) const
Definition PWidget.cpp:181
PWidget()
Definition PWidget.cpp:37
virtual void SetPosition(pInt x, pInt y)
This sets the widget's position on its parent widget.
Definition PWidget.cpp:220
virtual std::ostream & SendToStream(std::ostream &out) const
An inheritable printing class used by PEBLObjectBase::operator<<.
Definition PWidget.cpp:280
virtual void Show()
Definition PWidget.cpp:396
virtual void SetBackgroundColor(const PColor &color)
Definition PWidget.cpp:287
virtual bool RemoveLastSubWidget()
This is probably pretty useless.
Definition PWidget.cpp:388
virtual void SetWidth(pInt w)
Definition PWidget.cpp:265
virtual void SetZoomX(pDouble x)
This sets the widget's position on its parent widget.
Definition PWidget.cpp:236
virtual void SetZoomY(pDouble x)
This sets the widget's position on its parent widget.
Definition PWidget.cpp:247
pDouble mZoomX
Definition PWidget.h:139
virtual bool SetProperty(std::string, Variant v)
Definition PWidget.cpp:142
pInt mWidth
Definition PWidget.h:136
std::list< PWidget * > mSubWidgets
Definition PWidget.h:147
virtual PColor GetPixel(pInt x, pInt y)
Definition PWidget.cpp:133
PColor mBackgroundColor
The background color of the widget. if alpha = 0, will not be painted.
Definition PWidget.h:144
pInt mX
Definition PWidget.h:125
virtual bool AddSubWidget(PWidget *widget)
Definition PWidget.cpp:330
virtual bool RemoveSubWidgets()
Definition PWidget.cpp:353
pInt mHeight
Definition PWidget.h:136
virtual void SetHeight(pInt h)
Definition PWidget.cpp:258
virtual void Hide()
Definition PWidget.cpp:402
pInt mY
Definition PWidget.h:125
virtual void SetRotation(pDouble x)
This sets the widget's position on its parent widget.
Definition PWidget.cpp:273
virtual bool SetPoint(pInt x, pInt y, PColor col)
Definition PWidget.cpp:125
virtual ~PWidget()
Definition PWidget.cpp:111
virtual PWidget * GetParent()
Definition PWidget.h:63
virtual bool IsParent(PWidget *parent)
Definition PWidget.cpp:323
pDouble mZoomY
Definition PWidget.h:140
PWidget * mParent
Definition PWidget.h:154
virtual bool RemoveSubWidget(PWidget *widget)
Definition PWidget.cpp:375
pInt mDrawY
Definition PWidget.h:130
bool mIsVisible
Definition PWidget.h:150
virtual bool RotoZoom(pDouble angle, pDouble zoomx, pDouble zoomy, pInt smooth)
Definition PWidget.cpp:118
pDouble mRotation
Definition PWidget.h:141
virtual void SetParent(PWidget *widget)
This unconditionally sets the parent widget.
Definition PWidget.cpp:315
void SignalFatalError(const std::string &message)