Expert Delphi - Marco Cantù - E-Book

Expert Delphi E-Book

Marco Cantù

0,0
29,99 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.

Mehr erfahren.
Beschreibung

Master Delphi, the most powerful Object Pascal IDE and versatile component library for cross-platform native app development, by harnessing its capabilities for building natively compiled, blazingly fast apps for all major platforms, including Android, iOS, Windows, Mac, and Linux.
Expert Delphi begins with a quick overview of Delphi, helping you get acquainted with the IDE and the Object Pascal language. The book then quickly progresses to more advanced concepts, followed by the architecture of applications and the FireMonkey library, guiding you through building server-side services, parallel programming, and database access. Toward the end, you’ll learn how to integrate your app with various web services and deploy them effectively.
By the end of this book, you’ll be adept at building powerful, cross-platform, native apps for iOS, Android, Windows, and macOS—all from a single code base.

Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:

EPUB
MOBI

Seitenzahl: 521

Veröffentlichungsjahr: 2024

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Expert Delphi

Robust and fast cross-platform application development

Marco Cantù

Paweł Głowacki

Expert Delphi

Copyright © 2024 Packt Publishing

All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.

Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

Group Product Manager: Kunal Sawant

Publishing Product Manager: Akash Sharma

Book Project Manager: Manisha Singh

Senior Editor: Esha Banerjee

Technical Editor: Rajdeep Chakraborty

Copy Editor: Safis Editing

Indexer: Tejal Daruwale Soni

Production Designer: Ponraj Dhandapani

DevRel Marketing Coordinator: Shrinidhi Manoharan

First published: June 2017

Second edition: February 2024

Production reference: 1090224

Published by Packt Publishing Ltd.

Grosvenor House
11 St Paul’s SquareBirmingham
B3 1RB, UK

ISBN 978-1-80512-110-7

www.packtpub.com

When I joined Embarcadero, Paweł greeted me saying “It is great that you are now on board, and we are both working for the same company and making Delphi better!”

Working on a new edition of his work has been great. Therefore, this book is dedicated to the late Paweł Głowacki, a friend and one of the most passionate Delphi evangelists, and to the fond memories of the time spent together.


– Marco Cantù

Foreword

In mid-December 2017, the sad news of Paweł Głowacki’s passing broke in the Delphi developer community. I had the pleasure of working closely alongside Paweł for over 6 years until mid-2017, covering 10 major Delphi product launches with him.

Behind each product launch, there were new demos to create, events to attend, and new webinars to organize and attend to help share details about the new features available to Delphi developers. Between all these activities, Paweł’s enthusiasm for programming shined brightly, a light that shone so brightly that it was dearly missed when it left us all too soon. The outpouring of love from around the world showed how well he, and his content, was loved and the magnitude of his global stardom within the Delphi community.

I remember the time when I started working at Embarcadero and Paweł was in the UK office ahead of an event. I met him in the lead-up to the launch of XE2; I noticed that he had a really thick book about shader languages that he was reading intensely… He joked about how it was really interesting, but, how it could also double up as a pillow! (He’d had an early flight in). But this summed up his approach. He always wanted to bring great content, founded in a deep technical understanding of developers, and delivered it with a smile and in a fun way.

This is something that Expert Delphi brings to you. Many of the chapters lean heavily on Paweł’s years of making content understandable yet fun to learn. Chapter 2 starts off with one of his favorite Developer Direct webinar sessions that we ran for multiple series called Mind Your Language.

This second edition has been lovingly revised by Marco Cantù, to bring its content up to date with the latest developments in Delphi. With Paweł’s voice still shining through, you will learn how easy it is with Delphi to develop across the stack. Expert Delphi covers everything from front-end application development for mobile and desktop to 3D development, to embedding database access and building and using backend multi-tier architectures touching on both SOAP and REST with RAD Server.

As David I commented in the original edition’s foreword, “In Expert Delphi, Paweł encapsulates the knowledge gained through years as a world-class Delphi engineer, an entertaining presenter, a community leader, and a passionate advocate. With his words, step-by-step instructions, screenshots, source code snippets, examples, and links to additional sources of information, you will learn how to continuously enhance your skills and apps.”

And, as Paweł and I used to say at the end of our developer sessions… Thanks for joining us today and Happy Coding everyone!

Stephen Ball

Presales Director, Embarcadero
@DelphiABall

Contributors

About the authors

Marco Cantù is an experienced Delphi expert, who started working with the product since its introduction in 1995. He is currently working as a Product Manager for RAD Studio at Embarcadero Technologies, an Idera company. Prior to that, Marco was a Delphi trainer and consultant for Wintech Italia. Over the years, Marco has written 20 books on Delphi, from the classic Mastering Delphi series to the recent Object Pascal Handbook. Marco has been a speaker at many Delphi and programming conferences worldwide, including over 10 Borland US Conferences, the Software Development Conference, Borland European conferences, EKON (Germany), DCon (UK), Conference to the Max (Holland), DelphiTage, the Italian Delphi Day, and a few editions of Delphi Developer Days. Marco is based in Italy.

