<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>

<channel>
	<title>JMSoftware</title>
	<atom:link href="http://www.jmsoftware.co.uk/feed" rel="self" type="application/rss+xml" />
	<link>http://www.jmsoftware.co.uk</link>
	<description>Software Development</description>
	<pubDate>Sun, 26 Oct 2008 16:16:38 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.3</generator>
	<language>en</language>
			<item>
		<title>Getting the next n birthdays from a table in SQL</title>
		<link>http://www.jmsoftware.co.uk/blog/getting-the-next-n-birthdays-from-a-table-in-sql</link>
		<comments>http://www.jmsoftware.co.uk/blog/getting-the-next-n-birthdays-from-a-table-in-sql#comments</comments>
		<pubDate>Sun, 26 Oct 2008 14:10:35 +0000</pubDate>
		<dc:creator>Jonathan</dc:creator>
		
		<category><![CDATA[Programming]]></category>

		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://www.jmsoftware.co.uk/?p=20</guid>
		<description><![CDATA[If we have a table with a basic structure like this:

----------
person
----------
person_id
name
dob
...
----------

Let&#8217;s run a query with the results sorted by order of upcoming birthdays:

SELECT *,
IF(MONTH(`dob`) < MONTH(NOW()) &#124;&#124;
	(MONTH(`dob`) = MONTH(NOW()) &#038;&#038; DAY(`dob`) < DAY(NOW())),
	MONTH(`dob`) + 12, MONTH(`dob`)) AS `adjusted_month`
FROM `person`
ORDER BY `adjusted_month`, DAY(`dob`), `name`

An alias called adjusted_month is used.
If the month of birth is earlier then [...]]]></description>
			<content:encoded><![CDATA[<p>If we have a table with a basic structure like this:<br />
<code><br />
----------<br />
person<br />
----------<br />
person_id<br />
name<br />
dob<br />
...<br />
----------<br />
</code></p>
<p>Let&#8217;s run a query with the results sorted by order of upcoming birthdays:</p>
<pre><code class="prettyprint">
SELECT *,
IF(MONTH(`dob`) < MONTH(NOW()) ||
	(MONTH(`dob`) = MONTH(NOW()) &#038;&#038; DAY(`dob`) < DAY(NOW())),
	MONTH(`dob`) + 12, MONTH(`dob`)) AS `adjusted_month`
FROM `person`
ORDER BY `adjusted_month`, DAY(`dob`), `name`
</code></pre>
<p>An alias called adjusted_month is used.</p>
<p>If the month of birth is earlier then the current month, or if it is the same and the day of birth is earlier then the current day, adjusted_month is set to the the month of birth + 12. Otherwise adjusted_month is just set to the month of birth.</p>
<p>The results can then be sorted by adjusted_month and the day to get the desired result.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jmsoftware.co.uk/blog/getting-the-next-n-birthdays-from-a-table-in-sql/feed</wfw:commentRss>
		</item>
		<item>
		<title>Number of Selected Items in a Multiple Select Box with jQuery</title>
		<link>http://www.jmsoftware.co.uk/blog/number-of-selected-items-in-a-multiple-select-box-with-jquery</link>
		<comments>http://www.jmsoftware.co.uk/blog/number-of-selected-items-in-a-multiple-select-box-with-jquery#comments</comments>
		<pubDate>Sat, 25 Oct 2008 11:05:05 +0000</pubDate>
		<dc:creator>Jonathan</dc:creator>
		
		<category><![CDATA[Programming]]></category>

		<category><![CDATA[jquery]]></category>

		<category><![CDATA[input]]></category>

		<category><![CDATA[multiple select box]]></category>

		<guid isPermaLink="false">http://www.jmsoftware.co.uk/?p=21</guid>
		<description><![CDATA[With the html:

&#60;select name=&#34;my_list&#34; multiple=&#34;multiple&#34;&#62;
&#160;&#160;&#160;&#160;&#60;option value=&#34;one&#34;&#62;1&#60;/option&#62;
&#160;&#160;&#160;&#160;&#60;option value=&#34;two&#34;&#62;2&#60;/option&#62;
&#160;&#160;&#160;&#160;&#60;option value=&#34;three&#34;&#62;3&#60;/option&#62;
&#160;&#160;&#160;&#160;&#60;option value=&#34;four&#34;&#62;4&#60;/option&#62;
&#160;&#160;&#160;&#160;&#60;option value=&#34;five&#34;&#62;5&#60;/option&#62;
&#60;/select&#62;

One way to find the number of options selected:

$("select[@name='my_list']>option:selected&#8221;).length

]]></description>
			<content:encoded><![CDATA[<p>With the html:<br />
<code class="prettyprint"><br />
&lt;select name=&quot;my_list&quot; multiple=&quot;multiple&quot;&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&lt;option value=&quot;one&quot;&gt;1&lt;/option&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&lt;option value=&quot;two&quot;&gt;2&lt;/option&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&lt;option value=&quot;three&quot;&gt;3&lt;/option&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&lt;option value=&quot;four&quot;&gt;4&lt;/option&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&lt;option value=&quot;five&quot;&gt;5&lt;/option&gt;<br />
&lt;/select&gt;<br />
</code></p>
<p>One way to find the number of options selected:<br />
<code><br />
$("select[@name='my_list']>option:selected&#8221;).length<br />
</code></p>
]]></content:encoded>
			<wfw:commentRss>http://www.jmsoftware.co.uk/blog/number-of-selected-items-in-a-multiple-select-box-with-jquery/feed</wfw:commentRss>
		</item>
		<item>
		<title>TextRenderer is slow, DrawString is wrong</title>
		<link>http://www.jmsoftware.co.uk/blog/textrenderer-is-slow-drawstring-is-wrong</link>
		<comments>http://www.jmsoftware.co.uk/blog/textrenderer-is-slow-drawstring-is-wrong#comments</comments>
		<pubDate>Thu, 30 Aug 2007 22:42:38 +0000</pubDate>
		<dc:creator>Jonathan</dc:creator>
		
		<category><![CDATA[.net]]></category>

		<category><![CDATA[C#]]></category>

		<category><![CDATA[Programming]]></category>

		<category><![CDATA[.NET]]></category>

		<category><![CDATA[api_functions]]></category>

		<category><![CDATA[drawing]]></category>

		<category><![CDATA[DrawString]]></category>

		<category><![CDATA[google]]></category>

		<category><![CDATA[hacks]]></category>

		<category><![CDATA[string_array]]></category>

		<category><![CDATA[TextRenderer]]></category>

		<guid isPermaLink="false">http://www.jmsoftware.co.uk/blog/textrenderer-is-slow-drawstring-is-wrong</guid>
		<description><![CDATA[After struggling with WinAPI code and hacks in .NET 1.0 I was really looking forward to the TextRenderer class when I first heard about it. But as it turns out, not only is TextRenderer horrible at measuring character size it is also monumentally slow at rendering text.
All I needed to do was to draw a [...]]]></description>
			<content:encoded><![CDATA[<p>After struggling with WinAPI code and hacks in .NET 1.0 I was really looking forward to the <a href="http://msdn2.microsoft.com/en-us/library/system.windows.forms.textrenderer.aspx">TextRenderer</a> class when I first heard about it. But as it turns out, not only is TextRenderer horrible at measuring character size it is also monumentally slow at <em>rendering text</em>.<span id="more-18"></span></p>
<p>All I needed to do was to draw a series of characters onto an image, each one having a different Color value. Easy right? Straightforward code, plug it in to the Ascgen and&#8230; wait&#8230; we get the output required. After a quick profile of the application, it was clear that the problem was with the external function and there was nothing that could be done to speed things up to an acceptable level.</p>
<p>Before digging into api functions, I decided to look back to the older method of drawing strings: <a href="http://msdn2.microsoft.com/en-us/library/system.drawing.graphics.drawstring.aspx">Graphics.DrawString</a>.</p>
<p>The code is less straightforward, but, despite having to create a new SolidBrush object for every Color, it works quickly. An image that took around 10 seconds with TextRenderer takes milliseconds with DrawString. The only obvious problem was that DrawString shifted the text right a fraction, but a quick google leads to a solution: using StringFormat.GenericTypographic in the call.</p>
<p>Now, the next obvious step was to use DrawString to improve the speed of drawing text all in one Color, and that&#8217;s where it all goes wrong: DrawString adds extra space between characters, and there doesn&#8217;t seem to be a way to stop it.</p>
<p>Applying the same code as before and printing each character separately (after first having to split it into a string array) gives the text spaced correctly. However it&#8217;s actually marginally slower then calling TextRenderer once on the whole string, and far more complicated.</p>
<p>Ideally I&#8217;d like to put the two together, so I need to either find a way to speed up TextRenderer or a way to fix the spacing in DrawString.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jmsoftware.co.uk/blog/textrenderer-is-slow-drawstring-is-wrong/feed</wfw:commentRss>
		</item>
		<item>
		<title>Dragging and dropping onto a RichTextBox</title>
		<link>http://www.jmsoftware.co.uk/blog/dragging-and-dropping-onto-a-richtextbox</link>
		<comments>http://www.jmsoftware.co.uk/blog/dragging-and-dropping-onto-a-richtextbox#comments</comments>
		<pubDate>Tue, 24 Jul 2007 12:38:36 +0000</pubDate>
		<dc:creator>Jonathan</dc:creator>
		
		<category><![CDATA[.net]]></category>

		<category><![CDATA[C#]]></category>

		<category><![CDATA[Programming]]></category>

		<category><![CDATA[.NET]]></category>

		<category><![CDATA[drag_drop]]></category>

		<category><![CDATA[drop_operations]]></category>

		<category><![CDATA[filenames]]></category>

		<category><![CDATA[intellisense]]></category>

		<category><![CDATA[net_framework]]></category>

		<category><![CDATA[Practical]]></category>

		<category><![CDATA[RichTextBox]]></category>

		<category><![CDATA[richtextbox_control]]></category>

		<guid isPermaLink="false">http://www.jmsoftware.co.uk/blog/dragging-and-dropping-onto-a-richtextbox</guid>
		<description><![CDATA[One of many flaws with the RichTextBox control is its lack of support for drag/drop operations. Luckily it&#8217;s simple to add. In fact, it&#8217;s so simple you&#8217;ll wonder why they didn&#8217;t just support it out of the box.
Let&#8217;s assume that you&#8217;ve created a form and added a RichTextBox called richTextBox1. Rather then going to the [...]]]></description>
			<content:encoded><![CDATA[<p>One of many flaws with the RichTextBox control is its lack of support for drag/drop operations. Luckily it&#8217;s simple to add. In fact, it&#8217;s so simple you&#8217;ll wonder why they didn&#8217;t just support it out of the box.<span id="more-16"></span></p>
<p>Let&#8217;s assume that you&#8217;ve created a form and added a RichTextBox called richTextBox1. Rather then going to the properties window to add the events, you&#8217;ll need to do it yourself.</p>
<p>Add the following in the constructor of the form:</p>
<pre><code class="prettyprint">richTextBox1.AllowDrop = true;
richTextBox1.DragDrop += new DragEventHandler(richTextBox1_DragDrop);
richTextBox1.DragEnter += new DragEventHandler(richTextBox1_DragEnter);
</code></pre>
<p>Type in the &#8220;+=&#8221; and press tab to automatically complete the line, then press tab again to create the function. Or just add the handler functions manually:</p>
<pre><code class="prettyprint">public void richTextBox1_DragEnter(object sender, DragEventArgs e) {
	if (e.Data.GetDataPresent(DataFormats.FileDrop)) {
		e.Effect = DragDropEffects.Copy;
	}
	else {
		e.Effect = DragDropEffects.None;
	}
}

public void richTextBox1_DragDrop(object sender, DragEventArgs e) {
	string[] filenames = (string[])e.Data.GetData(DataFormats.FileDrop);

	foreach (string filename in filenames) {
		// code to process a filename
	}
}
</code></pre>
<p>You can also implement DragOver and DragLeave in the same way, although those are there to &#8220;support the .NET Framework infrastructure&#8221; and are &#8220;not intended to be used directly from your code&#8221;. Intellisense doesn&#8217;t even show those events, but you can type them in and compile without problems. Also note that DragLeave is an EventHandler rather then a DragEventHandler.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jmsoftware.co.uk/blog/dragging-and-dropping-onto-a-richtextbox/feed</wfw:commentRss>
		</item>
		<item>
		<title>Making sure a number is between 0 and 255</title>
		<link>http://www.jmsoftware.co.uk/blog/making-sure-a-number-is-between-0-and-255</link>
		<comments>http://www.jmsoftware.co.uk/blog/making-sure-a-number-is-between-0-and-255#comments</comments>
		<pubDate>Tue, 10 Jul 2007 13:21:47 +0000</pubDate>
		<dc:creator>Jonathan</dc:creator>
		
		<category><![CDATA[.net]]></category>

		<category><![CDATA[C#]]></category>

		<category><![CDATA[Programming]]></category>

		<category><![CDATA[.NET]]></category>

		<category><![CDATA[between]]></category>

		<category><![CDATA[bitwise_operations]]></category>

		<category><![CDATA[byte]]></category>

		<category><![CDATA[dotnet]]></category>

		<category><![CDATA[limit]]></category>

		<category><![CDATA[math]]></category>

		<category><![CDATA[max]]></category>

		<category><![CDATA[min]]></category>

		<category><![CDATA[number]]></category>

		<category><![CDATA[pixel]]></category>

		<category><![CDATA[Theory]]></category>

		<guid isPermaLink="false">http://www.jmsoftware.co.uk/blog/making-sure-a-number-is-between-0-and-255</guid>
		<description><![CDATA[Recently while reworking the code for the Ascgen dotNET I needed to make sure that a variable is between 0 and 255 before converting it to a byte, and I needed it to be as fast as possible since it can get called several million times a second.
After realising that there really wasn&#8217;t a built [...]]]></description>
			<content:encoded><![CDATA[<p>Recently while reworking the code for the <a href="/software/ascgen">Ascgen dotNET</a> I needed to make sure that a variable is between 0 and 255 before converting it to a byte, and I needed it to be as fast as possible since it can get called several million times a second.<span id="more-9"></span></p>
<p>After realising that there really wasn&#8217;t a built in function like:</p>
<p><code class="prettyprint">Result[x, y] = Math.Between(0, 255, pixel);</code></p>
<p>I initially used the minimum and maximum functions:</p>
<p><code class="prettyprint">Result[x, y] = Math.Max(0, Math.Min(255, pixel));</code></p>
<p>This works, and is a pretty neat little piece of code. However, after profiling the application it quickly became obvious that it was slowing things down. A look at the compiled code shows that even when built for release, Min and Max are left as function calls instead of being inlined.</p>
<p>Looking into bitwise operations, we see that using a bitwise AND with 255 will trim the value to be between 0 and 255. Unfortunately it loops around, so that isn&#8217;t what we need:</p>
<pre>
  100101100   300
&#038; 011111111   255
= 000101100   044
</pre>
<p>The next option was to just check it manually:</p>
<pre><code class="prettyprint">if (pixel &gt; 255) {
	Result[x, y] = 255;
}
else if (pixel &lt; 0) {
	Result[x, y] = 0;
}
else {
	Result[x, y] = (byte)pixel;
}
</code></pre>
<p>Which is better, or:</p>
<pre><code class="prettyprint">Result[x, y] = (byte)(pixel > 255 ? 255 : (pixel < 0 ? 0 : pixel));</code></pre>
<p>Which is silly and harder to maintain.</p>
<p>Either way a new problem is that most of the pixel values are between 0 and 255, therefore two checks will usually be required. Thanks to a great suggestion by biznatchio, we can instead do it in one comparison.</p>
<p>We saw earlier that &#8220;value &#038;= 255&#8243; will trim the value to be between 0 and 255. So, if we just compare the value to the trimmed value we will know if it is in range:</p>
<pre><code class="prettyprint">if ((pixel &#038; 255) == pixel) {
	Result[x, y] = (byte)pixel;
}
else if (pixel &gt; 255) {
	Result[x, y] = 255;
}
else {
	Result[x, y] = 0;
}</code></pre>
<p>This will be slower then before if the value is &lt;0 or &gt;255. As our values will be in range most of the time, this method is the best.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jmsoftware.co.uk/blog/making-sure-a-number-is-between-0-and-255/feed</wfw:commentRss>
		</item>
		<item>
		<title>Welcome to JMSoftware.co.uk</title>
		<link>http://www.jmsoftware.co.uk/blog/welcome-to-jmsoftwarecouk</link>
		<comments>http://www.jmsoftware.co.uk/blog/welcome-to-jmsoftwarecouk#comments</comments>
		<pubDate>Thu, 05 Jul 2007 17:06:25 +0000</pubDate>
		<dc:creator>Jonathan</dc:creator>
		
		<category><![CDATA[blog]]></category>

		<category><![CDATA[cms]]></category>

		<category><![CDATA[General]]></category>

		<category><![CDATA[posts]]></category>

		<category><![CDATA[topic]]></category>

		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://www.jmsoftware.co.uk/blog/welcome-to-jmsoftwarecouk</guid>
		<description><![CDATA[I&#8217;ve set up this blog to allow me to publish articles, small programs, and general thoughts without cluttering up my main program&#8217;s blog with off-topic posts.
Wordpress makes a surprisingly good CMS, but it&#8217;ll take me a while before I get everything set up just how I want it so excuse the mess.
]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve set up this blog to allow me to publish articles, small programs, and general thoughts without cluttering up my <a href="http://ascgendotnet.jmsoftware.co.uk/">main program&#8217;s blog</a> with off-topic posts.</p>
<p>Wordpress makes a surprisingly good CMS, but it&#8217;ll take me a while before I get everything set up just how I want it so excuse the mess.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jmsoftware.co.uk/blog/welcome-to-jmsoftwarecouk/feed</wfw:commentRss>
		</item>
	</channel>
</rss>
