BookmarkSubscribeRSS Feed
Choose Language Hide Translation Bar
Highlighted
STEFHOLZ

New Contributor

Joined:

Oct 23, 2018

Determining Size of a Text Box before drawing it.

Hi,

 

is it possible to get the Size of an Text Box before the Text Box is actually drawn?

I generate dynamically a window and need to generate spacers to allign objects. Therefore I need the Size of several Text Box elements, before I actually draw them.

 

Cheers

4 ACCEPTED SOLUTIONS

Accepted Solutions
David_Burnham

Super User

Joined:

Jul 13, 2011

Solution

Re: Determining Size of a Text Box before drawing it.

The crude method - create a window that contains your content - then send messages to get the sizes - then delete the window (hopefully before it actually gets rendered) - then use the sizes to render the window more elegantly - I have to use this technique sometimes to get consistent output on both Windows and Mac.

-Dave
STEFHOLZ

New Contributor

Joined:

Oct 23, 2018

Solution

Re: Determining Size of a Text Box before drawing it.

Hi David,

 

basically that is what I did at the end, only that I did not delete the window but merely send the correct size messages to the text boxes with  "<< Set Width"

 

!BEWARE!

everybody who tries this: After drawing your initial window, you have to wait a little bit before you can take the measurements with "<< Get Size". A wait(0.0001) was sufficiant for me. If you do not do it strange things happen. (That took me half a day to figure out).

!BEWARE!

pmroz

Super User

Joined:

Jun 23, 2011

Solution

Re: Determining Size of a Text Box before drawing it.

Here's the function I use to determine text box height.  The hide(1) argument prevents it from being displayed.

get_height = function({_text, _pixels},
	{Default Local},

	pname = "get_height";

	if (is missing(_text),
	// then
		pix_height = 0;
		,
	// else if
		_text == "",
		pix_height = 0;
		,
	// else we've got some text to work with
		tb = text box(_text, set wrap(_pixels), set width(_pixels), hide(1));
		tmp_win = new window("test", tb);
// Get size info
		pix_height = (tb << get size)[2];

		tmp_win << close window;
	);

	pix_height;
);	// end get_height

 

David_Burnham

Super User

Joined:

Jul 13, 2011

Solution

Re: Determining Size of a Text Box before drawing it.

You might want to try Wait(0).  This basically says: wait until the tasks have been completed.

-Dave
8 REPLIES 8
ian_jmp

Staff

Joined:

Jun 23, 2011

Re: Determining Size of a Text Box before drawing it.

Did you try 'ShowProperties()' with the text box to see what messages it understands? I guess the size required depends on what text you want to put in it (and the font used). The example below shows that JMP will respect any width that you have set, but is forced to override the height when the box is drawn.

tb = TextBox();
tb << setWidth(20);
tb << setHeight(30);
Print(tb << getWidth, tb << getHeight, "\!n");

tb << setText("How now brown cow");
Print(tb << getWidth, tb << getHeight, "\!n");

NewWindow("Test", tb);
Print(tb << getWidth, tb << getHeight, "\!n");

 

 

markbailey

Staff

Joined:

Jun 23, 2011

Re: Determining Size of a Text Box before drawing it.

Have you exhausted the existing display boxes that are specifically responsible for alignment and layout? Their automatic behavior might reduce or eliminate your need to 'roll your own.'

Learn it once, use it forever!
STEFHOLZ

New Contributor

Joined:

Oct 23, 2018

Re: Determining Size of a Text Box before drawing it.

Yup, it is possible,
but I wanted to keep some gui elements which all interact with the same data table together, to make the code easier to read.

So it would be nice to have a message giving the dimensions in advance.
Chrismas is coming soon, so I put a it on my wishlist. ;)
David_Burnham

Super User

Joined:

Jul 13, 2011

Solution

Re: Determining Size of a Text Box before drawing it.

The crude method - create a window that contains your content - then send messages to get the sizes - then delete the window (hopefully before it actually gets rendered) - then use the sizes to render the window more elegantly - I have to use this technique sometimes to get consistent output on both Windows and Mac.

-Dave
STEFHOLZ

New Contributor

Joined:

Oct 23, 2018

Solution

Re: Determining Size of a Text Box before drawing it.

Hi David,

 

basically that is what I did at the end, only that I did not delete the window but merely send the correct size messages to the text boxes with  "<< Set Width"

 

!BEWARE!

everybody who tries this: After drawing your initial window, you have to wait a little bit before you can take the measurements with "<< Get Size". A wait(0.0001) was sufficiant for me. If you do not do it strange things happen. (That took me half a day to figure out).

!BEWARE!

David_Burnham

Super User

Joined:

Jul 13, 2011

Solution

Re: Determining Size of a Text Box before drawing it.

You might want to try Wait(0).  This basically says: wait until the tasks have been completed.

-Dave
STEFHOLZ

New Contributor

Joined:

Oct 23, 2018

Re: Determining Size of a Text Box before drawing it.

Yup, it is possible, but I wanted to keep some gui elements which all interact with the same data table together, to make the code easier to read. So it would be nice to have a message giving the dimensions in advance. Chrismas is coming soon, so I put a it on my wishlist. ;)
pmroz

Super User

Joined:

Jun 23, 2011

Solution

Re: Determining Size of a Text Box before drawing it.

Here's the function I use to determine text box height.  The hide(1) argument prevents it from being displayed.

get_height = function({_text, _pixels},
	{Default Local},

	pname = "get_height";

	if (is missing(_text),
	// then
		pix_height = 0;
		,
	// else if
		_text == "",
		pix_height = 0;
		,
	// else we've got some text to work with
		tb = text box(_text, set wrap(_pixels), set width(_pixels), hide(1));
		tmp_win = new window("test", tb);
// Get size info
		pix_height = (tb << get size)[2];

		tmp_win << close window;
	);

	pix_height;
);	// end get_height