An introduction to using Kylix

The aim of this tutorial is purely to
introduce you to Kylix. In most ways, Kylix is pretty much identical
to Delphi, so you'd think that there wouldn't be much to say. You'd be
right, but there are still some things to take into consideration that
you may not be aware of, and this tutorial goes over them. Where
possible this site has now been made compatible with all things Kylix.
In practice this means that all the non-DelphiX tutorials and their
source code, will work with both Delphi and Kylix.

So, in terms of the actual interface,
you'll see very little differences from Delphi. Everything is where it
should be, the only thing worth mentioning here is that when you make
alterations to Properties in the Object Inspector you should get into
the practice of pressing Return after you make each alteration as
Kylix will forget anything you've entered unless you do so. You can
also just select another entry instead which will have the same
effect, but if say, you just go to the Object Inspector to change the
Name property, you need to remember to press return before you get
back to altering code, or whatever. It's quite possible that many of
you do this already so this won't be a problem for you, but if you're
like me - take note ;-)

Kylix running in the GNOME interface in Mandrake 8.0

So far so good, but when it comes to
reusing your old Delphi code, things get a little more complicated. As
long as your actual code doesn't use any instructions specific to the
Windows environment, it'll work fine. In most cases, you'll be fine
then. But, before you actually can get your existing code to work you
do need to make alterations to the uses section of your code in
both your unit's source and your project's source. Let's go through
these changes now then.

The 17th letter of the alphabet
Unfortunately, in Kylix most of the entries in your uses
section will be useless. Let's take a look at the code generated by
Delphi and Kylix when you first start a new application:

The first two entries of the Delphi Code - Windows and Messages,
are specific to Windows. SysUtils on the other hand will work for both
Windows and Linux, as will Classes. It should be clear to you the
similarity of the rest to each other. Simply adding a Q at the
beginning of an entry here, does the trick. The same applies for other
such entries that exist once you've added components. Menus in Delphi
is QMenus in Kylix, and Buttons in Delphi is QButtons in Delphi, for
example.

Due to these restrictions, you have the option of just making your
Delphi application Kylix-compatible by changing the relevant entries
so that they now work in just Kylix, or we can go for the better
option of making our code work in both. We can do this with the
following code:

This takes the most commonly used
entries and allows your game to check at compile time whether you're
using Windows or Linux, choosing the relevant entries. You may need to
alter this list depending on what you're doing. If you're lucky enough
to have Delphi 6 Professional or Enterprise then the above will not be
needed at all because any new code made with either of these two
products, includes such information.

We still have to modify our Project source now. To view the source
of your project you need to go to 'Project' on the menu and select
'View Source'. This brings up the source for our project where we can
edit our uses section. You need to modify the following code:

uses
Forms,

So that it is:

uses{$IFDEF WIn32}
Forms,{$ENDIF}

{$IFDEF Linux}
QForms,{$ENDIF}

This has a similar effect to that
which we've done already but you should make sure you don't modify the
line that follows this code, as it tells your project where to find
your unit source.

Case Sensitivity
The next thing we need to bare in mind before we can run our
program effectively in both Delphi and Kylix is that Linux is case
sensitive when it comes to file names. This means that Unit1.pas is
different from unit1.pas so you need to make sure that at all times
you're linking to the correct file. What this means for your existing
code is that we need to modify one line in our unit source and one
line in our project source.

As we're already viewing our project source, we may as well start
there. You'll notice the following line before the begin line:

{$R *.RES}

Notice that the .RES bit is in capital letters. The problem here is
that the actual res file that your project will create (in both Delphi
and Kylix) will have a lower case extension. This doesn't matter in
the land of windows where case sensitivity isn't an issue, but with
Linux it needs to know that it is lower case, so change it to:

{$R *.res}

This will still work for Windows, so
you can keep both operating systems happy. You may find that when you
change this line to lower-case that it brings up a warning, saying
that the .res file has been recreated. Just click OK and continue on
and everything will be fine! Anyway, if you now go back to your unit
source, you need to change a similar line which looks like the following:

{$R *.DFM}

You'll find this line just after the implementation
line in your program listing and as before, you should change it to
the lower case - .dfm so that Kylix recognises it.

And finally...
That's everything you need to know to at least start making
your code Kylix-compatible. More complicated programs may need
additional alterations, but they should on the whole be pretty
straight forward.

Before I leave you to it though,
here's a couple of things to bear in mind when coding. You may have
heard about how Linux hardly ever crashes and it's as solid as a rock.
For the most part this is true and you'll probably find that Linux
itself is never a problem. Kylix though will most probably crash more
often than not. There's a number of reasons for this, but the primary
reason is that it does make partial use of the WINE software which
allows windows applications to run in Linux. As Kylix hasn't been made
from the ground up in Linux, this causes some instability. You're
probably pretty used to this anyway if you've been using Delphi for
any length of time. At least when Kylix crashes it tends to come up
with a warning message, suggesting you restart it - when did you ever
see Delphi admit defeat like that? ;-) So basically, save often to
make sure you don't lose your work!

Another point to bear in mind is that
Kylix appears to be a bit more sensitive to sloppy programming. If
you're getting funny errors in your programs make sure that you've put
lines in exactly the way the online help suggests. A case in point
here is the Randomize; line. When I originally wrote the
tutorials for this site which made use of this function, I made the
mistake of declaring this line more than once in my program. In fact,
every OnTimer event called it again. This doesn't seem to matter in
Delphi, but if you try it in Kylix, the randomisation tends to be very
slow. A look at the online help for both Delphi and Kylix tells us to
only declare this once in our programs, so simply moving this line to
the FormCreate procedure sorts this out. I'd bet that there are other
such functions that will make a fuss in Kylix, that didn't in Delphi,
so watch out ;-)

Okay, so get coding in Linux. If you
get stuck let me know through either e-mail or the message board, and
if you think I should mention something else that you've come across,
let me know. Incidentally, if you're looking for a good reliable Linux
distribution, I'd really recommend Mandrake.
It's what I use, and it's very up to date and has a lot of support.
It's up to you though! Bye for now.