Paweł Głowacki was Embarcadero’s European Technical Lead for Developer Tools. Previously, Paweł spent over 7 years working as a senior consultant and trainer for Delphi within Borland Education Services and CodeGear. Apart from working with Embarcadero customers across the region, he represented Embarcadero internationally as a conference and seminar speaker. Paweł passed away in mid-December 2017, but he is alive in the hearts of the Delphi developers community, worldwide.

Marco Cantù (left), Paweł Głowacki (right)

About the reviewer

Mauricio Marcelo Paiva de Abreu has been passionate about technology since he was 9 years old. His first computer was Sharp’s MSX HotBit, in which he started programming with Basic. He has closely followed the development of many programming languages and PCs, including CP500, Prológica – Solution16, and several others since then.

Mauricio’s first contact with Delphi was in version 5.0. A self-taught person in Delphi, he developed projects for intern companies in Access and Delphi. He managed these contracts and worked on Delphi 7 videoconferencing room rental projects with SQL Server as the database. At present, he has a software development company that creates new ideas for the Brazilian market, which uses Delphi for 99% of its ERP development.

He is knowledgeable in several versions of Delphi, including 2010, XEs, Berlin, Tokyo, Alexandria, and Athens.

Table of Contents

Preface

Part 1: Building Blocks

1

Fasten Your Seat Belts

Delphi installation

Delphi versions

Running the Delphi installer

Installing custom components

IDE options

Riding the IDE

Delphi compilers and toolchains

The “Hello World” app

Deploying to mobile devices

Deploying to Android

Deploying to iOS

Summary

2

Mind Your Language

Do you speak Object Pascal?

Program structure

Console application

Building a forms application

The Object Pascal language phrasebook

Tokens

Constants

Types

Inline variables declaration

Sets and arrays

Helpers

Generics

Anonymous code

Operator overloading

Runtime type information

Custom attributes

Summary

3

Packing Up Your Toolbox

Technical requirements

Working with files and streams

Working with JSON

Writing JSON

Reading JSON

Using the JSON wizard in Delphi 12

Working with XML

Summary

4

Using the Parallel Programming Library

Technical requirements

Using threads

Access synchronization

Calling Synchronize

Atomic operations

Using a critical section

Using TMonitor

Exploring the Parallel Programming Library

Parallel loops

Using tasks

The beauty of futures

Summary

Part 2: Going Mobile

5

Playing with FireMonkey

Technical requirements

Drawing in code

Get moving with timers

Precise timing

The power of parenting

Shapes

Animations

Touching the screen

Touch

Gestures

Multi-touch

Game of Memory

Designing the game

Working with images

Designing the user interface

Aligning, anchoring, and margins

Layouts

Building the main form of the game

Storing the game’s configuration

The game’s settings form

Summary

6

FireMonkey in 3D

Technical requirements

Cross-platform 3D rendering

Direct use of Context3D

Using a cube component

A custom wireframe component

Ready-to-use 3D objects

The Moving Earth app

Building an interactive 3D scene

Using 3D models

A starfield simulation

Mixing 3D and 2D

Summary

7

Building User Interfaceswith Style

Working with built-in styles

Adding more controls

Adding the dials

Using custom styles

Embedding styles as resources

Customizing styles

Using frames

Working with inherited views

Previewing forms on devices

Summary

8

Working with Mobile Operating Systems

James Bond’s toy

What am I running on?

The life of an app

Sensing the world

Using the location

Using the orientation and motion sensors

Taking photos

Using share sheets

Camera, action!

Notify me!

Navigating the web

Delphi language bridges

Vibrating on Android

Vibrations on iOS

Summary

9

Desktop Apps and Mobile Bridges

Technical requirements

Single source for mobile and desktop

Using the MultiView control

Using data grids

Leveraging layout controls

Flow layout

Grid layout

Scaled layout

Grid panel layout

Building desktop apps for Windows, macOS, and Linux

Creating apps for macOS

What about targeting Linux?

App tethering

Summary

Part 3: From Data to Services

10

Embedding Databases

Technical requirements

Data-driven apps

Modeling data

Choosing a database

Accessing databases with FireDAC

Building a data-driven UI

Using visual live bindings

Fast UI prototyping

Summary

11

Integrating with Web Services

Technical requirements

Understanding Web Services

Building a native HTTP client

Consuming XML SOAP Web Services

Integrating with REST services

Integrating with the cloud

The Delphi Cloud API

Moving the ToDo List app to Azure

Using the AWS SDK for Delphi

Summary

12

Building Mobile Backends

Technical requirements

Delphi and multi-tier architectures

Working at a low level with WebBroker

Generating a standalone project

The Standalone WebBroker server

