+ /* #### ResetDC fails sometimes, Bill only knows why.
+ The solution below looks more like a workaround to me,
+ although it might be fine. --kkm */
+ if (ResetDC (DEVICE_MSPRINTER_HDC (d), devmode_out) == NULL)
+ {
+ DeleteDC (DEVICE_MSPRINTER_HDC (d));
+ DEVICE_MSPRINTER_HDC (d) =
+ CreateDC ("WINSPOOL", DEVICE_MSPRINTER_NAME(d), NULL, devmode_out);
+ }
+
+ return 1;
+}
+
+static void
+handle_devmode_changes (Lisp_Devmode *ldm, HGLOBAL hDevNames, HGLOBAL hDevMode)
+{
+ DEVNAMES* devnames = (DEVNAMES*) GlobalLock (hDevNames);
+ char *new_name = devnames ? (char*)devnames + devnames->wDeviceOffset : NULL;
+ DEVMODE* devmode = (DEVMODE*) GlobalLock (hDevMode);
+
+ /* Size and name may have changed */
+ ldm->devmode = xrealloc (ldm->devmode, DEVMODE_SIZE (devmode));
+ if (new_name)
+ {
+ if (ldm->printer_name)
+ xfree (ldm->printer_name);
+ ldm->printer_name = xstrdup (new_name);
+ }
+
+ if (!NILP (ldm->device))
+ {
+ /* Apply the new devmode to the printer and get a compete one back */
+ struct device *d = XDEVICE (ldm->device);
+ if (!sync_printer_with_devmode (d, devmode, ldm->devmode, new_name))
+ {
+ global_free_2_maybe (hDevNames, hDevMode);
+ error ("Printer device initialization I/O error, device deleted.");
+ }
+ }
+ else
+ {
+ /* Just copy the devmode structure */
+ memcpy (ldm->devmode, devmode, DEVMODE_SIZE (devmode));
+ }
+}
+
+static void
+ensure_not_printing (struct device *d)
+{
+ if (!NILP (DEVICE_FRAME_LIST (d)))
+ {
+ Lisp_Object device;
+ XSETDEVICE (device, d);
+ invalid_operation ("Cannot change settings while print job is active",
+ device);
+ }
+}
+
+static Lisp_Devmode *
+decode_devmode (Lisp_Object dev)
+{
+ if (DEVMODEP (dev))
+ return XDEVMODE (dev);
+ else
+ {
+ ensure_not_printing (XDEVICE (dev));
+ return XDEVMODE (DEVICE_MSPRINTER_DEVMODE (XDEVICE (dev)));
+ }
+}
+
+/*
+ * DEV can be either a printer or devmode
+ * PRINT_P is non-zero for the Print dialog, zero for the
+ * Page Setup dialog
+ */
+static Lisp_Object
+print_dialog_worker (Lisp_Object dev, int print_p)
+{
+ Lisp_Devmode *ldm = decode_devmode (dev);
+ PRINTDLG pd;
+
+ memset (&pd, 0, sizeof (pd));
+ pd.lStructSize = sizeof (pd);
+ pd.hwndOwner = mswindows_get_selected_frame_hwnd ();
+ pd.hDevMode = devmode_to_hglobal (ldm);
+ pd.Flags = (PD_NOSELECTION | PD_USEDEVMODECOPIESANDCOLLATE
+ | (print_p ? 0 : PD_PRINTSETUP));
+ pd.nMinPage = 0;
+ pd.nMaxPage = 0xFFFF;
+
+ if (!PrintDlg (&pd))
+ {
+ global_free_2_maybe (pd.hDevNames, pd.hDevMode);
+ return Qnil;
+ }
+
+ handle_devmode_changes (ldm, pd.hDevNames, pd.hDevMode);
+
+ /* Finally, build the resulting plist */
+ {
+ Lisp_Object result = Qnil;
+ struct gcpro gcpro1;
+ GCPRO1 (result);
+
+ /* Do consing in reverse order.
+ Number of copies */
+ if (print_p)
+ result = Fcons (Qcopies, Fcons (make_int (pd.nCopies), result));
+
+ /* Page range */
+ if (print_p && (pd.Flags & PD_PAGENUMS))
+ {
+ result = Fcons (Qto_page, Fcons (make_int (pd.nToPage), result));
+ result = Fcons (Qfrom_page, Fcons (make_int (pd.nFromPage), result));
+ }
+
+ /* Device name */
+ result = Fcons (Qname,
+ Fcons (build_ext_string (ldm->printer_name,
+ Qmswindows_tstr),
+ result));
+ UNGCPRO;
+
+ global_free_2_maybe (pd.hDevNames, pd.hDevMode);
+ return result;
+ }
+}
+
+Lisp_Object
+mswindows_handle_print_setup_dialog_box (struct frame *f, Lisp_Object keys)
+{
+ Lisp_Object device = Qunbound, settings = Qunbound;
+
+ {
+ EXTERNAL_PROPERTY_LIST_LOOP_3 (key, value, keys)
+ {
+ if (EQ (key, Q_device))
+ {
+ device = wrap_device (decode_device (value));
+ CHECK_MSPRINTER_DEVICE (device);
+ }
+ else if (EQ (key, Q_printer_settings))
+ {
+ CHECK_DEVMODE (value);
+ settings = value;
+ }
+ else
+ syntax_error ("Unrecognized print-dialog keyword", key);
+ }
+ }
+
+ if ((UNBOUNDP (device) && UNBOUNDP (settings)) ||
+ (!UNBOUNDP (device) && !UNBOUNDP (settings)))
+ syntax_error ("Exactly one of :device and :printer-settings must be given",
+ keys);
+
+ return print_dialog_worker (!UNBOUNDP (device) ? device : settings, 0);
+}
+
+Lisp_Object
+mswindows_handle_print_dialog_box (struct frame *f, Lisp_Object keys)
+{
+ Lisp_Object device = Qunbound, settings = Qunbound;
+
+ {
+ EXTERNAL_PROPERTY_LIST_LOOP_3 (key, value, keys)
+ {
+ if (EQ (key, Q_device))
+ {
+ device = wrap_device (decode_device (value));
+ CHECK_MSPRINTER_DEVICE (device);
+ }
+ else if (EQ (key, Q_printer_settings))
+ {
+ CHECK_DEVMODE (value);
+ settings = value;
+ }
+ else
+ syntax_error ("Unrecognized print-dialog keyword", key);
+ }
+ }
+
+ if ((UNBOUNDP (device) && UNBOUNDP (settings)) ||
+ (!UNBOUNDP (device) && !UNBOUNDP (settings)))
+ syntax_error ("Exactly one of :device and :printer-settings must be given",
+ keys);
+
+ return print_dialog_worker (!UNBOUNDP (device) ? device : settings, 1);
+}
+
+static int
+plist_get_margin (Lisp_Object plist, Lisp_Object prop)
+{
+ Lisp_Object val = Fplist_get (plist, prop, make_int (1440));
+ if (!INTP (val))
+ invalid_argument ("Margin value must be an integer", val);
+
+ return MulDiv (XINT (val), 100, 144);
+}
+
+static Lisp_Object
+plist_set_margin (Lisp_Object plist, Lisp_Object prop, int margin, int mm_p)
+{
+ Lisp_Object val = make_int (MulDiv (margin, 144, mm_p ? 2450 : 100));
+ return Fcons (prop, Fcons (val, plist));
+}
+
+Lisp_Object
+mswindows_handle_page_setup_dialog_box (struct frame *f, Lisp_Object keys)
+{
+ Lisp_Object device = Qunbound, settings = Qunbound;
+ Lisp_Object plist = Qnil;
+
+ {
+ EXTERNAL_PROPERTY_LIST_LOOP_3 (key, value, keys)
+ {
+ if (EQ (key, Q_device))
+ {
+ device = wrap_device (decode_device (value));
+ CHECK_MSPRINTER_DEVICE (device);
+ }
+ else if (EQ (key, Q_printer_settings))
+ {
+ CHECK_DEVMODE (value);
+ settings = value;
+ }
+ else if (EQ (key, Q_properties))
+ {
+ CHECK_LIST (value);
+ plist = value;
+ }
+ else
+ syntax_error ("Unrecognized page-setup dialog keyword", key);
+ }
+ }
+
+ if ((UNBOUNDP (device) && UNBOUNDP (settings)) ||
+ (!UNBOUNDP (device) && !UNBOUNDP (settings)))
+ syntax_error ("Exactly one of :device and :printer-settings must be given",
+ keys);
+
+ if (UNBOUNDP (device))
+ device = settings;
+
+ {
+ Lisp_Devmode *ldm = decode_devmode (device);
+ PAGESETUPDLG pd;
+
+ memset (&pd, 0, sizeof (pd));
+ pd.lStructSize = sizeof (pd);
+ pd.hwndOwner = mswindows_get_selected_frame_hwnd ();
+ pd.Flags = PSD_MARGINS;
+ pd.rtMargin.left = plist_get_margin (plist, Qleft_margin);
+ pd.rtMargin.top = plist_get_margin (plist, Qtop_margin);
+ pd.rtMargin.right = plist_get_margin (plist, Qright_margin);
+ pd.rtMargin.bottom = plist_get_margin (plist, Qbottom_margin);
+ pd.hDevMode = devmode_to_hglobal (ldm);
+
+ if (!PageSetupDlg (&pd))
+ {
+ global_free_2_maybe (pd.hDevNames, pd.hDevMode);
+ return Qnil;
+ }
+
+ if (pd.hDevMode)
+ handle_devmode_changes (ldm, pd.hDevNames, pd.hDevMode);
+
+ /* Finally, build the resulting plist */
+ {
+ Lisp_Object result = Qnil;
+ int mm_p = pd.Flags & PSD_INHUNDREDTHSOFMILLIMETERS;
+ result = plist_set_margin (result, Qbottom_margin, pd.rtMargin.bottom,
+ mm_p);
+ result = plist_set_margin (result, Qright_margin, pd.rtMargin.right,
+ mm_p);
+ result = plist_set_margin (result, Qtop_margin, pd.rtMargin.top, mm_p);
+ result = plist_set_margin (result, Qleft_margin, pd.rtMargin.left, mm_p);
+ return result;
+ }
+ }
+}
+
+DEFUN ("msprinter-get-settings", Fmsprinter_get_settings, 1, 1, 0, /*
+Return the settings object currently used by DEVICE.
+The object returned is not a copy, but rather a pointer to the
+original one. Use `msprinter-settings-copy' to create a copy of it.
+*/
+ (device))
+{
+ struct device *d = decode_device (device);
+ XSETDEVICE (device, d);
+ CHECK_MSPRINTER_DEVICE (device);
+ return DEVICE_MSPRINTER_DEVMODE (d);
+}
+
+DEFUN ("msprinter-select-settings", Fmsprinter_select_settings, 2, 2, 0, /*
+Select SETTINGS object into a DEVICE.
+The settings from the settings object are immediately applied to the
+printer, possibly changing even the target printer itself, and all
+future changes are applied synchronously to the printer device and the
+selected printer object, until a different settings object is selected
+into the same printer.
+
+A settings object can be selected to no more than one printer at a time.
+
+If the supplied settings object is not specialized, it is specialized
+for the printer immediately upon selection. The object can be
+despecialized after it is unselected by calling the function
+`msprinter-settings-despecialize'.
+
+Return value is the previously selected settings object.
+*/
+ (device, settings))
+{
+ Lisp_Devmode *ldm;
+ struct device *d = decode_device (device);
+
+ struct gcpro gcpro1;
+ GCPRO1 (settings);
+
+ XSETDEVICE (device, d);
+ CHECK_MSPRINTER_DEVICE (device);
+ CHECK_DEVMODE (settings);
+ ldm = XDEVMODE (settings);
+
+ if (!NILP (ldm->device))
+ invalid_operation ("The object is currently selected into a device",
+ settings);
+
+ /* If the object being selected is de-specialized, then its
+ size is perhaps not enough to receive the new devmode. We can ask
+ for printer's devmode size here, because despecialized settings
+ cannot force switching to a different printer, as they supply no
+ printer name at all. */
+ if (ldm->printer_name == NULL)
+ {
+ size_t dm_size =
+ DocumentProperties (NULL, DEVICE_MSPRINTER_HPRINTER(d),
+ DEVICE_MSPRINTER_NAME(d), NULL, NULL, 0);
+ if (dm_size <= 0)
+ invalid_operation ("Unable to specialize settings, printer error",
+ device);
+
+ assert (XDEVMODE_SIZE (ldm) <= dm_size);
+ ldm->devmode = xrealloc (ldm->devmode, dm_size);
+ }
+
+ /* If we bail out on signal here, no damage is done, except that
+ the storage for the DEVMODE structure might be reallocated to
+ hold a larger one - not a big deal */
+ if (!sync_printer_with_devmode (d, ldm->devmode, ldm->devmode,
+ ldm->printer_name))
+ error ("Printer device initialization I/O error, device deleted.");
+
+ if (ldm->printer_name == NULL)
+ ldm->printer_name = xstrdup (DEVICE_MSPRINTER_NAME(d));
+
+ {
+ Lisp_Object old_mode = DEVICE_MSPRINTER_DEVMODE (d);
+ ldm->device = device;
+ XDEVMODE (old_mode)->device = Qnil;
+ DEVICE_MSPRINTER_DEVMODE (d) = settings;
+ UNGCPRO;
+ return old_mode;
+ }
+}
+
+DEFUN ("msprinter-apply-settings", Fmsprinter_apply_settings, 2, 2, 0, /*
+Apply settings from a SETTINGS object to a 'msprinter DEVICE.
+The settings from the settings object are immediately applied to the
+printer, possibly changing even the target printer itself. The SETTING
+object is not modified, unlike `msprinter-select-settings', and the
+supplied object is not changed. The changes are immediately recorded
+into the settings object which is currently selected into the printer
+device.
+
+Return value is the currently selected settings object.
+*/
+ (device, settings))
+{
+ Lisp_Devmode *ldm_current, *ldm_new;
+ struct device *d = decode_device (device);
+
+ struct gcpro gcpro1;
+ GCPRO1 (settings);
+
+ XSETDEVICE (device, d);
+ CHECK_MSPRINTER_DEVICE (device);
+ CHECK_DEVMODE (settings);
+ ldm_new = XDEVMODE (settings);
+ ldm_current = XDEVMODE (DEVICE_MSPRINTER_DEVMODE (d));
+
+ /* If the supplied devmode is not specialized, then the current
+ devmode size will always be sufficient, as the printer does
+ not change. If it is specialized, we must reallocate the current
+ devmode storage to match with the supplied one, as it has the right
+ size for the new printer, if it is going to change. The correct
+ way is to use the largest of the two though, to keep the old
+ contents unchanged in case of preliminary exit.
+ */
+ if (ldm_new->printer_name)
+ ldm_current->devmode =
+ (DEVMODE*) xrealloc (ldm_current->devmode,
+ max (XDEVMODE_SIZE (ldm_new),
+ XDEVMODE_SIZE (ldm_current)));
+
+ if (!sync_printer_with_devmode (d, ldm_new->devmode,
+ ldm_current->devmode,
+ ldm_new->printer_name))
+ error ("Printer device initialization I/O error, device deleted.");
+
+ if (ldm_new->printer_name != NULL)
+ {
+ xfree (ldm_current->printer_name);
+ ldm_current->printer_name = xstrdup (ldm_new->printer_name);
+ }
+
+ UNGCPRO;
+ return DEVICE_MSPRINTER_DEVMODE (d);
+}
+\f
+/************************************************************************/
+/* devmode */
+/************************************************************************/
+
+static void
+print_devmode (Lisp_Object obj, Lisp_Object printcharfun,
+ int escapeflag)
+{
+ char buf[100];
+ Lisp_Devmode *dm = XDEVMODE (obj);
+ if (print_readably)
+ error ("printing unreadable object #<msprinter-settings 0x%x>",
+ dm->header.uid);
+ write_c_string ("#<msprinter-settings", printcharfun);
+ if (dm->printer_name)
+ {
+ write_c_string (" for \"", printcharfun);
+ write_c_string (dm->printer_name, printcharfun);
+ write_c_string ("\"", printcharfun);
+ }
+ if (!NILP (dm->device))
+ {
+ write_c_string (" (currently on ", printcharfun);
+ print_internal (dm->device, printcharfun, 0);
+ write_c_string (")", printcharfun);
+ }
+ sprintf (buf, " 0x%x>", dm->header.uid);
+ write_c_string (buf, printcharfun);
+}
+
+static void
+finalize_devmode (void *header, int for_disksave)
+{
+ Lisp_Devmode *dm = (Lisp_Devmode *) header;
+
+ if (for_disksave)
+ {
+ Lisp_Object devmode;
+ XSETDEVMODE (devmode, dm);
+ invalid_operation
+ ("Cannot dump XEmacs containing an msprinter-settings object",
+ devmode);
+ }
+
+ assert (NILP (dm->device));
+
+ if (dm->printer_name)
+ xfree (dm->printer_name);
+}
+
+static int
+equal_devmode (Lisp_Object obj1, Lisp_Object obj2, int depth)
+{
+ Lisp_Devmode *dm1 = XDEVMODE (obj1);
+ Lisp_Devmode *dm2 = XDEVMODE (obj2);
+
+ if ((dm1->devmode != NULL) != (dm1->devmode != NULL))
+ return 0;
+ if (dm1->devmode == NULL)
+ return 1;
+ if (memcmp (dm1->devmode, dm2->devmode, XDEVMODE_SIZE (dm1)) != 0)
+ return 0;
+ if (dm1->printer_name == NULL || dm2->printer_name == NULL)
+ return 1;
+ return stricmp (dm1->printer_name, dm2->printer_name) == 0;
+}
+
+static unsigned long
+hash_devmode (Lisp_Object obj, int depth)
+{
+ Lisp_Devmode *dm = XDEVMODE (obj);
+
+ return HASH3 (XDEVMODE_SIZE (dm),
+ dm->devmode ? memory_hash (dm->devmode, XDEVMODE_SIZE (dm))
+ : 0,
+ dm->printer_name ? string_hash (dm->printer_name) : 0);
+}
+
+DEFINE_LRECORD_IMPLEMENTATION ("msprinter-settings", devmode,
+ 0/*mark*/, print_devmode, finalize_devmode,
+ equal_devmode, hash_devmode, 0/*description*/,
+ Lisp_Devmode);
+static Lisp_Object
+allocate_devmode (DEVMODE* src_devmode, int do_copy,
+ char* src_name, struct device *d)
+{
+ Lisp_Devmode *dm;
+ Lisp_Object ob;
+
+ dm = alloc_lcrecord_type (Lisp_Devmode, &lrecord_devmode);
+
+ if (d)
+ XSETDEVICE (dm->device, d);
+ else
+ dm->device = Qnil;
+
+ dm->printer_name = src_name ? xstrdup (src_name) : NULL;
+
+ if (src_devmode != NULL && do_copy)
+ {
+ dm->devmode = (DEVMODE*) xmalloc (DEVMODE_SIZE (src_devmode));
+ memcpy (dm->devmode, src_devmode, DEVMODE_SIZE (src_devmode));
+ }
+ else
+ {
+ dm->devmode = src_devmode;
+ }
+
+ XSETDEVMODE (ob, dm);
+ return ob;
+}
+
+DEFUN ("msprinter-settings-copy", Fmsprinter_settings_copy, 1, 1, 0, /*
+Create and returns an exact copy of a printer settings object.
+*/
+ (settings))
+{
+ Lisp_Devmode *dm;
+
+ CHECK_DEVMODE (settings);
+ dm = XDEVMODE (settings);
+
+ return allocate_devmode (dm->devmode, 1, dm->printer_name, NULL);
+}
+
+DEFUN ("msprinter-settings-despecialize", Fmsprinter_settings_despecialize, 1, 1, 0, /*
+Erase printer-specific settings from a printer settings object.
+*/
+ (settings))
+{
+ Lisp_Devmode *ldm;
+ DEVMODE *dm;
+
+ CHECK_DEVMODE (settings);
+ ldm = XDEVMODE (settings);
+
+ if (!NILP (ldm->device))
+ invalid_operation ("The object is currently selected into a device",
+ settings);
+
+ dm = ldm->devmode;
+
+ /* #### TODO. Either remove references to device specific bins,
+ paper sizes etc, or signal an error of they are present. */
+
+ dm->dmDriverExtra = 0;
+ dm->dmDeviceName[0] = '\0';
+
+ if (ldm->printer_name)
+ xfree (ldm->printer_name);
+
+ return Qnil;
+}
+
+DEFUN ("mswindows-get-default-printer", Fmswindows_get_default_printer, 0, 0, 0, /*
+Return name of the default printer, as string, on nil if there is no default.
+*/
+ ())
+{
+ return msprinter_default_printer ();
+}
+
+static void
+signal_enum_printer_error (void)
+{
+ invalid_operation ("Error enumerating printers", make_int (GetLastError ()));
+}
+
+DEFUN ("mswindows-printer-list", Fmswindows_printer_list, 0, 0, 0, /*
+Return a list of string names of installed printers.
+If there is a default printer, it is returned as the first element of
+the list. If there is no default printer, the first element of the
+list will be nil. The rest of elements are guaranteed to have string
+values. Return value is nil if there are no printers installed.
+*/
+ ())
+{
+ int have_nt, ok;
+ BYTE *data_buf, dummy_byte;
+ size_t enum_entry_size;
+ DWORD enum_flags, enum_level, bytes_needed, num_printers;
+ struct gcpro gcpro1, gcpro2;
+ Lisp_Object result = Qnil, def_printer = Qnil;
+
+ /* Determine OS flavor, to use the fastest enumeration method available */
+ have_nt = !mswindows_windows9x_p ();
+ enum_flags = PRINTER_ENUM_LOCAL | (have_nt ? PRINTER_ENUM_CONNECTIONS : 0);
+ enum_level = have_nt ? 4 : 5;
+ enum_entry_size = have_nt ? sizeof (PRINTER_INFO_4) : sizeof (PRINTER_INFO_5);
+
+ /* Allocate memory for printer enum structure */
+ ok = EnumPrinters (enum_flags, NULL, enum_level, &dummy_byte, 1,
+ &bytes_needed, &num_printers);
+ if (ok)
+ /* No printers, if just 1 byte is enough */
+ return Qnil;
+
+ if (GetLastError () != ERROR_INSUFFICIENT_BUFFER)
+ signal_enum_printer_error ();
+
+ data_buf = alloca (bytes_needed);
+ ok = EnumPrinters (enum_flags, NULL, enum_level, data_buf, bytes_needed,
+ &bytes_needed, &num_printers);
+ if (!ok)
+ signal_enum_printer_error ();
+
+ if (num_printers == 0)
+ /* Strange but... */
+ return Qnil;
+
+ GCPRO2 (result, def_printer);
+
+ while (num_printers--)
+ {
+ LPCTSTR printer_name;
+ if (have_nt)
+ {
+ PRINTER_INFO_4 *info = (PRINTER_INFO_4*) data_buf;
+ printer_name = info->pPrinterName;
+ }
+ else
+ {
+ PRINTER_INFO_5 *info = (PRINTER_INFO_5*) data_buf;
+ printer_name = info->pPrinterName;
+ }
+ data_buf += enum_entry_size;
+
+ result = Fcons (build_ext_string (printer_name, Qmswindows_tstr),
+ result);
+ }
+
+ def_printer = msprinter_default_printer ();
+ result = Fdelete (def_printer, result);
+ result = Fcons (def_printer, result);
+
+ RETURN_UNGCPRO (result);