XEmacs 21.2.32 "Kastor & Polydeukes".
[chise/xemacs-chise.git.1] / src / device-msw.c
index 18231ba..1734233 100644 (file)
@@ -34,11 +34,23 @@ Boston, MA 02111-1307, USA.  */
 
 #include "console-msw.h"
 #include "console-stream.h"
+#include "objects-msw.h"
 #include "events.h"
 #include "faces.h"
 #include "frame.h"
 #include "sysdep.h"
 
+#if (defined (__CYGWIN32__) || defined(__MINGW32__)) && \
+       CYGWIN_VERSION_DLL_MAJOR < 21
+extern BOOL WINAPI DdeFreeStringHandle(DWORD,HSZ);
+#else
+#include <winspool.h>
+#endif
+
+#if !(defined (__CYGWIN32__) || defined(__MINGW32__))
+# include <objbase.h>  /* For CoInitialize */
+#endif
+
 /* win32 DDE management library globals */
 #ifdef HAVE_DRAGNDROP
 DWORD mswindows_dde_mlid;
@@ -64,20 +76,9 @@ Lisp_Object Qinit_pre_mswindows_win, Qinit_post_mswindows_win;
 /************************************************************************/
 
 static Lisp_Object
-build_syscolor_string (int index)
+build_syscolor_string (int idx)
 {
-  DWORD clr;
-  char buf[16];
-
-  if (index < 0)
-    return Qnil;
-
-  clr = GetSysColor (index);
-  sprintf (buf, "#%02X%02X%02X",
-          GetRValue (clr),
-          GetGValue (clr),
-          GetBValue (clr));
-  return build_string (buf);
+  return (idx < 0 ? Qnil : mswindows_color_to_string (GetSysColor (idx)));
 }
 
 static Lisp_Object
@@ -98,10 +99,17 @@ build_sysmetrics_cons (int index1, int index2)
                index2 < 0 ? Qnil : make_int (GetSystemMetrics (index2)));
 }
 
+static Lisp_Object
+build_devicecaps_cons (HDC hdc, int index1, int index2)
+{
+  return Fcons (index1 < 0 ? Qnil : make_int (GetDeviceCaps (hdc, index1)),
+               index2 < 0 ? Qnil : make_int (GetDeviceCaps (hdc, index2)));
+}
+
 
 \f
 /************************************************************************/
-/*                               methods                                */
+/*                          display methods                             */
 /************************************************************************/
 
 static void
@@ -129,9 +137,9 @@ mswindows_init_device (struct device *d, Lisp_Object props)
   DEVICE_MSWINDOWS_HORZSIZE(d) = GetDeviceCaps(hdc, HORZSIZE);
   DEVICE_MSWINDOWS_VERTSIZE(d) = GetDeviceCaps(hdc, VERTSIZE);
   DEVICE_MSWINDOWS_BITSPIXEL(d) = GetDeviceCaps(hdc, BITSPIXEL);
-  DeleteDC (hdc);
-
-  mswindows_enumerate_fonts (d);
+  DEVICE_MSWINDOWS_FONTLIST (d) = mswindows_enumerate_fonts (hdc);
+  
+  DEVICE_MSWINDOWS_HCDC(d) = hdc;
 
   /* Register the main window class */
   wc.cbSize = sizeof (WNDCLASSEX);
@@ -150,7 +158,7 @@ mswindows_init_device (struct device *d, Lisp_Object props)
   wc.lpszMenuName = NULL;
 
   wc.lpszClassName = XEMACS_CLASS;
-  wc.hIconSm = LoadImage (GetModuleHandle (NULL), XEMACS_CLASS,
+  wc.hIconSm = (HICON) LoadImage (GetModuleHandle (NULL), XEMACS_CLASS,
                          IMAGE_ICON, 16, 16, 0);
   RegisterClassEx (&wc);
 
@@ -164,7 +172,7 @@ mswindows_init_device (struct device *d, Lisp_Object props)
   RegisterClassEx (&wc);
 #endif
 
-#ifdef HAVE_TOOLBARS
+#if defined (HAVE_TOOLBARS) || defined (HAVE_WIDGETS)
   InitCommonControls ();
 #endif
 }
@@ -175,13 +183,20 @@ mswindows_finish_init_device (struct device *d, Lisp_Object props)
   /* Initialize DDE management library and our related globals. We execute a
    * dde Open("file") by simulating a drop, so this depends on dnd support. */
 #ifdef HAVE_DRAGNDROP