Adding Apache support

Adding a client application

Building the web module

Creating a new ToDo item

Reading the data and other operations

A client for the REST API

Do-it-yourself with DataSnap

Building the DataSnap server

Implementing the DataSnap server functionality

Building a DataSnap client

Summary

13

Easy REST API Publishing with RAD Server

Technical requirements

RAD Server setup

Building RAD Server resources

The ToDo list in RAD Server

Creating a client app for RAD Server

Summary

14

App Deployment

Technical requirements

Deploying to app stores

Apps artwork

Apps orientation

Publishing on the Apple iOS App Store

Publishing your Android apps to the Google Play Store

Monetizing with ads and in-app purchases

Automate the development process

App versioning

Practical version control

Sleeping well with unit testing

Summary

15

The Road Ahead

Review of what we have learned

Trends in mobile development

A single screen is not your limit

Serverless backends and NoSQL databases

The AI revolution

Ideas for new and innovative apps for you to build

Summary

Index

Other Books You May Enjoy

Part 1: Building Blocks

You’ve just installed Delphi and have its main window in front of you, how do you start building the next successful mobile app? We will get to it but before we get deeper into the main topic of this book, it’s important to focus on some of the foundations and building blocks of Delphi development.

In particular, we are going to take you on a tour of the IDE, explore Delphi’s Object Pascal programming language, and look into some core elements of the RunTime Library (RTL) focusing on ways to manage files using different formats and on multi-threading.

We’ll do this by building a small and focused example. Something that will help you get started on the main goal of the book to help you become an expert Delphi mobile developer.

This part has the following chapters:

Chapter 1, Fasten Your Seat BeltsChapter 2, Mind Your LanguageChapter 3, Packing Up Your ToolboxChapter 4, Using the Parallel Programming Library

1

Fasten Your Seat Belts

The key benefit of Delphi in mobile development is that you can design your app once and you can natively compile it for both Android and iOS from the same source code. This chapter is exactly about this capability. We are going to install Delphi, create a simple one-button “Hello World” app, and then run the same app on an Android phone and an iOS device.

The integrated development environment (IDE) is where the programmer spends most of their time. Learning best practices of using the IDE will pay off in the future and will increase your developer productivity. Before building apps, you need to feel comfortable working in the Delphi IDE.

The objective of this chapter is to help you install the Delphi IDE, learn basic IDE functionality, and prepare it for mobile development.

In this chapter, we will cover the following topics:

Delphi installationRiding the IDEDeploying to mobile devices

Delphi installation

Delphi is a Windows program, so you need to have a computer with a proper version of Windows installed and enough free space on your hard drive. You may want to install Delphi on a physical computer or a Windows virtual machine image using one of the available virtualization solutions, such as VMWare. In this case, you can also use a Mac computer with a Windows virtual machine. Installing it on a virtual machine has some advantages in cross-platform development. To create iOS apps, you will need to have access to a Mac computer anyway. This could be another computer available on the local network or the same physical machine configured with a virtual machine to run the IDE. Whether you choose to install Delphi on a physical or virtual Windows machine, the installation process is the same.

Delphi is implemented as a native Windows 32-bit executable, but it is recommended to install it on 64-bit versions of Windows 11, or a recent 64-bit version of Windows 10.

Delphi versions

Delphi comes in different versions, so before downloading the installer, we need to decide which version to choose. There are four Delphi versions: Community Edition (CE), Professional, Enterprise, and Architect:

CE is a free version of Delphi with very few technical limitations compared to the Professional edition, but a license valid only for hobbyists, startups, and other scenarios in which the developer or company revenues do not exceed 5,000 US dollars a year. Refer to the product’s End User License Agreement (EULA, available online) for more details on the CE license limitation and to make sure you are using it legally. The version of the CE edition generally lags several months behind the paid product versions, but it offers a great way to get started with Delphi, and it’s free.The next version is Professional. It contains all the features required for mobile development as it can target Windows, macOS, Android, and iOS. The Professional edition is a viable choice if you plan to build Delphi applications with no need for advanced database access, multitier support, and some of the other advanced features available in the Enterprise edition.The third version is Enterprise. This is the full version of Delphi and contains everything that is in Professional, plus support for building Linux server applications, access to all databases, DataSnap and RAD Server multitier solutions, and some more additional features. This is the version of Delphi that we are going to use in this book, although most of the examples and features we’ll cover are also available in the Professional edition.The highest Delphi version is Architect. This is a product bundle that contains Delphi “Enterprise” and separately installed tools from Embarcadero sister companies, such as Aqua Data Studio from Idera and Ext JS from Sencha. We are not going to cover any of those additional tools in this book.

