+
+ 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))
+ signal_simple_error ("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)
+ signal_simple_error ("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 stirage 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 cuttent
+ 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);
+ }
+
+ 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);
+ signal_simple_error (
+ "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))
+ signal_simple_error ("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;