]> Pileus Git - ~andy/gtk/blob - gtk/gtkprint-win32.c
Merge branch 'windows_list'
[~andy/gtk] / gtk / gtkprint-win32.c
1 /* GTK - The GIMP Toolkit
2  * gtkprintoperation.c: Print Operation
3  * Copyright (C) 2006, Red Hat, Inc.
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  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
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.
14  *
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.
19  */
20
21 #ifndef _MSC_VER
22 #define _WIN32_WINNT 0x0500
23 #define WINVER _WIN32_WINNT
24 #endif
25
26 #include "config.h"
27 #include "gtkprint-win32.h"
28
29 void
30 gtk_print_win32_devnames_free (GtkPrintWin32Devnames *devnames)
31 {
32   g_free (devnames->driver);
33   g_free (devnames->device);
34   g_free (devnames->output);
35   g_free (devnames);
36 }
37
38 GtkPrintWin32Devnames *
39 gtk_print_win32_devnames_from_win32 (HGLOBAL global)
40 {
41   LPDEVNAMES win = GlobalLock (global);
42   gunichar2 *data = (gunichar2 *)win;
43   GtkPrintWin32Devnames *devnames = g_new (GtkPrintWin32Devnames, 1);
44   
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;
52   
53   GlobalUnlock (global);
54
55   return devnames;
56 }
57
58 HGLOBAL 
59 gtk_print_win32_devnames_to_win32 (const GtkPrintWin32Devnames *devnames)
60 {
61   HGLOBAL global;
62   LPDEVNAMES windevnames;
63   gunichar2 *data;
64   gunichar2 *driver, *device, *output;
65   glong driver_len, device_len, output_len;
66   int i;
67
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);
71
72   global = GlobalAlloc (GMEM_MOVEABLE, 
73                         sizeof (DEVNAMES) + 
74                         (driver_len + 1) * 2 + 
75                         (device_len + 1) * 2 +
76                         (output_len + 1) * 2);
77
78   windevnames = GlobalLock (global);
79   data = (gunichar2 *)windevnames;
80   i = sizeof(DEVNAMES) / sizeof (gunichar2);
81
82   windevnames->wDriverOffset = i;
83   memcpy (data + i, driver, (driver_len + 1) * sizeof (gunichar2));
84   i += driver_len + 1;
85   windevnames->wDeviceOffset = i;
86   memcpy (data + i, device, (device_len + 1) * sizeof (gunichar2));
87   i += device_len + 1;
88   windevnames->wOutputOffset = i;
89   memcpy (data + i, output, (output_len + 1) * sizeof (gunichar2));
90   i += output_len + 1;
91   windevnames->wDefault = devnames->flags;
92   GlobalUnlock (global);
93
94   g_free (driver);
95   g_free (device);
96   g_free (output);
97
98   return global;
99 }
100
101 HGLOBAL 
102 gtk_print_win32_devnames_to_win32_from_printer_name (const char *printer_name)
103 {
104   HGLOBAL global;
105   GtkPrintWin32Devnames *devnames;
106
107   devnames = gtk_print_win32_devnames_from_printer_name(printer_name);
108   if (devnames)
109     {
110       global = gtk_print_win32_devnames_to_win32 (devnames);
111       gtk_print_win32_devnames_free (devnames);
112     }
113   else
114     global = NULL;
115
116   return global;
117 }
118
119 /*
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.
123  */
124 GtkPrintWin32Devnames *
125 gtk_print_win32_devnames_from_printer_name (const char *printer_name)
126 {
127   HANDLE hprinter;
128   gunichar2* win32_printer_name;
129   GtkPrintWin32Devnames *devnames;
130
131   win32_printer_name = g_utf8_to_utf16 (printer_name, -1, NULL, NULL, NULL);
132   if (OpenPrinterW (win32_printer_name, &hprinter, NULL))
133     {
134       DWORD needed;
135       PRINTER_INFO_2W* printer_info;
136
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);
144       devnames->flags  = 0;
145       ClosePrinter (hprinter);
146       g_free (printer_info);
147     }
148   else
149     {
150       /* Could not open printer */
151       devnames = NULL;
152     }
153   g_free (win32_printer_name);
154
155   return devnames;
156 }