private void setPreviewScale(float scale) { preview.setFitVertical(false); preview.setFitHorizontal(false); preview.setScale(scale); rememberScrollingPosition(); updatePreviewSize(); restoreScrollingPosition(); }
/** * Executes the GridPrint example. * * @param args the command line arguments. */ public static void main(String[] args) { final Display display = new Display(); Shell shell = new Shell(display, SWT.SHELL_TRIM); shell.setText("GridPrintVerticalAlignmentExample.java"); shell.setLayout(new GridLayout()); shell.setSize(600, 800); final PrintJob job = new PrintJob("GridPrintVerticalAlignmentExample.java", createPrint()); Composite buttonPanel = new Composite(shell, SWT.NONE); buttonPanel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false)); buttonPanel.setLayout(new RowLayout(SWT.HORIZONTAL)); final PrintPreview preview = new PrintPreview(shell, SWT.BORDER); Button prev = new Button(buttonPanel, SWT.PUSH); prev.setText("<< Prev"); prev.addListener( SWT.Selection, new Listener() { public void handleEvent(Event event) { preview.setPageIndex(Math.max(preview.getPageIndex() - 1, 0)); } }); Button next = new Button(buttonPanel, SWT.PUSH); next.setText("Next >>"); next.addListener( SWT.Selection, new Listener() { public void handleEvent(Event event) { preview.setPageIndex(Math.min(preview.getPageIndex() + 1, preview.getPageCount() - 1)); } }); Button print = new Button(buttonPanel, SWT.PUSH); print.setText("Print"); print.addListener( SWT.Selection, new Listener() { public void handleEvent(Event event) { PaperClips.print(job, new PrinterData()); } }); preview.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); preview.setFitHorizontal(true); preview.setFitVertical(true); preview.setPrintJob(job); shell.open(); while (!shell.isDisposed()) if (!display.readAndDispatch()) display.sleep(); display.dispose(); }
public Control createControl(Composite parent) { Composite shell = new Composite(parent, SWT.None); final PrintJob job = new PrintJob("GridPrintVerticalAlignmentExample.java", createPrint()); shell.setLayout(new org.eclipse.swt.layout.GridLayout()); Composite buttonPanel = new Composite(shell, SWT.NONE); buttonPanel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false)); buttonPanel.setLayout(new RowLayout(SWT.HORIZONTAL)); final PrintPreview preview = new PrintPreview(shell, SWT.BORDER); Button prev = new Button(buttonPanel, SWT.PUSH); prev.setText("<< Prev"); prev.addListener( SWT.Selection, new Listener() { public void handleEvent(Event event) { preview.setPageIndex(Math.max(preview.getPageIndex() - 1, 0)); } }); Button next = new Button(buttonPanel, SWT.PUSH); next.setText("Next >>"); next.addListener( SWT.Selection, new Listener() { public void handleEvent(Event event) { preview.setPageIndex(Math.min(preview.getPageIndex() + 1, preview.getPageCount() - 1)); } }); Button print = new Button(buttonPanel, SWT.PUSH); print.setText("Print"); print.addListener( SWT.Selection, new Listener() { public void handleEvent(Event event) { PaperClips.print(job, new PrinterData()); } }); preview.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); preview.setFitHorizontal(true); preview.setFitVertical(true); preview.setPrintJob(job); return shell; }
private Control createScrollingPreview(Composite parent) { scroll = new ScrolledComposite(parent, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL); scroll.setExpandHorizontal(true); scroll.setExpandVertical(true); preview = new PrintPreview(scroll, SWT.NONE); scroll.setContent(preview); scroll.addListener( SWT.Resize, new Listener() { public void handleEvent(Event event) { Rectangle bounds = scroll.getClientArea(); scroll.getHorizontalBar().setPageIncrement(bounds.width * 2 / 3); scroll.getVerticalBar().setPageIncrement(bounds.height * 2 / 3); if (preview.isFitHorizontal() ^ preview.isFitVertical()) { rememberScrollingPosition(); updatePreviewSize(); restoreScrollingPosition(); } } }); preview.setFitVertical(true); preview.setFitHorizontal(true); Listener dragListener = new Listener() { private final Point dpi = display.getDPI(); private boolean scrollable = false; private boolean dragging = false; private Point dragStartScrollOrigin = null; private Point dragStartMouseAnchor = null; public void handleEvent(Event event) { switch (event.type) { case SWT.Resize: forgetScrollingPosition(); Rectangle bounds = scroll.getClientArea(); Point size = preview.getSize(); scrollable = size.x > bounds.width || size.y > bounds.height; if (!scrollable && dragging) endDragging(); break; case SWT.MouseDown: forgetScrollingPosition(); if (scrollable && event.button == 1) beginDragging(event); break; case SWT.MouseMove: if (dragging) { forgetScrollingPosition(); Point point = preview.toDisplay(event.x, event.y); scroll.setOrigin( dragStartScrollOrigin.x + dragStartMouseAnchor.x - point.x, dragStartScrollOrigin.y + dragStartMouseAnchor.y - point.y); } break; case SWT.MouseUp: forgetScrollingPosition(); if (dragging) endDragging(); break; case SWT.MouseEnter: display.addFilter(SWT.MouseWheel, this); break; case SWT.MouseWheel: if (event.count != 0) { if (scrollable && !dragging && (event.stateMask == SWT.NONE || event.stateMask == SWT.SHIFT)) { forgetScrollingPosition(); bounds = scroll.getClientArea(); size = preview.getSize(); Point origin = scroll.getOrigin(); int direction = event.count > 0 ? -1 : 1; // Prefer vertical scrolling unless user is // pressing Shift if (size.y > bounds.height && event.stateMask == SWT.NONE) origin.y += direction * Math.min(dpi.y, bounds.height / 4); else if (size.x > bounds.width) origin.x += direction * Math.min(dpi.x, bounds.width / 4); scroll.setOrigin(origin); event.doit = false; } else if (event.stateMask == SWT.CTRL) { // Ctrl+MouseWheel // -> // zoom float scale = preview.getAbsoluteScale(); setPreviewScale(event.count > 0 ? scale / 1.1f : scale * 1.1f); } } break; case SWT.MouseExit: display.removeFilter(SWT.MouseWheel, this); break; } } private void beginDragging(Event event) { dragStartScrollOrigin = scroll.getOrigin(); dragStartMouseAnchor = preview.toDisplay(event.x, event.y); dragging = true; } private void endDragging() { dragging = false; dragStartMouseAnchor = null; dragStartScrollOrigin = null; } }; scroll.addListener(SWT.Resize, dragListener); preview.addListener(SWT.MouseDown, dragListener); preview.addListener(SWT.MouseMove, dragListener); preview.addListener(SWT.MouseUp, dragListener); // These are for mouse wheel handling preview.addListener(SWT.MouseEnter, dragListener); preview.addListener(SWT.MouseExit, dragListener); return scroll; }