gdkproperty-win32.c 14.5 KB
Newer Older
1 2
/* GDK - The GIMP Drawing Kit
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3
 * Copyright (C) 1998-2002 Tor Lillqvist
4 5
 *
 * This library is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU Lesser General Public
7 8 9 10 11 12
 * 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
13
 * Lesser General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Lesser General Public
16 17 18 19 20 21
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

/*
22
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
23 24 25 26 27
 * 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/. 
 */

28
#include "config.h"
29
#include <string.h>
30
#include <stdlib.h>
31
#include <glib/gprintf.h>
32

33
#include "gdkscreen.h"
34 35
#include "gdkproperty.h"
#include "gdkselection.h"
36
#include "gdkprivate-win32.h"
37 38 39 40 41

GdkAtom
gdk_atom_intern (const gchar *atom_name,
		 gint         only_if_exists)
{
42
  ATOM win32_atom;
43 44 45 46 47 48
  GdkAtom retval;
  static GHashTable *atom_hash = NULL;
  
  if (!atom_hash)
    atom_hash = g_hash_table_new (g_str_hash, g_str_equal);

49
  retval = g_hash_table_lookup (atom_hash, atom_name);
50 51 52 53 54 55
  if (!retval)
    {
      if (strcmp (atom_name, "PRIMARY") == 0)
	retval = GDK_SELECTION_PRIMARY;
      else if (strcmp (atom_name, "SECONDARY") == 0)
	retval = GDK_SELECTION_SECONDARY;
56 57
      else if (strcmp (atom_name, "CLIPBOARD") == 0)
	retval = GDK_SELECTION_CLIPBOARD;
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
      else if (strcmp (atom_name, "ATOM") == 0)
	retval = GDK_SELECTION_TYPE_ATOM;
      else if (strcmp (atom_name, "BITMAP") == 0)
	retval = GDK_SELECTION_TYPE_BITMAP;
      else if (strcmp (atom_name, "COLORMAP") == 0)
	retval = GDK_SELECTION_TYPE_COLORMAP;
      else if (strcmp (atom_name, "DRAWABLE") == 0)
	retval = GDK_SELECTION_TYPE_DRAWABLE;
      else if (strcmp (atom_name, "INTEGER") == 0)
	retval = GDK_SELECTION_TYPE_INTEGER;
      else if (strcmp (atom_name, "PIXMAP") == 0)
	retval = GDK_SELECTION_TYPE_PIXMAP;
      else if (strcmp (atom_name, "WINDOW") == 0)
	retval = GDK_SELECTION_TYPE_WINDOW;
      else if (strcmp (atom_name, "STRING") == 0)
	retval = GDK_SELECTION_TYPE_STRING;
      else
	{
76
	  win32_atom = GlobalAddAtom (atom_name);
77
	  retval = GUINT_TO_POINTER ((guint) win32_atom);
78
	}
79 80 81
      g_hash_table_insert (atom_hash, 
			   g_strdup (atom_name), 
			   retval);
82 83 84 85 86
    }

  return retval;
}

87 88 89 90 91 92 93 94 95
GdkAtom
gdk_atom_intern_static_string (const gchar *atom_name)
{
  /* on X11 this is supposed to save memory. On win32 there seems to be
   * no way to make a difference ?
   */
  return gdk_atom_intern (atom_name, FALSE);
}

