~ubuntu-branches/ubuntu/oneiric/tuxguitar/oneiric

« back to all changes in this revision

Viewing changes to TuxGuitar/src/org/herac/tuxguitar/gui/editors/tab/Tablature.java

  • Committer: Bazaar Package Importer
  • Author(s): Philippe Coval
  • Date: 2008-06-19 00:30:30 UTC
  • mto: (5.1.2 sid)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20080619003030-h719szrhsngou7c6
Tags: upstream-1.0
ImportĀ upstreamĀ versionĀ 1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Created on 29-nov-2005
 
3
 *
 
4
 * TODO To change the template for this generated file go to
 
5
 * Window - Preferences - Java - Code Style - Code Templates
 
6
 */
 
7
package org.herac.tuxguitar.gui.editors.tab;
 
8
 
 
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;
 
27
/**
 
28
 * @author julian
 
29
 * 
 
30
 * TODO To change the template for this generated type comment go to Window - Preferences - Java - Code Style - Code Templates
 
31
 */
 
32
public class Tablature extends Composite {
 
33
        
 
34
        private static final int SCROLL_DELAY = 15;
 
35
        private static final int SCROLL_INCREMENT = 50;
 
36
        
 
37
        private TGSongManager songManager;
 
38
        private Caret caret;
 
39
        private int width;
 
40
        private int height;
 
41
        private ViewLayout viewLayout;
 
42
        private EditorKit editorKit;
 
43
        
 
44
        private TGBeatImpl playedBeat;
 
45
        private TGMeasureImpl playedMeasure;
 
46
        
 
47
        private int scrollX;
 
48
        private int scrollY;
 
49
        private boolean resetScroll;
 
50
        protected long lastVScrollTime;
 
51
        protected long lastHScrollTime;
 
52
        
 
53
        private boolean painting;
 
54
        
 
55
        public Tablature(final Composite parent) {
 
56
                this(parent,SWT.NONE);
 
57
        }
 
58
        
 
59
        public Tablature(final Composite parent,int style) {
 
60
                super(parent, style);
 
61
                this.editorKit = new EditorKit(this);
 
62
        }
 
63
        
 
64
        public void initGUI(){
 
65
                this.addPaintListener(new TablaturePaintListener(this));
 
66
                
 
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()){
 
72
                                        redraw();
 
73
                                        Tablature.this.lastHScrollTime = System.currentTimeMillis();
 
74
                                }
 
75
                        }
 
76
                });
 
77
                
 
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()){
 
83
                                        redraw();
 
84
                                        Tablature.this.lastVScrollTime = System.currentTimeMillis();
 
85
                                }
 
86
                        }
 
87
                });
 
88
                
 
89
                this.addControlListener(new ControlAdapter() {
 
90
                        public void controlResized(ControlEvent arg0) {
 
91
                                updateScroll();
 
92
                        }
 
93
                });
 
94
        }
 
95
        
 
96
        public void initDefaults(){
 
97
                this.caret = new Caret(this);
 
98
        }
 
99
        
 
100
        public void updateTablature(){
 
101
                this.playedBeat = null;
 
102
                this.playedMeasure = null;
 
103
                getViewLayout().updateSong();
 
104
        }
 
105
        
 
106
        public void initCaret(){
 
107
                this.caret.update(1,TGDuration.QUARTER_TIME,1);
 
108
        }
 
109
        
 
110
        public synchronized void paintTablature(TGPainter painter){
 
111
                if(!TuxGuitar.instance().isLocked()){
 
112
                        TuxGuitar.instance().lock();
 
113
                        this.setPainting(true);
 
114
                        try{
 
115
                                this.checkScroll();
 
116
                                
 
117
                                Rectangle area = getClientArea();
 
118
                                ScrollBar xScroll = getHorizontalBar();
 
119
                                ScrollBar yScroll = getVerticalBar();
 
120
                                this.scrollX = xScroll.getSelection();
 
121
                                this.scrollY = yScroll.getSelection();
 
122
                                
 
123
                                this.getViewLayout().paint(painter,area,-this.scrollX,-this.scrollY);
 
124
                                
 
125
                                this.width = this.viewLayout.getWidth();
 
126
                                this.height = this.viewLayout.getHeight();
 
127
                                
 
128
                                this.updateScroll();
 
129
                                
 
130
                                if(TuxGuitar.instance().getPlayer().isRunning()){
 
131
                                        redrawPlayingMode(painter,true);
 
132
                                }
 
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);
 
139
                                        
 
140
                                        moveScrollTo(getCaret().getMeasure(), xScroll, yScroll,area);
 
141
                                }
 
142
                        }catch(Throwable throwable){
 
143
                                throwable.printStackTrace();
 
144
                        }
 
