Blaise 9UK PDF
Blaise 9UK PDF
Blaise 9UK PDF
Pascal
LEDS
LEDS
LEDS
LEDS
LEDS
LEDS
December 2009
Publisher: Foundation for Supporting the Pascal Programming Language
in collaboration with the Dutch Pascal User Group (Pascal Gebruikers Groep)
Stichting Ondersteuning Programmeertaal Pascal
Cover price Europe: 10.00 / UK 10.00 / US $ 10.00
CONTENTS
Articles
The 3D spheres generator
David Dirkse page 5
Christmas is a comming...
What is kbmMW?
By Benno Evers page 7
An explanation how to install
Delphi 2010 Feature Highlight - Debugger
Visualizers
Jeremy North page 12
A fantastic alternative for its expensive competitors, and its even
cheaper
Introduction to multithreading
Primo Gabrijeli page 18
Explains a lot
Writing Delphi Components III:
Compound Components and Custom Event
Marco Cant page 21
In the new Delphi versions it looks all different.
Talking Delphi
Henk Schreij page 25
Delphi talking back to you...
LCD Interfacing: Driving a
HD44780 LCD in Delphi
Thiago Batista Limeira page 28
Get text on an lcd panel
Exploring the inplace editing
capabilities of TAdvStringGrid
By Bruno Fierens page 34
Beautiful features
Columns
Foreword, page 4
Readers write... page 4
BLAISE PASCAL TROPHY: the winners page
10
Advertisers
Advantage Database Server page 3
Barnsten page 27
Components for Developers page 40
Datanamic page 6
Delphi Special classic editions upgrade pricing page 39
Fastreport for VCL page 17
Fastreport for .Net page 20
RT science page 24
Editor in chief
Detlef D. Overbeek, Netherlands
Tel.: +31 (0)30 68.76.981 / Mobile: +31 (0)6 21.23.62.68
News and Press Releases
email only to [email protected]
Authors
B Peter Bijlsma,
C Marco Cant,
D David Dirkse, Frans Doove,
G Primo Gabrijel! i! ,
N Jeremy North,
O Tim Opsteeg,
P Herman Peeren,
S Henk Schreij, Rik Smit, Bob Swart,
V Hallvard VassBotn.
Editors
Rob van den Bogert, W. (Wim) van Ingen Schenau,
M.J. (Marco) Roessen.
Corrector
A.W. (Bert) Jonker, M. L. E. J.M. (Miguel) van de Laar
Translations
M. L. E. J.M. (Miguel) van de Laar,
Kenneth Cox (Official Translator)
Copyright See the notice at the bottom of this page.
Trademarks All trademarks used are acknowledged as
the property of their respective owners.
Caveat Whilst we endeavour to ensure that what is
published in the magazine is correct, we cannot accept
responsibility for any errors or omissions. If you notice
something which may be incorrect, please contact the Editor
and we will publish a correction where relevant.
Subscriptions
Copyright notice
All material published in Blaise Pascal is copyright SOPP Stichting Ondersteuning Programeertaal Pascal unless otherwise noted and may not be copied,
distributed or republished without written permission. Authors agree that code associated with their articles will be made available to subscribers after publication by
placing it on the website of the PGG for download, and that articles and code will be placed on distributable data storage media. Use of program listings by subscribers
for research and study purposes is allowed, but not for commercial purposes. Commercial use of program listings and code is prohibited without the written
permission of the author.
Page 2 / 2156
COMPONENTS
DEVELOPERS
Foreword
It is already a white Christmas over here.
Lots of snow and children's enthusiastic voices.
Love that.
We have seen another year of Embarcadero
working hard to fulfill its roadmap. Yet there is no
version of Delphi for the beginners.
So because of that, we have made the choice to
bring out a special CD for Lazarus and to support
it (Windows, Linux (ready to launch) and Mac, so
beginners can start at low costs. Demo programs
etc. are on the CD's.
A little Christmas present from us:
To have fun at the web without problems,
we have created a special Internet CD.
This CD starts up without needing your Operating
System (there is a small version of Linux on it),
only the hardware. You can give that CD to any
person, young or old and even children, because
they can do nothing wrong. It can't store any data
because of it being on a CD. Useful for People
who are scared by using your system, or not
experienced with Internet.
Your PC or notebook is not harmed in any way,
no viruses or other unwanted nonsense.
Yet, if you want to store any data from the website
you still can do that to your USBstick.
To get the CD Free Internet, all you have to do
is download the ISO and burn it, and if you want
it in a beautiful case - to have a nice present - you
can order it at our website.
I hope you will enjoy this. Let us hear about your
experiences.
We have some news for the next year:
we will publish a special issue about Databases
and database programming.
Readers write...
Hi,
Just wanted to comment on your editor input as well as Jeremy
North's article "Using Free Pascal and Lazarus to create
application for OSX" in the latest issue of Blaise. Very much
appreciate Mr. North's helpful overview and I hope to see more of
these types of articles in Blaise!
Yes, Mac OSX does many, many things better than windows.
However:
After having made several sample applications using apple's
Xcode and interface builder I certainly share Mr. North's
implicitly stated frustration with these tools. Any Delphi
developer who attempts to play around with xcode will be baffled
by the almost ridicules manner in which things are done.
Producing a "hello world" will take at least twice as many mouse
clicks through several different dialogs and applications. What
xcode does expect from its users is that they are very conscious
about what they are doing, but its far from RAD as we have
become accustomed to in Delphi.
Page 4 / 2158
COMPONENTS
DEVELOPERS
expert
Introduction
Figure 1:
The switch "true" indicates that the red and blue color fields
have to be traded, due to the pf32 bit format.
The scanline[..] property is used twice to get the pointer to
pixel[0,0] and to get the increment value to the pixel below
in the bitmap. Avoiding the scanline and pixel[...] statements
accellerates painting considerably. Pixels are written directly
in memory.
Figure 2:
Drawing the sphere
This is done in bitmap Smap. (S - Sphere). All pixels of the
bitmap are addressed left to right, top to bottom. For each
pixel (x,y) a check is made to find it's position: inside or
outside the circle. See fig.3
Figure 4:
The slides
The slides are implemented by paintboxes in which the
arrows are painted. Xbox controls the horizontal focus
position, Ybox the vertical position. CCbox controls the
center-intensity, BCbox the border intensity.
Figure 3:
COMPONENTS
DEVELOPERS
Page 5 / 2159
Page 6 / 2160
COMPONENTS
DEVELOPERS
expert
urematrix/index.htm
MT
BDE
ADOX
IBX5
DBX
kbmMemTable (Components4Developers)
Borland Database Engine (CodeGear)
ADO Express (CodeGear)
IB Express v5 (CodeGear)
DB Express (CodeGear)
Transport support:
Local transport
Indy 10 TCP/IP Request/Response transport
ISAPI transport
Depending on the chosen version of kbmMW CG edition,
IDE support is installed for Delphi2007 or Delphi2009.
Installing kbmMW CodeGear edition
After downloading the CodeGear edition for your specific
IDE, unzip it. Inside this zip file there is an installer
<kbmMW_CG_Setup> that automates most of the tasks.
Just follow the steps of the setup wizard. By default the
software is installed in c:\Program Files\kbmMWCG
When prompted to select the components, don't change
anything because all components are needed.
COMPONENTS
DEVELOPERS
Page 7 / 2161
kbmMemDes<delphi
kbmMemRun<delphi
kbmMWDes<delphi
kbmMWRun<delphi
Files\kbmMWCG
to
version>CG.bpl
version>CG.bpl
version>CG.bpl
version>CG.bpl
Figur 2:
When clicking the button right from the editbox a dialog is
opened showing the BPL path
Page 8 / 2162
COMPONENTS
DEVELOPERS
http://www.components4programmers.com/products/kbmmw/featurematrix/index.htm
http://www.components4programmers.com/products/kbmmw/university/index.htm
http://www.components4programmers.com/downloads/kbmmw/documentation/Creation_of_customized_services.pdf
http://www.components4programmers.com/downloads/kbmmw/documentation/Using_kbmMW_as_a_query_server.pdf
http://www.components4programmers.com/downloads/kbmmw/documentation/Resolving_250.pdf
COMPONENTS
DEVELOPERS
Page 9 / 2163
The winners
COMPONENTS
DEVELOPERS
NEW!
Just published!
Specially designed
for self-study
Consisting of:
* translation of Marco Cant's book
Essential Pascal
* Lazarus for Windows on CD-ROM
* Delphi RAD Studio on CD-ROM (30-day trial
version)
* Sample projects
* Alphabetical dividers for notes
* Empty sample project folders for ring binder
Follow-up subscriptions available at 25 per year
(plus shipping fee)
Consisting of:
1. Articles on electronic applications for Delphi
2. Articles on the Lazarus IDE
3. Articles on the Delphi 2010 IDE
4. Exercises and code examples
Full subscription price in combination with
purchase of book with course and DVDs: 55.00
Orders may be placed with
our website store at
http://www.blaisepascal.eu/index.php?actie=./subscribers/subscription_mainpage or
COMPONENTS
DEVELOPERS
Page 11 / 2165
expert
By Jeremy North
COMPONENTS
DEVELOPERS
COMPONENTS
DEVELOPERS
Page 13 / 2167
IOTADebuggerVisualizerValueReplacer =
interface(IOTADebuggerVisualizer)
['{6BBFB765-E76F-449D-B059-A794FA06F917}']
function GetReplacementValue(const Expression, TypeName,
EvalResult: string): string;
end;
COMPONENTS
DEVELOPERS
Value-Replacer
To create a Value-Replacer visualizer, implement the
IOTADebuggerVisualizerValueReplacer interface.
http://www.jed-software.com
Some are free, while others incur a small charge. So
lets go visit him at his site!
COMPONENTS
DEVELOPERS
Page 15 / 2169
Figure 6: To install the visualizer, right click on the package name in the project manager and select the Install command.
Double clicking on the frame copies the Evaluated Result to
the clipboard, and double clicking on the panel copies the
Expression name to the clipboard.
The screen shot below shows the frame at design time.
COMPONENTS
DEVELOPERS
The screen shot below shows the test projects user interface.
The TColor property being changed is the forms Color
property. This means that the background color of the form
will change when clicking on one of the provided buttons.
Figure 10: Also note in the screen shot above where the
Value-Replacer part of the visualizer is also shown.
If you select a custom color after selecting the Select a
color... button in the test project the hex format of the
expression is displayed (as shown below).
Figure 11:
The hex format
Future Enhancements
* Allow custom painting added to the Value-Replacer
visualizer type. This should then allow a Value-Replacer
version of the TColor visualizer to just paint the colour
next to the formatted TColor value, instead of requiring a
popup window to show this information.
* It would be nice to be able to include a screen capture
associated with a visualizer that was displayed in the
registered visualizers list in the options dialog.
* Project specific enabling and disabling of visualizers.
Conclusion
I have no doubt Debugger Visualizers will improve your
debugging efficiency. This will certainly be the case when
you need to drill into and call methods on the expression
being visualized.
The source code and test project is available to subscribers.
Remember to catch the follow up article on how to call
methods on expressions in the next edition. There will also
be details of how to use the IDE wizard I'm creating to
easily create the base code for your own visualizers.
Page 17 / 2171
Page 5 / 2159
Introduction to multithreading
starter
expert
Page 18 / 2172
By Primo Gabrijeli
COMPONENTS
DEVELOPERS
Page 19 / 2173
Introduction to multithreading
(continuation 1)
Then there's a problem of data sharing. When threads share
data, we must be very careful to keep this data in a
consistent state.
For example, if two threads are updating shared data, it may
end in a mixed state where half the data was written by the
first thread and another half by the second.
This problem, race condition as it's called, is usually solved
by some kind of synchronization.
We use some kind of locking (critical sections, mutexes,
spinlocks, semaphores) to make sure that only one thread at
a time can update the data.
However, that brings us another problem or two.
Firstly, synchronization makes the code slower.
If two threads try to enter such locked code, only one will
succeed and another will be temporarily suspended and our
clever, multithreaded program will again use only one CPU
core.
Secondly, synchronization can cause
deadlocks
This is a state where two (or more) threads forever wait on
each other.
For example, thread A is waiting on a resource locked by
thread B and thread B is waiting on a resource locked by
thread A.
Not good. Deadlocks can be very tricky; easy to introduce
into the code and hard to find.
There's a way around synchronization problems too.
You can avoid data sharing and use messaging systems to
pass data around or you can use well-tested lock-free
structures for data sharing.
That doesn't solve the problem of livelocks though.
In livelock state, two (or more) threads are waiting on some
resource that will never be freed because the other thread is
using it, but they do that dynamically - they're not waiting
for some synchronization object to become released.
The code is executing and threads are alive, they can just
not enter a state where all conditions will be satisfied at
once.
The most famous (theoretical) example of resource
protection is Dijkstra's Dining philosophers problem,
well described at
en.wikipedia.org/wiki/Dining_philosophers.
Until next time it is your task to read about it and at least
pretend to understand
, so that we can continue our
journey.
Primoz Gabrijelcic
Page 20 / 2174
COMPONENTS
DEVELOPERS
expert
The SetSemState method is more complex than most propertysetting methods, in that it calls other private methods of this
class (TurnOff, StartPulse, and StopPulse). In fact, besides
assigning the new Value for the property, we need to start or
stop the Timer (if the SemState property changes to or from
scPulse), and change the status of the three embedded Led
components.
procedure TCntSemaphore.SetSemState (Value: TSemState);
begin
if Value <> fSemState then
begin
TurnOff;
if fSemState = scPulse then
StopPulse;
case Value of
scRed: fRedL.Status := lsOn;
scGreen: fGreenL.Status := lsOn;
scYellow: fYellowL.Status := lsOn;
scPulse: StartPulse;
// scOff: nothing to do
end;
fSemState := Value;
end;
end;
COMPONENTS
DEVELOPERS
Page 21 / 2175
COMPONENTS
DEVELOPERS
well need to set the internal field, and then copy the value
to the embedded component:
procedure TCntSemaphore.SetInterval
(Value: Integer);
begin
if Value <> fInterval then
begin
fInterval := Value;
if Assigned (fTimer) then
fTimer.Interval := fInterval;
end;
end;
type
TCntSemaphore = class (TCustomControl)
...
private
fGreenClick, fRedClick, fYellowClick:
TLightClickEvent;
// LED click response methods
procedure GreenLedClick (Sender: TObject);
procedure RedLedClick (Sender: TObject);
procedure YellowLedClick (Sender: TObject);
published
// custom events
property GreenClick: TLightClickEvent
read fGreenClick write fGreenClick;
property RedClick: TLightClickEvent
read fRedClick write fRedClick;
property YellowClick: TLightClickEvent
read fYellowClick write fYellowClick;
Page 23 / 2177
Page 24 / 2178
COMPONENTS
DEVELOPERS
expert
COMPONENTS
DEVELOPERS
Page 25 / 2179
Delphi <spell>Delphi</spell>.
Page 26 / 2180
COMPONENTS
DEVELOPERS
Here the computer first speaks the word 'Delphi' and then
spells it letter by letter.
There are also tags that can be used either singly or in pairs,
such as the 'Volume' tag. The parameter value for normal
volume is level = "100". In the first of the following
examples the word 'fox' is spoken softly, while in the
second example all of the text after 'fox' is spoken softly:
This way you will hear 'Delphi' with the authentic Greek
pronunciation, instead of the frightful 'Delfaai' of American
English. You can even use this to mimic a different
language. For example, you can use the following
pronunciations to count from one to four in Dutch:
<pron sym ="ey n"/>.
<pron sym ="t w ey"/>.
<pron sym ="d r iy"/>. <pron sym ="v iy r"/>.
sym
sym
sym
sym
aa
ae
ah
ao
aw
ax
ay
b
ch
d
father
cat
cut
dog
foul
ago
bite
big
chin
dig
dh
eh
er
ey
f
g
h
ih
iy
jh
then
pet
fur
ate
fork
gut
help
fill
feel
joy
k
l
m
n
ng
ow
oy
p
r
s
cut
lid
mat
no
sing
go
toy
put
red
sit
sh
t
th
uh
uw
v
w
y
z
zh
she
talk
thin
book
too
vat
with
yard
zap
pleasure
You can find the full list of letters in the tree structure on
the MSDN site in the 'American English Phenomea
Table' under the 'Miscellanea' heading:
Buy
Delphi
now
barnsten
COMPONENTS
DEVELOPERS
Page 27 / 2181
expert
COMPONENTS
DEVELOPERS
COMPONENTS
DEVELOPERS
Page 29 / 2183
const
// Command:
LCD_CLEAR
LCD_HOME
= $01;
= $02;
Description:
// Clear Display
// Put cursor in the first line
// and first column
Binary Value:
00000001
00000010
LCD_ONOFF
LCD_ONOFF_CP
LCD_ONOFF_C
LCD_ONOFF_D
=
=
=
=
// Display Control:
// Blink Cursor or not
// Cursor On or not
// Display On or not
00001000
00000001
00000010
00000100
LCD_SHIFT
LCD_SHIFT_D
LCD_SHIFT_M
= $10;
= $04;
= $08;
// Cursor Shift:
// Shift right or left
// Shift message
00010000
00000100
00001000
LCD_FUNCTION
LCD_FUNCTION_M
LCD_FUNCTION_L
LCD_FUNCTION_B
=
=
=
=
// Operation Mode:
// Use 5x10 or 5x7 Matrix
// 2 or more lines or 1 line
// 8 bits or 4 bits
00100000
00000100
00001000
00010000
LCD_DDRAM
= $80;
// DDRAM Address
10000000
$08;
$01;
$02;
$04;
$20;
$04;
$08;
$10;
COMPONENTS
DEVELOPERS
Note
The Parallel Port Control register (base address + 2) was
intended as a write only port. When a printer is attached to
the Parallel Port, four "controls" are used. These are
Strobe, Auto Linefeed, Initialize and Select Printer, all of
which are inverted except Initialize.
Sending data to the LCD
To send data to the LCD, we need to put the Register Select
pin low to select the LCD's data register. Below, we have
the needed steps:
7. Select Data register (Register Select = 1) and
Enable High
(Enable = 0, inverted hardware);
8. Wait for LCD to process;
9. Send data (write operation);
10. Wait for LCD to process;
11. Send Enable Low
(Enable = 1, inverted hardware);
12. Wait for LCD to process;
With this steps we can implement our SendData method, see
Listing 6.
procedure THD44780.SendData(Data : Byte);
{Register Select High and Enable High}
begin
WritePort($37A,4); // sends 00000100 to the control register
sleep(1); // 1 millisecond
WritePort($378,Data); // sends data to the data register
sleep(1); // 1 millisecond
{Register Select High and Enable Low}
WritePort($37A,5); // sends 00000101 to the control register
sleep(1); // 1 millisecond
end;
Listing 10. Methode voor het verschuiven van de cursor en/of tekst.
Note
that in order to send the Text parameter to the LCD we use
one for looping to convert each letter to its ASCII value
using the Ord function. Every time the LCD receives a
character, it writes it and moves 1 position to the right, the
cursor marks the place where the next character is going to
occupy.
Back to the application example
With the finished class we can go back to the example
application construction, now declare the uHD44780 in the
interface section. Do not forget to declare a THD44780
variable, just like below:
LCD : THD44780;
With this variable we can access all the functionality we
have already implemented in the THD44780 class, and we
will be able to interface the display without any concerns.
Now double click the frmMain and in its OnCreate event
type the code below:
LPTPort.Port[$378];
LCD := THD44780.Create(LPTPort);
COMPONENTS
DEVELOPERS
Page 31 / 2185
Figure 5. Suggested
GUI for the
application example
Figure 4: Display after the initialization.
Page 32 / 2186
COMPONENTS
DEVELOPERS
Conclusion
With our THD44780 LCD control class, interfacing
displays becomes very easy, which is a great alternative to
hobbyists and casemodders, besides recycling old printer
cables we can also give our computers a new design using
our favorite programming language. See you next time and
stay tuned to the Blaise Pascal Magazine.
Links
LCD Interfacing
http://www.8051projects.net/lcd-interfacing/
HD44780
http://pic.rocklizard.org/ReferenceDocs/HD44780HowTo.pdf
BLAISE PASCAL
ARCHIVE 2009
* 2 years complete issues on DVD
* More than 360 pages (in PDF format)
* Very fast search with multiple terms
* All issues indexed
* Find the article you want with just one click
* Full code for all articles included
Full version of Lazarus for Windows on DVD
Trial version of Delphi RAD Studio on DVD
All 35 Code Rage video tutorials on DVD - more than 3
three full days of self study material on
Delphi features and functions
Orders may be placed with
our website store at
http://www.blaisepascal.eu/index.php?actie=./subscribers/subscription_mainpage or
[email protected]
Price 20,00 including postage
COMPONENTS
DEVELOPERS
Page 33 / 2187
expert
This enables editing for all cells in the grid except fixed
cells. In many cases it is desirable that editing is enabled
only in some specific cells or columns.
A cell can be set as readonly by using:
grid.ReadOnly[column,row]: boolean
Typically, the next sequential cell is the cell right from the
current cell, ie. editing sequence is from left to right.
In some cases, it can be required that the editing sequence
is from top cell to bottom cell. The direction can be choosen
with the property grid.Navigation.AdvanceDirection.
Similary, it can be convenient that the inplace editor is also
immediately shown when the user moves to a next cell with
the arrow keys. To have this behaviour, set
grid.Navigation.AlwaysEdit to true. When you want to allow
that the user uses the TAB key to move to the next cell, set
goTabs to true in grid.Options. Normally, the TAB key
moves focus between controls. With goTabs set to true,
TAB key moves focus in cells of the grid and by default,
when the last cell (bottom right cell) is reached, the TAB
key moves focus back to the first cell. If you want that the
focus moves to the next control on the form when TAB is
pressed on the last cell, set
grid.Navigation.TabToNextAtEnd = true.
grid.MouseActions.DirectEdit: boolean;
grid.Navigation.AllowCtrlEnter = false
grid.MouseActions.EditOnDblClickOnly: boolean;
When false, the inplace editor starts with all text selected
and the caret after the last character. When true, the caret is
positioned at the location where the mouse was clicked to
start the inplace editing.
grid.MouseActions.EditSelectAll: boolean;
COMPONENTS
DEVELOPERS
grid.InvalidEntryTitle: string;
grid.InvalidEntryText : string;
grid.InvalidEntryIcon : integer;
procedure TForm1.AdvStringGrid1CellValidate(Sender:
TObject; ACol, ARow: Integer; var Value: string;
var Valid: Boolean);
var
len: integer;
begin
len := Length(Value);
Valid := (len >= 3) and (len <= 6);
if Valid then
Value := Value + '$'
else
begin
AdvStringGrid1.InvalidEntryTitle :=
'Incorrect number';
AdvStringGrid1.InvalidEntryText :=
'Please enter a number with 3 to 6 digits';
AdvStringGrid1.InvalidEntryIcon := ieError;
end;
end;
procedure TForm1.AdvStringGrid1GetEditorType(Sender:
TObject; ACol, ARow: Integer;
var AEditor: TEditorType);
begin
AEditor := edNumeric;
end;
Figur 1:
COMPONENTS
DEVELOPERS
Page 35 / 2189
procedure TForm1.AdvStringGrid1GetEditorType(Sender:
TObject; ACol,
ARow: Integer; var AEditor: TEditorType);
begin
case ACol of
1: AEditor := edComboEdit;
2: AEditor := edComboList;
3: AEditor := edSpinEdit;
4: AEditor := edFloatSpinEdit;
end;
end;
Figur 3:
Dropdown editors
For an even more rich user experience, TAdvStringGrid v5
introduces a new set of inplace editors for choosing colors,
images, time, edit numbers via a calculator, pick values
from a combobox with detail notes per item or pick values
from a dropdown grid. This set of inplace editors shares a
common structure.
The dropdown has a header and footer. Both header and
footer can contain HTML formatted informational text
about the editor and can feature buttons as well. The
settings for the dropdown control header and footer are
exposed via grid.ControlLook.DropDownHeader and
grid.ControlLook.DropDownFooter.
Note that the dropdown header and footer are optional and
can be turned off by setting the respective Visible property
to false. When the SizeGrid property is set to true on the
footer, the dropdown can be resized by dragging from the
bottom-right corner.
Figur 2:
Page 36 / 2190
COMPONENTS
DEVELOPERS
COMPONENTS
DEVELOPERS
Page 37 / 2191
http://www.tmssoftware.com/site/asg24.asp
Page 38 / 2192
COMPONENTS
DEVELOPERS
Delphi,
C++Builder,
or RAD Studio 2010,
you get
TMS Smooth Controls for free.
Purchase or upgrade to
Delphi, C++Builder,
or RAD Studio 2010 Enterprise
or Architect edition
and you also get Delphi for PHP absolutely free.
Page 39 / 2193
COMPONENTS
DEVELOPERS