File: MBoxLayout.m

package info (click to toggle)
mysql-query-browser 1.1.6-1sarge0
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 36,320 kB
  • ctags: 24,680
  • sloc: pascal: 203,479; xml: 136,561; ansic: 47,502; cpp: 28,926; sh: 12,433; objc: 4,823; java: 1,849; php: 1,485; python: 1,225; sql: 1,128; makefile: 872
file content (231 lines) | stat: -rw-r--r-- 5,018 bytes parent folder | download | duplicates (6)
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
//
//  MBoxLayout.m
//  MySQLGUICommon
//
//  Created by Alfredo Kojima on Fri Jul 23 2004.
//  Copyright (c) 2004 MySQL AB. All rights reserved.
//

#import "MBoxLayout.h"


@implementation MBoxLayout

- (id)initWithFrame:(NSRect)frame
{
  self= [super initWithFrame:frame];
  if (self)
  {
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(handleFrameChange:)
                                                 name:NSViewFrameDidChangeNotification
                                               object:self];
    [self setPostsFrameChangedNotifications:YES];
  }  
  return self;
}

- (id)init
{
  return [self initWithFrame:NSZeroRect];
}

- (void)dealloc
{
  [[NSNotificationCenter defaultCenter] removeObserver:self];
  [super dealloc];
}

- (void)handleFrameChange:(NSNotification*)notif
{
  [self updateLayout];
}

- (void)setVertical:(BOOL)vertical
{
  _vertical= vertical;
}

- (void)setLeftBorder:(float)leftWidth
            topBorder:(float)topWidth
          rightBorder:(float)rightWidth
         bottomBorder:(float)bottomWidth
{
  _topBorder= topWidth;
  _bottomBorder= bottomWidth;
  _leftBorder= leftWidth;
  _rightBorder= rightWidth;
}

- (void)setAllBorders:(float)width
{
  _topBorder= width;
  _bottomBorder= width;
  _leftBorder= width;
  _rightBorder= width;
}

- (void)setSpacing:(float)spacing
{
  _spacing= spacing;
}

- (void)pack:(NSView*)subview
      expand:(BOOL)expand
{
  [self pack:subview expand:expand minSize:1.0];
}

- (void)pack:(NSView*)subview
      expand:(BOOL)expand
     minSize:(float)minSize
{
  [self addSubview:subview];
  _subviewCount++;
  
  _flags= NSZoneRealloc(NSDefaultMallocZone(), _flags, sizeof(MBoxLayoutAttributes)*_subviewCount);
  _flags[_subviewCount-1].expand= expand;
  _flags[_subviewCount-1].minSize= minSize;
  _flags[_subviewCount-1].normalSize= [subview frame].size;
  
  [self updateLayout];
}

- (void)sizeToFit
{
  int i;
  float s;
  float totalSize;
  float maxOSize;
  NSRect myRect= [self frame];
  
#define SIZE(s) (_vertical ? (s).height : (s).width)
  totalSize= 0.0;
  s= 0.0;
  maxOSize= 0.0;
  for (i= 0; i < _subviewCount; i++)
  {
    MBoxLayoutAttributes *attr= _flags+i;
    if (_vertical)
    {
      totalSize+= attr->normalSize.height + s;
      maxOSize= MAX(attr->normalSize.width, maxOSize);
    }
    else
    {
      totalSize+= attr->normalSize.width + s;
      maxOSize= MAX(attr->normalSize.height, maxOSize);
    }
    s= _spacing;
  }
  if (_vertical)
  {
    myRect.size.height= totalSize + _topBorder+_bottomBorder;
    myRect.size.width= maxOSize + _leftBorder+_rightBorder;
  }
  else
  {
    myRect.size.width= totalSize + _leftBorder+_rightBorder;
    myRect.size.height= maxOSize + _topBorder + _bottomBorder;
  }
  [self setFrame:myRect];
}


- (void)resizeSubviewsWithOldSize:(NSSize)osize
{
  NSLog(@"resize subviews");
}

- (void)updateLayout
{
  int i;
  NSArray *children= [self subviews];
  int expandableViews;
  float totalSize, minSize, slackSize;
  NSRect myRect= [self frame];
  float mySize= _vertical ? myRect.size.height : myRect.size.width;
  float myAvailSize;
  float p, s;
  
  NSLog(@"update layout %@", NSStringFromSize(myRect.size));

  myAvailSize= mySize - (_subviewCount-1) * _spacing;
  if (_vertical)
    myAvailSize-= _topBorder+_bottomBorder;
  else
    myAvailSize-= _leftBorder+_rightBorder;

  expandableViews= 0;
  
  totalSize= 0.0;
  minSize= 0.0;
  for (i= 0; i < _subviewCount; i++)
  {
    id child= [children objectAtIndex:i];
    MBoxLayoutAttributes *attr= _flags+i;
    NSRect rect;
    
    rect= [child frame];
    
    totalSize+= SIZE(attr->normalSize);
    minSize+= attr->minSize;
    
    if (attr->expand)
      expandableViews++;
  }
  if (totalSize <= myAvailSize)
    slackSize= (myAvailSize - totalSize);
  else
    slackSize= (myAvailSize - minSize);

  if (_vertical)
    p= myRect.size.height - _topBorder;
  else
    p= _leftBorder;
  for (i= 0; i < _subviewCount; i++)
  {
    id child= [children objectAtIndex:i];
    MBoxLayoutAttributes *attr= _flags+i;
    NSRect rect= [child frame];

    if (slackSize < 0)
      s= attr->minSize;
    else
    {
      if (attr->expand)
      {
        if (totalSize <= myAvailSize)
          s= SIZE(attr->normalSize) + (slackSize / expandableViews);
        else
          s= attr->minSize + (slackSize / expandableViews);
      }
      else
        s= SIZE(attr->normalSize);
    }
    if (_vertical)
    {
      rect.origin.x= _leftBorder;
      rect.origin.y= p - s;
      rect.size.width= myRect.size.width - (_leftBorder + _rightBorder);
      rect.size.height= s;
    }
    else
    {
      rect.origin.x= p;
      rect.origin.y= _bottomBorder;
      rect.size.width= s;
      rect.size.height= myRect.size.height - (_topBorder + _bottomBorder);
    }

    [child setFrame:rect];
    [child setNeedsDisplay:YES];
    if (_vertical)
      p-= s + _spacing;
    else
      p+= s + _spacing;
  }
  [self setNeedsDisplay:YES];
}

@end