?? gdkcolor-win32.c
字號:
/* GDK - The GIMP Drawing Kit * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald * Copyright (C) 1998-2002 Tor Lillqvist * * 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., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. *//* * Modified by the GTK+ Team and others 1997-2000. 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/. */#include <config.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include "gdkcolor.h"#include "gdkscreen.h"#include "gdkinternals.h"#include "gdkprivate-win32.h"static gint gdk_colormap_match_color (GdkColormap *cmap, GdkColor *color, const gchar *available);static void gdk_colormap_init (GdkColormap *colormap);static void gdk_colormap_class_init (GdkColormapClass *klass);static void gdk_colormap_finalize (GObject *object);static gpointer parent_class = NULL;GTypegdk_colormap_get_type (void){ static GType object_type = 0; if (!object_type) { static const GTypeInfo object_info = { sizeof (GdkColormapClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) gdk_colormap_class_init, NULL, /* class_finalize */ NULL, /* class_data */ sizeof (GdkColormap), 0, /* n_preallocs */ (GInstanceInitFunc) gdk_colormap_init, }; object_type = g_type_register_static (G_TYPE_OBJECT, "GdkColormap", &object_info, 0); } return object_type;}static voidgdk_colormap_init (GdkColormap *colormap){ GdkColormapPrivateWin32 *private; private = g_new (GdkColormapPrivateWin32, 1); colormap->windowing_data = private; private->hpal = NULL; private->current_size = 0; private->use = NULL; private->hash = NULL; private->info = NULL; colormap->size = 0; colormap->colors = NULL;}static voidgdk_colormap_class_init (GdkColormapClass *klass){ GObjectClass *object_class = G_OBJECT_CLASS (klass); parent_class = g_type_class_peek_parent (klass); object_class->finalize = gdk_colormap_finalize;}static voidgdk_colormap_finalize (GObject *object){ GdkColormap *colormap = GDK_COLORMAP (object); GdkColormapPrivateWin32 *private = GDK_WIN32_COLORMAP_DATA (colormap); if (!DeleteObject (private->hpal)) WIN32_GDI_FAILED ("DeleteObject"); if (private->hash) g_hash_table_destroy (private->hash); g_free (private->info); g_free (colormap->colors); g_free (private); G_OBJECT_CLASS (parent_class)->finalize (object);}/* Mimics XAllocColorCells. Allocate read/write color cells. */static gbooleanalloc_color_cells (GdkColormap *cmap, gboolean contig, unsigned long plane_masks_return[], unsigned int nplanes, unsigned long pixels_return[], unsigned int npixels){ GdkColormapPrivateWin32 *cmapp = GDK_WIN32_COLORMAP_DATA (cmap); gint i, nfree, iret, start = 0; GDK_NOTE (COLORMAP, g_print ("alloc_color_cells: cmap=%p contig=%s npl=%d npix=%d", cmapp, contig ? "TRUE" : "FALSE", nplanes, npixels)); switch (cmap->visual->type) { case GDK_VISUAL_GRAYSCALE: case GDK_VISUAL_PSEUDO_COLOR: nfree = 0; for (i = 0; i < cmap->size && nfree < npixels; i++) if (cmapp->use[i] == GDK_WIN32_PE_AVAILABLE && (!contig || (nfree == 0 || cmapp->use[i-1] == GDK_WIN32_PE_AVAILABLE))) { if (nfree == 0) start = i; nfree++; } else if (contig) nfree = 0; if (npixels > nfree) { GDK_NOTE (COLORMAP, g_print ("...nope (%d > %d)\n", npixels, nfree)); return FALSE; } else GDK_NOTE (COLORMAP, g_print ("...ok\n")); iret = 0; for (i = start; i < cmap->size && iret < npixels; i++) if (cmapp->use[i] == GDK_WIN32_PE_AVAILABLE) { cmapp->use[i] = GDK_WIN32_PE_INUSE; pixels_return[iret] = i; iret++; } g_assert (iret == npixels); break; default: g_assert_not_reached (); } return TRUE;}/* The following functions are originally from Tk8.0, but heavily modified. Here are tk's licensing terms. I hope these terms don't conflict with the GNU Lesser General Public License? They shouldn't, as they are looser that the GLPL, yes? *//*This software is copyrighted by the Regents of the University ofCalifornia, Sun Microsystems, Inc., and other parties. The followingterms apply to all files associated with the software unless explicitlydisclaimed in individual files.The authors hereby grant permission to use, copy, modify, distribute,and license this software and its documentation for any purpose, providedthat existing copyright notices are retained in all copies and that thisnotice is included verbatim in any distributions. No written agreement,license, or royalty fee is required for any of the authorized uses.Modifications to this software may be copyrighted by their authorsand need not follow the licensing terms described here, provided thatthe new terms are clearly indicated on the first page of each file wherethey apply.IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTYFOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGESARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANYDERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE BEEN ADVISED OF THEPOSSIBILITY OF SUCH DAMAGE.THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES,INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. THIS SOFTWAREIS PROVIDED ON AN "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVENO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, ORMODIFICATIONS.GOVERNMENT USE: If you are acquiring this software on behalf of theU.S. government, the Government shall have only "Restricted Rights"in the software and related documentation as defined in the Federal Acquisition Regulations (FARs) in Clause 52.227.19 (c) (2). If youare acquiring the software on behalf of the Department of Defense, thesoftware shall be classified as "Commercial Computer Software" and theGovernment shall have only "Restricted Rights" as defined in Clause252.227-7013 (c) (1) of DFARs. Notwithstanding the foregoing, theauthors grant the U.S. Government and others acting in its behalfpermission to use and distribute the software in accordance with theterms specified in this license.*//* Mimics XAllocColor. Allocate a read-only colormap entry. */static intalloc_color (GdkColormap *cmap, PALETTEENTRY *color, guint *pixelp){ PALETTEENTRY entry, close_entry; COLORREF new_pixel; UINT index; GdkColormapPrivateWin32 *cmapp = GDK_WIN32_COLORMAP_DATA (cmap); gint i; entry = *color; entry.peFlags = 0; new_pixel = RGB (entry.peRed, entry.peGreen, entry.peBlue); switch (cmap->visual->type) { case GDK_VISUAL_PSEUDO_COLOR: /* Find the nearest existing palette entry. */ index = GetNearestPaletteIndex (cmapp->hpal, new_pixel); GetPaletteEntries (cmapp->hpal, index, 1, &close_entry); GDK_NOTE (COLORMAP, g_print ("alloc_color: new_pixel=%06lx index=%d=%02x close=%06lx\n", new_pixel, index, index, RGB (close_entry.peRed, close_entry.peGreen, close_entry.peBlue))); if (new_pixel != RGB (close_entry.peRed, close_entry.peGreen, close_entry.peBlue)) { /* Not a perfect match. */ if (cmapp->use[index] == GDK_WIN32_PE_AVAILABLE) { /* It was a nonused entry anyway, so we can use it, and * set it to the correct color. */ GDK_NOTE (COLORMAP, g_print ("...was free\n")); if (!SetPaletteEntries (cmapp->hpal, index, 1, &entry)) WIN32_GDI_FAILED ("SetPaletteEntries"); } else { /* The close entry found is in use, so search for a * available slot. */ gboolean done = FALSE; for (i = 0; i < cmap->size; i++) if (cmapp->use[i] == GDK_WIN32_PE_AVAILABLE) { /* An available slot, use it. */ GDK_NOTE (COLORMAP, g_print ("...use free slot %d%s\n", i, (i >= cmapp->current_size) ? ", will resize palette" : "")); if (i >= cmapp->current_size) { if (!ResizePalette (cmapp->hpal, i + 1)) { WIN32_GDI_FAILED ("ResizePalette"); break; } cmapp->current_size = i + 1; } if (!SetPaletteEntries (cmapp->hpal, i, 1, &entry)) { WIN32_GDI_FAILED ("SetPaletteEntries"); i = cmap->size; } else { done = TRUE; index = i; } break; } if (!done) { /* No free slots available, or failed to resize * palette or set palette entry. */ GDK_NOTE (COLORMAP, g_print ("... failure\n")); return FALSE; } } } else { /* We got a match, so use it. */ } *pixelp = index; cmapp->use[index] = GDK_WIN32_PE_INUSE; GDK_NOTE (COLORMAP, g_print ("alloc_color: %p: " "index=%3d=%02x for %02x %02x %02x: " "%02x %02x %02x\n", cmapp->hpal, index, index, entry.peRed, entry.peGreen, entry.peBlue, color->peRed, color->peGreen, color->peBlue)); return TRUE; case GDK_VISUAL_STATIC_COLOR: /* Find the nearest existing palette entry. */ index = GetNearestPaletteIndex (cmapp->hpal, new_pixel); GetPaletteEntries (cmapp->hpal, index, 1, &close_entry); *color = close_entry; *pixelp = index; GDK_NOTE (COLORMAP, g_print ("alloc_color %p: " "index=%3d=%02x for %02x %02x %02x: " "%02x %02x %02x\n", cmapp->hpal, index, index, entry.peRed, entry.peGreen, entry.peBlue, color->peRed, color->peGreen, color->peBlue)); return TRUE; case GDK_VISUAL_TRUE_COLOR: /* Determine what color will actually be used on non-colormap systems. */ *pixelp = GetNearestColor (_gdk_display_hdc, new_pixel); color->peRed = GetRValue (*pixelp); color->peGreen = GetGValue (*pixelp); color->peBlue = GetBValue (*pixelp); return TRUE; default: g_error ("cmap->visual->type=%d", cmap->visual->type); g_assert_not_reached (); return FALSE; }}/* Mimics XFreeColors. */static voidfree_colors (GdkColormap *cmap, gulong *pixels, gint npixels, gulong planes){ PALETTEENTRY pe; GdkColormapPrivateWin32 *cmapp = GDK_WIN32_COLORMAP_DATA (cmap); gint i;#ifdef G_ENABLE_DEBUG gint set_black_count = 0;#endif gboolean *cleared_entries; cleared_entries = g_new0 (gboolean, cmap->size); /* We don't have to do anything for non-palette devices. */ switch (cmap->visual->type) { case GDK_VISUAL_GRAYSCALE: case GDK_VISUAL_PSEUDO_COLOR: for (i = 0; i < npixels; i++) { if (pixels[i] >= cmap->size) ; /* Nothing */ else if (cmapp->use[pixels[i]] == GDK_WIN32_PE_STATIC) ; /* Nothing either*/ else { cmapp->use[pixels[i]] = GDK_WIN32_PE_AVAILABLE; cleared_entries[pixels[i]] = TRUE; } } for (i = cmapp->current_size - 1; i >= 0; i--) if (cmapp->use[i] != GDK_WIN32_PE_AVAILABLE) break; if (i < cmapp->current_size - 1) { GDK_NOTE (COLORMAP, g_print ("free_colors: hpal=%p resize=%d\n", cmapp->hpal, i + 1)); if (!ResizePalette (cmapp->hpal, i + 1)) WIN32_GDI_FAILED ("ResizePalette"); else cmapp->current_size = i + 1; } pe.peRed = pe.peGreen = pe.peBlue = pe.peFlags = 0; for (i = 0; i < cmapp->current_size; i++) { if (cleared_entries[i]) { if (!SetPaletteEntries (cmapp->hpal, i, 1, &pe)) WIN32_GDI_FAILED ("SetPaletteEntries"); GDK_NOTE (COLORMAP, set_black_count++); } }#if 0 GDK_NOTE (COLORMAP, _gdk_win32_print_hpalette (cmapp->hpal));#else GDK_NOTE (COLORMAP, (set_black_count > 0 ? g_print ("free_colors: %d (%d) set to black\n", set_black_count, cmapp->current_size) : (void) 0));#endif g_free (cleared_entries); break; default: g_assert_not_reached (); }}/* Mimics XCreateColormap. */
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -