1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * gtkD is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with gtkD; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 
20 // generated automatically - do not change
21 // find conversion definition on APILookup.txt
22 // implement new conversion functionalities on the wrap.utils pakage
23 
24 
25 module gtk.Socket;
26 
27 private import gdk.Window;
28 private import glib.ConstructionException;
29 private import gobject.ObjectG;
30 private import gobject.Signals;
31 private import gtk.Container;
32 private import gtk.Widget;
33 private import gtkc.gtk;
34 public  import gtkc.gtktypes;
35 private import std.algorithm;
36 
37 
38 /**
39  * Together with #GtkPlug, #GtkSocket provides the ability to embed
40  * widgets from one process into another process in a fashion that
41  * is transparent to the user. One process creates a #GtkSocket widget
42  * and passes that widget’s window ID to the other process, which then
43  * creates a #GtkPlug with that window ID. Any widgets contained in the
44  * #GtkPlug then will appear inside the first application’s window.
45  * 
46  * The socket’s window ID is obtained by using gtk_socket_get_id().
47  * Before using this function, the socket must have been realized,
48  * and for hence, have been added to its parent.
49  * 
50  * ## Obtaining the window ID of a socket.
51  * 
52  * |[<!-- language="C" -->
53  * GtkWidget *socket = gtk_socket_new ();
54  * gtk_widget_show (socket);
55  * gtk_container_add (GTK_CONTAINER (parent), socket);
56  * 
57  * // The following call is only necessary if one of
58  * // the ancestors of the socket is not yet visible.
59  * gtk_widget_realize (socket);
60  * g_print ("The ID of the sockets window is %#x\n",
61  * gtk_socket_get_id (socket));
62  * ]|
63  * 
64  * Note that if you pass the window ID of the socket to another
65  * process that will create a plug in the socket, you must make
66  * sure that the socket widget is not destroyed until that plug
67  * is created. Violating this rule will cause unpredictable
68  * consequences, the most likely consequence being that the plug
69  * will appear as a separate toplevel window. You can check if
70  * the plug has been created by using gtk_socket_get_plug_window().
71  * If it returns a non-%NULL value, then the plug has been
72  * successfully created inside of the socket.
73  * 
74  * When GTK+ is notified that the embedded window has been destroyed,
75  * then it will destroy the socket as well. You should always,
76  * therefore, be prepared for your sockets to be destroyed at any
77  * time when the main event loop is running. To prevent this from
78  * happening, you can connect to the #GtkSocket::plug-removed signal.
79  * 
80  * The communication between a #GtkSocket and a #GtkPlug follows the
81  * [XEmbed Protocol](http://www.freedesktop.org/Standards/xembed-spec).
82  * This protocol has also been implemented in other toolkits, e.g. Qt,
83  * allowing the same level of integration when embedding a Qt widget
84  * in GTK or vice versa.
85  * 
86  * The #GtkPlug and #GtkSocket widgets are only available when GTK+
87  * is compiled for the X11 platform and %GDK_WINDOWING_X11 is defined.
88  * They can only be used on a #GdkX11Display. To use #GtkPlug and
89  * #GtkSocket, you need to include the `gtk/gtkx.h` header.
90  */
91 public class Socket : Container
92 {
93 	/** the main Gtk struct */
94 	protected GtkSocket* gtkSocket;
95 
96 	/** Get the main Gtk struct */
97 	public GtkSocket* getSocketStruct(bool transferOwnership = false)
98 	{
99 		if (transferOwnership)
100 			ownedRef = false;
101 		return gtkSocket;
102 	}
103 
104 	/** the main Gtk struct as a void* */
105 	protected override void* getStruct()
106 	{
107 		return cast(void*)gtkSocket;
108 	}
109 
110 	protected override void setStruct(GObject* obj)
111 	{
112 		gtkSocket = cast(GtkSocket*)obj;
113 		super.setStruct(obj);
114 	}
115 
116 	/**
117 	 * Sets our main struct and passes it to the parent class.
118 	 */
119 	public this (GtkSocket* gtkSocket, bool ownedRef = false)
120 	{
121 		this.gtkSocket = gtkSocket;
122 		super(cast(GtkContainer*)gtkSocket, ownedRef);
123 	}
124 
125 
126 	/** */
127 	public static GType getType()
128 	{
129 		return gtk_socket_get_type();
130 	}
131 
132 	/**
133 	 * Create a new empty #GtkSocket.
134 	 *
135 	 * Returns: the new #GtkSocket.
136 	 *
137 	 * Throws: ConstructionException GTK+ fails to create the object.
138 	 */
139 	public this()
140 	{
141 		auto p = gtk_socket_new();
142 		
143 		if(p is null)
144 		{
145 			throw new ConstructionException("null returned by new");
146 		}
147 		
148 		this(cast(GtkSocket*) p);
149 	}
150 
151 	/**
152 	 * Adds an XEMBED client, such as a #GtkPlug, to the #GtkSocket.  The
153 	 * client may be in the same process or in a different process.
154 	 *
155 	 * To embed a #GtkPlug in a #GtkSocket, you can either create the
156 	 * #GtkPlug with `gtk_plug_new (0)`, call
157 	 * gtk_plug_get_id() to get the window ID of the plug, and then pass that to the
158 	 * gtk_socket_add_id(), or you can call gtk_socket_get_id() to get the
159 	 * window ID for the socket, and call gtk_plug_new() passing in that
160 	 * ID.
161 	 *
162 	 * The #GtkSocket must have already be added into a toplevel window
163 	 * before you can make this call.
164 	 *
165 	 * Params:
166 	 *     window = the Window of a client participating in the XEMBED protocol.
167 	 */
168 	public void addId(ulong window)
169 	{
170 		gtk_socket_add_id(gtkSocket, window);
171 	}
172 
173 	/**
174 	 * Gets the window ID of a #GtkSocket widget, which can then
175 	 * be used to create a client embedded inside the socket, for
176 	 * instance with gtk_plug_new().
177 	 *
178 	 * The #GtkSocket must have already be added into a toplevel window
179 	 * before you can make this call.
180 	 *
181 	 * Returns: the window ID for the socket
182 	 */
183 	public ulong getId()
184 	{
185 		return gtk_socket_get_id(gtkSocket);
186 	}
187 
188 	/**
189 	 * Retrieves the window of the plug. Use this to check if the plug has
190 	 * been created inside of the socket.
191 	 *
192 	 * Returns: the window of the plug if
193 	 *     available, or %NULL
194 	 *
195 	 * Since: 2.14
196 	 */
197 	public Window getPlugWindow()
198 	{
199 		auto p = gtk_socket_get_plug_window(gtkSocket);
200 		
201 		if(p is null)
202 		{
203 			return null;
204 		}
205 		
206 		return ObjectG.getDObject!(Window)(cast(GdkWindow*) p);
207 	}
208 
209 	protected class OnPlugAddedDelegateWrapper
210 	{
211 		static OnPlugAddedDelegateWrapper[] listeners;
212 		void delegate(Socket) dlg;
213 		gulong handlerId;
214 		
215 		this(void delegate(Socket) dlg)
216 		{
217 			this.dlg = dlg;
218 			this.listeners ~= this;
219 		}
220 		
221 		void remove(OnPlugAddedDelegateWrapper source)
222 		{
223 			foreach(index, wrapper; listeners)
224 			{
225 				if (wrapper.handlerId == source.handlerId)
226 				{
227 					listeners[index] = null;
228 					listeners = std.algorithm.remove(listeners, index);
229 					break;
230 				}
231 			}
232 		}
233 	}
234 
235 	/**
236 	 * This signal is emitted when a client is successfully
237 	 * added to the socket.
238 	 */
239 	gulong addOnPlugAdded(void delegate(Socket) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
240 	{
241 		auto wrapper = new OnPlugAddedDelegateWrapper(dlg);
242 		wrapper.handlerId = Signals.connectData(
243 			this,
244 			"plug-added",
245 			cast(GCallback)&callBackPlugAdded,
246 			cast(void*)wrapper,
247 			cast(GClosureNotify)&callBackPlugAddedDestroy,
248 			connectFlags);
249 		return wrapper.handlerId;
250 	}
251 	
252 	extern(C) static void callBackPlugAdded(GtkSocket* socketStruct, OnPlugAddedDelegateWrapper wrapper)
253 	{
254 		wrapper.dlg(wrapper.outer);
255 	}
256 	
257 	extern(C) static void callBackPlugAddedDestroy(OnPlugAddedDelegateWrapper wrapper, GClosure* closure)
258 	{
259 		wrapper.remove(wrapper);
260 	}
261 
262 	protected class OnPlugRemovedDelegateWrapper
263 	{
264 		static OnPlugRemovedDelegateWrapper[] listeners;
265 		bool delegate(Socket) dlg;
266 		gulong handlerId;
267 		
268 		this(bool delegate(Socket) dlg)
269 		{
270 			this.dlg = dlg;
271 			this.listeners ~= this;
272 		}
273 		
274 		void remove(OnPlugRemovedDelegateWrapper source)
275 		{
276 			foreach(index, wrapper; listeners)
277 			{
278 				if (wrapper.handlerId == source.handlerId)
279 				{
280 					listeners[index] = null;
281 					listeners = std.algorithm.remove(listeners, index);
282 					break;
283 				}
284 			}
285 		}
286 	}
287 
288 	/**
289 	 * This signal is emitted when a client is removed from the socket.
290 	 * The default action is to destroy the #GtkSocket widget, so if you
291 	 * want to reuse it you must add a signal handler that returns %TRUE.
292 	 *
293 	 * Returns: %TRUE to stop other handlers from being invoked.
294 	 */
295 	gulong addOnPlugRemoved(bool delegate(Socket) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
296 	{
297 		auto wrapper = new OnPlugRemovedDelegateWrapper(dlg);
298 		wrapper.handlerId = Signals.connectData(
299 			this,
300 			"plug-removed",
301 			cast(GCallback)&callBackPlugRemoved,
302 			cast(void*)wrapper,
303 			cast(GClosureNotify)&callBackPlugRemovedDestroy,
304 			connectFlags);
305 		return wrapper.handlerId;
306 	}
307 	
308 	extern(C) static int callBackPlugRemoved(GtkSocket* socketStruct, OnPlugRemovedDelegateWrapper wrapper)
309 	{
310 		return wrapper.dlg(wrapper.outer);
311 	}
312 	
313 	extern(C) static void callBackPlugRemovedDestroy(OnPlugRemovedDelegateWrapper wrapper, GClosure* closure)
314 	{
315 		wrapper.remove(wrapper);
316 	}
317 }