Home » U++ Library support » ArrayCtrl, HeaderCtrl & GridCtrl » Calculate needed cell width to fit some text 
	
		
		
			| Calculate needed cell width to fit some text [message #25168] | 
			Sat, 13 February 2010 10:31   | 
		 
		
			
				
				
				
					
						  
						mdelfede
						 Messages: 1310 Registered: September 2007 
						
					 | 
					Ultimate Contributor  | 
					 | 
		 
		 
	 | 
 
	
		I need to setup cell widths to fit some text; with HeaderCtrl().Absolute() I can set absoulte cell widths, but I can't manage to get pixel width of a given text. 
trying with 
 
StdFont().GetAveWidth() * myString.GetCount()
  
 
gives me a by far too big value for character width. 
 
Any hint ? 
 
Another stuff... I have a DropGrid containing images, embedded into an ArrayCtrl. I also need to set the cell width to fit the image, but doing with Image.GetSize().cx misses the width needed for the drop arrow.... By now I do it adding a fixed value, but I'm afraid that it'll behave bad changing platform. 
 
Ciao 
 
Max 
		
		
		
 |  
	| 
		
	 | 
 
 
 |  
	
		
		
			| Re: Calculate needed cell width to fit some text [message #25173 is a reply to message #25168] | 
			Sat, 13 February 2010 12:29    | 
		 
		
			
				
				
				
					
						  
						Sender Ghost
						 Messages: 301 Registered: November 2008 
						
					 | 
					Senior Member  | 
					 | 
		 
		 
	 | 
 
	
		Hello, Max. 
 
I made following sample application for your first question from which you can understand about how to setup ArrayCtrl columns with fixed width. 
#include <CtrlLib/CtrlLib.h>
using namespace Upp;
const String sampleText("Another Sample Text");
void CreateOption(One<Ctrl>& ctrl)
{
	ctrl.Create<Option>().NoWantFocus();
}
class FixedArray : public TopWindow {
public:
	typedef FixedArray CLASSNAME;
	FixedArray();
	ArrayCtrl list;
	void ArrayBar(Bar& bar);
	void ToggleOption();
	void FillWithData();
};
FixedArray::FixedArray()
{
	Title("Fixed ArrayCtrl test application");
	CenterScreen().MaximizeBox().MinimizeBox().Sizeable();
	SetRect(Size(320, 240));
	list.AutoHideSb();
	list.WhenBar = THISBACK(ArrayBar);
	// Initialize ArrayCtrl columns
	list.AddColumn().Ctrls(CreateOption).HeaderTab().Fixed(26); // first method
	list.AddColumn("Content");
	list.AddColumn("Description");
	// Get width for sampleText
	const int sampleWidth = GetTextSize(sampleText, Draw::GetStdFont()).cx;
	// Set width for column
	HeaderCtrl& hc = list.HeaderObject();
	hc.Tab(1).Fixed(sampleWidth + hc.Tab(1).GetMargin() * 2 + 1); // second method
	Add(list.VSizePosZ(4, 4).HSizePosZ(4, 4));
}
void FixedArray::ArrayBar(Bar& bar)
{
	if (list.HasFocus() && list.IsCursor())
		bar.AddKey(K_SPACE, THISBACK(ToggleOption));
	list.StdBar(bar);
}
void FixedArray::ToggleOption()
{
	GuiLock __;
	list.Set(0, !list.Get(0));
}
void FixedArray::FillWithData()
{
	GuiLock __;
	list.Clear();
	for (int i = 1; i <= 10; ++i)
		list.Add(i % 2 == 0, sampleText, Format("Description of %d", i));
}
GUI_APP_MAIN
{
	//Ctrl::GlobalBackPaintHint();
	FixedArray fa;
	fa.FillWithData();
	fa.Run();
}
 
  
		
		
		[Updated on: Sat, 13 February 2010 14:06] Report message to a moderator  
 |  
	| 
		
	 | 
 
 
 |  
	| 
		
 |  
	| 
		
 |   
Goto Forum:
 
 Current Time: Tue Nov 04 06:45:09 CET 2025 
 Total time taken to generate the page: 0.04897 seconds 
 |