Enabling UML mainpoint
[dia.git] / app / win32print.c
blob52771f70b2cc4106f39bf3f637b50c55d35af49d
1 /* Dia -- an diagram creation/manipulation program
2 * Copyright (C) 1998 Alexander Larsson
4 * win32print.c - output file directly to a windoze printer
5 * Don't try it with a *non* PostScript printer !
7 * Copyright (C) 2001 Hans Breuer
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #include <config.h>
26 #include <windows.h>
27 #include <winspool.h>
29 #include <stdlib.h>
30 #include <stdio.h>
32 #include <fcntl.h>
33 #include <io.h>
35 #include <glib.h>
37 #include "win32print.h"
39 static void
40 PrintError (const char* s, DWORD err)
42 if (0 != err)
44 char* lpBuffer;
45 /* get the Windows message */
46 FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER
47 | FORMAT_MESSAGE_FROM_SYSTEM
48 | FORMAT_MESSAGE_IGNORE_INSERTS,
49 NULL,
50 err,
51 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
52 (char*)&lpBuffer,
53 0,NULL);
54 g_print ("%s : %s", s, lpBuffer);
55 LocalFree (lpBuffer);
59 const char*
60 win32_printer_default (void)
62 static char sName[1024];
63 GetProfileString("windows", "device", "", sName, sizeof(sName));
64 if (strchr (sName, ','))
65 *strchr (sName, ',') = 0;
66 else if ( strlen (sName) < 1)
67 strcpy (sName, "unknown");
69 return sName;
72 static HANDLE hPrinter = NULL;
73 static ADDJOB_INFO_1* pJob = NULL;
74 static HANDLE hFile = INVALID_HANDLE_VALUE;
76 FILE*
77 win32_printer_open (char* sName)
79 BYTE data [_MAX_PATH*2];
80 DWORD dwID = 0;
81 DWORD dwSizeRequired=0;
82 int err = 0;
84 if (!OpenPrinter (sName,
85 &hPrinter,
86 NULL))
88 g_print ("Failed to open printer : %s\n", sName);
89 return NULL;
92 if (!AddJob (hPrinter, 1, data, sizeof(data), &dwSizeRequired))
94 PrintError ("Failed to add job", GetLastError());
96 else
98 pJob = (ADDJOB_INFO_1*)data;
99 hFile = CreateFile (pJob->Path,
100 GENERIC_WRITE,
101 0, /* no share */
102 NULL, /* default security */
103 OPEN_ALWAYS,
104 FILE_ATTRIBUTE_NORMAL,
105 NULL); /* template */
106 if (INVALID_HANDLE_VALUE == hFile)
108 PrintError ("Failed to CreateFile", GetLastError());
110 else
112 int hnd = _open_osfhandle ((long)hFile, _O_APPEND);
113 if (-1 != hnd)
114 return _fdopen (hnd, "wb");
115 else
116 PrintError ("Failed to _open_osfhandle", 0);
120 return NULL;
124 win32_printer_close (FILE* f)
126 int ret = 0;
128 fflush (f);
129 if (!pJob || !ScheduleJob (hPrinter, pJob->JobId))
131 PrintError ("Failed to schedule job", GetLastError());
132 ret = GetLastError();
135 if (f)
136 fclose (f);
137 CloseHandle (hFile);
138 ClosePrinter (hPrinter);
139 return ret;