2
* Created on 29-nov-2005
4
* TODO To change the template for this generated file go to
5
* Window - Preferences - Java - Code Style - Code Templates
7
package org.herac.tuxguitar.gui.editors.tab;
9
import org.eclipse.swt.SWT;
10
import org.eclipse.swt.events.ControlAdapter;
11
import org.eclipse.swt.events.ControlEvent;
12
import org.eclipse.swt.graphics.GC;
13
import org.eclipse.swt.graphics.Rectangle;
14
import org.eclipse.swt.widgets.Composite;
15
import org.eclipse.swt.widgets.Event;
16
import org.eclipse.swt.widgets.Listener;
17
import org.eclipse.swt.widgets.ScrollBar;
18
import org.herac.tuxguitar.gui.TuxGuitar;
19
import org.herac.tuxguitar.gui.editors.TGPainter;
20
import org.herac.tuxguitar.gui.editors.tab.edit.EditorKit;
21
import org.herac.tuxguitar.gui.editors.tab.layout.LinearViewLayout;
22
import org.herac.tuxguitar.gui.editors.tab.layout.PageViewLayout;
23
import org.herac.tuxguitar.gui.editors.tab.layout.ViewLayout;
24
import org.herac.tuxguitar.gui.system.config.TGConfigKeys;
25
import org.herac.tuxguitar.song.managers.TGSongManager;
26
import org.herac.tuxguitar.song.models.TGDuration;
30
* TODO To change the template for this generated type comment go to Window - Preferences - Java - Code Style - Code Templates
32
public class Tablature extends Composite {
34
private static final int SCROLL_DELAY = 15;
35
private static final int SCROLL_INCREMENT = 50;
37
private TGSongManager songManager;
41
private ViewLayout viewLayout;
42
private EditorKit editorKit;
44
private TGBeatImpl playedBeat;
45
private TGMeasureImpl playedMeasure;
49
private boolean resetScroll;
50
protected long lastVScrollTime;
51
protected long lastHScrollTime;
53
private boolean painting;
55
public Tablature(final Composite parent) {
56
this(parent,SWT.NONE);
59
public Tablature(final Composite parent,int style) {
61
this.editorKit = new EditorKit(this);
64
public void initGUI(){
65
this.addPaintListener(new TablaturePaintListener(this));
67
final ScrollBar hBar = getHorizontalBar();
68
hBar.setIncrement(SCROLL_INCREMENT);
69
hBar.addListener(SWT.Selection, new Listener() {
70
public void handleEvent(Event e) {
71
if(Tablature.this.lastHScrollTime + SCROLL_DELAY < System.currentTimeMillis()){
73
Tablature.this.lastHScrollTime = System.currentTimeMillis();
78
final ScrollBar vBar = getVerticalBar();
79
vBar.setIncrement(SCROLL_INCREMENT);
80
vBar.addListener(SWT.Selection, new Listener() {
81
public void handleEvent(Event e) {
82
if(Tablature.this.lastVScrollTime + SCROLL_DELAY < System.currentTimeMillis()){
84
Tablature.this.lastVScrollTime = System.currentTimeMillis();
89
this.addControlListener(new ControlAdapter() {
90
public void controlResized(ControlEvent arg0) {
96
public void initDefaults(){
97
this.caret = new Caret(this);
100
public void updateTablature(){
101
this.playedBeat = null;
102
this.playedMeasure = null;
103
getViewLayout().updateSong();
106
public void initCaret(){
107
this.caret.update(1,TGDuration.QUARTER_TIME,1);
110
public synchronized void paintTablature(TGPainter painter){
111
if(!TuxGuitar.instance().isLocked()){
112
TuxGuitar.instance().lock();
113
this.setPainting(true);
117
Rectangle area = getClientArea();
118
ScrollBar xScroll = getHorizontalBar();
119
ScrollBar yScroll = getVerticalBar();
120
this.scrollX = xScroll.getSelection();
121
this.scrollY = yScroll.getSelection();
123
this.getViewLayout().paint(painter,area,-this.scrollX,-this.scrollY);
125
this.width = this.viewLayout.getWidth();
126
this.height = this.viewLayout.getHeight();
130
if(TuxGuitar.instance().getPlayer().isRunning()){
131
redrawPlayingMode(painter,true);
133
// Si no estoy reproduciendo y hay cambios
134
// muevo el scroll al compas que tiene el caret
135
else if(getCaret().hasChanges()){
136
// Mover el scroll puede necesitar redibujar
137
// por eso es importante desmarcar los cambios antes de hacer el moveScrollTo
138
getCaret().setChanges(false);
140
moveScrollTo(getCaret().getMeasure(), xScroll, yScroll,area);
142
}catch(Throwable throwable){
143
throwable.printStackTrace();
145
this.setPainting(false);
146
TuxGuitar.instance().unlock();
150
public void resetScroll(){
151
this.resetScroll = true;
154
public void checkScroll(){
155
if(this.resetScroll){
156
getHorizontalBar().setSelection(0);
157
getVerticalBar().setSelection(0);
158
this.resetScroll = false;
162
public void updateScroll(){
163
Rectangle bounds = getBounds();
164
Rectangle client = getClientArea();
165
ScrollBar hBar = getHorizontalBar();
166
ScrollBar vBar = getVerticalBar();
167
hBar.setMaximum(this.width);
168
vBar.setMaximum(this.height);
169
hBar.setThumb(Math.min(bounds.width, client.width));
170
vBar.setThumb(Math.min(bounds.height, client.height));
173
public boolean moveScrollTo(TGMeasureImpl measure){
174
return moveScrollTo(measure,getHorizontalBar(),getVerticalBar(),getClientArea());
177
public boolean moveScrollTo(TGMeasureImpl measure,ScrollBar xScroll,ScrollBar yScroll,Rectangle area){
178
boolean success = false;
179
if(measure != null && measure.getTs() != null){
180
int mX = measure.getPosX();
181
int mY = measure.getPosY();
182
int mWidth = measure.getWidth(getViewLayout());
183
int mHeight = measure.getTs().getSize();
184
int marginWidth = getViewLayout().getFirstMeasureSpacing();
185
int marginHeight = getViewLayout().getFirstTrackSpacing();
186
boolean forceRedraw = false;
188
//Solo se ajusta si es necesario
189
//si el largo del compas es mayor al de la pantalla. nunca se puede ajustar a la medida.
190
if( mX < 0 || ( (mX + mWidth ) > area.width && (area.width >= mWidth + marginWidth || mX > marginWidth) ) ){
191
xScroll.setSelection((this.scrollX + mX) - marginWidth );
195
//Solo se ajusta si es necesario
196
//si el alto del compas es mayor al de la pantalla. nunca se puede ajustar a la medida.
197
if( mY < 0 || ( (mY + mHeight ) > area.height && (area.height >= mHeight + marginHeight || mY > marginHeight) ) ){
198
yScroll.setSelection( (this.scrollY + mY) - marginHeight );
203
// Si la seleccion "real" del scroll es distinta a la anterior, se fuerza el redraw
204
forceRedraw = (this.scrollX != xScroll.getSelection() || this.scrollY != yScroll.getSelection());
207
if(forceRedraw || success){
214
public void redraw(){
215
if(!super.isDisposed() && !TuxGuitar.instance().isLocked()){
216
this.playedBeat = null;
217
this.playedMeasure = null;
218
this.editorKit.tryBack();
219
this.setPainting(true);
224
public void redrawPlayingMode(){
225
if(!super.isDisposed() && !isPainting() && !TuxGuitar.instance().isLocked()){
226
TuxGuitar.instance().lock();
227
if(TuxGuitar.instance().getPlayer().isRunning()){
228
this.editorKit.tryBack();
229
this.setPainting(true);
231
TGPainter painter = new TGPainter(new GC(this));
232
redrawPlayingMode(painter,false);
235
this.setPainting(false);
237
TuxGuitar.instance().unlock();
241
private void redrawPlayingMode(TGPainter painter,boolean force){
242
if(!super.isDisposed() && !TuxGuitar.instance().isLocked()){
244
TGMeasureImpl measure = TuxGuitar.instance().getEditorCache().getPlayMeasure();
245
TGBeatImpl beat = TuxGuitar.instance().getEditorCache().getPlayBeat();
246
if(measure != null && beat != null && measure.hasTrack(getCaret().getTrack().getNumber())){
247
if(!moveScrollTo(measure) || force){
248
boolean paintMeasure = (force || this.playedMeasure == null || !this.playedMeasure.equals(measure));
249
if(this.playedMeasure != null && this.playedBeat != null && !this.playedMeasure.isOutOfBounds() && this.playedMeasure.hasTrack(getCaret().getTrack().getNumber())){
250
getViewLayout().paintPlayMode(painter, this.playedMeasure, this.playedBeat,paintMeasure);
252
if(!measure.isOutOfBounds()){
253
getViewLayout().paintPlayMode(painter, measure, beat,paintMeasure);
255
this.playedBeat = beat;
256
this.playedMeasure = measure;
259
}catch(Throwable throwable){
260
throwable.printStackTrace();
265
public boolean isPainting() {
266
return this.painting;
269
public void setPainting(boolean painting) {
270
this.painting = painting;
273
public Caret getCaret(){
277
public EditorKit getEditorKit() {
278
return this.editorKit;
281
public TGSongManager getSongManager() {
282
return this.songManager;
284
public void setSongManager(TGSongManager songManager) {
285
this.songManager = songManager;
288
public ViewLayout getViewLayout(){
289
return this.viewLayout;
292
public void setViewLayout(ViewLayout viewLayout){
293
if(getViewLayout() != null){
294
getViewLayout().disposeLayout();
296
this.viewLayout = viewLayout;
297
if(this.getHorizontalBar() != null){
298
this.getHorizontalBar().setSelection(0);
300
if(this.getVerticalBar() != null){
301
this.getVerticalBar().setSelection(0);
306
public void reloadStyles(){
307
if(this.getViewLayout() != null){
308
this.getViewLayout().reloadStyles();
309
this.setBackground(getViewLayout().getResources().getBackgroundColor());
313
public void reloadViewLayout(){
314
int style = TuxGuitar.instance().getConfig().getIntConfigValue(TGConfigKeys.LAYOUT_STYLE);
315
int mode = TuxGuitar.instance().getConfig().getIntConfigValue(TGConfigKeys.LAYOUT_MODE);
316
mode = (mode == 0)?ViewLayout.DEFAULT_MODE:mode;
318
case ViewLayout.MODE_PAGE:
319
setViewLayout(new PageViewLayout(this,style));
321
case ViewLayout.MODE_LINEAR:
322
setViewLayout(new LinearViewLayout(this,style));
327
public void dispose(){
329
this.getViewLayout().disposeLayout();
b'\\ No newline at end of file'