1 /*
2 * Copyright (c) 2002, 2014, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
262 public void setScrollPosition(int x, int y) {
263 scroll(x, y, VERTICAL | HORIZONTAL);
264 }
265
266 void scroll(int x, int y, int flag) {
267 scroll(x, y, flag, AdjustmentEvent.TRACK);
268 }
269
270 /**
271 * Scroll the contents to position x, y
272 */
273 @SuppressWarnings("deprecation")
274 void scroll(int x, int y, int flag, int type) {
275 checkSecurity();
276 ScrollPane sp = (ScrollPane)target;
277 Component c = getScrollChild();
278 if (c == null) {
279 return;
280 }
281 int sx, sy;
282 Color colors[] = getGUIcolors();
283
284 if (sp.getScrollbarDisplayPolicy() == ScrollPane.SCROLLBARS_NEVER) {
285 sx = -x;
286 sy = -y;
287 } else {
288 Point p = c.location();
289 sx = p.x;
290 sy = p.y;
291
292 if ((flag & HORIZONTAL) != 0) {
293 hsb.setValue(Math.min(x, hsb.getMaximum()-hsb.getVisibleAmount()));
294 ScrollPaneAdjustable hadj = (ScrollPaneAdjustable)sp.getHAdjustable();
295 setAdjustableValue(hadj, hsb.getValue(), type);
296 sx = -(hsb.getValue());
297 Graphics g = getGraphics();
298 if (g != null) {
299 try {
300 paintHorScrollbar(g, colors, true);
301 } finally {
302 g.dispose();
336 // paint rectangular region between scrollbars
337 g.fillRect(w, h, vsbSpace, hsbSpace);
338 if (MARGIN > 0) {
339 draw3DRect(g, colors, 0, 0, w - 1, h - 1, false);
340 }
341 paintScrollBars(g, colors);
342 }
343 private void paintScrollBars(Graphics g, Color[] colors) {
344 if (vsbSpace > 0) {
345 paintVerScrollbar(g, colors, true);
346 // paint the whole scrollbar
347 }
348
349 if (hsbSpace > 0) {
350 paintHorScrollbar(g, colors, true);
351 // paint the whole scrollbar
352 }
353 }
354 void repaintScrollBars() {
355 Graphics g = getGraphics();
356 Color colors[] = getGUIcolors();
357 if (g != null) {
358 try {
359 paintScrollBars(g, colors);
360 } finally {
361 g.dispose();
362 }
363 }
364 }
365 public void repaintScrollbarRequest(XScrollbar sb) {
366 Graphics g = getGraphics();
367 Color colors[] = getGUIcolors();
368 if (g != null) {
369 try {
370 if (sb == vsb) {
371 paintVerScrollbar(g, colors, true);
372 } else if (sb == hsb) {
373 paintHorScrollbar(g, colors, true);
374 }
375 } finally {
376 g.dispose();
377 }
378 }
379 }
380 public void handleEvent(java.awt.AWTEvent e) {
381 super.handleEvent(e);
382
383 int id = e.getID();
384 switch(id) {
385 case PaintEvent.PAINT:
386 case PaintEvent.UPDATE:
387 repaintScrollBars();
388 break;
389 }
390 }
391
392
393 /**
394 * Paint the horizontal scrollbar to the screen
395 *
396 * @param g the graphics context to draw into
397 * @param colors the colors used to draw the scrollbar
398 * @param paintAll paint the whole scrollbar if true, just the thumb if false
399 */
400 void paintHorScrollbar(Graphics g, Color colors[], boolean paintAll) {
401 if (hsbSpace <= 0) {
402 return;
403 }
404 Graphics ng = g.create();
405 g.setColor(colors[BACKGROUND_COLOR]);
406
407 // SCROLLBAR is the height of scrollbar area
408 // but the actual scrollbar is SCROLLBAR-SPACE high;
409 // the rest must be filled with background color
410 int w = width - vsbSpace - (2*MARGIN);
411 g.fillRect(MARGIN, height-SCROLLBAR, w, SPACE);
412 g.fillRect(0, height-SCROLLBAR, MARGIN, SCROLLBAR);
413 g.fillRect(MARGIN + w, height-SCROLLBAR, MARGIN, SCROLLBAR);
414
415 try {
416 ng.translate(MARGIN, height - (SCROLLBAR - SPACE));
417 hsb.paint(ng, colors, paintAll);
418 }
419 finally {
420 ng.dispose();
421 }
422
423
424 }
425
426
427
428
429 /**
430 * Paint the vertical scrollbar to the screen
431 *
432 * @param g the graphics context to draw into
433 * @param colors the colors used to draw the scrollbar
434 * @param paintAll paint the whole scrollbar if true, just the thumb if false
435 */
436 void paintVerScrollbar(Graphics g, Color colors[], boolean paintAll) {
437 if (vsbSpace <= 0) {
438 return;
439 }
440 Graphics ng = g.create();
441 g.setColor(colors[BACKGROUND_COLOR]);
442
443 // SCROLLBAR is the width of scrollbar area
444 // but the actual scrollbar is SCROLLBAR-SPACE wide;
445 // the rest must be filled with background color
446 int h = height - hsbSpace - (2*MARGIN);
447 g.fillRect(width-SCROLLBAR, MARGIN, SPACE, h);
448 g.fillRect(width-SCROLLBAR, 0, SCROLLBAR, MARGIN);
449 g.fillRect(width-SCROLLBAR, MARGIN+h, SCROLLBAR, MARGIN);
450
451 try {
452 ng.translate(width - (SCROLLBAR - SPACE), MARGIN);
453 vsb.paint(ng, colors, paintAll);
454 }
455 finally {
456 ng.dispose();
|
1 /*
2 * Copyright (c) 2002, 2018, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
262 public void setScrollPosition(int x, int y) {
263 scroll(x, y, VERTICAL | HORIZONTAL);
264 }
265
266 void scroll(int x, int y, int flag) {
267 scroll(x, y, flag, AdjustmentEvent.TRACK);
268 }
269
270 /**
271 * Scroll the contents to position x, y
272 */
273 @SuppressWarnings("deprecation")
274 void scroll(int x, int y, int flag, int type) {
275 checkSecurity();
276 ScrollPane sp = (ScrollPane)target;
277 Component c = getScrollChild();
278 if (c == null) {
279 return;
280 }
281 int sx, sy;
282 Color[] colors = getGUIcolors();
283
284 if (sp.getScrollbarDisplayPolicy() == ScrollPane.SCROLLBARS_NEVER) {
285 sx = -x;
286 sy = -y;
287 } else {
288 Point p = c.location();
289 sx = p.x;
290 sy = p.y;
291
292 if ((flag & HORIZONTAL) != 0) {
293 hsb.setValue(Math.min(x, hsb.getMaximum()-hsb.getVisibleAmount()));
294 ScrollPaneAdjustable hadj = (ScrollPaneAdjustable)sp.getHAdjustable();
295 setAdjustableValue(hadj, hsb.getValue(), type);
296 sx = -(hsb.getValue());
297 Graphics g = getGraphics();
298 if (g != null) {
299 try {
300 paintHorScrollbar(g, colors, true);
301 } finally {
302 g.dispose();
336 // paint rectangular region between scrollbars
337 g.fillRect(w, h, vsbSpace, hsbSpace);
338 if (MARGIN > 0) {
339 draw3DRect(g, colors, 0, 0, w - 1, h - 1, false);
340 }
341 paintScrollBars(g, colors);
342 }
343 private void paintScrollBars(Graphics g, Color[] colors) {
344 if (vsbSpace > 0) {
345 paintVerScrollbar(g, colors, true);
346 // paint the whole scrollbar
347 }
348
349 if (hsbSpace > 0) {
350 paintHorScrollbar(g, colors, true);
351 // paint the whole scrollbar
352 }
353 }
354 void repaintScrollBars() {
355 Graphics g = getGraphics();
356 Color[] colors = getGUIcolors();
357 if (g != null) {
358 try {
359 paintScrollBars(g, colors);
360 } finally {
361 g.dispose();
362 }
363 }
364 }
365 public void repaintScrollbarRequest(XScrollbar sb) {
366 Graphics g = getGraphics();
367 Color[] colors = getGUIcolors();
368 if (g != null) {
369 try {
370 if (sb == vsb) {
371 paintVerScrollbar(g, colors, true);
372 } else if (sb == hsb) {
373 paintHorScrollbar(g, colors, true);
374 }
375 } finally {
376 g.dispose();
377 }
378 }
379 }
380 public void handleEvent(java.awt.AWTEvent e) {
381 super.handleEvent(e);
382
383 int id = e.getID();
384 switch(id) {
385 case PaintEvent.PAINT:
386 case PaintEvent.UPDATE:
387 repaintScrollBars();
388 break;
389 }
390 }
391
392
393 /**
394 * Paint the horizontal scrollbar to the screen
395 *
396 * @param g the graphics context to draw into
397 * @param colors the colors used to draw the scrollbar
398 * @param paintAll paint the whole scrollbar if true, just the thumb if false
399 */
400 void paintHorScrollbar(Graphics g, Color[] colors, boolean paintAll) {
401 if (hsbSpace <= 0) {
402 return;
403 }
404 Graphics ng = g.create();
405 g.setColor(colors[BACKGROUND_COLOR]);
406
407 // SCROLLBAR is the height of scrollbar area
408 // but the actual scrollbar is SCROLLBAR-SPACE high;
409 // the rest must be filled with background color
410 int w = width - vsbSpace - (2*MARGIN);
411 g.fillRect(MARGIN, height-SCROLLBAR, w, SPACE);
412 g.fillRect(0, height-SCROLLBAR, MARGIN, SCROLLBAR);
413 g.fillRect(MARGIN + w, height-SCROLLBAR, MARGIN, SCROLLBAR);
414
415 try {
416 ng.translate(MARGIN, height - (SCROLLBAR - SPACE));
417 hsb.paint(ng, colors, paintAll);
418 }
419 finally {
420 ng.dispose();
421 }
422
423
424 }
425
426
427
428
429 /**
430 * Paint the vertical scrollbar to the screen
431 *
432 * @param g the graphics context to draw into
433 * @param colors the colors used to draw the scrollbar
434 * @param paintAll paint the whole scrollbar if true, just the thumb if false
435 */
436 void paintVerScrollbar(Graphics g, Color[] colors, boolean paintAll) {
437 if (vsbSpace <= 0) {
438 return;
439 }
440 Graphics ng = g.create();
441 g.setColor(colors[BACKGROUND_COLOR]);
442
443 // SCROLLBAR is the width of scrollbar area
444 // but the actual scrollbar is SCROLLBAR-SPACE wide;
445 // the rest must be filled with background color
446 int h = height - hsbSpace - (2*MARGIN);
447 g.fillRect(width-SCROLLBAR, MARGIN, SPACE, h);
448 g.fillRect(width-SCROLLBAR, 0, SCROLLBAR, MARGIN);
449 g.fillRect(width-SCROLLBAR, MARGIN+h, SCROLLBAR, MARGIN);
450
451 try {
452 ng.translate(width - (SCROLLBAR - SPACE), MARGIN);
453 vsb.paint(ng, colors, paintAll);
454 }
455 finally {
456 ng.dispose();
|