File: MSplitView.m

package info (click to toggle)
mysql-gui-tools 5.0r14%2BopenSUSE-2.1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 116,956 kB
  • ctags: 48,715
  • sloc: sql: 341,918; pascal: 276,698; ansic: 91,020; cpp: 90,451; objc: 33,236; sh: 29,481; yacc: 10,756; xml: 10,589; java: 10,079; php: 2,806; python: 2,092; makefile: 1,783; perl: 4
file content (222 lines) | stat: -rw-r--r-- 5,612 bytes parent folder | download | duplicates (4)
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
//
//  MSplitView.m
//  MySQLGUICommon
//
//  Created by Alfredo Kojima on 5/6/05.
//  Copyright 2005 MySQL AB. All rights reserved.
//

#import "MSplitView.h"


static void blendFunction(void *info, const float *in, float *out)
{
  float *fromColors= (float*)info;
  float *toColors= (float*)info + 4;
  
  out[0] = (1.0 - *in) * fromColors[0] + *in * toColors[0];
  out[1] = (1.0 - *in) * fromColors[1] + *in * toColors[1];
  out[2] = (1.0 - *in) * fromColors[2] + *in * toColors[2];
  out[3] = (1.0 - *in) * fromColors[3] + *in * toColors[3];
}

static const CGFunctionCallbacks blendCallbacks = {0, &blendFunction, &free};

static const float domainAndRange[8] = {0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0};

@implementation MSplitView

- (void)dealloc
{
  [_inactiveColor release];
  [super dealloc];
}

- (float)dividerThickness
{
  if (_thickness > 0.0)
    return _thickness;
  else
    return [super dividerThickness];
}

- (void)drawDividerInRect:(NSRect)aRect
{
  if (_thickness < 4.0)
  {
    NSRect topR, botR;
    
    NSDivideRect(aRect,&topR,&botR,1.0,NSMaxYEdge);

    [[NSColor lightGrayColor] set];
    NSRectFill(botR);
    [[NSColor darkGrayColor] set];
    NSRectFill(topR);
  }
  else if (_dividerColor)
  {
    CGContextRef context = [[NSGraphicsContext currentContext] graphicsPort];
    CGColorSpaceRef colorSpace= CGColorSpaceCreateDeviceRGB();    
    float hue, saturation, brightness, alpha;
    [[_dividerColor colorUsingColorSpaceName:NSDeviceRGBColorSpace] getHue:&hue saturation:&saturation brightness:&brightness alpha:&alpha];
    float *colors= malloc(sizeof(float)*6);
    NSColor *light = [NSColor colorWithDeviceHue:hue saturation:MAX(0.0, saturation-0.1) brightness:MIN(1.0, brightness+0.1) alpha:alpha];
    NSColor *dark = [NSColor colorWithDeviceHue:hue saturation:MIN(1.0, (saturation > 0.04) ? saturation+0.10 : 0.0) brightness:brightness alpha:alpha];

    [[light colorUsingColorSpaceName:NSDeviceRGBColorSpace] getRed:colors+0 green:colors+1 blue:colors+2 alpha:colors+3];
    [[dark colorUsingColorSpaceName:NSDeviceRGBColorSpace] getRed:colors+4 green:colors+5 blue:colors+6 alpha:colors+7];

    CGFunctionRef functionRef = CGFunctionCreate(colors, 1, domainAndRange, 4, domainAndRange, &blendCallbacks);

    CGContextSaveGState(context);
    {
      CGRect crect= {{NSMinX(aRect), NSMinY(aRect)+1.0}, {NSWidth(aRect), NSHeight(aRect)-1.0}};
      CGContextClipToRect(context, crect);
      CGShadingRef shade;
      
      if ([self isVertical])
        shade= CGShadingCreateAxial(colorSpace, 
                                    CGPointMake(NSMinX(aRect), 0),
                                    CGPointMake(NSMaxX(aRect), 0),
                                    functionRef, NO, NO);
      else
        shade= CGShadingCreateAxial(colorSpace, 
                                    CGPointMake(0, NSMinY(aRect)), 
                                    CGPointMake(0, NSMaxY(aRect)), 
                                    functionRef, NO, NO);
      CGContextDrawShading(context, shade);
      CGShadingRelease(shade);
    } 
    CGContextRestoreGState(context);
    
    CGFunctionRelease(functionRef);
    CGColorSpaceRelease(colorSpace);
    
    [super drawDividerInRect:aRect];
  }
  else
    [super drawDividerInRect:aRect];
}

- (void)setDividerThickness:(float)value
{
  _thickness= value;
}

- (void)setDividerColor:(NSColor*)color
{
  if (_dividerColor != color)
  {
    [_dividerColor release];
    _dividerColor= [color retain];
  }
}

- (void)setActive:(BOOL)active
{
  _active= active;
}

- (void)setInactiveColor:(NSColor*)color
{
  if (_inactiveColor != color)
  {
    [_inactiveColor release];
    _inactiveColor= [color retain];
  }
}

- (void)drawRect:(NSRect)aRect
{
  if (_inactiveColor)
  {
    if (_active)
      [[NSColor whiteColor] set];
    else
      [_inactiveColor set];
    NSRectFill(aRect);
  }
  [super drawRect:aRect];
}

- (void)resizeSubview:(id)view toSize:(float)size
{
  NSArray *subviews= [self subviews];
  NSMutableArray *ratios= [NSMutableArray arrayWithCapacity:[subviews count]];
  unsigned int i, c= [subviews count];
  float s, offs, totalSize;
  float divThick= [self dividerThickness];
  float leftOver;

  totalSize= 0.0;
  // 1st calculate how much space are the subviews using relative to each other
  for (i= 0; i < c; i++)
  {
    id subview= [subviews objectAtIndex:i];
    float t;
    if (![self isVertical])
      t= NSHeight([subview frame]);
    else
      t= NSWidth([subview frame]);
    totalSize+= t;
  }
  totalSize+= (c-1)*divThick;

  for (i= 0; i < c; i++)
  {
    id subview= [subviews objectAtIndex:i];
    float t;
    if (![self isVertical])
      t= NSHeight([subview frame]);
    else
      t= NSWidth([subview frame]);
    [ratios addObject:[NSNumber numberWithFloat:t/totalSize]];
  }
  
  if (size > totalSize)
    size= totalSize;
  
  leftOver= totalSize - size;

  offs= 0.0;
  // then resize all subviews
  for (i= 0; i < c; i++)
  {
    id subview= [subviews objectAtIndex:i];
    NSRect f= [subview frame];

    s= [[ratios objectAtIndex:i] floatValue] * totalSize;
    
    if (subview == view)
    {
      if (![self isVertical])
      {
        f.origin.y= offs;
        f.size.height= size;
      }
      else
      {
        f.origin.x= offs;
        f.size.width= size;
      }
    }
    else
    {
      if (![self isVertical])
      {
        f.origin.y= offs;
        f.size.height= s;
      }
      else
      {
        f.origin.x= offs;
        f.size.width= s;
      }
    }
    [subview setFrame:f];
    offs+= s + divThick;
  }
  [self setNeedsDisplay:YES];
}

@end