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.Plug;
26 
27 private import gdk.Display;
28 private import gdk.Window : GdkWin = Window;
29 private import glib.ConstructionException;
30 private import gobject.ObjectG;
31 private import gobject.Signals;
32 private import gtk.Widget;
33 private import gtk.Window;
34 public  import gtkc.gdktypes;
35 private import gtkc.gtk;
36 public  import gtkc.gtktypes;
37 private import std.algorithm;
38 
39 
40 /**
41  * Together with #GtkSocket, #GtkPlug provides the ability to embed
42  * widgets from one process into another process in a fashion that is
43  * transparent to the user. One process creates a #GtkSocket widget
44  * and passes the ID of that widget’s window to the other process,
45  * which then creates a #GtkPlug with that window ID. Any widgets
46  * contained in the #GtkPlug then will appear inside the first
47  * application’s window.
48  * 
49  * The communication between a #GtkSocket and a #GtkPlug follows the
50  * [XEmbed Protocol](http://www.freedesktop.org/Standards/xembed-spec).
51  * This protocol has also been implemented in other toolkits, e.g. Qt,
52  * allowing the same level of integration when embedding a Qt widget
53  * in GTK+ or vice versa.
54  * 
55  * The #GtkPlug and #GtkSocket widgets are only available when GTK+
56  * is compiled for the X11 platform and %GDK_WINDOWING_X11 is defined.
57  * They can only be used on a #GdkX11Display. To use #GtkPlug and
58  * #GtkSocket, you need to include the `gtk/gtkx.h` header.
59  */
60 public class Plug : Window
61 {
62 	/** the main Gtk struct */
63 	protected GtkPlug* gtkPlug;
64 
65 	/** Get the main Gtk struct */
66 	public GtkPlug* getPlugStruct()
67 	{
68 		return gtkPlug;
69 	}
70 
71 	/** the main Gtk struct as a void* */
72 	protected override void* getStruct()
73 	{
74 		return cast(void*)gtkPlug;
75 	}
76 
77 	protected override void setStruct(GObject* obj)
78 	{
79 		gtkPlug = cast(GtkPlug*)obj;
80 		super.setStruct(obj);
81 	}
82 
83 	/**
84 	 * Sets our main struct and passes it to the parent class.
85 	 */
86 	public this (GtkPlug* gtkPlug, bool ownedRef = false)
87 	{
88 		this.gtkPlug = gtkPlug;
89 		super(cast(GtkWindow*)gtkPlug, ownedRef);
90 	}
91 
92 
93 	/** */
94 	public static GType getType()
95 	{
96 		return gtk_plug_get_type();
97 	}
98 
99 	/**
100 	 * Creates a new plug widget inside the #GtkSocket identified
101 	 * by @socket_id. If @socket_id is 0, the plug is left “unplugged” and
102 	 * can later be plugged into a #GtkSocket by  gtk_socket_add_id().
103 	 *
104 	 * Params:
105 	 *     socketId = the window ID of the socket, or 0.
106 	 *
107 	 * Return: the new #GtkPlug widget.
108 	 *
109 	 * Throws: ConstructionException GTK+ fails to create the object.
110 	 */
111 	public this(ulong socketId)
112 	{
113 		auto p = gtk_plug_new(socketId);
114 		
115 		if(p is null)
116 		{
117 			throw new ConstructionException("null returned by new");
118 		}
119 		
120 		this(cast(GtkPlug*) p);
121 	}
122 
123 	/**
124 	 * Create a new plug widget inside the #GtkSocket identified by socket_id.
125 	 *
126 	 * Params:
127 	 *     display = the #GdkDisplay on which @socket_id is displayed
128 	 *     socketId = the XID of the socket’s window.
129 	 *
130 	 * Return: the new #GtkPlug widget.
131 	 *
132 	 * Since: 2.2
133 	 *
134 	 * Throws: ConstructionException GTK+ fails to create the object.
135 	 */
136 	public this(Display display, ulong socketId)
137 	{
138 		auto p = gtk_plug_new_for_display((display is null) ? null : display.getDisplayStruct(), socketId);
139 		
140 		if(p is null)
141 		{
142 			throw new ConstructionException("null returned by new_for_display");
143 		}
144 		
145 		this(cast(GtkPlug*) p);
146 	}
147 
148 	/**
149 	 * Finish the initialization of @plug for a given #GtkSocket identified by
150 	 * @socket_id. This function will generally only be used by classes deriving from #GtkPlug.
151 	 *
152 	 * Params:
153 	 *     socketId = the XID of the socket’s window.
154 	 */
155 	public void construct(ulong socketId)
156 	{
157 		gtk_plug_construct(gtkPlug, socketId);
158 	}
159 
160 	/**
161 	 * Finish the initialization of @plug for a given #GtkSocket identified by
162 	 * @socket_id which is currently displayed on @display.
163 	 * This function will generally only be used by classes deriving from #GtkPlug.
164 	 *
165 	 * Params:
166 	 *     display = the #GdkDisplay associated with @socket_id’s
167 	 *         #GtkSocket.
168 	 *     socketId = the XID of the socket’s window.
169 	 *
170 	 * Since: 2.2
171 	 */
172 	public void constructForDisplay(Display display, ulong socketId)
173 	{
174 		gtk_plug_construct_for_display(gtkPlug, (display is null) ? null : display.getDisplayStruct(), socketId);
175 	}
176 
177 	/**
178 	 * Determines whether the plug is embedded in a socket.
179 	 *
180 	 * Return: %TRUE if the plug is embedded in a socket
181 	 *
182 	 * Since: 2.14
183 	 */
184 	public bool getEmbedded()
185 	{
186 		return gtk_plug_get_embedded(gtkPlug) != 0;
187 	}
188 
189 	/**
190 	 * Gets the window ID of a #GtkPlug widget, which can then
191 	 * be used to embed this window inside another window, for
192 	 * instance with gtk_socket_add_id().
193 	 *
194 	 * Return: the window ID for the plug
195 	 */
196 	public ulong getId()
197 	{
198 		return gtk_plug_get_id(gtkPlug);
199 	}
200 
201 	/**
202 	 * Retrieves the socket the plug is embedded in.
203 	 *
204 	 * Return: the window of the socket, or %NULL
205 	 *
206 	 * Since: 2.14
207 	 */
208 	public GdkWin getSocketWindow()
209 	{
210 		auto p = gtk_plug_get_socket_window(gtkPlug);
211 		
212 		if(p is null)
213 		{
214 			return null;
215 		}
216 		
217 		return ObjectG.getDObject!(GdkWin)(cast(GdkWindow*) p);
218 	}
219 
220 	protected class OnEmbeddedDelegateWrapper
221 	{
222 		void delegate(Plug) dlg;
223 		gulong handlerId;
224 		ConnectFlags flags;
225 		this(void delegate(Plug) dlg, gulong handlerId, ConnectFlags flags)
226 		{
227 			this.dlg = dlg;
228 			this.handlerId = handlerId;
229 			this.flags = flags;
230 		}
231 	}
232 	protected OnEmbeddedDelegateWrapper[] onEmbeddedListeners;
233 
234 	/**
235 	 * Gets emitted when the plug becomes embedded in a socket.
236 	 */
237 	gulong addOnEmbedded(void delegate(Plug) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
238 	{
239 		onEmbeddedListeners ~= new OnEmbeddedDelegateWrapper(dlg, 0, connectFlags);
240 		onEmbeddedListeners[onEmbeddedListeners.length - 1].handlerId = Signals.connectData(
241 			this,
242 			"embedded",
243 			cast(GCallback)&callBackEmbedded,
244 			cast(void*)onEmbeddedListeners[onEmbeddedListeners.length - 1],
245 			cast(GClosureNotify)&callBackEmbeddedDestroy,
246 			connectFlags);
247 		return onEmbeddedListeners[onEmbeddedListeners.length - 1].handlerId;
248 	}
249 	
250 	extern(C) static void callBackEmbedded(GtkPlug* plugStruct,OnEmbeddedDelegateWrapper wrapper)
251 	{
252 		wrapper.dlg(wrapper.outer);
253 	}
254 	
255 	extern(C) static void callBackEmbeddedDestroy(OnEmbeddedDelegateWrapper wrapper, GClosure* closure)
256 	{
257 		wrapper.outer.internalRemoveOnEmbedded(wrapper);
258 	}
259 
260 	protected void internalRemoveOnEmbedded(OnEmbeddedDelegateWrapper source)
261 	{
262 		foreach(index, wrapper; onEmbeddedListeners)
263 		{
264 			if (wrapper.dlg == source.dlg && wrapper.flags == source.flags && wrapper.handlerId == source.handlerId)
265 			{
266 				onEmbeddedListeners[index] = null;
267 				onEmbeddedListeners = std.algorithm.remove(onEmbeddedListeners, index);
268 				break;
269 			}
270 		}
271 	}
272 	
273 }