Delphi is available as a standalone product, but it can also be used as part of RAD Studio. RAD Studio contains Delphi and C++Builder. Delphi and C++Builder are two different IDE personalities of RAD Studio; both can be installed from the same RAD Studio installer into one deeply integrated environment. Both products, Delphi and C++Builder, share the same component libraries but differ in the programming language being used. Delphi uses Object Pascal, while C++Builder uses standard C++. As this book’s title implies, here, we are focusing on Delphi.

In this book, we are going to use Delphi 12.0 Enterprise edition. Delphi is produced by Embarcadero, and you can find more information about different Delphi versions and features on the Embarcadero Delphi home page: http://www.embarcadero.com/products/delphi.

You can get Delphi directly through the Embarcadero website, including a 30-day trial and the CE version of Delphi (at the time of writing, this is version 11.3). After purchasing Delphi, you will receive an automatically generated email with the download link to the Delphi installer and the serial number needed during the installation.

Running the Delphi installer

The Delphi installer is available in two different formats. You can either choose to install Delphi using the so-called Online Installer, or you can use the Offline Installer, based on an ISO image you can download upfront. The first option is the default one and makes it for faster installation; it is also the only option available for the CE version. The Online Installer (also called Minimal Installer) is a small application that downloads and installs only some minimal parts of Delphi. After this “minimal Delphi” is ready, you can choose to install the support for different platforms and features. Even if you install from the ISO image, the process is the same as the minimal installation, followed by the platform selection. This is useful when internet access is slow, unreliable, or for some security reasons, the Windows machine that you want to install Delphi on does not have internet access.

In this section, we are going to use the standard Online Installer, using the download link provided by the website (for trial or CE versions) or in the confirmation email. You will need to have administration rights on Windows to be able to install Delphi. On one of the first installer screens, you will be prompted to enter the serial number that you have received from Embarcadero, and you will need to enter your Embarcadero Developer Network (EDN) username and password. If you do not have an EDN account, you can quickly create one from within the installer or on the website (my.embarcadero.com). It is free and it makes the connection between the serial number that you have received and you as the user of the software.

After the initial installation, as shown in Figure 1.1, you can choose which platforms and additional features you want to install. You can always add or remove platforms and features from the Tools | Manage Platforms menu item of the Delphi IDE after the installation is complete:

Figure 1.1: Platform Selection in Delphi 12

The list that’s displayed depends on your license, which might include both Delphi and C++ languages (as in the preceding screenshot) or only Delphi. I recommend going for the full installation, selecting all platforms and all additional features, except for languages other than English. At the end of the installation, you will see the message that the installation is complete, and you can start working.

If you run into problems during the installation, you can review Delphi Installation Notes on Embarcadero DocWiki at https://docwiki.embarcadero.com/RADStudio/en/Installation_Notes.

Delphi will be installed. Run it – the first thing you will see is the Welcome Page area, as shown in Figure 1.2:

Figure 1.2: The Delphi Welcome Page area

From the Welcome Page area, you can create new applications (in the Create New section) open recent projects (in the Open Recent section), access new or promoted add-on packages in GetIt, and check recent YouTube videos by Embarcadero (in the Learn section). The projects that you frequently work with can be marked as Favorite projects and will show at the top of the Open Recent list. Now, let’s look at extending Delphi with new components.

Installing custom components

The Delphi IDE has an open architecture and provides many ways to extend its functionality through additional packages. You can see the list of currently installed packages by clicking the Installed Packages... option available from the Component menu. The most typical Delphi building block is a component. Technically, this is about taking object-oriented programming principles and moving them one step further. With components, you can manipulate regular programming language object instances at design time before the project is built for a given platform. Packages may also contain custom property editors and the Open Tools API extension to the IDE itself, which may add custom functionality to the IDE, such as additional menus and windows.

Out of the box, with the default Delphi installation, you will get a few hundred components already preinstalled. Depending on your Delphi version and selected features, this list may differ. As shown in Figure 1.3, you can click on the Install Packages option in the Component menu to verify which components are installed:

Figure 1.3: The Install Packages window shows the components in a package

Many additional free and commercial component packages are not installed by default. They can be installed with the integrated GetIt Package Manager. It is available either from the Welcome Page area or from the Tools menu. See Figure 1.4.

Figure 1.4: The Delphi GetIt Package Manager dialog box

If you want to install any of the available component packages, just click on the Install button to the right of the package. The installation process is very straightforward. After accepting the license, the components are downloaded from the internet, possibly compiled, and installed into the IDE. Notice that some special add-on components and premium styles are available only to customers with an active update subscription and, for this reason, they cannot be installed with the CE version.

How do you customize other areas of the Delphi IDE? You can use the Options dialog.

IDE options

You can manage all aspects of how the Delphi IDE works from the Options dialog, which is available from the Tools menu. Here, you can customize various aspects of how Delphi operates as a whole. See Figure 1.5.

Figure 1.5: One of the many pages of the Delphi IDE Options dialog box

You cannot understand the assorted options of this dialog box until you have a better understanding of the different capabilities the Delphi IDE offers, which is what I am going to focus on in the next section.

