Category Archives: Beginner

Articles for those who are in the early stages of a programming career

Setting the default XML DOM in Delphi XE7

Managing the default XML DOM

Scanning the whats new in RAD Studio XE7 (Delphi, C++ Builder) it covers the introduction of a new XML Dom (OmniXML) meaning there are now 3 XML DOMS to choose from when setting the default XML DOM.

  • MSXML
  • ADOM
  • OmniXML

The XML Dom defines which engine in essence is used to work with your XML documents.

Should I read on? Well if your using SOAP or XML in your projects, then this could make a difference to your speed and performance of the applications, and it takes seconds to implement.

Continue reading Setting the default XML DOM in Delphi XE7

IBLite example from CodeRage 9

The Object Pascal programming source code for the IBLite example from CodeRage 9 that I demo’ed is now available from Embarcadero’s Code Central http://cc.embarcadero.com/item/30035

The C++ programming version demo’ed requires the data module from the Object Pascal code, so once I’ve cleaned that up I’ll post that as well.

The IBLite example code works on Windows, Mac OS X, iOS and Android.  The session replays are available online via the CodeRage website. – More on the InterBase sessions in my previous post

 

Adding VCL Styles at runtime

VCL Styles

An example before & after

Screen Shot 2014-10-22 at 06.09.20VCL Styles at run time

VCL Styles provide a great way to update the look and feel of your VCL applications.  VCL Styles can be easily added into an application before you compile through  project options > appearance options.

Adding VCL Styles to Project : Options : Appearance
Adding VCL Styles via Project : Options : Appearance

While you can add VCL Styles into the application binary this way, they will increase the size of the application. If you want to avoid this, (or maybe run a mix of compiled in and dynamically loaded) you can distribute styles along side your application.

By default, VCL Styles are located at the following file path when installing your IDE.

C:\Users\Public\Documents\Embarcadero\Studio\15.0\Styles

A full overview of them is available on docwiki  http://docwiki.embarcadero.com/RADStudio/XE7/en/VCL_Styles_Overview

TStyleManager

Styles are managed via TStyleManager, a class located in unit VCL.Themes.

TStyleManager is a sealed class with a number of class methods that allow you to interact with the global application settings for the styles.

Some key properties on TStyleManager

  • TStyleManager.StyleNames – an array of the styles available in the application.
  • TStyleManager.SetStyle(StyleName : string) – the call to set the application style to a new style

Using these basic methods, its possible to implement code that loops the styles in the application and makes them available to a menu that can then be used at run time to choose which style is selected.

  • TStyleManager.IsValidStyle(FileName: string) – checks a file is a valid style
  • TStyleManager.LoadFromFile(FileName : string) – loads a style up from file.

With the additional file based calls its possible to check for style files and load those into your application as well.  – So lets do that!

VCL Styles code example

The MastApp demo that is installed by default with RAD Studio has an example of using TStyleManager. You can find the demo at:

C:\Users\Public\Documents\Embarcadero\Studio\15.0\Samples\Object Pascal\Database\IBX\IBMastApp

This demo uses 2 code parts and 1 design time selection element to provide styles in the demo.

Part 1 – Compiling VCL Styles into your application.

Open the project and then choose Project > Options and go to appearance. As above, select a couple of styles and run the application. You will then see the Styles menu is updated to show these styles.

The initial code to build the menu loops the StyleNames, creates a menu item for each style, adds then links to an OnClick event to change the style.

Part 2 – Building the style list menu

procedure TMainForm.FormCreate(Sender: TObject);
var
 Style: String;
 Item: TMenuItem;
begin
 //Add child menu items based on available styles.
 for Style in TStyleManager.StyleNames do
 begin
   Item := TMenuItem.Create(StyleMenu);
   Item.Caption := Style;
   Item.OnClick := StyleClick;
   if TStyleManager.ActiveStyle.Name = Style then
     Item.Checked := true;
   StyleMenu.Add(Item);
 end;
end;

Step 3 – OnClick event for the Style selected.

procedure TMainForm.StyleClick(Sender: TObject);
var
 StyleName: String;
 i: Integer;
