Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
General
GeneralPortable MacsHardwareNetworking
Applications
Mac ApplicationsEudoraFirefox / MozillaInternet ExplorerOutlook ExpressMS OfficeEntourageExcelPowerPointWordVirtual PCMedia PlayerOther MS Products
Programming
Mac ProgrammingCodeWarriorPerl
Country Specific
Australian Mac GroupUK Mac Group

Mac Forum / Programming / Mac Programming / December 2005



Tip: Looking for answers? Try searching our database.

NSWindow -- Where To Put Content-Drawing Code?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
DarelRex@gmail.com - 25 Dec 2005 17:20 GMT
I'm very new to Cocoa and Xcode, so this is a pretty basic question,
but I've searched the web and Apple's built-in documentation for
several hours now with no luck.

When a window redraws, I want to execute the following code to draw its
content region:

   [imageRep draw];

Wow, what could be simpler?  But where do I put that code (or any other
drawing code I might later write) so that it is executed when my window
is refreshed?  I have read about delegation and notification, and get
nothing but confused.  Everything I try generates unhelpful compile
errors.  I tried looking in the AppKit examples, but couldn't find any
that answered this question.

As an experiment, I hooked up a button in another window to an IBAction
that did this:

   NSView *myView=[imageWindow contentView];
   [myView lockFocus];
   [imageRep draw];
   [myView unlockFocus];

That works! -- but only when I click the button.  And when imageWindow
is miniaturized into the dock and then restored, my image is gone, of
course.

All I need is a simple code example that illustrates where my "draw"
command needs to reside in my project so that it gets executed every
time my window is drawing its content area.

Thanks much, and please CC your reply to <DarelRex@gmail.com> if you
don't mind.  I'll check back at this newsgroup periodically.

Darel
Uli Kusterer - 25 Dec 2005 18:30 GMT
> I'm very new to Cocoa and Xcode, so this is a pretty basic question,
> but I've searched the web and Apple's built-in documentation for
> several hours now with no luck.

You don't draw into windows or views like that. You subclass NSView and
do all your drawing in its drawRect: method. If you already have an
NSImage (or NSImageRep which you can easily wrap in an NSImage), you'd
probably want to just use an NSImageView and let it take care of drawing
your image, though.

Cheers,
-- Uli Kusterer
http://www.zathras.de
DarelRex@gmail.com - 25 Dec 2005 18:39 GMT
Uli,

Thanks for the quick reply, but I really need a code sample.  I've read
about NSViews, subclassing, etc., and I'm just banging my head into the
wall trying to get it to work.  (Vague compile errors about things
being undefined or improperly used.)  Do you know where I can find a
simple, little code example of the technique you are describing?  I'm
finding that Apple's built-in documentation is woefully lacking of any
code samples at all, except for their Examples/AppKit projects.

In the meantime, I will read-up on NSImageView -- I think that's one
object I didn't know about, and have not yet looked at.

Darel

> > I'm very new to Cocoa and Xcode, so this is a pretty basic question,
> > but I've searched the web and Apple's built-in documentation for
[quoted text clipped - 9 lines]
> -- Uli Kusterer
> http://www.zathras.de
DarelRex@gmail.com - 25 Dec 2005 19:35 GMT
Uli,

IT WORKED!  You're a genius.  I made a borderless NSImageView in my
window, made it the same size as the window, and then put in code to
hook it up to a new NSImage object that I connect to my imagerep.  And
bingo, I don't even have to draw the content at all -- it's just there
all the time.

Thank you very much, now I'm back in action writing my app.

Darel
Uli Kusterer - 29 Dec 2005 10:15 GMT
> Do you know where I can find a
> simple, little code example of the technique you are describing?  I'm
> finding that Apple's built-in documentation is woefully lacking of any
> code samples at all, except for their Examples/AppKit projects.

