Action Script 3: Scaling fonts for text in a TextField

Quite a few times in AS3 I need dynamic text in a TextField control to scale while the solution is quite simple finding the method was time consuming. Essentially we need to call setTextFormat on the TextField control for it to recalculate textHeight and textWidth, thus allowing us to discover weather all the content of the TextField object will be visible.
Setting the defaultTextFormat.size member on its own will not cause textHeight or textWidth to be recalculated – indeed it may not even be reflected by the TextField when it is rendered.

Below is a simple example of an override of TextField control which will scale its font accordingly (download)

package
{
 import flash.text.TextField;
 import flash.text.TextFormat;

 public class ScaleTextField extends TextField
 {
  public function ScaleTextField()
  {
  }

  public override function set text(val:String):void
  {
   super.text = val;
   var tf:TextFormat = this.getTextFormat();
   if(this.textHeight>(this.height-4) || this.textWidth>this.width)
   {
    // Scale down
    while((this.textHeight>(this.height-4) || this.textWidth>this.width) && int(tf.size)>1)
    {
    tf.size = int(tf.size)-1;
    this.setTextFormat(tf);
   }
   }else
   {
    // Scale up
    while(this.textHeight<(this.height-4) && this.textWidth<this.width)
    {
    tf.size = int(tf.size)+1;
    this.setTextFormat(tf);
    }
    tf.size = int(tf.size)-1;
    this.setTextFormat(tf);
   }
  }
 }
}

Of course for the starling framework you only need change the import statments as appropriate.

Posted in AS3 | Tagged | Comments Off

Installing Class 1 StartSSL Certificate on Debian

To install a Class 1 StartSSL Certificate on Debian first you will need to obtain both your private key and certificate from StartSSL’s website and upload them to your server. I’d recommend that you keep both the Webserver SSL/TLS Certificate and StartSSL’s CA Certificates in a secure directory for the website it is to be used by (i.e. a directory not accessible by HTTP).

Step 1: Assuming you have encrypted your private key when generating it on StartSSL’s website you will need to decrypt the key on your server or a password will be required whenever the HTTP server restarts.
Doing this is simple, login via SSH to your server and go to the directory containing your certificate and (assuming the file is named ssl.key) type the following:

openssl rsa -in ssl.key -out ssl.key

openssl should then request the password for the private key and decrypt it.

Step 2: Once the key is decrypted we need to download StartSSL’s Certificate Authority Certificates. For the Class 1 certificate we only need ca.pem and sub.class1.server.ca.pem
So again in your SSH console type the following 2 commands:

wget https://www.startssl.com/certs/ca.pem
wget https://www.startssl.com/certs/sub.class1.server.ca.pem

Once you have downloaded these files you should set the permissions to read only by the file owner (chmod 400 *)

Step 3: Assuming you have already enabled SSL for your HTTP server you only need add the SSL certificate and CA Certificates to your virtual host, in apache we do this by creating something like the following:

<VirtualHost *:443>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/vhosts/example.com/public_html/
SSLEngine on
SSLProtocol all -SSLv2
SSLCipherSuite ALL:!ADH:!EXPORT:!SSLv2:RC4+RSA:+HIGH:+MEDIUM
SSLCertificateFile /path/to/ssl/certificate.crt
SSLCertificateKeyFile /path/to/ssl/certificate.key
SSLCertificateChainFile /path/to/ssl/ca/certificate/sub.class1.server.ca.pem
SSLCACertificateFile /path/to/ssl/ca/certificate/ca.pem
SetEnvIf User-Agent “.*MSIE.*” nokeepalive ssl-unclean-shutdown
<VirtualHost>

If you have not already enabled SSL in apache you can do so by moving the file mods-available/ssl.load to the mods-enabled directory (typically these can be found in /etc/apache2/)

All that is left is to restart the HTTP server (for apache this would be /etc/init.d/apache2 restart) and as long as there are no errors your certificate is ready for use!

Posted in Debian, Linux, Security | Tagged , , , , , | Comments Off

Converting to and from VB6 Date to Unix / POSIX time

Visual Basic 6 as a language may slowly be falling into disuse and is a language which is no longer supported by Microsoft but we still need to interface old and new systems from time to time.
The rise of PHP, Ruby and other languages which have roots in the Unix environment converting date time values from Unix time to the IEEE 64 bit value used by VBScript is common task which can sometimes prove to be a bit of a headache. However, the conversion is actually quite simple.