96 97 98
gchar *
gdk_atom_name (GdkAtom atom)
{
99
  ATOM win32_atom;
100
  gchar name[256];
101

102 103
  if (GDK_NONE == atom) return g_strdup ("<none>");
  else if (GDK_SELECTION_PRIMARY == atom) return g_strdup ("PRIMARY");
Hans Breuer's avatar
Hans Breuer committed
104 105 106 107 108 109 110 111 112 113
  else if (GDK_SELECTION_SECONDARY == atom) return g_strdup ("SECONDARY");
  else if (GDK_SELECTION_CLIPBOARD == atom) return g_strdup ("CLIPBOARD");
  else if (GDK_SELECTION_TYPE_ATOM == atom) return g_strdup ("ATOM");
  else if (GDK_SELECTION_TYPE_BITMAP == atom) return g_strdup ("BITMAP");
  else if (GDK_SELECTION_TYPE_COLORMAP == atom) return g_strdup ("COLORMAP");
  else if (GDK_SELECTION_TYPE_DRAWABLE == atom) return g_strdup ("DRAWABLE");
  else if (GDK_SELECTION_TYPE_INTEGER == atom) return g_strdup ("INTEGER");
  else if (GDK_SELECTION_TYPE_PIXMAP == atom) return g_strdup ("PIXMAP");
  else if (GDK_SELECTION_TYPE_WINDOW == atom) return g_strdup ("WINDOW");
  else if (GDK_SELECTION_TYPE_STRING == atom) return g_strdup ("STRING");
114 115 116 117
  
  win32_atom = GPOINTER_TO_UINT (atom);
  
  if (win32_atom < 0xC000)
118
    return g_strdup_printf ("#%p", atom);
119
  else if (GlobalGetAtomName (win32_atom, name, sizeof (name)) == 0)
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
    return NULL;
  return g_strdup (name);
}

gint
gdk_property_get (GdkWindow   *window,
		  GdkAtom      property,
		  GdkAtom      type,
		  gulong       offset,
		  gulong       length,
		  gint         pdelete,
		  GdkAtom     *actual_property_type,
		  gint        *actual_format_type,
		  gint        *actual_length,
		  guchar     **data)
{
136
  g_return_val_if_fail (window != NULL, FALSE);
137
  g_return_val_if_fail (GDK_IS_WINDOW (window), FALSE);
138

Tor Lillqvist's avatar
Tor Lillqvist committed
139
  if (GDK_WINDOW_DESTROYED (window))
140 141
    return FALSE;

142 143 144 145 146 147
  g_warning ("gdk_property_get: Not implemented");

  return FALSE;
}

void
148 149 150 151 152 153 154
gdk_property_change (GdkWindow    *window,
		     GdkAtom       property,
		     GdkAtom       type,
		     gint          format,
		     GdkPropMode   mode,
		     const guchar *data,
		     gint          nelements)
