summaryrefslogtreecommitdiff
path: root/src/PlotMgt/PlotMgt.cxx
blob: 12e51e98d6390c07a87c4bea5ae7e9d14a997889 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// File:      PlotMgt.cxx
// Created:   22-JAN-99
// Author:    DCB
// Copyright: MatraDatavision 1999

#ifdef WNT
# include <windows.h>
# pragma comment(lib, "winspool.lib")
# pragma comment(lib, "user32.lib"  )
# include <Standard_ProgramError.hxx>
#endif // WNT

#include <stdio.h>

#include <PlotMgt.ixx>

//==============================================================================
static int __NumberOfTypes         = PlotMgt_TOPP_ListString + 1;
static int __NumberOfOrigins       = PlotMgt_TOO_TOPRIGHT + 1;
static int __NumberOfQualities     = PlotMgt_TOQ_BEST + 1;
static int __NumberOfImageFormats  = PlotMgt_IF_GIF + 1;
static int __NumberOfPlottingTypes = PlotMgt_PT_PENEMULATOR + 1;
static int __NumberOfPaperFormats  = PlotMgt_PF_MONARCH + 1;

static TCollection_AsciiString aTypeStr;
static TCollection_AsciiString anOriginStr;
static TCollection_AsciiString aQualityStr;
static TCollection_AsciiString anImageFormatStr;
static TCollection_AsciiString aPlottingTypeStr;
static TCollection_AsciiString aPaperFormatStr;

static const char* __TypeOfParameter[] = {
  "undefined",  "boolean",
  "integer",    "real",
  "string",     "list_string"
};

static const char* __TypeOfOrigin [] = {
  "Center",       "Bottom left",
  "Bottom right", "Top left",
  "Top right"
};

static const char* __TypeOfQuality [] = {
  "Draft", "Normal",
  "High",  "Best"
};

static const char* __ImageFormat [] = {
  "DISABLE", "ENABLE",
  "XWD",     "BMP",
  "GIF"
};

static const char* __PlottingType [] = {
  "Raster", "PenEmulator"
};

static const char* __PaperFormats [] = {
  "UserDefined", "A0",    "A1",        "A2",       "A3",
  "A4",          "A5",    "Letter",    "Legal",    "Statement",
  "Executive",   "Folio", "Quarto",    "Envelope", "Monarch"
};

//==============================================================================
#ifdef WNT
static BOOL IsWindows95 () {
  OSVERSIONINFO os;
  os.dwOSVersionInfoSize = sizeof ( OSVERSIONINFO );
  GetVersionEx (&os);
  return (os.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS);
}
#endif

