2009-12-07 Rolf Bjarne Kvinge <RKvinge@novell.com>
[moon.git] / src / grid.h
blob05500a1f91f25e1a33988dedd2aa469bf279ce6a
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3 * grid.h
5 * Copyright 2008 Novell, Inc. (http://www.novell.com)
7 * See the LICENSE file included with the distribution for details.
8 *
9 */
11 #ifndef __MOON_GRID_H__
12 #define __MOON_GRID_H__
14 #include <glib.h>
15 #include "panel.h"
17 /* @IncludeInKinds */
18 /* @Namespace=System.Windows */
19 struct GridLength {
20 public:
21 double val;
22 GridUnitType type;
24 GridLength () {
25 val = 0;
26 type = GridUnitTypeAuto;
29 GridLength (double v, GridUnitType t)
31 val = v;
32 type = t;
35 bool operator == (const GridLength &v) const
37 return (fabs (val - v.val) < DBL_EPSILON && type == v.type);
40 bool operator != (const GridLength &point) const
42 return !(*this == point);
46 /* @Namespace=System.Windows.Controls */
47 class ColumnDefinition : public DependencyObject {
48 protected:
49 virtual ~ColumnDefinition ();
51 public:
52 /* @PropertyType=double,DefaultValue=INFINITY,GenerateAccessors */
53 const static int MaxWidthProperty;
54 /* @PropertyType=double,DefaultValue=0.0,GenerateAccessors */
55 const static int MinWidthProperty;
56 /* @PropertyType=GridLength,DefaultValue=GridLength (1.0\, GridUnitTypeStar),GenerateAccessors */
57 const static int WidthProperty;
58 /* @PropertyType=double,DefaultValue=0.0,GenerateAccessors,ManagedSetterAccess=Private,ManagedFieldAccess=Private */
59 const static int ActualWidthProperty;
61 /* @GenerateCBinding,GeneratePInvoke */
62 ColumnDefinition ();
64 // property accessors
65 double GetActualWidth ();
66 void SetActualWidth (double value);
68 double GetMaxWidth();
69 void SetMaxWidth (double value);
71 double GetMinWidth();
72 void SetMinWidth (double value);
74 GridLength* GetWidth();
75 void SetWidth (GridLength *value);
78 /* @Namespace=System.Windows.Controls */
79 class RowDefinition : public DependencyObject {
80 protected:
81 virtual ~RowDefinition ();
83 public:
84 /* @PropertyType=GridLength,DefaultValue=GridLength (1.0\, GridUnitTypeStar),GenerateAccessors */
85 const static int HeightProperty;
86 /* @PropertyType=double,DefaultValue=INFINITY,GenerateAccessors */
87 const static int MaxHeightProperty;
88 /* @PropertyType=double,DefaultValue=0.0,GenerateAccessors */
89 const static int MinHeightProperty;
90 /* @PropertyType=double,DefaultValue=0.0,GenerateAccessors,ManagedSetterAccess=Private,ManagedFieldAccess=Private */
91 const static int ActualHeightProperty;
93 /* @GenerateCBinding,GeneratePInvoke */
94 RowDefinition ();
96 // property accessors
97 double GetActualHeight ();
98 void SetActualHeight (double value);
100 double GetMaxHeight();
101 void SetMaxHeight (double value);
103 double GetMinHeight();
104 void SetMinHeight (double value);
106 GridLength* GetHeight();
107 void SetHeight (GridLength *value);
110 /* @Namespace=System.Windows.Controls */
111 class ColumnDefinitionCollection : public DependencyObjectCollection {
112 protected:
113 virtual ~ColumnDefinitionCollection ();
115 virtual bool AddedToCollection (Value *value, MoonError *error);
117 public:
118 /* @GenerateCBinding,GeneratePInvoke,ManagedAccess=Internal */
119 ColumnDefinitionCollection ();
121 virtual Type::Kind GetElementType () { return Type::COLUMNDEFINITION; }
125 /* @Namespace=System.Windows.Controls */
126 class RowDefinitionCollection : public DependencyObjectCollection {
127 protected:
128 virtual ~RowDefinitionCollection ();
130 virtual bool AddedToCollection (Value *value, MoonError *error);
132 public:
133 /* @GenerateCBinding,GeneratePInvoke,ManagedAccess=Internal */
134 RowDefinitionCollection ();
136 virtual Type::Kind GetElementType () { return Type::ROWDEFINITION; }
139 struct Segment {
140 double original_size;
141 double max;
142 double min;
143 double size;
144 double stars;
145 GridUnitType type;
147 Segment ();
148 Segment (double size, double min, double max, GridUnitType type);
150 private:
151 void Init (double size, double min, double max, GridUnitType type);
154 /* @Namespace=System.Windows.Controls */
155 class Grid : public Panel {
156 int row_matrix_dim;
157 int col_matrix_dim;
158 Segment **row_matrix;
159 Segment **col_matrix;
161 void AllocateGridSegments (int row_count, int col_count);
162 void AssignSize (Segment **matrix, int start, int end, double *size, GridUnitType type);
163 void CreateMatrices (int row_count, int col_count);
164 void DestroyMatrices ();
165 void ExpandStarRows (Size availableSize);
166 void ExpandStarCols (Size availableSize);
168 void SaveMeasureResults ();
169 void RestoreMeasureResults ();
171 protected:
172 virtual ~Grid ();
173 virtual void PostRender (cairo_t *cr, Region *region, bool front_to_back);
175 public:
176 /* @PropertyType=gint32,DefaultValue=0,Attached,GenerateAccessors,Validator=PositiveIntValidator */
177 const static int ColumnProperty;
178 /* @PropertyType=ColumnDefinitionCollection,AutoCreateValue,ManagedFieldAccess=Internal,ManagedSetterAccess=Internal,GenerateAccessors */
179 const static int ColumnDefinitionsProperty;
180 /* @PropertyType=gint32,DefaultValue=1,Attached,GenerateAccessors,Validator=IntGreaterThanZeroValidator */
181 const static int ColumnSpanProperty;
182 /* @PropertyType=gint32,DefaultValue=0,Attached,GenerateAccessors,Validator=PositiveIntValidator */
183 const static int RowProperty;
184 /* @PropertyType=RowDefinitionCollection,AutoCreateValue,ManagedFieldAccess=Internal,ManagedSetterAccess=Internal,GenerateAccessors */
185 const static int RowDefinitionsProperty;
186 /* @PropertyType=gint32,DefaultValue=1,Attached,GenerateAccessors,Validator=IntGreaterThanZeroValidator */
187 const static int RowSpanProperty;
190 * NOTE: The ShowGridLines property defaults to false but appears
191 * to be uninitialized before InitializeComponents is called
192 * causing the current moon-unit test to fail.
194 /* @PropertyType=bool,DefaultValue=false,GenerateAccessors */
195 const static int ShowGridLinesProperty;
197 /* @GenerateCBinding,GeneratePInvoke */
198 Grid ();
200 virtual void OnPropertyChanged (PropertyChangedEventArgs *args, MoonError *error);
201 virtual void OnCollectionChanged (Collection *col, CollectionChangedEventArgs *args);
202 virtual void OnCollectionItemChanged (Collection *col, DependencyObject *obj, PropertyChangedEventArgs *args);
203 virtual void ComputeBounds ();
205 virtual Size MeasureOverride (Size availableSize);
206 virtual Size ArrangeOverride (Size finalSize);
208 // property accessors
209 ColumnDefinitionCollection *GetColumnDefinitions ();
210 void SetColumnDefinitions (ColumnDefinitionCollection* value);
212 static gint32 GetColumn (DependencyObject *obj);
213 static void SetColumn (DependencyObject *obj, gint32 value);
215 static gint32 GetColumnSpan (DependencyObject *obj);
216 static void SetColumnSpan (DependencyObject *obj, gint32 value);
218 RowDefinitionCollection *GetRowDefinitions ();
219 void SetRowDefinitions (RowDefinitionCollection* value);
221 static gint32 GetRow (DependencyObject *obj);
222 static void SetRow (DependencyObject *obj, gint32 value);
224 static gint32 GetRowSpan (DependencyObject *obj);
225 static void SetRowSpan (DependencyObject *obj, gint32 value);
227 bool GetShowGridLines ();
228 void SetShowGridLines (bool value);
230 static double Clamp (double val, double min, double max);
233 // We need this class to figure out what kinds of elements the grid
234 // contains before the grid starts measuring them.
235 class GridWalker {
236 public:
237 bool HasAutoAuto () { return has_auto_auto; }
238 bool HasStarAuto () { return has_star_auto; }
239 bool HasAutoStar () { return has_auto_star; }
240 GridWalker (Grid *grid, Segment **row_matrix, int row_count, Segment **col_matrix, int col_count);
242 private:
243 bool has_auto_auto;
244 bool has_star_auto;
245 bool has_auto_star;
248 class GridNode : public List::Node {
249 public:
250 int row;
251 int col;
252 double size;
253 Segment ** matrix;
255 GridNode (Segment **matrix, int row, int col, double size) {
256 this->matrix = matrix;
257 this->row = row;
258 this->col = col;
259 this->size = size;
262 #endif /* __MOON_PANEL_H__ */