145
                        this.setPainting(false);
 
146
                        TuxGuitar.instance().unlock();
 
147
                }
 
148
        }
 
149
        
 
150
        public void resetScroll(){
 
151
                this.resetScroll = true;
 
152
        }
 
153
        
 
154
        public void checkScroll(){
 
155
                if(this.resetScroll){
 
156
                        getHorizontalBar().setSelection(0);
 
157
                        getVerticalBar().setSelection(0);
 
158
                        this.resetScroll = false;
 
159
                }
 
160
        }
 
161
        
 
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));
 
171
        }
 
172
        
 
173
        public boolean moveScrollTo(TGMeasureImpl measure){
 
174
                return moveScrollTo(measure,getHorizontalBar(),getVerticalBar(),getClientArea());
 
175
        }
 
176
        
 
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;
 
187
                        
 
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 );
 
192
                                success = true;
 
193
                        }
 
194
                        
 
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 );
 
199
                                success = true;
 
200
                        }
 
201
                        
 
202
                        if(!success){
 
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());
 
205
                        }
 
206
                        
 
207
                        if(forceRedraw || success){
 
208
                                redraw();
 
209
                        }
 
210
                }
 
211
                return success;
 
212
        }
 
213
        
 
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);
 
220
                        super.redraw();
 
221
                }
 
222
        }
 
223
        
 
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);
 
230
                                
 
231
                                TGPainter painter = new TGPainter(new GC(this));
 
232
                                redrawPlayingMode(painter,false);
 
233
                                painter.dispose();
 
234
                                
 
235
                                this.setPainting(false);
 
236
                        }
 
237
                        TuxGuitar.instance().unlock();
 
238
                }
 
239
        }
 
240
        
 
241
        private void redrawPlayingMode(TGPainter painter,boolean force){
 
242
                if(!super.isDisposed() && !TuxGuitar.instance().isLocked()){
 
243
                        try{
 
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);
 
251
                                                }
 
252
                                                if(!measure.isOutOfBounds()){
 
253
                                                        getViewLayout().paintPlayMode(painter, measure, beat,paintMeasure);
 
254
                                                }
 
255
                                                this.playedBeat = beat;
 
256
                                                this.playedMeasure =  measure;
 
257
                                        }
 
258
                                }
 
259
                        }catch(Throwable throwable){
 
260
                                throwable.printStackTrace();
 
261
                        }
 
262
                }
 
263
        }
 
264
        
 
265
        public boolean isPainting() {
 
266
                return this.painting;
 
267
        }
 
268
        
 
269
        public void setPainting(boolean painting) {
 
270
                this.painting = painting;
 
271
        }
 
272
        
 
273
        public Caret getCaret(){
 
274
                return this.caret;
 
275
        }
 
276
        
 
277
        public EditorKit getEditorKit() {
 
278
                return this.editorKit;
 
279
        }
 
280
        
 
281
        public TGSongManager getSongManager() {
 
282
                return this.songManager;
 
283
        }
 
284
        public void setSongManager(TGSongManager songManager) {
 
285
                this.songManager = songManager;
 
286
        }
 
287
        
 
288
        public ViewLayout getViewLayout(){
 
289
                return this.viewLayout;
 
290
        }
 
291
        
 
292
        public void setViewLayout(ViewLayout viewLayout){
 
293
                if(getViewLayout() != null){
 
294
                        getViewLayout().disposeLayout();
 
295
                }
 
296
                this.viewLayout = viewLayout;
 
297
                if(this.getHorizontalBar() != null){
 
298
                        this.getHorizontalBar().setSelection(0);
 
299
                }
 
300
                if(this.getVerticalBar() != null){
 
301
                        this.getVerticalBar().setSelection(0);
 
302
                }
 
303
                this.reloadStyles();
 
304
        }
 
305
        
 
306
        public void reloadStyles(){
 
307
                if(this.getViewLayout() != null){
 
308
                        this.getViewLayout().reloadStyles();
 
309
                        this.setBackground(getViewLayout().getResources().getBackgroundColor());
 
310
                }
 
311
        }
 
312
        
 
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;
 
317
                switch(mode){
 
318
                        case ViewLayout.MODE_PAGE:
 
319
                                setViewLayout(new PageViewLayout(this,style));
 
320
                                break;
 
321
                        case ViewLayout.MODE_LINEAR:
 
322
                                setViewLayout(new LinearViewLayout(this,style));
 
323
                                break;
 
324
                }
 
325
        }
 
326
        
 
327
        public void dispose(){
 
328
                super.dispose();
 
329
                this.getViewLayout().disposeLayout();
 
330
        }
 
331
}
 
 
b'\\ No newline at end of file'