Introduction to platform-sensitive development

From Free Pascal wiki
Jump to: navigation, search
Comparison of platform-independent with platform-sensitive applications on Mac OS X Leopard. All shown applications are platform-independent, but only the programs in the right half of the screen are platform-sensitive.
Comparison of platform-independent with platform-sensitive applications on Mac OS X Leopard. All shown applications are platform-independent, but only the programs in the right half of the screen are platform-sensitive. Note that the platform-insensitive X Window programs in the left half of the screen have an alien look. They tend to confuse the users with atypical behaviour and ambiguous elements like a double menu bar.

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.

Platform-sensitive development is more than adherence to user-interface style guides. It also addresses aspects that are as different as memory management, file handling and interprocess communication.

This article provides the reader with essential information and hints for platform-sensitive development.

Prelude

Stock-dialog-warning.svg

This article applies to platform-sensitive development only.

See also: Multiplatform Programming Guide

Most adaptations for the specific target platforms are performed automatically by the LCL, FCL, RTL and the respective widgetsets. Often, only minor modifications are needed to create a finished application. Using conditional compilation by means of compiler directives helps to keep your code portable.

The following example demonstrates this technique. The procedure 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 users of the respective platform.

procedure AdaptMenus;
{ Adapts Menus and Shortcuts to the interface style guidelines
  of the respective operating system }
var
  modifierKey: TShiftState;
begin
  {$IFDEF LCLcarbon}
  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}
  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, ssModifier); //ssModifier equals ssMeta on Mac, ssCtrl on other platforms
  MainForm.OpenMenuItem.ShortCut := ShortCut(VK_O, ssModifier);
  MainForm.CloseMenuItem.ShortCut := ShortCut(VK_W, ssModifier);
  MainForm.SaveItem.ShortCut := ShortCut(VK_S, ssModifier);
  MainForm.PrintItem.ShortCut := ShortCut(VK_P, ssModifier);
  MainForm.QuitMenuItem.ShortCut := ShortCut(VK_Q, ssModifier);
  MainForm.UndoMenuItem.ShortCut := ShortCut(VK_Z, ssModifier);
  MainForm.CutMenuItem.ShortCut := ShortCut(VK_X, ssModifier);
  MainForm.CopyMenuItem.ShortCut := ShortCut(VK_C, ssModifier);
  MainForm.PasteMenuItem.ShortCut := ShortCut(VK_V, ssModifier);
  MainForm.SelectAllMenuItem.ShortCut := ShortCut(VK_A, ssModifier);
end;

In older Lazarus versions (before 1.0) ssModifier is not supported. The following code is compatible with Lazarus 0.9.x, albeit mor complex than the example above:

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;
Note-icon.png

Note: To see if LCL functionality is supported on your target widgetset, you can use the Restriction Browser (menu View/Restriction Browser)

Platform-specific hints

Android

Android is an atypical Linux variant, therefore many considerations for usual Linux distributions do not apply. Furthermore, development for Android is faced by the challenge that the software landscape is fragmented in a large field of different system versions and by a considerable heterogeneity of hardware among supported devices.

See also

External Links

iOS

iOS is a pruned variant of OS X for mobile devices. Recent additions to the Lazarus evironment help to develop applications with Free Pascal for iOS now. The development for iOS differs in some sense from that for other platforms, however thanks to the iOS Designer it is possible to graphically design the GUI. For submission to Apple's App Store it is mandatory to implement the iOS Human Interface Guidelines.

See also

External Links

Linux

Linux is less standardized than other operating systems. This is a consequence of both heterogeneity of distributions and the fact that there is no "standard" desktop environment for Linux. GNOME, KDE, LXDE and other desktop environments differ in their recommendations for GUI design.

See also

External links

Mac OS X

Among all operating systems the GUI of Mac OS X relies on the richest tradition. It dates back to classical Mac OS from 1984 and its less successful predecessors Lisa OS (1983), Xerox Star (1981) and Xerox Alto (1973). Today, many regard the Mac OS X GUI as delivering the best user experience of all platforms. This is payed off by a more complex development process, however.

Compared with other operating systems many issues have been differently implemented on Mac OS X. This covers design and location of the menu bar, the position of some standard menus and the style of buttons and icons. Also non-GUI features differ on Mac OS X, e.g. the architecture of programs, the location of preferences files and interprocess communication.

Many of these issues are automatically cared for by the Carbon and Cocoa widgetsets, so that it is easy to develop a simple program for Mac OS, often without any change of the source code. Some fine-tuning is still necessary to develop a finished Mac application, however.

See also

External Links

Windows

Windows is one of the oldest platforms supported by Lazarus. Therefore, most applications generated by the IDE are automatically in good agreement with the requirements of usability engineering. However, the spectrum of Windows implementations is very wide today, covering mobile operating systems like Windows CE or Windows mobile, desktop environments like Windows 2000 or Windows 8, and Server platforms. Therefore, it may be advisable to fine-tune applications for special environments and their GUI guidelines.

See also

External links

See also