33/*
44 Part of the Processing project - http://processing.org
55
6- Copyright (c) 2004-11 Ben Fry and Casey Reas
6+ Copyright (c) 2004-13 Ben Fry and Casey Reas
77 Copyright (c) 2001-04 Massachusetts Institute of Technology
88
99 This program is free software; you can redistribute it and/or modify
2525
2626import java .awt .*;
2727import java .awt .event .*;
28+ import java .awt .geom .GeneralPath ;
2829import java .util .Arrays ;
2930
3031import javax .swing .*;
3435 * Sketch tabs at the top of the editor window.
3536 */
3637public class EditorHeader extends JComponent {
38+ // standard UI sizing (OS-specific, but generally consistent)
39+ static final int SCROLLBAR_WIDTH = 16 ;
40+ // amount of space on the left edge before the tabs start
41+ static final int MARGIN_WIDTH = 6 ;
42+ // distance from the righthand side of a tab to the drop-down arrow
43+ static final int ARROW_GAP_WIDTH = 8 ;
44+ // indent x/y for notch on the tab
45+ static final int NOTCH = 4 ;
46+ // how far to raise the tab from the bottom of this Component
47+ static final int TAB_HEIGHT = 27 ;
48+ // amount of margin on the left/right for the text on the tab
49+ static final int TEXT_MARGIN = 5 ;
50+ // width of the tab when no text visible
51+ // (total tab width will be this plus TEXT_MARGIN*2)
52+ static final int NO_TEXT_WIDTH = 10 ;
53+
3754 Color backgroundColor ;
3855 Color textColor [] = new Color [2 ];
56+ Color tabColor [] = new Color [2 ];
3957
4058 Editor editor ;
4159
@@ -68,8 +86,8 @@ public class EditorHeader extends JComponent {
6886 static final int PIECE_HEIGHT = 33 ;
6987 Image [][] pieces ;
7088
71- static final int ARROW_WIDTH = 6 ;
72- static final int ARROW_HEIGHT = 6 ;
89+ static final int ARROW_WIDTH = 14 ;
90+ static final int ARROW_HEIGHT = 14 ;
7391 Image tabArrow ;
7492
7593 //
@@ -167,12 +185,15 @@ public void updateMode() {
167185 }
168186 }
169187
170- tabArrow = mode .loadImage ("theme/tab-arrow" );
188+ tabArrow = mode .loadImage ("theme/tab-arrow" + suffix );
171189
172190 backgroundColor = mode .getColor ("header.bgcolor" );
173191 textColor [SELECTED ] = mode .getColor ("header.text.selected.color" );
174192 textColor [UNSELECTED ] = mode .getColor ("header.text.unselected.color" );
175193 font = mode .getFont ("header.text.font" );
194+
195+ tabColor [SELECTED ] = mode .getColor ("header.tab.selected.color" );
196+ tabColor [UNSELECTED ] = mode .getColor ("header.tab.unselected.color" );
176197 }
177198
178199
@@ -228,6 +249,15 @@ public void paintComponent(Graphics screen) {
228249 // set the background for the offscreen
229250 g .setColor (backgroundColor );
230251 g .fillRect (0 , 0 , imageW , imageH );
252+
253+ // EditorToolbar toolbar = editor.toolbar;
254+ // if (toolbar != null && toolbar.backgroundImage != null) {
255+ // g.drawImage(toolbar.backgroundImage,
256+ // 0, -toolbar.getHeight(),
257+ // EditorToolbar.BACKGROUND_WIDTH,
258+ // EditorToolbar.BACKGROUND_HEIGHT, null);
259+ // }
260+ editor .getMode ().drawBackground (g , EditorToolbar .BUTTON_HEIGHT );
231261
232262// int codeCount = sketch.getCodeCount();
233263// if ((tabLeft == null) || (tabLeft.length < codeCount)) {
@@ -242,11 +272,10 @@ public void paintComponent(Graphics screen) {
242272 visitOrder = new Tab [sketch .getCodeCount () - 1 ];
243273 }
244274
245- menuRight = sizeW - 16 ;
275+ // menuRight = sizeW - 16;
246276// menuLeft = menuRight - pieces[0][MENU].getWidth(this);
247- menuLeft = menuRight - 50 ; // FIXME!!
248- int tabLeft = 6 ;
249- int tabMax = menuLeft - tabLeft ;
277+ // menuLeft = menuRight - 50; // FIXME!!
278+ int tabMax = menuLeft - (MARGIN_WIDTH + SCROLLBAR_WIDTH );
250279
251280 // reset all tab positions
252281 for (Tab tab : tabs ) {
@@ -265,7 +294,7 @@ public void paintComponent(Graphics screen) {
265294 }
266295
267296 // make sure everything can fit
268- if (!placeTabs (tabLeft , tabMax , null )) {
297+ if (!placeTabs (MARGIN_WIDTH , tabMax , null )) {
269298 //System.arraycopy(tabs, 0, visitOrder, 0, tabs.length);
270299 // always show the tab with the sketch's name
271300// System.arraycopy(tabs, 1, visitOrder, 0, tabs.length - 1);
@@ -280,69 +309,103 @@ public void paintComponent(Graphics screen) {
280309// }
281310// System.out.println();
282311
312+ // Keep shrinking the tabs one-by-one until things fit properly
283313 for (int i = 0 ; i < visitOrder .length ; i ++) {
284314 tabs [visitOrder [i ].index ].textVisible = false ;
285- if (placeTabs (tabLeft , tabMax , null )) {
315+ if (placeTabs (MARGIN_WIDTH , tabMax , null )) {
286316 break ;
287317 }
288318 }
289319 }
290320
291321 // now actually draw the tabs
292- placeTabs (tabLeft , tabMax , g );
322+ placeTabs (MARGIN_WIDTH , tabMax , g2 );
293323
294324 // draw the dropdown menu target
325+ menuLeft = tabs [tabs .length - 1 ].right + ARROW_GAP_WIDTH ;
326+ menuRight = menuLeft + ARROW_WIDTH ;
327+ int arrowY = (getHeight () - TAB_HEIGHT ) + (TAB_HEIGHT - ARROW_HEIGHT )/2 ;
328+ g .drawImage (tabArrow , menuLeft , arrowY ,
329+ ARROW_WIDTH , ARROW_HEIGHT , null );
295330// g.drawImage(pieces[popup.isVisible() ? SELECTED : UNSELECTED][MENU],
296331// menuLeft, 0, null);
297332
298333 screen .drawImage (offscreen , 0 , 0 , imageW , imageH , null );
299334 }
300335
301336
302- private boolean placeTabs (int left , int right , Graphics g ) {
337+ private boolean placeTabs (int left , int right , Graphics2D g ) {
303338 Sketch sketch = editor .getSketch ();
304339 int x = left ;
305340
341+ final int bottom = getHeight ();
342+ final int top = bottom - TAB_HEIGHT ;
343+ GeneralPath path = null ;
344+
306345 for (int i = 0 ; i < sketch .getCodeCount (); i ++) {
307346 SketchCode code = sketch .getCode (i );
308347 Tab tab = tabs [i ];
309348
310- int pieceCount = 2 + (tab .textWidth / PIECE_WIDTH );
311- if (tab .textVisible == false ) {
312- pieceCount = 4 ;
313- }
314- int pieceWidth = pieceCount * PIECE_WIDTH ;
349+ // int pieceCount = 2 + (tab.textWidth / PIECE_WIDTH);
350+ // if (tab.textVisible == false) {
351+ // pieceCount = 4;
352+ // }
353+ // int pieceWidth = pieceCount * PIECE_WIDTH;
315354
316355 int state = (code == sketch .getCurrentCode ()) ? SELECTED : UNSELECTED ;
317356 if (g != null ) {
318- g .drawImage (pieces [state ][LEFT ], x , 0 , PIECE_WIDTH , PIECE_HEIGHT , null );
357+ //g.drawImage(pieces[state][LEFT], x, 0, PIECE_WIDTH, PIECE_HEIGHT, null);
358+ path = new GeneralPath ();
359+ path .moveTo (x , bottom );
360+ path .lineTo (x , top + NOTCH );
361+ path .lineTo (x + NOTCH , top );
319362 }
320- x += PIECE_WIDTH ;
321-
322- int contentLeft = x ;
323363 tab .left = x ;
324- for (int j = 0 ; j < pieceCount ; j ++) {
325- if (g != null ) {
326- g .drawImage (pieces [state ][MIDDLE ], x , 0 , PIECE_WIDTH , PIECE_HEIGHT , null );
327- }
328- x += PIECE_WIDTH ;
329- }
364+ x += TEXT_MARGIN ;
365+ // x += PIECE_WIDTH;
366+
367+ // int contentLeft = x;
368+ // for (int j = 0; j < pieceCount; j++) {
369+ // if (g != null) {
370+ // g.drawImage(pieces[state][MIDDLE], x, 0, PIECE_WIDTH, PIECE_HEIGHT, null);
371+ // }
372+ // x += PIECE_WIDTH;
373+ // }
374+ // if (g != null) {
375+ int drawWidth = tab .textVisible ? tab .textWidth : NO_TEXT_WIDTH ;
376+ x += drawWidth + TEXT_MARGIN ;
377+ // path.moveTo(x, top);
378+ // }
330379 tab .right = x ;
331380
381+ if (g != null ) {
382+ path .lineTo (x - NOTCH , top );
383+ path .lineTo (x , top + NOTCH );
384+ path .lineTo (x , bottom );
385+ path .closePath ();
386+ g .setColor (tabColor [state ]);
387+ g .fill (path );
388+ //g.drawImage(pieces[state][RIGHT], x, 0, PIECE_WIDTH, PIECE_HEIGHT, null);
389+ }
390+
332391 if (tab .textVisible ) {
333- int textLeft = contentLeft + (pieceWidth - tab .textWidth ) / 2 ;
392+ // int textLeft = contentLeft + (pieceWidth - tab.textWidth) / 2;
334393 if (g != null ) {
394+ int textLeft = tab .left + ((tab .right - tab .left ) - tab .textWidth ) / 2 ;
335395 g .setColor (textColor [state ]);
336- int baseline = (sizeH + fontAscent ) / 2 ;
396+ // int baseline = (int) Math.ceil((sizeH + fontAscent) / 2.0);
397+ int baseline = bottom - (TAB_HEIGHT - fontAscent )/2 - 1 ;
337398 //g.drawString(sketch.code[i].name, textLeft, baseline);
338399 g .drawString (tab .text , textLeft , baseline );
400+ // g.drawLine(tab.left, baseline-fontAscent, tab.right, baseline-fontAscent);
339401 }
340402 }
341403
342- if (g != null ) {
343- g .drawImage (pieces [state ][RIGHT ], x , 0 , PIECE_WIDTH , PIECE_HEIGHT , null );
344- }
345- x += PIECE_WIDTH - 1 ; // overlap by 1 pixel
404+ // if (g != null) {
405+ // g.drawImage(pieces[state][RIGHT], x, 0, PIECE_WIDTH, PIECE_HEIGHT, null);
406+ // }
407+ // x += PIECE_WIDTH - 1; // overlap by 1 pixel
408+ x += 1 ;
346409 }
347410 return x <= right ;
348411 }
0 commit comments