<?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"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Galfar's Lair &#187; Programming</title>
	<atom:link href="http://galfar.vevb.net/wp/category/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://galfar.vevb.net/wp</link>
	<description>Oh hai!</description>
	<lastBuildDate>Fri, 23 Apr 2010 15:52:37 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Ugly Images of Disabled Menu Items in Delphi</title>
		<link>http://galfar.vevb.net/wp/2010/04/ugly-images-of-disabled-items-in-delphi/</link>
		<comments>http://galfar.vevb.net/wp/2010/04/ugly-images-of-disabled-items-in-delphi/#comments</comments>
		<pubDate>Fri, 23 Apr 2010 15:44:14 +0000</pubDate>
		<dc:creator>Marek Mauder</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Pascal & Delphi]]></category>

		<guid isPermaLink="false">http://galfar.vevb.net/wp/?p=726</guid>
		<description><![CDATA[Ever used 32bit images stored in TImageList in your Delphi application? Toolbars and some other VCL controls have DisabledImages property which is automatically used to get images for disabled toolbar buttons. But what about menu components? They don&#8217;t have this property and drawing of disabled images is handled by TImageList with original enabled images (TMainMenu.Images [...]]]></description>
			<content:encoded><![CDATA[<p>Ever used 32bit images stored in TImageList in your Delphi application? Toolbars and some other VCL controls have DisabledImages property which is automatically used to get images for disabled toolbar buttons. But what about menu components? They don&#8217;t have this property and drawing of disabled images is handled by TImageList with original enabled images (TMainMenu.Images property).  And the results are really abysmal. How can this be fixed?</p>
<p><a href="http://galfar.vevb.net/wp/wp-content/uploads/2010/04/disabled-items-delphi.png" rel="lightbox[726]"><img class="aligncenter size-full wp-image-731" title="Disabled menu items" src="http://galfar.vevb.net/wp/wp-content/uploads/2010/04/disabled-items-delphi.png" alt="" width="153" height="190" /></a>One way is to override DoDraw method of TImageList and change the code that draws disabled images. You can do regular RGB to grayscale conversion here or let  Windows draw it for you in grayscale with nearly no work on your part. You can do this by using <a href="http://msdn.microsoft.com/en-us/library/bb761537(VS.85).aspx" target="_blank">ImageList_DrawIndirect</a> with ILS_SATURATE parameter. Note that this works only on Windows XP and newer and for 32bit images only. For older targets or color depths doing your own RGB-&gt;grayscale conversion is an option (good idea would probably be to cache converted grayscale images somewhere so they won&#8217;t need to be converted on every draw call).</p>
<p>Here&#8217;s the code of DoDraw method using  ILS_SATURATE:</p>
<pre class="brush: delphi;">
type
// Descendant of regular TImageList
TSIImageList = class(TImageList)
protected
  procedure DoDraw(Index: Integer; Canvas: TCanvas; X, Y: Integer;
    Style: Cardinal; Enabled: Boolean = True); override;
end;

procedure TSIImageList.DoDraw(Index: Integer; Canvas: TCanvas; X, Y: Integer;
  Style: Cardinal; Enabled: Boolean);
var
  Options: TImageListDrawParams;

  function GetRGBColor(Value: TColor): Cardinal;
  begin
    Result := ColorToRGB(Value);
    case Result of
      clNone: Result := CLR_NONE;
      clDefault: Result := CLR_DEFAULT;
    end;
  end;

begin
  if Enabled or (ColorDepth &lt;&gt; cd32Bit) then
    inherited
  else if HandleAllocated then
  begin
    FillChar(Options, SizeOf(Options), 0);
    Options.cbSize := SizeOf(Options);
    Options.himl := Self.Handle;
    Options.i := Index;
    Options.hdcDst := Canvas.Handle;
    Options.x := X;
    Options.y := Y;
    Options.cx := 0;
    Options.cy := 0;
    Options.xBitmap := 0;
    Options.yBitmap := 0;
    Options.rgbBk := GetRGBColor(BkColor);
    Options.rgbFg := GetRGBColor(BlendColor);
    Options.fStyle := Style;
    Options.fState := ILS_SATURATE; // Grayscale for 32bit images

    ImageList_DrawIndirect(@Options);
  end;
end;
</pre>
<p><strong>Important note: </strong>For ILS_SATURATE to work correctly source image files must be 32bit with proper alpha channel data, setting color depth of TImageList to 32bit is not enough! If you don&#8217;t see any images drawn this is probably the cause: 8/24bit image is loaded from file and then inserted into 32bit TImageList. As there is no alpha channel data in source image it is drawn as fully transparent so you don&#8217;t see anything.</p>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">http://msdn.microsoft.com/en-us/library/bb761537(VS.85).aspx</div>
]]></content:encoded>
			<wfw:commentRss>http://galfar.vevb.net/wp/2010/04/ugly-images-of-disabled-items-in-delphi/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Shift Right: Delphi vs C</title>
		<link>http://galfar.vevb.net/wp/2009/11/shift-right-delphi-vs-c/</link>
		<comments>http://galfar.vevb.net/wp/2009/11/shift-right-delphi-vs-c/#comments</comments>
		<pubDate>Wed, 04 Nov 2009 14:33:59 +0000</pubDate>
		<dc:creator>Marek Mauder</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Pascal & Delphi]]></category>

		<guid isPermaLink="false">http://galfar.vevb.net/wp/?p=605</guid>
		<description><![CDATA[Few weeks ago I converted a little function from C language to Delphi. I kept getting completely wrong results all the time even though I was sure the C to Pascal conversion was right (it was really just few lines). After some desperate time, I just tried replacing SHR operator by normal DIV (as A [...]]]></description>
			<content:encoded><![CDATA[<p>Few weeks ago I converted a little function from C language to Delphi. I kept getting completely wrong results all the time even though I was sure the C to Pascal conversion was right (it was really just few lines). After some desperate time, I just tried replacing <strong>SHR</strong> operator by normal <strong>DIV</strong> (as A SHR 1 = A DIV 2 and so on). To my surprise, I immediately got the right results. Can Delphi&#8217;s (I didn&#8217;t test it in Free Pascal) <strong>SHR</strong> operator behave differently than C&#8217;s <strong>&gt;&gt;</strong> operator?</p>
<p>It does in fact. SHR treats its first operand as unsigned value even though it is a variable of signed type whereas &gt;&gt; takes the sign bit into account. In the function I converted the operand for right shift was often negative and Delphi&#8217;s SHR just ignored the value of the sign bit.</p>
<h3>A Bit of Code</h3>
<pre class="brush: cpp;">
int a, b1, b2;
a = -512;
b1 = a &gt;&gt; 1;
b2 = a / 2;
</pre>
<p>After running this C code both <strong>b1</strong> and <strong>b2</strong> have a value of -256.</p>
<pre class="brush: delphi;">
var A, B1, B2: Integer;
A := -512;
B1 := A shr 1;
B2 := A div 2;
</pre>
<p>This Delphi code however yields different result: <strong>B2</strong> is -256 as expected but <strong>B1</strong> has a value of 2147483392.</p>
<h3>A Bit of Assembler</h3>
<p>Assembler output of C code:</p>
<pre class="brush: plain;">
Unit1.cpp.22: b1 = a &gt;&gt; 1;
mov eax,[ebp-$0c]
sar eax,1
mov [ebp-$10],eax
Unit1.cpp.23: b2 = a / 2;
mov edx,[ebp-$0c]
sar edx,1
jns $00401bb9
adc edx,$00
mov [ebp-$14],edx
</pre>
<p>Assembler output of Delphi code:</p>
<pre class="brush: plain;">
Unit1.pas.371: B1 := A shr 1;
mov eax,[ebp-$0c]
shr eax,1
mov [ebp-$1c],eax
Unit1.pas.373: B2 := A div 2;
mov eax,[ebp-$0c]
sar eax,1
jns $00565315
adc eax,$00
mov [ebp-$20],eax
</pre>
<p>As you can see, asm output of C and Delphi divisions is identical. What differs is asm for shift right operator. Delphi uses <strong>shr</strong> instruction whereas C uses <strong>sar</strong> instruction. The difference: shr does logical shift and sar does arithmetic one.</p>
<blockquote><p>The SHR instruction clears the most significant bit (see Figure 6-7 in the Intel Architecture Software Developer&#8217;s Manual, Volume 1); the SAR instruction sets or clears the most significant bit to correspond to the sign (most significant bit) of the original value in the destination operand.</p></blockquote>
<p>Quoted from: <a href="http://faydoc.tripod.com/cpu/shr.htm">http://faydoc.tripod.com/cpu/shr.htm</a></p>
]]></content:encoded>
			<wfw:commentRss>http://galfar.vevb.net/wp/2009/11/shift-right-delphi-vs-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Working in MODULA-2</title>
		<link>http://galfar.vevb.net/wp/2009/04/working-in-modula-2/</link>
		<comments>http://galfar.vevb.net/wp/2009/04/working-in-modula-2/#comments</comments>
		<pubDate>Fri, 10 Apr 2009 10:36:37 +0000</pubDate>
		<dc:creator>Marek Mauder</dc:creator>
				<category><![CDATA[General News]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://galfar.vevb.net/wp/2009/04/working-in-modula-2/</guid>
		<description><![CDATA[I’ve been working in Modula-2 for last few weeks. It’s a considered a dead language now, one of Pascal’s descendants designed by Niclaus Wirth. I’ve been doing some updates to quite an old project, embedded system for military usage.&#160; I had to run Mac OS 7.5 emulator to get original 1989 compiler running. Modula-2 dialect [...]]]></description>
			<content:encoded><![CDATA[<p>I’ve been working in Modula-2 for last few weeks. It’s a considered a dead language now, one of Pascal’s descendants designed by Niclaus Wirth. I’ve been doing some updates to quite an old project, embedded system for military usage.&#160; I had to run Mac OS 7.5 emulator to get original 1989 compiler running. Modula-2 dialect understood by this compiler is quite strict – you cannot even mix cardinal and integer numbers without explicitly converting one of them. </p>
<p>First difference you may notice (compared to Pascal) is case sensitiveness of Modula-2, for instance all reserved words are upper cased. You don’t have to write so many begin-end statements though. Another C-like trait is importance of header files (definition modules) order during compiling – just change it a little bit in a large project and whole thing breaks down. </p>
<p align="left">More info about Modula-2 if you’re interested: <a href="http://www.modula2.org/">http://www.modula2.org/</a>.</p>
<p align="left"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; margin-left: 0px; border-left-width: 0px; margin-right: 0px" title="Modula-2" border="0" alt="Modula-2" src="http://galfar.vevb.net/wp/wp-content/uploads/2009/04/modula2.png" width="448" height="480" /></p>
]]></content:encoded>
			<wfw:commentRss>http://galfar.vevb.net/wp/2009/04/working-in-modula-2/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>