+# if !(defined(__CYGWIN32__) || defined(__MINGW32__))
+  CoInitialize (NULL);
+# endif
+
   mswindows_dde_mlid = 0;
   DdeInitialize (&mswindows_dde_mlid, (PFNCALLBACK)mswindows_dde_callback,
                 APPCMD_FILTERINITS|CBF_FAIL_SELFCONNECTIONS|CBF_FAIL_ADVISES|
-                CBF_FAIL_POKES|CBF_FAIL_REQUESTS|CBF_SKIP_ALLNOTIFICATIONS, 0);
+                CBF_FAIL_POKES|CBF_FAIL_REQUESTS|CBF_SKIP_ALLNOTIFICATIONS,
+                0);
   
-  mswindows_dde_service = DdeCreateStringHandle (mswindows_dde_mlid, XEMACS_CLASS, 0);
-  mswindows_dde_topic_system = DdeCreateStringHandle (mswindows_dde_mlid, SZDDESYS_TOPIC, 0);
+  mswindows_dde_service = DdeCreateStringHandle (mswindows_dde_mlid,
+                                                XEMACS_CLASS, 0);
+  mswindows_dde_topic_system = DdeCreateStringHandle (mswindows_dde_mlid,
+                                                     SZDDESYS_TOPIC, 0);
   mswindows_dde_item_open = DdeCreateStringHandle (mswindows_dde_mlid,
                                                   TEXT(MSWINDOWS_DDE_ITEM_OPEN), 0);
   DdeNameService (mswindows_dde_mlid, mswindows_dde_service, 0L, DNS_REGISTER);
@@ -191,24 +206,34 @@ mswindows_finish_init_device (struct device *d, Lisp_Object props)
 static void
 mswindows_delete_device (struct device *d)
 {
-  struct mswindows_font_enum *fontlist, *next;
-
-  fontlist = DEVICE_MSWINDOWS_FONTLIST (d);
-  while (fontlist)
-    {
-      next = fontlist->next;
-      free (fontlist);
-      fontlist = next;
-    }
-
 #ifdef HAVE_DRAGNDROP
-  DdeNameService (mswindows_dde_mlid, 0L, 0L, DNS_REGISTER);
+  DdeNameService (mswindows_dde_mlid, 0L, 0L, DNS_UNREGISTER);
+  DdeFreeStringHandle (mswindows_dde_mlid, mswindows_dde_item_open);
+  DdeFreeStringHandle (mswindows_dde_mlid, mswindows_dde_topic_system);
+  DdeFreeStringHandle (mswindows_dde_mlid, mswindows_dde_service);
   DdeUninitialize (mswindows_dde_mlid);
+
+# if !(defined(__CYGWIN32__) || defined(__MINGW32__))
+  CoUninitialize ();
+# endif
 #endif
 
+  DeleteDC (DEVICE_MSWINDOWS_HCDC(d));
   free (d->device_data);
 }
 
+void
+msw_get_workspace_coords (RECT *rc)
+{
+  SystemParametersInfo (SPI_GETWORKAREA, 0, rc, 0);
+}
+
+static void
+mswindows_mark_device (struct device *d)
+{
+  mark_object (DEVICE_MSWINDOWS_FONTLIST (d));
+}
+
 static Lisp_Object
 mswindows_device_system_metrics (struct device *d,
                                 enum device_metrics m)