begin
 //get style name
 StyleName :=   StringReplace(TMenuItem(Sender).Caption, '&', '',
 [rfReplaceAll, rfIgnoreCase]);
 //set active style
 TStyleManager.SetStyle(StyleName);
 //check the currently selected menu item
 (Sender as TMenuItem).Checked := true;
 //uncheck all other style menu items
 for I := 0 to StyleMenu.Count -1 do begin
   if not StyleMenu.Items[i].Equals(Sender) then
     StyleMenu.Items[i].Checked := false;
 end;

end;

So: We have styles working, but you can also load them from file with very little change to the application.

Loading Styles from file and displaying them in the list of styles

VCL Styles can be loaded at run time easily using TStyleManager.LoadFromFile

Using the existing demo, its possible to have it check for a styles sub folder and read any file in there to see if its a valid style.  To do this, we just need to use FindFirst, iterate the returned files and then if they are valid (something we can use TStyleManager.IsValidStyle to determine) load the files.

So the updated code looks something like this block below where we first build the path to check for VCL Style files; then check the folder exists, and if it does check each file to see if its a valid VCL Style (before loading it).  Secondly, using a TStringList the styles loaded are sorted and then the sorted list is used now to build a menu for selecting the styles.

procedure TMainForm.FormCreate(Sender: TObject);
var
 Style: String;
 Item: TMenuItem;
 i: integer;
 searchResults : TSearchRec;
 SearchDir: string;
 SLStyles: TStringList;
begin
 SearchDir := ExtractFilePath(ParamStr(0))  
                 +'styles'+ PathDelim;
 if DirectoryExists(SearchDir) then begin
   if FindFirst(SearchDir+'*.*',  
                faAnyFile - faDirectory,
                searchResults) = 0 then
   repeat
     try
       if TStyleManager.IsValidStyle(
            SearchDir+searchResults.Name) then
          TStyleManager.LoadFromFile(
             SearchDir+searchResults.Name);
     except
       // Who cares.. try the next one.
     end;
   until FindNext(searchResults) <> 0;
 end;
 // Sort the styles using a StringList 
 SLStyles := TStringList.Create;
 try
   SLStyles.Duplicates := TDuplicates.dupIgnore;
   for Style in TStyleManager.StyleNames do
     SLStyles.Add(Style);
   SLStyles.Sort;

   PMStyles.Items.Clear;
   // build menu from sorted list of styles
   for Style in SLStyles do begin
     Item := TMenuItem.Create(StyleMenu);   
     Item.Caption := Style; 
     Item.OnClick := StyleClick; 
     if TStyleManager.ActiveStyle.Name=Style then 
       Item.Checked := true; 
     StyleMenu.Add(Item);
   end;
 finally
   SLStyles.Free;
 end;
end;

Thats it! You can now create a sub folder for VCL styles that you ship along side your application or deploy at a later point.

For C++ use the same classes and units, just with the C++ Loops and constructs. more on VCL Styles with C++ can be found on docwiki

 

Mobile Development Lessons – Delphi & C++

Free series of mobile development lessons for Android and iOS based on C++ and Object Pascal.

Earlier in the year the 2nd edition of the mobile development summer school was run by David I and Jim McKeeth. It was great fun doing the first edition and its amazing to see how some of the tech has moved on further in the year since we first ran summer school. This years was more popular than ever and has been made available on YouTube

YouTube Playlist for video replays

https://www.youtube.com/playlist?list=PLwUPJvR9mZHhLj-gegxc0KpGE0wL2zVBo

Blogs for slides and samples:

To get started is easy!

Download your free trial of RAD Studio https://downloads.embarcadero.com/free/rad_studio

or your FREE edition of Appmethod (C++ Free for Android mobile)
http://www.appmethod.com 

Happy Coding!

Debugging to PA Server on Windows

PA Server – What and why?

When developing software for multiple platforms you often need to debug and run applications on machine and devices that are not your development PC. The RAD Studio and Appmethod approach to this is an ingenious little program that acts as a go-between from the IDE to the remote device / machine. Called PAServer (PA = Platform Assistant) allows the IDE to retrieve the full call stack at run time, pause code with break points, inspect values etc, exactly as you would do debugging a local application.

PA Server is often used on a Mac OS X target for running and debugging applications to Mac OS X, iOS Simulator and iOS Devices, however there is also a windows version of PAServer and this can also be used to simplify preparing for deployment.

