Skip to content

Commit

Permalink
Various fixes to support creating images with specifying the size upf…
Browse files Browse the repository at this point in the history
…ront.

[cappuccino#41 milestone:0.5.2 state:resolved];
[#20 milestone:0.5.2 state:resolved];
  • Loading branch information
Ross Boucher committed Sep 18, 2008
1 parent dbc0999 commit 334776d
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 23 deletions.
13 changes: 10 additions & 3 deletions AppKit/CPImage.j
Expand Up @@ -20,10 +20,11 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

import <Foundation/CPObject.j>
import <Foundation/CPString.j>
import <Foundation/CPBundle.j>
import <Foundation/CPNotificationCenter.j>
import <Foundation/CPObject.j>
import <Foundation/CPRunLoop.j>
import <Foundation/CPString.j>

import "CPGeometry.j"

Expand All @@ -35,6 +36,8 @@ CPImageLoadStatusInvalidData = 4;
CPImageLoadStatusUnexpectedEOF = 5;
CPImageLoadStatusReadError = 6;

CPImageDidLoadNotification = @"CPImageDidLoadNotification";

@implementation CPBundle (CPImageAdditions)

- (CPString)pathForResource:(CPString)aFilename
Expand Down Expand Up @@ -219,7 +222,11 @@ CPImageLoadStatusReadError = 6;
// FIXME: IE is wrong on image sizes????
if (!_size || (_size.width == -1 && _size.height == -1))
_size = CGSizeMake(_image.width, _image.height);


[[CPNotificationCenter defaultCenter]
postNotificationName:CPImageDidLoadNotification
object:self];

if ([_delegate respondsToSelector:@selector(imageDidLoad:)])
[_delegate imageDidLoad:self];

Expand Down
51 changes: 35 additions & 16 deletions AppKit/CPImageView.j
Expand Up @@ -20,8 +20,10 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

import "CPImage.j"
import <Foundation/CPNotificationCenter.j>

import "CPControl.j"
import "CPImage.j"
import "CPShadowView.j"

#include "Platform/Platform.h"
Expand Down Expand Up @@ -110,20 +112,35 @@ var LEFT_SHADOW_INSET = 3.0,
if (_image == anImage)
return;

_image = anImage;
var center = [CPNotificationCenter defaultCenter];

if (_image)
[center removeObserver:self name:CPImageDidLoadNotification object:_image];

_image = anImage;
_DOMImageElement.src = [anImage filename];

var size = [_image size];

if (_imageScaling == CPScaleNone && _image)
if (size && size.width == -1 && size.height == -1)
{
var size = [_image size];
[center addObserver:self selector:@selector(imageDidLoad:) name:CPImageDidLoadNotification object:_image];

_DOMImageElement.width = 0;
_DOMImageElement.height = 0;

_DOMImageElement.width = ROUND(size.width);
_DOMImageElement.height = ROUND(size.height);
[_shadowView setHidden:YES];
}

else
{
[self hideOrDisplayContents];
[self tile];
}
}

- (void)imageDidLoad:(CPNotification)aNotification
{
[self hideOrDisplayContents];

[self tile];
}

Expand Down Expand Up @@ -181,13 +198,6 @@ var LEFT_SHADOW_INSET = 3.0,
{
CPDOMDisplayServerSetStyleLeftTop(_DOMImageElement, NULL, 0.0, 0.0);
}
else if (_imageScaling == CPScaleNone && _image)
{
var size = [_image size];

_DOMImageElement.width = ROUND(size.width);
_DOMImageElement.height = ROUND(size.height);
}

[self tile];
}
Expand Down Expand Up @@ -240,7 +250,7 @@ var LEFT_SHADOW_INSET = 3.0,
{
if (!_image)
return;

var bounds = [self bounds],
x = 0.0,
y = 0.0,
Expand All @@ -260,6 +270,9 @@ var LEFT_SHADOW_INSET = 3.0,
{
var size = [_image size];

if (size.width == -1 && size.height == -1)
return;

if (_imageScaling == CPScaleProportionally)
{
// The max size it can be is size.width x size.height, so only
Expand Down Expand Up @@ -289,6 +302,12 @@ var LEFT_SHADOW_INSET = 3.0,
height = size.height;
}

if (_imageScaling == CPScaleNone)
{
_DOMImageElement.width = ROUND(size.width);
_DOMImageElement.height = ROUND(size.height);
}

var x = (boundsWidth - width) / 2.0,
y = (boundsHeight - height) / 2.0;

Expand Down
8 changes: 4 additions & 4 deletions Foundation/CPNotificationCenter.j
Expand Up @@ -219,7 +219,7 @@ var _CPNotificationCenterPostNotification = function(/* CPNotificationCenter */
while (key = [keys nextObject])
{
var observers = [_objectObservers objectForKey:key],
count = observers.length;
count = observers ? observers.length : 0;

while (count--)
if ([observers[count] observer] == anObserver)
Expand All @@ -231,15 +231,15 @@ var _CPNotificationCenterPostNotification = function(/* CPNotificationCenter */
observers.splice(count, 1);
}

if (observers.length == 0)
if (!observers || observers.length == 0)
removedKeys.push(key);
}
}
else
{
var key = [anObject hash],
observers = [_objectObservers objectForKey:key];
count = observers.length;
count = observers ? observers.length : 0;

while (count--)
if ([observers[count] observer] == anObserver)
Expand All @@ -251,7 +251,7 @@ var _CPNotificationCenterPostNotification = function(/* CPNotificationCenter */
observers.splice(count, 1)
}

if (observers.length == 0)
if (!observers || observers.length == 0)
removedKeys.push(key);
}

Expand Down

0 comments on commit 334776d

Please sign in to comment.