//==============================================================================
Handle(TColStd_HSequenceOfAsciiString) PlotMgt::DeviceList ()
{
  Handle(TColStd_HSequenceOfAsciiString) aList = new TColStd_HSequenceOfAsciiString ();
#ifdef WNT
  char                    buff[ MAX_PATH ];
  DWORD                   dwLen, dwLenRet;
  TCollection_AsciiString dev, defDev;
  buff[ dwLen =
    GetProfileString ( "Windows", "Device", ",,,", buff, MAX_PATH ) ] = '\x00';
  defDev = TCollection_AsciiString ( buff );
  defDev = defDev.Token ( ",", 1 );
  dwLen  = 0;
  if (IsWindows95()) {
    ///////////////////////// W95 ////////////////////////////
    PRINTER_INFO_5* ppi = NULL;
    if (!EnumPrinters (PRINTER_ENUM_LOCAL, NULL, 5, NULL, 0, &dwLen, &dwLenRet)) {
      if (GetLastError () == ERROR_INSUFFICIENT_BUFFER) {
        ppi = ( PRINTER_INFO_5* )HeapAlloc (GetProcessHeap (), HEAP_GENERATE_EXCEPTIONS, dwLen);
        EnumPrinters (PRINTER_ENUM_LOCAL, NULL, 5, ( LPBYTE )ppi, dwLen, &dwLen, &dwLenRet);
      } else {
        wsprintf (buff, "PlotMgt::DeviceList -> Could not obtain device list (%d)", GetLastError ());
        Standard_ProgramError :: Raise ( buff );
        dwLenRet = 0;
      }  // end else
    }
    for (dwLen = 0; dwLen < dwLenRet; ++ dwLen) {
      dev = TCollection_AsciiString ( ppi[ dwLen ].pPrinterName );
      if (dev.IsEqual (defDev)) aList -> Prepend ( dev );
      else                      aList -> Append ( dev );
    }  // end for
    if (ppi != NULL)
      HeapFree (GetProcessHeap (), 0, ( LPVOID )ppi);
  } else {
    ///////////////////////// WNT ////////////////////////////
    PRINTER_INFO_4* ppi = NULL;
    if (!EnumPrinters (PRINTER_ENUM_LOCAL | PRINTER_ENUM_CONNECTIONS,
                       NULL, 4, NULL, 0, &dwLen, &dwLenRet)) {
      if (GetLastError () == ERROR_INSUFFICIENT_BUFFER) {
        ppi = ( PRINTER_INFO_4* )HeapAlloc (GetProcessHeap (), HEAP_GENERATE_EXCEPTIONS, dwLen);
        EnumPrinters (PRINTER_ENUM_LOCAL | PRINTER_ENUM_CONNECTIONS,
                      NULL, 4, ( LPBYTE )ppi, dwLen, &dwLen, &dwLenRet);
      } else {
        wsprintf (buff, "PlotMgt::DeviceList -> Could not obtain device list (%d)", GetLastError () );
        Standard_ProgramError :: Raise ( buff );
        dwLenRet = 0;
      }  // end else
    }
    for (dwLen = 0; dwLen < dwLenRet; ++ dwLen) {
      dev = TCollection_AsciiString ( ppi[ dwLen ].pPrinterName );
      if (dev.IsEqual (defDev)) aList -> Prepend ( dev );
      else                      aList -> Append ( dev );
    }  // end for
    if (ppi != NULL)
      HeapFree (GetProcessHeap (), 0, ( LPVOID )ppi);
  } // WINNT
#else
  const char *cmd = "lpstat -v";
  char buf[BUFSIZ];
  FILE *ptr;
  if ((ptr = popen(cmd, "r")) != NULL) {
    while (fgets(buf, BUFSIZ, ptr) != NULL)
      aList -> Append (buf);
    pclose (ptr);
  }
#endif // WNT
  return aList;
}

//==============================================================================
PlotMgt_TypeOfPlotterParameter PlotMgt::TypeFromString (
                               TCollection_AsciiString& aTypeString)
{
  aTypeString.LowerCase ();
  aTypeString.LeftAdjust ();
  aTypeString.RightAdjust ();
  for (int i = 0; i < __NumberOfTypes; i++)
    if (aTypeString.IsEqual(__TypeOfParameter[i]))
      return PlotMgt_TypeOfPlotterParameter(i);
  return PlotMgt_TOPP_Undefined;
}

//==============================================================================
TCollection_AsciiString PlotMgt::StringFromType (
                        const PlotMgt_TypeOfPlotterParameter aType)
{
  aTypeStr = TCollection_AsciiString (__TypeOfParameter [(int)aType]);
  return aTypeStr;
}

//==============================================================================
PlotMgt_TypeOfOrigin PlotMgt::OriginFromString (
                     TCollection_AsciiString& anOriginString)
{
  anOriginString.LeftAdjust ();
  anOriginString.RightAdjust ();
  for (int i = 0; i < __NumberOfOrigins; i++)
    if (anOriginString.IsEqual(__TypeOfOrigin[i]))
      return PlotMgt_TypeOfOrigin(i);
  return PlotMgt_TOO_CENTER;
}

//==============================================================================
TCollection_AsciiString PlotMgt::StringFromOrigin (
                        const PlotMgt_TypeOfOrigin anOrigin)
{
  anOriginStr = TCollection_AsciiString (__TypeOfOrigin [(int)anOrigin]);
  return anOriginStr;
}

//==============================================================================
PlotMgt_TypeOfQuality PlotMgt::QualityFromString (
                      TCollection_AsciiString& aQualityString)
{
  aQualityString.LeftAdjust ();
  aQualityString.RightAdjust ();
  for (int i = 0; i < __NumberOfQualities; i++)
    if (aQualityString.IsEqual(__TypeOfQuality[i]))
      return PlotMgt_TypeOfQuality(i);
  return PlotMgt_TOQ_DRAFT;
}

