﻿///<reference path="MicrosoftAjax.debug.js" > 
///<reference path="shared.js" > 
///<reference path="control.js" > 


SLx.ControlFactory.Label = function () 
{ 
    var template = SLx.Application.Current.get_resources().findName ( SLx.Constants.Label); 
    if ( template  != "" ) 
    {     
        var label= new SLx.Label ( null,  template , SLx.Label.DefaultTemplate );          
        return label ; 
        
    } 
    Sys.Debug.assert( false , "this should not fail");     
    return null ; 
} 


SLx.Label = function ( visual , xamlTemplate , templateParts )
{     
    this._textAlignment = SLx.HorizontalAlignment.Left; 
    
    SLx.Label.initializeBase (this , [ visual , xamlTemplate , templateParts ] ); 
    Sys.Debug.assert( this._textblock != null ) ; 
    
}, 

SLx.Label.DefaultTemplate = [ 
{ name: "Label" , ref: "_visualElement" , type: "element" }, 
{ name: "PART_Text" , ref: "_textblock"  , type: "element" } 
/* , { name: "PART_Content" , ref: "_content" , type: "ContentPresenter" } */]; 


SLx.Label.prototype = 
{ 
    set_text : function  ( value ) 
    { 
        if ( typeof  (value) != "string" ) 
            value = value.toString(); 
            
        this._textblock.Text = value ; 
        this.invalidateLayout(); 
    }, 
    
    get_text : function  ( ) 
    {
        return this._textblock.Text;  
    }, 
    
    get_width : function  () 
    { 
        // if it has NOT been measured before 
        if ( this._isMeasureInvalidated ) 
        {             
            if ( this._sizeToContent ) 
                return this._textblock.actualWidth ; 
            else 
                return this._visualElement.Width ; 
        }   
        else 
        {   
            Sys.Debug.assert (  this._visualElement.Width == this._desiredSize.Width ,  "Remove, looking for gaps between measure and arrange");     
            return this._visualElement.Width ; 
        } 
            
    }, 
    
    get_height : function  () 
    { 
        if ( this._isMeasureInvalidated ) 
        { 
            return this._textblock.actualHeight; 
        }   
        else 
            return this._visualElement.Height ; 
            
    },
    
    set_textAlignment : function ( value ) 
    { 
        this._textAlignment = value ; 
    } , 
    
    get_textAlignment : function ( ) 
    { 
        return this._textAlignment ; 
    },  
    
    measure : function SLx$Label$measure (  width, height ) 
    { 
        Sys.Debug.assert ( this._textblock  != null , "bindParts should have been called"); 
        
      
        if ( this.get_sizeToParent () ) 
        { 
            if ( this._horizontalAlignment == SLx.HorizontalAlignment.Stretch ) 
                this._desiredSize.Width =  width ; 
            else 
                this._desiredSize.Width =  this._textblock.actualWidth ; 
                
            if ( this._verticalAlignment == SLx.VerticalAlignment.Stretch )                
                this._desiredSize.Height = height ;                   
            else 
                this._desiredSize.Height = this._textblock.actualHeight ;                   
                
        } 
        else if (this.get_sizeToContent () ) 
        {         
            this._desiredSize.Width =  this._textblock.actualWidth ; 
            this._desiredSize.Height = this._textblock.actualHeight  ;         
        }         
        else 
            this._desiredSize = new SLx.Size ( this._visualElement.width , this._visualElement.height ) ;         
        
        Sys.Debug.trace ("Label$measure:  Desired Size " + this._desiredSize.toString () );         
        this._isMeasureInvalidated = false ; 
    },
    
    set_foreground : function ( value ) 
    { 
      if ( this._textblock != null ) 
            this._textblock.Foreground = value ;             

    },  
    
    get_foreground : function () 
    { 
        if ( this._textblock != null ) 
            return this._textblock.Foreground ; 
            
        return null ;     
    }, 
    
    
    set_fontFamily : function ( value ) 
    { 
        if ( this._textblock != null ) 
            this._textblock.FontFamily = value ;             
    } , 
    
    get_fontFamily : function () 
    { 
        if ( this._textblock != null ) 
            return this._textblock.FontFamily; 
        return null ; 
    
    }, 
    
    /*  I really should avoid this; it will break layout 
    get_textBlock : function ( ) 
    { 
        return this._textblock ; 
    } 
        */ 
        
        
    arrange : function SLx$Label$arrange ( left, top, width , height ) 
    { 
    
        this._visualElement["Canvas.Left"] = left; 
        this._visualElement["Canvas.Top"] = top ;         
        this._visualElement.width = width ; 
        this._visualElement.height = height; 
        
        
        if (  this._textAlignment != SLx.HorizontalAlignment.Left ) 
        { 
            SLx.VisualTreeHelper.alignVisual ( this._textblock , width , height , this._textAlignment , SLx.VerticalAlignment.Top ); 
            
        } 
        /* 
        if ( this._content != null && this._content.get_visual().Tag  != null ) 
        {
            
                var verAlignment = this._content.get_verticalAlignment(); 
                var horAlignment = this._content.get_horizontalAlignment (); 
                var top = 0 ; 
                var size = this._content.get_desiredSize(); 
                
                if ( horAlignment == SLx.HorizontalAlignment.Center ) 
                { 
                    left = ( this.get_width() - size.Width )/2 ;                  
                } 
                else if ( horAlignment == SLx.HorizontalAlignment.Right ) 
                    left =  this.get_width() - size.Width  ; 
                else 
                    left = 0 ; 
                    
                if ( verAlignment == SLx.VerticalAlignment.Center ) 
                { 
                    top = ( this.get_height() - size.Height )/2 ;                  
                } 
                else if ( verAlignment == SLx.VerticalAlignment.Right ) 
                    top=   (this.get_height() - size.Height )  ; 
                else 
                    top = 0 ; 
                    
                this._content.arrange ( left, top, size.Width , size.Height ); 
        }  */      
        
        this._isArrangeInvalidated = false ;
    
    }, 
    toString : function  () 
    { 
        return this._uniqueSystemId.toString ()  + "    " + Object.getTypeName(this) + "    " + this._id ; 
    }  
}  


SLx.Label.registerClass('SLx.Label', SLx.Control ); 
