org.eclipse.swt.widgets.Canvas Maven / Gradle / Ivy
/*******************************************************************************
* Copyright (c) 2002, 2012 Innoopract Informationssysteme GmbH 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:
* Innoopract Informationssysteme GmbH - initial API and implementation
* EclipseSource - ongoing development
******************************************************************************/
package org.eclipse.swt.widgets;
import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTException;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.internal.graphics.GCAdapter;
import org.eclipse.swt.internal.graphics.IGCAdapter;
/**
* Instances of this class provide a surface for drawing
* arbitrary graphics.
*
* - Styles:
* - (none)
* - Events:
* - (none)
*
*
* This class may be subclassed by custom control implementors
* who are building controls that are not constructed
* from aggregates of other controls. That is, they are either
* painted using SWT graphics calls or are handled by native
* methods.
*
*
* @see Composite
* @since 1.0
*/
public class Canvas extends Composite {
private transient GCAdapter gcAdapter;
Canvas( Composite parent ) {
// prevent instantiation from outside this package
super( parent );
}
/**
* Constructs a new instance of this class given its parent
* and a style value describing its behavior and appearance.
*
* The style value is either one of the style constants defined in
* class SWT
which is applicable to instances of this
* class, or must be built by bitwise OR'ing together
* (that is, using the int
"|" operator) two or more
* of those SWT
style constants. The class description
* lists the style constants that are applicable to the class.
* Style bits are also inherited from superclasses.
*
*
* @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
* - ERROR_NULL_ARGUMENT - if the parent is null
*
* @exception SWTException
* - ERROR_THREAD_INVALID_ACCESS - if not called from the thread that
* created the parent
*
*
* @see SWT
* @see Widget#checkSubclass
* @see Widget#getStyle
*/
public Canvas( Composite parent, int style ) {
super( parent, style );
}
/**
* Implementation of the Adaptable
interface.
* IMPORTANT: This method is not part of the RWT
* public API. It is marked public only so that it can be shared
* within the packages provided by RWT. It should never be accessed
* from application code.
*
*/
@Override
@SuppressWarnings("unchecked")
public T getAdapter( Class adapter ) {
T result;
if( adapter == IGCAdapter.class ) {
if( gcAdapter == null ) {
gcAdapter = new GCAdapter();
}
result = ( T )gcAdapter;
} else {
result = super.getAdapter( adapter );
}
return result;
}
/**
* Adds the listener to the collection of listeners who will
* be notified when the receiver needs to be painted, by sending it
* one of the messages defined in the PaintListener
* interface.
*
* @param listener the listener which should be notified
*
* @exception IllegalArgumentException
* - ERROR_NULL_ARGUMENT - if the listener is null
*
* @exception SWTException
* - ERROR_WIDGET_DISPOSED - if the receiver has been disposed
* - ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
*
*
* @see PaintListener
* @see #removePaintListener
* @since 1.3
*/
public void addPaintListener( PaintListener listener ) {
checkWidget();
if( listener == null ) {
error( SWT.ERROR_NULL_ARGUMENT );
}
TypedListener typedListener = new TypedListener( listener );
addListener( SWT.Paint, typedListener );
}
/**
* Removes the listener from the collection of listeners who will
* be notified when the receiver needs to be painted.
*
* @param listener the listener which should no longer be notified
*
* @exception IllegalArgumentException
* - ERROR_NULL_ARGUMENT - if the listener is null
*
* @exception SWTException
* - ERROR_WIDGET_DISPOSED - if the receiver has been disposed
* - ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
*
*
* @see PaintListener
* @see #addPaintListener
* @since 1.3
*/
public void removePaintListener( PaintListener listener ) {
checkWidget();
if( listener == null ) {
error( SWT.ERROR_NULL_ARGUMENT );
}
removeListener( SWT.Paint, listener );
}
/////////////
// repainting
@Override
void notifyResize( Point oldSize ) {
super.notifyResize( oldSize );
if( !oldSize.equals( getSize() ) ) {
repaint();
}
}
@Override
void internalSetRedraw( boolean redraw ) {
super.internalSetRedraw( redraw );
if( redraw ) {
repaint();
}
}
private void repaint() {
if( gcAdapter != null ) {
gcAdapter.clearGCOperations();
gcAdapter.setForceRedraw( true );
}
GC gc = new GC( this );
Event paintEvent = new Event();
paintEvent.gc = gc;
paintEvent.setBounds( getClientArea() );
notifyListeners( SWT.Paint, paintEvent );
gc.dispose();
}
}