155
{
156
  HGLOBAL hdata;
157
  gint i, size;
158
  guchar *ucptr, *buf = NULL;
159
  wchar_t *wcptr, *p;
160
  glong wclen;
161

162
  g_return_if_fail (window != NULL);
163
  g_return_if_fail (GDK_IS_WINDOW (window));
164

Tor Lillqvist's avatar
Tor Lillqvist committed
165
  if (GDK_WINDOW_DESTROYED (window))
166 167
    return;

168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
  GDK_NOTE (DND, {
      gchar *prop_name = gdk_atom_name (property);
      gchar *type_name = gdk_atom_name (type);
      
      g_print ("gdk_property_change: %p %s %s %s %d*%d bits: %s\n",
	       GDK_WINDOW_HWND (window),
	       prop_name,
	       type_name,
	       (mode == GDK_PROP_MODE_REPLACE ? "REPLACE" :
		(mode == GDK_PROP_MODE_PREPEND ? "PREPEND" :
		 (mode == GDK_PROP_MODE_APPEND ? "APPEND" :
		  "???"))),
	       format, nelements,
	       _gdk_win32_data_to_string (data, MIN (10, format*nelements/8)));
      g_free (prop_name);
      g_free (type_name);
    });
185

186 187 188 189 190 191
  /* We should never come here for these types */
  g_return_if_fail (type != GDK_TARGET_STRING);
  g_return_if_fail (type != _text);
  g_return_if_fail (type != _compound_text);
  g_return_if_fail (type != _save_targets);

192 193 194
  if (property == _gdk_selection &&
      format == 8 &&
      mode == GDK_PROP_MODE_REPLACE)
195
    {
196
      if (type == _utf8_string)
197
	{
198 199 200 201 202
	  if (!OpenClipboard (GDK_WINDOW_HWND (window)))
	    {
	      WIN32_API_FAILED ("OpenClipboard");
	      return;
	    }
203

204
	  wcptr = g_utf8_to_utf16 ((char *) data, nelements, NULL, &wclen, NULL);
205

206 207 208 209 210
	  wclen++;		/* Terminating 0 */
	  size = wclen * 2;
	  for (i = 0; i < wclen; i++)
	    if (wcptr[i] == '\n')
	      size += 2;
211 212 213 214 215 216
	  
	  if (!(hdata = GlobalAlloc (GMEM_MOVEABLE, size)))
	    {
	      WIN32_API_FAILED ("GlobalAlloc");
	      if (!CloseClipboard ())
		WIN32_API_FAILED ("CloseClipboard");
217
	      g_free (buf);
218 219
	      return;
	    }
220

221
	  ucptr = GlobalLock (hdata);
222

223 224
	  p = (wchar_t *) ucptr;
	  for (i = 0; i < wclen; i++)
225
	    {
226 227 228
	      if (wcptr[i] == '\n')
		*p++ = '\r';
	      *p++ = wcptr[i];
229
	    }
230
	  g_free (wcptr);
231 232

	  GlobalUnlock (hdata);
233 234 235 236
	  GDK_NOTE (DND, g_print ("... SetClipboardData(CF_UNICODETEXT,%p)\n",
				  hdata));
	  if (!SetClipboardData (CF_UNICODETEXT, hdata))
	    WIN32_API_FAILED ("SetClipboardData");
237
      
238 239 240
	  if (!CloseClipboard ())
	    WIN32_API_FAILED ("CloseClipboard");
	}
241 242
      else
        {
243 244 245 246
	  /* We use delayed rendering for everything else than
	   * text. We can't assign hdata to the clipboard here as type
	   * may be "image/png", "image/jpg", etc. In this case
	   * there's a further conversion afterwards.
247
	   */
248
	  GDK_NOTE (DND, g_print ("... delayed rendering\n"));
249 250
	  _delayed_rendering_data = NULL;
	  if (!(hdata = GlobalAlloc (GMEM_MOVEABLE, nelements > 0 ? nelements : 1)))
251
	    {
252 253
	      WIN32_API_FAILED ("GlobalAlloc");
	      return;
254
	    }
255 256 257 258
	  ucptr = GlobalLock (hdata);
	  memcpy (ucptr, data, nelements);
	  GlobalUnlock (hdata);
	  _delayed_rendering_data = hdata;
259 260
	}
    }
Tor Lillqvist's avatar
Tor Lillqvist committed
261 262 263 264 265
  else if (property == _gdk_ole2_dnd)
    {
      /* Will happen only if gdkdnd-win32.c has OLE2 dnd support compiled in */
      _gdk_win32_ole2_dnd_property_change (type, format, data, nelements);
    }
266 267 268 269 270 271 272 273
  else
    g_warning ("gdk_property_change: General case not implemented");
}

void
gdk_property_delete (GdkWindow *window,
		     GdkAtom    property)
{
274
  gchar *prop_name;
275

276
  g_return_if_fail (window != NULL);
277
  g_return_if_fail (GDK_IS_WINDOW (window));
278

279 280
  GDK_NOTE (DND, {
      prop_name = gdk_atom_name (property);
281

282 283 284 285 286 287 288
      g_print ("gdk_property_delete: %p %s\n",
	       GDK_WINDOW_HWND (window),
	       prop_name);
      g_free (prop_name);
    });

  if (property == _gdk_selection)
289
    _gdk_selection_property_delete (window);
290
  else if (property == _wm_transient_for)
291
    gdk_window_set_transient_for (window, _gdk_root);
292
  else
293 294 295 296 297 298
    {
      prop_name = gdk_atom_name (property);
      g_warning ("gdk_property_delete: General case (%s) not implemented",
		 prop_name);
      g_free (prop_name);
    }
299
}
Hans Breuer's avatar
updated  
Hans Breuer committed
300

