Skip to content
This repository

hitTest not working properly after first row in CPTableView #1092

Open
statonjr opened this Issue January 12, 2011 · 4 comments

3 participants

Larry Staton Jr. CappBot Andrew Hankinson
Larry Staton Jr.

I'm using Cappuccino 0.9.0 RC1. I have a CPTableView inside of a CPScrollView. The table has one column and 30 rows and has variable row height. The data view for the column is a view that contains 7 subviews. In the data view subclass, I override mouseDown and hitTest for clicks within certain subviews.

When I click on a certain subview in a row, I expect to receive a CPView back. When I click in the first row of the table, [self hitTest:pointInView] works as expected and returns the CPView. When I click on the same subview in the second row, [self hitTest:pointInView] returns null. I observed the same behavior in the remaining rows.

ViewController.j

// A CPTableColumn contains a dataview to display for its column of the CPTableView
// The default dataview is a CPTextField but you can set it to any view you'd like. See -setDataView:
var dataViewPrototype = [[MessageDataView alloc] initWithFrame:CGRectMake(0,0,CGRectGetWidth([messagesTableView bounds]),100)];
[dataViewPrototype setHitTests:YES];
[dataViewPrototype setAutoresizingMask:CPViewWidthSizable];
[messagesColumn setDataView:dataViewPrototype];

MessageDataView.j

@implementation MessageDataView : CPView
 {
    CPTextField messageUser;
    CPTextField messageCreatedAt;
    MessageTextView messageBody;
    CPTextField reply;
    CPTextField like;
    CPTextField flag;
    CPImageView messageAvatarView;

     JSObject    message;
}

- (id)initWithFrame:(CGRect)aFrame
{
    if (self = [super initWithFrame:aFrame]) {
        messageUser = [[CPTextField alloc] initWithFrame:CGRectMake(65, 5, 100, 20)];
        [messageUser setFont:[CPFont boldSystemFontOfSize:13]];
        [messageUser setTag:@"user"];

        messageCreatedAt = [[CPTextField alloc] initWithFrame:CGRectMake(65, 85, 120, 20)];
        [messageCreatedAt setAutoresizingMask:CPViewMinYMargin|CPViewMaxXMargin];
        [messageCreatedAt setFont:[CPFont systemFontOfSize:11]];
        [messageCreatedAt setTextColor:[CPColor grayColor]];

        reply = [[CPTextField alloc] initWithFrame:CGRectMake(132, 85, 32, 20)];
        [reply setAutoresizingMask:CPViewMinYMargin|CPViewMaxXMargin|CPViewMinXMargin];
        [reply setFont:[CPFont systemFontOfSize:11]];
        [reply setTextColor:[CPColor colorWithHexString:@"0099FF"]];
        [reply setAlignment:CPRightTextAlignment];
        [reply setStringValue:@"Reply"];
        [reply setTag:@"reply"];

        like = [[CPTextField alloc] initWithFrame:CGRectMake(163, 85, 25, 20)];
        [like setAutoresizingMask:CPViewMinYMargin|CPViewMaxXMargin];
        [like setFont:[CPFont systemFontOfSize:11]];
        [like setStringValue:@"Like"];
        [like setTextColor:[CPColor colorWithHexString:@"0099FF"]];
        [like setAlignment:CPRightTextAlignment];

        flag = [[CPTextField alloc] initWithFrame:CGRectMake(187, 85, 25, 20)];
        [flag setAutoresizingMask:CPViewMinYMargin|CPViewMaxXMargin];
        [flag setFont:[CPFont systemFontOfSize:11]];
        [flag setStringValue:@"Flag"];
        [flag setTextColor:[CPColor colorWithHexString:@"0099FF"]];
        [flag setAlignment:CPRightTextAlignment];

        messageBody = [[MessageTextView alloc] initWithFrame:CGRectMake(66, 25, 220, 20)];

        messageAvatarView = [[CPImageView alloc] initWithFrame:CGRectMake(5, 5, 50, 50)];

        [self setSubviews:[messageUser, messageCreatedAt, messageBody, messageAvatarView, reply, like, flag]];

    }

    return self;
}

