[comp.sys.mac.programmer] Get Text from a Dialog

dmmg1176@uxa.cso.uiuc.edu (David M Marcovitz) (02/09/91)

I am trying to copy text from an editText item in a Dialog box into a
Str255 variable.  I am using Think C 4.0.  I did a GetDItem followed
by either the GetIText routine in the toolbox, or GetTextHandle (from
the CStaticText class in the Think Class Library).  Either way, I keep
getting a bus error.

There must be a simple way to get text from an editText item in a
Dialog box either using TCL or toolbox calls.  Could someone please
send me a brief explanation of how to do this.  HELP!!

--
David M. Marcovitz                     |  internet: marcovitz@uiuc.edu
Computer-based Education Research Lab  |            dmmg1176@uxa.cso.uiuc.edu
University of Illinois                 |  novanet:  marco / cca / cerl

delann@cs.umu.se (Anders Nyman) (02/10/91)

In article <1991Feb8.204416.390@ux1.cso.uiuc.edu> dmmg1176@uxa.cso.uiuc.edu (David M Marcovitz) writes:
>
>I am trying to copy text from an editText item in a Dialog box into a
>Str255 variable.  I am using Think C 4.0.  I did a GetDItem followed
>by either the GetIText routine in the toolbox, or GetTextHandle (from
>the CStaticText class in the Think Class Library).  Either way, I keep
>getting a bus error.
>
>There must be a simple way to get text from an editText item in a
>Dialog box either using TCL or toolbox calls.  Could someone please
>send me a brief explanation of how to do this.  HELP!!

Try to do something like this:

	char *thestring; /* Pointer to the string where you want the answer */
	int item;
	Handle theitemhndl;
	DialogPtr thedialog;
	Rect therect;

	GetDItem(thedialog,itemnumber,&item,&itemhndl,&itemrect);
	GetIText(itemhndl,thestring);
	PtoCstr(thestring); /* If you want the string to be a c-string */

thedialog is your dialogptr from NewDialog or GetNewDialog. 
iteitemnumber sould be the items number in the itemlist.
In the int item, you will recieve the itemtype, and in itemrect the rectangle
for the item. Be sure to put all the '&' in right places. I have noticed
that sometimes forgotten '&' will cause BOMB!.

Hope this help.
Anders Nyman

bin@primate.wisc.edu (Brain in Neutral) (02/10/91)

From article <1991Feb10.023120.3539@cs.umu.se>, by delann@cs.umu.se (Anders Nyman):
> Try to do something like this:
> 
> 	char *thestring; /* Pointer to the string where you want the answer */
> 	int item;
> 	Handle theitemhndl;
> 	DialogPtr thedialog;
> 	Rect therect;
> 
> 	GetDItem(thedialog,itemnumber,&item,&itemhndl,&itemrect);
> 	GetIText(itemhndl,thestring);
> 	PtoCstr(thestring); /* If you want the string to be a c-string */
> 
> thedialog is your dialogptr from NewDialog or GetNewDialog. 
> iteitemnumber sould be the items number in the itemlist.
> In the int item, you will recieve the itemtype, and in itemrect the rectangle
> for the item. Be sure to put all the '&' in right places. I have noticed
> that sometimes forgotten '&' will cause BOMB!.

Which it should.  There are (at least) two other things that could cause
bombs in the above code.

(1) You're passing thestring to GetIText, but thestring is a pointer
to who knows where?  thestring should be a Str255, not a char * (or,
which is roughly the same, a StringPtr), and you should pass its address.
(Of course, in C, that's just "thestring".)

(2) Depending on your compiler "int" might be two or four bytes long.
If it's 4, and your compiler doesn't check the sizes of the arguments
passed to toolbox routines, GetDItem() could crash.

Otherwise, it should work wonderfully! :-)

--
Paul DuBois
dubois@primate.wisc.edu

dmmg1176@uxa.cso.uiuc.edu (David M Marcovitz) (02/11/91)

Thanks to everyone for your help figuring out why I couldn't get the
text from a dialog box.  My problem was that I was using an instance
of a class (my dialog box class), instead of a DialogPtr variable
(declared as part of that class) in my call to GetDItem.  I haven't
yet gotten the text, but I am no longer getting a bus error, and I am
much closer.  Thanks.


--
David M. Marcovitz                     |  internet: marcovitz@uiuc.edu
Computer-based Education Research Lab  |            dmmg1176@uxa.cso.uiuc.edu
University of Illinois                 |  novanet:  marco / cca / cerl

dmmg1176@uxa.cso.uiuc.edu (David M Marcovitz) (02/12/91)

dmmg1176@uxa.cso.uiuc.edu (David M Marcovitz) writes:
>As I said I am no longer getting the bus error, but I still can't get
>the text.  GetDItem works fine and gives me a pointer to the editText
>item.  I can then use GetIText, but the text that it returns is the
>empty string.  I can also use SetIText; if I follow that with a
>GetIText, I can retrieve the text I just set it with.  However, I
>cannot get the text that I type into the editText item in the dialog
>box.  How can I do that?

>By the way, I can use GetIText to successfully retrieve the Text in a
>staticText item, just not the text typed into an editText item.

Well, I've gotten lots of code samples from people, and none of them
solve my problem.  I went into ResEdit and set the default text of the
editText item.  Now, no matter what I type in the editText item,
GetIText returns that default string.

The problem must be with the way I am interacting with Dialog Box
Class Library that I got off the net.  I am going to scrap that and
write the code myself (with a lot of help from the Mac Programming
Primer) to deal with the dialog.

Thanks to those who sent me responses.  I'm sure I'll use them when I
rewrite my code.

--
David M. Marcovitz                     |  internet: marcovitz@uiuc.edu
Computer-based Education Research Lab  |            dmmg1176@uxa.cso.uiuc.edu
University of Illinois                 |  novanet:  marco / cca / cerl

dmmg1176@uxa.cso.uiuc.edu (David M Marcovitz) (02/15/91)

dmmg1176@uxa.cso.uiuc.edu (David M Marcovitz) writes:
>Well, I've gotten lots of code samples from people, and none of them
>solve my problem.  I went into ResEdit and set the default text of the
>editText item.  Now, no matter what I type in the editText item,
>GetIText returns that default string.

>The problem must be with the way I am interacting with Dialog Box
>Class Library that I got off the net.  I am going to scrap that and
>write the code myself (with a lot of help from the Mac Programming
>Primer) to deal with the dialog.

>Thanks to those who sent me responses.  I'm sure I'll use them when I
>rewrite my code.

I believe I understand the problem.  It is definitely with the Dialog
Box Class Library I was trying to use.  This library just uses the
dialog template created with ResEdit to get the initial items for the
dialog box.  It does not use the dialog manager to manipulate the
dialog.  Thus, when I use dialog manager functions (like GetDItem and
GetIText) I get information about the original template, not the
dialog box that is actually appearing on my screen.  I should be able
to use TextEdit routines (like TEGetText) to manipulate the TextEdit
items.  Or, I should be able to find methods in the library to do the
same thing.

--
David M. Marcovitz                     |  internet: marcovitz@uiuc.edu
Computer-based Education Research Lab  |            dmmg1176@uxa.cso.uiuc.edu
University of Illinois                 |  novanet:  marco / cca / cerl