Mercurial > hg > xemacs-beta
annotate src/device-msw.c @ 5117:3742ea8250b5 ben-lisp-object ben-lisp-object-final-ws-year-2005
Checking in final CVS version of workspace 'ben-lisp-object'
author | Ben Wing <ben@xemacs.org> |
---|---|
date | Sat, 26 Dec 2009 00:20:27 -0600 |
parents | facf3239ba30 |
children | e0db3c197671 |
rev | line source |
---|---|
428 | 1 /* device functions for mswindows. |
2 Copyright (C) 1994, 1995 Board of Trustees, University of Illinois. | |
3 Copyright (C) 1994, 1995 Free Software Foundation, Inc. | |
771 | 4 Copyright (C) 2000, 2001, 2002 Ben Wing. |
428 | 5 |
6 This file is part of XEmacs. | |
7 | |
8 XEmacs is free software; you can redistribute it and/or modify it | |
9 under the terms of the GNU General Public License as published by the | |
10 Free Software Foundation; either version 2, or (at your option) any | |
11 later version. | |
12 | |
13 XEmacs is distributed in the hope that it will be useful, but WITHOUT | |
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
19 along with XEmacs; see the file COPYING. If not, write to | |
20 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
21 Boston, MA 02111-1307, USA. */ | |
22 | |
23 /* Synched up with: Not in FSF. */ | |
24 | |
771 | 25 /* This file Mule-ized 8-11-2000. */ |
26 | |
428 | 27 /* Authorship: |
28 | |
29 Original authors: Jamie Zawinski and the FSF | |
30 Rewritten by Ben Wing and Chuck Thompson. | |
31 Rewritten for mswindows by Jonathan Harris, November 1997 for 21.0. | |
510 | 32 Print support added by Kirill Katsnelson, July 2000. |
428 | 33 */ |
34 | |
771 | 35 #define NEED_MSWINDOWS_COMMCTRL |
36 #define NEED_MSWINDOWS_OBJBASE /* for CoInitialize */ | |
428 | 37 |
38 #include <config.h> | |
39 #include "lisp.h" | |
40 | |
872 | 41 #include "device-impl.h" |
800 | 42 #include "events.h" |
43 #include "faces.h" | |
44 #include "frame.h" | |
45 | |
872 | 46 #include "console-msw-impl.h" |
428 | 47 #include "console-stream.h" |
442 | 48 #include "objects-msw.h" |
800 | 49 |
428 | 50 #include "sysdep.h" |
51 | |
52 /* win32 DDE management library globals */ | |
53 #ifdef HAVE_DRAGNDROP | |
54 DWORD mswindows_dde_mlid; | |
657 | 55 int mswindows_dde_enable; |
428 | 56 HSZ mswindows_dde_service; |
57 HSZ mswindows_dde_topic_system; | |
903 | 58 HSZ mswindows_dde_topic_eval; |
59 HSZ mswindows_dde_item_result; | |
428 | 60 HSZ mswindows_dde_item_open; |
61 #endif | |
62 | |
63 Lisp_Object Qinit_pre_mswindows_win, Qinit_post_mswindows_win; | |
442 | 64 Lisp_Object Qdevmodep; |
428 | 65 |
510 | 66 static Lisp_Object Q_allow_selection; |
67 static Lisp_Object Q_allow_pages; | |
68 static Lisp_Object Q_selected_page_button; | |
69 static Lisp_Object Qselected_page_button; | |
70 | |
1204 | 71 static const struct memory_description mswindows_device_data_description_1 [] = { |
72 { XD_LISP_OBJECT, offsetof (struct mswindows_device, fontlist) }, | |
73 { XD_END } | |
74 }; | |
75 | |
76 extern const struct sized_memory_description mswindows_device_data_description; | |
77 | |
78 const struct sized_memory_description mswindows_device_data_description = { | |
79 sizeof (struct mswindows_device), mswindows_device_data_description_1 | |
80 }; | |
81 | |
1346 | 82 static const struct memory_description msprinter_device_data_description_1 [] = { |
83 { XD_LISP_OBJECT, offsetof (struct msprinter_device, name) }, | |
84 { XD_LISP_OBJECT, offsetof (struct msprinter_device, devmode) }, | |
85 { XD_LISP_OBJECT, offsetof (struct msprinter_device, fontlist) }, | |
86 { XD_END } | |
87 }; | |
88 | |
89 extern const struct sized_memory_description msprinter_device_data_description; | |
90 | |
91 const struct sized_memory_description msprinter_device_data_description = { | |
92 sizeof (struct msprinter_device), msprinter_device_data_description_1 | |
93 }; | |
94 | |
771 | 95 static Lisp_Object allocate_devmode (DEVMODEW *src_devmode, int do_copy, |
96 Lisp_Object src_name, struct device *d); | |
428 | 97 |
98 /************************************************************************/ | |
99 /* helpers */ | |
100 /************************************************************************/ | |
101 | |
102 static Lisp_Object | |
440 | 103 build_syscolor_string (int idx) |
428 | 104 { |
442 | 105 return (idx < 0 ? Qnil : mswindows_color_to_string (GetSysColor (idx))); |
428 | 106 } |
107 | |
108 static Lisp_Object | |
109 build_syscolor_cons (int index1, int index2) | |
110 { | |
111 Lisp_Object color1, color2; | |
112 struct gcpro gcpro1; | |
113 GCPRO1 (color1); | |
114 color1 = build_syscolor_string (index1); | |
115 color2 = build_syscolor_string (index2); | |
116 RETURN_UNGCPRO (Fcons (color1, color2)); | |
117 } | |
118 | |
119 static Lisp_Object | |
120 build_sysmetrics_cons (int index1, int index2) | |
121 { | |
122 return Fcons (index1 < 0 ? Qnil : make_int (GetSystemMetrics (index1)), | |
123 index2 < 0 ? Qnil : make_int (GetSystemMetrics (index2))); | |
124 } | |
125 | |
440 | 126 static Lisp_Object |
127 build_devicecaps_cons (HDC hdc, int index1, int index2) | |
128 { | |
129 return Fcons (index1 < 0 ? Qnil : make_int (GetDeviceCaps (hdc, index1)), | |
130 index2 < 0 ? Qnil : make_int (GetDeviceCaps (hdc, index2))); | |
131 } | |
132 | |
428 | 133 |
134 /************************************************************************/ | |
440 | 135 /* display methods */ |
428 | 136 /************************************************************************/ |
137 | |
138 static void | |
2286 | 139 mswindows_init_device (struct device *d, Lisp_Object UNUSED (props)) |
428 | 140 { |
141 HDC hdc; | |
771 | 142 WNDCLASSEXW wc; |
428 | 143 |
144 DEVICE_CLASS (d) = Qcolor; | |
145 DEVICE_INFD (d) = DEVICE_OUTFD (d) = -1; | |
146 init_baud_rate (d); | |
147 init_one_device (d); | |
148 | |
149 d->device_data = xnew_and_zero (struct mswindows_device); | |
150 hdc = CreateCompatibleDC (NULL); | |
771 | 151 assert (hdc != NULL); |
152 DEVICE_MSWINDOWS_HCDC (d) = hdc; | |
440 | 153 DEVICE_MSWINDOWS_FONTLIST (d) = mswindows_enumerate_fonts (hdc); |
442 | 154 DEVICE_MSWINDOWS_UPDATE_TICK (d) = GetTickCount (); |
428 | 155 |
156 /* Register the main window class */ | |
771 | 157 wc.cbSize = sizeof (wc); |
428 | 158 wc.style = CS_OWNDC; /* One DC per window */ |
159 wc.lpfnWndProc = (WNDPROC) mswindows_wnd_proc; | |
160 wc.cbClsExtra = 0; | |
161 wc.cbWndExtra = MSWINDOWS_WINDOW_EXTRA_BYTES; | |
162 /* This must match whatever is passed to CreateWIndowEx, NULL is ok | |
163 for this. */ | |
771 | 164 wc.hInstance = NULL; |
165 wc.hIcon = qxeLoadIcon (qxeGetModuleHandle (NULL), XETEXT (XEMACS_CLASS)); | |
166 wc.hCursor = qxeLoadCursor (NULL, IDC_ARROW); | |
428 | 167 /* Background brush is only used during sizing, when XEmacs cannot |
168 take over */ | |
771 | 169 wc.hbrBackground = (HBRUSH) (COLOR_APPWORKSPACE + 1); |
428 | 170 wc.lpszMenuName = NULL; |
171 | |
771 | 172 wc.lpszClassName = (XELPTSTR) XETEXT (XEMACS_CLASS); |
173 wc.hIconSm = (HICON) qxeLoadImage (qxeGetModuleHandle (NULL), | |
174 XETEXT (XEMACS_CLASS), | |
175 IMAGE_ICON, 16, 16, 0); | |
176 qxeRegisterClassEx (&wc); | |
428 | 177 |
178 #ifdef HAVE_WIDGETS | |
179 xzero (wc); | |
180 /* Register the main window class */ | |
771 | 181 wc.cbSize = sizeof (wc); |
428 | 182 wc.lpfnWndProc = (WNDPROC) mswindows_control_wnd_proc; |
771 | 183 wc.lpszClassName = (XELPTSTR) XETEXT (XEMACS_CONTROL_CLASS); |
428 | 184 wc.hInstance = NULL; |
771 | 185 qxeRegisterClassEx (&wc); |
428 | 186 #endif |
187 | |
440 | 188 #if defined (HAVE_TOOLBARS) || defined (HAVE_WIDGETS) |
428 | 189 InitCommonControls (); |
190 #endif | |
191 } | |
192 | |
657 | 193 #ifdef HAVE_DRAGNDROP |
428 | 194 static void |
771 | 195 mswindows_init_dde (void) |
428 | 196 { |
197 /* Initialize DDE management library and our related globals. We execute a | |
771 | 198 * dde Open ("file") by simulating a drop, so this depends on dnd support. */ |
442 | 199 |
428 | 200 mswindows_dde_mlid = 0; |
659 | 201 mswindows_dde_enable = 0; |
771 | 202 qxeDdeInitialize (&mswindows_dde_mlid, (PFNCALLBACK)mswindows_dde_callback, |
903 | 203 APPCMD_FILTERINITS|CBF_FAIL_SELFCONNECTIONS| |
204 CBF_FAIL_POKES|CBF_SKIP_ALLNOTIFICATIONS, | |
771 | 205 0); |
206 | |
207 mswindows_dde_service = | |
208 qxeDdeCreateStringHandle (mswindows_dde_mlid, | |
209 XETEXT (XEMACS_CLASS), | |
210 XEUNICODE_P ? CP_WINUNICODE : CP_WINANSI); | |
211 /* The following strings we Unicode-ize ourselves: | |
212 -- SZDDESYS_TOPIC is system-provided | |
903 | 213 -- MSWINDOWS_DDE_TOPIC_EVAL is defined by us |
214 -- MSWINDOWS_DDE_ITEM_RESULT is defined by us | |
771 | 215 -- MSWINDOWS_DDE_ITEM_OPEN is used in internal-format comparisons |
216 */ | |
217 mswindows_dde_topic_system = | |
218 qxeDdeCreateStringHandle (mswindows_dde_mlid, | |
219 XETEXT (SZDDESYS_TOPIC), | |
220 XEUNICODE_P ? CP_WINUNICODE : CP_WINANSI); | |
903 | 221 mswindows_dde_topic_eval = |
222 qxeDdeCreateStringHandle (mswindows_dde_mlid, | |
223 XETEXT (MSWINDOWS_DDE_TOPIC_EVAL), | |
224 XEUNICODE_P ? CP_WINUNICODE : CP_WINANSI); | |
225 mswindows_dde_item_result = | |
226 qxeDdeCreateStringHandle (mswindows_dde_mlid, | |
227 XETEXT (MSWINDOWS_DDE_ITEM_RESULT), | |
228 XEUNICODE_P ? CP_WINUNICODE : CP_WINANSI); | |
771 | 229 mswindows_dde_item_open = |
230 qxeDdeCreateStringHandle (mswindows_dde_mlid, | |
231 XETEXT (MSWINDOWS_DDE_ITEM_OPEN), | |
232 XEUNICODE_P ? CP_WINUNICODE : CP_WINANSI); | |
428 | 233 DdeNameService (mswindows_dde_mlid, mswindows_dde_service, 0L, DNS_REGISTER); |
657 | 234 } |
771 | 235 #endif /* HAVE_DRAGNDROP */ |
657 | 236 |
237 void | |
771 | 238 init_mswindows_dde_very_early (void) |
657 | 239 { |
771 | 240 #if !defined (NO_CYGWIN_COM_SUPPORT) |
241 /* Needed by SHBrowseForFolder, so do it always */ | |
242 CoInitialize (NULL); | |
243 #endif | |
244 | |
657 | 245 #ifdef HAVE_DRAGNDROP |
246 /* Initializing dde when the device is created is too late - the | |
247 client will give up waiting. Instead we initialize here and tell | |
248 the client we're too busy until the rest of initialization has | |
249 happened. */ | |
771 | 250 mswindows_init_dde (); |
657 | 251 #endif |
252 } | |
253 | |
254 static void | |
2286 | 255 mswindows_finish_init_device (struct device *UNUSED (d), |
256 Lisp_Object UNUSED (props)) | |
657 | 257 { |
258 #ifdef HAVE_DRAGNDROP | |
259 /* Tell pending clients we are ready. */ | |
260 mswindows_dde_enable = 1; | |
428 | 261 #endif |
262 } | |
263 | |
264 static void | |
265 mswindows_delete_device (struct device *d) | |
266 { | |
267 #ifdef HAVE_DRAGNDROP | |
442 | 268 DdeNameService (mswindows_dde_mlid, 0L, 0L, DNS_UNREGISTER); |
903 | 269 DdeFreeStringHandle (mswindows_dde_mlid, mswindows_dde_item_result); |
442 | 270 DdeFreeStringHandle (mswindows_dde_mlid, mswindows_dde_item_open); |
271 DdeFreeStringHandle (mswindows_dde_mlid, mswindows_dde_topic_system); | |
903 | 272 DdeFreeStringHandle (mswindows_dde_mlid, mswindows_dde_topic_eval); |
442 | 273 DdeFreeStringHandle (mswindows_dde_mlid, mswindows_dde_service); |
428 | 274 DdeUninitialize (mswindows_dde_mlid); |
442 | 275 |
771 | 276 # if !defined (NO_CYGWIN_COM_SUPPORT) |
442 | 277 CoUninitialize (); |
278 # endif | |
428 | 279 #endif |
280 | |
771 | 281 DeleteDC (DEVICE_MSWINDOWS_HCDC (d)); |
1726 | 282 xfree (d->device_data, void *); |
442 | 283 } |
284 | |
285 void | |
286 mswindows_get_workspace_coords (RECT *rc) | |
287 { | |
771 | 288 qxeSystemParametersInfo (SPI_GETWORKAREA, 0, rc, 0); |
428 | 289 } |
290 | |
440 | 291 static void |
292 mswindows_mark_device (struct device *d) | |
293 { | |
294 mark_object (DEVICE_MSWINDOWS_FONTLIST (d)); | |
295 } | |
296 | |
428 | 297 static Lisp_Object |
298 mswindows_device_system_metrics (struct device *d, | |
299 enum device_metrics m) | |
300 { | |
442 | 301 const HDC hdc = DEVICE_MSWINDOWS_HCDC(d); |
302 | |
428 | 303 switch (m) |
304 { | |
305 case DM_size_device: | |
442 | 306 return Fcons (make_int (GetDeviceCaps (hdc, HORZRES)), |
307 make_int (GetDeviceCaps (hdc, VERTRES))); | |
428 | 308 break; |
440 | 309 case DM_device_dpi: |
442 | 310 return Fcons (make_int (GetDeviceCaps (hdc, LOGPIXELSX)), |
311 make_int (GetDeviceCaps (hdc, LOGPIXELSY))); | |
440 | 312 break; |
428 | 313 case DM_size_device_mm: |
442 | 314 return Fcons (make_int (GetDeviceCaps (hdc, HORZSIZE)), |
315 make_int (GetDeviceCaps (hdc, VERTSIZE))); | |
428 | 316 break; |
317 case DM_num_bit_planes: | |
318 /* this is what X means by bitplanes therefore we ought to be | |
319 consistent. num planes is always 1 under mswindows and | |
320 therefore useless */ | |
442 | 321 return make_int (GetDeviceCaps (hdc, BITSPIXEL)); |
428 | 322 break; |
323 case DM_num_color_cells: | |
442 | 324 /* #### SIZEPALETTE only valid if RC_PALETTE bit set in RASTERCAPS, |
325 what should we return for a non-palette-based device? */ | |
326 return make_int (GetDeviceCaps (hdc, SIZEPALETTE)); | |
428 | 327 break; |
328 | |
329 /*** Colors ***/ | |
442 | 330 #define FROB(met, fore, back) \ |
428 | 331 case DM_##met: \ |
442 | 332 return build_syscolor_cons (fore, back); |
333 | |
334 FROB (color_default, COLOR_WINDOWTEXT, COLOR_WINDOW); | |
335 FROB (color_select, COLOR_HIGHLIGHTTEXT, COLOR_HIGHLIGHT); | |
336 FROB (color_balloon, COLOR_INFOTEXT, COLOR_INFOBK); | |
337 FROB (color_3d_face, COLOR_BTNTEXT, COLOR_BTNFACE); | |
338 FROB (color_3d_light, COLOR_3DHILIGHT, COLOR_3DLIGHT); | |
339 FROB (color_3d_dark, COLOR_3DDKSHADOW, COLOR_3DSHADOW); | |
340 FROB (color_menu, COLOR_MENUTEXT, COLOR_MENU); | |
341 FROB (color_menu_highlight, COLOR_HIGHLIGHTTEXT, COLOR_HIGHLIGHT); | |
342 FROB (color_menu_button, COLOR_MENUTEXT, COLOR_MENU); | |
343 FROB (color_menu_disabled, COLOR_GRAYTEXT, COLOR_MENU); | |
344 FROB (color_toolbar, COLOR_BTNTEXT, COLOR_BTNFACE); | |
345 FROB (color_scrollbar, COLOR_CAPTIONTEXT, COLOR_SCROLLBAR); | |
428 | 346 FROB (color_desktop, -1, COLOR_DESKTOP); |
347 FROB (color_workspace, -1, COLOR_APPWORKSPACE); | |
348 #undef FROB | |
349 | |
350 /*** Sizes ***/ | |
351 #define FROB(met, index1, index2) \ | |
352 case DM_##met: \ | |
353 return build_sysmetrics_cons (index1, index2); | |
354 | |
355 FROB (size_cursor, SM_CXCURSOR, SM_CYCURSOR); | |
356 FROB (size_scrollbar, SM_CXVSCROLL, SM_CYHSCROLL); | |
357 FROB (size_menu, -1, SM_CYMENU); | |
358 FROB (size_icon, SM_CXICON, SM_CYICON); | |
359 FROB (size_icon_small, SM_CXSMICON, SM_CYSMICON); | |
360 #undef FROB | |
361 | |
362 case DM_size_workspace: | |
363 { | |
364 RECT rc; | |
442 | 365 mswindows_get_workspace_coords (&rc); |
428 | 366 return Fcons (make_int (rc.right - rc.left), |
367 make_int (rc.bottom - rc.top)); | |
368 } | |
442 | 369 |
370 case DM_offset_workspace: | |
371 { | |
372 RECT rc; | |
373 mswindows_get_workspace_coords (&rc); | |
374 return Fcons (make_int (rc.left), make_int (rc.top)); | |
375 } | |
376 | |
428 | 377 /* |
378 case DM_size_toolbar: | |
379 case DM_size_toolbar_button: | |
380 case DM_size_toolbar_border: | |
381 */ | |
382 | |
383 /*** Features ***/ | |
384 #define FROB(met, index) \ | |
385 case DM_##met: \ | |
386 return make_int (GetSystemMetrics (index)); | |
387 | |
388 FROB (mouse_buttons, SM_CMOUSEBUTTONS); | |
389 FROB (swap_buttons, SM_SWAPBUTTON); | |
390 FROB (show_sounds, SM_SHOWSOUNDS); | |
391 FROB (slow_device, SM_SLOWMACHINE); | |
392 FROB (security, SM_SECURE); | |
393 #undef FROB | |
394 | |
395 } | |
396 | |
397 /* Do not know such property */ | |
398 return Qunbound; | |
399 } | |
400 | |
401 | |
402 /************************************************************************/ | |
442 | 403 /* printer helpers */ |
440 | 404 /************************************************************************/ |
405 | |
406 static void | |
407 signal_open_printer_error (struct device *d) | |
408 { | |
442 | 409 invalid_operation ("Failed to open printer", DEVICE_CONNECTION (d)); |
410 } | |
411 | |
412 | |
413 /* Helper function */ | |
414 static int | |
771 | 415 msprinter_init_device_internal (struct device *d, Lisp_Object printer_name) |
442 | 416 { |
771 | 417 Extbyte *printer_ext; |
418 HDC hdc; | |
442 | 419 |
771 | 420 DEVICE_MSPRINTER_NAME (d) = printer_name; |
421 | |
422 LISP_STRING_TO_TSTR (printer_name, printer_ext); | |
423 | |
424 if (!qxeOpenPrinter (printer_ext, &DEVICE_MSPRINTER_HPRINTER (d), NULL)) | |
442 | 425 { |
426 DEVICE_MSPRINTER_HPRINTER (d) = NULL; | |
427 return 0; | |
428 } | |
429 | |
771 | 430 DEVICE_MSPRINTER_HDC (d) = qxeCreateDC (XETEXT ("WINSPOOL"), printer_ext, |
431 NULL, NULL); | |
442 | 432 if (DEVICE_MSPRINTER_HDC (d) == NULL) |
433 return 0; | |
434 | |
771 | 435 hdc = CreateCompatibleDC (DEVICE_MSPRINTER_HDC (d)); |
436 DEVICE_MSPRINTER_HCDC (d) = hdc; | |
437 DEVICE_MSPRINTER_FONTLIST (d) = mswindows_enumerate_fonts (hdc); | |
442 | 438 |
439 DEVICE_CLASS (d) = (GetDeviceCaps (DEVICE_MSPRINTER_HDC (d), BITSPIXEL) | |
440 * GetDeviceCaps (DEVICE_MSPRINTER_HDC (d), PLANES) | |
441 > 1) ? Qcolor : Qmono; | |
442 return 1; | |
440 | 443 } |
444 | |
445 static void | |
442 | 446 msprinter_delete_device_internal (struct device *d) |
447 { | |
448 if (DEVICE_MSPRINTER_HPRINTER (d)) | |
449 ClosePrinter (DEVICE_MSPRINTER_HPRINTER (d)); | |
450 if (DEVICE_MSPRINTER_HDC (d)) | |
451 DeleteDC (DEVICE_MSPRINTER_HDC (d)); | |
452 if (DEVICE_MSPRINTER_HCDC (d)) | |
453 DeleteDC (DEVICE_MSPRINTER_HCDC (d)); | |
454 | |
455 DEVICE_MSPRINTER_FONTLIST (d) = Qnil; | |
456 } | |
457 | |
458 static int | |
771 | 459 msprinter_reinit_device (struct device *d, Lisp_Object devname) |
442 | 460 { |
461 msprinter_delete_device_internal (d); | |
462 return msprinter_init_device_internal (d, devname); | |
463 } | |
464 | |
465 Lisp_Object | |
466 msprinter_default_printer (void) | |
467 { | |
468 Extbyte name[666]; | |
867 | 469 Ibyte *nameint; |
442 | 470 |
771 | 471 if (qxeGetProfileString (XETEXT ("windows"), XETEXT ("device"), NULL, name, |
472 sizeof (name) / XETCHAR_SIZE) <= 0) | |
442 | 473 return Qnil; |
771 | 474 TSTR_TO_C_STRING (name, nameint); |
442 | 475 |
771 | 476 if (nameint[0] == '\0') |
442 | 477 return Qnil; |
478 | |
771 | 479 /* this is destructive, but that's ok because the string is either in |
851 | 480 name[] or ALLOCA ()ed */ |
771 | 481 qxestrtok (nameint, ","); |
482 | |
483 return build_intstring (nameint); | |
442 | 484 } |
485 | |
486 | |
487 /************************************************************************/ | |
488 /* printer methods */ | |
489 /************************************************************************/ | |
490 | |
491 static void | |
2286 | 492 msprinter_init_device (struct device *d, Lisp_Object UNUSED (props)) |
440 | 493 { |
771 | 494 DEVMODEW *pdm; |
647 | 495 LONG dm_size; |
771 | 496 Extbyte *printer_name; |
440 | 497 |
498 d->device_data = xnew_and_zero (struct msprinter_device); | |
499 | |
442 | 500 DEVICE_INFD (d) = DEVICE_OUTFD (d) = -1; |
771 | 501 DEVICE_MSPRINTER_DEVMODE (d) = Qnil; |
502 DEVICE_MSPRINTER_NAME (d) = Qnil; | |
440 | 503 |
2367 | 504 #if 0 /* #### deleted in new ikeyama ws */ |
771 | 505 /* We do not use printer font list as we do with the display |
506 device. Rather, we allow GDI to pick the closest match to the | |
440 | 507 display font. */ |
508 DEVICE_MSPRINTER_FONTLIST (d) = Qnil; | |
771 | 509 #endif /* 0 */ |
440 | 510 |
442 | 511 CHECK_STRING (DEVICE_CONNECTION (d)); |
512 | |
771 | 513 if (!msprinter_init_device_internal (d, DEVICE_CONNECTION (d))) |
442 | 514 signal_open_printer_error (d); |
515 | |
771 | 516 LISP_STRING_TO_TSTR (DEVICE_CONNECTION (d), printer_name); |
442 | 517 /* Determine DEVMODE size and store the default DEVMODE */ |
771 | 518 dm_size = qxeDocumentProperties (NULL, DEVICE_MSPRINTER_HPRINTER (d), |
519 printer_name, NULL, NULL, 0); | |
442 | 520 if (dm_size <= 0) |
521 signal_open_printer_error (d); | |
522 | |
771 | 523 pdm = (DEVMODEW *) xmalloc (dm_size); |
524 if (qxeDocumentProperties (NULL, DEVICE_MSPRINTER_HPRINTER(d), | |
525 printer_name, pdm, | |
526 NULL, DM_OUT_BUFFER) < 0) | |
552 | 527 signal_open_printer_error (d); |
442 | 528 |
529 assert (DEVMODE_SIZE (pdm) <= dm_size); | |
530 | |
771 | 531 DEVICE_MSPRINTER_DEVMODE (d) = |
532 allocate_devmode (pdm, 0, DEVICE_CONNECTION (d), d); | |
442 | 533 } |
534 | |
535 static void | |
536 msprinter_delete_device (struct device *d) | |
537 { | |
538 if (d->device_data) | |
539 { | |
540 msprinter_delete_device_internal (d); | |
541 | |
542 /* Disassociate the selected devmode with the device */ | |
543 if (!NILP (DEVICE_MSPRINTER_DEVMODE (d))) | |
544 { | |
545 XDEVMODE (DEVICE_MSPRINTER_DEVMODE (d))->device = Qnil; | |
546 DEVICE_MSPRINTER_DEVMODE (d) = Qnil; | |
547 } | |
548 | |
1726 | 549 xfree (d->device_data, void *); |
442 | 550 } |
440 | 551 } |
552 | |
553 static Lisp_Object | |
554 msprinter_device_system_metrics (struct device *d, | |
555 enum device_metrics m) | |
556 { | |
557 switch (m) | |
558 { | |
559 /* Device sizes - pixel and mm */ | |
560 #define FROB(met, index1, index2) \ | |
561 case DM_##met: \ | |
562 return build_devicecaps_cons \ | |
771 | 563 (DEVICE_MSPRINTER_HDC (d), index1, index2); |
440 | 564 |
565 FROB (size_device, PHYSICALWIDTH, PHYSICALHEIGHT); | |
566 FROB (size_device_mm, HORZSIZE, VERTSIZE); | |
567 FROB (size_workspace, HORZRES, VERTRES); | |
568 FROB (offset_workspace, PHYSICALOFFSETX, PHYSICALOFFSETY); | |
569 FROB (device_dpi, LOGPIXELSX, LOGPIXELSY); | |
570 #undef FROB | |
571 | |
572 case DM_num_bit_planes: | |
573 /* this is what X means by bitplanes therefore we ought to be | |
574 consistent. num planes is always 1 under mswindows and | |
575 therefore useless */ | |
771 | 576 return make_int (GetDeviceCaps (DEVICE_MSPRINTER_HDC (d), BITSPIXEL)); |
440 | 577 |
442 | 578 case DM_num_color_cells: /* Printers are non-palette devices */ |
440 | 579 case DM_slow_device: /* Animation would be a really bad idea */ |
580 case DM_security: /* Not provided by windows */ | |
581 return Qzero; | |
582 } | |
583 | |
584 /* Do not know such property */ | |
585 return Qunbound; | |
586 } | |
587 | |
588 static void | |
589 msprinter_mark_device (struct device *d) | |
590 { | |
591 mark_object (DEVICE_MSPRINTER_FONTLIST (d)); | |
442 | 592 mark_object (DEVICE_MSPRINTER_DEVMODE (d)); |
771 | 593 mark_object (DEVICE_MSPRINTER_NAME (d)); |
440 | 594 } |
595 | |
596 | |
597 /************************************************************************/ | |
442 | 598 /* printer Lisp subroutines */ |
440 | 599 /************************************************************************/ |
600 | |
442 | 601 static void |
602 global_free_2_maybe (HGLOBAL hg1, HGLOBAL hg2) | |
603 { | |
604 if (hg1 != NULL) | |
605 GlobalFree (hg1); | |
606 if (hg2 != NULL) | |
607 GlobalFree (hg2); | |
608 } | |
609 | |
610 static HGLOBAL | |
611 devmode_to_hglobal (Lisp_Devmode *ldm) | |
612 { | |
613 HGLOBAL hg = GlobalAlloc (GHND, XDEVMODE_SIZE (ldm)); | |
614 memcpy (GlobalLock (hg), ldm->devmode, XDEVMODE_SIZE (ldm)); | |
615 GlobalUnlock (hg); | |
616 return hg; | |
617 } | |
618 | |
619 /* Returns 0 if the printer has been deleted due to a fatal I/O error, | |
620 1 otherwise. */ | |
621 static int | |
771 | 622 sync_printer_with_devmode (struct device* d, DEVMODEW* devmode_in, |
623 DEVMODEW* devmode_out, Lisp_Object devname) | |
440 | 624 { |
442 | 625 /* Change connection if the device changed */ |
771 | 626 if (!NILP (devname) |
627 && lisp_strcasecmp (devname, DEVICE_MSPRINTER_NAME (d)) != 0) | |
442 | 628 { |
771 | 629 Lisp_Object new_connection = devname; |
442 | 630 |
631 DEVICE_CONNECTION (d) = Qnil; | |
632 if (!NILP (Ffind_device (new_connection, Qmsprinter))) | |
633 { | |
634 /* We are in trouble - second msprinter for the same device. | |
635 Nothing wrong on the Windows side, just forge a unique | |
636 connection name. Use the memory address of d as a unique | |
637 suffix. */ | |
867 | 638 Ibyte new_connext[20]; |
771 | 639 |
640 qxesprintf (new_connext, ":%X", d->header.uid); | |
641 new_connection = concat2 (devname, build_intstring (new_connext)); | |
442 | 642 } |
643 DEVICE_CONNECTION (d) = new_connection; | |
644 | |
645 /* Reinitialize printer. The device can pop off in process */ | |
646 if (!msprinter_reinit_device (d, devname)) | |
647 { | |
648 /* Kaboom! */ | |
649 delete_device_internal (d, 1, 0, 1); | |
650 return 0; | |
651 } | |
652 } | |
771 | 653 { |
654 Extbyte *nameext; | |
442 | 655 |
771 | 656 LISP_STRING_TO_TSTR (DEVICE_MSPRINTER_NAME (d), nameext); |
657 | |
658 /* Apply the new devmode to the printer */ | |
659 qxeDocumentProperties (NULL, DEVICE_MSPRINTER_HPRINTER (d), | |
660 nameext, devmode_out, devmode_in, | |
661 DM_IN_BUFFER | DM_OUT_BUFFER); | |
440 | 662 |
771 | 663 /* #### ResetDC fails sometimes, Bill only knows why. |
664 The solution below looks more like a workaround to me, | |
665 although it might be fine. --kkm */ | |
666 if (qxeResetDC (DEVICE_MSPRINTER_HDC (d), devmode_out) == NULL) | |
667 { | |
668 DeleteDC (DEVICE_MSPRINTER_HDC (d)); | |
669 DEVICE_MSPRINTER_HDC (d) = | |
670 qxeCreateDC (XETEXT ("WINSPOOL"), nameext, NULL, | |
671 devmode_out); | |
672 } | |
673 } | |
674 | |
442 | 675 return 1; |
676 } | |
677 | |
678 static void | |
679 handle_devmode_changes (Lisp_Devmode *ldm, HGLOBAL hDevNames, HGLOBAL hDevMode) | |
680 { | |
771 | 681 DEVNAMES *devnames = (DEVNAMES *) GlobalLock (hDevNames); |
682 Extbyte *new_name = | |
683 devnames ? | |
684 (Extbyte *) devnames + XETCHAR_SIZE * devnames->wDeviceOffset : NULL; | |
685 DEVMODEW *devmode = (DEVMODEW *) GlobalLock (hDevMode); | |
442 | 686 |
687 /* Size and name may have changed */ | |
771 | 688 ldm->devmode = (DEVMODEW *) xrealloc (ldm->devmode, DEVMODE_SIZE (devmode)); |
442 | 689 if (new_name) |
771 | 690 ldm->printer_name = build_tstr_string (new_name); |
440 | 691 |
442 | 692 if (!NILP (ldm->device)) |
693 { | |
694 /* Apply the new devmode to the printer and get a compete one back */ | |
695 struct device *d = XDEVICE (ldm->device); | |
771 | 696 if (!sync_printer_with_devmode (d, devmode, ldm->devmode, |
697 new_name ? ldm->printer_name : Qnil)) | |
442 | 698 { |
699 global_free_2_maybe (hDevNames, hDevMode); | |
771 | 700 signal_error |
701 (Qio_error, | |
702 "Printer device initialization I/O error, device deleted", | |
703 ldm->device); | |
442 | 704 } |
705 } | |
706 else | |
707 { | |
708 /* Just copy the devmode structure */ | |
709 memcpy (ldm->devmode, devmode, DEVMODE_SIZE (devmode)); | |
710 } | |
711 } | |
440 | 712 |
442 | 713 static void |
714 ensure_not_printing (struct device *d) | |
715 { | |
716 if (!NILP (DEVICE_FRAME_LIST (d))) | |
717 { | |
793 | 718 Lisp_Object device = wrap_device (d); |
719 | |
442 | 720 invalid_operation ("Cannot change settings while print job is active", |
721 device); | |
722 } | |
723 } | |
724 | |
725 static Lisp_Devmode * | |
726 decode_devmode (Lisp_Object dev) | |
727 { | |
728 if (DEVMODEP (dev)) | |
729 return XDEVMODE (dev); | |
730 else | |
731 { | |
732 ensure_not_printing (XDEVICE (dev)); | |
733 return XDEVMODE (DEVICE_MSPRINTER_DEVMODE (XDEVICE (dev))); | |
734 } | |
440 | 735 } |
736 | |
737 /* | |
442 | 738 * DEV can be either a printer or devmode |
440 | 739 */ |
442 | 740 static Lisp_Object |
510 | 741 print_dialog_worker (Lisp_Object dev, DWORD flags) |
442 | 742 { |
743 Lisp_Devmode *ldm = decode_devmode (dev); | |
771 | 744 PRINTDLGW pd; |
442 | 745 |
746 memset (&pd, 0, sizeof (pd)); | |
747 pd.lStructSize = sizeof (pd); | |
748 pd.hwndOwner = mswindows_get_selected_frame_hwnd (); | |
749 pd.hDevMode = devmode_to_hglobal (ldm); | |
510 | 750 pd.Flags = flags | PD_USEDEVMODECOPIESANDCOLLATE; |
442 | 751 pd.nMinPage = 0; |
752 pd.nMaxPage = 0xFFFF; | |
753 | |
771 | 754 if (!qxePrintDlg (&pd)) |
442 | 755 { |
756 global_free_2_maybe (pd.hDevNames, pd.hDevMode); | |
757 return Qnil; | |
758 } | |
759 | |
760 handle_devmode_changes (ldm, pd.hDevNames, pd.hDevMode); | |
761 | |
762 /* Finally, build the resulting plist */ | |
763 { | |
764 Lisp_Object result = Qnil; | |
765 struct gcpro gcpro1; | |
766 GCPRO1 (result); | |
767 | |
768 /* Do consing in reverse order. | |
769 Number of copies */ | |
510 | 770 result = Fcons (Qcopies, Fcons (make_int (pd.nCopies), result)); |
442 | 771 |
772 /* Page range */ | |
510 | 773 if (pd.Flags & PD_PAGENUMS) |
442 | 774 { |
775 result = Fcons (Qto_page, Fcons (make_int (pd.nToPage), result)); | |
776 result = Fcons (Qfrom_page, Fcons (make_int (pd.nFromPage), result)); | |
510 | 777 result = Fcons (Qselected_page_button, Fcons (Qpages, result)); |
442 | 778 } |
510 | 779 else if (pd.Flags & PD_SELECTION) |
780 result = Fcons (Qselected_page_button, Fcons (Qselection, result)); | |
781 else | |
782 result = Fcons (Qselected_page_button, Fcons (Qall, result)); | |
442 | 783 |
784 /* Device name */ | |
771 | 785 result = Fcons (Qname, Fcons (ldm->printer_name, result)); |
442 | 786 UNGCPRO; |
787 | |
788 global_free_2_maybe (pd.hDevNames, pd.hDevMode); | |
789 return result; | |
790 } | |
791 } | |
792 | |
793 Lisp_Object | |
2286 | 794 mswindows_handle_print_dialog_box (struct frame *UNUSED (f), Lisp_Object keys) |
442 | 795 { |
796 Lisp_Object device = Qunbound, settings = Qunbound; | |
510 | 797 DWORD flags = PD_NOSELECTION; |
442 | 798 |
799 { | |
800 EXTERNAL_PROPERTY_LIST_LOOP_3 (key, value, keys) | |
801 { | |
802 if (EQ (key, Q_device)) | |
803 { | |
804 device = wrap_device (decode_device (value)); | |
805 CHECK_MSPRINTER_DEVICE (device); | |
806 } | |
807 else if (EQ (key, Q_printer_settings)) | |
808 { | |
809 CHECK_DEVMODE (value); | |
810 settings = value; | |
811 } | |
510 | 812 else if (EQ (key, Q_allow_pages)) |
813 { | |
814 if (NILP (value)) | |
815 flags |= PD_NOPAGENUMS; | |
816 } | |
817 else if (EQ (key, Q_allow_selection)) | |
442 | 818 { |
510 | 819 if (!NILP (value)) |
820 flags &= ~PD_NOSELECTION; | |
442 | 821 } |
510 | 822 else if (EQ (key, Q_selected_page_button)) |
442 | 823 { |
510 | 824 if (EQ (value, Qselection)) |
825 flags |= PD_SELECTION; | |
826 else if (EQ (value, Qpages)) | |
827 flags |= PD_PAGENUMS; | |
828 else if (!EQ (value, Qall)) | |
563 | 829 invalid_constant ("for :selected-page-button", value); |
442 | 830 } |
831 else | |
563 | 832 invalid_constant ("Unrecognized print-dialog keyword", key); |
442 | 833 } |
834 } | |
835 | |
836 if ((UNBOUNDP (device) && UNBOUNDP (settings)) || | |
837 (!UNBOUNDP (device) && !UNBOUNDP (settings))) | |
563 | 838 sferror ("Exactly one of :device and :printer-settings must be given", |
442 | 839 keys); |
840 | |
510 | 841 return print_dialog_worker (!UNBOUNDP (device) ? device : settings, flags); |
442 | 842 } |
843 | |
506 | 844 int |
845 mswindows_get_default_margin (Lisp_Object prop) | |
846 { | |
847 if (EQ (prop, Qleft_margin)) return 1440; | |
848 if (EQ (prop, Qright_margin)) return 1440; | |
849 if (EQ (prop, Qtop_margin)) return 720; | |
850 if (EQ (prop, Qbottom_margin)) return 720; | |
2500 | 851 ABORT (); |
506 | 852 return 0; |
853 } | |
854 | |
442 | 855 static int |
798 | 856 plist_get_margin (Lisp_Object plist, Lisp_Object prop, int mm_p) |
442 | 857 { |
506 | 858 Lisp_Object val = |
859 Fplist_get (plist, prop, make_int (mswindows_get_default_margin (prop))); | |
442 | 860 if (!INTP (val)) |
861 invalid_argument ("Margin value must be an integer", val); | |
862 | |
798 | 863 return MulDiv (XINT (val), mm_p ? 254 : 100, 144); |
442 | 864 } |
865 | |
866 static Lisp_Object | |
867 plist_set_margin (Lisp_Object plist, Lisp_Object prop, int margin, int mm_p) | |
868 { | |
798 | 869 Lisp_Object val = make_int (MulDiv (margin, 144, mm_p ? 254 : 100)); |
442 | 870 return Fcons (prop, Fcons (val, plist)); |
871 } | |
872 | |
873 Lisp_Object | |
2286 | 874 mswindows_handle_page_setup_dialog_box (struct frame *UNUSED (f), |
875 Lisp_Object keys) | |
442 | 876 { |
877 Lisp_Object device = Qunbound, settings = Qunbound; | |
878 Lisp_Object plist = Qnil; | |
879 | |
880 { | |
881 EXTERNAL_PROPERTY_LIST_LOOP_3 (key, value, keys) | |
882 { | |
883 if (EQ (key, Q_device)) | |
884 { | |
885 device = wrap_device (decode_device (value)); | |
886 CHECK_MSPRINTER_DEVICE (device); | |
887 } | |
888 else if (EQ (key, Q_printer_settings)) | |
889 { | |
890 CHECK_DEVMODE (value); | |
891 settings = value; | |
892 } | |
893 else if (EQ (key, Q_properties)) | |
894 { | |
895 CHECK_LIST (value); | |
896 plist = value; | |
897 } | |
898 else | |
563 | 899 invalid_constant ("Unrecognized page-setup dialog keyword", key); |
442 | 900 } |
901 } | |
902 | |
903 if ((UNBOUNDP (device) && UNBOUNDP (settings)) || | |
904 (!UNBOUNDP (device) && !UNBOUNDP (settings))) | |
563 | 905 sferror ("Exactly one of :device and :printer-settings must be given", |
800 | 906 keys); |
442 | 907 |
908 if (UNBOUNDP (device)) | |
909 device = settings; | |
910 | |
911 { | |
912 Lisp_Devmode *ldm = decode_devmode (device); | |
771 | 913 PAGESETUPDLGW pd; |
853 | 914 Extbyte measure[2 * MAX_XETCHAR_SIZE]; |
850 | 915 int data; |
798 | 916 |
850 | 917 qxeGetLocaleInfo (LOCALE_USER_DEFAULT, LOCALE_IMEASURE, |
853 | 918 measure, sizeof (measure) / XETCHAR_SIZE); |
2421 | 919 data = qxetcscmp (measure, XETEXT ("0")); |
442 | 920 |
921 memset (&pd, 0, sizeof (pd)); | |
922 pd.lStructSize = sizeof (pd); | |
923 pd.hwndOwner = mswindows_get_selected_frame_hwnd (); | |
924 pd.Flags = PSD_MARGINS; | |
798 | 925 pd.rtMargin.left = plist_get_margin (plist, Qleft_margin, !data); |
926 pd.rtMargin.top = plist_get_margin (plist, Qtop_margin, !data); | |
927 pd.rtMargin.right = plist_get_margin (plist, Qright_margin, !data); | |
928 pd.rtMargin.bottom = plist_get_margin (plist, Qbottom_margin, !data); | |
442 | 929 pd.hDevMode = devmode_to_hglobal (ldm); |
930 | |
771 | 931 if (!qxePageSetupDlg (&pd)) |
442 | 932 { |
933 global_free_2_maybe (pd.hDevNames, pd.hDevMode); | |
934 return Qnil; | |
935 } | |
936 | |
937 if (pd.hDevMode) | |
938 handle_devmode_changes (ldm, pd.hDevNames, pd.hDevMode); | |
939 | |
940 /* Finally, build the resulting plist */ | |
941 { | |
942 Lisp_Object result = Qnil; | |
943 int mm_p = pd.Flags & PSD_INHUNDREDTHSOFMILLIMETERS; | |
944 result = plist_set_margin (result, Qbottom_margin, pd.rtMargin.bottom, | |
945 mm_p); | |
946 result = plist_set_margin (result, Qright_margin, pd.rtMargin.right, | |
947 mm_p); | |
948 result = plist_set_margin (result, Qtop_margin, pd.rtMargin.top, mm_p); | |
949 result = plist_set_margin (result, Qleft_margin, pd.rtMargin.left, mm_p); | |
950 return result; | |
951 } | |
952 } | |
953 } | |
954 | |
955 DEFUN ("msprinter-get-settings", Fmsprinter_get_settings, 1, 1, 0, /* | |
956 Return the settings object currently used by DEVICE. | |
957 The object returned is not a copy, but rather a pointer to the | |
958 original one. Use `msprinter-settings-copy' to create a copy of it. | |
959 */ | |
960 (device)) | |
961 { | |
962 struct device *d = decode_device (device); | |
793 | 963 device = wrap_device (d); |
442 | 964 CHECK_MSPRINTER_DEVICE (device); |
965 return DEVICE_MSPRINTER_DEVMODE (d); | |
966 } | |
967 | |
968 DEFUN ("msprinter-select-settings", Fmsprinter_select_settings, 2, 2, 0, /* | |
969 Select SETTINGS object into a DEVICE. | |
970 The settings from the settings object are immediately applied to the | |
971 printer, possibly changing even the target printer itself, and all | |
972 future changes are applied synchronously to the printer device and the | |
973 selected printer object, until a different settings object is selected | |
974 into the same printer. | |
975 | |
976 A settings object can be selected to no more than one printer at a time. | |
977 | |
978 If the supplied settings object is not specialized, it is specialized | |
979 for the printer immediately upon selection. The object can be | |
980 despecialized after it is unselected by calling the function | |
981 `msprinter-settings-despecialize'. | |
982 | |
983 Return value is the previously selected settings object. | |
984 */ | |
985 (device, settings)) | |
440 | 986 { |
442 | 987 Lisp_Devmode *ldm; |
988 struct device *d = decode_device (device); | |
989 | |
990 struct gcpro gcpro1; | |
991 GCPRO1 (settings); | |
992 | |
793 | 993 device = wrap_device (d); |
442 | 994 CHECK_MSPRINTER_DEVICE (device); |
995 CHECK_DEVMODE (settings); | |
996 ldm = XDEVMODE (settings); | |
997 | |
998 if (!NILP (ldm->device)) | |
999 invalid_operation ("The object is currently selected into a device", | |
1000 settings); | |
1001 | |
1002 /* If the object being selected is de-specialized, then its | |
1003 size is perhaps not enough to receive the new devmode. We can ask | |
1004 for printer's devmode size here, because despecialized settings | |
1005 cannot force switching to a different printer, as they supply no | |
1006 printer name at all. */ | |
771 | 1007 if (NILP (ldm->printer_name)) |
442 | 1008 { |
771 | 1009 Extbyte *nameext; |
1010 LONG dm_size; | |
1011 | |
1012 LISP_STRING_TO_TSTR (DEVICE_MSPRINTER_NAME (d), nameext); | |
1013 dm_size = qxeDocumentProperties (NULL, DEVICE_MSPRINTER_HPRINTER (d), | |
1014 nameext, NULL, NULL, 0); | |
442 | 1015 if (dm_size <= 0) |
563 | 1016 signal_error (Qio_error, |
1017 "Unable to specialize settings, printer error", | |
1018 device); | |
442 | 1019 |
1020 assert (XDEVMODE_SIZE (ldm) <= dm_size); | |
771 | 1021 ldm->devmode = (DEVMODEW *) xrealloc (ldm->devmode, dm_size); |
442 | 1022 } |
1023 | |
1024 /* If we bail out on signal here, no damage is done, except that | |
1025 the storage for the DEVMODE structure might be reallocated to | |
1026 hold a larger one - not a big deal */ | |
1027 if (!sync_printer_with_devmode (d, ldm->devmode, ldm->devmode, | |
1028 ldm->printer_name)) | |
563 | 1029 signal_error (Qio_error, |
1030 "Printer device initialization I/O error, device deleted", | |
771 | 1031 device); |
442 | 1032 |
771 | 1033 if (NILP (ldm->printer_name )) |
1034 ldm->printer_name = DEVICE_MSPRINTER_NAME (d); | |
442 | 1035 |
1036 { | |
1037 Lisp_Object old_mode = DEVICE_MSPRINTER_DEVMODE (d); | |
1038 ldm->device = device; | |
1039 XDEVMODE (old_mode)->device = Qnil; | |
1040 DEVICE_MSPRINTER_DEVMODE (d) = settings; | |
1041 UNGCPRO; | |
1042 return old_mode; | |
1043 } | |
1044 } | |
1045 | |
1046 DEFUN ("msprinter-apply-settings", Fmsprinter_apply_settings, 2, 2, 0, /* | |
3025 | 1047 Apply settings from a SETTINGS object to a `msprinter' DEVICE. |
442 | 1048 The settings from the settings object are immediately applied to the |
1049 printer, possibly changing even the target printer itself. The SETTING | |
1050 object is not modified, unlike `msprinter-select-settings', and the | |
1051 supplied object is not changed. The changes are immediately recorded | |
1052 into the settings object which is currently selected into the printer | |
1053 device. | |
1054 | |
1055 Return value is the currently selected settings object. | |
1056 */ | |
1057 (device, settings)) | |
1058 { | |
1059 Lisp_Devmode *ldm_current, *ldm_new; | |
1060 struct device *d = decode_device (device); | |
1061 | |
1062 struct gcpro gcpro1; | |
1063 GCPRO1 (settings); | |
1064 | |
793 | 1065 device = wrap_device (d); |
442 | 1066 CHECK_MSPRINTER_DEVICE (device); |
1067 CHECK_DEVMODE (settings); | |
1068 ldm_new = XDEVMODE (settings); | |
1069 ldm_current = XDEVMODE (DEVICE_MSPRINTER_DEVMODE (d)); | |
1070 | |
1071 /* If the supplied devmode is not specialized, then the current | |
1072 devmode size will always be sufficient, as the printer does | |
1073 not change. If it is specialized, we must reallocate the current | |
1074 devmode storage to match with the supplied one, as it has the right | |
1075 size for the new printer, if it is going to change. The correct | |
1076 way is to use the largest of the two though, to keep the old | |
1077 contents unchanged in case of preliminary exit. | |
1078 */ | |
771 | 1079 if (!NILP (ldm_new->printer_name)) |
442 | 1080 ldm_current->devmode = |
771 | 1081 (DEVMODEW*) xrealloc (ldm_current->devmode, |
442 | 1082 max (XDEVMODE_SIZE (ldm_new), |
1083 XDEVMODE_SIZE (ldm_current))); | |
1084 | |
1085 if (!sync_printer_with_devmode (d, ldm_new->devmode, | |
1086 ldm_current->devmode, | |
1087 ldm_new->printer_name)) | |
771 | 1088 signal_error |
1089 (Qio_error, | |
1090 "Printer device initialization I/O error, device deleted", device); | |
1091 | |
1092 if (!NILP (ldm_new->printer_name)) | |
1093 ldm_current->printer_name = ldm_new->printer_name; | |
442 | 1094 |
446 | 1095 UNGCPRO; |
442 | 1096 return DEVICE_MSPRINTER_DEVMODE (d); |
1097 } | |
1098 | |
1099 /************************************************************************/ | |
1100 /* devmode */ | |
1101 /************************************************************************/ | |
1102 | |
1204 | 1103 static const struct memory_description devmode_description[] = { |
934 | 1104 { XD_LISP_OBJECT, offsetof (struct Lisp_Devmode, printer_name) }, |
964 | 1105 { XD_LISP_OBJECT, offsetof (struct Lisp_Devmode, device) }, |
934 | 1106 { XD_END } |
1107 }; | |
1108 | |
771 | 1109 static Lisp_Object |
1110 mark_devmode (Lisp_Object obj) | |
1111 { | |
1112 Lisp_Devmode *data = XDEVMODE (obj); | |
1113 mark_object (data->printer_name); | |
1114 return data->device; | |
1115 } | |
1116 | |
442 | 1117 static void |
1118 print_devmode (Lisp_Object obj, Lisp_Object printcharfun, | |
2286 | 1119 int UNUSED (escapeflag)) |
442 | 1120 { |
1121 Lisp_Devmode *dm = XDEVMODE (obj); | |
1122 if (print_readably) | |
563 | 1123 printing_unreadable_object ("#<msprinter-settings 0x%x>", |
1124 dm->header.uid); | |
826 | 1125 write_c_string (printcharfun, "#<msprinter-settings"); |
771 | 1126 if (!NILP (dm->printer_name)) |
800 | 1127 write_fmt_string_lisp (printcharfun, " for %S", 1, dm->printer_name); |
442 | 1128 if (!NILP (dm->device)) |
800 | 1129 write_fmt_string_lisp (printcharfun, " (currently on %s)", 1, dm->device); |
1130 write_fmt_string (printcharfun, " 0x%x>", dm->header.uid); | |
442 | 1131 } |
1132 | |
1133 static void | |
1134 finalize_devmode (void *header, int for_disksave) | |
1135 { | |
1136 Lisp_Devmode *dm = (Lisp_Devmode *) header; | |
1137 | |
1138 if (for_disksave) | |
1139 { | |
793 | 1140 Lisp_Object devmode = wrap_devmode (dm); |
1141 | |
442 | 1142 invalid_operation |
1143 ("Cannot dump XEmacs containing an msprinter-settings object", | |
1144 devmode); | |
1145 } | |
1146 | |
1147 assert (NILP (dm->device)); | |
1148 } | |
1149 | |
1150 static int | |
2286 | 1151 equal_devmode (Lisp_Object obj1, Lisp_Object obj2, int UNUSED (depth)) |
442 | 1152 { |
1153 Lisp_Devmode *dm1 = XDEVMODE (obj1); | |
1154 Lisp_Devmode *dm2 = XDEVMODE (obj2); | |
440 | 1155 |
442 | 1156 if ((dm1->devmode != NULL) != (dm1->devmode != NULL)) |
1157 return 0; | |
1158 if (dm1->devmode == NULL) | |
1159 return 1; | |
1160 if (memcmp (dm1->devmode, dm2->devmode, XDEVMODE_SIZE (dm1)) != 0) | |
1161 return 0; | |
771 | 1162 if (NILP (dm1->printer_name) || NILP (dm2->printer_name)) |
442 | 1163 return 1; |
771 | 1164 return lisp_strcasecmp (dm1->printer_name, dm2->printer_name) == 0; |
442 | 1165 } |
1166 | |
665 | 1167 static Hashcode |
442 | 1168 hash_devmode (Lisp_Object obj, int depth) |
1169 { | |
1170 Lisp_Devmode *dm = XDEVMODE (obj); | |
1171 | |
1172 return HASH3 (XDEVMODE_SIZE (dm), | |
1173 dm->devmode ? memory_hash (dm->devmode, XDEVMODE_SIZE (dm)) | |
1174 : 0, | |
771 | 1175 internal_hash (dm->printer_name, depth + 1)); |
442 | 1176 } |
1177 | |
5117
3742ea8250b5
Checking in final CVS version of workspace 'ben-lisp-object'
Ben Wing <ben@xemacs.org>
parents:
3025
diff
changeset
|
1178 DEFINE_NONDUMPABLE_LISP_OBJECT ("msprinter-settings", devmode, |
3742ea8250b5
Checking in final CVS version of workspace 'ben-lisp-object'
Ben Wing <ben@xemacs.org>
parents:
3025
diff
changeset
|
1179 mark_devmode, print_devmode, |
3742ea8250b5
Checking in final CVS version of workspace 'ben-lisp-object'
Ben Wing <ben@xemacs.org>
parents:
3025
diff
changeset
|
1180 finalize_devmode, |
3742ea8250b5
Checking in final CVS version of workspace 'ben-lisp-object'
Ben Wing <ben@xemacs.org>
parents:
3025
diff
changeset
|
1181 equal_devmode, hash_devmode, |
3742ea8250b5
Checking in final CVS version of workspace 'ben-lisp-object'
Ben Wing <ben@xemacs.org>
parents:
3025
diff
changeset
|
1182 devmode_description, |
3742ea8250b5
Checking in final CVS version of workspace 'ben-lisp-object'
Ben Wing <ben@xemacs.org>
parents:
3025
diff
changeset
|
1183 Lisp_Devmode); |
934 | 1184 |
442 | 1185 static Lisp_Object |
771 | 1186 allocate_devmode (DEVMODEW* src_devmode, int do_copy, |
1187 Lisp_Object src_name, struct device *d) | |
442 | 1188 { |
5117
3742ea8250b5
Checking in final CVS version of workspace 'ben-lisp-object'
Ben Wing <ben@xemacs.org>
parents:
3025
diff
changeset
|
1189 Lisp_Object obj = ALLOC_LISP_OBJECT (devmode); |
3742ea8250b5
Checking in final CVS version of workspace 'ben-lisp-object'
Ben Wing <ben@xemacs.org>
parents:
3025
diff
changeset
|
1190 Lisp_Devmode *dm = XDEVMODE (obj); |
442 | 1191 |
1192 if (d) | |
793 | 1193 dm->device = wrap_device (d); |
442 | 1194 else |
1195 dm->device = Qnil; | |
1196 | |
771 | 1197 dm->printer_name = src_name; |
442 | 1198 |
1199 if (src_devmode != NULL && do_copy) | |
1200 { | |
771 | 1201 dm->devmode = (DEVMODEW*) xmalloc (DEVMODE_SIZE (src_devmode)); |
442 | 1202 memcpy (dm->devmode, src_devmode, DEVMODE_SIZE (src_devmode)); |
1203 } | |
1204 else | |
1205 { | |
1206 dm->devmode = src_devmode; | |
1207 } | |
1208 | |
5117
3742ea8250b5
Checking in final CVS version of workspace 'ben-lisp-object'
Ben Wing <ben@xemacs.org>
parents:
3025
diff
changeset
|
1209 return obj; |
442 | 1210 } |
1211 | |
1212 DEFUN ("msprinter-settings-copy", Fmsprinter_settings_copy, 1, 1, 0, /* | |
1213 Create and returns an exact copy of a printer settings object. | |
1214 */ | |
1215 (settings)) | |
1216 { | |
1217 Lisp_Devmode *dm; | |
1218 | |
1219 CHECK_DEVMODE (settings); | |
1220 dm = XDEVMODE (settings); | |
1221 | |
1222 return allocate_devmode (dm->devmode, 1, dm->printer_name, NULL); | |
1223 } | |
1224 | |
1225 DEFUN ("msprinter-settings-despecialize", Fmsprinter_settings_despecialize, 1, 1, 0, /* | |
1226 Erase printer-specific settings from a printer settings object. | |
1227 */ | |
1228 (settings)) | |
1229 { | |
1230 Lisp_Devmode *ldm; | |
771 | 1231 DEVMODEW *dm; |
442 | 1232 |
1233 CHECK_DEVMODE (settings); | |
1234 ldm = XDEVMODE (settings); | |
1235 | |
1236 if (!NILP (ldm->device)) | |
1237 invalid_operation ("The object is currently selected into a device", | |
1238 settings); | |
1239 | |
1240 dm = ldm->devmode; | |
1241 | |
1242 /* #### TODO. Either remove references to device specific bins, | |
1243 paper sizes etc, or signal an error of they are present. */ | |
440 | 1244 |
442 | 1245 dm->dmDriverExtra = 0; |
1246 dm->dmDeviceName[0] = '\0'; | |
1247 | |
771 | 1248 ldm->printer_name = Qnil; |
442 | 1249 |
1250 return Qnil; | |
1251 } | |
1252 | |
1253 DEFUN ("mswindows-get-default-printer", Fmswindows_get_default_printer, 0, 0, 0, /* | |
1254 Return name of the default printer, as string, on nil if there is no default. | |
1255 */ | |
1256 ()) | |
1257 { | |
1258 return msprinter_default_printer (); | |
1259 } | |
1260 | |
1261 static void | |
1262 signal_enum_printer_error (void) | |
1263 { | |
1264 invalid_operation ("Error enumerating printers", make_int (GetLastError ())); | |
1265 } | |
1266 | |
1267 DEFUN ("mswindows-printer-list", Fmswindows_printer_list, 0, 0, 0, /* | |
1268 Return a list of string names of installed printers. | |
1269 If there is a default printer, it is returned as the first element of | |
1270 the list. If there is no default printer, the first element of the | |
1271 list will be nil. The rest of elements are guaranteed to have string | |
1272 values. Return value is nil if there are no printers installed. | |
1273 */ | |
1274 ()) | |
1275 { | |
1276 int have_nt, ok; | |
1277 BYTE *data_buf, dummy_byte; | |
665 | 1278 Bytecount enum_entry_size; |
442 | 1279 DWORD enum_flags, enum_level, bytes_needed, num_printers; |
1280 struct gcpro gcpro1, gcpro2; | |
1281 Lisp_Object result = Qnil, def_printer = Qnil; | |
1282 | |
1283 /* Determine OS flavor, to use the fastest enumeration method available */ | |
771 | 1284 have_nt = !mswindows_windows9x_p; |
442 | 1285 enum_flags = PRINTER_ENUM_LOCAL | (have_nt ? PRINTER_ENUM_CONNECTIONS : 0); |
1286 enum_level = have_nt ? 4 : 5; | |
771 | 1287 enum_entry_size = (have_nt ? sizeof (PRINTER_INFO_4) : |
1288 sizeof (PRINTER_INFO_5)); | |
442 | 1289 |
1290 /* Allocate memory for printer enum structure */ | |
771 | 1291 ok = qxeEnumPrinters (enum_flags, NULL, enum_level, &dummy_byte, 1, |
1292 &bytes_needed, &num_printers); | |
442 | 1293 if (ok) |
1294 /* No printers, if just 1 byte is enough */ | |
1295 return Qnil; | |
1296 | |
1297 if (GetLastError () != ERROR_INSUFFICIENT_BUFFER) | |
1298 signal_enum_printer_error (); | |
1299 | |
2367 | 1300 data_buf = alloca_array (BYTE, bytes_needed); |
771 | 1301 ok = qxeEnumPrinters (enum_flags, NULL, enum_level, data_buf, bytes_needed, |
1302 &bytes_needed, &num_printers); | |
442 | 1303 if (!ok) |
1304 signal_enum_printer_error (); | |
1305 | |
1306 if (num_printers == 0) | |
1307 /* Strange but... */ | |
1308 return Qnil; | |
1309 | |
1310 GCPRO2 (result, def_printer); | |
1311 | |
1312 while (num_printers--) | |
1313 { | |
771 | 1314 Extbyte *printer_name; |
442 | 1315 if (have_nt) |
1316 { | |
771 | 1317 PRINTER_INFO_4 *info = (PRINTER_INFO_4 *) data_buf; |
1318 printer_name = (Extbyte *) info->pPrinterName; | |
442 | 1319 } |
1320 else | |
1321 { | |
771 | 1322 PRINTER_INFO_5 *info = (PRINTER_INFO_5 *) data_buf; |
1323 printer_name = (Extbyte *) info->pPrinterName; | |
442 | 1324 } |
1325 data_buf += enum_entry_size; | |
1326 | |
771 | 1327 result = Fcons (build_tstr_string (printer_name), result); |
442 | 1328 } |
1329 | |
1330 def_printer = msprinter_default_printer (); | |
1331 result = Fdelete (def_printer, result); | |
1332 result = Fcons (def_printer, result); | |
1333 | |
1334 RETURN_UNGCPRO (result); | |
440 | 1335 } |
1336 | |
1337 | |
1338 /************************************************************************/ | |
428 | 1339 /* initialization */ |
1340 /************************************************************************/ | |
1341 | |
1342 void | |
1343 syms_of_device_mswindows (void) | |
1344 { | |
5117
3742ea8250b5
Checking in final CVS version of workspace 'ben-lisp-object'
Ben Wing <ben@xemacs.org>
parents:
3025
diff
changeset
|
1345 INIT_LISP_OBJECT (devmode); |
442 | 1346 |
1347 DEFSUBR (Fmsprinter_get_settings); | |
1348 DEFSUBR (Fmsprinter_select_settings); | |
1349 DEFSUBR (Fmsprinter_apply_settings); | |
1350 DEFSUBR (Fmsprinter_settings_copy); | |
1351 DEFSUBR (Fmsprinter_settings_despecialize); | |
1352 DEFSUBR (Fmswindows_get_default_printer); | |
1353 DEFSUBR (Fmswindows_printer_list); | |
1354 | |
510 | 1355 DEFKEYWORD (Q_allow_selection); |
1356 DEFKEYWORD (Q_allow_pages); | |
1357 DEFKEYWORD (Q_selected_page_button); | |
1358 DEFSYMBOL (Qselected_page_button); | |
1359 | |
1360 DEFSYMBOL (Qinit_pre_mswindows_win); | |
1361 DEFSYMBOL (Qinit_post_mswindows_win); | |
428 | 1362 } |
1363 | |
1364 void | |
1365 console_type_create_device_mswindows (void) | |
1366 { | |
1367 CONSOLE_HAS_METHOD (mswindows, init_device); | |
1368 CONSOLE_HAS_METHOD (mswindows, finish_init_device); | |
440 | 1369 CONSOLE_HAS_METHOD (mswindows, mark_device); |
428 | 1370 CONSOLE_HAS_METHOD (mswindows, delete_device); |
1371 CONSOLE_HAS_METHOD (mswindows, device_system_metrics); | |
545 | 1372 CONSOLE_IMPLEMENTATION_FLAGS (mswindows, XDEVIMPF_PIXEL_GEOMETRY); |
440 | 1373 |
1374 CONSOLE_HAS_METHOD (msprinter, init_device); | |
1375 CONSOLE_HAS_METHOD (msprinter, mark_device); | |
1376 CONSOLE_HAS_METHOD (msprinter, delete_device); | |
1377 CONSOLE_HAS_METHOD (msprinter, device_system_metrics); | |
545 | 1378 CONSOLE_IMPLEMENTATION_FLAGS (msprinter, (XDEVIMPF_PIXEL_GEOMETRY |
1379 | XDEVIMPF_IS_A_PRINTER | |
1380 | XDEVIMPF_NO_AUTO_REDISPLAY | |
1381 | XDEVIMPF_DONT_PREEMPT_REDISPLAY | |
1382 | XDEVIMPF_FRAMELESS_OK)); | |
428 | 1383 } |
1384 | |
440 | 1385 |
428 | 1386 void |
1387 vars_of_device_mswindows (void) | |
1388 { | |
1389 } |