301
/*
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
  For reference, from gdk/x11/gdksettings.c:

  "Net/DoubleClickTime\0"     "gtk-double-click-time\0"
  "Net/DoubleClickDistance\0" "gtk-double-click-distance\0"
  "Net/DndDragThreshold\0"    "gtk-dnd-drag-threshold\0"
  "Net/CursorBlink\0"         "gtk-cursor-blink\0"
  "Net/CursorBlinkTime\0"     "gtk-cursor-blink-time\0"
  "Net/ThemeName\0"           "gtk-theme-name\0"
  "Net/IconThemeName\0"       "gtk-icon-theme-name\0"
  "Gtk/CanChangeAccels\0"     "gtk-can-change-accels\0"
  "Gtk/ColorPalette\0"        "gtk-color-palette\0"
  "Gtk/FontName\0"            "gtk-font-name\0"
  "Gtk/IconSizes\0"           "gtk-icon-sizes\0"
  "Gtk/KeyThemeName\0"        "gtk-key-theme-name\0"
  "Gtk/ToolbarStyle\0"        "gtk-toolbar-style\0"
  "Gtk/ToolbarIconSize\0"     "gtk-toolbar-icon-size\0"
  "Gtk/IMPreeditStyle\0"      "gtk-im-preedit-style\0"
  "Gtk/IMStatusStyle\0"       "gtk-im-status-style\0"
  "Gtk/Modules\0"             "gtk-modules\0"
  "Gtk/FileChooserBackend\0"  "gtk-file-chooser-backend\0"
  "Gtk/ButtonImages\0"        "gtk-button-images\0"
  "Gtk/MenuImages\0"          "gtk-menu-images\0"
  "Gtk/MenuBarAccel\0"        "gtk-menu-bar-accel\0"
325
  "Gtk/CursorBlinkTimeout\0"  "gtk-cursor-blink-timeout\0"
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
  "Gtk/CursorThemeName\0"     "gtk-cursor-theme-name\0"
  "Gtk/CursorThemeSize\0"     "gtk-cursor-theme-size\0"
  "Gtk/ShowInputMethodMenu\0" "gtk-show-input-method-menu\0"
  "Gtk/ShowUnicodeMenu\0"     "gtk-show-unicode-menu\0"
  "Gtk/TimeoutInitial\0"      "gtk-timeout-initial\0"
  "Gtk/TimeoutRepeat\0"       "gtk-timeout-repeat\0"
  "Gtk/ColorScheme\0"         "gtk-color-scheme\0"
  "Gtk/EnableAnimations\0"    "gtk-enable-animations\0"
  "Xft/Antialias\0"           "gtk-xft-antialias\0"
  "Xft/Hinting\0"             "gtk-xft-hinting\0"
  "Xft/HintStyle\0"           "gtk-xft-hintstyle\0"
  "Xft/RGBA\0"                "gtk-xft-rgba\0"
  "Xft/DPI\0"                 "gtk-xft-dpi\0"
  "Net/FallbackIconTheme\0"   "gtk-fallback-icon-theme\0"
  "Gtk/TouchscreenMode\0"     "gtk-touchscreen-mode\0"
  "Gtk/EnableAccels\0"        "gtk-enable-accels\0"
  "Gtk/EnableMnemonics\0"     "gtk-enable-mnemonics\0"
  "Gtk/ScrolledWindowPlacement\0" "gtk-scrolled-window-placement\0"
  "Gtk/IMModule\0"            "gtk-im-module\0"
  "Fontconfig/Timestamp\0"    "gtk-fontconfig-timestamp\0"
  "Net/SoundThemeName\0"      "gtk-sound-theme-name\0"
  "Net/EnableInputFeedbackSounds\0" "gtk-enable-input-feedback-sounds\0"
  "Net/EnableEventSounds\0"  "gtk-enable-event-sounds\0";

  More, from various places in gtk sources:

352 353 354 355
  gtk-entry-select-on-focus
  gtk-split-cursor

*/
Hans Breuer's avatar
updated  
Hans Breuer committed
356
gboolean
357 358 359
gdk_screen_get_setting (GdkScreen   *screen,
                        const gchar *name,
                        GValue      *value)
