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 / March 2008



Tip: Looking for answers? Try searching our database.

Scrolling text like a stock ticker

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
flopbucket - 29 Mar 2008 00:05 GMT
Hi,

I'm an experienced C++ developer but new to Mac OS X.  I am familiar
with basic Cocoa development, Objective-C, etc.

I want to write a small app that open a window and at the bottom has a
scrolling text area, like a stock ticker.  As new data arrives, I want
it to smoothly scroll in from the right, causing text at the extreme
left to scroll out of the window.  Normal "ticker" type stuff.

Being new to Mac OS, I'm not exactly sure how best to go about this.
Especially about getting the letters to smoothly scroll in (i.e. not
whole letter at a time).  Do I draw to an offscreen bitmap and copy
part of it to the window for display?

If anyone has any ideas on how to go about this it'd be greatly
appreciated.  I don't mind reading and doing the hard work of course,
I'm just not sure what way you would do this in Cocoa.

Thanks
David Phillip Oster - 29 Mar 2008 19:27 GMT
In article
<79d3e4eb-b1f1-4fc9-ac43-d1da8b39c4ea@t54g2000hsg.googlegroups.com>,

> I'm an experienced C++ developer but new to Mac OS X.  I am familiar
> with basic Cocoa development, Objective-C, etc.
[quoted text clipped - 12 lines]
> appreciated.  I don't mind reading and doing the hard work of course,
> I'm just not sure what way you would do this in Cocoa.

Text is complicated. There are Unicode and kerning rules that will have
you tearing your hair. Better just to re-use the existing Cocoa classes
for drawing scrolling text.

For you, I've built a simple working model of the ticker tape view you
asked for here: http://www.TurboZen.com/sourcecode/Ticker.zip

It is built out of an NSTextView, with the following tricks:
The animation is driven by a timer, that fires 60 times a second,
executing this:

- (void)step:(NSTimer *)timer {
 mOffset -= 2; // pixels per tick
 [self setTextContainerInset:NSMakeSize(mOffset, 0)];
 [self setNeedsDisplay:YES];
}

Limitations:

* the timer is created scheduled, on the runloop in the default mode. To
get the ticker to work while the user is tracking a menu or a control,
you need to add the timer to the other runloop modes.

* I didn't add any API to set the font attributes, such as color of the
appended text.

* A real program would use the NSTextContainer owned by the NSTextView
to measure the pixels in the string prefix, delete the prefix, and reset
the container offset, so you wouldn't waste storage storing dead history
in the prefix of the string of the NSTextStorage.

(If you are new to Cocoa, you may miss that much of the creation and
wiring up of the objects in a program is specified in Interface Builder,
in the .nib file. That is how the AppDelegate and gets created and how
it gets a pointer to the TickerView.)

See
file:///Developer/ADC%20Reference%20Library/documentation/Cocoa/Conceptua
l/TextArchitecture/index.html for more information.

Signature

David Phillip Oster

flopbucket - 30 Mar 2008 16:35 GMT
> In article
> <79d3e4eb-b1f1-4fc9-ac43-d1da8b39c...@t54g2000hsg.googlegroups.com>,
[quoted text clipped - 59 lines]
> --
> David Phillip Oster

Hi David,

Thank you very much, much appreciated.  I'm going to download it now
and take a look, I'm sure it will get me started on the right track.

Do you know any good books covering Cocoa?  I've seen "Cocoa
Development" mentioned, but the reviews said it was out of date at
this time.

Thanks again
David Phillip Oster - 30 Mar 2008 16:52 GMT
In article
<f8c02720-2e2c-403e-95ba-92124ada8522@m73g2000hsh.googlegroups.com>,

> Thank you very much, much appreciated.  I'm going to download it now
> and take a look, I'm sure it will get me started on the right track.
>
> Do you know any good books covering Cocoa?  I've seen "Cocoa
> Development" mentioned, but the reviews said it was out of date at
> this time.

You are welcome. The best book is "Cocoa Programming for Mac OS X" by
Aaron Hillegass. Unfortunately, the 3rd edition, revised for 10.5
(Leopard), isn't scheduled to be available until May 26, 2008.

My second choice book is "Cocoa Programming for Mac OS X" by Aaron
Hillegass. 2nd edition. Some of the screen shots look a little old
fashioned, and only the classic Objective-C language is covered, not the
new Objective-C 2, but this is the book that got me going.

Also, I use Apple's docs almost every day.
Simon Slavin - 30 Mar 2008 19:58 GMT
On 28/03/2008, flopbucket wrote in message <79d3e4eb-b1f1-4fc9-ac43-
d1da8b39c4ea@t54g2000hsg.googlegroups.com>:

> I want to write a small app that open a window and at the bottom has a
> scrolling text area, like a stock ticker.  As new data arrives, I want
[quoted text clipped - 5 lines]
> whole letter at a time).  Do I draw to an offscreen bitmap and copy
> part of it to the window for display?

That's very oldschool.  There are better ways now.

In Cocoa, you will be writing your text to an NSTextView.

NSTextView inherits from NSView.

NSViews have two properties called 'frame' and 'bounds'.  Once controls
where the view appears in the view that contains it.  The other controls
where the contents of the view are drawn in relation to its boundaries.
Normally they're the same but a good way to scroll content is to mess with
one, while leaving the other static.

I think one way to scroll your content (the text in an NSTextView) would
be to use setBounds continually.  Take a look at the documentation for
NSView, and the conceptual doc here:

<http://developer.apple.com/documentation/Cocoa/Conceptual/CocoaViewsGuide/
Coordinates/chapter_3_section_3.html>

Since you're relatively new to Cocoa I would suggest you handle your own
scrolling, because it will give you a good idea how Cocoa objects behave.
But once you understand what's happening you might like to know that Cocoa
has an animation class especially designed for animating views:

<http://developer.apple.com/documentation/Cocoa/Conceptual/AnimationGuide/A
rticles/ViewAnimations.html>

Simon.
Signature

http://www.hearsay.demon.co.uk

 
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.