Search in sources :

Example 11 with PrintDialog

use of org.eclipse.swt.printing.PrintDialog in project eclipse.platform.text by eclipse.

the class TextViewer method print.

/**
 * {@inheritDoc}
 *
 * @since 3.4
 */
@Override
public void print(StyledTextPrintOptions options) {
    final Shell shell = fTextWidget.getShell();
    if (Printer.getPrinterList().length == 0) {
        // $NON-NLS-1$
        String title = JFaceTextMessages.getString("TextViewer.warning.noPrinter.title");
        // $NON-NLS-1$
        String msg = JFaceTextMessages.getString("TextViewer.warning.noPrinter.message");
        MessageDialog.openWarning(shell, title, msg);
        return;
    }
    final PrintDialog dialog = new PrintDialog(shell, SWT.PRIMARY_MODAL);
    dialog.setPrinterData(fgPrinterData);
    final PrinterData data = dialog.open();
    if (data != null) {
        final Printer printer = new Printer(data);
        final Runnable styledTextPrinter = fTextWidget.print(printer, options);
        Thread printingThread = new // $NON-NLS-1$
        Thread(// $NON-NLS-1$
        "Printing") {

            @Override
            public void run() {
                styledTextPrinter.run();
                printer.dispose();
            }
        };
        printingThread.start();
        /*
			 * FIXME:
			 * 	Should copy the printer data to avoid threading issues,
			 *	but this is currently not possible, see http://bugs.eclipse.org/297957
			 */
        fgPrinterData = data;
        fgPrinterData.startPage = 1;
        fgPrinterData.endPage = 1;
        fgPrinterData.scope = PrinterData.ALL_PAGES;
        fgPrinterData.copyCount = 1;
    }
}
Also used : Shell(org.eclipse.swt.widgets.Shell) PrintDialog(org.eclipse.swt.printing.PrintDialog) PrinterData(org.eclipse.swt.printing.PrinterData) Printer(org.eclipse.swt.printing.Printer)

Example 12 with PrintDialog

use of org.eclipse.swt.printing.PrintDialog in project eclipse.platform.swt by eclipse.

the class DialogTab method createControlWidgets.

/**
 * Creates the "Control" widget children.
 */
