lib/browser/awtUtilities/ScrollCanvas.java
author berghofe
Wed, 06 Aug 1997 00:06:47 +0200
changeset 3599 89cbba12863d
permissions -rw-r--r--
Source files for Isabelle theory graph browser. Initial revision.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3599
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
     1
/***************************************************************************
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
     2
  Title:      awtUtilities/ScrollCanvas.java
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
     3
  ID:         $Id$
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
     4
  Author:     Stefan Berghofer, TU Muenchen
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
     5
  Copyright   1997  TU Muenchen
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
     6
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
     7
  This class contains defines a window with scrollbars, which is not
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
     8
  supported by the 1.0.2 version of the JDK. To use this class, simply
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
     9
  derive your own class from it and overwrite the paintCanvas method.
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    10
  When a repaint event occurs, ScrollCanvas automatically calls the
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    11
  paintCanvas method. The user doesn't have to take care of transposing
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    12
  mouse or graphics coordinates.
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    13
  Note: In later versions of the JDK, a ScrollPane class will be provided,
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    14
  which can be used instead.
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    15
***************************************************************************/
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    16
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    17
package awtUtilities;
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    18
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    19
import java.awt.*;
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    20
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    21
class DummyCanvas extends Canvas {
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    22
	ScrollCanvas parent;
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    23
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    24
	public DummyCanvas(ScrollCanvas p) {
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    25
		parent=p;
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    26
	}
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    27
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    28
	public void paint(Graphics g) {
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    29
		parent.paint(g);
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    30
	}
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    31
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    32
	public void update(Graphics g) {
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    33
		parent.update(g);
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    34
	}
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    35
}
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    36
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    37
public class ScrollCanvas extends Panel
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    38
{
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    39
	Scrollbar scrollv,scrollh;
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    40
	Canvas can;
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    41
	Image img=null;
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    42
	int offset_x=0,offset_y=0;
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    43
	int size_x=100,size_y=100;
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    44
	int old_x=0,old_y=0;
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    45
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    46
	public boolean handleEvent(Event evt)
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    47
	{
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    48
		evt.x+=offset_x;
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    49
		evt.y+=offset_y;
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    50
		if (evt.target instanceof Scrollbar)
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    51
		{
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    52
			offset_x=scrollh.getValue();
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    53
			offset_y=scrollv.getValue();
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    54
			Graphics g=can.getGraphics();
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    55
			if (old_x!=size_x || old_y!=size_y)
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    56
				update(g);
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    57
			else {
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    58
				g.translate(-offset_x,-offset_y);
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    59
				g.drawImage(img,0,0,this);
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    60
			}
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    61
			return true;
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    62
		}
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    63
		else return super.handleEvent(evt);
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    64
	}
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    65
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    66
	public void adjust_scrollbars()
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    67
	{
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    68
		int viewport_v=can.size().height;
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    69
		int viewport_h=can.size().width;
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    70
		int value_v=scrollv.getValue();
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    71
		int value_h=scrollh.getValue();
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    72
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    73
		scrollv.setValues(value_v,viewport_v,0,size_y-viewport_v);
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    74
		scrollh.setValues(value_h,viewport_h,0,size_x-viewport_h);
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    75
		scrollv.setLineIncrement(viewport_v/20);
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    76
		scrollv.setPageIncrement(viewport_v);
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    77
		scrollh.setLineIncrement(viewport_h/20);
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    78
		scrollh.setPageIncrement(viewport_h);
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    79
		offset_x=scrollh.getValue();
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    80
		offset_y=scrollv.getValue();
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    81
	}
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    82
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    83
	public void set_size(int x,int y)
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    84
	{
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    85
		size_x=x;size_y=y;
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    86
		adjust_scrollbars();
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    87
	}
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    88
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    89
	public void focus_to(int x,int y) {
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    90
		offset_x=Math.min(scrollh.getMaximum(),Math.max(0,x-can.size().width/2));
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    91
		offset_y=Math.min(scrollv.getMaximum(),Math.max(0,y-can.size().height/2));
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    92
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    93
		scrollh.setValue(offset_x);
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    94
		scrollv.setValue(offset_y);
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    95
		Graphics g=can.getGraphics();
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    96
		if (old_x!=size_x || old_y!=size_y)
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    97
			update(g);
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    98
		else {
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
    99
			g.translate(-offset_x,-offset_y);
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
   100
			g.drawImage(img,0,0,this);
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
   101
		}
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
   102
	}		
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
   103
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
   104
	public void update(Graphics g) {
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
   105
		g=can.getGraphics();
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
   106
		int viewport_v=can.size().height;
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
   107
		int viewport_h=can.size().width;
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
   108
		int img_x=Math.max(viewport_h,size_x+1);
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
   109
		int img_y=Math.max(viewport_v,size_y+1);
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
   110
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
   111
		old_x=size_x;old_y=size_y;
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
   112
		img=createImage(img_x,img_y);
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
   113
		Graphics g2=img.getGraphics();
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
   114
		g2.setColor(Color.lightGray);
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
   115
		g2.fillRect(0,0,img_x,img_y);
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
   116
		g2.setColor(Color.black);
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
   117
		paintCanvas(g2);
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
   118
		adjust_scrollbars();
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
   119
		g.translate(-offset_x,-offset_y);
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
   120
		g.drawImage(img,0,0,this);
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
   121
	}
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
   122
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
   123
	public void paint(Graphics g)
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
   124
	{
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
   125
		if (img!=null && old_x==size_x && old_y==size_y) {
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
   126
			g=can.getGraphics();
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
   127
			adjust_scrollbars();
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
   128
			g.translate(-offset_x,-offset_y);
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
   129
			g.drawImage(img,0,0,this);
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
   130
		} else update(g);
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
   131
	}
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
   132
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
   133
	public Graphics getGraphics() {
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
   134
		Graphics g=can.getGraphics();
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
   135
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
   136
		g.translate(-offset_x,-offset_y);
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
   137
		return g;
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
   138
	}	
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
   139
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
   140
	public void paintCanvas(Graphics g)
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
   141
	{}
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
   142
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
   143
	public ScrollCanvas()
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
   144
	{
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
   145
		can=new DummyCanvas(this);
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
   146
		GridBagLayout gridbag = new GridBagLayout();
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
   147
		GridBagConstraints c = new GridBagConstraints();
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
   148
		scrollv=new Scrollbar(Scrollbar.VERTICAL);
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
   149
		scrollh=new Scrollbar(Scrollbar.HORIZONTAL);
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
   150
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
   151
		setLayout(gridbag);
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
   152
		c.weightx = 1.0;
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
   153
		c.weighty = 1.0;
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
   154
		c.gridwidth=1;
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
   155
		c.fill = GridBagConstraints.BOTH;
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
   156
		gridbag.setConstraints(can,c);
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
   157
		add(can);
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
   158
		c.weightx=0;
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
   159
		c.weighty=0;
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
   160
		c.gridwidth = GridBagConstraints.REMAINDER;
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
   161
		c.fill = GridBagConstraints.VERTICAL;
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
   162
		gridbag.setConstraints(scrollv,c);
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
   163
		add(scrollv);
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
   164
		c.gridwidth = 1;
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
   165
		c.fill = GridBagConstraints.HORIZONTAL;
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
   166
		gridbag.setConstraints(scrollh,c);
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
   167
		add(scrollh);
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
   168
	}
89cbba12863d Source files for Isabelle theory graph browser.
berghofe
parents:
diff changeset
   169
}