Removed some of the XXX_reg macros now that we are using the standard
[wine] / dlls / wineps / driver.c
1 /*
2  * Exported functions from the PostScript driver.
3  *
4  * [Ext]DeviceMode, DeviceCapabilities, AdvancedSetupDialog. 
5  *
6  * Will need ExtTextOut for winword6 (urgh!)
7  *
8  * Copyright 1998  Huw D M Davies
9  *
10  */
11
12 #include <string.h>
13 #include "psdrv.h"
14 #include "debugtools.h"
15 #include "winuser.h"
16 #include "winspool.h"
17 #include "prsht.h"
18 #include "psdlg.h"
19
20 DEFAULT_DEBUG_CHANNEL(psdrv)
21
22
23 /************************************************************************
24  *
25  *              PSDRV_MergeDevmodes
26  *
27  * Updates dm1 with some fields from dm2
28  *
29  */
30 void PSDRV_MergeDevmodes(PSDRV_DEVMODEA *dm1, PSDRV_DEVMODEA *dm2,
31                          PRINTERINFO *pi)
32 {
33     /* some sanity checks here on dm2 */
34
35     if(dm2->dmPublic.dmFields & DM_ORIENTATION)
36         dm1->dmPublic.u1.s1.dmOrientation = dm2->dmPublic.u1.s1.dmOrientation;
37
38     /* NB PaperWidth is always < PaperLength */
39
40     if(dm2->dmPublic.dmFields & DM_PAPERSIZE) {
41         PAGESIZE *page;
42
43         for(page = pi->ppd->PageSizes; page; page = page->next) {
44             if(page->WinPage == dm2->dmPublic.u1.s1.dmPaperSize)
45                 break;
46         }
47         if(page) {
48             dm1->dmPublic.u1.s1.dmPaperSize = dm2->dmPublic.u1.s1.dmPaperSize;
49             dm1->dmPublic.u1.s1.dmPaperWidth = page->PaperDimension->x * 
50                                                                 254.0 / 72.0;
51             dm1->dmPublic.u1.s1.dmPaperLength = page->PaperDimension->y *
52                                                                 254.0 / 72.0;
53             TRACE("Changing page to %s %d x %d\n", page->FullName,
54                   dm1->dmPublic.u1.s1.dmPaperWidth,
55                   dm1->dmPublic.u1.s1.dmPaperLength );
56         } else {
57             TRACE("Trying to change to unsupported pagesize %d\n",
58                       dm2->dmPublic.u1.s1.dmPaperSize);
59         }
60     }
61
62     if(dm2->dmPublic.dmFields & DM_PAPERLENGTH) {
63         dm1->dmPublic.u1.s1.dmPaperLength = dm2->dmPublic.u1.s1.dmPaperLength; 
64         TRACE("Changing PaperLength to %d\n",
65               dm2->dmPublic.u1.s1.dmPaperLength);
66         FIXME("Changing PaperLength.  Do we adjust PaperSize?\n");
67     }
68
69     if(dm2->dmPublic.dmFields & DM_PAPERWIDTH) {
70         dm1->dmPublic.u1.s1.dmPaperWidth = dm2->dmPublic.u1.s1.dmPaperWidth; 
71         TRACE("Changing PaperWidth to %d\n",
72               dm2->dmPublic.u1.s1.dmPaperWidth);
73         FIXME("Changing PaperWidth.  Do we adjust PaperSize?\n");
74     }
75
76     if(dm2->dmPublic.dmFields & DM_SCALE) {
77         dm1->dmPublic.dmScale = dm2->dmPublic.dmScale;
78         TRACE("Changing Scale to %d\n", dm2->dmPublic.dmScale);
79     }
80
81     if(dm2->dmPublic.dmFields & DM_COPIES) {
82         dm1->dmPublic.dmCopies = dm2->dmPublic.dmCopies;
83         TRACE("Changing Copies to %d\n", dm2->dmPublic.dmCopies);
84     }
85
86     if(dm2->dmPublic.dmFields & DM_DEFAULTSOURCE) {
87         INPUTSLOT *slot;
88         
89         for(slot = pi->ppd->InputSlots; slot; slot = slot->next) {
90             if(slot->WinBin == dm2->dmPublic.dmDefaultSource)
91                 break;
92         }
93         if(slot) {
94             dm1->dmPublic.dmDefaultSource = dm2->dmPublic.dmDefaultSource;
95             TRACE("Changing bin to '%s'\n", slot->FullName);
96         } else {
97           TRACE("Trying to change to unsupported bin %d\n",
98                 dm2->dmPublic.dmDefaultSource);
99         }
100     }
101
102    if (dm2->dmPublic.dmFields & DM_DEFAULTSOURCE )
103        dm1->dmPublic.dmDefaultSource = dm2->dmPublic.dmDefaultSource;
104    if (dm2->dmPublic.dmFields & DM_PRINTQUALITY )
105        dm1->dmPublic.dmPrintQuality = dm2->dmPublic.dmPrintQuality;
106    if (dm2->dmPublic.dmFields & DM_COLOR )
107        dm1->dmPublic.dmColor = dm2->dmPublic.dmColor;
108    if (dm2->dmPublic.dmFields & DM_DUPLEX )
109        dm1->dmPublic.dmDuplex = dm2->dmPublic.dmDuplex;
110    if (dm2->dmPublic.dmFields & DM_YRESOLUTION )
111        dm1->dmPublic.dmYResolution = dm2->dmPublic.dmYResolution;
112    if (dm2->dmPublic.dmFields & DM_TTOPTION )
113        dm1->dmPublic.dmTTOption = dm2->dmPublic.dmTTOption;
114    if (dm2->dmPublic.dmFields & DM_COLLATE )
115        dm1->dmPublic.dmCollate = dm2->dmPublic.dmCollate;
116    if (dm2->dmPublic.dmFields & DM_FORMNAME )
117        lstrcpynA(dm1->dmPublic.dmFormName, dm2->dmPublic.dmFormName, CCHFORMNAME);
118    if (dm2->dmPublic.dmFields & DM_BITSPERPEL )
119        dm1->dmPublic.dmBitsPerPel = dm2->dmPublic.dmBitsPerPel;
120    if (dm2->dmPublic.dmFields & DM_PELSWIDTH )
121        dm1->dmPublic.dmPelsWidth = dm2->dmPublic.dmPelsWidth;
122    if (dm2->dmPublic.dmFields & DM_PELSHEIGHT )
123        dm1->dmPublic.dmPelsHeight = dm2->dmPublic.dmPelsHeight;
124    if (dm2->dmPublic.dmFields & DM_DISPLAYFLAGS )
125        dm1->dmPublic.dmDisplayFlags = dm2->dmPublic.dmDisplayFlags;
126    if (dm2->dmPublic.dmFields & DM_DISPLAYFREQUENCY )
127        dm1->dmPublic.dmDisplayFrequency = dm2->dmPublic.dmDisplayFrequency;
128    if (dm2->dmPublic.dmFields & DM_POSITION )
129        dm1->dmPublic.u1.dmPosition = dm2->dmPublic.u1.dmPosition;
130    if (dm2->dmPublic.dmFields & DM_LOGPIXELS )
131        dm1->dmPublic.dmLogPixels = dm2->dmPublic.dmLogPixels;
132    if (dm2->dmPublic.dmFields & DM_ICMMETHOD )
133        dm1->dmPublic.dmICMMethod = dm2->dmPublic.dmICMMethod;
134    if (dm2->dmPublic.dmFields & DM_ICMINTENT )
135        dm1->dmPublic.dmICMIntent = dm2->dmPublic.dmICMIntent;
136    if (dm2->dmPublic.dmFields & DM_MEDIATYPE )
137        dm1->dmPublic.dmMediaType = dm2->dmPublic.dmMediaType;
138    if (dm2->dmPublic.dmFields & DM_DITHERTYPE )
139        dm1->dmPublic.dmDitherType = dm2->dmPublic.dmDitherType;
140    if (dm2->dmPublic.dmFields & DM_PANNINGWIDTH )
141        dm1->dmPublic.dmPanningWidth = dm2->dmPublic.dmPanningWidth;
142    if (dm2->dmPublic.dmFields & DM_PANNINGHEIGHT )
143        dm1->dmPublic.dmPanningHeight = dm2->dmPublic.dmPanningHeight;
144
145     return;
146 }
147
148
149 /**************************************************************
150  *
151  *      PSDRV_AdvancedSetupDialog16     [WINEPS.93]
152  *
153  */
154 WORD WINAPI PSDRV_AdvancedSetupDialog16(HWND16 hwnd, HANDLE16 hDriver,
155                                          LPDEVMODEA devin, LPDEVMODEA devout)
156 {
157
158   TRACE("hwnd = %04x, hDriver = %04x devin=%p devout=%p\n", hwnd,
159         hDriver, devin, devout);
160   return IDCANCEL;
161 }
162
163 /****************************************************************
164  *       PSDRV_PaperDlgProc
165  *
166  * Dialog proc for 'Paper' propsheet
167  */
168 BOOL WINAPI PSDRV_PaperDlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM
169                                lParam)
170 {
171   PSDRV_DLGINFO *di;
172   int i, Cursel = 0; 
173   PAGESIZE *ps;
174
175
176   switch(msg) {
177   case WM_INITDIALOG:
178     di = (PSDRV_DLGINFO*)((PROPSHEETPAGEA*)lParam)->lParam;
179     SetWindowLongA(hwnd, DWL_USER, (LONG)di);
180
181     for(ps = di->pi->ppd->PageSizes, i = 0; ps; ps = ps->next, i++) {
182       SendDlgItemMessageA(hwnd, IDD_PAPERS, LB_INSERTSTRING, i, 
183                           (LPARAM)ps->FullName);
184       if(di->pi->Devmode->dmPublic.u1.s1.dmPaperSize == ps->WinPage)
185         Cursel = i;
186     }
187     SendDlgItemMessageA(hwnd, IDD_PAPERS, LB_SETCURSEL, Cursel, 0);
188
189     CheckRadioButton(hwnd, IDD_ORIENT_PORTRAIT, IDD_ORIENT_LANDSCAPE,
190                      di->pi->Devmode->dmPublic.u1.s1.dmOrientation ==
191                      DMORIENT_PORTRAIT ? IDD_ORIENT_PORTRAIT :
192                      IDD_ORIENT_LANDSCAPE);
193     break;
194
195   case WM_COMMAND:
196     di = (PSDRV_DLGINFO *)GetWindowLongA(hwnd, DWL_USER);
197     switch(LOWORD(wParam)) {
198     case IDD_PAPERS:
199       if(HIWORD(wParam) == LBN_SELCHANGE) {
200         Cursel = SendDlgItemMessageA(hwnd, LOWORD(wParam), LB_GETCURSEL, 0, 0);
201         for(i = 0, ps = di->pi->ppd->PageSizes; i < Cursel; i++, ps = ps->next)
202           ;
203         TRACE("Setting pagesize to item %d Winpage = %d\n", Cursel,
204               ps->WinPage);
205         di->dlgdm->dmPublic.u1.s1.dmPaperSize = ps->WinPage;
206       }
207       break;
208     case IDD_ORIENT_PORTRAIT:
209     case IDD_ORIENT_LANDSCAPE:
210       TRACE("Setting orientation to %s\n", wParam == IDD_ORIENT_PORTRAIT ?
211             "portrait" : "landscape");
212       di->dlgdm->dmPublic.u1.s1.dmOrientation = wParam == IDD_ORIENT_PORTRAIT ?
213         DMORIENT_PORTRAIT : DMORIENT_LANDSCAPE;
214       break;
215     }
216     break;
217
218   case WM_NOTIFY:
219    {
220     NMHDR *nmhdr = (NMHDR *)lParam;
221     di = (PSDRV_DLGINFO *)GetWindowLongA(hwnd, DWL_USER);
222     switch(nmhdr->code) {
223     case PSN_APPLY:
224       memcpy(di->pi->Devmode, di->dlgdm, sizeof(PSDRV_DEVMODEA));
225       SetWindowLongA(hwnd, DWL_MSGRESULT, PSNRET_NOERROR);
226       return TRUE;
227       break;
228
229     default:
230       return FALSE;
231       break;
232     }
233     break;
234    }
235    
236   default:
237     return FALSE;
238   }
239   return TRUE;
240 }
241
242
243 static void (WINAPI* pInitCommonControls) (void);
244 static HPROPSHEETPAGE (WINAPI* pCreatePropertySheetPage) (LPCPROPSHEETPAGEA);
245 static int (WINAPI* pPropertySheet) (LPCPROPSHEETHEADERA);
246
247 /***************************************************************
248  *
249  *      PSDRV_ExtDeviceMode16   [WINEPS.90]
250  *
251  * Just returns default devmode at the moment
252  */
253 INT16 WINAPI PSDRV_ExtDeviceMode16(HWND16 hwnd, HANDLE16 hDriver,
254                                    LPDEVMODEA lpdmOutput, LPSTR lpszDevice,
255                                    LPSTR lpszPort, LPDEVMODEA lpdmInput,
256                                    LPSTR lpszProfile, WORD fwMode)
257 {
258   PRINTERINFO *pi = PSDRV_FindPrinterInfo(lpszDevice);
259   if(!pi) return -1;
260
261   TRACE("(hwnd=%04x, hDriver=%04x, devOut=%p, Device='%s', Port='%s', devIn=%p, Profile='%s', Mode=%04x)\n",
262 hwnd, hDriver, lpdmOutput, lpszDevice, lpszPort, lpdmInput, lpszProfile,
263 fwMode);
264
265   if(!fwMode)
266     return sizeof(DEVMODEA); /* Just copy dmPublic bit of PSDRV_DEVMODE */
267
268   if((fwMode & DM_MODIFY) && lpdmInput) {
269     TRACE("DM_MODIFY set. devIn->dmFields = %08lx\n", lpdmInput->dmFields);
270     PSDRV_MergeDevmodes(pi->Devmode, (PSDRV_DEVMODEA *)lpdmInput, pi);
271   }
272
273   if(fwMode & DM_PROMPT) {
274     HINSTANCE hinstComctl32, hinstWineps32 = LoadLibraryA("WINEPS");
275     HPROPSHEETPAGE hpsp[1];
276     PROPSHEETPAGEA psp;
277     PROPSHEETHEADERA psh;
278     PSDRV_DLGINFO *di;
279     PSDRV_DEVMODEA *dlgdm;
280
281     hinstComctl32 = LoadLibraryA("comctl32.dll");
282     pInitCommonControls = (void*)GetProcAddress(hinstComctl32,
283                                                 "InitCommonControls");
284     pCreatePropertySheetPage = (void*)GetProcAddress(hinstComctl32,
285                                                     "CreatePropertySheetPage");
286     pPropertySheet = (void*)GetProcAddress(hinstComctl32, "PropertySheet"); 
287     memset(&psp,0,sizeof(psp));
288     dlgdm = HeapAlloc( PSDRV_Heap, 0, sizeof(*dlgdm) );
289     memcpy(dlgdm, pi->Devmode, sizeof(*dlgdm));
290     di = HeapAlloc( PSDRV_Heap, 0, sizeof(*di) );
291     di->pi = pi;
292     di->dlgdm = dlgdm;
293     psp.dwSize = sizeof(psp);
294     psp.hInstance = hinstWineps32;
295     psp.u.pszTemplate = "PAPER";
296     psp.u2.pszIcon = NULL;
297     psp.pfnDlgProc = PSDRV_PaperDlgProc;
298     psp.lParam = (LPARAM)di;
299     hpsp[0] = pCreatePropertySheetPage(&psp);
300
301     memset(&psh, 0, sizeof(psh));
302     psh.dwSize = sizeof(psh);
303     psh.pszCaption = "Setup";
304     psh.nPages = 1;
305     psh.hwndParent = hwnd;
306     psh.u3.phpage = hpsp;
307     
308     pPropertySheet(&psh);
309     
310   }
311   if(fwMode & DM_UPDATE)
312     FIXME("Mode DM_UPDATE.  Just do the same as DM_COPY\n");
313
314   if((fwMode & DM_COPY) || (fwMode & DM_UPDATE)) {
315     memcpy(lpdmOutput, pi->Devmode, sizeof(DEVMODEA));
316   }
317   return IDOK;
318 }
319
320 /**************************************************************
321  *
322  *       PSDRV_ExtDeviceMode
323  */
324 INT PSDRV_ExtDeviceMode(LPSTR lpszDriver, HWND hwnd, LPDEVMODEA lpdmOutput,
325                         LPSTR lpszDevice, LPSTR lpszPort, LPDEVMODEA lpdmInput,
326                         LPSTR lpszProfile, DWORD dwMode)
327 {
328     return PSDRV_ExtDeviceMode16(hwnd, 0, lpdmOutput, lpszDevice, lpszPort,
329                                  lpdmInput, lpszProfile, dwMode);
330 }
331
332 /***********************************************************************
333  *
334  *      PSDRV_DeviceCapabilities16      [WINEPS.91]
335  *
336  */
337 DWORD WINAPI PSDRV_DeviceCapabilities16(LPCSTR lpszDevice, LPCSTR lpszPort,
338                                         WORD fwCapability, LPSTR lpszOutput,
339                                         LPDEVMODEA lpDevMode)
340 {
341   PRINTERINFO *pi;
342   DEVMODEA *lpdm;
343   pi = PSDRV_FindPrinterInfo(lpszDevice);
344
345   TRACE("Cap=%d. Got PrinterInfo = %p\n", fwCapability, pi);
346
347
348   if (!pi) {
349           ERR("no printerinfo for %s, return 0!\n",lpszDevice);
350           return 0;
351   }
352
353
354   lpdm = lpDevMode ? lpDevMode : (DEVMODEA *)pi->Devmode;
355
356   switch(fwCapability) {
357
358   case DC_PAPERS:
359     {
360       PAGESIZE *ps;
361       WORD *wp = (WORD *)lpszOutput;
362       int i = 0;
363
364       for(ps = pi->ppd->PageSizes; ps; ps = ps->next, i++)
365         if(lpszOutput != NULL)
366           *wp++ = ps->WinPage;
367       return i;
368     }
369
370   case DC_PAPERSIZE:
371     {
372       PAGESIZE *ps;
373       POINT16 *pt = (POINT16 *)lpszOutput;
374       int i = 0;
375
376       for(ps = pi->ppd->PageSizes; ps; ps = ps->next, i++)
377         if(lpszOutput != NULL) {
378           pt->x = ps->PaperDimension->x * 254.0 / 72.0;
379           pt->y = ps->PaperDimension->y * 254.0 / 72.0;
380           pt++;
381         }
382       return i;
383     }
384
385   case DC_PAPERNAMES:
386     {
387       PAGESIZE *ps;
388       char *cp = lpszOutput;
389       int i = 0;
390
391       for(ps = pi->ppd->PageSizes; ps; ps = ps->next, i++)
392         if(lpszOutput != NULL) {
393           lstrcpynA(cp, ps->FullName, 64);
394           cp += 64;
395         }
396       return i;
397     }
398
399   case DC_ORIENTATION:
400     return pi->ppd->LandscapeOrientation ? pi->ppd->LandscapeOrientation : 90;
401
402   case DC_BINS:
403     {
404       INPUTSLOT *slot;
405       WORD *wp = (WORD *)lpszOutput;
406       int i = 0;
407
408       for(slot = pi->ppd->InputSlots; slot; slot = slot->next, i++)
409         if(lpszOutput != NULL)
410           *wp++ = slot->WinBin;
411       return i;
412     }
413
414   case DC_BINNAMES:
415     {
416       INPUTSLOT *slot;
417       char *cp = lpszOutput;
418       int i = 0;
419       
420       for(slot = pi->ppd->InputSlots; slot; slot = slot->next, i++)
421         if(lpszOutput != NULL) {
422           lstrcpynA(cp, slot->FullName, 24);
423           cp += 24;
424         }
425       return i;
426     }
427
428   case DC_BINADJUST:
429     FIXME("DC_BINADJUST: stub.\n");
430     return DCBA_FACEUPNONE;
431
432   case DC_ENUMRESOLUTIONS:
433     {
434       LONG *lp = (LONG*)lpszOutput;
435
436       if(lpszOutput != NULL) {
437         lp[0] = (LONG)pi->ppd->DefaultResolution;
438         lp[1] = (LONG)pi->ppd->DefaultResolution;
439       }
440       return 1;
441     }
442
443   case DC_COPIES:
444     FIXME("DC_COPIES: returning %d.  Is this correct?\n", lpdm->dmCopies);
445     return lpdm->dmCopies;
446
447   case DC_DRIVER:
448     return lpdm->dmDriverVersion;
449
450   case DC_DATATYPE_PRODUCED:
451     FIXME("DATA_TYPE_PRODUCED: stub.\n");
452     return -1; /* simulate that the driver supports 'RAW' */
453
454   case DC_DUPLEX:
455     FIXME("DC_DUPLEX: returning %d.  Is this correct?\n", lpdm->dmDuplex);
456     return lpdm->dmDuplex;
457
458   case DC_EMF_COMPLIANT:
459     FIXME("DC_EMF_COMPLIANT: stub.\n");
460     return -1; /* simulate that the driver do not support EMF */
461
462   case DC_EXTRA:
463     return lpdm->dmDriverExtra;
464
465   case DC_FIELDS:
466     return lpdm->dmFields;
467
468   case DC_FILEDEPENDENCIES:
469     FIXME("DC_FILEDEPENDENCIES: stub.\n");
470     return 0;
471
472   case DC_MAXEXTENT:
473     {
474       PAGESIZE *ps;
475       int i;
476       POINT ptMax;
477       ptMax.x = ptMax.y = 0;
478
479       if(lpszOutput == NULL)
480         return -1;
481  
482       i = 0;
483       for(ps = pi->ppd->PageSizes; ps; ps = ps->next, i++) {
484         if(ps->PaperDimension->x > ptMax.x)
485           ptMax.x = ps->PaperDimension->x;
486         if(ps->PaperDimension->y > ptMax.y)
487           ptMax.y = ps->PaperDimension->y;
488       }
489       *((POINT*)lpszOutput) = ptMax;
490       return 1;
491     }
492
493   case DC_MINEXTENT:
494     {
495       PAGESIZE *ps;
496       int i;
497       POINT ptMax;
498       ptMax.x = ptMax.y = 0;
499
500       if(lpszOutput == NULL)
501         return -1;
502  
503       i = 0;
504       for(ps = pi->ppd->PageSizes; ps; ps = ps->next, i++) {
505         if(ps->PaperDimension->x > ptMax.x)
506           ptMax.x = ps->PaperDimension->x;
507         if(ps->PaperDimension->y > ptMax.y)
508           ptMax.y = ps->PaperDimension->y;
509       }
510       *((POINT*)lpszOutput) = ptMax;
511       return 1;
512     }
513
514   case DC_SIZE:
515     return lpdm->dmSize;
516
517   case DC_TRUETYPE:
518     FIXME("DC_TRUETYPE: stub\n");
519     return DCTT_SUBDEV;
520
521   case DC_VERSION:
522     return lpdm->dmSpecVersion;
523
524   default:
525     FIXME("Unsupported capability %d\n", fwCapability);
526   }
527   return -1;
528 }
529
530 /**************************************************************
531  *
532  *     PSDRV_DeviceCapabilities
533  */
534 DWORD PSDRV_DeviceCapabilities(LPSTR lpszDriver, LPCSTR lpszDevice,
535                                LPCSTR lpszPort, WORD fwCapability,
536                                LPSTR lpszOutput, LPDEVMODEA lpdm)
537 {
538     return PSDRV_DeviceCapabilities16(lpszDevice, lpszPort, fwCapability,
539                                       lpszOutput, lpdm);
540 }
541
542 /***************************************************************
543  *
544  *      PSDRV_DeviceMode16      [WINEPS.13]
545  *
546  */
547 void WINAPI PSDRV_DeviceMode16(HWND16 hwnd, HANDLE16 hDriver,
548 LPSTR lpszDevice, LPSTR lpszPort)
549 {
550     PSDRV_ExtDeviceMode16( hwnd, hDriver, NULL, lpszDevice, lpszPort, NULL, 
551                            NULL, DM_PROMPT );
552     return;
553 }
554
555 #if 0
556 typedef struct {
557   DWORD nPages;
558   DWORD Unknown;
559   HPROPSHEETPAGE hPages[10];
560 } EDMPS;
561
562 INT PSDRV_ExtDeviceModePropSheet(HWND hwnd, LPSTR lpszDevice, LPSTR lpszPort,
563                                  LPVOID pPropSheet)
564 {
565     EDMPS *ps = pPropSheet;
566     PROPSHEETPAGE psp;
567
568     psp->dwSize = sizeof(psp);
569     psp->hInstance = 0x1234;
570     
571     ps->nPages = 1;
572     
573 }
574
575 #endif