- (void)setValue:(id)value forKey:(id)bindingPath
{
    [self setObjectValue:value];
}

- (void)setObjectValue:(id)aMessage
{
    if (!aMessage) {
        return;
    }

    message = aMessage;

    [self setTag:[aMessage valueForKey:@"id"]];

    [messageUser setStringValue:[aMessage valueForKey:@"user_login"]];

    var messageDate = [CPDate dateWithDateTimeString:[aMessage valueForKey:@"created_at"]];
    [messageCreatedAt setStringValue:[messageDate relativeDateSinceNow]];
    [messageCreatedAt sizeToFit];

    [messageBody setStringValue:[aMessage valueForKey:@"body"]];

    var frame = [messageBody frame],
          height = [self bounds].size.height - 35;

    frame.size.height = height;

    [messageBody setFrame:frame];

    var userImage = [[CPImage alloc] initWithContentsOfFile:[aMessage valueForKey:@"avatar_url"] size:CGSizeMake(50,50)];
    [messageAvatarView setImage:userImage];
}

- (id)objectValue
{
}

- (void)mouseDown:(CPEvent)anEvent
{
     var messageID = self._DOMElement.attributes[1].nodeValue;
    // Hit test for the mouse location
    // Convert from window coordinates to view coordinates
    var pointInView = [self convertPoint:[anEvent locationInWindow] fromView:nil];
    console.log(self._frame, pointInView, CPRectContainsPoint(self._frame, pointInView));
    console.log(self, [self hitTest:pointInView]);
    console.log([[self hitTest:pointInView] tag]);

    switch([[self hitTest:pointInView] tag])
    {
        case "user": console.log("launch user!");
                           break;
        case "reply": console.log("reply to message");
                            break;
    }

}

- (void)encodeWithCoder:(CPCoder)aCoder
{
    [super encodeWithCoder:aCoder];

    [aCoder encodeObject:messageUser forKey:"messageUser"];
    [aCoder encodeObject:messageCreatedAt forKey:"messageCreatedAt"];
    [aCoder encodeObject:messageBody forKey:"messageBody"];
    [aCoder encodeObject:messageAvatarView forKey:"messageAvatarView"];
    [aCoder encodeObject:reply forKey:"reply"];
    [aCoder encodeObject:like forKey:"like"];
    [aCoder encodeObject:flag forKey:"flag"];
}

- (id)initWithCoder:(CPCoder)aCoder
{
    self = [super initWithCoder:aCoder];

    if (self)
    {
        messageUser       = [aCoder decodeObjectForKey:"messageUser"];
        messageCreatedAt  = [aCoder decodeObjectForKey:"messageCreatedAt"];
        messageBody       = [aCoder decodeObjectForKey:"messageBody"];
        messageAvatarView = [aCoder decodeObjectForKey:"messageAvatarView"];
        reply             = [aCoder decodeObjectForKey:"reply"];
        like              = [aCoder decodeObjectForKey:"like"];
        flag              = [aCoder decodeObjectForKey:"flag"];
    }

    return self;
}
@end
CappBot
Collaborator
cappbot commented May 09, 2012

Label: #new. What's next? A reviewer should examine this issue.

CappBot
Collaborator

Milestone: Someday. Label: #new. What's next? A reviewer should examine this issue.

Andrew Hankinson

This issue needs the above code converted to a working reduction, please.

-#new
+bug
+#needs-confirmation
+#needs-reduction

CappBot
Collaborator

Milestone: Someday. Labels: #needs-confirmation, #needs-reduction, bug. What's next?

  • This issue needs a volunteer to independently reproduce the issue.
  • A minimal test app should be created which demonstrates the concern of this issue in isolation.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.