Riding the IDE

In a nutshell, Delphi is a program for making other programs. The program responsible for generating executable files from the source code is a compiler. It is typically implemented as a command-line application, but it can also be invoked directly from the IDE. When executing the compiler as a command-line application, you can pass to it command-line parameters. As depicted in Figure 1.6, compilers take different command-line parameters, including the location of source code files necessary to generate the resulting binary file:

Figure 1.6: A simplified compiler architecture

It is possible to write your programs using a text editor such as Notepad and then execute the compiler from the command line, but it is not the most efficient way of creating applications. Most programmers use IDEs to work on apps. The idea of an IDE originates from Delphi’s ancestor – Borland Turbo Pascal, in the 1980s – and it comes from the integration of three previously separate programs: Code Editor, Compiler, and Debugger.

Delphi compilers and toolchains

The Delphi IDE contains different compilers for generating apps for different platforms. The IDE manages all the necessary source code files for building an app. It also takes care of passing correct parameters to the right compilers and manages their output. In the IDE, it is possible to build, deploy, and run an app directly on the mobile device connected with a USB cable by just pressing the Run button. It is also possible to generate an application executable without running it by using the Compile or Build commands. Sometimes, it is very handy to be able to step into an application as it is executed in the host operating system. This process is called debugging. In the Delphi IDE, you can run your program “with” or “without” debugging.

Nine Delphi compilers come installed with Delphi 12.0, as indicated in Table 1.1. All compilers are available in all product editions, except for the Linux compiler, which is only in the Enterprise and Architect versions:

Delphi Compiler

Target Platform

Dcccaarm

32-bit Android

dccaarm64

64-bit Android

dcciosarm64

64-bit iOS

dcc32

32-bit Windows

dcc64

64-bit Windows

dccosx64

64-bit macOS (Intel)

dccosxarm64

64-bit macOS (ARM)

dcciossimarm64

iOS simulator for macOS ARM

dcclinux64

64-bit Linux (Intel)

Table 1.1 – The Delphi compilers for the different target platforms

These compilers are typically installed into the bin folder of the product installation folder (by default, this is under C:\Program Files (x86)\Embarcadero\Studio\). The bin folder of Delphi is added to the Windows path by the installer, so you can try and execute them directly from the command line. As shown in Figure 1.7, executed with no parameters, the Delphi compilers will just display their version numbers and the available command-line switches:

Figure 1.7: The Delphi Android compiler invoked from the command prompt

In the IDE, we don’t need to invoke command-line compilers directly. The IDE does this for us when we choose to either run, build, or compile our project. It will also take care of outputting the resulting binary files into a separate folder for each supported platform and build configuration. When we build, deploy, and run our apps, we can see the commands being executed in the message log pane, including the parameters passed to them and their output.

The “Hello World” app

Starting with a new programming language or framework typically involves creating a program that displays the classic Hello World message; we will follow this convention. Our app will have just one button. When you press this button, the message Delphi Hello World! will be displayed. Later in this chapter, we are going to put this app on an iPhone and an Android device.

To start, click on the Multi-Device Application – Delphi option in the File | New menu (see Figure 1.8) or the welcome page:

Figure 1.8: The Multi-Device Application – Delphi menu option

This will display a wizard with different multi-device project templates, as shown in Figure 1.9.

Figure 1.9: The Multi-Device Application project templates dialog

Double-click on the Blank Application project template. This will create a new project and open its main form in the IDE. See Figure 1.10.

Figure 1.10: A new, blank multi-device application in the IDE

The first thing you must do after creating a new project is to save it all. The Delphi project is made up of multiple files that are managed by the IDE. The IDE will also create subfolders in the project directory for managing different artifacts, such as compiled executable files, editor history, and recovery files, so it is always a good idea to save a new project into a new empty directory.

Click on the Save All button in the Delphi toolbar just below the main menu (see Figure 1.11) to save all the files in the project. Remember that Delphi does not automatically save the files in the editor, so you need to do that from time to time. As an alternative, you can configure the IDE so that it saves the source code files when you start the debugger. In any case, the IDE does save temporary files in a separate location so that in case of a crash, it can recover unsaved files:

Figure 1.11: The “Save All” speed button

First, we will be asked to save the main form for our application. Enter uFormHelloWorld in the file save dialog and click Save. Next, a second file save dialog will be displayed. Here, we need to give our project a name. Enter DelphiHelloWorld and click Save.

Delphi organizes all the necessary files to build an app into a “project.” The Delphi IDE contains several windows that help us work on our projects. There are different project types. It is possible to create a project group inside the IDE, keeping several projects at hand, but at any given moment, there could be only one project active. You can see the name of the active Delphi project in the title bar at the top-left corner of the Delphi window. Its name is also displayed in bold font in the Project Manager area.