@Override
void createControlWidgets() {
    /* Create the combo */
    String[] strings = { ControlExample.getResourceString("ColorDialog"), ControlExample.getResourceString("DirectoryDialog"), ControlExample.getResourceString("FileDialog"), ControlExample.getResourceString("FontDialog"), ControlExample.getResourceString("PrintDialog"), ControlExample.getResourceString("MessageBox") };
    dialogCombo = new Combo(dialogStyleGroup, SWT.READ_ONLY);
    dialogCombo.setItems(strings);
    dialogCombo.setText(strings[0]);
    dialogCombo.setVisibleItemCount(strings.length);
    /* Create the create dialog button */
    createButton = new Button(dialogStyleGroup, SWT.NONE);
    createButton.setText(ControlExample.getResourceString("Create_Dialog"));
    createButton.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_CENTER));
    /* Create a group for the various dialog button style controls */
    Group buttonStyleGroup = new Group(controlGroup, SWT.NONE);
    buttonStyleGroup.setLayout(new GridLayout());
    buttonStyleGroup.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL));
    buttonStyleGroup.setText(ControlExample.getResourceString("Button_Styles"));
    /* Create the button style buttons */
    okButton = new Button(buttonStyleGroup, SWT.CHECK);
    okButton.setText("SWT.OK");
    cancelButton = new Button(buttonStyleGroup, SWT.CHECK);
    cancelButton.setText("SWT.CANCEL");
    yesButton = new Button(buttonStyleGroup, SWT.CHECK);
    yesButton.setText("SWT.YES");
    noButton = new Button(buttonStyleGroup, SWT.CHECK);
    noButton.setText("SWT.NO");
    retryButton = new Button(buttonStyleGroup, SWT.CHECK);
    retryButton.setText("SWT.RETRY");
    abortButton = new Button(buttonStyleGroup, SWT.CHECK);
    abortButton.setText("SWT.ABORT");
    ignoreButton = new Button(buttonStyleGroup, SWT.CHECK);
    ignoreButton.setText("SWT.IGNORE");
    /* Create a group for the icon style controls */
    Group iconStyleGroup = new Group(controlGroup, SWT.NONE);
    iconStyleGroup.setLayout(new GridLayout());
    iconStyleGroup.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL));
    iconStyleGroup.setText(ControlExample.getResourceString("Icon_Styles"));
    /* Create the icon style buttons */
    iconErrorButton = new Button(iconStyleGroup, SWT.RADIO);
    iconErrorButton.setText("SWT.ICON_ERROR");
    iconInformationButton = new Button(iconStyleGroup, SWT.RADIO);
    iconInformationButton.setText("SWT.ICON_INFORMATION");
    iconQuestionButton = new Button(iconStyleGroup, SWT.RADIO);
    iconQuestionButton.setText("SWT.ICON_QUESTION");
    iconWarningButton = new Button(iconStyleGroup, SWT.RADIO);
    iconWarningButton.setText("SWT.ICON_WARNING");
    iconWorkingButton = new Button(iconStyleGroup, SWT.RADIO);
    iconWorkingButton.setText("SWT.ICON_WORKING");
    noIconButton = new Button(iconStyleGroup, SWT.RADIO);
    noIconButton.setText(ControlExample.getResourceString("No_Icon"));
    /* Create a group for the modal style controls */
    Group modalStyleGroup = new Group(controlGroup, SWT.NONE);
    modalStyleGroup.setLayout(new GridLayout());
    modalStyleGroup.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL));
    modalStyleGroup.setText(ControlExample.getResourceString("Modal_Styles"));
    /* Create the modal style buttons */
    primaryModalButton = new Button(modalStyleGroup, SWT.RADIO);
    primaryModalButton.setText("SWT.PRIMARY_MODAL");
    applicationModalButton = new Button(modalStyleGroup, SWT.RADIO);
    applicationModalButton.setText("SWT.APPLICATION_MODAL");
    systemModalButton = new Button(modalStyleGroup, SWT.RADIO);
    systemModalButton.setText("SWT.SYSTEM_MODAL");
    /* Create a group for the file dialog style controls */
    Group fileDialogStyleGroup = new Group(controlGroup, SWT.NONE);
    fileDialogStyleGroup.setLayout(new GridLayout());
    fileDialogStyleGroup.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL));
    fileDialogStyleGroup.setText(ControlExample.getResourceString("File_Dialog_Styles"));
    /* Create the file dialog style buttons */
    openButton = new Button(fileDialogStyleGroup, SWT.RADIO);
    openButton.setText("SWT.OPEN");
    saveButton = new Button(fileDialogStyleGroup, SWT.RADIO);
    saveButton.setText("SWT.SAVE");
    multiButton = new Button(fileDialogStyleGroup, SWT.CHECK);
    multiButton.setText("SWT.MULTI");
    /* Create the orientation group */
    if (RTL_SUPPORT_ENABLE) {
        createOrientationGroup();
    }
    /* Create a group for other style and setting controls */
    Group otherGroup = new Group(controlGroup, SWT.NONE);
    otherGroup.setLayout(new GridLayout());
    otherGroup.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL));
    otherGroup.setText(ControlExample.getResourceString("Other"));
    /* Create the other style and setting controls */
    sheetButton = new Button(otherGroup, SWT.CHECK);
    sheetButton.setText("SWT.SHEET");
    usePreviousResultButton = new Button(otherGroup, SWT.CHECK);
    usePreviousResultButton.setText(ControlExample.getResourceString("Use_Previous_Result"));
    effectsVisibleButton = new Button(otherGroup, SWT.CHECK);
    effectsVisibleButton.setText("FontDialog.setEffectsVisible");
    /* Add the listeners */
    dialogCombo.addSelectionListener(widgetSelectedAdapter(event -> dialogSelected(event)));
    createButton.addSelectionListener(widgetSelectedAdapter(event -> createButtonSelected(event)));
    SelectionListener buttonStyleListener = widgetSelectedAdapter(event -> buttonStyleSelected(event));
    okButton.addSelectionListener(buttonStyleListener);
    cancelButton.addSelectionListener(buttonStyleListener);
    yesButton.addSelectionListener(buttonStyleListener);
    noButton.addSelectionListener(buttonStyleListener);
    retryButton.addSelectionListener(buttonStyleListener);
    abortButton.addSelectionListener(buttonStyleListener);
    ignoreButton.addSelectionListener(buttonStyleListener);
    /* Set default values for style buttons */
    okButton.setEnabled(false);
    cancelButton.setEnabled(false);
    yesButton.setEnabled(false);
    noButton.setEnabled(false);
    retryButton.setEnabled(false);
    abortButton.setEnabled(false);
    ignoreButton.setEnabled(false);
    iconErrorButton.setEnabled(false);
    iconInformationButton.setEnabled(false);
    iconQuestionButton.setEnabled(false);
    iconWarningButton.setEnabled(false);
    iconWorkingButton.setEnabled(false);
    noIconButton.setEnabled(false);
    saveButton.setEnabled(false);
    openButton.setEnabled(false);
    openButton.setSelection(true);
    multiButton.setEnabled(false);
    noIconButton.setSelection(true);
    effectsVisibleButton.setEnabled(false);
    effectsVisibleButton.setSelection(true);
}
Also used : Text(org.eclipse.swt.widgets.Text) Combo(org.eclipse.swt.widgets.Combo) Button(org.eclipse.swt.widgets.Button) PrinterData(org.eclipse.swt.printing.PrinterData) FileDialog(org.eclipse.swt.widgets.FileDialog) FontDialog(org.eclipse.swt.widgets.FontDialog) Group(org.eclipse.swt.widgets.Group) SelectionListener.widgetSelectedAdapter(org.eclipse.swt.events.SelectionListener.widgetSelectedAdapter) ColorDialog(org.eclipse.swt.widgets.ColorDialog) PrintDialog(org.eclipse.swt.printing.PrintDialog) SWT(org.eclipse.swt.SWT) FontData(org.eclipse.swt.graphics.FontData) DirectoryDialog(org.eclipse.swt.widgets.DirectoryDialog) MessageBox(org.eclipse.swt.widgets.MessageBox) RGB(org.eclipse.swt.graphics.RGB) SelectionEvent(org.eclipse.swt.events.SelectionEvent) GridData(org.eclipse.swt.layout.GridData) Widget(org.eclipse.swt.widgets.Widget) SelectionListener(org.eclipse.swt.events.SelectionListener) GridLayout(org.eclipse.swt.layout.GridLayout) Group(org.eclipse.swt.widgets.Group) GridLayout(org.eclipse.swt.layout.GridLayout) Button(org.eclipse.swt.widgets.Button) GridData(org.eclipse.swt.layout.GridData) Combo(org.eclipse.swt.widgets.Combo) SelectionListener(org.eclipse.swt.events.SelectionListener)

