diff --git a/components/printers/win32/winprinters.inc b/components/printers/win32/winprinters.inc index 54bc10d557..3d99abf420 100644 --- a/components/printers/win32/winprinters.inc +++ b/components/printers/win32/winprinters.inc @@ -9,11 +9,9 @@ uses // todo: this ^ is a mess: mixed WinUtilPrn/Windows units clean... // todo: this should be a method, can not be atm because mixed units ^ -{$IFDEF USEUNICODE} -function GetCurrentDevMode(out DM:PDeviceModeW): Boolean; -{$ELSE} -function GetCurrentDevMode(out DM:PDeviceMode): Boolean; -{$ENDIF} + + +function GetCurrentDevModeW(out DM:PDeviceModeW): Boolean; var PDev: TPrinterDevice; begin @@ -21,7 +19,20 @@ begin if (Printer.Printers.Count > 0) then begin PDev := TPrinterDevice(Printer.Printers.Objects[Printer.PrinterIndex]); - DM := PDev.DevMode; + DM := PDev.DevModeW; + Result := DM <> nil; + end; +end; + +function GetCurrentDevModeA(out DM:PDeviceModeA): Boolean; +var + PDev: TPrinterDevice; +begin + Result := false; + if (Printer.Printers.Count > 0) then + begin + PDev := TPrinterDevice(Printer.Printers.Objects[Printer.PrinterIndex]); + DM := PDev.DevModeA; Result := DM <> nil; end; end; @@ -116,27 +127,27 @@ begin if (fLastHandleType=htNone) and (Printers.Count>0) then begin PDev:=TPrinterDevice(Printers.Objects[PrinterIndex]); - {$IFDEF USEUNICODE} - fDC:=CreateICW( - PWidechar(UTF8Decode(PDev.Driver)), - PWidechar(UTF8Decode(PDev.Device)), - PWidechar(UTF8Decode(PDev.Port)), - PDev.DevMode); - {$ELSE} - fDC:=CreateIC(PChar(PDev.Driver),PChar(PDev.Device), - PChar(PDev.Port),PDev.DevMode); - {$ENDIF} - if fDC=0 then - {$IFDEF USEUNICODE} + if UseUnicode then fDC:=CreateICW( - PWidechar('WINSPOOL'), + PWidechar(UTF8Decode(PDev.Driver)), PWidechar(UTF8Decode(PDev.Device)), PWidechar(UTF8Decode(PDev.Port)), - PDev.DevMode); - {$ELSE} - fDC:=CreateIC(PChar('WINSPOOL'),PChar(PDev.Device), - PChar(PDev.Port),PDev.DevMode); - {$ENDIF} + PDev.DevModeW) + else + fDC:=CreateIC(PChar(PDev.Driver),PChar(PDev.Device), + PChar(PDev.Port),PDev.DevModeA); + if fDC=0 then + begin + if UseUnicode then + fDC:=CreateICW( + PWidechar('WINSPOOL'), + PWidechar(UTF8Decode(PDev.Device)), + PWidechar(UTF8Decode(PDev.Port)), + PDev.DevModeW) + else + fDC:=CreateIC(PChar('WINSPOOL'),PChar(PDev.Device), + PChar(PDev.Port),PDev.DevModeA); + end; if fDC=0 then raise EPrinter.Create( Format('Invalid printer (DC=%d Driver=%s Device=%s Port=%s)', @@ -159,34 +170,34 @@ begin PDev:=TPrinterDevice(Printers.Objects[PrinterIndex]); try - //Device is only 32 chars long, - //if the Printername or share is longer than 32 chars, this will return 0 - {$IFDEF USEUNICODE} - fDC := CreateDCW(nil, PWidechar(UTF8Decode(PDev.Name)), nil, PDev.DevMode); - {$ELSE} - fDC := CreateDC(nil, PChar(PDev.Name), nil, PDev.DevMode); - {$ENDIF} - if fDC=0 then - {$IFDEF USEUNICODE} - fDC := CreateDCW(PWidechar('WINSPOOL'),PWidechar(UTF8Decode(PDev.Name)), nil, PDev.DevMode); - {$ELSE} - fDC := CreateDC(PChar('WINSPOOL'),PChar(PDev.Name), nil, PDev.DevMode); - {$ENDIF} - - {Workaround (hack) for Lexmark 1020 JetPrinter (Mono)} - if fDC=0 then - {$IFDEF USEUNICODE} - fDC:=CreateDCW(nil,PWidechar(UTF8Decode(PDev.Driver)),nil, PDev.DevMode); - {$ELSE} - fDC:=CreateDC(nil,PChar(PDev.Driver),nil, PDev.DevMode); - {$ENDIF} - if fDC=0 then - {$IFDEF USEUNICODE} - fDC:=CreateDCW(PWideChar('WINSPOOL'),PWideChar(UTF8Decode(PDev.Driver)),nil,PDev.DevMode); - {$ELSE} - fDC:=CreateDC(pChar('WINSPOOL'),PChar(PDev.Driver),nil,PDev.DevMode); - {$ENDIF} - + //Device is only 32 chars long, + //if the Printername or share is longer than 32 chars, this will return 0 + if UseUnicode then + fDC := CreateDCW(nil, PWidechar(UTF8Decode(PDev.Name)), nil, PDev.DevModeW) + else + fDC := CreateDC(nil, PChar(PDev.Name), nil, PDev.DevModeA); + if fDC=0 then + begin + if UseUnicode then + fDC := CreateDCW(PWidechar('WINSPOOL'),PWidechar(UTF8Decode(PDev.Name)), nil, PDev.DevModeW) + else + fDC := CreateDC(PChar('WINSPOOL'),PChar(PDev.Name), nil, PDev.DevModeA); + end; + {Workaround (hack) for Lexmark 1020 JetPrinter (Mono)} + if fDC=0 then + begin + if UseUnicode then + fDC:=CreateDCW(nil,PWidechar(UTF8Decode(PDev.Driver)),nil, PDev.DevModeW) + else + fDC:=CreateDC(nil,PChar(PDev.Driver),nil, PDev.DevModeA); + end; + if fDC=0 then + begin + if UseUnicode then + fDC:=CreateDCW(PWideChar('WINSPOOL'),PWideChar(UTF8Decode(PDev.Driver)),nil,PDev.DevModeW) + else + fDC:=CreateDC(pChar('WINSPOOL'),PChar(PDev.Driver),nil,PDev.DevModeA); + end; except on E:Exception do raise EPrinter.Create(Format('CreateDC Exception:"%s" (Error:"%s", '+ 'DC=%d Driver="%s" Device="%s" Port="%s")', [E.Message, @@ -244,14 +255,18 @@ begin // 1. Determine the required size of the buffer from the device, // and then allocate enough memory for it. - {$IFDEF USEUNICODE} - PDev.DevModeSize := DocumentPropertiesW(0, FPrinterHandle, Pwidechar(UTF8Decode(PDev.Name)), - nil, nil, 0); - {$ELSE} - PDev.DevModeSize := DocumentProperties(0, FPrinterHandle, pchar(PDev.Name), - nil, nil, 0); - {$ENDIF} - ReallocMem(Pdev.DevMode, PDev.DevModeSize); + if UseUnicode then + begin + PDev.DevModeSize := DocumentPropertiesW(0, FPrinterHandle, Pwidechar(UTF8Decode(PDev.Name)), + nil, nil, 0); + ReallocMem(Pdev.DevModeW, PDev.DevModeSize); + end + else + begin + PDev.DevModeSize := DocumentProperties(0, FPrinterHandle, pchar(PDev.Name), + nil, nil, 0); + ReallocMem(Pdev.DevModeA, PDev.DevModeSize); + end; if PDev.DevModeSize=0 then begin result := false; exit; @@ -259,16 +274,18 @@ begin // 2. Ask the device driver to initialize the DEVMODE buffer with // the default settings. - {$IFDEF USEUNICODE} - dwRet := DocumentPropertiesW(0, FPrinterHandle, PWideChar(UTF8Decode(Pdev.Name)), - PDev.DevMode, nil, DM_OUT_BUFFER); - {$ELSE} - dwRet := DocumentProperties(0, FPrinterHandle, pchar(Pdev.Name), - PDev.DevMode, nil, DM_OUT_BUFFER); - {$ENDIF} + if UseUnicode then + dwRet := DocumentPropertiesW(0, FPrinterHandle, PWideChar(UTF8Decode(Pdev.Name)), + PDev.DevModeW, nil, DM_OUT_BUFFER) + else + dwRet := DocumentProperties(0, FPrinterHandle, pchar(Pdev.Name), + PDev.DevModeA, nil, DM_OUT_BUFFER); result := (dwRet=IDOK); if not result then begin - ReallocMem(PDev.Devmode, 0); + if UseUnicode then + ReallocMem(PDev.DevmodeW, 0) + else + ReallocMem(PDev.DevmodeA, 0); exit; end; @@ -385,40 +402,38 @@ begin // see: http://support.microsoft.com/default.aspx?scid=kb;en-us;246772 Result := ''; if Win32Platform = VER_PLATFORM_WIN32_WINDOWS then - begin + begin //No unicode printer function on Win9x platform // Get PRINT_INFO_2 record size SetLastError(0); - {$IFDEF USEUNICODE} - BoolRes := EnumPrintersW(PRINTER_ENUM_DEFAULT, nil, 2, nil, 0, @Needed, @PrtCount); - {$ELSE} - BoolRes := EnumPrinters(PRINTER_ENUM_DEFAULT, nil, 2, nil, 0, - @Needed, @PrtCount); - {$ENDIF} + //if UseUnicode then + // BoolRes := EnumPrintersW(PRINTER_ENUM_DEFAULT, nil, 2, nil, 0, @Needed, @PrtCount); + //else + BoolRes := EnumPrinters(PRINTER_ENUM_DEFAULT, nil, 2, nil, 0, @Needed, @PrtCount); if not BoolRes and ((GetLastError <> ERROR_INSUFFICIENT_BUFFER) or (Needed = 0)) then Exit; // Get PRINT_INFO_2 record GetMem(PrintInfo2Buf, Needed); - {$IFDEF USEUNICODE} - BoolRes := EnumPrintersW(PRINTER_ENUM_DEFAULT, nil, 2, PrintInfo2Buf, - Needed, @Needed, @PrtCount); - {$ELSE} - BoolRes := EnumPrinters(PRINTER_ENUM_DEFAULT, nil, 2, PrintInfo2Buf, - Needed, @Needed, @PrtCount); - {$ENDIF} + //if UseUnicode then + // BoolRes := EnumPrintersW(PRINTER_ENUM_DEFAULT, nil, 2, PrintInfo2Buf, + // Needed, @Needed, @PrtCount); + //else + BoolRes := EnumPrinters(PRINTER_ENUM_DEFAULT, nil, 2, PrintInfo2Buf, + Needed, @Needed, @PrtCount); if not BoolRes then begin FreeMem(PrintInfo2Buf); Exit; end; - {$IFDEF USEUNICODE} - Result := UTF8Encode(widestring(PPRINTER_INFO_2(PrintInfo2Buf)^.pPrinterName)); - {$ELSE} - Result := PPRINTER_INFO_2(PrintInfo2Buf)^.pPrinterName; - Result := AnsiToUTF8(Result); - {$ENDIF} + //if UseUnicode then + // Result := UTF8Encode(widestring(PPRINTER_INFO_2W(PrintInfo2Buf)^.pPrinterName)); + //else + begin + Result := PPRINTER_INFO_2A(PrintInfo2Buf)^.pPrinterName; + Result := AnsiToUTF8(Result); + end; FreeMem(PrintInfo2Buf); end else @@ -431,11 +446,10 @@ begin SpoolerHandle := LoadLibrary(LibWinSpool); if SpoolerHandle = 0 then Exit; - {$IFDEF USEUNICODE} - Pointer(GetDefPrnFunc) := GetProcAddress(SpoolerHandle, 'GetDefaultPrinterW'); - {$ELSE} - Pointer(GetDefPrnFunc) := GetProcAddress(SpoolerHandle, 'GetDefaultPrinterA'); - {$ENDIF} + if UseUnicode then + Pointer(GetDefPrnFunc) := GetProcAddress(SpoolerHandle, 'GetDefaultPrinterW') + else + Pointer(GetDefPrnFunc) := GetProcAddress(SpoolerHandle, 'GetDefaultPrinterA'); if GetDefPrnFunc = nil then begin FreeLibrary(SpoolerHandle); @@ -444,15 +458,18 @@ begin Boolres := GetDefPrnFunc(nil, PrtCount); result := ''; if (prtcount>0) then begin - {$IFDEF USEUNICODE} - SetLength(AName, PrtCount-1); // this includes the #0 terminator - BoolRes := GetDefPrnFunc(@AName[1], prtCount); - result := UTF8Encode(AName); - {$ELSE} - SetLength(Result, PrtCount); // make room for printer name - BoolRes := GetDefPrnFunc(pchar(Result), prtCount); - Result := AnsiToUTF8(Result); - {$ENDIF} + if UseUnicode then + begin + SetLength(AName, PrtCount-1); // this includes the #0 terminator + BoolRes := GetDefPrnFunc(@AName[1], prtCount); + result := UTF8Encode(AName); + end + else + begin + SetLength(Result, PrtCount); // make room for printer name + BoolRes := GetDefPrnFunc(pchar(Result), prtCount); + Result := AnsiToUTF8(Result); + end; end; FreeLibrary(SpoolerHandle); end else @@ -467,8 +484,6 @@ begin Result := '' end; end; - - end; @@ -484,12 +499,11 @@ var i : Integer; DefaultPrinter : string; PDev : TPrinterDevice; - {$IFDEF USEUNICODE} - TmpDevMode : PDeviceModeW; - {$ELSE} - TmpDevMode : PDeviceMode; - {$ENDIF} + TmpDevModeW : PDeviceModeW; + TmpDevModeA : PDeviceMode; PrtStr : string; + BoolRes: LCLType.BOOL; + B: Boolean; begin {$IFDEF NOPRINTERS} Lst.Clear; @@ -502,22 +516,21 @@ begin //Evaluate buffer size Needed := 0; - {$IFDEF USEUNICODE} - EnumPrintersW(Flags, nil, Level, nil, 0, @Needed, @PrtCount); - {$ELSE} - EnumPrinters(Flags, nil, Level, nil, 0, @Needed, @PrtCount); - {$ENDIF} + if UseUnicode then + EnumPrintersW(Flags, nil, Level, nil, 0, @Needed, @PrtCount) + else + EnumPrinters(Flags, nil, Level, nil, 0, @Needed, @PrtCount); if Needed <> 0 then begin GetMem(Buffer, Needed); Fillchar(Buffer^, Needed, 0); try //Enumerate Printers - {$IFDEF USEUNICODE} - if EnumPrintersW(Flags, nil, Level, Buffer, Needed, @Needed, @PrtCount) then - {$ELSE} - if EnumPrinters(Flags, nil, Level, Buffer, Needed, @Needed, @PrtCount) then - {$ENDIF} + if UseUnicode then + BoolRes := EnumPrintersW(Flags, nil, Level, Buffer, Needed, @Needed, @PrtCount) + else + BoolRes := EnumPrinters(Flags, nil, Level, Buffer, Needed, @Needed, @PrtCount); + if BoolRes then begin InfoPrt := Buffer; for i := 0 to PrtCount - 1 do @@ -525,17 +538,22 @@ begin if Level = 2 then begin PDev := TPrinterDevice.Create; - {$IFDEF USEUNICODE} - PDev.Name := UTF8Encode(widestring(PPRINTER_INFO_2(InfoPrt)^.pPrinterName)); - PDev.Driver := UTF8Encode(widestring(PPRINTER_INFO_2(InfoPrt)^.pDriverName)); - PDev.Port := UTF8Encode(widestring(PPRINTER_INFO_2(InfoPrt)^.pPortName)); - {$ELSE} - PDev.Name := PPRINTER_INFO_2(InfoPrt)^.pPrinterName; - PDev.Driver := PPRINTER_INFO_2(InfoPrt)^.pDriverName; - PDev.Port := PPRINTER_INFO_2(InfoPrt)^.pPortName; - {$ENDIF} - TmpDevMode := PPRINTER_INFO_2(InfoPrt)^.pDevMode; - if TmpDevMode <> nil then + if UseUnicode then + begin + PDev.Name := UTF8Encode(widestring(PPRINTER_INFO_2W(InfoPrt)^.pPrinterName)); + PDev.Driver := UTF8Encode(widestring(PPRINTER_INFO_2W(InfoPrt)^.pDriverName)); + PDev.Port := UTF8Encode(widestring(PPRINTER_INFO_2W(InfoPrt)^.pPortName)); + TmpDevModeW := PPRINTER_INFO_2W(InfoPrt)^.pDevMode; + end + else + begin + PDev.Name := PPRINTER_INFO_2A(InfoPrt)^.pPrinterName; + PDev.Driver := PPRINTER_INFO_2A(InfoPrt)^.pDriverName; + PDev.Port := PPRINTER_INFO_2A(InfoPrt)^.pPortName; + TmpDevModeA := PPRINTER_INFO_2(InfoPrt)^.pDevMode; + end; + + if (UseUnicode and (TmpDevModeW <> nil)) or (not UseUnicode and (TmpDevModeA <> nil)) then begin // the devmode structure obtained this way have two problems // 1. It's not the full devmode, because it doesn't have @@ -544,33 +562,49 @@ begin // have not extra settings at all. // // PDev.DevMode:=PPRINTER_INFO_2(InfoPrt)^.PDevMode^; - {$IFDEF USEUNICODE} - PDev.Device := UTF8Encode(widestring(TmpDevMode^.dmDeviceName)); - {$ELSE} - PDev.Device := PChar(PByte(TmpDevMode^.dmDeviceName)); - {$ENDIF} - PDev.DefaultPaper := TmpDevMode^.dmPaperSize; - PDev.DefaultBin := TmpDevMode^.dmDefaultSource; + if UseUnicode then + begin + PDev.Device := UTF8Encode(widestring(TmpDevModeW^.dmDeviceName)); + PDev.DefaultPaper := TmpDevModeW^.dmPaperSize; + PDev.DefaultBin := TmpDevModeW^.dmDefaultSource; + end + else + begin + {$IF FPC_FULLVERSION>20602} + PDev.Device := PChar(TmpDevModeA^.dmDeviceName); + {$ELSE} + PDev.Device := PChar(PByte(TmpDevModeA^.dmDeviceName)); + {$ENDIF} + PDev.DefaultPaper := TmpDevModeA^.dmPaperSize; + PDev.DefaultBin := TmpDevModeA^.dmDefaultSource; + end; end else begin PDev.Device:=''; PDev.DefaultPaper:=0; PDev.DefaultBin := 0 end; - {$IFDEF USEUNICODE} - PrtStr := PDev.Name; - if CompareText(PrtStr, DefaultPrinter)<>0 then - {$ELSE} - PrtStr := AnsiToUTF8(PDev.Name); - if AnsiCompareText(PrtStr,DefaultPrinter)<>0 then - {$ENDIF} + if UseUnicode then + begin + PrtStr := PDev.Name; + B := CompareText(PrtStr, DefaultPrinter)<>0 + end + else + begin + PrtStr := AnsiToUTF8(PDev.Name); + B := AnsiCompareText(PrtStr,DefaultPrinter)<>0 + end; + if B then Lst.AddObject(PrtStr,PDev) else begin Lst.Insert(0,PrtStr); Lst.Objects[0]:=PDev; end; - Inc(InfoPrt,SizeOf(_PRINTER_INFO_2A)); + if UseUnicode then + Inc(InfoPrt,SizeOf(_PRINTER_INFO_2W)) + else + Inc(InfoPrt,SizeOf(_PRINTER_INFO_2A)); end; end; end; @@ -595,11 +629,8 @@ end; procedure TWinPrinter.DoEnumPapers(Lst: TStrings); var - {$IFDEF USEUNICODE} - Buffer : PWideChar; - {$ELSE} - Buffer : PChar; - {$ENDIF} + BufferW : PWideChar; + BufferA : PChar; PaperN : String; PaperC,i : Integer; Count : Integer; @@ -620,57 +651,61 @@ begin //Retreive the supported papers PaperC:=0; - {$IFDEF USEUNICODE} - Count := DeviceCapabilitiesW( - PWidechar(UTF8Decode(Pdev.Name)), - PWidechar(UTF8Decode(PDev.Port)), DC_PAPERNAMES, nil, nil); - {$ELSE} - Count := DeviceCapabilities(PChar(Pdev.Name), PCHar(PDev.Port), DC_PAPERNAMES, nil, nil); - {$ENDIF} + if UseUnicode then + Count := DeviceCapabilitiesW( + PWidechar(UTF8Decode(Pdev.Name)), + PWidechar(UTF8Decode(PDev.Port)), DC_PAPERNAMES, nil, nil) + else + Count := DeviceCapabilities(PChar(Pdev.Name), PCHar(PDev.Port), DC_PAPERNAMES, nil, nil); if Count<=0 then raise EPrinter.CreateFmt('DoEnumPapers error : %d, (%s)', [GetLastError,SysErrorMessage(GetLastError)]); try - {$IFDEF USEUNICODE} - GetMem(Buffer,64*SizeOf(Widechar)*Count); - PaperC := DeviceCapabilitiesW( - PWidechar(UTF8Decode(Pdev.Name)), - PWidechar(UTF8Decode(PDev.Port)), - DC_PAPERNAMES, - Buffer, - nil); - for i:=0 to PaperC-1 do + if UseUnicode then begin - PaperN:=UTF8Encode(Widestring(Buffer+i*64)); - Lst.Add(PaperN); - end; - {$ELSE} - GetMem(Buffer,64*Count); - PaperC:=DeviceCapabilities(PChar(Pdev.Name),PCHar(PDev.Port), - DC_PAPERNAMES,Buffer,nil); - for i:=0 to PaperC-1 do + GetMem(BufferW,64*SizeOf(Widechar)*Count); + PaperC := DeviceCapabilitiesW( + PWidechar(UTF8Decode(Pdev.Name)), + PWidechar(UTF8Decode(PDev.Port)), + DC_PAPERNAMES, + BufferW, + nil); + for i:=0 to PaperC-1 do + begin + PaperN:=UTF8Encode(Widestring(BufferW+i*64)); + Lst.Add(PaperN); + end; + end + else begin - PaperN:=StrPas(Buffer+i*64); - Lst.Add(PaperN); + GetMem(BufferA,64*Count); + PaperC:=DeviceCapabilities(PChar(Pdev.Name),PCHar(PDev.Port), + DC_PAPERNAMES,BufferA,nil); + for i:=0 to PaperC-1 do + begin + PaperN:=StrPas(BufferA+i*64); + Lst.Add(PaperN); + end; end; - {$ENDIF} finally - FreeMem(Buffer); + if UseUnicode then + FreeMem(BufferW) + else + FreeMem(BufferA); end; //Retreive the code of papers FillChar(ArPapers,SizeOf(ArPapers),0); - {$IFDEF USEUNICODE} - PaperC:=DeviceCapabilitiesW( - PWidechar(UTF8Decode(Pdev.Name)), - PWidechar(UTF8Decode(PDev.Port)), - DC_PAPERS, - PWidechar(@ArPapers[0]), - nil); - {$ELSE} - PaperC:=DeviceCapabilities(PChar(Pdev.Name),PChar(PDev.Port), - DC_PAPERS,PChar(@ArPapers[0]),nil); - {$ENDIF} + if UseUnicode then + PaperC:=DeviceCapabilitiesW( + PWidechar(UTF8Decode(Pdev.Name)), + PWidechar(UTF8Decode(PDev.Port)), + DC_PAPERS, + PWidechar(@ArPapers[0]), + nil) + else + PaperC:=DeviceCapabilities(PChar(Pdev.Name),PChar(PDev.Port), + DC_PAPERS,PChar(@ArPapers[0]),nil); if PaperC<=0 then raise EPrinter.CreateFmt('DoEnumPapers error : %d, (%s)', [GetLastError,SysErrorMessage(GetLastError)]) @@ -685,19 +720,28 @@ end; function TWinPrinter.DoGetPaperName: string; var i : Integer; - {$IFDEF USEUNICODE} - dm : PDeviceModeW; - {$ELSE} - dm : PDeviceMode; - {$ENDIF} + dmW : PDeviceModeW; + dmA : PDeviceModeA; begin Result:=inherited DoGetPaperName; - if GetCurrentDevMode(dm) then - with PaperSize.SupportedPapers do begin - i := IndexOfObject(TObject(ptrInt(dm^.dmPaperSize))); - if i>=0 then - result := PaperSize.SupportedPapers[i]; - end; + if UseUnicode then + begin + if GetCurrentDevModeW(dmW) then + with PaperSize.SupportedPapers do begin + i := IndexOfObject(TObject(ptrInt(dmW^.dmPaperSize))); + if i>=0 then + result := PaperSize.SupportedPapers[i]; + end; + end + else + begin + if GetCurrentDevModeA(dmA) then + with PaperSize.SupportedPapers do begin + i := IndexOfObject(TObject(ptrInt(dmA^.dmPaperSize))); + if i>=0 then + result := PaperSize.SupportedPapers[i]; + end; + end; end; function TWinPrinter.DoGetDefaultPaperName: string; @@ -719,18 +763,28 @@ end; procedure TWinPrinter.DoSetPaperName(aName: string); var i : Integer; - {$IFDEF USEUNICODE} - dm : PDeviceModeW; - {$ELSE} - dm : PDeviceMode; - {$ENDIF} + dmW : PDeviceModeW; + dmA : PDeviceModeA; begin inherited DoSetPaperName(aName); - if GetCurrentDevMode(dm) then begin - i:=PaperSize.SupportedPapers.IndexOf(aName); - if i<>-1 then begin - ClearDC; - dm^.dmPaperSize := SHORT(ptrint(PaperSize.SupportedPapers.Objects[i])); + if UseUnicode then + begin + if GetCurrentDevModeW(dmW) then begin + i:=PaperSize.SupportedPapers.IndexOf(aName); + if i<>-1 then begin + ClearDC; + dmW^.dmPaperSize := SHORT(ptrint(PaperSize.SupportedPapers.Objects[i])); + end; + end; + end + else + begin + if GetCurrentDevModeA(dmA) then begin + i:=PaperSize.SupportedPapers.IndexOf(aName); + if i<>-1 then begin + ClearDC; + dmA^.dmPaperSize := SHORT(ptrint(PaperSize.SupportedPapers.Objects[i])); + end; end; end; end; @@ -796,6 +850,7 @@ function TWinPrinter.DoSetPrinter(aName: string): Integer; var i: Integer; PDev: TPrinterDevice; + BoolRes: LCLType.BOOL; begin Result := inherited DoSetPrinter(aName); @@ -811,11 +866,11 @@ begin result := i else begin PDev := TPrinterDevice(Printers.Objects[i]); - {$IFDEF USEUNICODE} - if not OpenPrinterW(PWideChar(UTF8Decode(PDev.Name)), @fPrinterHandle, nil) then - {$ELSE} - if not OpenPrinter(PChar(PDev.Name), @fPrinterHandle, nil) then - {$ENDIF} + if UseUnicode then + BoolRes := OpenPrinterW(PWideChar(UTF8Decode(PDev.Name)), @fPrinterHandle, nil) + else + BoolRes := OpenPrinter(PChar(PDev.Name), @fPrinterHandle, nil); + if not BoolRes then begin FprinterHandle := 0; raise EPrinter.CreateFmt('OpenPrinter exception : %s', @@ -832,64 +887,97 @@ end; function TWinPrinter.DoGetCopies: Integer; var - {$IFDEF USEUNICODE} - dm: PDeviceModeW; - {$ELSE} - dm: PDeviceMode; - {$ENDIF} + dmW: PDeviceModeW; + dmA: PDeviceMode; + Boolres: Boolean; begin - if GetCurrentDevMode(dm) then begin - if dm^.dmCopies<>0 then - result := dm^.dmCopies; + if UseUnicode then + begin + Boolres := GetCurrentDevModeW(dmW); + if BoolRes then begin + if dmW^.dmCopies<>0 then + result := dmW^.dmCopies; + end; end else + begin + BoolRes := GetCurrentDevModeA(dmA); + if BoolRes then begin + if dmA^.dmCopies<>0 then + result := dmA^.dmCopies; + end; + end; + if Not BoolRes then Result:=inherited DoGetCopies; end; procedure TWinPrinter.DoSetCopies(aValue: Integer); var - {$IFDEF USEUNICODE} - dm: PDeviceModeW; - {$ELSE} - dm: PDeviceMode; - {$ENDIF} + dmW: PDeviceModeW; + dmA: PDeviceModeA; begin inherited DoSetCopies(aValue); - if (AValue>0) and GetCurrentDevMode(dm) then begin - ClearDC; - dm^.dmCopies := SHORT(aValue) + if UseUnicode then + begin + if (AValue>0) and GetCurrentDevModeW(dmW) then begin + ClearDC; + dmW^.dmCopies := SHORT(aValue) + end; + end + else + begin + if (AValue>0) and GetCurrentDevModeA(dmA) then begin + ClearDC; + dmA^.dmCopies := SHORT(aValue) + end; end; end; function TWinPrinter.DoGetOrientation: TPrinterOrientation; var - {$IFDEF USEUNICODE} - dm: PDeviceModeW; - {$ELSE} - dm: PDeviceMode; - {$ENDIF} + dmW: PDeviceModeW; + dmA: PDeviceModeA; begin Result:=inherited DoGetOrientation; - if GetCurrentDevMode(dm) then begin - case dm^.dmOrientation of - DMORIENT_PORTRAIT : result:=poPortrait; - DMORIENT_LANDSCAPE: result:=poLandscape; + if UseUnicode then + begin + if GetCurrentDevModeW(dmW) then begin + case dmW^.dmOrientation of + DMORIENT_PORTRAIT : result:=poPortrait; + DMORIENT_LANDSCAPE: result:=poLandscape; + end; + end; + end + else + begin + if GetCurrentDevModeA(dmA) then begin + case dmA^.dmOrientation of + DMORIENT_PORTRAIT : result:=poPortrait; + DMORIENT_LANDSCAPE: result:=poLandscape; + end; end; end; end; procedure TWinPrinter.DoSetOrientation(aValue: TPrinterOrientation); var - {$IFDEF USEUNICODE} - dm: PDeviceModeW; - {$ELSE} - dm: PDeviceMode; - {$ENDIF} + dmW: PDeviceModeW; + dmA: PDeviceModeA; begin inherited DoSetOrientation(aValue); - if GetCurrentDevMode(dm) then begin - ClearDC; - dm^.dmOrientation := Win32Orientations[aValue]; + if UseUnicode then + begin + if GetCurrentDevModeW(dmW) then begin + ClearDC; + dmW^.dmOrientation := Win32Orientations[aValue]; + end; + end + else + begin + if GetCurrentDevModeA(dmA) then begin + ClearDC; + dmA^.dmOrientation := Win32Orientations[aValue]; + end; end; end; @@ -932,8 +1020,8 @@ begin raise EPrinter.CreateFmt('GetPrinterState failed : %s', [SysErrorMessage(GetLastError)]); - Jobs := PPRINTER_INFO_2(InfoPrt)^.cJobs; - Status := PPRINTER_INFO_2(InfoPrt)^.Status; + Jobs := PPRINTER_INFO_2A(InfoPrt)^.cJobs; + Status := PPRINTER_INFO_2A(InfoPrt)^.Status; case Status of 0: Result := psReady; PRINTER_STATUS_PRINTING, @@ -975,16 +1063,15 @@ begin if (Printers.Count>0) then begin PDev:=TPrinterDevice(Printers.Objects[PrinterIndex]); - {$IFDEF USEUNICODE} - Count := DeviceCapabilitiesW( - PWidechar(UTF8Decode(Pdev.Name)), - PWidechar(UTF8Decode(PDev.Port)), - DC_COPIES, - nil,PDev.DevMode); - {$ELSE} - Count := DeviceCapabilities(PChar(Pdev.Name),PCHar(PDev.Port), - DC_COPIES,nil,PDev.DevMode); - {$ENDIF} + if UseUnicode then + Count := DeviceCapabilitiesW( + PWidechar(UTF8Decode(Pdev.Name)), + PWidechar(UTF8Decode(PDev.Port)), + DC_COPIES, + nil,PDev.DevModeW) + else + Count := DeviceCapabilities(PChar(Pdev.Name),PCHar(PDev.Port), + DC_COPIES,nil,PDev.DevModeA); Result := (Count>1); end else @@ -998,32 +1085,28 @@ begin if Printers.Count>0 then begin PDev:=TPrinterDevice(Printers.Objects[PrinterIndex]); - {$IFDEF USEUNICODE} - DocumentPropertiesW( - Widgetset.AppHandle, - FPrinterHandle, - PWidechar(UTF8Decode(PDev.Name)), - Pdev.DevMode, Pdev.DevMode, - DM_OUT_BUFFER or DM_IN_BUFFER or DM_IN_PROMPT); - {$ELSE} - DocumentProperties( - Widgetset.AppHandle, - FPrinterHandle, - pchar(PDev.Name), - Pdev.DevMode, Pdev.DevMode, - DM_OUT_BUFFER or DM_IN_BUFFER or DM_IN_PROMPT); - {$ENDIF} + if UseUnicode then + DocumentPropertiesW( + Widgetset.AppHandle, + FPrinterHandle, + PWidechar(UTF8Decode(PDev.Name)), + Pdev.DevModeW, Pdev.DevModeW, + DM_OUT_BUFFER or DM_IN_BUFFER or DM_IN_PROMPT) + else + DocumentProperties( + Widgetset.AppHandle, + FPrinterHandle, + pchar(PDev.Name), + Pdev.DevModeA, Pdev.DevModeA, + DM_OUT_BUFFER or DM_IN_BUFFER or DM_IN_PROMPT); //PrinterProperties(Widgetset.AppHandle,fPrinterHandle) end; end; procedure TWinPrinter.DoEnumBins(Lst : TStrings); var - {$IFDEF USEUNICODE} - Buffer : PWideChar; - {$ELSE} - Buffer : PChar; - {$ENDIF} + BufferW: PWideChar; + BufferA: PChar; BinN : String; BinC,i : Integer; Count : Integer; @@ -1048,58 +1131,62 @@ begin //Retreive the supported bins BinC:=0; - {$IFDEF USEUNICODE} - Count := DeviceCapabilitiesW( - PWidechar(UTF8Decode(Pdev.Name)), - PWidechar(UTF8Decode(PDev.Port)), DC_BINNAMES, nil, nil); - {$ELSE} - Count := DeviceCapabilities(PChar(Pdev.Name), PCHar(PDev.Port), DC_BINNAMES, nil, nil); - {$ENDIF} + if UseUnicode then + Count := DeviceCapabilitiesW( + PWidechar(UTF8Decode(Pdev.Name)), + PWidechar(UTF8Decode(PDev.Port)), DC_BINNAMES, nil, nil) + else + Count := DeviceCapabilities(PChar(Pdev.Name), PCHar(PDev.Port), DC_BINNAMES, nil, nil); if Count<=0 then raise EPrinter.CreateFmt('DoEnumBins error : %d, (%s)', [GetLastError,SysErrorMessage(GetLastError)]); try - {$IFDEF USEUNICODE} - GetMem(Buffer,24*SizeOf(Widechar)*Count); - BinC := DeviceCapabilitiesW( - PWidechar(UTF8Decode(Pdev.Name)), - PWidechar(UTF8Decode(PDev.Port)), - DC_BINNAMES, - Buffer, - nil); - for i:=0 to BinC-1 do + if UseUnicode then begin - BinN:=UTF8Encode(Widestring(Buffer+i*24)); - Lst.Add(BinN); - end; - {$ELSE} - GetMem(Buffer,24*Count); - BinC:=DeviceCapabilities(PChar(Pdev.Name),PCHar(PDev.Port), - DC_BINNAMES,Buffer,nil); - for i:=0 to BinC-1 do + GetMem(BufferW,24*SizeOf(Widechar)*Count); + BinC := DeviceCapabilitiesW( + PWidechar(UTF8Decode(Pdev.Name)), + PWidechar(UTF8Decode(PDev.Port)), + DC_BINNAMES, + BufferW, + nil); + for i:=0 to BinC-1 do + begin + BinN:=UTF8Encode(Widestring(BufferW+i*24)); + Lst.Add(BinN); + end; + end + else begin - BinN:=StrPas(Buffer+i*24); - Lst.Add(BinN); + GetMem(BufferA,24*Count); + BinC:=DeviceCapabilities(PChar(Pdev.Name),PCHar(PDev.Port), + DC_BINNAMES,BufferA,nil); + for i:=0 to BinC-1 do + begin + BinN:=StrPas(BufferA+i*24); + Lst.Add(BinN); + end; end; - {$ENDIF} finally - FreeMem(Buffer); + if UseUnicode then + Freemem(BufferW) + else + FreeMem(BufferA); end; //Retreive the code of bins FillChar(arBins,SizeOf(arBins),0); - {$IFDEF USEUNICODE} - BinC:=DeviceCapabilitiesW( - PWidechar(UTF8Decode(Pdev.Name)), - PWidechar(UTF8Decode(PDev.Port)), - DC_BINS, - PWidechar(@ArBins[0]), - nil); - {$ELSE} - BinC:=DeviceCapabilities(PChar(Pdev.Name),PChar(PDev.Port), - DC_BINS,PChar(@ArBins[0]),nil); - {$ENDIF} + if UseUnicode then + BinC:=DeviceCapabilitiesW( + PWidechar(UTF8Decode(Pdev.Name)), + PWidechar(UTF8Decode(PDev.Port)), + DC_BINS, + PWidechar(@ArBins[0]), + nil) + else + BinC:=DeviceCapabilities(PChar(Pdev.Name),PChar(PDev.Port), + DC_BINS,PChar(@ArBins[0]),nil); if BinC<=0 then raise EPrinter.CreateFmt('DoEnumBinss error : %d, (%s)', [GetLastError,SysErrorMessage(GetLastError)]) @@ -1130,39 +1217,48 @@ end; function TWinPrinter.DoGetBinName: string; var i : Integer; - {$IFDEF USEUNICODE} - dm: PDeviceModeW; - {$ELSE} - dm: PDeviceMode; - {$ENDIF} + dmW: PDeviceModeW; + dmA: PDeviceModeA; begin Result:=inherited DoGetBinName; - if GetCurrentDevMode(dm) then - with SupportedBins do begin - i := IndexOfObject(TObject(ptrInt(dm^.dmDefaultSource))); - if i>=0 then - result := Strings[i]; - end; + if UseUnicode then + begin + if GetCurrentDevModeW(dmW) then + with SupportedBins do begin + i := IndexOfObject(TObject(ptrInt(dmW^.dmDefaultSource))); + if i>=0 then + result := Strings[i]; + end; + end + else + begin + if GetCurrentDevModeA(dmA) then + with SupportedBins do begin + i := IndexOfObject(TObject(ptrInt(dmA^.dmDefaultSource))); + if i>=0 then + result := Strings[i]; + end; + end; end; procedure TWinPrinter.DoSetBinName(aName: string); var - i : Integer; - {$IFDEF USEUNICODE} - dm: PDeviceModeW; - {$ELSE} - dm: PDeviceMode; - {$ENDIF} + i : Integer; + dmW: PDeviceModeW; + dmA: PDeviceModeA; begin with SupportedBins do begin - if not GetCurrentDevMode(dm) then + if (UseUnicode and (not GetCurrentDevModeW(dmW))) or ((not UseUnicode) and (not GetCurrentDevModeA(dmA))) then raise EPrinter.Create('DoSetBinName error : unable to get current DevMode'); i := IndexOf(aName); if (i>=0) then begin ClearDC; - dm^.dmDefaultSource := SHORT(ptrint(Objects[i])); + if UseUnicode then + dmW^.dmDefaultSource := SHORT(ptrint(Objects[i])) + else + dmA^.dmDefaultSource := SHORT(ptrint(Objects[i])); end else inherited DoSetBinName(aName); // handle uknown bin name @@ -1171,5 +1267,8 @@ end; initialization Printer:=TWinPrinter.Create; + {$IFnDef WinCE} + if Win32Platform = VER_PLATFORM_WIN32_WINDOWS then UseUnicode := False; + {$ENDIF} {end.} diff --git a/components/printers/win32/winprinters_h.inc b/components/printers/win32/winprinters_h.inc index 65682ca10f..b2c86eb49e 100644 --- a/components/printers/win32/winprinters_h.inc +++ b/components/printers/win32/winprinters_h.inc @@ -22,6 +22,9 @@ uses Classes, SysUtils,Printers,LCLType,{Forms,}Windows;//,dialogs; +const + UseUnicode: Boolean = True; //Determined at runtime + Type THandleType = (htNone, htIC, htDC); diff --git a/components/printers/win32/winprndialogs.inc b/components/printers/win32/winprndialogs.inc index 557361eb74..81e37f9398 100644 --- a/components/printers/win32/winprndialogs.inc +++ b/components/printers/win32/winprndialogs.inc @@ -35,11 +35,10 @@ begin begin lpp := PtagPD(lParam); if (lParam<>0) and (lpp^.lCustData<>0) then - {$IFDEF USEUNICODE} - SetWindowTextW(hdlg, pwidechar(lpp^.lCustData)); - {$ELSE} - SetWindowText(hdlg,pChar(lpp^.lCustData)); - {$ENDIF} + if UseUnicode then + SetWindowTextW(hdlg, pwidechar(lpp^.lCustData)) + else + SetWindowText(hdlg,pChar(lpp^.lCustData)); Reposition(hdlg); end; Result := 0; @@ -53,11 +52,10 @@ begin begin lpp := PtagPSD(lParam); if (lParam<>0) and (lpp^.lCustData<>0) then - {$IFDEF USEUNICODE} - SetWindowTextW(hdlg, pwidechar(lpp^.lCustData)); - {$ELSE} - SetWindowText(hdlg,pChar(lpp^.lCustData)); - {$ENDIF} + if UseUnicode then + SetWindowTextW(hdlg, pwidechar(lpp^.lCustData)) + else + SetWindowText(hdlg,pChar(lpp^.lCustData)); Reposition(hdlg); end; Result := 0; @@ -72,13 +70,11 @@ var PDev : TPrinterDevice; DeviceMode : THandle; DevNames : PDevNames; - {$IFDEF USEUNICODE} - DevMode : PDeviceModeW; - St : PWidechar; - {$ELSE} - DevMode : PDeviceMode; - St : PChar; - {$ENDIF} + DevModeW : PDeviceModeW; + StW : PWidechar; + DevModeA : PDeviceMode; + StA : PChar; + BoolRes: BOOL; begin Result := False; if not Assigned(Printer) then Exit; @@ -91,11 +87,12 @@ begin hInstance := System.HInstance; lpfnPageSetupHook := @PageSetupHookProc; if Title<>'' then - {$IFDEF USEUNICODE} - lCustData := LPARAM(pWideChar(UTF8Decode(Title))) - {$ELSE} - lCustData := LPARAM(pChar(Utf8ToAnsi(Title))) - {$ENDIF} + begin + if UseUnicode then + lCustData := LPARAM(pWideChar(UTF8Decode(Title))) + else + lCustData := LPARAM(pChar(Utf8ToAnsi(Title))) + end else lCustData := 0; Flags := PSD_MARGINS or PSD_ENABLEPAGESETUPHOOK; @@ -105,31 +102,40 @@ begin // Pdev.DevMode has the required size, just copy to the global memory DeviceMode := GLobalAlloc(GHND, PDev.DevModeSize); try - DevMode := {$IFDEF USEUNICODE}PDeviceModeW{$ELSE}PDeviceMode{$ENDIF}(GlobalLock(DeviceMode)); + if UseUnicode then + DevModeW := PDeviceModeW(GlobalLock(DeviceMode)) + else + DevModeA := PDeviceModeA(GlobalLock(DeviceMode)); try - CopyMemory(DevMode, PDev.DevMode, Pdev.DevModeSize); + if UseUnicode then + CopyMemory(DevModeW, PDev.DevModeW, Pdev.DevModeSize) + else + CopyMemory(DevModeA, PDev.DevModeA, Pdev.DevModeSize); finally GlobalUnlock(DeviceMode); end; hDevMode := DeviceMode; - {$IFDEF USEUNICODE} - if PageSetupDlgW(@Lpp) then - {$ELSE} - if PageSetupDlg(@Lpp) then - {$ENDIF} + if UseUnicode then + BoolRes := PageSetupDlgW(@Lpp) + else + BoolRes := PageSetupDlg(@Lpp); + if BoolRes then begin - St := ''; + if UseUnicode then StW := '' else StA := ''; if Lpp.HdevNames <> 0 then begin DevNames := PDevNames(GlobalLock(lpp.hDevNames)); try - {$IFDEF USEUNICODE} - St := PWidechar(DEVNames) + DevNames^.wDeviceOffset; - Printer.SetPrinter(UTF8Encode(widestring(st))); - {$ELSE} - St := PChar(DevNames) + DevNames^.wDeviceOffset; - Printer.SetPrinter(St); - {$ENDIF} + if UseUnicode then + begin + StW := PWidechar(DEVNames) + DevNames^.wDeviceOffset; + Printer.SetPrinter(UTF8Encode(widestring(StW))); + end + else + begin + StA := PChar(DevNames) + DevNames^.wDeviceOffset; + Printer.SetPrinter(StA); + end finally GlobalUnlock(lpp.hDevNames); GlobalFree(lpp.hDevNames); @@ -145,11 +151,17 @@ begin if lpp.hDevMode <> 0 then begin - DevMode := {$IFDEF USEUNICODE}PDeviceModeW{$ELSE}PDeviceMode{$ENDIF}(GlobalLock(lpp.hDevMode)); + if UseUnicode then + DevModeW := PDeviceModeW(GlobalLock(lpp.hDevMode)) + else + DevModeA := PDeviceModeA(GlobalLock(lpp.hDevMode)); try //Set the properties for the selected printer PDev := TPrinterDevice(Printer.Printers.Objects[Printer.PrinterIndex]); - CopyMemory(PDev.DevMode, DevMode, PDev.DevModeSize); + if UseUnicode then + CopyMemory(PDev.DevModeW, DevModeW, PDev.DevModeSize) + else + CopyMemory(PDev.DevModeA, DevModeA, PDev.DevModeSize); finally GlobalUnlock(lpp.hDevMode); end; @@ -171,13 +183,11 @@ var PDev : TPrinterDevice; DeviceMode : THandle; DevNames : PDevNames; - {$IFDEF USEUNICODE} - DevMode : PDeviceModeW; - St : PWidechar; - {$ELSE} - DevMode : PDeviceMode; - St : PChar; - {$ENDIF} + DevModeW : PDeviceModeW; + StW : PWidechar; + DevModeA : PDeviceMode; + StA : PChar; + BoolRes: BOOL; begin Result:=False; if not Assigned(Printer) then Exit; @@ -190,11 +200,10 @@ begin hInstance := System.HInstance; lpfnSetupHook := @PrintHookProc; if Title<>'' then - {$IFDEF USEUNICODE} - lCustData := LPARAM(pWideChar(UTF8Decode(Title))) - {$ELSE} - lCustData := LPARAM(pChar(Utf8ToAnsi(Title))) - {$ENDIF} + if UseUnicode then + lCustData := LPARAM(pWideChar(UTF8Decode(Title))) + else + lCustData := LPARAM(pChar(Utf8ToAnsi(Title))) else lCustData := 0; Flags := PD_PRINTSETUP or PD_RETURNDC or PD_ENABLESETUPHOOK; @@ -203,32 +212,41 @@ begin // Pdev.DevMode has the required size, just copy to the global memory DeviceMode := GlobalAlloc(GHND, PDev.DevModeSize); try - DevMode := {$IFDEF USEUNICODE}PDeviceModeW{$ELSE}PDeviceMode{$ENDIF}(GlobalLock(DeviceMode)); + if UseUnicode then + DevModeW := PDeviceModeW(GlobalLock(DeviceMode)) + else + DevModeA := PDeviceModeA(GlobalLock(DeviceMode)); try - CopyMemory(DevMode, Pdev.DevMode, Pdev.DevModeSize); + if useUnicode then + CopyMemory(DevModeW, Pdev.DevModeW, Pdev.DevModeSize) + else + CopyMemory(DevModeA, Pdev.DevModeA, Pdev.DevModeSize); finally GlobalUnlock(DeviceMode); end; hDevMode := DeviceMode; - {$IFDEF USEUNICODE} - if PrintDlgW(@lpp) then - {$ELSE} - if PrintDlg(@lpp) then - {$ENDIF} + if UseUnicode then + BoolRes := PrintDlgW(@lpp) + else + BoolRes := PrintDlg(@lpp); + if BoolRes then begin - St := ''; + if UseUnicode then StW := '' else StA := ''; //Change Selected printer if lpp.hDevNames <> 0 then begin DevNames := PDevNames(GlobalLock(lpp.hDevNames)); try - {$IFDEF USEUNICODE} - St := PWidechar(DevNames) + DevNames^.wDeviceOffset; - Printer.SetPrinter(UTF8Encode(widestring(st))); - {$ELSE} - St := PChar(DevNames) + DevNames^.wDeviceOffset; - Printer.SetPrinter(St); - {$ENDIF} + if UseUnicode then + begin + StW := PWidechar(DevNames) + DevNames^.wDeviceOffset; + Printer.SetPrinter(UTF8Encode(widestring(StW))); + end + else + begin + StA := PChar(DevNames) + DevNames^.wDeviceOffset; + Printer.SetPrinter(StA); + end; finally GlobalUnlock(lpp.hDevNames); GlobalFree(lpp.hDevNames); @@ -239,11 +257,17 @@ begin if lpp.hDevMode <> 0 then begin - DevMode := {$IFDEF USEUNICODE}PDeviceModeW{$ELSE}PDeviceMode{$ENDIF}(GlobalLock(lpp.hDevMode)); + if UseUnicode then + DevModeW := PDeviceModeW(GlobalLock(lpp.hDevMode)) + else + DevModeA := PDeviceMode(GlobalLock(lpp.hDevMode)); try //Set the properties for the selected printer PDev := TPrinterDevice(Printer.Printers.Objects[Printer.PrinterIndex]); - CopyMemory(PDev.DevMode, DevMode, PDev.DevModeSize); + if UseUnicode then + CopyMemory(PDev.DevModeW, DevModeW, PDev.DevModeSize) + else + CopyMemory(PDev.DevModeA, DevModeA, PDev.DevModeSize); TWinPrinter(Printer).Handle := hDC; finally GlobalUnlock(lpp.hDevMode); @@ -266,13 +290,12 @@ var PDev : TPrinterDevice; DeviceMode : THandle; DevNames : PDevNames; - {$IFDEF USEUNICODE} - DevMode : PDeviceModeW; - St : PWidechar; - {$ELSE} - DevMode : PDeviceMode; - St : PChar; - {$ENDIF} + DevModeW : PDeviceModeW; + StW : PWidechar; + DevModeA : PDeviceModeA; + StA : PChar; + BoolRes: BOOL; + Index: Integer; begin Result := False; if not Assigned(Printer) then Exit; @@ -286,11 +309,12 @@ begin lpfnPrintHook := @PrintHookProc; lpfnSetupHook := @PrintHookProc; if Title<>'' then - {$IFDEF USEUNICODE} - lCustData := LPARAM(pWideChar(UTF8Decode(Title))) - {$ELSE} - lCustData := LPARAM(pChar(Utf8ToAnsi(Title))) - {$ENDIF} + begin + if UseUnicode then + lCustData := LPARAM(pWideChar(UTF8Decode(Title))) + else + lCustData := LPARAM(pChar(Utf8ToAnsi(Title))); + end else lCustData := 0; Flags := PD_ENABLEPRINTHOOK or PD_ENABLESETUPHOOK; @@ -316,9 +340,15 @@ begin // Pdev.DevMode has the required size, just copy to the global memory DeviceMode := GlobalAlloc(GHND, PDEV.DevModeSize); try - DevMode := {$IFDEF USEUNICODE}PDeviceModeW{$ELSE}PDeviceMode{$ENDIF}(GlobalLock(DeviceMode)); + if UseUnicode then + DevModeW := PDeviceModeW(GlobalLock(DeviceMode)) + else + DevModeA := PDeviceModeA(GlobalLock(DeviceMode)); try - CopyMemory(DevMode, PDev.DevMode, PDev.DevModeSize); + if UseUnicode then + CopyMemory(DevModeW, PDev.DevModeW, PDev.DevModeSize) + else + CopyMemory(DevModeA, PDev.DevModeA, PDev.DevModeSize); finally GlobalUnlock(DeviceMode); end; @@ -329,26 +359,32 @@ begin nToPage := Word(ToPage); nMinPage := Word(MinPage); nMaxPage := Word(MaxPage); - DevMode^.dmCopies := nCopies; - {$IFDEF USEUNICODE} - if PrintDlgW(@lpp) then - {$ELSE} - if PrintDlg(@lpp) then - {$ENDIF} + if UseUnicode then + DevModeW^.dmCopies := nCopies + else + DevModeA^.dmCopies := nCopies; + if UseUnicode then + BoolRes := PrintDlgW(@lpp) + else + BoolRes := PrintDlg(@lpp); + if BoolRes then begin - St:=''; + if UseUnicode then StW := '' else StA:=''; //Change Selected printer if lpp.hDevNames <> 0 then begin DevNames := PDevNames(GlobalLock(lpp.hDevNames)); try - {$IFDEF USEUNICODE} - St := PWidechar(DevNames) + DevNames^.wDeviceOffset; - Printer.SetPrinter(UTF8Encode(widestring(st))); - {$ELSE} - St := PChar(DevNames) + DevNames^.wDeviceOffset; - Printer.SetPrinter(St); - {$ENDIF} + if UseUnicode then + begin + StW := PWidechar(DevNames) + DevNames^.wDeviceOffset; + Printer.SetPrinter(UTF8Encode(widestring(stW))); + end + else + begin + StA := PChar(DevNames) + DevNames^.wDeviceOffset; + Printer.SetPrinter(StA); + end; finally GlobalUnlock(lpp.hDevNames); GlobalFree(lpp.hDevNames); @@ -360,19 +396,45 @@ begin // printer might have changed, check if new printer // support extended device modes PDev:=TPrinterDevice(Printer.Printers.Objects[Printer.PrinterIndex]); - if (lpp.hDevMode<>0) and (Pdev.DevMode<>nil) then + + if (lpp.hDevMode<>0) and ( (UseUnicode and (Pdev.DevModeW<>nil)) or + ((not UseUnicode) and (Pdev.DevModeA<>nil)))then + begin - DevMode:={$IFDEF USEUNICODE}PDeviceModeW{$ELSE}PDeviceMode{$ENDIF}(GlobalLock(lpp.hDevMode)); + if UseUnicode then + DevModeW := PDeviceModeW(GlobalLock(lpp.hDevMode)) + else + DevModeA := PDeviceModeA(GlobalLock(lpp.hDevMode)); try - CopyMemory(PDev.DevMode,DevMode,PDev.DevModeSize); - - if Printer.PaperSize.SupportedPapers.IndexOfObject(TObject(ptrint(DevMode^.dmPaperSize))) <> -1 - then - PDev.DevMode^.dmPaperSize := DevMode^.dmPaperSize + if UseUnicode then + CopyMemory(PDev.DevModeW,DevModeW,PDev.DevModeSize) else - PDev.DevMode^.dmPaperSize := PDev.DefaultPaper; + CopyMemory(PDev.DevModeA,DevModeA,PDev.DevModeSize); + if UseUnicode then + Index := Printer.PaperSize.SupportedPapers.IndexOfObject(TObject(ptrint(DevModeW^.dmPaperSize))) + else + Index := Printer.PaperSize.SupportedPapers.IndexOfObject(TObject(ptrint(DevModeA^.dmPaperSize))); + if Index <> -1 then + begin + if UseUnicode then + PDev.DevModeW^.dmPaperSize := DevModeW^.dmPaperSize + else + PDev.DevModeA^.dmPaperSize := DevModeA^.dmPaperSize + end + else + begin + if Useunicode then + PDev.DevModeW^.dmPaperSize := PDev.DefaultPaper + else + PDev.DevModeA^.dmPaperSize := PDev.DefaultPaper + end; if nCopies=1 then - Copies := DevMode^.dmCopies + begin + if UseUnicode then + Copies := DevModeW^.dmCopies + else + Copies := DevModeA^.dmCopies + end else Copies := nCopies; Printer.Copies := Copies; diff --git a/components/printers/win32/winutilprn.pas b/components/printers/win32/winutilprn.pas index f3f7f8dc51..5a67f0fd00 100644 --- a/components/printers/win32/winutilprn.pas +++ b/components/printers/win32/winutilprn.pas @@ -88,12 +88,6 @@ type end; PtagPD = ^tagPD; - {$IFDEF USEUNICODE} - LPTSTR = PWidechar; - {$ELSE} - LPTSTR = PChar; - {$ENDIF} - _PRINTER_DEFAULTSA = record pDatatype : LPSTR; pDevMode : LPDEVMODE; @@ -118,11 +112,7 @@ type pDriverName : LPTSTR; pComment : LPTSTR; pLocation : LPTSTR; - {$IFDEF USEUNICODE} - pDevMode : LPDEVMODEW; - {$ELSE} pDevMode : LPDEVMODE; - {$ENDIF} pSepFile : LPTSTR; pPrintProcessor : LPTSTR; pDatatype : LPTSTR; @@ -145,6 +135,41 @@ type PPRINTER_INFO_2 = ^PRINTER_INFO_2; LPPRINTER_INFO_2 = ^PRINTER_INFO_2; + _PRINTER_INFO_2W = record + pServerName : LPWSTR; + pPrinterName : LPWSTR; + pShareName : LPWSTR; + pPortName : LPWSTR; + pDriverName : LPWSTR; + pComment : LPWSTR; + pLocation : LPWSTR; + pDevMode : LPDEVMODEW; + pSepFile : LPWSTR; + pPrintProcessor : LPWSTR; + pDatatype : LPWSTR; + pParameters : LPWSTR; + pSecurityDescriptor : PSECURITY_DESCRIPTOR; + Attributes : DWORD; + Priority : DWORD; + DefaultPriority : DWORD; + StartTime : DWORD; + UntilTime : DWORD; + Status : DWORD; + cJobs : DWORD; + AveragePPM : DWORD; + end; + + PRINTER_INFO_2W = _PRINTER_INFO_2W; + PPRINTER_INFO_2W = ^_PRINTER_INFO_2W; + LPPRINTER_INFO_2W = ^_PRINTER_INFO_2W; + + //PRINTER_INFO_2 = PRINTER_INFO_2W; + //PPRINTER_INFO_2 = ^PRINTER_INFO_2; + //LPPRINTER_INFO_2 = ^PRINTER_INFO_2; + + + + _PRINTER_INFO_4A = record pPrinterName : LPSTR; pServerName : LPSTR; @@ -158,6 +183,19 @@ type PPRINTER_INFO_4 = ^PRINTER_INFO_4; LPPRINTER_INFO_4 = ^PRINTER_INFO_4; + _PRINTER_INFO_4W = record + pPrinterName : LPWSTR; + pServerName : LPWSTR; + Attributes : DWORD; + end; + PRINTER_INFO_4W = _PRINTER_INFO_4W; + PPRINTER_INFO_4W = ^_PRINTER_INFO_4W; + LPPRINTER_INFO_4W = ^_PRINTER_INFO_4W; + + //PRINTER_INFO_4 = PRINTER_INFO_4W; + //PPRINTER_INFO_4 = ^PRINTER_INFO_4; + //LPPRINTER_INFO_4 = ^PRINTER_INFO_4; + type { TPrinterDevice } @@ -170,11 +208,8 @@ type DefaultPaper: Short; DefaultBin: short; - {$IFDEF USEUNICODE} - DevMode: PDeviceModeW; - {$ELSE} - DevMode: PDeviceMode; - {$ENDIF} + DevModeW: PDeviceModeW; + DevModeA: PDeviceModeA; DevModeSize: integer; destructor Destroy; override; end; @@ -228,7 +263,8 @@ implementation destructor TPrinterDevice.Destroy; begin - ReallocMem(DevMode, 0); + ReallocMem(DevModeA, 0); + ReallocMem(DevModeW, 0); inherited Destroy; end;