Archive for October, 2010

Being Resourceful with Delphi 7

Wednesday, October 27th, 2010

With TImageLists and other  components that allow you to embed rich media in your applications, developers don’t often need to use resource-definition script (.rc) files and compile them into resource (.res) files.  I recently came across the need to do so for 24 bit PNG images, so I thought I would author an article in case I need to remember this in the future.  Hopefully someone else will also find it useful.  There are good resources out there, such as the articles on delphi.about.com, but I found conflicting information on how best to include them.

For instance, on StackOverFlow the question came up in June 2009 with a reply to include the following in your project:

{$R 'Splash.res' 'Splash.rc'}
Above directive is what I use to include an image for a splash screen. It will automatically compile the .RC file. As an option, you can just include the .RC to your Delphi project, in which case the above line will be added to your project file (*.DPR) and it will also automatically compile. (And you can use Delphi to edit the .RC file.)

What I found out by experimentation is that Delphi 7 doesn’t actually compile the RC file into a RES file as indicated above, even when you build the project.  It will include the RES file, but you still have to manually compile the RC file into a RES file using BRCC32 just as delphi.about.com indicates.  You can associate BRCC32 with an RC file so double clicking it will compile it, but Delphi 7 will not automatically invoke BRCC32 as part of the compile process, so if your RC file changes you have to remember to update the RES file.

You can in fact add the RC files to your Delphi project in the Project Manager.  The advantage to adding them to the project is that you can edit the file as a Text File in the Delphi IDE, or invoke the String Table editor by Rt. Clicking on the file in the Project Manager.  It does not change the fact that RES files are not generated by a Build.

Sometimes I really wish Borland would have logged the output from the compiler and the rest of the toolchain so you could tell what was going on in these earlier Delphi versions.  Of course a real resource editor would have been a nice feature as well!

EMB Needs more Insight

Friday, October 15th, 2010

Code Insight that is.

First, a disclaimer.  I am using Delphi 7 on my current project and have not yet tested this with Delphi XE or anything newer than D7.

Let’s say you have a descendant class and you want to implement a method override.  First you have to remember what the scope of the method is in the ancestor class, and then you have to type the prototype into the appropriate declaration section, and use Code Completion to get the implementation (or you can do the reverse).  In any event, you have to know the method prototype.

For instance, if you have the following class declaration and attempt to use CodeInsight to complete the prototype you’re SOL in D7 at least:

type
  TftInvoiceItem = class(ftInvoiceDesign.TftInvoiceItem)
  public
    function IsValid(XXX) :boolean; override;
  end;

Pressing Ctrl+Space after typing “func” provides no methods that can be overridden, and still nothing within the brackets containing XXX.  You are forced to navigate to the ancestor class and copy the method declaration so you can paste it in a descendant.

This wouldn’t be a big deal except that unlike C# Delphi requires forward declarations for the compiler.  That’s the trade off made for a fast compiler, and IMO a good one.  Managing method prototypes can be a pain though, because everywhere you want to add or change one, it has to be done in two places.  You would think the IDE would help by providing refactoring tools to do so, yet it is sadly lacking.  They pale in comparison to Resharper and CodeRush for Visual Studio.  Why?  I think it’s because Microsoft provides the Code DOM where EMB forces everyone to implement their own parser which means a slower IDE because you have 2 parsers at work, and less accuracy as the language evolves.

If EMB is developers developing tools for developers, then why doesn’t the Open Tools API expose the parser’s data or EMB at least provide better refactoring capabilities?  After all, we could still use Notepad if we didn’t need such functionality that an IDE should provide.  I’m sure the makers of Castalia, and ModelMaker would appreciate this, and every developer would benefit from having the fastest way to develop Windows applications, as opposed to the fastest way to develop Native ones.

Possible Refactoring Improvements

Thursday, October 14th, 2010

Have you ever wanted to assign a method to an event property in code when you haven’t written the event handler yet?  Code Insight can provide the method prototype for the event, but you have to manually type in the declaration for the new method you want to assign.  Clear as mud?  Perhaps an example to illustate the situation will make it clear:

Let’s say I  want to assign an event handler to a TcxSpinEdit control’s OnValidate event.  I have the following in code:

TcxSpinEdit(Control).Properties.OnValidate

and I press Ctrl+Space for Code Insight.  It shows the method prototype below.

CodeInsight

CodeInsight Shows the Prototype

and I assign it the name of a method I’m about to declare:

TcxSpinEdit(Control).Properties.OnValidate := ValidatePercentage;

Now I don’t work with Developer Express controls often enough to have memorized all their method prototypes so I have to remember the prototype CodeInsight provided so I can key it, and then use Ctrl+Shift+C to declare it (or in my case CodeRush’s Ctrl+M which also provides the ability to scope the declaration - something that Code Completion should have had eons ago).  Wouldn’t it be nice to be able to Rt. Click on the Event property (OnValidate in this case) and choose “Declare Handler” or something to that effect that would do it all for you?