Introduction to platform-sensitive development

From Free Pascal wiki
Jump to navigationJump to search

Platform-sensitive development is the next step in cross-platform development. It extends the process of porting an application by implementing platform-specific functions and user-interface style guides.

Thanks to the "Write Once - Compile Anywhere" principle of Lazarus and Free Pascal it is very easy to develop a program that runs out of the box on multiple platforms. To make a finished application that adheres to the respective GUI style guidelines (and even the requirements of app stores) warrants additional steps, however.

This article is to provide the reader with essential information and hints for platform-sensitive development.

Prelude

Most adaptations for the specific target platforms are performed automatically by the LCL, FCL, RTL and the respective widget sets. It are minor modifications that are needed to create a finished application for your target platform. To use conditional compilation by means of compiler directives helps to keep your code portable.

The following example demonstrates this technique. The procedures AdaptMenus adjusts the display of the menu bar according to the target platform. This is necessary as the positions of the "About" and "Preferences" items are on different locations in Mac OS than they are in Windows or Linux. Additionally, the menu shortcuts are invoked by the command key on Mac OS (in the LCL referred to as Meta key), while they are triggered with the control key on most other operating systems.

The main menu of the form contains some platform-specific items, WinAboutItem (it opens an info box) and WinPreferencesItem (for program settings). They are intended for use on Non-Mac operating systems. On the other side, it contains an Apple Menu for Mac OS X. Depending from the target platform, the code hides unneeded entries (and corresponding dividers) and shows those that are expected by the user.

procedure AdaptMenus;
{ Adapts Menus and Shortcuts to the interface style guidelines
  of the respective operating system }
var
  modifierKey: TShiftState;
begin
  {$IFDEF LCLcarbon}
  modifierKey := [ssMeta];
  MainForm.WinAboutItem.Visible := False;
  MainForm.Divider_5_1.Visible := False;
  MainForm.Divider_2_2.Visible := False;
  MainForm.WinPreferencesItem.Visible := False;
  MainForm.AppleMenu.Visible := True;
  {$ELSE}
  modifierKey := [ssCtrl];
  MainForm.WinAboutItem.Visible := True;
  MainForm.Divider_5_1.Visible := True;
  MainForm.Divider_2_2.Visible := True;
  MainForm.WinPreferencesItem.Visible := True;
  MainForm.AppleMenu.Visible := False;
  {$ENDIF}
  MainForm.NewMenuItem.ShortCut := ShortCut(VK_N, modifierKey);
  MainForm.OpenMenuItem.ShortCut := ShortCut(VK_O, modifierKey);
  MainForm.CloseMenuItem.ShortCut := ShortCut(VK_W, modifierKey);
  MainForm.SaveItem.ShortCut := ShortCut(VK_S, modifierKey);
  MainForm.PrintItem.ShortCut := ShortCut(VK_P, modifierKey);
  MainForm.QuitMenuItem.ShortCut := ShortCut(VK_Q, modifierKey);
  MainForm.UndoMenuItem.ShortCut := ShortCut(VK_Z, modifierKey);
  MainForm.CutMenuItem.ShortCut := ShortCut(VK_X, modifierKey);
  MainForm.CopyMenuItem.ShortCut := ShortCut(VK_C, modifierKey);
  MainForm.PasteMenuItem.ShortCut := ShortCut(VK_V, modifierKey);
  MainForm.SelectAllMenuItem.ShortCut := ShortCut(VK_A, modifierKey);
end;

Platform-specific hints

Android

To be continued ...

See also

External Links

iOS

To be continued ...

External Links

Linux

To be continued ...

External links

Mac OS X

To be continued ...

See also

External Links

Windows

To be continued ...

External links

See also