view dwt/printing/PrintDialog.d @ 156:969e7de37c3d default tip

Fixes to get dwt to work with dmd and ldc
author Jacob Carlborg <doob@me.com>
date Wed, 08 Jul 2009 21:56:44 +0200
parents 2e671fa40eec
children
line wrap: on
line source

/*******************************************************************************
 * Copyright (c) 2000, 2008 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     
 * Port to the D programming language:
 *     Jacob Carlborg <doob@me.com>
 *******************************************************************************/
module dwt.printing.PrintDialog;

import dwt.dwthelper.utils;

import dwt.DWT;
import dwt.DWTException;
import dwt.internal.cocoa.NSData;
import dwt.internal.cocoa.NSKeyedArchiver;
import dwt.internal.cocoa.NSMutableDictionary;
import dwt.internal.cocoa.NSNumber;
import dwt.internal.cocoa.NSPrintInfo;
import dwt.internal.cocoa.NSPrintPanel;
import dwt.internal.cocoa.NSPrinter;
import dwt.internal.cocoa.NSString;
import dwt.internal.cocoa.OS;
import dwt.widgets.Dialog;
import dwt.widgets.Shell;
import dwt.widgets.Widget;

import dwt.printing.Printer;
import dwt.printing.PrinterData;

/**
 * Instances of this class allow the user to select
 * a printer and various print-related parameters
 * prior to starting a print job.
 * <p>
 * IMPORTANT: This class is intended to be subclassed <em>only</em>
 * within the DWT implementation.
 * </p>
 *
 * @see <a href="http://www.eclipse.org/swt/snippets/#printing">Printing snippets</a>
 * @see <a href="http://www.eclipse.org/swt/examples.php">DWT Example: ControlExample, Dialog tab</a>
 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
 */
