Coherent UI  2.5.3
A modern user interface library for games
 All Classes Namespaces Functions Variables Enumerations Enumerator Pages
Child Windows

Child windows support

Coherent UI allows clients to choose which child(popup) windows should be opened and which should not. Whenever Coherent UI detects a new window to be opened, a callback for the View Listener is executed. This callback method should return a boolean value that either allows or disallows the creation of the new window. If the child window is to be allowed, the client must supply some initialization data (e.g. the width and height of the new window) and a new View Listener that is responsible for the child window's callbacks.

Warning
Currently child windows are not compatible with on-demand views. Trying to open a child window in an on-demand view might result in blocking the client application.

Child windows creation events in C++

When a new child window is to be opened, the client is notified via the Coherent::UI::ViewListener::OnCanCreateChildWindow event. The client is supplied with the opener and target URLs so she can do some filtering based on them. If the creation should be disallowed, the client should just return false in the callback. If is should be allowed, the client should return true and fill in the Coherent::UI::ChildViewInfo structure with the needed data. This data is a Coherent::UI::ViewInfo structure and a Coherent::UI::ViewListenerBase which should be a newly created View Listener that will receive the notifications for the new window.

Example (C++)

class ChildWindowExampleViewListener : public Coherent::UI::ViewListener
{
public:
// ... omitted for brevity
virtual bool OnCanCreateChildWindow(const wchar_t* openerUrl, const wchar_t* targetUrl, Coherent::UI::ChildViewInfo& childViewInfo) OVERRIDE
{
if (!AllowChildWindowRequest(openerUrl, targetUrl))
{
return false;
}
// Create a new view listener
std::unique_ptr<ChildWindowExampleViewListener> listener(new ChildWindowExampleViewListener());
childViewInfo.Listener = listener.get();
// Save the listener in some global collection
g_ViewListenersCollection.push_back(std::move(listener));
// Fill in the ViewInfo
childViewInfo.NewViewInfo->Width = 1280;
childViewInfo.NewViewInfo->Height = 720;
childViewInfo.NewViewInfo->UsesSharedMemory = true;
childViewInfo.NewViewInfo->IsTransparent = false;
childViewInfo.NewViewInfo->SupportClickThrough = false;
#ifndef COHERENT_UI_LIMITED_VERSION
childViewInfo.NewViewInfo->TargetFrameRate = 60;
childViewInfo.NewViewInfo->IsOnDemand = false;
childViewInfo.NewViewInfo->ControlTimeOnDemand = false;
#endif
return true;
}
private:
bool AllowChildWindowRequest(const wchar_t* openerUrl, const wchar_t* targetUrl)
{
bool allow = true;
// Do some filtering based on the opener and target URLs...
// allow = ...
return allow;
}
};

Child windows creation events in C#

When a new child window is to be opened, the client is notified via the Coherent.UI.ViewListener.OnCanCreateChildWindow event. The client is supplied with the opener and target URLs so she can do some filtering based on them. If the creation should be disallowed, the client should just return false in the callback. If is should be allowed, the client should return true and fill in the Coherent.UI.ChildViewInfo structure with the needed data. This data is a Coherent.UI.ViewInfo structure and a Coherent.UI.ViewListenerBase which should be a newly created View Listener that will receive the notifications for the new window.

