00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include "xrb_cellpaddingwidget.hpp"
00012
00013 #include "xrb_screen.hpp"
00014
00015 namespace Xrb
00016 {
00017
00018 CellPaddingWidget::CellPaddingWidget (
00019 ContainerWidget *const parent,
00020 std::string const &name)
00021 :
00022 ContainerWidget(parent, name)
00023 {
00024 m_accepts_focus = false;
00025 m_accepts_mouseover = true;
00026 m_alignment = Alignment2(CENTER, CENTER);
00027
00028 CellPaddingWidget::UpdateRenderBackground();
00029
00030 DirtyContentsSizeProperties();
00031 }
00032
00033 Widget *CellPaddingWidget::SingleChildWidget ()
00034 {
00035 ASSERT1(m_child_vector.size() <= 1);
00036
00037 WidgetVector::iterator it = m_child_vector.begin();
00038 WidgetVector::iterator it_end = m_child_vector.end();
00039 if (it != it_end)
00040 {
00041 Widget *child = *it;
00042 ASSERT1(child != NULL);
00043 return child;
00044 }
00045 else
00046 {
00047 return NULL;
00048 }
00049 }
00050
00051 Widget const *CellPaddingWidget::SingleChildWidget () const
00052 {
00053 ASSERT1(m_child_vector.size() <= 1);
00054
00055 WidgetVector::const_iterator it = m_child_vector.begin();
00056 WidgetVector::const_iterator it_end = m_child_vector.end();
00057 if (it != it_end)
00058 {
00059 Widget const *child = *it;
00060 ASSERT1(child != NULL);
00061 return child;
00062 }
00063 else
00064 {
00065 return NULL;
00066 }
00067 }
00068
00069 Bool2 CellPaddingWidget::ContentsMinSizeEnabled () const
00070 {
00071 UpdateContentsSizeProperties();
00072 return m_contents_size_properties.m_min_size_enabled;
00073 }
00074
00075 ScreenCoordVector2 CellPaddingWidget::ContentsMinSize () const
00076 {
00077 UpdateContentsSizeProperties();
00078 return m_contents_size_properties.m_min_size;
00079 }
00080
00081 Bool2 CellPaddingWidget::ContentsMaxSizeEnabled () const
00082 {
00083 UpdateContentsSizeProperties();
00084 return m_contents_size_properties.m_max_size_enabled;
00085 }
00086
00087 ScreenCoordVector2 CellPaddingWidget::ContentsMaxSize () const
00088 {
00089 UpdateContentsSizeProperties();
00090 return m_contents_size_properties.m_max_size;
00091 }
00092
00093 void CellPaddingWidget::SetAlignment (
00094 Uint32 const index,
00095 Alignment const alignment)
00096 {
00097 ASSERT1(index <= 1);
00098 ASSERT1(alignment != SPACED);
00099 if (m_alignment[index] != alignment)
00100 {
00101 m_alignment[index] = alignment;
00102 PositionSingleChildWidget();
00103 }
00104 }
00105
00106 void CellPaddingWidget::SetAlignment (
00107 Alignment2 const &alignment)
00108 {
00109 ASSERT1(alignment[Dim::X] != SPACED);
00110 ASSERT1(alignment[Dim::Y] != SPACED);
00111 if (m_alignment != alignment)
00112 {
00113 m_alignment = alignment;
00114 PositionSingleChildWidget();
00115 }
00116 }
00117
00118 void CellPaddingWidget::SetSizePropertyEnabled (
00119 SizeProperties::Property const property,
00120 Uint32 const component,
00121 bool const value)
00122 {
00123 ASSERT1(component <= 1);
00124 if (property == SizeProperties::MIN)
00125 m_preferred_size_properties.m_min_size_enabled[component] = value;
00126 else
00127 m_preferred_size_properties.m_max_size_enabled[component] = value;
00128 ContainerWidget::SetSizePropertyEnabled(property, component, value, true);
00129 CalculateMinAndMaxSizePropertiesFromContents();
00130
00131 }
00132
00133 void CellPaddingWidget::SetSizePropertyEnabled (
00134 SizeProperties::Property const property,
00135 Bool2 const &value)
00136 {
00137 if (property == SizeProperties::MIN)
00138 m_preferred_size_properties.m_min_size_enabled = value;
00139 else
00140 m_preferred_size_properties.m_max_size_enabled = value;
00141 ContainerWidget::SetSizePropertyEnabled(property, value, true);
00142 CalculateMinAndMaxSizePropertiesFromContents();
00143
00144 }
00145
00146 void CellPaddingWidget::SetSizeProperty (
00147 SizeProperties::Property const property,
00148 Uint32 const component,
00149 ScreenCoord const value)
00150 {
00151 ASSERT1(component <= 1);
00152 ASSERT1(value >= 0);
00153 if (property == SizeProperties::MIN)
00154 m_preferred_size_properties.m_min_size[component] = value;
00155 else
00156 m_preferred_size_properties.m_max_size[component] = value;
00157 ContainerWidget::SetSizeProperty(property, component, value, true);
00158 CalculateMinAndMaxSizePropertiesFromContents();
00159
00160 }
00161
00162 void CellPaddingWidget::SetSizeProperty (
00163 SizeProperties::Property const property,
00164 ScreenCoordVector2 const &value)
00165 {
00166 ASSERT1(value[Dim::X] >= 0);
00167 ASSERT1(value[Dim::Y] >= 0);
00168 if (property == SizeProperties::MIN)
00169 m_preferred_size_properties.m_min_size = value;
00170 else
00171 m_preferred_size_properties.m_max_size = value;
00172 ContainerWidget::SetSizeProperty(property, value, true);
00173 CalculateMinAndMaxSizePropertiesFromContents();
00174
00175 }
00176
00177 ScreenCoordVector2 CellPaddingWidget::Resize (
00178 ScreenCoordVector2 const &size)
00179 {
00180 ContainerWidget::Resize(size);
00181
00182
00183 Widget *child = SingleChildWidget();
00184 if (child != NULL)
00185 {
00186
00187 if (ChildResizeBlockerCount() == 0)
00188 {
00189
00190 child->Resize(size);
00191
00192 PositionSingleChildWidget();
00193 }
00194 else
00195 IndicateChildResizeWasBlocked();
00196 }
00197
00198 return Size();
00199 }
00200
00201 void CellPaddingWidget::AttachChild (Widget *const child)
00202 {
00203
00204
00205 ASSERT0(SingleChildWidget() == NULL);
00206
00207 ContainerWidget::AttachChild(child);
00208
00209
00210 DirtyContentsSizeProperties();
00211
00212 if (ChildResizeBlockerCount() == 0)
00213 {
00214
00215 CalculateMinAndMaxSizePropertiesFromContents();
00216
00217 Resize(Size());
00218
00219
00220 }
00221 else
00222 IndicateChildResizeWasBlocked();
00223 }
00224
00225 void CellPaddingWidget::DetachChild (Widget *const child)
00226 {
00227
00228 ASSERT0(SingleChildWidget() != NULL);
00229
00230 ContainerWidget::DetachChild(child);
00231
00232
00233 DirtyContentsSizeProperties();
00234
00235 if (ChildResizeBlockerCount() == 0)
00236 {
00237
00238 CalculateMinAndMaxSizePropertiesFromContents();
00239
00240 Resize(Size());
00241
00242
00243 }
00244 else
00245 IndicateChildResizeWasBlocked();
00246 }
00247
00248 void CellPaddingWidget::ChildSizePropertiesChanged (Widget *const child)
00249 {
00250 ASSERT1(child == SingleChildWidget());
00251
00252
00253 DirtyContentsSizeProperties();
00254
00255 if (ChildResizeBlockerCount() == 0)
00256 {
00257
00258 CalculateMinAndMaxSizePropertiesFromContents();
00259
00260 Resize(Size());
00261
00262
00263 }
00264 IndicateChildResizeWasBlocked();
00265 }
00266
00267 void CellPaddingWidget::PositionSingleChildWidget ()
00268 {
00269 Widget *child = SingleChildWidget();
00270 if (child == NULL)
00271 return;
00272
00273
00274 ScreenCoordVector2 extra_space(Size() - child->Size());
00275
00276 ScreenCoordVector2 child_position_offset;
00277 for (Uint8 i = 0; i < 2; ++i)
00278 {
00279 switch (m_alignment.m[i])
00280 {
00281 case TOP:
00282 case LEFT:
00283 child_position_offset.m[i] = 0;
00284 break;
00285
00286 case CENTER:
00287 child_position_offset.m[i] = extra_space.m[i] / 2;
00288 break;
00289
00290 case BOTTOM:
00291 case RIGHT:
00292 child_position_offset.m[i] = extra_space.m[i];
00293 break;
00294
00295 case SPACED:
00296 ASSERT1(false && "SPACED is invalid for use in CellPaddingWidget");
00297 break;
00298
00299 default:
00300 ASSERT1(false && "Invalid Alignment");
00301 break;
00302 }
00303 }
00304
00305 child->MoveTo(Position() + child_position_offset);
00306 }
00307
00308 void CellPaddingWidget::DirtyContentsSizeProperties ()
00309 {
00310 m_contents_size_properties_need_update = true;
00311 }
00312
00313 void CellPaddingWidget::UpdateContentsSizeProperties () const
00314 {
00315
00316 if (!m_contents_size_properties_need_update)
00317 return;
00318
00319 m_contents_size_properties_need_update = false;
00320
00321
00322 m_contents_size_properties.m_min_size_enabled = Bool2(false, false);
00323 m_contents_size_properties.m_min_size = ScreenCoordVector2::ms_zero;
00324
00325
00326 m_contents_size_properties.m_max_size_enabled = Bool2(false, false);
00327 m_contents_size_properties.m_max_size =
00328 ScreenCoordVector2(
00329 SizeProperties::DefaultMaxSizeComponent(),
00330 SizeProperties::DefaultMaxSizeComponent());
00331
00332 Widget const *child = SingleChildWidget();
00333 ASSERT1(child != NULL);
00334
00335
00336 if (child->IsHidden() || child->IsModal())
00337 {
00338 m_contents_size_properties = m_preferred_size_properties;
00339 return;
00340 }
00341
00342
00343 for (Uint8 d = 0; d < 2; ++d)
00344 {
00345 m_contents_size_properties.m_min_size_enabled[d] =
00346 child->MinSizeEnabled()[d] ||
00347 m_preferred_size_properties.m_min_size_enabled[d];
00348 if (child->MinSizeEnabled()[d])
00349 {
00350 if (m_preferred_size_properties.m_min_size_enabled[d])
00351 {
00352 m_contents_size_properties.m_min_size[d] =
00353 Max(child->MinSize()[d],
00354 m_preferred_size_properties.m_min_size[d]);
00355 }
00356 else
00357 {
00358 m_contents_size_properties.m_min_size[d] = child->MinSize()[d];
00359 }
00360 }
00361 else
00362 {
00363 if (m_preferred_size_properties.m_min_size_enabled[d])
00364 {
00365 m_contents_size_properties.m_min_size[d] = m_preferred_size_properties.m_min_size[d];
00366 }
00367 else
00368 {
00369
00370 }
00371 }
00372
00373 m_contents_size_properties.m_max_size_enabled[d] =
00374 m_preferred_size_properties.m_max_size_enabled[d];
00375 if (m_preferred_size_properties.m_max_size_enabled[d])
00376 {
00377 if (child->MaxSizeEnabled()[d])
00378 {
00379 m_contents_size_properties.m_max_size[d] =
00380 Max(child->MaxSize()[d],
00381 m_preferred_size_properties.m_max_size[d]);
00382 }
00383 else
00384 {
00385 m_contents_size_properties.m_max_size[d] =
00386 m_preferred_size_properties.m_max_size_enabled[d];
00387 }
00388 }
00389 }
00390 }
00391
00392 }