public class PrintDialog : Dialog {
    PrinterData printerData;
    int scope_ = PrinterData.ALL_PAGES;
    int startPage = 1, endPage = 1;
    bool printToFile = false;

/**
 * Constructs a new instance of this class given only its parent.
 *
 * @param parent a composite control which will be the parent of the new instance (cannot be null)
 *
 * @exception IllegalArgumentException <ul>
 *    <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
 * </ul>
 * @exception DWTException <ul>
 *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
 *    <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
 * </ul>
 *
 * @see DWT
 * @see Widget#checkSubclass
 * @see Widget#getStyle
 */
public this (Shell parent) {    
    this (parent, DWT.PRIMARY_MODAL);
}

/**
 * Constructs a new instance of this class given its parent
 * and a style value describing its behavior and appearance.
 * <p>
 * The style value is either one of the style constants defined in
 * class <code>DWT</code> which is applicable to instances of this
 * class, or must be built by <em>bitwise OR</em>'ing together 
 * (that is, using the <code>int</code> "|" operator) two or more
 * of those <code>DWT</code> style constants. The class description
 * lists the style constants that are applicable to the class.
 * Style bits are also inherited from superclasses.
 * </p>
 *
 * @param parent a composite control which will be the parent of the new instance (cannot be null)
 * @param style the style of control to construct
 *
 * @exception IllegalArgumentException <ul>
 *    <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
 * </ul>
 * @exception DWTException <ul>
 *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
 *    <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
 * </ul>
 *
 * @see DWT
 * @see Widget#checkSubclass
 * @see Widget#getStyle
 */
public this (Shell parent, int style) {    
    super (parent, style);
    checkSubclass ();
}

/**
 * Sets the printer data that will be used when the dialog
 * is opened.
 * 
 * @param data the data that will be used when the dialog is opened
 * 
 * @since 3.4
 */
public void setPrinterData(PrinterData data) {
    this.printerData = data;
}

/**
 * Returns the printer data that will be used when the dialog
 * is opened.
 * 
 * @return the data that will be used when the dialog is opened
 * 
 * @since 3.4
 */
public PrinterData getPrinterData() {
    return printerData;
}

/**
 * Makes the receiver visible and brings it to the front
 * of the display.
 *
 * @return a printer data object describing the desired print job parameters
 *
 * @exception DWTException <ul>
 *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
 *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
 * </ul>
 */
public PrinterData open() {
    PrinterData data = null;
    NSPrintPanel panel = NSPrintPanel.printPanel();
    NSPrintInfo printInfo = new NSPrintInfo(NSPrintInfo.sharedPrintInfo().copy());
    NSMutableDictionary dict = printInfo.dictionary();  
    if (printToFile) {
        dict.setValue(OS.NSPrintSaveJob, OS.NSPrintJobDisposition);
    }
    //TODO - setting range not work. why?
    dict.setValue(NSNumber.numberWithBool(scope_ is PrinterData.ALL_PAGES), OS.NSPrintAllPages);
    if (scope_ is PrinterData.PAGE_RANGE) {
        dict.setValue(NSNumber.numberWithInt(startPage), OS.NSPrintFirstPage);
        dict.setValue(NSNumber.numberWithInt(endPage), OS.NSPrintLastPage);
    }
    panel.setOptions(OS.NSPrintPanelShowsPageSetupAccessory | panel.options());
    if (panel.runModalWithPrintInfo(printInfo) !is OS.NSCancelButton) {
        NSPrinter printer = printInfo.printer();
        NSString str = printer.name();
        data = new PrinterData(Printer.DRIVER, str.getString());
        data.printToFile = printInfo.jobDisposition().isEqual(OS.NSPrintSaveJob);
        if (data.printToFile) {
            NSString filename = new NSString(dict.objectForKey(OS.NSPrintSavePath));
            data.fileName = filename.getString();
        }
        data.scope_ = (new NSNumber(dict.objectForKey(OS.NSPrintAllPages))).intValue() !is 0 ? PrinterData.ALL_PAGES : PrinterData.PAGE_RANGE;
        if (data.scope_ is PrinterData.PAGE_RANGE) {
            data.startPage = (new NSNumber(dict.objectForKey(OS.NSPrintFirstPage))).intValue();
            data.endPage = (new NSNumber(dict.objectForKey(OS.NSPrintLastPage))).intValue();
        }
        data.collate = (new NSNumber(dict.objectForKey(OS.NSPrintMustCollate))).intValue() !is 0;
        data.copyCount = (new NSNumber(dict.objectForKey(OS.NSPrintCopies))).intValue();
        NSData nsData = NSKeyedArchiver.archivedDataWithRootObject(printInfo);
        data.otherData = new byte[nsData.length()];
        OS.memmove(&data.otherData, nsData.bytes(), data.otherData.length);

        printToFile = data.printToFile;
        scope_ = data.scope_;
        startPage = data.startPage;
        endPage = data.endPage;
    }
    printInfo.release();
    return data;
}

/**
 * Returns the print job scope that the user selected
 * before pressing OK in the dialog. This will be one
 * of the following values:
 * <dl>
 * <dt><code>PrinterData.ALL_PAGES</code></dt>
 * <dd>Print all pages in the current document</dd>
 * <dt><code>PrinterData.PAGE_RANGE</code></dt>
 * <dd>Print the range of pages specified by startPage and endPage</dd>
 * <dt><code>PrinterData.SELECTION</code></dt>
 * <dd>Print the current selection</dd>
 * </dl>
 *
 * @return the scope setting that the user selected
 */
public int getScope() {
    return scope_;
}

/**
 * Sets the scope of the print job. The user will see this
 * setting when the dialog is opened. This can have one of
 * the following values:
 * <dl>
 * <dt><code>PrinterData.ALL_PAGES</code></dt>
 * <dd>Print all pages in the current document</dd>
 * <dt><code>PrinterData.PAGE_RANGE</code></dt>
 * <dd>Print the range of pages specified by startPage and endPage</dd>
 * <dt><code>PrinterData.SELECTION</code></dt>
 * <dd>Print the current selection</dd>
 * </dl>
 *
 * @param scope the scope setting when the dialog is opened
 */
public void setScope(int scope_) {
    this.scope_ = scope_;
}

/**
 * Returns the start page setting that the user selected
 * before pressing OK in the dialog.
 * <p>
 * This value can be from 1 to the maximum number of pages for the platform.
 * Note that it is only valid if the scope is <code>PrinterData.PAGE_RANGE</code>.
 * </p>
 *
 * @return the start page setting that the user selected
 */
public int getStartPage() {
    return startPage;
}

/**
 * Sets the start page that the user will see when the dialog
 * is opened.
 * <p>
 * This value can be from 1 to the maximum number of pages for the platform.
 * Note that it is only valid if the scope is <code>PrinterData.PAGE_RANGE</code>.
 * </p>
 * 
 * @param startPage the startPage setting when the dialog is opened
 */
public void setStartPage(int startPage) {
    this.startPage = startPage;
}

/**
 * Returns the end page setting that the user selected
 * before pressing OK in the dialog.
 * <p>
 * This value can be from 1 to the maximum number of pages for the platform.
 * Note that it is only valid if the scope is <code>PrinterData.PAGE_RANGE</code>.
 * </p>
 *
 * @return the end page setting that the user selected
 */
public int getEndPage() {
    return endPage;
}

/**
 * Sets the end page that the user will see when the dialog
 * is opened.
 * <p>
 * This value can be from 1 to the maximum number of pages for the platform.
 * Note that it is only valid if the scope is <code>PrinterData.PAGE_RANGE</code>.
 * </p>
 * 
 * @param endPage the end page setting when the dialog is opened
 */
public void setEndPage(int endPage) {
    this.endPage = endPage;
}

/**
 * Returns the 'Print to file' setting that the user selected
 * before pressing OK in the dialog.
 *
 * @return the 'Print to file' setting that the user selected
 */
public bool getPrintToFile() {
    return printToFile;
}

/**
 * Sets the 'Print to file' setting that the user will see
 * when the dialog is opened.
 *
 * @param printToFile the 'Print to file' setting when the dialog is opened
 */
public void setPrintToFile(bool printToFile) {
    this.printToFile = printToFile;
}

protected void checkSubclass() {
    String name = this.classinfo.name;
    String validName = PrintDialog.classinfo.name;
    if (!validName.equals(name)) {
        DWT.error(DWT.ERROR_INVALID_SUBCLASS);
    }
}
}