LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

How to get table label width ?

Solved!
Go to solution
Hello,
How can I get table label width ? Length of label names varies.  
Or any other simple solutions to resize column widths to fit table nicely when even amount of columns varies ?   
Regards,
Petri
 
0 Kudos
Message 1 of 12
(5,238 Views)
Hello Petri,

There is no explicit attribute for the width of a table column label, but there is a more general method for getting the display width of a particular string.  You can pass a string and the name of a Meta Font to GetTextDisplaySize, which will return the height and width in pixels.  To get the width of the label for table column i, you would do the following:

int width;
char fontBuffer[128], labelBuffer[128];

GetTableColumnAttribute(panel, table, i, ATTR_COLUMN_LABEL_FONT, fontBuffer);
GetTableColumnAttribute(panel, table, i, ATTR_COLUMN_LABEL_TEXT, labelBuffer);

GetTextDisplaySize(labelBuffer, fontBuffer, NULL, &width);


(Note that if you do not feel safe using a fixed-size buffer, you can get the ATTR_COLUMN_LABEL_FONT_NAME_LENGTH and ATTR_COLUMN_LABEL_TEXT_LENGTH to dynamically create a buffer large enough to hold the font name and column label, respectively.)

To set your table column width, the table column size mode (ATTR_SIZE_MODE) must be VAL_USE_EXPLICIT_SIZE, which it is by default.  Then all you need to do is call the following:

int extraSpace = 10; /* however much padding you want */
SetTableColumnAttribute(panel, table, i, ATTR_COLUMN_WIDTH, width + extraSpace);

Good luck.

Mert A.
National Instruments

0 Kudos
Message 2 of 12
(5,223 Views)
Mert A.,
Thanks for your quick reply. I attach a .uir file to demonstrate how I would like to do. In .uir file I have defined in edit-time maximum amount of columns, but in run-time I keep the amount of columns same or remove some of them. What would be the simplest solution to recalculate the width of columns to fill or fit the table area fully ?
 
I was thinking an approach using your example as a reference:
1. get the table boundaries by using GetCtrlBoundingRect function
2. get columns in the table by using GetNumTableColumns
3. get longest label text in a row by using GetTableRowAttribute
4. convert longest label text to meta font size by using  GetTextDisplaySize
5. calculate column widths and resize columns taken account size of vertical scroll bar and width of label "column".
 
But I run in to a problem using the GetTextDisplaySize function. It requires a meta font. If I change all the cell fonts to e.g. NiAppMetaFont in edit time, I still get error message "The font passed is not in the meta font table". I have even changed all row labels to same meta font wihtout help. When debugging the returned font in run-time I get NiApp font. Any ideas why ?
 
Regards,
Petri
 
 
0 Kudos
Message 3 of 12
(5,206 Views)

Petri,

A metafont is a font that has all of its attributes specified: a typeface, a size, and properties such as bold, italics, etc. The GetTextDisplaySize requires a metafont, so that it can tell you how much space a string requires, based on all of the characteristics of a font, especially the point size.

What you have to do is to "ask" the table for all those characteristics -- not just the font name, but the size, the italics, etc... there is one attribute for each of those characteristics. Once you have all of those values, you should create your own metafont by passing them to the CreateMetaFont function -- name the metafont whatever you like -- and then pass this new name to the GetTextDisplaySize function.

Hope this helps.

Luis
NI

Message 4 of 12
(5,191 Views)

 

I forget to say thanks.

 

I had to modify this old project for other reasons and therefore I tried to improve this source because it never worked perfectly when resizing the table columns...

 

I attach a sample project which shows how the column width is set for all columns, but still the last column is out of the table boundaries. What goes wrong ?

 

/petri

 

0 Kudos
Message 5 of 12
(4,762 Views)

Petri,

 

The table width includes the "zero-th" column where you had labeled "Antenna Input", "f[MHz]",

"Channel ID", and "BER".  If you count this column, there are actually 11 columns in your table.

However, the function GetNumTableColumns(summary, SUMMARY_SUMMARYTABLE, &cols)

only returns 10 to the variable "cols".  Just modify "(width-maxLabelWidth-35)/cols" and

change it to "(width-maxLabelWidth-35)/(cols+1)".  It'll solve your problem. 

0 Kudos
Message 6 of 12
(4,734 Views)

thanks! 

 

I changed according to your advise and even removed the constant, with other words to '(width-maxLabelWidth)/(cols+1)'. It seems that still the table has somekind of automatic mechanism to adjust itself. If the number of columns is such a few, e.g. 3, the width of the columns will not fill the whole table width area. I can't know the number of columns in forhand, but it's better now than before...

 

The same applies for the height of the rows. I'm wondering why the command SetTableRowAttribute (summary, SUMMARY_SUMMARYTABLE, -1, ATTR_ROW_HEIGHT, (height/rows)); will not fulfill the whole heigth of the area...

 

regards,

petri

 

0 Kudos
Message 7 of 12
(4,716 Views)

I wonder myesef, too. 

Hope someone from NI can asnser this question.......

0 Kudos
Message 8 of 12
(4,702 Views)

Hi Petri,

 

The mistake you're making is in assuming that the width of the row label column is equivalent to the width of the widest row label string. The width of this column, in your program, has a fixed size which is set in the .uir file and is not affected by the text labels that you are using.

 

You also need to take into account the scroll bar width and the frame width. There is no attribute that returns the frame width, but for this style of table, you can hardcode it to 6 (3 for the left edge + 3 for the right edge).

 

This code should get you a lot closer:

 

    if (cols>0)
    {
        GetCtrlAttribute (summary, SUMMARY_SUMMARYTABLE, ATTR_ROW_LABELS_WIDTH, &rowLabelsWidth);
        GetCtrlAttribute (summary, SUMMARY_SUMMARYTABLE, ATTR_SCROLL_BAR_SIZE, &sbSize);
        SetTableColumnAttribute (summary, SUMMARY_SUMMARYTABLE,
                             -1, ATTR_COLUMN_WIDTH, (width-rowLabelsWidth-sbSize-6)/cols);
    }
 

The reason I said "closer" is because you will most likely not get an exact match. The reason is because (width-rowLabelsWidth-sbSize-6) is probably not a nice multiple of cols and therefore, the division will create a truncation error which will result in a gap.

 

Fortunately, there's a much simpler solution for what you want to do, as long as you're willing to size the table to match the column widths, rather than size the column widths to size the table. Use the function call below, either instead of the code above, or following the code above (if you still want to adjust the column width, and then remove the round-off error):

 

    SetCtrlAttribute (summary, SUMMARY_SUMMARYTABLE, ATTR_NUM_VISIBLE_COLUMNS, cols);

 

You can also do this in the User Interface Editor instead:

 

    Edit Table>>Size/Scroll Options

 

Hope this helps.

 

Luis

 

Message 9 of 12
(4,682 Views)

thanks Luis!

 

that helped me with the width of the columns. those attributes were new for me... I want to resize the columns to static table area.

 

what would be the reason why heights of the rows are not fulfilling the table height area ?

 

regards,

petri

 

 

0 Kudos
Message 10 of 12
(4,666 Views)