Professional Web Applications Themes

NSTableView and custom field drawing - Mac Programming

i want to create an NSTableView (and/or NSOutlineView) that has fields that display a small icon to the left and some text to the right. that's what safari does in its bookmark view, which i want to duplicate. i figured the way to go about this would be to create my own custom NSCell class, use [NSTableColumn setDataCell:] to attach it to a column, then override [NSCell drawInteriorWithFrame:inView:], and do my custom drawing in there. i got most of the way down that path and realized i have no idea how to get access to the object that the data ...

  1. #1

    Default NSTableView and custom field drawing

    i want to create an NSTableView (and/or NSOutlineView) that has fields
    that display a small icon to the left and some text to the right.
    that's what safari does in its bookmark view, which i want to duplicate.

    i figured the way to go about this would be to create my own custom
    NSCell class, use [NSTableColumn setDataCell:] to attach it to a column,
    then override [NSCell drawInteriorWithFrame:inView:], and do my custom
    drawing in there. i got most of the way down that path and realized i
    have no idea how to get access to the object that the data source
    returns for any particular cell. i suppose i could get a pointer to the
    data source, find the right column, find the right row number, and ask
    for the object myself, but that seems like a ridiculous amount of
    overkill, and would result in the object being retrieved *twice* for
    every draw. (i added print statements to assure myself of this.)

    i also had a look at NSFormatter, but that doesn't seem like it would
    work. the docs talk about converting objects to and from strings, but
    that's not what i want to do.

    so, this is not looking very promising. i can figure out how to define
    custom objects to return from the data source that contain both a string
    and a bitmap, but i don't know how an NSCell inside NSTableColumn gets
    access to those objects. anybody?
    Jhnny Guest

  2. #2

    Default Re: NSTableView and custom field drawing

    Jhnny Fvrt (it means "halo, then resonate") <com>
    wrote:
     

    Consider tableView:willDisplayCell:forTableColumn:row: ?? m.

    --
    matt neuburg, phd = com, http://www.tidbits.com/matt/
    AppleScript: The Definitive Guide
    http://www.amazon.com/exec/obidos/ASIN/0596005571/somethingsbymatt
    Read TidBITS! It's free and smart. http://www.tidbits.com
    matt Guest

  3. #3

    Default Re: NSTableView and custom field drawing

    matt neuburg <com> wrote:
     
    >
    > Consider tableView:willDisplayCell:forTableColumn:row: ?? m.[/ref]

    Or (sorry, forgot about this) override NSTableColumn's dataCellForRow:
    to delegate to the data source with syntax *like*
    tableView:willDisplayCell:forTableColumn:row:. (I was reminded of this
    by a recent note from Mike Ferris on cocoa-dev.) m.


    --
    matt neuburg, phd = com, http://www.tidbits.com/matt/
    AppleScript: The Definitive Guide
    http://www.amazon.com/exec/obidos/ASIN/0596005571/somethingsbymatt
    Read TidBITS! It's free and smart. http://www.tidbits.com
    matt Guest

  4. #4

    Default Re: NSTableView and custom field drawing

    In article <nashville.comcast.net>, it means
    "halo, then resonate <com> wrote:
     

    Your data cell will have its object value set appropriately before
    drawing. In other words, you don't have to worry about it. Just let
    the cell render itself based on its value, and trust the framework to
    do the heavy lifting.

    -- Chris

    --
    Chris Hanson <com>
    http://www.livejournal.com/users/chanson/

    Chris Guest

  5. #5

    Default Re: NSTableView and custom field drawing

    On Wed, 7 Jul 2004, =?ISO-8859-1?Q?J=F8hnny_F=E4v=F2r=EDt=EA_=28it_means_=22halo= 2C_then_resonate=22=29?= wrote:
     

    Your question has already been answered, but I want tot suggest an
    alternate approach. It's possible to get this effect using a standard
    NSTextFieldCell and NSAttributedString with an embedded image. I've done
    this before and it works fine.
    Michael Guest

  6. #6

    Default Re: NSTableView and custom field drawing

    Chris Hanson wrote: 

    i discovered that there is an [NSCell objectValue] method, although i
    haven't tried it yet. i suppose that will work ...
    Jhnny Guest

  7. Moderated Post

    Default Re: NSTableView and custom field drawing

    Removed by Administrator
    Jhnny Guest
    Moderated Post

  8. #8

    Default Re: NSTableView and custom field drawing

    matt neuburg wrote: 

    thanks for the suggestion. i'll give it a try if michael's idea doesn't
    pan out for some reason.

    thanks everybody, as a matter of fact. a very nice range of responses!
    i was afraid i was going to stump the peanut gallery, like i did with my
    last NSTableView question.
    Jhnny Guest

  9. Moderated Post

    Default Re: NSTableView and custom field drawing

    Removed by Administrator
    Michael Guest
    Moderated Post

  10. #10

    Default Re: NSTableView and custom field drawing

    Michael Ash wrote: 

    the understatement of the year! after much scowling at the docs, i got
    it to work, sort of, but my implementation leaves a lot to be desired.
    here's how it looks:

    http://www.beosdevelopers.org/brunsona/png/bad_alignment.png

    as you can see, text in cells with an icon don't line up with the text
    in cells that display nothing but an NSString. further scowling at the
    docs revealed no methods that looked like they would fix that.

    also, i will eventually give the user the ability to change the
    NSTableView's font. will i have to apply the table view's font to the
    NSAttributedString, or can it be expected to follow the table view's
    font without me doing anything? if i *do* have to apply a non-default
    font to my NSAttributedString, how? i don't see anything like
    [NSMutableAttributedString setFont:] or similar.

    i've appended the function i wrote that converts an NSImage and an
    NSString into an NSAttributedString, which is returned from my data
    source. comments welcome ...

    -(id)convert:(CDataCell*)cell
    {
    NSMutableAttributedString* nstr = nil;
    NSTextAttachmentCell* ntac = nil;
    NSTextAttachment* ntat = nil;
    NSAttributedString* spac = nil;
    NSAttributedString* text = nil;

    ntac = [[[NSTextAttachmentCell alloc] init] autorelease];
    [ntac setImage:cell->fBitmap];

    ntat = [[[NSTextAttachment alloc] init] autorelease];
    [ntat setAttachmentCell:ntac];

    spac = [[NSAttributedString alloc] initWithString:" "];
    text = [[NSAttributedString alloc] initWithString:cell->fString];

    [spac autorelease];
    [text autorelease];

    nstr = [NSMutableAttributedString
    attributedStringWithAttachment:ntat];

    [nstr appendAttributedString:spac];
    [nstr appendAttributedString:text];

    return nstr;
    }
    Jhnny Guest

  11. #11

    Default Re: NSTableView and custom field drawing

    On Fri, 9 Jul 2004, =?ISO-8859-1?Q?J=F8hnny_F=E4v=F2r=EDt=EA_=28it_means_=22halo= 2C_then_resonate=22=29?= wrote:
     
    >
    > the understatement of the year! after much scowling at the docs, i got
    > it to work, sort of, but my implementation leaves a lot to be desired.
    > here's how it looks:
    >
    > http://www.beosdevelopers.org/brunsona/png/bad_alignment.png
    >
    > as you can see, text in cells with an icon don't line up with the text
    > in cells that display nothing but an NSString. further scowling at the
    > docs revealed no methods that looked like they would fix that.
    >
    > also, i will eventually give the user the ability to change the
    > NSTableView's font. will i have to apply the table view's font to the
    > NSAttributedString, or can it be expected to follow the table view's
    > font without me doing anything? if i *do* have to apply a non-default
    > font to my NSAttributedString, how? i don't see anything like
    > [NSMutableAttributedString setFont:] or similar.[/ref]

    I also ran into the problem of alignment. Both this and the font can be
    set via attributes. Instead of having a hundred different methods for
    setting all of the various properties of the text, they're all set via
    attributes. Look in the NSAttributedString Additions docs (be careful,
    NSAttributedString is doented in two places, and you want the one in
    AppKit) to find all of the various "official" attributes you can use.
    There's one to change the baseline offset, which you can use to align your
    image, and there's also one for the font. I don't know whether you'll have
    to set the font yourself or not, but I'm guessing you will.
    Michael Guest

  12. #12

    Default Re: NSTableView and custom field drawing

    Michael Ash wrote: 

    well! after having tried it, i am now soured on the NSAttributedString
    method. for me it ranks as the third most annoying thing in cocoa,
    right behind retain/release and NSView's flipped-or-maybe-not-flipped Y
    coordinates. after several hours of fiddling, i couldn't get anywhere
    close to plain old centered vertical alignment, which *should* be the
    default behavior. i could make a better api out of a banana.

    thanks for trying, but if they're going to make it this hard to do
    things the "right" way, then i'm turning rogue. i'll do my own drawing
    after all.
    Jhnny Guest

Similar Threads

  1. custom field in verity
    By davidihill in forum Coldfusion - Advanced Techniques
    Replies: 0
    Last Post: May 14th, 12:13 AM
  2. Drawing in Flex? Drawing and AS for custom compon
    By yoway in forum Macromedia Flex General Discussion
    Replies: 0
    Last Post: April 18th, 10:19 PM
  3. Custom Value in Field Name HOW TO?
    By tracycholzer@adobeforums.com in forum Adobe Acrobat Windows
    Replies: 12
    Last Post: April 13th, 02:34 PM
  4. Navigating in NSTableView
    By Loic in forum Mac Programming
    Replies: 3
    Last Post: October 23rd, 12:23 AM
  5. [Cocoa] Bug with NSTableView?
    By Mark Bee in forum Mac Programming
    Replies: 1
    Last Post: July 7th, 07:38 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

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