If you don't have it already, try to get Aaron Hillegass' book on Cocoa
programming (the first one (2nd edition), not the new 'advanced' one).
It covers all the structural basics of Cocoa programming as a nice
walkthrough-style tutorial. Apple's Currency Converter and Moon Travel
Tutorial never helped me understand anything, but after Hillegass it was
crystal-clear. Once you know what he tells you, it gets a lot easier to
understand how the reference docs are structured.

Cheers,
-- Uli
http://www.zathras.de
Michael Ash - 25 Dec 2005 19:38 GMT
> I'm very new to Cocoa and Xcode, so this is a pretty basic question,
> but I've searched the web and Apple's built-in documentation for
[quoted text clipped - 27 lines]
> command needs to reside in my project so that it gets executed every
> time my window is drawing its content area.

I would highly recommend that you check out a Cocoa tutorial. There's a
good list here, although others probably know better places to find them
than I do:

http://www.cocoadev.com/index.pl?CocoaTutorials

You may prefer dead-tree material, and there's a good list here:

http://www.cocoadev.com/index.pl?CocoaBooks

I know that it's tempting to ignore the holistic view and jump right in,
and try to find specific help with specific problems, but it doesn't
really work that way. For large APIs in general, and Cocoa in particular,
you need a certain base. That is to say, before you can do anything, you
have to know a fair chunk of everything. Rather than concentrating on this
single problem, following a tutorial or book that's oriented towards
beginners will help you along the path to knowing a fair chunk of
everything in a relatively painless fashion.

If you really insist on going directly into this one thing, check out the
docs. Aside from the API references that cover "what does X do?", Apple
has a lot of excellent conceptual documentation that covers questions like
"how do I do X?" Your particular question is covered here:

http://developer.apple.com/documentation/Cocoa/Conceptual/DrawViews/index.html?h
ttp://developer.apple.com/documentation/Cocoa/Conceptual/DrawViews/Tasks/Drawing
InViews.html


Using Google with "site:apple.com" is usually helpful for finding these
things. I found that page by searching for "cocoa drawing site:apple.com".

And finally, to cut to the punch line a bit, you would subclass NSView and
override drawRect:.

Signature

Michael Ash
Rogue Amoeba Software

DarelRex@gmail.com - 26 Dec 2005 00:40 GMT
Michael,

Maybe you should ask before jumping to conclusions.  I did the
CurrencyConverter tutorial on Apple's Cocoa website before I even
started my own project.  Unfortunately, the tutorial doesn't cover much
except the most general use of Xcode, Interface Builder, and the MVC
organization method (which I am using).  The rest of Apple's
documentation seems to be a huge object-library reference with no code
samples.

Since Uli answered my question, my project is coming along very nicely.

Darel
<DarelRex@gmail.com>

> > I'm very new to Cocoa and Xcode, so this is a pretty basic question,
> > but I've searched the web and Apple's built-in documentation for
[quoted text clipped - 59 lines]
> And finally, to cut to the punch line a bit, you would subclass NSView and
> override drawRect:.
Chris Hanson - 26 Dec 2005 02:08 GMT
> Maybe you should ask before jumping to conclusions.  I did the
> CurrencyConverter tutorial on Apple's Cocoa website before I even
[quoted text clipped - 3 lines]
> documentation seems to be a huge object-library reference with no code
> samples.

There is a lot of example code on the Apple Developer Connection web
site and in /Developer/Examples on your Mac when you have the Xcode
Tools installed.

 -- Chris
Gregory Weston - 26 Dec 2005 02:31 GMT
> Michael,
>
[quoted text clipped - 5 lines]
> documentation seems to be a huge object-library reference with no code
> samples.

Darel,