Let’s add a button to the form. The fastest way to work in the IDE is to use IDE Insight. There are hundreds of different options, components, and windows in the IDE. If you know what you are looking for, you can just press F6 or press Ctrl + . keys at the same time. The IDE Insight combo box, in the IDE title bar, will receive focus. You can also just click on the IDE Insight combo box itself. We want to add a button to the form. Just start typing what you are looking for and notice how, with every keystroke, the list of available items in the IDE Insight combo box changes. Type b, u, and t; and after three keystrokes, the TButton component will be the first element of the list. See Figure 1.12.

Figure 1.12: Incremental filtering in the IDE Insight combo box

Just press Enter and a button will be added to the form. IDE Insight is probably the single most useful productivity feature of the IDE. It only takes five or six keystrokes to add the component to the form. Alternatively, we could locate the TButton component in the Tool Palette window (by navigating over the available components categories or typing in the local search box, as shown in Figure 1.13) and double-click it to add it to the form:

Figure 1.13: Searching for the Button component in the Tool Palette window

We now have a button in the middle of an empty form. Let’s start working on how our app will look – that is, in the form designer. Later, we will focus on what our app is going to do – that is, coding in the editor window. Click on the button and move it more toward the window’s top-left corner. In the bottom left part of the IDE, there is an Object Inspector pane. Here, we can modify the properties and events of a component. In this case, we want to change the text that’s displayed on the button. Make sure that TButton1 is selected in the Object Inspector pane and find its Text property. As I’ve done in Figure 1.14, replace the default caption, Button1, with the text Hello World:

Figure 1.14: Changing the button’s text in the Object Inspector pane

Now, find the Name property of the button control and change it to BtnHelloWorld. Notice that the component’s new name is immediately reflected in the Structure view above the Object Inspector pane. The change, however, also affects the application code skeleton we’ve generated so far. Now, select the form. You can click somewhere on the form in the Form Designer to select it or just click on the form’s node in the Structure view above Object Inspector. That is how the IDE works. Different windows are synchronized with each other, so selecting a component changes the current component selection in other views. Find the Name property of the main form and change the default, Form1, to FormHelloWorld. Now, find the Caption property of the form and change it to Delphi Hello World. Save everything.

It is always a good practice to give components and their properties meaningful names. There are different naming conventions. One of the most popular ones is to use an abbreviation of the component type at the beginning of its name so that, from the name, we can immediately figure out the type of the specific component. You also want to keep names short. If names are too long, you need to type more. Long names in the source code also tend to be less readable. It is up to you to decide what naming convention to use, but once you have decided, it is important to stick to it for consistency.

Now that we have built the user interface, we need to define what will happen when the user of our app clicks on the button. For this, we need to attach an event handler to the OnClick event of the BtnHelloWorld button. We can select the button, switch to the Events tab in the Object Inspector pane, locate the OnClick event, and double-click in the space next to it to generate an empty event handler. However, there is a faster way: just double-click on the button on the form. Either way, the IDE will switch from the Form Designer to the Code Editor and the cursor will be placed just at the beginning of an empty line between “begin” and “end,” where we need to enter some code in the Object Pascal language. This code will be executed by our app in response to the click event – that is when the user clicks on the button. It could be misleading that this event is called OnClick as you can only use a mouse on desktop computers. Don’t worry – if we compile our app to mobile targets, this event will be triggered in response to tapping on the button on the mobile device’s screen.

In the code of our event handler, we will invoke the built-in ShowMessage function and pass to it Delphi Hello World as the string that we want to display in the message. The editor should look like what’s shown in Figure 1.15:

Figure 1.15: The button’s OnClick event handler code

Click on Save All again. Now, let’s run the application directly. Press F9 or click on the green run arrow icon under the main menu to build and run the project. You can see the application running in Figure 1.16:

Figure 1.16: The Delphi “Hello World” multi-device app running on Windows

We are on Windows so, by default, we will compile our multi-device app as a Windows 32-bit executable. This is extremely useful when developing mobile apps with Delphi. During development, it is quicker to run our multi-device app on Windows to see that it does not contain any mistakes. If it compiles fine on Windows and runs with no problem, then there is a big chance that it will also compile OK with mobile compilers and run on a device. Building and deploying it to a mobile device typically takes longer.

Every Delphi form is made of two source code files. When we entered uFormHelloWorld as the name of the main form of our app, the IDE created two files: uFormHelloWorld.pasand uFormHelloWorld.fmx.

You can switch between these files by clicking on a specific button of the toolbar with a “Toggle Form/Unit (F12)” hint, press F12, or use the Code and Design tabs at the bottom of the editor or form designer. The first file is what we can see in the Code tab: it’s the source code of the form and the file in which we added the event handler code. You can see this code in Figure 1.17:

Figure 1.17: The Code Editor with the unit of the “Hello World” app