Unix represents time by counting the seconds since the Unix epoch, which is January 1st 1970 00:00:00 UTC. Most systems today use a 32 bit signed integer to hold this value allowing it to represent dates and times between December 13th 1901 and January 19th 2038

Thus to convert Unix Time to a Date value useful to Visual Basic we need only add the number of seconds in the value to the date 1/1/1970 00:00:00 UTC. This can easily be done using the DateAdd function:

Function FromUnixTime(UnixTime As Long) As Date
FromUnixTime = DateAdd("s", UnixTime, DateSerial(1970, 1, 1))
End Function

Converting from Visual Basic to Unix Time is just as simple save that Technically Visual Basic can represent Dates and Times outside that which Unix Time can represent. To do the conversion we need only find the number of seconds from Unix epoch to our target date, for which we can use the DateDiff function:

Function ToUnixTime(time As Date) As Long
ToUnixTime = DateDiff("s", DateSerial(1970, 1, 1), time)
End Function

Note: Should the date value in Visual Basic be beyond a date which Unix Time can represent then ToUnixTime will throw and overflow error.

Posted in PHP, VB6 | Comments Off

PHP SoapClient Authentication

SoapClientAuth replaces the SoapClient class in PHP. This Soap Client, whose interface and operation is identical to the SoapClient class, will perform HTTP authenticaion for SOAP messages and when downloading WSDL over HTTP & HTTPS.

Out of the box PHPs SoapClient class is unable to authenticate via HTTP when downloading WSDL and is unable to authenticate to web services protected by NTLM authentication. SoapClientAuth however will authenticate to Basic, Digest and NTLM web services even if the WSDL file itself is protected by HTTP authentication.

You should use this class as you would use the SoapClient class and simply provide the username and password to the service in the options (login and password) parameter of the constructor. For Example:

$soapClient = new SoapClientAuth('http://url.to.my/soap/service',
            array(
                'login' => 'username',
                'password' => 'password'
            ));

$soapClient->MyMethod();


Download SoapClientAuth

Requirements:
PHP 5.0
curl

SoapClientAuth is distributed and licensed under the terms of the GNU General Public License.
Copyright tc software © 2011

Posted in PHP | Comments Off

Crystal Reports Redistributable Download (.NET 2005)

Redistributable installation packs for Crystal Reports as used in Visual Studio 2005. These msi’s where built from the merge modules as supplied by Business Objects, they will install the Crystal Reporting engine and assemblies required by any Applications (both Desktop and ASP.NET) utilizing Crystal. Simply select the appropriate file for your platform, download and install. Then restart any application which uses Crystal Reports (Restart IIS for web applications)

Crystal Reports Redistributable for x86 systems
Crystal Reports Redistributable for x64 systems
Crystal Reports Redistributable for i64 systems

.NET 2.0 is required, the installers will take you to Microsoft’s website if it is not found on your system.
These installations will install the following assemblies (Public Key Token: 692fbea5521e1304, Version 10.2.3600.0):

CrystalDecisions.CrystalReports.Engine
CrystalDecisions.CrystalReports.Engine.resources
CrystalDecisions.Data.AdoDotNetInterop
CrystalDecisions.Enterprise.Desktop.Report
CrystalDecisions.Enterprise.Framework
CrystalDecisions.Enterprise.InfoStore
CrystalDecisions.Enterprise.PluginManager
CrystalDecisions.Enterprise.Viewing.ReportSource
CrystalDecisions.KeyCode
CrystalDecisions.ReportAppServer.ClientDoc
CrystalDecisions.ReportAppServer.CommLayer
CrystalDecisions.ReportAppServer.CommonControls
CrystalDecisions.ReportAppServer.CommonObjectModel
CrystalDecisions.ReportAppServer.Controllers
CrystalDecisions.ReportAppServer.CubeDefModel
CrystalDecisions.ReportAppServer.DataDefModel
CrystalDecisions.ReportAppServer.DataSetConversion
CrystalDecisions.ReportAppServer.ObjectFactory
CrystalDecisions.ReportAppServer.ReportDefModel
CrystalDecisions.ReportAppServer.XmlSerialize
CrystalDecisions.ReportSource
CrystalDecisions.ReportSource.resources
CrystalDecisions.Shared
CrystalDecisions.Shared.resources
CrystalDecisions.Web
CrystalDecisions.Web.resources
CrystalDecisions.Windows.Forms
CrystalDecisions.Windows.Forms.resources

Posted in .NET | Tagged | Comments Off

How to create a silent ringtone for the iPhone