@@ -219,6 +244,10 @@ mswindows_device_system_metrics (struct device *d,
       return Fcons (make_int (DEVICE_MSWINDOWS_HORZRES(d)),
                    make_int (DEVICE_MSWINDOWS_VERTRES(d)));
       break;
+    case DM_device_dpi:
+      return Fcons (make_int (DEVICE_MSWINDOWS_LOGPIXELSX(d)),
+                   make_int (DEVICE_MSWINDOWS_LOGPIXELSY(d)));
+      break;
     case DM_size_device_mm:
       return Fcons (make_int (DEVICE_MSWINDOWS_HORZSIZE(d)),
                    make_int (DEVICE_MSWINDOWS_VERTSIZE(d)));
@@ -234,22 +263,22 @@ mswindows_device_system_metrics (struct device *d,
       break;
 
       /*** Colors ***/
-#define FROB(met, index1, index2)                      \
+#define FROB(met, fore, back)                          \
     case DM_##met:                                     \
-      return build_syscolor_cons (index1, index2);
+      return build_syscolor_cons (fore, back);
       
-      FROB (color_default, COLOR_WINDOW, COLOR_WINDOWTEXT);
-      FROB (color_select, COLOR_HIGHLIGHT, COLOR_HIGHLIGHTTEXT);
-      FROB (color_balloon, COLOR_INFOBK, COLOR_INFOTEXT);
-      FROB (color_3d_face, COLOR_3DFACE, COLOR_BTNTEXT);
-      FROB (color_3d_light, COLOR_3DLIGHT, COLOR_3DHILIGHT);
-      FROB (color_3d_dark, COLOR_3DSHADOW, COLOR_3DDKSHADOW);
-      FROB (color_menu, COLOR_MENU, COLOR_MENUTEXT);
-      FROB (color_menu_highlight, COLOR_HIGHLIGHT, COLOR_HIGHLIGHTTEXT);
-      FROB (color_menu_button, COLOR_MENU, COLOR_MENUTEXT);
-      FROB (color_menu_disabled, COLOR_MENU, COLOR_GRAYTEXT);
-      FROB (color_toolbar, COLOR_BTNFACE, COLOR_BTNTEXT);
-      FROB (color_scrollbar, COLOR_SCROLLBAR, COLOR_CAPTIONTEXT);
+      FROB (color_default, COLOR_WINDOWTEXT, COLOR_WINDOW);
+      FROB (color_select, COLOR_HIGHLIGHTTEXT, COLOR_HIGHLIGHT);
+      FROB (color_balloon, COLOR_INFOTEXT, COLOR_INFOBK);
+      FROB (color_3d_face, COLOR_BTNTEXT, COLOR_BTNFACE);
+      FROB (color_3d_light, COLOR_3DHILIGHT, COLOR_3DLIGHT);
+      FROB (color_3d_dark, COLOR_3DDKSHADOW, COLOR_3DSHADOW);
+      FROB (color_menu, COLOR_MENUTEXT, COLOR_MENU);
+      FROB (color_menu_highlight, COLOR_HIGHLIGHTTEXT, COLOR_HIGHLIGHT);
+      FROB (color_menu_button, COLOR_MENUTEXT, COLOR_MENU);
+      FROB (color_menu_disabled, COLOR_GRAYTEXT, COLOR_MENU);
+      FROB (color_toolbar, COLOR_BTNTEXT, COLOR_BTNFACE);
+      FROB (color_scrollbar, COLOR_CAPTIONTEXT, COLOR_SCROLLBAR);
       FROB (color_desktop, -1, COLOR_DESKTOP);
       FROB (color_workspace, -1, COLOR_APPWORKSPACE);
 #undef FROB
@@ -269,10 +298,18 @@ mswindows_device_system_metrics (struct device *d,
     case DM_size_workspace:
       {
        RECT rc;
-       SystemParametersInfo (SPI_GETWORKAREA, 0, &rc, 0);
+       msw_get_workspace_coords (&rc);
        return Fcons (make_int (rc.right - rc.left),
                      make_int (rc.bottom - rc.top));
       }
+
+    case DM_offset_workspace:
+      {
+       RECT rc;
+       msw_get_workspace_coords (&rc);
+       return Fcons (make_int (rc.left), make_int (rc.top));
+      }
+
       /*
        case DM_size_toolbar:
        case DM_size_toolbar_button:
@@ -305,6 +342,198 @@ mswindows_device_implementation_flags (void)
 
 \f
 /************************************************************************/
+/*                          printer methods                             */
+/************************************************************************/
+
+static void
+signal_open_printer_error (struct device *d)
+{
+  signal_simple_error ("Failed to open printer", DEVICE_CONNECTION (d));
+}
+
+static void
+msprinter_init_device (struct device *d, Lisp_Object props)
+{
+  char* printer_name;
+
+  DEVICE_INFD (d) = DEVICE_OUTFD (d) = -1;
+
+  CHECK_STRING (DEVICE_CONNECTION (d));
+
+  TO_EXTERNAL_FORMAT (LISP_STRING, DEVICE_CONNECTION (d),
+                     C_STRING_ALLOCA, printer_name,
+                     Qctext);
+
+  d->device_data = xnew_and_zero (struct msprinter_device);
+
+  DEVICE_MSPRINTER_NAME(d) = xstrdup (printer_name);
+
+  if (!OpenPrinter (printer_name, &DEVICE_MSPRINTER_HPRINTER (d), NULL))
+    {
+      DEVICE_MSPRINTER_HPRINTER (d) = NULL;
+      signal_open_printer_error (d);
+    }
+
+  DEVICE_MSPRINTER_HDC (d) = CreateDC ("WINSPOOL", printer_name,
+                                      NULL, NULL);
+  if (DEVICE_MSPRINTER_HDC (d) == NULL)
+    signal_open_printer_error (d);
+
+  DEVICE_MSPRINTER_HCDC(d) =
+    CreateCompatibleDC (DEVICE_MSPRINTER_HDC (d));
+
+  /* Determinie DEVMODE size and store the default DEVMODE */
+  DEVICE_MSPRINTER_DEVMODE_SIZE(d) =
+    DocumentProperties (NULL, DEVICE_MSPRINTER_HPRINTER(d),
+                       printer_name, NULL, NULL, 0);
+  if (DEVICE_MSPRINTER_DEVMODE_SIZE(d) <= 0)
+    signal_open_printer_error (d);
+
+  DEVICE_MSPRINTER_DEVMODE(d) =
+    (DEVMODE*) xmalloc (DEVICE_MSPRINTER_DEVMODE_SIZE(d));
+  DocumentProperties (NULL, DEVICE_MSPRINTER_HPRINTER(d),
+                     printer_name, DEVICE_MSPRINTER_DEVMODE(d),
+                     NULL, DM_OUT_BUFFER);
+
+  /* We do not use printer fon list as we do with the display
+     device. Rather, we allow GDI to pick the closest match to the
+     display font. */
+  DEVICE_MSPRINTER_FONTLIST (d) = Qnil;
+
+  DEVICE_CLASS (d) = (GetDeviceCaps (DEVICE_MSPRINTER_HDC (d), BITSPIXEL)
+                     * GetDeviceCaps (DEVICE_MSPRINTER_HDC (d), PLANES)
+                     > 1) ? Qcolor : Qmono;
+}
+
+static Lisp_Object
+msprinter_device_system_metrics (struct device *d,
+                                enum device_metrics m)
+{
+  switch (m)
+    {
+      /* Device sizes - pixel and mm */
+#define FROB(met, index1, index2)                      \
+    case DM_##met:                                     \
+      return build_devicecaps_cons                     \
+         (DEVICE_MSPRINTER_HDC(d), index1, index2);
+
+      FROB (size_device, PHYSICALWIDTH, PHYSICALHEIGHT);
+      FROB (size_device_mm, HORZSIZE, VERTSIZE);
+      FROB (size_workspace, HORZRES, VERTRES);
+      FROB (offset_workspace, PHYSICALOFFSETX, PHYSICALOFFSETY);
+      FROB (device_dpi, LOGPIXELSX, LOGPIXELSY);
+#undef FROB
+
+    case DM_num_bit_planes:
+      /* this is what X means by bitplanes therefore we ought to be
+         consistent. num planes is always 1 under mswindows and
+         therefore useless */
+      return make_int (GetDeviceCaps (DEVICE_MSPRINTER_HDC(d), BITSPIXEL));
+
+    case DM_num_color_cells:   /* Prnters are non-palette devices */
+    case DM_slow_device:       /* Animation would be a really bad idea */
+    case DM_security:          /* Not provided by windows */
+      return Qzero;
+    }
+
+  /* Do not know such property */
+  return Qunbound;
+}
+
+static void
+msprinter_delete_device (struct device *d)
+{
+  if (d->device_data)
+    {
+      if (DEVICE_MSPRINTER_HPRINTER (d))
+       ClosePrinter (DEVICE_MSPRINTER_HPRINTER (d));
+      if (DEVICE_MSPRINTER_HDC (d))
+       DeleteDC (DEVICE_MSPRINTER_HDC (d));
+      if (DEVICE_MSPRINTER_HCDC (d))
+       DeleteDC (DEVICE_MSPRINTER_HCDC (d));
+      if (DEVICE_MSPRINTER_NAME (d))
+       free (DEVICE_MSPRINTER_NAME (d));
+      if (DEVICE_MSPRINTER_DEVMODE (d))
+       free (DEVICE_MSPRINTER_DEVMODE (d));
+      if (DEVICE_MSPRINTER_DEVMODE_MIRROR (d))
+       free (DEVICE_MSPRINTER_DEVMODE_MIRROR (d));
+
+      free (d->device_data);
+    }
+}
+
+static void
+msprinter_mark_device (struct device *d)
+{
+  mark_object (DEVICE_MSPRINTER_FONTLIST (d));
+}
+
+static unsigned int
+msprinter_device_implementation_flags (void)
+{
+  return (  XDEVIMPF_PIXEL_GEOMETRY
+         | XDEVIMPF_IS_A_PRINTER
+         | XDEVIMPF_NO_AUTO_REDISPLAY
+         | XDEVIMPF_FRAMELESS_OK );
+}
+
+\f
+/************************************************************************/
+/*                      printer external functions                      */
+/************************************************************************/
+
+/* 
+ * Return a copy of default DEVMODE. The copy returned is in
+ * a static buffer which will be overwritten by next call.
+ */
+DEVMODE*
+msprinter_get_devmode_copy (struct device *d)
+{
+  assert (DEVICE_MSPRINTER_P (d));
+
+  if (DEVICE_MSPRINTER_DEVMODE_MIRROR(d) == NULL)
+    DEVICE_MSPRINTER_DEVMODE_MIRROR(d) = 
+      (DEVMODE*) xmalloc (DEVICE_MSPRINTER_DEVMODE_SIZE(d));
+
+  memcpy (DEVICE_MSPRINTER_DEVMODE_MIRROR(d),
+         DEVICE_MSPRINTER_DEVMODE(d),
+         DEVICE_MSPRINTER_DEVMODE_SIZE(d));
+
+  return DEVICE_MSPRINTER_DEVMODE_MIRROR(d);
+}
+
+/*
+ * Apply settings from the DEVMODE. The settings are considered
+ * incremental to the default DEVMODE, so that changes in the
+ * passed structure supercede parameters of the printer.
+ *
+ * The passed structure is overwritten by the fuction call;
+ * complete printer settings are returned.
+ */
+void
+msprinter_apply_devmode (struct device *d, DEVMODE *devmode)
+{
+  assert (DEVICE_MSPRINTER_P (d));
+
+  DocumentProperties (NULL,
+                     DEVICE_MSPRINTER_HPRINTER(d),
+                     DEVICE_MSPRINTER_NAME(d),
+                     devmode, devmode,
+                     DM_IN_BUFFER | DM_OUT_BUFFER);
+
+  /* #### ResetDC fails sometimes, Bill only know s why.
+     The solution below looks more like a workaround to me,
+     although it might be fine. --kkm */
+  if (ResetDC (DEVICE_MSPRINTER_HDC (d), devmode) == NULL)
+    {
+      DeleteDC (DEVICE_MSPRINTER_HDC (d));
+      DEVICE_MSPRINTER_HDC (d) =
+       CreateDC ("WINSPOOL", DEVICE_MSPRINTER_NAME(d), NULL, devmode);
+    }
+}
+
+\f
+/************************************************************************/
 /*                            initialization                            */
 /************************************************************************/
 
@@ -320,12 +549,19 @@ console_type_create_device_mswindows (void)
 {
   CONSOLE_HAS_METHOD (mswindows, init_device);
   CONSOLE_HAS_METHOD (mswindows, finish_init_device);
-/*  CONSOLE_HAS_METHOD (mswindows, mark_device); */
+  CONSOLE_HAS_METHOD (mswindows, mark_device);
   CONSOLE_HAS_METHOD (mswindows, delete_device);
   CONSOLE_HAS_METHOD (mswindows, device_system_metrics);
   CONSOLE_HAS_METHOD (mswindows, device_implementation_flags);
+
+  CONSOLE_HAS_METHOD (msprinter, init_device);
+  CONSOLE_HAS_METHOD (msprinter, mark_device);
+  CONSOLE_HAS_METHOD (msprinter, delete_device);
+  CONSOLE_HAS_METHOD (msprinter, device_system_metrics);
+  CONSOLE_HAS_METHOD (msprinter, device_implementation_flags);
 }
 
+
 void
 vars_of_device_mswindows (void)
 {