Transparent window and opaque objects

  • Hi,
    I set my window with a transparent background

        [win setBackgroundColor:[NSColor clearColor]];
        [win setOpaque:NO];
        [win setHasShadow:NO];
        [[win contentView] addSubview:aView];

    The aView contains an NSImageView and some white NSTextField labels.
    The NSImageView contains a rounded corner rectangle gray image with alpha
    0.5. My intention is to see an half a transparent rounded corner pop-up
    window with some pure-white text over.
    In IB I can quite see the half-transparent picture within the NSImageView,
    and the white text OVER the image. Instead when I launch the application,
    the white text gets multiplied (I guess) by the image alpha value and
    results in a dark gray color. What did I forget? Any idea?

    Best Regards
    --
    Lorenzo
    email: <archidea...>
  • On 10/24/07, Lorenzo <archidea...> wrote:
    > Hi,
    > I set my window with a transparent background
    >
    > [win setBackgroundColor:[NSColor clearColor]];
    > [win setOpaque:NO];
    > [win setHasShadow:NO];
    > [[win contentView] addSubview:aView];
    >
    > The aView contains an NSImageView and some white NSTextField labels.
    > The NSImageView contains a rounded corner rectangle gray image with alpha
    > 0.5. My intention is to see an half a transparent rounded corner pop-up
    > window with some pure-white text over.
    > In IB I can quite see the half-transparent picture within the NSImageView,
    > and the white text OVER the image. Instead when I launch the application,
    > the white text gets multiplied (I guess) by the image alpha value and
    > results in a dark gray color. What did I forget? Any idea?

    The display order of sibling views is undefined in Tiger and earlier.
    So you are likely getting your text fields rendered before the image
    view renders.

    I suggest that you consider using a custom view to render you
    background image in and then add your text fields as sub-views of that
    custom view.

    In the short term you could try to add the text fields as subviews of
    the image view (have to do that using code, IB wont help with this).

    -Shawn
  • Hi, Lorenzo,

    It sounds as though you've placed the text field views (overlapped)
    on the image view.  See this post:

    <http://lists.apple.com/archives/cocoa-dev/2007/Mar/msg00961.html>

    To quote, it has a ref to Apple's docs, that says:

    > "Note: For performance reasons, Cocoa does not enforce clipping
    > among sibling views or guarantee correct invalidation and drawing
    > behavior when sibling views overlap. If you want a view to be drawn
    > in front of another view, you should make the front view a subview
    > (or descendant) of the rear view."

    Additionally, the poster (Michael Watson) says:

    > There are things in IB and NSView that appear to be planning for
    > future additions, but in the end, the documentation says not to
    > overlap views.

    So we may (I have no knowledge about this) see something new in
    Leopard, or we may not.  Time will tell.

    As for your issue, you may wish to try to draw the text onto the
    image that you're trying to display, rather than placing text fields
    above the image.

    If we knew more about what you were trying to do (maybe a diagram or
    screenshot?), maybe a more dynamic (as in, for updating the text)
    solution could be conceived.

    Best,
        Andrew

    On Oct 24, 2007, at 3:14 PM, Lorenzo wrote:

    > Hi,
    > I set my window with a transparent background
    >
    > [win setBackgroundColor:[NSColor clearColor]];
    > [win setOpaque:NO];
    > [win setHasShadow:NO];
    > [[win contentView] addSubview:aView];
    >
    > The aView contains an NSImageView and some white NSTextField labels.
    > The NSImageView contains a rounded corner rectangle gray image with
    > alpha
    > 0.5. My intention is to see an half a transparent rounded corner
    > pop-up
    > window with some pure-white text over.
    > In IB I can quite see the half-transparent picture within the
    > NSImageView,
    > and the white text OVER the image. Instead when I launch the
    > application,
    > the white text gets multiplied (I guess) by the image alpha value and
    > results in a dark gray color. What did I forget? Any idea?
    >
    >
    > Best Regards
    > --
    > Lorenzo
    > email: <archidea...>
  • Thank you Shawn. Your suggestions made me think about the trick I used to
    solve an overlapping problem. I solved it reordering the subviews.

    I iterate through the subviews looking for the NSImageView obj and I store
    its index. Then I delete all the subviews. Now I addSubview:NSImageView (as
    first) then I add all the other objects. So the NSView gets drawn as first.
    It works like a charm. Thank you so much!

    Best Regards
    --
    Lorenzo
    email: <archidea...>

    > From: Shawn Erickson <shawnce...>
    > Date: Wed, 24 Oct 2007 15:27:24 -0700
    > To: Lorenzo <archidea...>
    > Cc: <cocoa-dev...>
    > Subject: Re: Transparent window and opaque objects
    >
    > On 10/24/07, Lorenzo <archidea...> wrote:
    >> Hi,
    >> I set my window with a transparent background
    >>
    >> [win setBackgroundColor:[NSColor clearColor]];
    >> [win setOpaque:NO];
    >> [win setHasShadow:NO];
    >> [[win contentView] addSubview:aView];
    >>
    >> The aView contains an NSImageView and some white NSTextField labels.
    >> The NSImageView contains a rounded corner rectangle gray image with alpha
    >> 0.5. My intention is to see an half a transparent rounded corner pop-up
    >> window with some pure-white text over.
    >> In IB I can quite see the half-transparent picture within the NSImageView,
    >> and the white text OVER the image. Instead when I launch the application,
    >> the white text gets multiplied (I guess) by the image alpha value and
    >> results in a dark gray color. What did I forget? Any idea?
    >
    > The display order of sibling views is undefined in Tiger and earlier.
    > So you are likely getting your text fields rendered before the image
    > view renders.
    >
    > I suggest that you consider using a custom view to render you
    > background image in and then add your text fields as sub-views of that
    > custom view.
    >
    > In the short term you could try to add the text fields as subviews of
    > the image view (have to do that using code, IB wont help with this).
    >
    > -Shawn
  • On Oct 24, 2007, at 6:44 PM, Lorenzo wrote:

    > Thank you Shawn. Your suggestions made me think about the trick I
    > used to
    > solve an overlapping problem. I solved it reordering the subviews.
    >
    > I iterate through the subviews looking for the NSImageView obj and
    > I store
    > its index. Then I delete all the subviews. Now I
    > addSubview:NSImageView (as
    > first) then I add all the other objects. So the NSView gets drawn
    > as first.
    > It works like a charm. Thank you so much!

    There is no 'trick' to solving overlapping view problems.
    Overlapping views are not supported at all.  If you continue to go
    that route, don't be surprised if some hardware/OS/etc combo will
    cause problems for your app.

    Valid alternatives have already been mentioned in earlier replies

    ___________________________________________________________
    Ricky A. Sharp        mailto:<rsharp...>
    Instant Interactive(tm)  http://www.instantinteractive.com
  • Hi Ricky,
    On the docs I have found the following:

    ===============================
    - (NSArray *)subviews
    Discussion
    The order of the subviews may be considered as being back-to-front, but this
    does not imply invalidation and drawing behavior. The order is based on the
    order of the receiver's subviews as specified in the nib file from which
    they were unarchived or the programmatic interface for modifying the
    receiver's subview list. This ordering is also the reverse of the order in
    which hit-testing is done.
    ===============================

    Something is not so clear there. Does the OS draw the objects from the index
    0 to the last index? If so, I can quite assume that the object 1 will be
    drawn over the object 0. Actually it seems to work right this way.
    Also, I just need to show a half-a-transparent pop-up under the mouse
    containing some text fields. So I setIntValue of the NSTextFields then I
    call [textField setNeedsDisplay:YES]; It works.

    May you please explain what I should be aware?

    Best Regards
    --
    Lorenzo
    email: <archidea...>

    > From: Ricky Sharp <rsharp...>
    > Date: Wed, 24 Oct 2007 18:59:27 -0500
    > To: Lorenzo <archidea...>
    > Cc: Shawn Erickson <shawnce...>, <cocoa-dev...>
    > Subject: Re: Transparent window and opaque objects
    >
    >
    > On Oct 24, 2007, at 6:44 PM, Lorenzo wrote:
    >
    >> Thank you Shawn. Your suggestions made me think about the trick I
    >> used to
    >> solve an overlapping problem. I solved it reordering the subviews.
    >>
    >> I iterate through the subviews looking for the NSImageView obj and
    >> I store
    >> its index. Then I delete all the subviews. Now I
    >> addSubview:NSImageView (as
    >> first) then I add all the other objects. So the NSView gets drawn
    >> as first.
    >> It works like a charm. Thank you so much!
    >
    > There is no 'trick' to solving overlapping view problems.
    > Overlapping views are not supported at all.  If you continue to go
    > that route, don't be surprised if some hardware/OS/etc combo will
    > cause problems for your app.
    >
    > Valid alternatives have already been mentioned in earlier replies
    >
    > ___________________________________________________________
    > Ricky A. Sharp        mailto:<rsharp...>
    > Instant Interactive(tm)  http://www.instantinteractive.com
    >
  • On Oct 25, 2007, at 10:16 AM, Lorenzo wrote:

    > Something is not so clear there. Does the OS draw the objects from
    > the index
    > 0 to the last index?
    Yes, No, Maybe - it isn't documented one way or the other.

    > If so, I can quite assume that the object 1 will be
    > drawn over the object 0.
    No - it isn't documented one way or the other.

    > Actually it seems to work right this way.
    Only on your specific OS version and video card and your specific
    usage/refreshing history.  On somebody else computer, or if they do
    different things, different results will happen.

    >
    > May you please explain what I should be aware?
    >

    As was posted earlier:
    > "Note: For performance reasons, Cocoa does not enforce clipping
    > among sibling views or guarantee correct invalidation and drawing
    > behavior when sibling views overlap. If you want a view to be drawn
    > in front of another view, you should make the front view a subview
    > (or descendant) of the rear view."

    Just because it works for you doesn't mean it will work for others.
    Don't rely on overlapping views working in any consistent fashion on
    any currently shipping OS (at least as of 25 Oct 07).

    Glenn Andreas                      <gandreas...>
      <http://www.gandreas.com/> wicked fun!
    quadrium2 | build, mutate, evolve, animate  | images, textures,
    fractals, art
  • The OS doesn't draw objects.  Objects draw objects.  Cocoa is not technically part of any operating system.

      You can not make any assumptions about the order in which sibling views will be drawn in Tiger or previous Cocoa versions.  It is that simple.  The documentation passage you quote says quite explicitly, "but this does not imply invalidation and drawing behavior."

      Would you be happier if the subviews were stored in an unordered set instead of an array ?  Why does the implementation detail (that an array is used) cause you to draw conclusions about totally unrelated behavior ?

      The documentation also says: "Note: For performance reasons, Cocoa does not enforce clipping among sibling views or guarantee correct invalidation and drawing behavior
    when sibling views overlap. If you want a view to be drawn in front of  another view, you should make the front view a subview (or descendant)  of the rear view."

      http://developer.apple.com/documentation/Cocoa/Conceptual/CocoaViewsGuide/W
    orkingWithAViewHierarchy/chapter_4_section_5.html
previous month october 2007 next month
MTWTFSS
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        
Go to today