Example 13 with PrintDialog

use of org.eclipse.swt.printing.PrintDialog in project eclipse.platform.swt by eclipse.

the class Test_org_eclipse_swt_printing_PrintDialog method setUp.

@Override
@Before
public void setUp() {
    super.setUp();
    printDialog = new PrintDialog(shell, SWT.NONE);
    setDialog(printDialog);
}
Also used : PrintDialog(org.eclipse.swt.printing.PrintDialog) Before(org.junit.Before)

Example 14 with PrintDialog

use of org.eclipse.swt.printing.PrintDialog in project eclipse.platform.swt by eclipse.

the class ImageAnalyzer method menuPrint.

void menuPrint() {
    if (image == null)
        return;
    try {
        // Ask the user to specify the printer.
        PrintDialog dialog = new PrintDialog(shell, SWT.NONE);
        if (printerData != null)
            dialog.setPrinterData(printerData);
        printerData = dialog.open();
        if (printerData == null)
            return;
        Printer printer = new Printer(printerData);
        Point screenDPI = display.getDPI();
        Point printerDPI = printer.getDPI();
        int scaleFactor = printerDPI.x / screenDPI.x;
        Rectangle trim = printer.computeTrim(0, 0, 0, 0);
        if (printer.startJob(currentName)) {
            if (printer.startPage()) {
                GC gc = new GC(printer);
                int transparentPixel = imageData.transparentPixel;
                if (transparentPixel != -1 && !transparent) {
                    imageData.transparentPixel = -1;
                }
                Image printerImage = new Image(printer, imageData);
                gc.drawImage(printerImage, 0, 0, imageData.width, imageData.height, -trim.x, -trim.y, scaleFactor * imageData.width, scaleFactor * imageData.height);
                if (transparentPixel != -1 && !transparent) {
                    imageData.transparentPixel = transparentPixel;
                }
                printerImage.dispose();
                gc.dispose();
                printer.endPage();
            }
            printer.endJob();
        }
        printer.dispose();
    } catch (SWTError e) {
        MessageBox box = new MessageBox(shell, SWT.ICON_ERROR);
        box.setMessage(bundle.getString("Printing_error") + e.getMessage());
        box.open();
    }
}
Also used : SWTError(org.eclipse.swt.SWTError) PrintDialog(org.eclipse.swt.printing.PrintDialog) Rectangle(org.eclipse.swt.graphics.Rectangle) Point(org.eclipse.swt.graphics.Point) Printer(org.eclipse.swt.printing.Printer) GC(org.eclipse.swt.graphics.GC) Image(org.eclipse.swt.graphics.Image) Point(org.eclipse.swt.graphics.Point) MessageBox(org.eclipse.swt.widgets.MessageBox)