The content of the fmx file is managed by the Form Designer and we generally do not edit it directly. Every time we change something in the Object Inspector pane or the Form Designer, these changes are stored in the fmx file. The Form Designer gives us a “what you see is what you get” user experience so that we can see what our app will look like even before we run it.

You can preview the text representation of the form file by right-clicking somewhere on the form and selecting the View As Text option from the context menu, as shown in Figure 1.18.

Figure 1.18: The Delphi Form Designer, after calling View as Text

To return to the form view, just right-click in the editor again and select View As Form from the context menu.

The most important window in the IDE is Project Manager (see Figure 1.19). It provides a graphical interface to work with all the files that make up our projects and lets us switch between different compilers and build configurations. You can build your applications in either Debug or Release mode. Debug mode is used during an app’s development. The resulting binary file will contain additional binary information that can be used by a debugger. When our app is ready to be built for distribution to an app store, then we can switch to Release mode. This will generate an app in a form that is suitable for distribution, removing useless debug information and optimizing the generated code:

Figure 1.19: The Delphi Project Manager

Let’s have a look at the different files that make up a Delphi project. If you want to quickly find the folder where the current project is located, you can right-click on one of the project files in the Project Manager area and use the Show in Explorer command.

You should now see all files and folders that make up our DelphiHelloWorld project, as shown in Figure 1.20:

Figure 1.20: The files that make up a Delphi project in Windows Resource Explorer

Among others, you will see two files named uFormHelloWorld and two project files with the same name as our project, DelphiHelloWorld, but with different extensions. The first one has an extension of dpr, which stands for “Delphi Project” and contains the main program file of our application. We can preview this file inside the IDE (see Figure 1.21) by going to the Project | View Source menu:

Figure 1.21: The Delphi project source code

This is the application’s main program file, which is managed by the IDE, and most of the time, we do not need to change anything there. The second project file has a dproj extension. If we open it in an external editor (in Figure 1.22, below UltraEdit, but Notepad will also be fine), we will see that it contains XML code with build instructions for the MSBuild engine that is used by Delphi to manage all files and resources that are needed to build our project for different targets and configurations:

Figure 1.22: The contents of the”dproj” project file in an external editor

There is also a win32 subfolder here. It contains a subfolder called debug and two files: DelphiHelloWorld.exe and uFormHelloWorld.dcu. The first file is a regular Windows 32-bit executable program that we have just built. We could copy this file to another computer to run it, although it doesn’t do much, as it only displays a message. The second file is the product of compiling a single Object Pascal file and it is only useful for the IDE during the build process. You can safely delete the win32 folder and its contents. The next time you run the application, these folders and files will be recreated. That is the difference between “building” and “compiling” the project. The first time, both commands do the same thing. They generate all binary files in the output folder. dcu files are generated first and the exe file is generated next. If we select Build every time, all binary files are recreated. If we select Compile, only the units (Object Pascal files and matching dcu files) that have changes recreated. This speeds up compiling a large project.

Deploying to mobile devices

Our DelphiHelloWorld project is now ready for deployment to mobile devices. We have already built and run it on Windows. Now, we are going to deploy it to an Android device, and then to iOS.

There is a one-time preparation phase for mobile development that you need to go through after Delphi is installed. We want to get to the point in which you can see your devices as targets inside the Project Manager area.

For Android, all the steps to configure your system to detect your device are described at https://docwiki.embarcadero.com/RADStudio/en/Configuring_Your_System_to_Detect_Your_Android_Device.

Deploying to Android

Deploying apps from Delphi to Android devices is simpler than deploying to iOS. You only need to have an Android device and a USB cable to connect your device to a Windows machine where you have Delphi installed.

The first step is to enable USB debugging on the device. This option can be set in Developer options on your device. However, this option is not easy to find because you need to enable development support on the device itself first. Follow these steps to do so:

On your Android device, go to Settings | About phone. Tap on Build number seven times to become a developer! This makes the developer options available.Under Settings | System | Developer options (or Settings | Developer options on older versions of Android), check the USB Debugging option.Connect your Android device with a USB cable to a computer where your PC is running. The first time you connect your Android device, you may see a message displayed on the device to Allow USB debugging from the computer identified by a string of hexadecimal numbers that represent the computer’s RSA key fingerprint. Check the Always allow from this computer option and click on OK. More information on enabling USB debugging on Android devices is available at https://docwiki.embarcadero.com/RADStudio/en/Enabling_USB_Debugging_on_an_Android_Device.The next step is to install the USB driver for your Android device on the machine where Delphi is installed. This feature is not part of Delphi but of the Android SDK. You can install the SDK by enabling it in the Additional Features area available in the Delphi installer and Platform Manager, or manually after downloading it from Google. The Delphi installer configures the Android SDK, but the USB driver installation is a manual step. Depending on your device, the steps may be different. You can find detailed information on how to install the USB driver for your Android device at https://docwiki.embarcadero.com/RADStudio/en/Installing_the_USB_Driver_for_Your_Android_Device.

