File: cslayout.h

package info (click to toggle)
crystalspace 0.94-20020412-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 62,276 kB
  • ctags: 52,843
  • sloc: cpp: 274,783; ansic: 6,608; perl: 6,276; objc: 3,952; asm: 2,942; python: 2,354; php: 542; pascal: 530; sh: 430; makefile: 370; awk: 193
file content (190 lines) | stat: -rw-r--r-- 6,817 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
    Copyright (C) Aleksandras Gluchovas
    CS port by Norman Krmer <norman@users.sourceforge.net>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public
    License along with this library; if not, write to the Free
    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#ifndef __CSLAYOUT_H__
#define __CSLAYOUT_H__

#include "csws/csdialog.h"
#include "csgeom/cspoint.h"

/**
 * csLayoutConstraint is a basic constraint used for positioning a control
 * in a csLayout derived component.
 */
class csLayoutConstraint
{
public:
  /// the attached component
  csComponent *comp;
public:
  /// Constructor: initialize the object with zero
  csLayoutConstraint ()
  { comp = NULL; }
  /// Constructor: initialize the object with given value
  csLayoutConstraint (csComponent *comp)
  { this->comp = comp; }
  /// the destructor ... nothing much to say about.
  virtual ~csLayoutConstraint () {}
  /// make a copy of this constraint
  virtual csLayoutConstraint *Clone ();
};

/**
 * The layout classes collect the constraints in here.
 * The constraints (and thus the components to place) are
 * taken into account in the sequence they are added to this vector.
 */
class csConstraintVector : public csVector
{
public:
  /// for looking up an constraint by comparing the attached components
  virtual int Compare (csSome Item1, csSome Item2, int Mode = 0) const
  {
    (void)Mode;
    csLayoutConstraint *c1 = (csLayoutConstraint *)Item1;
    csLayoutConstraint *c2 = (csLayoutConstraint *)Item2;
    return (c1->comp < c2->comp ? -1 : c1->comp > c2->comp ? 1 : 0);
  }
  /// look up an constraint given a components
  virtual int CompareKey (csSome Item1, csConstSome Item2, int Mode = 0) const
  {
    (void)Mode;
    csLayoutConstraint *c1 = (csLayoutConstraint *)Item1;
    csComponent *c2 = (csComponent *)Item2;
    return (c1->comp < c2 ? -1 : c1->comp > c2 ? 1 : 0);
  }
  /// for automatic cleanup of the vector elements
  virtual bool FreeItem (csSome Item)
  { if (Item) delete (csLayoutConstraint *)Item; return true; }
  /// for convenience
  csLayoutConstraint *Get (int idx)
  { return (csLayoutConstraint *)csVector::Get (idx); }
};

/**
 * csLayout is our baseclass for various derived classes like
 * csFlowLayout, csBoxLayout, csGridBagLayout and others.
 *
 * What are layouts for ?
 *
 * Usually one designs dialogs, forms etc. by placing components like
 * listcontrols, inputlines, treecontrols and so on at absolute locations
 * inside the dialog. While this is fine for a fixed size of your dialog
 * it turns out to be pretty ugly if you want allow resizing of dialogs,
 * forms etc. A comparable ugly effect you achive by running an application
 * in different window sizes.
 *
 * Layouts will help you to overcome this drawback.
 * They will allow you to relatively place a control and to resize
 * components when necessary.
 *
 * Layouts are themselfs csComponents and have a transparent canvas.
 * Thus you will not note them. One important issue about layouts is
 * that they will transfer all Events of type csevCommand to its parent
 * control. This will allow you overwrite just one HandleEvent to receive
 * all commands from the components embedded in the layouts no matter how
 * deeply nested they are.
 */

class csLayout : public csDialog
{
protected:
  /**
   * A shortcoming of the original java layouts is that components are
   * asked for its preferred size without knowing at what size its parent
   * component will be layout in the end. So the two phase layout is an
   * attempt to overcome this. Currently only FlowLayout uses this.
   */
  static bool mUseTwoPhaseLayoutingGlobally;
  static int mCurrentLayoutingPhase;
  /// do we need to recalc the positions and sizes of placed components ?
  bool bRecalcLayout;
  /// collect all constraints here
  csConstraintVector vConstraints;
  /// a pointer to the current constraint
  csLayoutConstraint *lc;

public:
  /// preserve space at the 4 borders of a layout
  csRect insets;
  enum LAYOUTING_PHASES {PHASE_0 = 0, PHASE_1 = 1};
  /// Here we have the constraint a components will be layout with.
  /// When a component is added a copy of this will be made and
  /// attached to the component.
  csLayoutConstraint c;

public:
  csLayout (csComponent *iParent, csDialogFrameStyle iFrameStyle = csdfsNone);

  /**
   * A components is added to a layout by creating it and passing
   * the layout component as its parent component.
   * If you insist of doing some voodoo you should use AddLayoutComponent
   * to add it to the layout.
   * <pre>
   * IN:  the component to add
   * OUT: the constraint that is used to layout the component
   *      (a copy of variable c  see above)
   * </pre>
   */
  virtual csLayoutConstraint *AddLayoutComponent (csComponent *comp);
  /// remove a component from the layout
  virtual void RemoveLayoutComponent (csComponent *comp);
  /// return best size for this layout
  virtual void SuggestSize (int &sugw, int& sugh) = 0;
  /// recalc positions and sizes of components
  virtual void LayoutContainer () = 0;
  /// make sure next time the layout is drawn the components are
  /// layed out first
  virtual void InvalidateLayout ();

  /// return current phase of layouting
  virtual int GetLayoutingPhase ();
  /// set current phase of layouting
  virtual void SetLayoutingPhase (int phase);
  /// save size of first phase for later reference
  virtual csPoint GetPhase0Size ();
  /// is the two phase layouting enabled ?
  virtual bool TwoPhaseLayoutingEnabled ();
  /// enable or disable 2 phase layouting
  static void SetTwoPhaseLayoutingGlobally (bool on);

  /// new impl. for csComponent
  virtual void Insert (csComponent *child);
  virtual bool HandleEvent (iEvent &Event);
  virtual void Draw ();
  virtual bool SetRect (int xmin, int ymin, int xmax, int ymax);
  virtual void FixSize (int &newWidth, int &newHeight);
};

/**
 * csLayout2 extends csLayout to take the maximum layout size and
 * aligning along the x and y axis into account.
 */
class csLayout2 : public csLayout
{
 public:
  csLayout2 (csComponent *pParent);

  virtual void MaximumLayoutSize (int &w, int &h) = 0;
  virtual float GetLayoutAlignmentX () = 0;
  virtual float GetLayoutAlignmentY () = 0;
};

#endif // __CSLAYOUT_H__