+/*---------------------------------------------------------------------*/
+/*----- PRINTER FRAME -----*/
+/*---------------------------------------------------------------------*/
+
+void
+msprinter_start_page (struct frame *f)
+{
+ if (!FRAME_MSPRINTER_PAGE_STARTED (f))
+ {
+ FRAME_MSPRINTER_PAGE_STARTED (f) = 1;
+ StartPage (DEVICE_MSPRINTER_HDC (XDEVICE (FRAME_DEVICE (f))));
+ }
+}
+
+static void
+error_frame_unsizable (struct frame *f)
+{
+ Lisp_Object frame;
+ XSETFRAME (frame, f);
+ signal_simple_error ("Cannot resize frame (margins)"
+ " after print job has started.", frame);
+}
+
+static void
+maybe_error_if_job_active (struct frame *f)
+{
+ if (FRAME_MSPRINTER_JOB_STARTED (f))
+ error_frame_unsizable (f);
+}
+
+static void
+msprinter_init_frame_1 (struct frame *f, Lisp_Object props)
+{
+ /* Make sure this is the only frame on device. Windows printer can
+ handle only one job at a time. */
+ if (!NILP (DEVICE_FRAME_LIST (XDEVICE (FRAME_DEVICE (f)))))
+ error ("Only one frame (print job) at a time is allowed on "
+ "this printer device.");
+
+ f->frame_data = xnew_and_zero (struct msprinter_frame);
+
+ /* Default margin size is 1" = 1440 twips */
+ FRAME_MSPRINTER_TOP_MARGIN(f) = 1440;
+ FRAME_MSPRINTER_BOTTOM_MARGIN(f) = 1440;
+ FRAME_MSPRINTER_LEFT_MARGIN(f) = 1440;
+ FRAME_MSPRINTER_RIGHT_MARGIN(f) = 1440;
+
+ /* Negative for "uinspecified" */
+ FRAME_MSPRINTER_CHARWIDTH(f) = -1;
+ FRAME_MSPRINTER_CHARHEIGHT(f) = -1;
+
+ /* nil is for "system default" for these properties. */
+ FRAME_MSPRINTER_ORIENTATION(f) = Qnil;
+ FRAME_MSPRINTER_DUPLEX(f) = Qnil;
+}
+
+static void
+msprinter_init_frame_3 (struct frame *f)
+{
+ DOCINFO di;
+ struct device *device = XDEVICE (FRAME_DEVICE (f));
+ HDC hdc;
+ int frame_left, frame_top, frame_width, frame_height;
+
+ /* Change printer parameters */
+ {
+ DEVMODE* devmode = msprinter_get_devmode_copy (device);
+ devmode->dmFields = 0;
+
+ if (!NILP (FRAME_MSPRINTER_ORIENTATION(f)))
+ {
+ devmode->dmFields = DM_ORIENTATION;
+ if (EQ (FRAME_MSPRINTER_ORIENTATION(f), Qportrait))
+ devmode->dmOrientation = DMORIENT_PORTRAIT;
+ else if (EQ (FRAME_MSPRINTER_ORIENTATION(f), Qlandscape))
+ devmode->dmOrientation = DMORIENT_LANDSCAPE;
+ else
+ abort();
+ }
+
+ if (!NILP (FRAME_MSPRINTER_DUPLEX(f)))
+ {
+ devmode->dmFields = DM_DUPLEX;
+ if (EQ (FRAME_MSPRINTER_DUPLEX(f), Qnone))
+ devmode->dmDuplex = DMDUP_SIMPLEX;
+ if (EQ (FRAME_MSPRINTER_DUPLEX(f), Qvertical))
+ devmode->dmDuplex = DMDUP_VERTICAL;
+ if (EQ (FRAME_MSPRINTER_DUPLEX(f), Qhorizontal))
+ devmode->dmDuplex = DMDUP_HORIZONTAL;
+ else
+ abort();
+ }
+
+ assert (!FRAME_MSPRINTER_PAGE_STARTED (f));
+ msprinter_apply_devmode (device, devmode);
+ }
+
+ /* DC might be recreated in msprinter_apply_devmode,
+ so do not initialize until now */
+ hdc = DEVICE_MSPRINTER_HDC (device);
+
+ /* Compute geometry properties */
+ frame_left = (MulDiv (GetDeviceCaps (hdc, LOGPIXELSX),
+ FRAME_MSPRINTER_LEFT_MARGIN(f), 1440)
+ - GetDeviceCaps (hdc, PHYSICALOFFSETX));
+
+ if (FRAME_MSPRINTER_CHARWIDTH(f) > 0)
+ {
+ char_to_real_pixel_size (f, FRAME_MSPRINTER_CHARWIDTH(f), 0,
+ &frame_width, NULL);
+ FRAME_MSPRINTER_RIGHT_MARGIN(f) =
+ MulDiv (GetDeviceCaps (hdc, PHYSICALWIDTH)
+ - (frame_left + frame_width), 1440,
+ GetDeviceCaps (hdc, LOGPIXELSX));
+ }
+ else
+ frame_width = (GetDeviceCaps (hdc, PHYSICALWIDTH)
+ - frame_left
+ - MulDiv (GetDeviceCaps (hdc, LOGPIXELSX),
+ FRAME_MSPRINTER_RIGHT_MARGIN(f), 1440));
+
+ frame_top = (MulDiv (GetDeviceCaps (hdc, LOGPIXELSY),
+ FRAME_MSPRINTER_TOP_MARGIN(f), 1440)
+ - GetDeviceCaps (hdc, PHYSICALOFFSETY));
+
+ if (FRAME_MSPRINTER_CHARHEIGHT(f) > 0)
+ {
+ char_to_real_pixel_size (f, 0, FRAME_MSPRINTER_CHARHEIGHT(f),
+ NULL, &frame_height);
+
+ FRAME_MSPRINTER_BOTTOM_MARGIN(f) =
+ MulDiv (GetDeviceCaps (hdc, PHYSICALHEIGHT)
+ - (frame_top + frame_height), 1440,
+ GetDeviceCaps (hdc, LOGPIXELSY));
+ }
+ else
+ frame_height = (GetDeviceCaps (hdc, PHYSICALHEIGHT)
+ - frame_top
+ - MulDiv (GetDeviceCaps (hdc, LOGPIXELSY),
+ FRAME_MSPRINTER_BOTTOM_MARGIN(f), 1440));
+
+ /* Geometry sanity checks */
+ if (!frame_pixsize_valid_p (f, frame_width, frame_height))
+ error ("Area inside print margins has shrunk to naught.");
+
+ if (frame_left < 0
+ || frame_top < 0
+ || frame_left + frame_width > GetDeviceCaps (hdc, HORZRES)
+ || frame_top + frame_height > GetDeviceCaps (hdc, VERTRES))
+ error ("Print area is ouside of the printer's hardware printable area.");
+
+ /* Apply XEmacs frame geometry and layout windows */
+ {
+ int rows, columns;
+ FRAME_PIXWIDTH(f) = frame_width;
+ FRAME_PIXHEIGHT(f) = frame_height;
+ pixel_to_char_size (f, frame_width, frame_height, &columns, &rows);
+ change_frame_size (f, rows, columns, 0);
+ }
+
+ /* Apply DC geometry */
+ SetTextAlign (hdc, TA_BASELINE | TA_LEFT | TA_NOUPDATECP);
+ SetViewportOrgEx (hdc, frame_left, frame_top, NULL);
+ SetWindowOrgEx (hdc, 0, 0, NULL);
+
+ /* Start print job */
+ di.cbSize = sizeof (di);
+ di.lpszDocName = (STRINGP(f->name)
+ ? (char*) XSTRING_DATA(f->name)
+ : "XEmacs print document");
+ di.lpszOutput = NULL;
+ di.lpszDatatype = NULL;
+ di.fwType = 0;
+
+ if (StartDoc (hdc, &di) <= 0)
+ error ("Cannot start print job");
+
+ /* Finish frame setup */
+ FRAME_MSPRINTER_JOB_STARTED (f) = 1;
+ FRAME_VISIBLE_P(f) = 0;
+}
+
+static void
+msprinter_mark_frame (struct frame *f)
+{
+ /* NOTE: These need not be marked as long as we allow only c-defined
+ symbols for their values. Although, marking these is safer than
+ expensive. [I know a proof to the theorem postulating that a
+ gator is longer than greener. Ask me. -- kkm] */
+ mark_object (FRAME_MSPRINTER_ORIENTATION (f));
+ mark_object (FRAME_MSPRINTER_DUPLEX (f));
+}
+
+static void
+msprinter_delete_frame (struct frame *f)
+{
+ if (f->frame_data)
+ {
+ HDC hdc = DEVICE_MSPRINTER_HDC (XDEVICE (FRAME_DEVICE (f)));
+ if (FRAME_MSPRINTER_PAGE_STARTED (f))
+ EndPage (hdc);
+ if (FRAME_MSPRINTER_JOB_STARTED (f))
+ EndDoc (hdc);
+ xfree (f->frame_data);
+ }
+
+ f->frame_data = 0;
+}
+
+static Lisp_Object
+msprinter_frame_property (struct frame *f, Lisp_Object property)
+{
+ if (EQ (Qleft_margin, property))
+ return make_int (FRAME_MSPRINTER_LEFT_MARGIN(f));
+ else if (EQ (Qtop_margin, property))
+ return make_int (FRAME_MSPRINTER_TOP_MARGIN(f));
+ if (EQ (Qright_margin, property))
+ return make_int (FRAME_MSPRINTER_RIGHT_MARGIN(f));
+ else if (EQ (Qbottom_margin, property))
+ return make_int (FRAME_MSPRINTER_BOTTOM_MARGIN(f));
+ else if (EQ (Qorientation, property))
+ return FRAME_MSPRINTER_ORIENTATION(f);
+ else if (EQ (Qduplex, property))
+ return FRAME_MSPRINTER_DUPLEX(f);
+ else
+ return Qunbound;
+}
+
+static int
+msprinter_internal_frame_property_p (struct frame *f, Lisp_Object property)
+{
+ return (EQ (Qleft_margin, property) || EQ (Qtop_margin, property) ||
+ EQ (Qright_margin, property) || EQ (Qbottom_margin, property) ||
+ EQ (Qorientation, property) || EQ (Qduplex, property));
+}
+
+static Lisp_Object
+msprinter_frame_properties (struct frame *f)
+{
+ Lisp_Object props = Qnil;
+ props = cons3 (Qorientation, FRAME_MSPRINTER_ORIENTATION(f), props);
+ props = cons3 (Qduplex, FRAME_MSPRINTER_DUPLEX(f), props);
+ props = cons3 (Qbottom_margin,
+ make_int (FRAME_MSPRINTER_BOTTOM_MARGIN(f)), props);
+ props = cons3 (Qright_margin,
+ make_int (FRAME_MSPRINTER_RIGHT_MARGIN(f)), props);
+ props = cons3 (Qtop_margin,
+ make_int (FRAME_MSPRINTER_TOP_MARGIN(f)), props);
+ props = cons3 (Qleft_margin,
+ make_int (FRAME_MSPRINTER_LEFT_MARGIN(f)), props);
+ return props;
+}
+
+static void
+msprinter_set_frame_properties (struct frame *f, Lisp_Object plist)
+{
+ Lisp_Object tail;
+
+ /* Extract the properties from plist */
+ for (tail = plist; !NILP (tail); tail = Fcdr (Fcdr (tail)))
+ {
+ Lisp_Object prop = Fcar (tail);
+ Lisp_Object val = Fcar (Fcdr (tail));
+
+ if (SYMBOLP (prop))
+ {
+ if (EQ (prop, Qwidth))
+ {
+ maybe_error_if_job_active (f);
+ if (!NILP (val))
+ {
+ CHECK_NATNUM (val);
+ FRAME_MSPRINTER_CHARWIDTH(f) = XINT (val);
+ }
+ }
+ if (EQ (prop, Qheight))
+ {
+ maybe_error_if_job_active (f);
+ if (!NILP (val))
+ {
+ CHECK_NATNUM (val);
+ FRAME_MSPRINTER_CHARHEIGHT(f) = XINT (val);
+ }
+ }
+ else if (EQ (prop, Qleft_margin))
+ {
+ maybe_error_if_job_active (f);
+ CHECK_NATNUM (val);
+ FRAME_MSPRINTER_LEFT_MARGIN(f) = XINT (val);
+ }
+ else if (EQ (prop, Qtop_margin))
+ {
+ maybe_error_if_job_active (f);
+ CHECK_NATNUM (val);
+ FRAME_MSPRINTER_TOP_MARGIN(f) = XINT (val);
+ }
+ else if (EQ (prop, Qright_margin))
+ {
+ maybe_error_if_job_active (f);
+ CHECK_NATNUM (val);
+ FRAME_MSPRINTER_RIGHT_MARGIN(f) = XINT (val);
+ }
+ else if (EQ (prop, Qbottom_margin))
+ {
+ maybe_error_if_job_active (f);
+ CHECK_NATNUM (val);
+ FRAME_MSPRINTER_BOTTOM_MARGIN(f) = XINT (val);
+ }
+ else if (EQ (prop, Qorientation))
+ {
+ maybe_error_if_job_active (f);
+ CHECK_SYMBOL (val);
+ if (!NILP(val) &&
+ !EQ (val, Qportrait) &&
+ !EQ (val, Qlandscape))
+ signal_simple_error ("Page orientation can only be "
+ "'portrait or 'landscape", val);
+ FRAME_MSPRINTER_ORIENTATION(f) = val;
+ }
+ else if (EQ (prop, Qduplex))
+ {
+ maybe_error_if_job_active (f);
+ CHECK_SYMBOL (val);
+ if (!NILP(val) &&
+ !EQ (val, Qnone) &&
+ !EQ (val, Qvertical) &&
+ !EQ (val, Qhorizontal))
+ signal_simple_error ("Duplex can only be 'none, "
+ "'vertical or 'horizontal", val);
+ FRAME_MSPRINTER_DUPLEX(f) = val;
+ }
+ }
+ }
+}
+
+static void
+msprinter_set_frame_size (struct frame *f, int width, int height)
+{
+ /* We're absolutely unsizeable */
+ error_frame_unsizable (f);
+}
+
+static void
+msprinter_eject_page (struct frame *f)
+{
+ /* #### Should we eject empty pages? */
+ if (FRAME_MSPRINTER_PAGE_STARTED (f))
+ {
+ FRAME_MSPRINTER_PAGE_STARTED (f) = 0;
+ EndPage (DEVICE_MSPRINTER_HDC (XDEVICE (FRAME_DEVICE (f))));
+ }
+}
+
+\f