//==============================================================================
TCollection_AsciiString PlotMgt::StringFromQuality (
                        const PlotMgt_TypeOfQuality aQuality)
{
  aQualityStr = TCollection_AsciiString (__TypeOfQuality [(int)aQuality]);
  return aQualityStr;
}

//==============================================================================
PlotMgt_ImageFormat PlotMgt::ImageFormatFromString (
                             TCollection_AsciiString& anImageFormatString)
{
  anImageFormatString.LeftAdjust ();
  anImageFormatString.RightAdjust ();
  for (int i = 0; i < __NumberOfImageFormats; i++)
    if (anImageFormatString.IsEqual(__ImageFormat[i]))
      return PlotMgt_ImageFormat(i);
  return PlotMgt_IF_DISABLE;
}

//==============================================================================
TCollection_AsciiString PlotMgt::StringFromImageFormat (
                        const PlotMgt_ImageFormat anImageFormat)
{
  anImageFormatStr = TCollection_AsciiString (__ImageFormat [(int)anImageFormat]);
  return anImageFormatStr;
}

//==============================================================================
PlotMgt_PlottingType PlotMgt::PlottingTypeFromString (
                              TCollection_AsciiString& aPlottingTypeString)
{
  aPlottingTypeString.LeftAdjust ();
  aPlottingTypeString.RightAdjust ();
  for (int i = 0; i < __NumberOfPlottingTypes; i++)
    if (aPlottingTypeString.IsEqual(__PlottingType[i]))
      return PlotMgt_PlottingType(i);
  return PlotMgt_PT_RASTER;
}

//==============================================================================
TCollection_AsciiString PlotMgt::StringFromPlottingType (
                        const PlotMgt_PlottingType aPlottingType)
{
  aPlottingTypeStr = TCollection_AsciiString (__PlottingType [(int)aPlottingType]);
  return aPlottingTypeStr;
}

//==============================================================================
//==============================================================================
//==============================================================================
static Standard_Real aPaperWidths [] = {
  // UserDefined
  00.00,
  // A0, A1, A2, A3, A4, A5
  84.10,  59.40,  42.00,  29.70,  21.00,  14.80,
  // Letter, Legal, Statement, Executive, Folio, Quarto
  21.59,  21.59,  13.97,  18.41,  21.59,  21.50,
  // Envelope, Monarch
  11.00,   9.84
};

static Standard_Real aPaperLengths [] = {
  // UserDefined
  00.00,
  // A0, A1, A2, A3, A4, A5
  118.9,  84.10,  59.40,  42.00,  29.70,  21.00,
  // Letter, Legal, Statement, Executive, Folio, Quarto
  27.94,  35.56,  21.59,  26.67,  33.02,  27.50,
  // Envelope, Monarch
  23.00,  19.04
};

//==============================================================================
PlotMgt_PaperFormat PlotMgt::PaperFormatFromString (
                             TCollection_AsciiString& aPaperFormatString)
{
  aPaperFormatString.LeftAdjust ();
  aPaperFormatString.RightAdjust ();
  for (int i = 0; i < __NumberOfPaperFormats; i++)
    if (aPaperFormatString.IsEqual(__PaperFormats[i]))
      return PlotMgt_PaperFormat(i);
  return PlotMgt_PF_A4;
}

//==============================================================================
TCollection_AsciiString PlotMgt::StringFromPaperFormat (
                        const PlotMgt_PaperFormat aPaperFormat)
{
  aPaperFormatStr = TCollection_AsciiString (__PaperFormats [(int)aPaperFormat]);
  return aPaperFormatStr;
}

//==============================================================================
void PlotMgt::PaperSize (TCollection_AsciiString& aFormat,
                         Standard_Real& aWidth,
                         Standard_Real& aLength)
{
  PlotMgt_PaperFormat aFmt = PlotMgt::PaperFormatFromString (aFormat);
  aWidth  = aPaperWidths  [(int)aFmt];
  aLength = aPaperLengths [(int)aFmt];
}