[comp.sys.mac.programmer] Can anyone answer these Questions?

rcbaem@eutrc3.urc.tue.nl (Ernst Mulder) (08/14/89)

After 3 years of owning an Apple Macintosh I finally overtook the barrier
a Mac programmer has to take. I started to program it. I used to program a
lot before I had a Mac, but I looked up to Mac programmers, especially
when I saw how immensely complex the IM routines (seemed to be) where.
Well, now I started myself, and my first program is already getting rather
complex.

I'm writing a program to do IFS. (Read Barnsley for more info) It supports
multiple Windows, is MF compatible, and fully IM compatible :)

A few questions:

1) I draw my picture(s) in an offscreen Grafport, and the picture's Window
   displays it (or a part of it). When I copy the offscreen port's picture
   to the Window I use CopyBits. The IM (1) however tells me not to
   CopyBit more than 3K at the time. Is this still so with newer ROMs?
   I CopyBits the whole picture at update times, and the picture is about
   20K bytes large! I always have enough Stack space available, so I
   can't figure where the 3K limit is needed for...

2) The user can select a part of the picture to copy it to the clipboard.
   MacPaint and HyperCard have a nice option, when you press Command while
   selecting graphics, the selection rectangle is schrinked to fit the
   picture you selected. I couldn't find any IM routines to do this
   for me. Do I have to write my own Assembly code to do this? Or is
   there a higher level solution?

3) What is the best way to update the default button's outline in a
   dialogbox? (For when a screensaver or so makes it disappear)

4) When the user selects About... I put a dialog on the screen (GetNewDialog)
   and DisposDialog it after the user clicked the mouse. I remove the 
   mousedown event from the eventqueue. After the dialog is disposed of
   my current front window gets a deactivate and an activate events, and
   even though it's not disasterous, it still doesn't look nice. How
   can I solve this? (Strange enough, another (modal) dialog in my program
   uses ModalDialog and a filter, and when Disposing of that dialog the
   frontWindow gets an activate event only...)

5) When a file is selected with Open... (or by doubleclicking it/them from
   the Finder) I read my DATA from it and close it again (Remembering its
   name and vRefNum) because I don't like to leave files open. However, 
   when the user moves the file into another folder using MultiFinder, 
   there's no way for me to know that. When the file is saved again, you
   get two copies, one in the folder and one where it was in the first
   place. Recognising this problem I checked some other Applications on
   it. And apparently I'm not the only one having this problem... The
   solution seems to be to leave the file open and refering it by the
   reference number returned by FSOpen for the rest of the program, or
   shouldn't I be bothered?

6) I use Str2Num (SANE) to read a number typed in a TEItem in a Dialog
   box. How can I detect the number is valid? (jshgjs) is detected
   not being a number my SANE, returning a NaN code. (12.23.1) however
   returns (12.23) and isn't detected as being wrong... Solution?

These are my questions so far. Answers may be posted on this newsgroup
or sent to me directly. You will see the program when I think it's
ready enough. I'm very proud having fittet all it can do for now into
26K bytes of Application. Not that anyone seems to care about the size
of an Application anymore, but last time I programmed was on a 64K
byte Apple //, and every byte counted. Think I still use that principle.
:)))

 Any help appreciated. I want my first Mac programm to be a very good one. :)

 Ernst.
   >

dorourke@polyslo.CalPoly.EDU (David M. O'Rourke) (08/15/89)

rcbaem@eutrc3.urc.tue.nl (Ernst Mulder) writes:
>1) [asks about the 3k stack limit using copy bits...]

  I don't belive that's it's an issue anymore with the larger memeory
  machines.  I remember reading that tid bit somewhere, but I can't
  find it now.  {my kingdom for a complete x-ref of my personal memory so
  that I could used it for a subject search  :-)}

>2) [asks about routines for selecting graphics....]

  Volume 4, pages 24 & 25 have some routines that might be able to help you
  out.  I've heard they can be used to build a graphics selection routine
  but I've never done it myself.

>3) What is the best way to update the default button's outline in a
>   dialogbox? (For when a screensaver or so makes it disappear)

  If you're using modal dialog I don't know of a great way to do it.  Most
  Mac programmer's I know have gotten around to building their own 
  ModalDialog that does it automaticly when it gets an update event.

  But I guess you could make the outline portion of the button a useritem
  in the dialog list, and then have your draw routine simply draw the 
  rectangle, modal dialog will then call your draw routine to accomplish
  update, and volia your default rectangle gets redrawn.....

  You can find additional information regarding user item's in inside mac
  volume 1, pages 404 & 405, & 421..