Now, let’s go back to the Delphi IDE. For each platform we want to target outside of Windows, we need to configure a specific SDK Manager. Open the Tools | Options dialog box and select the Deployment | SDK Manager section in the tree on the left. Now, click on the Add button to add a new SDK, as shown in Figure 1.23. Select the Android 32-bit platform:

Figure 1.23: Adding a new SDK in the Delphi IDE SDK Manager

In this case, we don’t need to configure a remote connection (that’s for Apple OSs and Linux). In the last combo box, we must select the Android SDK installation folder, in the GetIt Catalog Repository (or the location of your manual installation of the Android SDK).

You might want to repeat the same steps to install an SDK for the Android 64-bit platform (the steps are identical).

At this point, you can get back to the DelphiHelloWorld project, reopening it in the Delphi IDE if it’s not still open. In the Project Manager area, expand Target Platforms and double-click on the Android 32-bit node or the Android 64-bit node to select one of the two available Android targets. It should display the name in bold font, indicating that this is now the current project compilation and execution target.

Expand the Target node under the Android platform; if you have an Android device connected via USB, with the proper developer configuration and permissions, you’ll see the device’s name listed. See Figure 1.24.

Figure 1.24: Selecting a target device in the Delphi Project Manager area

Now, when you click on the green run arrow, the IDE will build our DelphiHelloWorld app using the Delphi Android compiler, create an APK package, deploy the app, install it on the device, and run it – all in just one click. You can see the app running in Figure 1.25:

Figure 1.25: The Delphi “Hello World” app running on Android

Deploying to iOS

Deploying apps from Delphi to an iOS device is significantly more complex than the Android counterpart. Here is a quick checklist of what you need:

An Apple Mac computer running macOS. It can also be an Intel or ARM version, but the ARM version is needed if you want to run your apps in the iOS simulator.The Apple Xcode (free) development environment installed on your Mac. There is no separate SDK for iOS; the development tools are only available as part of the Apple developer IDE. The reason is that during the process of deploying to an iOS device, an app needs to be digitally signed with the special command-line tool that comes with Apple Xcode installation.A paid Apple iOS Developer Program account.A physical iOS (or iPadOS) device. To be able to deploy apps to the iOS device, it needs to be provisioned first. The process of provisioning an iOS device, joining the Apple iOS Developer Program, and installing various certificates is the same as in the case of any app developed with Xcode. You can follow the documentation provided by Apple to learn about this process.

In addition to these requirements, which depend on Apple procedures and are the same regardless of the development tool of your choice, one extra step is required by Delphi. You need to install a special Platform Assistant Server (PAServer) program on the Mac where you have Xcode installed. The PAServer installable package is installed in the “PAServer” subdirectory of the Delphi installation folder. Just copy it over to your Mac and execute it.

PAServer is implemented as a command-line app. After it is launched on Mac, it will display a command-line interface where you will be prompted to enter your Mac password. Your Mac will also display a dialog, asking if it is OK to give PAServer debugging permissions. Accept it.

You can think about PAServer as an agent that performs operations on behalf of the Delphi IDE that runs on a remote Windows machine. For Delphi to be able to connect, you need to know the IP address of the machine where PAServer is running. You can find out the IP address by issuing the i command from PAServer’s command prompt. The list of all available command-line parameters to PAServer is displayed when you enter ? in the command prompt. See Figure 1.26.

Figure 1.26: The PAServer tool running on Mac

Now, you need to configure Delphi so that it can find the iOS device you want to deploy to. The goal is to find your device listed in the Project Manager area. In general, you could have multiple different Macs visible to your Delphi installation. For every Mac you want to deploy to, you need to define a connection profile.

Like what we have done for Android, we need to configure the SDK for iOS. Again, open the Tools | Options dialog box and select Deployment | SDK Manager. This time, you have to select the iOS Device 64-bit platform. At this point, you have to pick an existing or define a new profile to connect. A profile is a configuration that points to the IP address of the Mac where PAServer is running.

When you select Add New in the Connection Profile combo, the Create a Connection Profile wizard will be displayed (see Figure 1.27). Enter a profile name. Click Next. On the next tab of the wizard dialog, we need to enter the address and port of the remote machine. Keep the default port of 64211 and enter your Mac password in the last field:

Figure 1.27: The Create a Connection Profile dialog, which is used to configure an iOS SDK in Delphi

Now, click on the Test Connection button to verify that the IDE can communicate with the remote PAServer. If everything went well, you should see a message stating that the connection to your Mac succeeded. Click on Finish, then proceed with the following steps in the SDK Manager configuration (see Figure 1.28):

Figure 1.28: The Add a New SDK dialog

Once the cache has refreshed, the IDE should automatically update the iOS 64-bit Target node and you should see your device listed there. In Figure 1.29 you can see the project with the configuration of the iOS Simulator target.