PA Server runs over TCP/IP and while developers often use it for local network work, in theory there is no reason why you can not use it to remote debug that trouble some customer where you can’t quite recreate what they are doing. (as long as they are happy for you to install the PA Server client on their machine).

PA Server is also great for deploying files directly to a remote machine when used with the Deployment Options for the project. This ensures that all the files specified are pushed remotely. This is great for updating a remote server or internal build machines.

Installation of PAServer

PA Server needs to be installed on the machine you want to run applications on remotely The install files both Windows and Mac OS X are located in the PAServer folder under your Appmethod / RAD Studio / Delphi / C++ Builder installation. e.g. with RAD Studio XE7 they are located at C:\Program Files (x86)\Embarcadero\Studio\15.0\PAServer.

Both installers (Windows / Mac OS X) just require you to push the next button a few times to install the server.

Running PA Server remotely…

PA Server is ultimately a Console application that you launch, enter a session password (that remote developers will need to connect to the session) and leave running without having to go back to it, but this is how to launch it on each platform.

..on MAC OS X

To launch PAServer on a Mac you have two choices.

  1. Go to “Applications” and choose PAServer 15.0 (for XE7)
  2. Use the new GUI in LaunchPad called PAServer Manager PAServer Manager Icon

If you use PAServer Manager you will see the icon appear at the top of your screen in the menu bar. Clicking on this allows you to “Add Server” (I just call it MyMac by default) and then start and stop the services as well as other useful things like viewing the information (such as IP Address etc).  A lot easier than remembering command codes.

PA Server Manager

PA Server Manager is also useful for managing groups of developers who want to run multiple instances of PA Server on the same machine when developing.

..On Windows

To launch PA Server on windows once installed, you need to browse to the PAServer folder (typically C:\Program Files (x86)\Embarcadero\PAServer\15.0) and double click on the PAServer application.

PAServer on Windows Running
PAServer running on Windows

 Connecting to PAServer running on a Windows machine from the IDE

With PA Server installed, opened and a password set for the session, it is possible to make the remote connection (or even a loopback for a more advanced local test). To achieve this we need to configure a profile for connecting to the remote PAServer instance. – this is really quick to do.

Firstly select the desired compilation target of Win32 or Win64  in the Project Manager, and then right click and choose properties.  The platform properties window is then opened allowing you to choose a profile for the target.

Platform Properties

By default you will need to choose “Add New…” under platforms the first time you run this step, subsequently you will already have the profile saved.

 

Following the wizard for Add New, you can enter a name (e.g. MyPC) and then the IP address (port 64211 should be default unless you have changed it during install). Once you have the IPAddress or pc name entered you can “Test Connection” to verify that the path is working correctly.

Platform Properties Wizard 2

If this fails then check you IP Address is correct – if unsure type “i” into the PAServer console and hit enter to get a list of the listening IPAddresses and check your firewall.

Once you have selected the Platform Profile and its tested, all you need to do is hit run just like before.  Rather than deploying to your project directory, deploying to PA Server sends out all the files into a the PAServer scratch directory (under Documents\PAServer) e.g. C:\Users\Steve\Documents\PAServer\15.0\scratch-dir\

Tip for making things simple?

If you have selected any feature files or added your own files under Deployment Options when developing your applications, e.g. enabling InterBase, IBLite or DBExpress etc. These files will be packaged up for you when running out to PAServer giving you a complete folder structure with files ready for packaging. This also makes testing locally a lot lot simpler 🙂

Getting back to normal

Once you are done testing against a remote profile you can easily return to running locally by right clicking on the target and choosing to Revert to Default Connection.

Revert to default connection

Summer School 2014 – Android and iOS development training

Last year I had the pleasure of hosting the majority of the mobile development training series “Developer Direct – Summer School“.  It was great fun covering everything from Application UI design to local storage through to remote data collection.

The last year has seem some major enhancements to RAD Studio and Appmethod with new components making some of this easier and more exciting with new possibilities, so the content has been updated and re-run by David I and Jim McKeeth in both C++ and Object Pascal/Delphi programming lessons.

SummerSchool2014