Silencing the ringtone on your phone is a useful feature for call screening but by default the iPhone is not able to turn off ringtones for individual contacts. Fortunately creating a ringtone is quite simple and creating a silent tone only requires an empty sound file and iTunes.

First, to create a silent tone (download), you should create an empty sound file using a sound editor (such as Audacity). To avoid any problems with any iPhone updates I would recommend creating a file with at least 1 second of silence. Audacity itself can generate this for you by clicking on ‘Generate’ and then ‘Silence’ on the menu bar. The silent file can be in any format supported by iTunes (wave file, mp3, AAC, etc…)

Next to convert the sound file to a ring tone you should import the file into your iTunes library (Note: the maximum length for a ring tone is 40 seconds) and use iTunes to convert the file into an AAC file (right click on the file in iTunes and click ‘Create AAC Version’.

Once iTunes has created an AAC copy of the file simply drag this out onto your desktop and change the file extension to m4r (i.e. silence.m4a should be changed to silence.m4r).

Return to iTunes and delete the AAC copy of your sound file (if you do not iTunes will not import the ring tone) and then drag the ring tone file on your desktop into iTunes. Then simply import the ring tone into your iPhone!

If you’re using a silent tone for call screening its often useful to create a contact on your phone and set the ringtone for the contact to your silent ring tone. Then simply add numbers to this contact that you do not want the phone to play a tone to.

You can also download our copy of the silent ringtone by clicking here

Posted in iPhone | Tagged , | Comments Off

Capturing Return / Enter in DataGridViewTextBoxCell’s in a DataGridView

By default the DataGridView will move to the cell below whenever the user presses the return key and the only way for a user to add a new line to a text box cell, if the column’s style WrapMode has been set to true, is to press SHIFT+ENTER.
For many users editing a multiline field in a DataGridView this behaviour can be annoying and counter-intuitive but changing this behaviour is not just as simple as adding a KeyDown / KeyPress event handler.

To Change the default behaviour of the DataGridView’s response to the key press we must implement a new class which is derived from DataGridView and then override the ProcessCmdKey member, allowing us to intercept the message before the DataGridView applies its logic and apply our own.

For the application I am working I wanted a new line to be created when the user pressed the return key instead of moving to the next row if the user was editing a text box cell and the cell’s WrapMode was true. The following code is what we’re using at the moment in one of our applications:

	public class MultiLineDataGridView : DataGridView
	{
		protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
		{
			if (this.CurrentCell is DataGridViewTextBoxCell &&
				this.CurrentCell.IsInEditMode &&
				(keyData == Keys.Enter || keyData == Keys.Return))
			{
				SendKeys.Send("+~");
				return true;
			}else
				return base.ProcessCmdKey(ref msg, keyData);
		}
	}


While I’m no fan of using SendKeys and would much rather modify the key code being processed by the application it turns out that in this case the DataGridView responds better to having the key event triggered as if the user had initiated it themselves and will create new lines in Word Wrapped text box cells but apply its default behaviour (and move to the next row) on any other cell.

intuitive

Posted in .NET | Comments Off

Efficient Subtotalling in MySQL

I quite often find when developing a site or an application that uses an SQL database that I have to produce multiple subtotals from a single table and/or using very complex rules and most of the time I’m trying to generate these totals using SQL only to minimize the workload on a system. While there are many ways to do this by far the most efficient and flexible way I’ve found is to use the aggregate function SUM() and IF() in MySQL.

For example, recently while working on Car Arena, we needed to produce a report for the site owners which contained the number of trade cars, private cars and the total number of cars submitted per day. Not only was this report to be as lightweight as possible (as it would display all dates on a single page) but also writing a query which would produce the report without calculating the product of a conditional statement would possibly mean writing subqueries and overcomplicating the SQL.

So for this report my solution was to count the number of records in the database which had its trade valuation flag set to true and the total number of cars grouped into days, months and years. The conditional statement IF produces the value of 1 or 0, depending on the logic for the field, which is then passed to SUM to ‘count’ the number of records in the group. For example:

SUM(IF(TradeValuation, 1, 0)) As Trade

While TRUE and FALSE can be cast to an unsigned integer type MySQL defines TRUE as being anything but zero. TRUE and FALSE are simply aliases for 1 and 0 respectively and while often you find zero and one being used as boolean values within a database using IF() ensures you never fall fowl of the exception to the rule and future proofs the statement somewhat.

The whole statement for this report which calculates the subtotals for the number of cars posted was just:

Just one pass on the Valuations table is needed to generate the subtotals for the report so responses are quick and do not require much of the web servers resources.

Posted in SQL | Tagged , | Comments Off