>4) [asks about gettine too many activate events after disposing of
     a dialog]

  That's a toughy, I've not noticed that behavior in my own code, but then
  again I haven't really looked.  I can think of two possibilities...

  1)  You're getting the leftover deactive event from when you originally
      put the dialog on the screen.  If that's the case you need to 
      handle deactivate events why you're checkking for the mouse.

  2)  You're getting a deactive event frow clossing the dialog, and
      possibly not checking the message field to see which window it's for.

>5) [questions about leaving a file open or closing it while the use has
     a window open to it...]

  I've seen both behaviors.  I personally prefer letting the user do what
  ever they want with a file after I've read in the data.  I've found it
  quite difficult to explain to non-computer mac people why they can't
  play with a file in the finder just because it's open in another application
  so to avoid the problem my software open and closes.

  But again I feel this one is a judgment call, and depends on the programmer
  target user, and a varity of un-scientific principals called "do I feel
  like coding this routine up???"

>6) [asks about detecting bad #'s using SANE....]

  Well, I know this in in-effiecient but I conver the number from str->binary
  and then from binary->str, and see if the two str's are equal, if they are
  then you got a #, if not you don't.  Better methods anyone??

> Any help appreciated. I want my first Mac programm to be a very good one. :)

  I hope you find the answers useful.

> Ernst.
-- 
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\|/////////////////////////////////////////
David M. O'Rourke____________________|_____________dorourke@polyslo.calpoly.edu
|  God doesn't know, he would have never designed it like that in the first   |
|_ place. ____________________________________________________________________|

topping@dinghy.cis.ohio-state.edu (brian e topping) (08/15/89)

dorourke@polyslo.CalPoly.EDU (David M. O'Rourke) writes:

>>3) What is the best way to update the default button's outline in a
>>   dialogbox? (For when a screensaver or so makes it disappear)

>  If you're using modal dialog I don't know of a great way to do it.  Most

Why not use _SFPPutFile?  It allows you to specify a modal dialog filter
procedure to call after doing the standard filtering.  In this procedure,
you simply watch for update events, and re-draw the highlighting when
an update comes thru.  (Be sure to double-check the grafport in the event
record!)

>>6) [asks about detecting bad #'s using SANE....]

>  Well, I know this in in-effiecient but I conver the number from str->binary
>  and then from binary->str, and see if the two str's are equal, if they are
>  then you got a #, if not you don't.  Better methods anyone??

Is it possible to use TestException() (SANE reference manual, pg. 54) to
check for INEXACT after a conversion using Str2Dec()?  I haven't done
anything yet with exceptions, so I am kind of interested in this one too.

>> Any help appreciated. I want my first Mac programm to be a very good one. :)

>  I hope you find the answers useful.

   Me too.

>> Ernst.

>  David M. O'Rourke

Brian Topping
<topping@cis.ohio-state.edu>

dorourke@polyslo.CalPoly.EDU (David M. O'Rourke) (08/15/89)

brian e topping <topping@cis.ohio-state.edu> writes:
>Why not use _SFPPutFile?  It allows you to specify a modal dialog filter
>procedure to call after doing the standard filtering.  In this procedure,
>you simply watch for update events, and re-draw the highlighting when
>an update comes thru.  (Be sure to double-check the grafport in the event
>record!)

  I don't think he was restricting the type of Dialog to SFPPutFile. He 
wants to do it for every dialog, not just the system ones, or am I missing
something and you can use _SFPPutFile for any type of dialog??

>Brian Topping
-- 
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\|/////////////////////////////////////////
David M. O'Rourke____________________|_____________dorourke@polyslo.calpoly.edu
|  God doesn't know, he would have never designed it like that in the first   |
|_ place. ____________________________________________________________________|

sdh@wind.bellcore.com (Stephen D Hawley) (08/15/89)

In article <840@eutrc3.urc.tue.nl> rcbaem@eutrc3.urc.tue.nl (Ernst Mulder) writes:
Welcome to Mac programming.  I'll try to field these for you.

>1) [Is there a 3K limit to CopyBits?]

Yes, and no.  If you are running on an old mac, there is.  If not, then
there isn't.  If you don't care about old machines, do 1 CopyBits() call.
Otherwise, break up the call into several pieces.

>2) [How do I shrink selection rects?  Is there a high level sol'n?]

Yes.  There is a high level solution:
void
ShrinkWrap(map, r)
BitMap *map;
Rect *r;
{
	/* let r be the current selection rect 
	 * map is the bitmap in question (assuming bilevel)
	 */
	while(NoPixelsInColumn(map, r->left, r->top, r->bottom)) ++r->left;
	while(NoPixelsInColumn(map, r->right, r->top, r->bottom)) --r->right;
	if (r->right > r->left) NastyErrorMessage();
	while(NoPixelsInRow(map, r->top, r->left, r->right)) ++r->top;
	while(NoPixelsInRow(map, r->bottom, r->left, r->right)) --r->bottom;
	if (r->top > r->bottom) NastyErrorMessage();
}

NoPixelsInColumn(map, col, from, to)
BitMap *map;
register short col, from, to;
{
	while(from <= to) {
		if (PixelOn(map, col, from++)) return(0);
	}
	return(1);
}

NoPixelsInRow(map, row, from, to)
BitMap *map;
register short row, from, to;
{
	while(from <= to) {
		if (PixelOn(map, from++, row)) return(0);
	}
	return(1);
}

Writing PixelOn() is left as a task for the astute reader.  Obviously,
this can be done more efficiently, but you should understand what its doing.

>3) What is the best way to update the default button's outline in a
>   dialogbox? (For when a screensaver or so makes it disappear)