To watch the 6 iOS and Android native development lessons register for free at http://forms.embarcadero.com/RADSummerSchool2014

Checking your Android device is ready for development

In this blog I want to take you through the steps for getting your Android device working with the standard USB device drivers, including steps to follow to check if what you have done has worked.

Jim McKeeth did a blog post a while back about installing custom specified Android devices that is worth reading if your device does not work with the standard drivers, however I would recommend trying this first.

Developer mode Enabled?

When you plug in your Android device, you will need to ensure you have  enabled developer mode and confirmed that you want to enable USB Debugging. Under “Setting”, you should be able to see

{ } Developer options

as a menu item. If you don’t, tap “About phone” menu item 5 times to start the messages about going into developer mode.

It is also worth checking at this point that “USB Debugging” is enabled under developer options. You will still need to confirm on the phone when you finally have it all setup and you plug the phone in for the first time, but this is the first steps.

Phone drivers installed?

On windows, you need to have the drivers installed for the phone to enable it for developer work. Once you plug the device in, check the device manager. If it shows under Other Devices with a yellow alert, then you will need to update the drivers.

Nexus 4 showing in Device Manger when its not installed correctly
Nexus 4 showing in Device Manger when its not installed correctly

You can download the drivers if you search the web for them and point to those, or you can download and install them easily with the Android SDK Manager.

Launching the Android SDK Manager

To launch the Android SDK Manager, first open a command prompt and navigate to the android SDK tools folder. With Appmethod / RAD Studio they will be installed by default in the public documents. You can reach them using the cd command. e.g.

cd \Users\Public\Documents\Embarcadero\Studio\14.0\PlatformSDKs\adt-bundle-windows-x86-20131030\sdk\tools

From the SDK tools folder type in Android and hit enter and the SDK manager will open.

Installing the USB Drivers

With the Android SDK Manager launched you can now see the packages installed / to update. Specifically we are looking for the Google USB Driver. (see image below) Once found, choose “Install xx packages” button at the bottom of the screen.

Android SDK Manager, USB Drivers ready to update
Android SDK Manager, USB Drivers ready to update

Note: If you don’t see it, then install any packages that are pending update, close and re-open. It should then connect back to the Internet and refresh the list and make the Google USB Driver visible ready to install.

Updating the device driver

Once you have the drivers on your machine, the next step is to update the driver back in the device manager.

Update Driver in Device Manager
Update Driver in Device Manager

Choose to browse the computer and go back to the PlatformSDKs folder (path above) and choose to search sub folders.

Set path to find Android SDK for Appmethod / RAD Studio
Set path to find Android SDK for Appmethod / RAD Studio

If you have not already said to “always trust” google inc. You should see immediately be asked to confirm installing the driver.

Confirm Install
Confirm Install

Once installed the Android Composite ADB Interface driver will be installed and you will get confirmation.

Confirmed, Driver Installed
Confirmed, Driver Installed

At this point, you should be able to see in the device manager the device is installed.

Android Device Installed
Android Device Installed

With the phone drivers installed, it is now time to check the ADB command line can see your device. If it can, then you are ready to use it for development.

Checking ADB can see your Android device

To check that ADB can see your device, its time to head back to the command line and run the ADB application.  ADB.exe lives in the ..sdk\platform-tools folder. From the command line you should be able to get there with something like…

cd \Users\Public\Documents\Embarcadero\Studio\14.0\PlatformSDKs\adt-bundle-windows-x86-20131030\sdk\platform-tools

Once your command prompt window is open at Platform Tools, run the following command to view the connected devices.

adb devices

The following screen below shows the output. Here I have my android device connected, but as yet I have not confirmed the connection on my phone as being allowed This causes the device to show as unauthorized.

Un-authroised Device
Un-authroised Device

If the android device shows as unauthorized then unlock the phone and choose to Allow USB debugging.

Approving Debugging for Android
Approving Debugging for Android

Once the device is approved, you should be able to re-run the ADB devices command and see the device id nows shows the status of “device”

ADB for approved device
ADB for approved device

At this point you are ready to use your android device for development! Refresh your Android devices in your IDE (right click on Android as a Target platform in the Project Manager and it will become visible.

Screen Shot 2014-07-30 at 17.13.58