# Delphi

Source: https://docs.interop.io/desktop/capabilities/data-sharing/channels/delphi/index.html

## Overview

> ℹ️ *See the [Delphi 10](https://github.com/InteropIO/native-examples/tree/main/glue-com/GlueDelphi) and [Delphi 7](https://github.com/InteropIO/native-examples/tree/main/glue-com/GlueDelphi7) examples on GitHub.*

The io.Connect Channels are enabled by default when registering a VBA window as an io.Connect Window. You have to provide handler implementations for the Channel related events that will be raised due to interaction with the io.Connect Window instance - [changing Channels](#handling_channel_change) and [handling Channel data updates](#subscribing_for_channel_updates). When the io.Connect Channels are enabled, a Channel Selector box is available in the window title bar.

To check whether the Channel Selector is visible, use the [`GetChannelSupport`](https://docs.interop.io/desktop/getting-started/how-to/interop-enable-your-apps/delphi/index.md#interfaces-igluewindow-getchannelsupport) method of an [`IGlueWindow`](https://docs.interop.io/desktop/getting-started/how-to/interop-enable-your-apps/delphi/index.md#interfaces-igluewindow) instance. To hide or show the Channel Selector, use the [`SetChannelSupport`](https://docs.interop.io/desktop/getting-started/how-to/interop-enable-your-apps/delphi/index.md#interfaces-igluewindow-setchannelsupport).

> ⚠️ *Note that, currently, the `SetChannelSupport` method only hides or shows the Channel Selector, but doesn't enable or disable the Channel support. This can be done only during window registration.*

```delphi
// Toggle the visibility of the Channel Selector.
if  glueWin.GetChannelSupport() then
  glueWin.SetChannelSupport(False)
else
  glueWin.SetChannelSupport(True);
```

## Handling Channel Change

To handle the event when the user changes the Channel via the Channel Selector, use the [`HandleChannelChanged`](https://docs.interop.io/desktop/getting-started/how-to/interop-enable-your-apps/delphi/index.md#interfaces-igluewindoweventhandler-handlechannelchanged) method of the [`IGlueWindowEventHandler`](https://docs.interop.io/desktop/getting-started/how-to/interop-enable-your-apps/delphi/index.md#interfaces-igluewindoweventhandler) interface:

```delphi
function TMainForm.HandleChannelChanged(const glueWindow: IGlueWindow; const channel: IGlueContext; prevChannel: GlueContext): HResult;
begin
  if not Assigned(channel) then
  begin
    // Previous Channel deselected, no new Channel selected.
    ...
  end
  else
  begin
    // New Channel selected.
    ...
  end;
  Result := S_OK;
end;
```

## Subscribing for Channel Updates

To handle the event when the data in the current Channel is updated or the user selects a new Channel, use the [`HandleChannelData`](https://docs.interop.io/desktop/getting-started/how-to/interop-enable-your-apps/delphi/index.md#interfaces-igluewindoweventhandler-handlechanneldata) method of the [`IGlueWindowEventHandler`](https://docs.interop.io/desktop/getting-started/how-to/interop-enable-your-apps/delphi/index.md#interfaces-igluewindoweventhandler) interface:

```delphi
function TMainForm.HandleChannelData(const glueWindow: IGlueWindow; const channelUpdate: IGlueContextUpdate): HResult;
var
  channel: IGlueContext;
  data: TGlueContextValueArray;
begin
  channel := channelUpdate.GetContext();
  // Get the Channel data in a native data structure.
  rawData := channel.GetData();
  data := SA_AsTranslatedContextValues(rawData);
  ...
  SafeArrayDestroy(rawData);
  Result := S_OK;
end;
```

## Publishing Data

To publish data to a Channel, you must update the shared context object dedicated to the respective Channel.

> ℹ️ *For details on how to update a shared context object, see the [`Shared Contexts > Delphi > Updating a Context`](https://docs.interop.io/desktop/capabilities/data-sharing/shared-contexts/delphi/index.md#updating_contexts) section.*

> ⚠️ *Note that you must only update the value of the `"data"` property of the context object. The `"name"` and `"meta"` properties must not be modified, because they are specifically set for each Channel.*
