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.
5 This file is part of XEmacs.
7 XEmacs is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 2, or (at your option) any
12 XEmacs is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with XEmacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /* Synched up with: Not in FSF. */
26 Original authors: Jamie Zawinski and the FSF
27 Rewritten by Ben Wing and Chuck Thompson.
28 Rewritten for mswindows by Jonathan Harris, November 1997 for 21.0.
35 #include "console-msw.h"
36 #include "console-stream.h"
37 #include "objects-msw.h"
43 #if !(defined (__CYGWIN32__) || defined(__MINGW32__))
44 # include <objbase.h> /* For CoInitialize */
47 /* win32 DDE management library globals */
49 DWORD mswindows_dde_mlid;
50 HSZ mswindows_dde_service;
51 HSZ mswindows_dde_topic_system;
52 HSZ mswindows_dde_item_open;
55 /* Control conversion of upper case file names to lower case.
56 nil means no, t means yes. */
57 Lisp_Object Vmswindows_downcase_file_names;
59 /* Control whether stat() attempts to determine file type and link count
60 exactly, at the expense of slower operation. Since true hard links
61 are supported on NTFS volumes, this is only relevant on NT. */
62 Lisp_Object Vmswindows_get_true_file_attributes;
64 Lisp_Object Qinit_pre_mswindows_win, Qinit_post_mswindows_win;
67 /************************************************************************/
69 /************************************************************************/
72 build_syscolor_string (int idx)
74 return (idx < 0 ? Qnil : mswindows_color_to_string (GetSysColor (idx)));
78 build_syscolor_cons (int index1, int index2)
80 Lisp_Object color1, color2;
83 color1 = build_syscolor_string (index1);
84 color2 = build_syscolor_string (index2);
85 RETURN_UNGCPRO (Fcons (color1, color2));
89 build_sysmetrics_cons (int index1, int index2)
91 return Fcons (index1 < 0 ? Qnil : make_int (GetSystemMetrics (index1)),
92 index2 < 0 ? Qnil : make_int (GetSystemMetrics (index2)));
96 build_devicecaps_cons (HDC hdc, int index1, int index2)
98 return Fcons (index1 < 0 ? Qnil : make_int (GetDeviceCaps (hdc, index1)),
99 index2 < 0 ? Qnil : make_int (GetDeviceCaps (hdc, index2)));
104 /************************************************************************/
105 /* display methods */
106 /************************************************************************/
109 mswindows_init_device (struct device *d, Lisp_Object props)
114 DEVICE_CLASS (d) = Qcolor;
115 DEVICE_INFD (d) = DEVICE_OUTFD (d) = -1;
119 d->device_data = xnew_and_zero (struct mswindows_device);
120 hdc = CreateCompatibleDC (NULL);
122 DEVICE_MSWINDOWS_LOGPIXELSX(d) = GetDeviceCaps(hdc, LOGPIXELSX);
123 DEVICE_MSWINDOWS_LOGPIXELSY(d) = GetDeviceCaps(hdc, LOGPIXELSY);
124 DEVICE_MSWINDOWS_PLANES(d) = GetDeviceCaps(hdc, PLANES);
125 /* #### SIZEPALETTE only valid if RC_PALETTE bit set in RASTERCAPS,
126 what should we return for a non-palette-based device? */
127 DEVICE_MSWINDOWS_CELLS(d) = GetDeviceCaps(hdc, SIZEPALETTE);
128 DEVICE_MSWINDOWS_HORZRES(d) = GetDeviceCaps(hdc, HORZRES);
129 DEVICE_MSWINDOWS_VERTRES(d) = GetDeviceCaps(hdc, VERTRES);
130 DEVICE_MSWINDOWS_HORZSIZE(d) = GetDeviceCaps(hdc, HORZSIZE);
131 DEVICE_MSWINDOWS_VERTSIZE(d) = GetDeviceCaps(hdc, VERTSIZE);
132 DEVICE_MSWINDOWS_BITSPIXEL(d) = GetDeviceCaps(hdc, BITSPIXEL);
133 DEVICE_MSWINDOWS_FONTLIST (d) = mswindows_enumerate_fonts (hdc);
135 DEVICE_MSWINDOWS_HCDC(d) = hdc;
137 /* Register the main window class */
138 wc.cbSize = sizeof (WNDCLASSEX);
139 wc.style = CS_OWNDC; /* One DC per window */
140 wc.lpfnWndProc = (WNDPROC) mswindows_wnd_proc;
142 wc.cbWndExtra = MSWINDOWS_WINDOW_EXTRA_BYTES;
143 /* This must match whatever is passed to CreateWIndowEx, NULL is ok
146 wc.hIcon = LoadIcon (GetModuleHandle(NULL), XEMACS_CLASS);
147 wc.hCursor = LoadCursor (NULL, IDC_ARROW);
148 /* Background brush is only used during sizing, when XEmacs cannot
150 wc.hbrBackground = (HBRUSH)(COLOR_APPWORKSPACE + 1);
151 wc.lpszMenuName = NULL;
153 wc.lpszClassName = XEMACS_CLASS;
154 wc.hIconSm = (HICON) LoadImage (GetModuleHandle (NULL), XEMACS_CLASS,
155 IMAGE_ICON, 16, 16, 0);
156 RegisterClassEx (&wc);
160 /* Register the main window class */
161 wc.cbSize = sizeof (WNDCLASSEX);
162 wc.lpfnWndProc = (WNDPROC) mswindows_control_wnd_proc;
163 wc.lpszClassName = XEMACS_CONTROL_CLASS;
165 RegisterClassEx (&wc);
168 #if defined (HAVE_TOOLBARS) || defined (HAVE_WIDGETS)
169 InitCommonControls ();
174 mswindows_finish_init_device (struct device *d, Lisp_Object props)
176 /* Initialize DDE management library and our related globals. We execute a
177 * dde Open("file") by simulating a drop, so this depends on dnd support. */
178 #ifdef HAVE_DRAGNDROP
179 # if !(defined(__CYGWIN32__) || defined(__MINGW32__))
183 mswindows_dde_mlid = 0;
184 DdeInitialize (&mswindows_dde_mlid, (PFNCALLBACK)mswindows_dde_callback,
185 APPCMD_FILTERINITS|CBF_FAIL_SELFCONNECTIONS|CBF_FAIL_ADVISES|
186 CBF_FAIL_POKES|CBF_FAIL_REQUESTS|CBF_SKIP_ALLNOTIFICATIONS,
189 mswindows_dde_service = DdeCreateStringHandle (mswindows_dde_mlid,
191 mswindows_dde_topic_system = DdeCreateStringHandle (mswindows_dde_mlid,
193 mswindows_dde_item_open = DdeCreateStringHandle (mswindows_dde_mlid,
194 TEXT(MSWINDOWS_DDE_ITEM_OPEN), 0);
195 DdeNameService (mswindows_dde_mlid, mswindows_dde_service, 0L, DNS_REGISTER);
200 mswindows_delete_device (struct device *d)
202 #ifdef HAVE_DRAGNDROP
203 DdeNameService (mswindows_dde_mlid, 0L, 0L, DNS_UNREGISTER);
204 DdeFreeStringHandle (mswindows_dde_mlid, mswindows_dde_item_open);
205 DdeFreeStringHandle (mswindows_dde_mlid, mswindows_dde_topic_system);
206 DdeFreeStringHandle (mswindows_dde_mlid, mswindows_dde_service);
207 DdeUninitialize (mswindows_dde_mlid);
209 # if !(defined(__CYGWIN32__) || defined(__MINGW32__))
214 DeleteDC (DEVICE_MSWINDOWS_HCDC(d));
215 free (d->device_data);
219 msw_get_workspace_coords (RECT *rc)
221 SystemParametersInfo (SPI_GETWORKAREA, 0, rc, 0);
225 mswindows_mark_device (struct device *d)
227 mark_object (DEVICE_MSWINDOWS_FONTLIST (d));
231 mswindows_device_system_metrics (struct device *d,
232 enum device_metrics m)
237 return Fcons (make_int (DEVICE_MSWINDOWS_HORZRES(d)),
238 make_int (DEVICE_MSWINDOWS_VERTRES(d)));
241 return Fcons (make_int (DEVICE_MSWINDOWS_LOGPIXELSX(d)),
242 make_int (DEVICE_MSWINDOWS_LOGPIXELSY(d)));
244 case DM_size_device_mm:
245 return Fcons (make_int (DEVICE_MSWINDOWS_HORZSIZE(d)),
246 make_int (DEVICE_MSWINDOWS_VERTSIZE(d)));
248 case DM_num_bit_planes:
249 /* this is what X means by bitplanes therefore we ought to be
250 consistent. num planes is always 1 under mswindows and
252 return make_int (DEVICE_MSWINDOWS_BITSPIXEL(d));
254 case DM_num_color_cells:
255 return make_int (DEVICE_MSWINDOWS_CELLS(d));
259 #define FROB(met, fore, back) \
261 return build_syscolor_cons (fore, back);
263 FROB (color_default, COLOR_WINDOWTEXT, COLOR_WINDOW);
264 FROB (color_select, COLOR_HIGHLIGHTTEXT, COLOR_HIGHLIGHT);
265 FROB (color_balloon, COLOR_INFOTEXT, COLOR_INFOBK);
266 FROB (color_3d_face, COLOR_BTNTEXT, COLOR_BTNFACE);
267 FROB (color_3d_light, COLOR_3DHILIGHT, COLOR_3DLIGHT);
268 FROB (color_3d_dark, COLOR_3DDKSHADOW, COLOR_3DSHADOW);
269 FROB (color_menu, COLOR_MENUTEXT, COLOR_MENU);
270 FROB (color_menu_highlight, COLOR_HIGHLIGHTTEXT, COLOR_HIGHLIGHT);
271 FROB (color_menu_button, COLOR_MENUTEXT, COLOR_MENU);
272 FROB (color_menu_disabled, COLOR_GRAYTEXT, COLOR_MENU);
273 FROB (color_toolbar, COLOR_BTNTEXT, COLOR_BTNFACE);
274 FROB (color_scrollbar, COLOR_CAPTIONTEXT, COLOR_SCROLLBAR);
275 FROB (color_desktop, -1, COLOR_DESKTOP);
276 FROB (color_workspace, -1, COLOR_APPWORKSPACE);
280 #define FROB(met, index1, index2) \
282 return build_sysmetrics_cons (index1, index2);
284 FROB (size_cursor, SM_CXCURSOR, SM_CYCURSOR);
285 FROB (size_scrollbar, SM_CXVSCROLL, SM_CYHSCROLL);
286 FROB (size_menu, -1, SM_CYMENU);
287 FROB (size_icon, SM_CXICON, SM_CYICON);
288 FROB (size_icon_small, SM_CXSMICON, SM_CYSMICON);
291 case DM_size_workspace:
294 msw_get_workspace_coords (&rc);
295 return Fcons (make_int (rc.right - rc.left),
296 make_int (rc.bottom - rc.top));
299 case DM_offset_workspace:
302 msw_get_workspace_coords (&rc);
303 return Fcons (make_int (rc.left), make_int (rc.top));
307 case DM_size_toolbar:
308 case DM_size_toolbar_button:
309 case DM_size_toolbar_border:
313 #define FROB(met, index) \
315 return make_int (GetSystemMetrics (index));
317 FROB (mouse_buttons, SM_CMOUSEBUTTONS);
318 FROB (swap_buttons, SM_SWAPBUTTON);
319 FROB (show_sounds, SM_SHOWSOUNDS);
320 FROB (slow_device, SM_SLOWMACHINE);
321 FROB (security, SM_SECURE);
326 /* Do not know such property */
331 mswindows_device_implementation_flags (void)
333 return XDEVIMPF_PIXEL_GEOMETRY;
337 /************************************************************************/
338 /* printer methods */
339 /************************************************************************/
342 signal_open_printer_error (struct device *d)
344 signal_simple_error ("Failed to open printer", DEVICE_CONNECTION (d));
348 msprinter_init_device (struct device *d, Lisp_Object props)
352 DEVICE_INFD (d) = DEVICE_OUTFD (d) = -1;
354 CHECK_STRING (DEVICE_CONNECTION (d));
356 TO_EXTERNAL_FORMAT (LISP_STRING, DEVICE_CONNECTION (d),
357 C_STRING_ALLOCA, printer_name,
360 d->device_data = xnew_and_zero (struct msprinter_device);
362 DEVICE_MSPRINTER_NAME(d) = xstrdup (printer_name);
364 if (!OpenPrinter (printer_name, &DEVICE_MSPRINTER_HPRINTER (d), NULL))
366 DEVICE_MSPRINTER_HPRINTER (d) = NULL;
367 signal_open_printer_error (d);
370 DEVICE_MSPRINTER_HDC (d) = CreateDC ("WINSPOOL", printer_name,
372 if (DEVICE_MSPRINTER_HDC (d) == NULL)
373 signal_open_printer_error (d);
375 DEVICE_MSPRINTER_HCDC(d) =
376 CreateCompatibleDC (DEVICE_MSPRINTER_HDC (d));
378 /* Determinie DEVMODE size and store the default DEVMODE */
379 DEVICE_MSPRINTER_DEVMODE_SIZE(d) =
380 DocumentProperties (NULL, DEVICE_MSPRINTER_HPRINTER(d),
381 printer_name, NULL, NULL, 0);
382 if (DEVICE_MSPRINTER_DEVMODE_SIZE(d) <= 0)
383 signal_open_printer_error (d);
385 DEVICE_MSPRINTER_DEVMODE(d) =
386 (DEVMODE*) xmalloc (DEVICE_MSPRINTER_DEVMODE_SIZE(d));
387 DocumentProperties (NULL, DEVICE_MSPRINTER_HPRINTER(d),
388 printer_name, DEVICE_MSPRINTER_DEVMODE(d),
389 NULL, DM_OUT_BUFFER);
391 /* We do not use printer fon list as we do with the display
392 device. Rather, we allow GDI to pick the closest match to the
394 DEVICE_MSPRINTER_FONTLIST (d) = Qnil;
396 DEVICE_CLASS (d) = (GetDeviceCaps (DEVICE_MSPRINTER_HDC (d), BITSPIXEL)
397 * GetDeviceCaps (DEVICE_MSPRINTER_HDC (d), PLANES)
398 > 1) ? Qcolor : Qmono;
402 msprinter_device_system_metrics (struct device *d,
403 enum device_metrics m)
407 /* Device sizes - pixel and mm */
408 #define FROB(met, index1, index2) \
410 return build_devicecaps_cons \
411 (DEVICE_MSPRINTER_HDC(d), index1, index2);
413 FROB (size_device, PHYSICALWIDTH, PHYSICALHEIGHT);
414 FROB (size_device_mm, HORZSIZE, VERTSIZE);
415 FROB (size_workspace, HORZRES, VERTRES);
416 FROB (offset_workspace, PHYSICALOFFSETX, PHYSICALOFFSETY);
417 FROB (device_dpi, LOGPIXELSX, LOGPIXELSY);
420 case DM_num_bit_planes:
421 /* this is what X means by bitplanes therefore we ought to be
422 consistent. num planes is always 1 under mswindows and
424 return make_int (GetDeviceCaps (DEVICE_MSPRINTER_HDC(d), BITSPIXEL));
426 case DM_num_color_cells: /* Prnters are non-palette devices */
427 case DM_slow_device: /* Animation would be a really bad idea */
428 case DM_security: /* Not provided by windows */
432 /* Do not know such property */
437 msprinter_delete_device (struct device *d)
441 if (DEVICE_MSPRINTER_HPRINTER (d))
442 ClosePrinter (DEVICE_MSPRINTER_HPRINTER (d));
443 if (DEVICE_MSPRINTER_HDC (d))
444 DeleteDC (DEVICE_MSPRINTER_HDC (d));
445 if (DEVICE_MSPRINTER_HCDC (d))
446 DeleteDC (DEVICE_MSPRINTER_HCDC (d));
447 if (DEVICE_MSPRINTER_NAME (d))
448 free (DEVICE_MSPRINTER_NAME (d));
449 if (DEVICE_MSPRINTER_DEVMODE (d))
450 free (DEVICE_MSPRINTER_DEVMODE (d));
451 if (DEVICE_MSPRINTER_DEVMODE_MIRROR (d))
452 free (DEVICE_MSPRINTER_DEVMODE_MIRROR (d));
454 free (d->device_data);
459 msprinter_mark_device (struct device *d)
461 mark_object (DEVICE_MSPRINTER_FONTLIST (d));
465 msprinter_device_implementation_flags (void)
467 return ( XDEVIMPF_PIXEL_GEOMETRY
468 | XDEVIMPF_IS_A_PRINTER
469 | XDEVIMPF_NO_AUTO_REDISPLAY
470 | XDEVIMPF_FRAMELESS_OK );
474 /************************************************************************/
475 /* printer external functions */
476 /************************************************************************/
479 * Return a copy of default DEVMODE. The copy returned is in
480 * a static buffer which will be overwritten by next call.
483 msprinter_get_devmode_copy (struct device *d)
485 assert (DEVICE_MSPRINTER_P (d));
487 if (DEVICE_MSPRINTER_DEVMODE_MIRROR(d) == NULL)
488 DEVICE_MSPRINTER_DEVMODE_MIRROR(d) =
489 (DEVMODE*) xmalloc (DEVICE_MSPRINTER_DEVMODE_SIZE(d));
491 memcpy (DEVICE_MSPRINTER_DEVMODE_MIRROR(d),
492 DEVICE_MSPRINTER_DEVMODE(d),
493 DEVICE_MSPRINTER_DEVMODE_SIZE(d));
495 return DEVICE_MSPRINTER_DEVMODE_MIRROR(d);
499 * Apply settings from the DEVMODE. The settings are considered
500 * incremental to the default DEVMODE, so that changes in the
501 * passed structure supercede parameters of the printer.
503 * The passed structure is overwritten by the fuction call;
504 * complete printer settings are returned.
507 msprinter_apply_devmode (struct device *d, DEVMODE *devmode)
509 assert (DEVICE_MSPRINTER_P (d));
511 DocumentProperties (NULL,
512 DEVICE_MSPRINTER_HPRINTER(d),
513 DEVICE_MSPRINTER_NAME(d),
515 DM_IN_BUFFER | DM_OUT_BUFFER);
517 /* #### ResetDC fails sometimes, Bill only know s why.
518 The solution below looks more like a workaround to me,
519 although it might be fine. --kkm */
520 if (ResetDC (DEVICE_MSPRINTER_HDC (d), devmode) == NULL)
522 DeleteDC (DEVICE_MSPRINTER_HDC (d));
523 DEVICE_MSPRINTER_HDC (d) =
524 CreateDC ("WINSPOOL", DEVICE_MSPRINTER_NAME(d), NULL, devmode);
529 /************************************************************************/
531 /************************************************************************/
534 syms_of_device_mswindows (void)
536 defsymbol (&Qinit_pre_mswindows_win, "init-pre-mswindows-win");
537 defsymbol (&Qinit_post_mswindows_win, "init-post-mswindows-win");
541 console_type_create_device_mswindows (void)
543 CONSOLE_HAS_METHOD (mswindows, init_device);
544 CONSOLE_HAS_METHOD (mswindows, finish_init_device);
545 CONSOLE_HAS_METHOD (mswindows, mark_device);
546 CONSOLE_HAS_METHOD (mswindows, delete_device);
547 CONSOLE_HAS_METHOD (mswindows, device_system_metrics);
548 CONSOLE_HAS_METHOD (mswindows, device_implementation_flags);
550 CONSOLE_HAS_METHOD (msprinter, init_device);
551 CONSOLE_HAS_METHOD (msprinter, mark_device);
552 CONSOLE_HAS_METHOD (msprinter, delete_device);
553 CONSOLE_HAS_METHOD (msprinter, device_system_metrics);
554 CONSOLE_HAS_METHOD (msprinter, device_implementation_flags);
559 vars_of_device_mswindows (void)
561 DEFVAR_LISP ("mswindows-downcase-file-names", &Vmswindows_downcase_file_names /*
562 Non-nil means convert all-upper case file names to lower case.
563 This applies when performing completions and file name expansion.
565 Vmswindows_downcase_file_names = Qnil;
567 DEFVAR_LISP ("mswindows-get-true-file-attributes", &Vmswindows_get_true_file_attributes /*
568 Non-nil means determine accurate link count in file-attributes.
569 This option slows down file-attributes noticeably, so is disabled by
570 default. Note that it is only useful for files on NTFS volumes,
571 where hard links are supported.
573 Vmswindows_get_true_file_attributes = Qnil;