[comp.sys.mac.programmer] Drawstring Problem

rg2c+@andrew.cmu.edu (Robert Nelson Gasch) (10/15/90)

I'm using graphics and the Drawstring function. I need to use Drawstring
since I have to display status lines that get updated. The problem is,
once you have put something in your window with Drawstring, it is
like you glued it to the window. You can't write over it since that will just 
put the new string over the old one and you end up with something
completeley unledgible. What is the most sensible way to erase strings
in this situation? The method should be flexible as the size of the
text displaying area is subject to frequent change.
If anybody has done something like this and solved this problem in a
sensible manner, please let me know the details of how you went about
it. The perfect thing would be to just write over the old string. I've
tried this, but could not get it to work.

Thanx alot --> Rob

sam@uhura.neoucom.EDU (Scott A. Mason) (10/16/90)

In article <gb6MX8600UhWQ0dklE@andrew.cmu.edu> rg2c+@andrew.cmu.edu (Robert Nelson Gasch) writes:
>I'm using graphics and the Drawstring function. I need to use Drawstring
>since I have to display status lines that get updated. The problem is,

Why not just use a static text item in the window and use that to display your
status line.
You'll have to excuse errors on parameters due to the fact that IM is not here
in front of me.  The code would look something like this.

	short itemType;	Handle itemHandle; Rect itemRect;

	GetDItem (theDialog, &itemType, &itemHandle, &itemRect);
	SetItemText ("\pYour update here", itemNumber, itemHandle);
-- 
--------------------------------------------------------------------------------
"If it ain't broke, don't fix it," and certainly don't blame me.
UUCP:  {pitt,lll-winken,usenet,aablue}!neoucom!sam   INTERNET:  sam@neoucom.EDU
Scott A. Mason, Coordinator of Systems Operations, NEOUCOM

nick@cs.edinburgh.ac.uk (Nick Rothwell) (10/16/90)

In article <gb6MX8600UhWQ0dklE@andrew.cmu.edu>, rg2c+@andrew.cmu.edu (Robert Nelson Gasch) writes:
> I'm using graphics and the Drawstring function. I need to use Drawstring
> since I have to display status lines that get updated. The problem is,
> once you have put something in your window with Drawstring, it is
> like you glued it to the window. You can't write over it since that will just 
> put the new string over the old one and you end up with something
> completeley unledgible. What is the most sensible way to erase strings
> in this situation?

Don't know about the most sensible, but the easiest is to use EraseRect.

> The method should be flexible as the size of the
> text displaying area is subject to frequent change.

Or, do you mean the size of the text?

If you've got an unknown amount of text, you could use a fixed size
rectangular region and draw the text with TextBox.

-- 
Nick Rothwell,	Laboratory for Foundations of Computer Science, Edinburgh.
		nick@lfcs.ed.ac.uk    <Atlantic Ocean>!mcsun!ukc!lfcs!nick
~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~
 "Now remember - and this is most important - you must think in Russian."

stevec@Apple.COM (Steve Christensen) (10/16/90)

 rg2c+@andrew.cmu.edu (Robert Nelson Gasch) writes:
>I'm using graphics and the Drawstring function. I need to use Drawstring
>since I have to display status lines that get updated. The problem is,
>once you have put something in your window with Drawstring, it is
>like you glued it to the window. You can't write over it since that will just 
>put the new string over the old one and you end up with something
>completeley unledgible. What is the most sensible way to erase strings
>in this situation? The method should be flexible as the size of the
>text displaying area is subject to frequent change.
>If anybody has done something like this and solved this problem in a
>sensible manner, please let me know the details of how you went about
>it. The perfect thing would be to just write over the old string. I've
>tried this, but could not get it to work.

There are a few ways of doing this depending on how you want the display to
look.  You can use TextBox() and specify a bounding rectangle big enough to
hold the string you'd display.  You'd call StringWidth() to find out how wide
the string is and then build a rectangle that wide.  The down side to this is
that whatever's behind the string gets erased each time.  Another way to do
this is to draw the string in XOR mode and then draw it again (to erase it)
just before updating the display.  This way is better if something's drawn
"behind" the string...

steve


-- 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 whoami?     Steve Christensen
 snail:      Apple Computer, 20525 Mariani Ave, MS-81CS, Cupertino, CA  95014
 Internet:   stevec@goofy.apple.com
 AppleLink:  stevec
 CompuServe: 76174,1712

lippin@ragu.berkeley.edu (The Apathist) (10/17/90)

Recently rg2c+@andrew.cmu.edu (Robert Nelson Gasch) wrote:
>The problem is,
>once you have put something in your window with Drawstring, it is
>like you glued it to the window. You can't write over it since that will just 
>put the new string over the old one and you end up with something
>completeley unledgible. What is the most sensible way to erase strings
>in this situation?

I suggest setting the text mode to srcCopy, which will cause the new
text to cover up the text underneath.  Since this won't erase all of
the previous text if the new text is shorter, you can use something
like this to erase to the end of the line:

void cleartoeol(short eol)
  {
   FontInfo fi;
   Point pen;
   Rect r;
   GetFontInfo(&fi);
   GetPen(&pen);
   SetRect(&r,pen.h,pen.v-fi.ascent,eol,pen.v+fi.descent);
   EraseRect(&r);
  }

I prefer this to doing an EraseRect of the whole text before drawing
the new line, because this won't make the text flicker.

					--Tom Lippincott
					  lippin@math.berkeley.edu

	"But let me repeat myself, at the risk of being clear:
	 There must be fifty ways to write your program.
		Fifty ways to write your program."

sampson@cod.NOSC.MIL (Charles H. Sampson) (11/01/90)

In article <10727@goofy.Apple.COM-  stevec@Apple.COM (Steve Christensen) writes:
- 
-  rg2c+@andrew.cmu.edu (Robert Nelson Gasch) writes:
-- 
--  [Description of some difficulties in using Drawstring to overwrite
--   text]
- 
- There are a few ways of doing this depending on how you want the display to
- look.  You can use TextBox() and specify a bounding rectangle big enough to
- hold the string you'd display.  You'd call StringWidth() to find out how wide
- the string is and then build a rectangle that wide.  The down side to this is
- that whatever's behind the string gets erased each time.  Another way to do
- this is to draw the string in XOR mode and then draw it again (to erase it)
- just before updating the display.  This way is better if something's drawn
- "behind" the string...
- 
     Redrawing in XOR mode will erase the previously drawn text, but it
won't refresh whatever was "behind" the string.  When the new text is
drawn the background will have holes where the old text was.  However,
for the simple problem, where no background is involved, this looks like
the nicest solution; you don't have to worry about figuring out how to
handle the tail of the old text, as would be necessary if SrcCopy mode
were used.

                              Charlie

llama@eleazar.dartmouth.edu (Joe Francis) (11/02/90)

Steve Christensen writes:

>- that whatever's behind the string gets erased each time.  Another way to do
>- this is to draw the string in XOR mode and then draw it again (to erase it)
>- just before updating the display.  This way is better if something's drawn
>- "behind" the string...
>- 

Charles H. Sampson responds:
>     Redrawing in XOR mode will erase the previously drawn text, but it
>won't refresh whatever was "behind" the string.  When the new text is
>drawn the background will have holes where the old text was.  However,
>for the simple problem, where no background is involved, this looks like
>the nicest solution; you don't have to worry about figuring out how to
>handle the tail of the old text, as would be necessary if SrcCopy mode
>were used.


Actually, if you look again at what Steve wrote, you will find this 
is not true.  Steve says to do both the drawing and "redrawing"
(erasing) in XOR.  This WILL refresh whatever was behind the string, at 
the expense of the text being white instead of black at those pixels 
where the background was black.

Great for text on white or black backgrounds.  Lousy for medium or small
text over a grey pattern.

----------------------------------------------------------------------------
"Read My Lips: No Nude Texans!" - George Bush clearing up a misunderstanding