gtksocket-win32.c 8.66 KB
Newer Older
Cody Russell's avatar
Cody Russell committed
1
/* GTK - The GIMP Toolkit
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 * Copyright (C) 2005 Novell, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/* By Tor Lillqvist <tml@novell.com> 2005 */

/*
 * Modified by the GTK+ Team and others 1997-2005.  See the AUTHORS
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
 */

29
#include "config.h"
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
#include <string.h>

#include "gtkwindow.h"
#include "gtkplug.h"
#include "gtkprivate.h"
#include "gtksocket.h"
#include "gtksocketprivate.h"

#include "win32/gdkwin32.h"

#include "gtkwin32embed.h"

GdkNativeWindow
_gtk_socket_windowing_get_id (GtkSocket *socket)
{
  g_return_val_if_fail (GTK_IS_SOCKET (socket), 0);
  g_return_val_if_fail (GTK_WIDGET_ANCHORED (socket), 0);

48
  if (!gtk_widget_get_realized (GTK_WIDGET (socket)))
49 50
    gtk_widget_realize (GTK_WIDGET (socket));

51
  return (GdkNativeWindow) GDK_WINDOW_HWND (gtk_widget_get_window (GTK_WIDGET (socket)));
52 53 54 55 56 57 58 59 60 61 62 63
}

void
_gtk_socket_windowing_realize_window (GtkSocket *socket)
{
  /* XXX Anything needed? */
}

void
_gtk_socket_windowing_end_embedding_toplevel (GtkSocket *socket)
{
  gtk_window_remove_embedded_xid (GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (socket))),
64
				  GDK_WINDOW_HWND (socket->plug_window));
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
}

void
_gtk_socket_windowing_size_request (GtkSocket *socket)
{
  MINMAXINFO mmi;

  socket->request_width = 1;
  socket->request_height = 1;
  
  mmi.ptMaxSize.x = mmi.ptMaxSize.y = 16000; /* ??? */
  mmi.ptMinTrackSize.x = mmi.ptMinTrackSize.y = 1;
  mmi.ptMaxTrackSize.x = mmi.ptMaxTrackSize.y = 16000; /* ??? */
  mmi.ptMaxPosition.x = mmi.ptMaxPosition.y = 0;

  if (SendMessage (GDK_WINDOW_HWND (socket->plug_window), WM_GETMINMAXINFO,
		   0, (LPARAM) &mmi) == 0)
    {
      socket->request_width = mmi.ptMinTrackSize.x;
      socket->request_height = mmi.ptMinTrackSize.y;
    }
  socket->have_size = TRUE;
}

void
_gtk_socket_windowing_send_key_event (GtkSocket *socket,
				      GdkEvent  *gdk_event,
				      gboolean   mask_key_presses)
{
  PostMessage (GDK_WINDOW_HWND (socket->plug_window),
	       (gdk_event->type == GDK_KEY_PRESS ? WM_KEYDOWN : WM_KEYUP),
	       gdk_event->key.hardware_keycode, 0);
}

void
_gtk_socket_windowing_focus_change (GtkSocket *socket,
				    gboolean   focus_in)
{
  if (focus_in)
    _gtk_win32_embed_send_focus_message (socket->plug_window,
					 GTK_WIN32_EMBED_FOCUS_IN,
					 GTK_WIN32_EMBED_FOCUS_CURRENT);
  else
    _gtk_win32_embed_send (socket->plug_window,
			   GTK_WIN32_EMBED_FOCUS_OUT,
			   0, 0);
}

void
_gtk_socket_windowing_update_active (GtkSocket *socket,
				     gboolean   active)
{
  _gtk_win32_embed_send (socket->plug_window,
			 (active ? GTK_WIN32_EMBED_WINDOW_ACTIVATE : GTK_WIN32_EMBED_WINDOW_DEACTIVATE),
			 0, 0);
}

void
_gtk_socket_windowing_update_modality (GtkSocket *socket,
				       gboolean   modality)
{
  _gtk_win32_embed_send (socket->plug_window,
			 (modality ? GTK_WIN32_EMBED_MODALITY_ON : GTK_WIN32_EMBED_MODALITY_OFF),
			 0, 0);
}

void
_gtk_socket_windowing_focus (GtkSocket       *socket,
			     GtkDirectionType direction)
{
  int detail = -1;

  switch (direction)
    {
    case GTK_DIR_UP:
    case GTK_DIR_LEFT:
    case GTK_DIR_TAB_BACKWARD:
      detail = GTK_WIN32_EMBED_FOCUS_LAST;
      break;
    case GTK_DIR_DOWN:
    case GTK_DIR_RIGHT:
    case GTK_DIR_TAB_FORWARD:
      detail = GTK_WIN32_EMBED_FOCUS_FIRST;
      break;
    }
  
  _gtk_win32_embed_send_focus_message (socket->plug_window,
				       GTK_WIN32_EMBED_FOCUS_IN,
				       detail);
}

void
_gtk_socket_windowing_send_configure_event (GtkSocket *socket)
{
  /* XXX Nothing needed? */
}

void
_gtk_socket_windowing_select_plug_window_input (GtkSocket *socket)
{
  /* XXX Nothing needed? */
}

void
_gtk_socket_windowing_embed_get_info (GtkSocket *socket)
{
  socket->is_mapped = TRUE;	/* XXX ? */
}

void
_gtk_socket_windowing_embed_notify (GtkSocket *socket)
{
  /* XXX Nothing needed? */
}

gboolean
_gtk_socket_windowing_embed_get_focus_wrapped (void)
{
  return _gtk_win32_embed_get_focus_wrapped ();
}