Maybe you should actually read Michael's suggestion all the way through
(to where he gave you the explicit answer to your question and
suggestions as to how to find answers to future questions) or at least
up to the handy links before deciding that he wasn't going to be
helpful. You might then have realized that he had jumped to one
conclusion and it was correct. The fact that you asked the question you
did was an excellent indication that you hadn't gone through any
relevant tutorial. Your follow-up here indicates that you've done one,
and that one happens to be so contrived as to be pretty much useless (as
you've discovered).

Greg

> > And finally, to cut to the punch line a bit, you would subclass NSView and
> > override drawRect:.

Signature

Goal 2005: Convincing James Hetfield to cover the Strawberry Shortcake
"Are You Berry Berry Happy?" song.

Michael Ash - 26 Dec 2005 12:27 GMT
> Michael,
>
[quoted text clipped - 3 lines]
> except the most general use of Xcode, Interface Builder, and the MVC
> organization method (which I am using).

I'm sorry, which conclusion did I jump to? I never said you didn't do a
tutorial, I merely recommended that you try one. The fact that you've
tried exactly *one* tutorial which is extremely small and does not, in
fact, provide anything like the holistic view I was advocating doesn't
change anything there.

>  The rest of Apple's
> documentation seems to be a huge object-library reference with no code
> samples.

This would be why I introduced you to the conceputal docs. Apple also has
a truly enormous library of sample code, both on your hard drive as others
have pointed out, and on their web site.

Signature

Michael Ash
Rogue Amoeba Software

DarelRex@gmail.com - 26 Dec 2005 15:58 GMT
No wonder the Mac hasn't risen above 5% market share in 20+ years...
ask a simple, specific question and get extremely verbose criticisms
that your whole approach is wrong.  That'll win converts!

Thanks againk, Uli, for your concise and very helpful reply -- you
stand as a model to others.
Michael Ash - 26 Dec 2005 19:36 GMT
> No wonder the Mac hasn't risen above 5% market share in 20+ years...
> ask a simple, specific question and get extremely verbose criticisms
> that your whole approach is wrong.  That'll win converts!

It wasn't criticism, it was advice. If you can't tell the difference, you
may wish to avoid posting to these sorts of forums.

I made the assumption, based on the fact that you posted to a forum full
of human beings with "help" in the name, that you were looking for help,
not robotic answers. As such, I provided the help I felt was appropriate.
You certainly don't have to like it, I'm not egotistical enough to think
that my advice is always worth reading, but does it really merit
complaining because you got a verbose response with lots of pointers to
various forms of help?

If you want robotic answers, stick to Google and noninteractive web sites.

Signature

Michael Ash
Rogue Amoeba Software

Darel - 29 Dec 2005 23:15 GMT
Michael Ash said:
>If you want robotic answers, stick to Google and noninteractive web sites.

Believe me, I tried both of those quite a lot before posting my
question to this group.

Maybe I overreacted a bit to your first post on this thread, and for
that I apologize.  But I went to a lot of trouble to formulate my
question in a way that would project that I had tried both research and
experimentation, and was at a dead end.  If a specific answer to a
specific question is "robotic," then yes, I was looking for a "robotic"
answer, and it was very frustrating to get a reply that leads off with
links to very general Cocoa reading material, and tells me I'm not
taking the "holistic view."

I don't post a question here unless I feel I'm seriously stuck, and
then I try to describe my problem as specifically as possible, to
maximize the probability that a repsonder will get me unstuck.  Most of
the problems I have encountered in my recent attempts to learn Cocoa
have been solved by my own research and experimentation, and you didn't
hear about them here.

Thanks,
Darel
Gregory Weston - 27 Dec 2005 03:48 GMT
> No wonder the Mac hasn't risen above 5% market share in 20+ years...

(Actually, a major element of that is that fact that market share is a
measure of the relative sales volume per quarter and that favors
equipment with a shorter service life.)

> ask a simple, specific question and get extremely verbose criticisms
> that your whole approach is wrong.

Your approach _was_ wrong. And you got a good, complete answer which you
didn't read all the way through. You did not get criticism, or anything
resembling it, until you decided that you wanted to play victim.

G

Signature

Goal 2005: Convincing James Hetfield to cover the Strawberry Shortcake
"Are You Berry Berry Happy?" song.

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.