Hans Breuer's avatar
updated  
Hans Breuer committed
360
{
361
  g_return_val_if_fail (GDK_IS_SCREEN (screen), FALSE);
362

Hans Breuer's avatar
updated  
Hans Breuer committed
363 364 365 366
  /*
   * XXX : if these values get changed through the Windoze UI the
   *       respective gdk_events are not generated yet.
   */
367 368 369 370 371
  if (strcmp ("gtk-theme-name", name) == 0) 
    {
      g_value_set_string (value, "ms-windows");
    }
  else if (strcmp ("gtk-double-click-time", name) == 0)
Hans Breuer's avatar
updated  
Hans Breuer committed
372
    {
373 374 375
      gint i = GetDoubleClickTime ();
      GDK_NOTE(MISC, g_print("gdk_screen_get_setting(\"%s\") : %d\n", name, i));
      g_value_set_int (value, i);
Hans Breuer's avatar
updated  
Hans Breuer committed
376 377
      return TRUE;
    }
378
  else if (strcmp ("gtk-double-click-distance", name) == 0)
Hans Breuer's avatar
updated  
Hans Breuer committed
379
    {
380 381 382
      gint i = MAX(GetSystemMetrics (SM_CXDOUBLECLK), GetSystemMetrics (SM_CYDOUBLECLK));
      GDK_NOTE(MISC, g_print("gdk_screen_get_setting(\"%s\") : %d\n", name, i));
      g_value_set_int (value, i);
Hans Breuer's avatar
updated  
Hans Breuer committed
383 384
      return TRUE;
    }
385 386 387 388 389 390 391 392 393 394 395 396 397
  else if (strcmp ("gtk-dnd-drag-threshold", name) == 0)
    {
      gint i = MAX(GetSystemMetrics (SM_CXDRAG), GetSystemMetrics (SM_CYDRAG));
      GDK_NOTE(MISC, g_print("gdk_screen_get_setting(\"%s\") : %d\n", name, i));
      g_value_set_int (value, i);
      return TRUE;
    }
  else if (strcmp ("gtk-split-cursor", name) == 0)
    {
      GDK_NOTE(MISC, g_print("gdk_screen_get_setting(\"%s\") : FALSE\n", name));
      g_value_set_boolean (value, FALSE);
      return TRUE;
    }
398 399 400 401 402 403
  else if (strcmp ("gtk-alternative-button-order", name) == 0)
    {
      GDK_NOTE(MISC, g_print("gdk_screen_get_setting(\"%s\") : TRUE\n", name));
      g_value_set_boolean (value, TRUE);
      return TRUE;
    }
404 405 406 407 408 409
  else if (strcmp ("gtk-alternative-sort-arrows", name) == 0)
    {
      GDK_NOTE(MISC, g_print("gdk_screen_get_setting(\"%s\") : TRUE\n", name));
      g_value_set_boolean (value, TRUE);
      return TRUE;
    }
410 411 412 413 414 415 416 417 418 419 420 421 422 423
#if 0
  /*
   * With 'MS Sans Serif' as windows menu font (default on win98se) you'll get a 
   * bunch of :
   *   WARNING **: Couldn't load font "MS Sans Serif 8" falling back to "Sans 8"
   * at least with testfilechooser (regardless of the bitmap check below)
   * so just disabling this code seems to be the best we can do --hb
   */
  else if (strcmp ("gtk-font-name", name) == 0)
    {
      NONCLIENTMETRICS ncm;
      ncm.cbSize = sizeof(NONCLIENTMETRICS);
      if (SystemParametersInfo (SPI_GETNONCLIENTMETRICS, ncm.cbSize, &ncm, FALSE))
        {
424 425 426
          /* Pango finally uses GetDeviceCaps to scale, we use simple
	   * approximation here.
	   */
427 428 429 430 431
          int nHeight = (0 > ncm.lfMenuFont.lfHeight ? -3*ncm.lfMenuFont.lfHeight/4 : 10);
          if (OUT_STRING_PRECIS == ncm.lfMenuFont.lfOutPrecision)
            GDK_NOTE(MISC, g_print("gdk_screen_get_setting(%s) : ignoring bitmap font '%s'\n", 
                                   name, ncm.lfMenuFont.lfFaceName));
          else if (ncm.lfMenuFont.lfFaceName && strlen(ncm.lfMenuFont.lfFaceName) > 0 &&
432
                   /* Avoid issues like those described in bug #135098 */
433 434 435 436 437 438 439 440 441 442 443 444 445 446
                   g_utf8_validate (ncm.lfMenuFont.lfFaceName, -1, NULL))
            {
              char* s = g_strdup_printf ("%s %d", ncm.lfMenuFont.lfFaceName, nHeight);
              GDK_NOTE(MISC, g_print("gdk_screen_get_setting(%s) : %s\n", name, s));
              g_value_set_string (value, s);

              g_free(s);
              return TRUE;
            }
        }
    }
#endif

  return FALSE;
447
}