Example (C#)

class ChildWindowExampleViewListener : public Coherent::UI::ViewListener
{
// ... omitted for brevity
public override bool OnCanCreateChildWindow(string openerUrl, string targetUrl, ChildViewInfo childViewInfo)
{
Console.WriteLine("{0} trying to open popup: {1}", openerUrl, targetUrl);
// Create a new view listener
var listener = new ChildWindowExampleViewListener();
childViewInfo.Listener = listener;
// Save the listener in some global collection
g_ViewListenersCollection.Add(listener);
childViewInfo.NewViewInfo.Width = 1280;
childViewInfo.NewViewInfo.Height = 720;
childViewInfo.NewViewInfo.UsesSharedMemory = false;
return true;
}
private bool AllowChildWindowRequest(string openerUrl, string targetUrl)
{
bool allow = true;
// Do some filtering based on the opener and target URLs...
// allow = ...
return allow;
}
}

Child windows creation events for .NET controls

When a new child window is to be opened, the client is notified via the BrowserViewControl's PopupWindowRequested event. The client is supplied with the opener and target URLs so she can do some filtering based on them. By default new window requests are disallowed so if you do nothing in the event handler, no new windows will be created. If the creation should be allowed, the client should call the BrowserViewControl.PopupWindowResponse.Allow method. This method will return a new instance of a BrowserViewControl that the client can place in a suitable location in the form.

Example (.NET Windows Forms control)

public partial class MainForm : Form
{
private void viewControlWinForms1_PopupWindowRequested(string openerUrl, string targetUrl, BrowserViewControl.PopupWindowResponse response)
{
if (AllowPopup(openerUrl, targetUrl))
{
BrowserViewControl ctrl = response.Allow();
this.Controls.Add(ctrl);
this.components.Add(ctrl); // Add new control to the components collection so it's disposed on exit
}
}
// In the autogenerated InitializeComponent() method...
private void InitializeComponent()
{
// ...
this.viewControlWinForms1.PopupWindowRequested += new Coherent.UI.Windows.Forms.BrowserViewControl.PopupWindowRequestHandler(this.viewControlWinForms1_PopupWindowRequested);
// ...
}
}

Example (.NET WPF control)

public partial class MainWindow : Window
{
private void viewControl_PopupWindowRequested(string openerUrl, string targetUrl, BrowserViewControl.PopupWindowResponse response)
{
if (AllowPopup(openerUrl, targetUrl))
{
// Add the control. Here we assume the layout is composed using a Grid
// and we want to add the new control in a new row at the bottom
BrowserViewControl ctrl = response.Allow();
this.gridMain.Children.Add(ctrl);
// BrowserViewControls are automatically disposed when the Dispatcher.ShutdownStarted event is fired
// Add a new row to accomodate the control
var rowDef = new RowDefinition();
rowDef.Height = new GridLength(1, GridUnitType.Star);
this.gridMain.RowDefinitions.Add(rowDef);
// Place the control in the added row
Grid.SetRow(ctrl, this.gridMain.RowDefinitions.Count - 1);
}
}
}
<coui:BrowserViewControl Name="viewControl" ViewWidth="640" ViewHeight="480" InitialUrl="http://www.w3schools.com/js/js_popup.asp"
Grid.Column="0" Grid.Row="2" PopupWindowRequested="viewControl_PopupWindowRequested" UsesOwnUISystem="True" />

Example (.NET GTK# control)

public partial class MainWindow: Gtk.Window
{
private System.Collections.Generic.List<IDisposable> m_TrackedComponents;
public MainWindow (): base (Gtk.WindowType.Toplevel)
{
// ... omitted for brevity
// Initialize the tracked components collection
m_TrackedComponents = new System.Collections.Generic.List<IDisposable>();
// Add any static controls...
m_TrackedComponents.Add(viewControl1);
m_TrackedComponents.Add(viewControl2);
}
private void CleanTrackedComponents()
{
foreach (IDisposable item in m_TrackedComponents) {
item.Dispose();
}
}
protected void OnDeleteEvent (object sender, DeleteEventArgs a)
{
CleanTrackedComponents();
Application.Quit ();
a.RetVal = true;
}
protected void OnNewWindow (string openerUrl, string targetUrl, Coherent.UI.Mono.Gtk.BrowserViewControl.PopupWindowResponse response)
{
if (AllowPopup(openerUrl, targetUrl))
{
// Here we assume the layout is composed using a Fixed container for the main window
BrowserViewControl ctrl = response.Allow();
m_TrackedComponents.Add(ctrl);
this.fixed1.Put(ctrl, 10, 40);
ctrl.Show();
}
}
// In the autogenerated Build() method...
protected virtual void Build ()
{
// ...
this.DeleteEvent += new global::Gtk.DeleteEventHandler (this.OnDeleteEvent);
// ...
this.viewControl1.PopupWindowRequested += new global::Coherent.UI.Mono.Gtk.BrowserViewControl.PopupWindowRequestHandler (this.OnNewWindow);
// ...
}
}