void
_gtk_socket_windowing_embed_set_focus_wrapped (void)
{
  _gtk_win32_embed_set_focus_wrapped ();
}

GdkFilterReturn
_gtk_socket_windowing_filter_func (GdkXEvent *gdk_xevent,
				   GdkEvent  *event,
				   gpointer   data)
{
  GtkSocket *socket;
  GtkWidget *widget;
  MSG *msg;
  GdkFilterReturn return_val;

  socket = GTK_SOCKET (data);

  return_val = GDK_FILTER_CONTINUE;

  if (socket->plug_widget)
    return return_val;

  widget = GTK_WIDGET (socket);
  msg = (MSG *) gdk_xevent;

  switch (msg->message)
    {
    default:
      if (msg->message == _gtk_win32_embed_message_type (GTK_WIN32_EMBED_PARENT_NOTIFY))
	{
217 218
	  GTK_NOTE (PLUGSOCKET, g_printerr ("GtkSocket: PARENT_NOTIFY received window=%p version=%d\n",
					    (gpointer) msg->wParam, (int) msg->lParam));
219 220 221 222 223
	  /* If we some day different protocols deployed need to add
	   * some more elaborate version handshake
	   */
	  if (msg->lParam != GTK_WIN32_EMBED_PROTOCOL_VERSION)
	    g_warning ("GTK Win32 embedding protocol version mismatch, "
224 225
		       "client uses version %d, we understand version %d",
		       (int) msg->lParam, GTK_WIN32_EMBED_PROTOCOL_VERSION);
226 227
	  if (!socket->plug_window)
	    {
228
	      _gtk_socket_add_window (socket, (GdkNativeWindow) msg->wParam, FALSE);
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
	      
	      if (socket->plug_window)
		GTK_NOTE (PLUGSOCKET, g_printerr ("GtkSocket: window created"));
	      
	      return_val = GDK_FILTER_REMOVE;
	    }
	}
      else if (msg->message == _gtk_win32_embed_message_type (GTK_WIN32_EMBED_EVENT_PLUG_MAPPED))
	{
	  gboolean was_mapped = socket->is_mapped;
	  gboolean is_mapped = msg->wParam != 0;

	  GTK_NOTE (PLUGSOCKET, g_printerr ("GtkSocket: PLUG_MAPPED received is_mapped:%d\n", is_mapped));
	  if (was_mapped != is_mapped)
	    {
	      if (is_mapped)
		_gtk_socket_handle_map_request (socket);
	      else
		{
		  gdk_window_show (socket->plug_window);
		  _gtk_socket_unmap_notify (socket);
		}
	    }
	  return_val = GDK_FILTER_REMOVE;
	}
      else if (msg->message == _gtk_win32_embed_message_type (GTK_WIN32_EMBED_PLUG_RESIZED))
	{
	  GTK_NOTE (PLUGSOCKET, g_printerr ("GtkSocket: PLUG_RESIZED received\n"));
	  socket->have_size = FALSE;
	  gtk_widget_queue_resize (widget);
	  return_val = GDK_FILTER_REMOVE;
	}
      else if (msg->message == _gtk_win32_embed_message_type (GTK_WIN32_EMBED_REQUEST_FOCUS))
	{
	  GTK_NOTE (PLUGSOCKET, g_printerr ("GtkSocket: REQUEST_FOCUS received\n"));
	  _gtk_win32_embed_push_message (msg);
	  _gtk_socket_claim_focus (socket, TRUE);
	  _gtk_win32_embed_pop_message ();
	  return_val = GDK_FILTER_REMOVE;
	}
      else if (msg->message == _gtk_win32_embed_message_type (GTK_WIN32_EMBED_FOCUS_NEXT))
	{
	  GTK_NOTE (PLUGSOCKET, g_printerr ("GtkSocket: FOCUS_NEXT received\n"));
	  _gtk_win32_embed_push_message (msg);
	  _gtk_socket_advance_toplevel_focus (socket, GTK_DIR_TAB_FORWARD);
	  _gtk_win32_embed_pop_message ();
	  return_val = GDK_FILTER_REMOVE;
	}
      else if (msg->message == _gtk_win32_embed_message_type (GTK_WIN32_EMBED_FOCUS_PREV))
	{
	  GTK_NOTE (PLUGSOCKET, g_printerr ("GtkSocket: FOCUS_PREV received\n"));
	  _gtk_win32_embed_push_message (msg);
	  _gtk_socket_advance_toplevel_focus (socket, GTK_DIR_TAB_BACKWARD);
	  _gtk_win32_embed_pop_message ();
	  return_val = GDK_FILTER_REMOVE;
	}
      else if (msg->message == _gtk_win32_embed_message_type (GTK_WIN32_EMBED_GRAB_KEY))
	{
	  GTK_NOTE (PLUGSOCKET, g_printerr ("GtkSocket: GRAB_KEY received\n"));
	  _gtk_win32_embed_push_message (msg);
	  _gtk_socket_add_grabbed_key (socket, msg->wParam, msg->lParam);
	  _gtk_win32_embed_pop_message ();
	  return_val = GDK_FILTER_REMOVE;
	}
      else if (msg->message == _gtk_win32_embed_message_type (GTK_WIN32_EMBED_UNGRAB_KEY))
	{
	  GTK_NOTE (PLUGSOCKET, g_printerr ("GtkSocket: UNGRAB_KEY received\n"));
	  _gtk_win32_embed_push_message (msg);
	  _gtk_socket_remove_grabbed_key (socket, msg->wParam, msg->lParam);
	  _gtk_win32_embed_pop_message ();
	  return_val = GDK_FILTER_REMOVE;
	}
      break;
    }

  return return_val;
}