add page setup & print

This commit is contained in:
benfry
2006-11-06 04:39:18 +00:00
parent f4ff456dec
commit e6ec59389d
3 changed files with 105 additions and 15 deletions
+56 -2
View File
@@ -32,6 +32,7 @@ import java.awt.*;
import java.awt.datatransfer.*;
import java.awt.dnd.*;
import java.awt.event.*;
import java.awt.print.*;
import java.io.*;
import java.lang.reflect.*;
import java.net.*;
@@ -77,6 +78,9 @@ public class Editor extends JFrame
boolean handleNewShift;
boolean handleNewLibrary;
PageFormat pageFormat;
PrinterJob printerJob;
EditorButtons buttons;
EditorHeader header;
EditorStatus status;
@@ -561,11 +565,20 @@ public class Editor extends JFrame
menu.addSeparator();
item = newJMenuItem("Page Setup", 'P', true);
item.setEnabled(false);
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handlePageSetup();
}
});
//item.setEnabled(false);
menu.add(item);
item = newJMenuItem("Print", 'P');
item.setEnabled(false);
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handlePrint();
}
});
menu.add(item);
// macosx already has its own preferences and quit menu
@@ -1811,6 +1824,47 @@ public class Editor extends JFrame
}
public void handlePageSetup() {
//printerJob = null;
if (printerJob == null) {
printerJob = PrinterJob.getPrinterJob();
}
if (pageFormat == null) {
pageFormat = printerJob.defaultPage();
}
pageFormat = printerJob.pageDialog(pageFormat);
//System.out.println("page format is " + pageFormat);
}
public void handlePrint() {
message("Printing...");
//printerJob = null;
if (printerJob == null) {
printerJob = PrinterJob.getPrinterJob();
}
if (pageFormat != null) {
//System.out.println("setting page format " + pageFormat);
printerJob.setPrintable(textarea.getPainter(), pageFormat);
} else {
printerJob.setPrintable(textarea.getPainter());
}
if (printerJob.printDialog()) {
try {
printerJob.print();
message("Done printing.");
} catch (PrinterException pe) {
error("Error while printing.");
pe.printStackTrace();
}
} else {
message("Printing canceled.");
}
//printerJob = null; // clear this out?
}
/**
* Quit, but first ask user if it's ok. Also store preferences
* to disk just in case they want to quit. Final exit() happens
+45 -10
View File
@@ -18,6 +18,7 @@ import javax.swing.text.*;
import javax.swing.JComponent;
import java.awt.event.MouseEvent;
import java.awt.*;
import java.awt.print.*;
/**
* The text area repaint manager. It performs double buffering and paints
@@ -25,8 +26,12 @@ import java.awt.*;
* @author Slava Pestov
* @version $Id$
*/
public class TextAreaPainter extends JComponent implements TabExpander
public class TextAreaPainter extends JComponent
implements TabExpander, Printable
{
/** True if inside printing, will handle disabling the highlight */
boolean printing;
/**
* Creates a new repaint manager. This should be not be called
* directly.
@@ -405,6 +410,34 @@ public class TextAreaPainter extends JComponent implements TabExpander
}
}
public int print(Graphics g, PageFormat pageFormat, int pageIndex) {
//System.out.println("painter page format is " + pageFormat);
int lineHeight = fm.getHeight();
int linesPerPage = (int) (pageFormat.getImageableHeight() / lineHeight);
int lineCount = textArea.getLineCount();
//int pageCount = (lineCount + linesPerPage-1) / linesPerPage;
int lastPage = lineCount / linesPerPage;
if (pageIndex > lastPage) {
return NO_SUCH_PAGE;
} else {
Graphics2D g2d = (Graphics2D)g;
TokenMarker tokenMarker = textArea.getDocument().getTokenMarker();
int firstLine = pageIndex*linesPerPage;
g2d.translate(pageFormat.getImageableX(),
pageFormat.getImageableY() - firstLine*lineHeight);
printing = true;
for (int line = firstLine; line < firstLine + linesPerPage; line++) {
paintLine(g2d, tokenMarker, line, 0);
}
printing = false;
return PAGE_EXISTS;
}
}
/**
* Marks a line as needing a repaint.
* @param line The line to invalidate
@@ -600,18 +633,20 @@ public class TextAreaPainter extends JComponent implements TabExpander
protected void paintHighlight(Graphics gfx, int line, int y)
{
if (line >= textArea.getSelectionStartLine()
&& line <= textArea.getSelectionEndLine())
paintLineHighlight(gfx,line,y);
if (!printing) {
if (line >= textArea.getSelectionStartLine()
&& line <= textArea.getSelectionEndLine())
paintLineHighlight(gfx,line,y);
if (highlights != null)
highlights.paintHighlight(gfx,line,y);
if (highlights != null)
highlights.paintHighlight(gfx,line,y);
if (bracketHighlight && line == textArea.getBracketLine())
paintBracketHighlight(gfx,line,y);
if (bracketHighlight && line == textArea.getBracketLine())
paintBracketHighlight(gfx,line,y);
if (line == textArea.getCaretLine())
paintCaret(gfx,line,y);
if (line == textArea.getCaretLine())
paintCaret(gfx,line,y);
}
}
protected void paintLineHighlight(Graphics gfx, int line, int y)
+4 -3
View File
@@ -1,6 +1,10 @@
0121 pde
X fix button fatness on osx
X quaqua already takes care of this for us
X implement page setup and print
X pretty printing of code in project
X http://dev.processing.org/bugs/show_bug.cgi?id=27
X also turn off line highlighting while printing
reference
_ need to document changes to int() (no longer accepts boolean)
@@ -360,9 +364,6 @@ _ if it's made too small, stuff from the bottom disappears
_ http://dev.processing.org/bugs/show_bug.cgi?id=25
_ add mnemonics for menus (alt-f to open 'file')
_ http://dev.processing.org/bugs/show_bug.cgi?id=26
_ implement page setup and print
_ pretty printing of code in project
_ http://dev.processing.org/bugs/show_bug.cgi?id=27
_ option to just print all code in project
_ option to export all the code as colored html
_ http://dev.processing.org/bugs/show_bug.cgi?id=28