1 /* GTK - The GIMP Toolkit
2 * gtkprintoperation.c: Print Operation
3 * Copyright (C) 2006, Red Hat, Inc.
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 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library 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 GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
22 #define _WIN32_WINNT 0x0500
23 #define WINVER _WIN32_WINNT
27 #include "gtkprint-win32.h"
30 gtk_print_win32_devnames_free (GtkPrintWin32Devnames *devnames)
32 g_free (devnames->driver);
33 g_free (devnames->device);
34 g_free (devnames->output);
38 GtkPrintWin32Devnames *
39 gtk_print_win32_devnames_from_win32 (HGLOBAL global)
41 LPDEVNAMES win = GlobalLock (global);
42 gunichar2 *data = (gunichar2 *)win;
43 GtkPrintWin32Devnames *devnames = g_new (GtkPrintWin32Devnames, 1);
45 devnames->driver = g_utf16_to_utf8 (data + win->wDriverOffset,
46 -1, NULL, NULL, NULL);
47 devnames->device = g_utf16_to_utf8 (data + win->wDeviceOffset,
48 -1, NULL, NULL, NULL);
49 devnames->output = g_utf16_to_utf8 (data + win->wOutputOffset,
50 -1, NULL, NULL, NULL);
51 devnames->flags = win->wDefault;
53 GlobalUnlock (global);
59 gtk_print_win32_devnames_to_win32 (const GtkPrintWin32Devnames *devnames)
62 LPDEVNAMES windevnames;
64 gunichar2 *driver, *device, *output;
65 glong driver_len, device_len, output_len;
68 driver = g_utf8_to_utf16 (devnames->driver, -1, NULL, &driver_len, NULL);
69 device = g_utf8_to_utf16 (devnames->device, -1, NULL, &device_len, NULL);
70 output = g_utf8_to_utf16 (devnames->output, -1, NULL, &output_len, NULL);
72 global = GlobalAlloc (GMEM_MOVEABLE,
74 (driver_len + 1) * 2 +
75 (device_len + 1) * 2 +
76 (output_len + 1) * 2);
78 windevnames = GlobalLock (global);
79 data = (gunichar2 *)windevnames;
80 i = sizeof(DEVNAMES) / sizeof (gunichar2);
82 windevnames->wDriverOffset = i;
83 memcpy (data + i, driver, (driver_len + 1) * sizeof (gunichar2));
85 windevnames->wDeviceOffset = i;
86 memcpy (data + i, device, (device_len + 1) * sizeof (gunichar2));
88 windevnames->wOutputOffset = i;
89 memcpy (data + i, output, (output_len + 1) * sizeof (gunichar2));
91 windevnames->wDefault = devnames->flags;
92 GlobalUnlock (global);
102 gtk_print_win32_devnames_to_win32_from_printer_name (const char *printer_name)
105 GtkPrintWin32Devnames *devnames;
107 devnames = gtk_print_win32_devnames_from_printer_name(printer_name);
110 global = gtk_print_win32_devnames_to_win32 (devnames);
111 gtk_print_win32_devnames_free (devnames);
120 * Used to get printer device information from a printer name. This
121 * can fail if the user has no right to read printer properties, so
122 * this function can return NULL.
124 GtkPrintWin32Devnames *
125 gtk_print_win32_devnames_from_printer_name (const char *printer_name)
128 gunichar2* win32_printer_name;
129 GtkPrintWin32Devnames *devnames;
131 win32_printer_name = g_utf8_to_utf16 (printer_name, -1, NULL, NULL, NULL);
132 if (OpenPrinterW (win32_printer_name, &hprinter, NULL))
135 PRINTER_INFO_2W* printer_info;
137 GetPrinterW (hprinter, 2, NULL, 0, &needed);
138 printer_info = (PRINTER_INFO_2W* )g_malloc ((gsize) needed);
139 GetPrinterW (hprinter, 2, (LPBYTE) printer_info, needed, &needed);
140 devnames = g_new (GtkPrintWin32Devnames, 1);
141 devnames->driver = g_utf16_to_utf8 (printer_info->pDriverName, -1, NULL, NULL, NULL);
142 devnames->device = g_strdup (printer_name);
143 devnames->output = g_utf16_to_utf8 (printer_info->pPortName, -1, NULL, NULL, NULL);
145 ClosePrinter (hprinter);
146 g_free (printer_info);
150 /* Could not open printer */
153 g_free (win32_printer_name);