Create an item in your dialog box that is a UserItem.  Make the proc for
it draw the outline.  This will get hit by update events.

>4) When the user selects About... I put a dialog on the screen (GetNewDialog)
>   and DisposDialog it after the user clicked the mouse. I remove the 
>   mousedown event from the eventqueue. After the dialog is disposed of
>   my current front window gets a deactivate and an activate events, and
>   even though it's not disasterous, it still doesn't look nice.

If it happens consistently, remove the events from the event queue.  This
isn't a nice thing to do, and may break in future releases of the machine,
but it should work just fine.


>5) [Should I worry about leaving files open]

Leave them open while your program is running.  Otherwise, there is no
good way for any other program to know that they are in use.  By opening
a file, you are making the operating system put a lock on the file so that
you can avoid a critical section problem (and trust me, you want to avoid
them).  If you do not intend to modify the file in any way, go ahead and
close it and forget about it.  If you intend to work with it, leave it open,
but just make sure you close it before you exit.


>6) I use Str2Num (SANE) to read a number typed in a TEItem in a Dialog
>   box. How can I detect the number is valid? (jshgjs) is detected
>   not being a number my SANE, returning a NaN code. (12.23.1) however
>   returns (12.23) and isn't detected as being wrong... Solution?

I would advise writing the code yourself.  There are 2 attitudes, you can
either prevent the wrong characters from ever getting to the dialog box
(which is a royal pain [I know, I just had to do it] and I wouldn't wish
that fate on anyone who hasn't already done it), or you can do the checking
post hoc.

You can do a quick scan through the list to see that everything is hunky
dorey:

char numbers[] = "0123456789-+";

#define OK_NUM(numstr) HunkyDorey(numstr, numbers);

HunkyDorey(ps, vset)
register unsigned char *ps;
register char *vset;
{
	/*
	 * ps is a pascal string.
	 * vset is a c string of chars valid for the language.
	 */
	register short i;

	for (i = *ps++; i > 0; i--, ps++) {
		if (!CInStr(*ps, vset)) return(0);
	}
	return(1);
}

CInStr(c, s)
register char c, *s;
{
	while(c != *s) s++;
	return(c == *s);
}

Again, this could be written more efficiently, but I chose not to do so for
clarity.

Thr ROMs give you quite a powerful set of programming tools, but you shouldn't
feel tied down to them if they don't give you quite what you want.

Good luck.

Steve Hawley
sdh@flash.bellcore.com
"Up is where you hang your hat."
	--Jim Blandy, computer scientist

rcbaem@eutrc3.urc.tue.nl (Ernst Mulder) (08/16/89)

Thank you all for trying to answer my questions. I now update the default
button with an userItem, works fine. (However this still doesn't solve
the same problem with standard Alerts.. Do I have to write my own Alert
routine? Or use normal Dialogs instead of Alerts?)

I'll try Stephen D Hawley's selection routine, though I imagine it may be
slow. If so I'll try if anything can be done with CopyMask etc.

I don't think I will leave my file open during a session. I feel any sensible
user won't move those files while they are being used by another Application,
and accoding to Ben Cranston even Unix has this problem (VI for instance)...

Still lots of code to write, so I'm off. :)

Till later, bye and thanks!  Ernst.
                               >

pepke@loligo.cc.fsu.edu (Eric Pepke) (08/17/89)

In article <13547@polyslo.CalPoly.EDU> dorourke@polyslo.CalPoly.EDU (David M. O'Rourke) writes:
>rcbaem@eutrc3.urc.tue.nl (Ernst Mulder) writes:
>>6) [asks about detecting bad #'s using SANE....]
>
>  Well, I know this in in-effiecient but I conver the number from str->binary
>  and then from binary->str, and see if the two str's are equal, if they are
>  then you got a #, if not you don't.  Better methods anyone??

You're half right.  01 would fail your test but would still be a perfectly
valid number.

I usually parse the string.  However, if you do that, it is almost trivial to
assemble the number as you're parsing it, so I tend to avoid SANE's conversions
altogether.

Eric Pepke                                     INTERNET: pepke@gw.scri.fsu.edu
Supercomputer Computations Research Institute  MFENET:   pepke@fsu
Florida State University                       SPAN:     scri::pepke
Tallahassee, FL 32306-4052                     BITNET:   pepke@fsu

Disclaimer: My employers seldom even LISTEN to my opinions.
Meta-disclaimer: Any society that needs disclaimers has too many lawyers.

zben@umd5.umd.edu (Ben Cranston) (08/18/89)

In article <850@eutrc3.urc.tue.nl> rcbaem@eutrc3.urc.tue.nl (Ernst Mulder) writes:

> I don't think I will leave my file open during a session. I feel any sensible
> user won't move those files while they are being used by another Application,
> and accoding to Ben Cranston even Unix has this problem (VI for instance)...

I just mentioned (in private mail) the analagous situation under Unix.

1. Edit a file in directory A
2. Put the editor asleep with control-Z.
3. Move the file from directory A to directory B.
4. Wake the editor back up.
5. Write the file from the editor.

It recreates the file in directory A, since it had no knowledge that the
user moved the file to directory B while it was asleep.

This is not necessarily a bug.
-- 
Sig     DS.L    ('ZBen')       ; Ben Cranston <zben@umd2.UMD.EDU>
* Computer Science Center Network Infrastructures Group
* University of Maryland at College Park

wrs@apple.com (Walter Smith) (08/19/89)

In article <17406@bellcore.bellcore.com> sdh@wind.bellcore.com (Stephen D 
Hawley) writes:
> In article <840@eutrc3.urc.tue.nl> rcbaem@eutrc3.urc.tue.nl (Ernst 
Mulder) writes:
> >2) [How do I shrink selection rects?  Is there a high level sol'n?]
> 
> Yes.  There is a high level solution:
> [C code for bitmap-shrinking algorithm]

I'm happy to say that Bill Atkinson already implemented a bitmap-shrinking 
algorithm many years ago, and it's sitting there in CopyBits waiting for 
you.  Just make the source and destination rectangles for CopyBits 
different sizes.

- Walt

--
Walter Smith                            wrs@apple.com, apple!wrs
Apple Computer, Inc.                    (408) 974-5892
My corporation disavows any knowledge of my activities on the network.

wrs@Apple.COM (Walter Smith) (08/19/89)

Sorry--on re-reading your question, I see that you are talking about the
"marquee-lasso" feature invoked by holding the Command key down while using
the rectangular selection tool.  I thought you were referring to the ability
to squish an existing marquee selection by clicking in a corner with the
command key down and dragging.

What you want is a combination of SeedFill and CopyMask, both documented
in Inside Macintosh volume IV.  Unless I actually read your question right
in the first place.

- Walt

tim@hoptoad.uucp (Tim Maroney) (08/21/89)

In article <3707@internal.Apple.COM> wrs@apple.com (Walter Smith) writes:
>I'm happy to say that Bill Atkinson already implemented a bitmap-shrinking 
>algorithm many years ago, and it's sitting there in CopyBits waiting for 
>you.  Just make the source and destination rectangles for CopyBits 
>different sizes.

And then look at the incredibly ugly screen picture.  Unless, of
course, there's a shrinkage of about 50% or greater, in which case the
bitmap will have been turned solid black.  Comes from trying to
preserve the black dots.  It's not a particularly useful feature of
CopyBits, sorry.
-- 
Tim Maroney, Mac Software Consultant, sun!hoptoad!tim, tim@toad.com

"This signature is not to be quoted." -- Erland Sommarskog

rcbaem@eutrc3.urc.tue.nl (Ernst Mulder) (08/22/89)

In article <3707@internal.Apple.COM> wrs@apple.com (Walter Smith) writes:
>
>I'm happy to say that Bill Atkinson already implemented a bitmap-shrinking 
>algorithm many years ago, and it's sitting there in CopyBits waiting for 
>you.  Just make the source and destination rectangles for CopyBits 
>different sizes.
>
>- Walt
>
 Sorry Walt, but you misread my question. You were not the only one, so
probably I wasn't entirely clear enough.
 I know what CopyBits does, what I actually wanted is an implememtation of
what MacPaint (for instance) does when you press the Command key while
selecting graphics with the selection rectangle. When you release the
mouse, the SIZE of the selection rectangle is schrinked to fit the
graphic. Talking about graphics, maybe an illustration is in place
here:

Before releasing the mouse:                After releasing the mouse

  -----********-------             --********-
  |    ********      |             | ********|
  |   ********       |             |******** |
  |    *****         |             | *****   |
  |                  |             -----------
  --------------------                 (Happens only when Command was pressed)

Hope this settles it once and for all.

 (Do I need SeedFill and CopyMask for this?
 )

 Ernst.
   >