Example 15 with PrintDialog

use of org.eclipse.swt.printing.PrintDialog in project org.csstudio.display.builder by kasemir.

the class PrintAction method run.

/**
 * {@inheritDoc}
 */
@Override
public void run() {
    // Get snapshot. Disposed at end of printing
    final Image snapshot = media_pool.get(ImageConverter.convertToSWT(graph.getImage()));
    if (snapshot == null) {
        Logger.getLogger(getClass().getName()).fine("Cannot obtain image");
        return;
    }
    // Printer GUI
    final PrintDialog dlg = new PrintDialog(shell);
    PrinterData data = dlg.open();
    if (data == null) {
        Logger.getLogger(getClass().getName()).fine("Cannot obtain printer");
        snapshot.dispose();
        return;
    }
    // At least on Linux, access to SWT Printer must be on UI thread.
    // Printing in other thread can deadlock with UI thread.
    final Printer printer = new Printer(data);
    try {
        if (!printer.startJob("Data Browser")) {
            Logger.getLogger(getClass().getName()).fine("Cannot start print job");
            return;
        }
        try {
            // Printer page info
            final Rectangle area = printer.getClientArea();
            final Rectangle trim = printer.computeTrim(0, 0, 0, 0);
            final Point dpi = printer.getDPI();
            // Compute layout
            final Rectangle image_rect = snapshot.getBounds();
            // Leave one inch on each border.
            // (copied the computeTrim stuff from an SWT example.
            // Really no clue...)
            final int left_right = dpi.x + trim.x;
            final int top_bottom = dpi.y + trim.y;
            final int printed_width = area.width - 2 * left_right;
            // Try to scale height according to on-screen aspect ratio.
            final int max_height = area.height - 2 * top_bottom;
            final int printed_height = Math.min(max_height, image_rect.height * printed_width / image_rect.width);
            // Print one page
            printer.startPage();
            final GC gc = new GC(printer);
            gc.drawImage(snapshot, 0, 0, image_rect.width, image_rect.height, left_right, top_bottom, printed_width, printed_height);
            printer.endPage();
        } finally {
            printer.endJob();
        }
    } finally {
        printer.dispose();
        snapshot.dispose();
    }
}
Also used : PrintDialog(org.eclipse.swt.printing.PrintDialog) PrinterData(org.eclipse.swt.printing.PrinterData) Rectangle(org.eclipse.swt.graphics.Rectangle) Point(org.eclipse.swt.graphics.Point) Image(org.eclipse.swt.graphics.Image) Printer(org.eclipse.swt.printing.Printer) GC(org.eclipse.swt.graphics.GC) Point(org.eclipse.swt.graphics.Point)

Aggregations

PrintDialog (org.eclipse.swt.printing.PrintDialog)24 PrinterData (org.eclipse.swt.printing.PrinterData)21 Printer (org.eclipse.swt.printing.Printer)19 Point (org.eclipse.swt.graphics.Point)6 Shell (org.eclipse.swt.widgets.Shell)6 Insets (org.eclipse.draw2d.geometry.Insets)4 StyledTextPrintOptions (org.eclipse.swt.custom.StyledTextPrintOptions)4 GC (org.eclipse.swt.graphics.GC)4 Image (org.eclipse.swt.graphics.Image)4 Rectangle (org.eclipse.swt.graphics.Rectangle)4 IFigure (org.eclipse.draw2d.IFigure)3 PrintFigureOperation (org.eclipse.draw2d.PrintFigureOperation)3 GraphicalViewer (org.eclipse.gef.GraphicalViewer)3 MessageBox (org.eclipse.swt.widgets.MessageBox)3 PrintGraphicalViewerOperation (org.eclipse.gef.print.PrintGraphicalViewerOperation)2 IPreferenceStore (org.eclipse.jface.preference.IPreferenceStore)2 SelectionEvent (org.eclipse.swt.events.SelectionEvent)2 SelectionListener (org.eclipse.swt.events.SelectionListener)2 FontData (org.eclipse.swt.graphics.FontData)2 ImageLoader (org.eclipse.swt.graphics.ImageLoader)2