/** * Synchronize the scrollbar with the image. If the transform is out of range, it will correct it. * This function considers only following factors :<b> transform, image size, client area</b>. */ public void syncScrollBars() { if (sourceImage == null) { redraw(); return; } AffineTransform af = transform; double sx = af.getScaleX(), sy = af.getScaleY(); double tx = af.getTranslateX(), ty = af.getTranslateY(); if (tx > 0) tx = 0; if (ty > 0) ty = 0; ScrollBar horizontal = getHorizontalBar(); horizontal.setIncrement((int) (getClientArea().width / 20)); horizontal.setPageIncrement(getClientArea().width); Rectangle imageBound = sourceImage.getBounds(); int cw = getClientArea().width, ch = getClientArea().height; if (imageBound.width * sx > cw) { /* image is wider than client area */ horizontal.setMaximum((int) (imageBound.width * sx)); horizontal.setEnabled(true); if (((int) -tx) > horizontal.getMaximum() - cw) tx = -horizontal.getMaximum() + cw; } else { /* image is narrower than client area */ horizontal.setEnabled(false); tx = (cw - imageBound.width * sx) / 2; // center if too small. } horizontal.setSelection((int) (-tx)); horizontal.setThumb((int) (getClientArea().width)); ScrollBar vertical = getVerticalBar(); vertical.setIncrement((int) (getClientArea().height / 20)); vertical.setPageIncrement((int) (getClientArea().height)); if (imageBound.height * sy > ch) { /* image is higher than client area */ vertical.setMaximum((int) (imageBound.height * sy)); vertical.setEnabled(true); if (((int) -ty) > vertical.getMaximum() - ch) ty = -vertical.getMaximum() + ch; } else { /* image is less higher than client area */ vertical.setEnabled(false); ty = (ch - imageBound.height * sy) / 2; // center if too small. } vertical.setSelection((int) (-ty)); vertical.setThumb((int) (getClientArea().height)); /* update transform. */ af = AffineTransform.getScaleInstance(sx, sy); af.preConcatenate(AffineTransform.getTranslateInstance(tx, ty)); transform = af; redraw(); }
@Test public void testRenderOrigin_SetByScrollbar() throws IOException { Composite content = new Composite(sc, SWT.NONE); sc.setContent(content); hScroll.setSelection(1); vScroll.setSelection(2); lca.renderChanges(sc); TestMessage message = Fixture.getProtocolMessage(); JsonArray expected = JsonArray.readFrom("[ 1, 2 ]"); assertEquals(expected, message.findSetProperty(sc, "origin")); }
@Test public void testPreserveValues() { RemoteAdapter adapter = WidgetUtil.getAdapter(sc); assertEquals(null, adapter.getPreserved(PROP_SHOW_FOCUSED_CONTROL)); hScroll.setSelection(23); vScroll.setSelection(42); sc.setShowFocusedControl(true); assertEquals(23, hScroll.getSelection()); assertEquals(42, vScroll.getSelection()); Rectangle rectangle = new Rectangle(12, 30, 20, 40); sc.setBounds(rectangle); Fixture.markInitialized(display); Fixture.preserveWidgets(); assertEquals(Boolean.TRUE, adapter.getPreserved(PROP_SHOW_FOCUSED_CONTROL)); }
/** * search next text * * @param widgetText text widget * @param widgetFind search text widget */ private void findNext(StyledText widgetText, Text widgetFind) { if (!widgetText.isDisposed()) { String findText = widgetFind.getText().toLowerCase(); if (!findText.isEmpty()) { // get cursor position int cursorIndex = widgetText.getCaretOffset(); // search int offset = -1; if (cursorIndex >= 0) { String text = widgetText.getText(); offset = (cursorIndex + 1 < text.length()) ? text.substring(cursorIndex + 1).toLowerCase().indexOf(findText) : -1; } if (offset >= 0) { int index = cursorIndex + 1 + offset; widgetText.setCaretOffset(index); widgetText.setSelection(index); widgetText.redraw(); int topIndex = widgetText.getTopIndex(); widgetLineNumbers.setTopIndex(topIndex); widgetVerticalScrollBar.setSelection(topIndex); } else { Widgets.flash(widgetFind); } } } }
private void appendLog(String text) { synchronized (console) { console.setText(console.getText() + text); ScrollBar verticalBar = console.getVerticalBar(); if (verticalBar != null) { verticalBar.setSelection(verticalBar.getMaximum()); } } }
/** * This method is responsible to set the scroll bar attributes so that they reflect the size of * the current image at the current zoom factor */ private void synchronizeScrollBars() { // Syncronizing only makes sense if there is a skin being drawn if (currentSkinImage != null) { // Retrieves the current image and client area sizes Rectangle imageBound = currentSkinImage.getBounds(); int cw = getClientArea().width; int ch = getClientArea().height; // Updates horizontal scroll bar attributes ScrollBar horizontal = getHorizontalBar(); horizontal.setIncrement((cw / 100)); horizontal.setPageIncrement((cw / 2)); horizontal.setMaximum(imageBound.width); horizontal.setThumb(cw); horizontal.setSelection(displayRectangle.x); // Updates vertical scroll bar attributes ScrollBar vertical = getVerticalBar(); vertical.setIncrement((ch / 100)); vertical.setPageIncrement((ch / 2)); vertical.setMaximum(imageBound.height); vertical.setThumb(ch); vertical.setSelection(displayRectangle.y); if (horizontal.getMaximum() > cw) // Image is wider than client area { horizontal.setEnabled(true); } else { horizontal.setEnabled(false); } if (vertical.getMaximum() > ch) // Image is wider than client area { vertical.setEnabled(true); } else { vertical.setEnabled(false); } } }
/** * Adjusts the scrollbars to give the effect of panning * * @param dx the relative adjustment in x-direction * @param dy the relative adjustment in y-direction */ private void pan(Point p) { Rectangle bounds = screenImage.getBounds(); ScrollBar hBar = getHorizontalBar(), vBar = getVerticalBar(); Rectangle barBounds = bounds; // new Rectangle(hBar.getMinimum(), vBar.getMinimum(), hBar.getMaximum() - // hBar.getMinimum(), vBar.getMaximum() - vBar.getMinimum()); int sdx = p.x * barBounds.width / bounds.width; int sdy = p.y * barBounds.height / bounds.height; // report back the remaining delta p.x = p.x - sdx * bounds.width / barBounds.width; p.y = p.y - sdy * bounds.height / barBounds.height; if (sdx != 0) { hBar.setSelection(hBar.getSelection() + sdx); scrollHorizontally(hBar); } if (sdy != 0) { vBar.setSelection(vBar.getSelection() + sdy); scrollVertically(vBar); } }
/** * set text * * @param lines lines * @param widgetLineNumbers number text widget * @param widgetText text widget * @param widgetHorizontalScrollBar horizontal scrollbar widget * @param widgetVerticalScrollBar horizontal scrollbar widget */ private void setText( String[] lines, StyledText widgetLineNumbers, StyledText widgetText, ScrollBar widgetHorizontalScrollBar, ScrollBar widgetVerticalScrollBar) { if (!widgetLineNumbers.isDisposed() && !widgetText.isDisposed() && !widgetVerticalScrollBar.isDisposed() && !widgetHorizontalScrollBar.isDisposed()) { StringBuilder lineNumbers = new StringBuilder(); StringBuilder text = new StringBuilder(); int lineNb = 1; int maxWidth = 0; // get text for (String line : lines) { lineNumbers.append(String.format("%d\n", lineNb)); lineNb++; text.append(line); text.append('\n'); maxWidth = Math.max(maxWidth, line.length()); } // set text widgetLineNumbers.setText(lineNumbers.toString()); widgetText.setText(text.toString()); widgetText.setCaretOffset(0); // set scrollbars widgetHorizontalScrollBar.setMinimum(0); widgetHorizontalScrollBar.setMaximum(maxWidth); widgetVerticalScrollBar.setMinimum(0); widgetVerticalScrollBar.setMaximum(lineNb - 1); // force redraw (Note: for some reason this is necessary to keep texts and scrollbars in sync) widgetLineNumbers.redraw(); widgetText.redraw(); widgetLineNumbers.update(); widgetText.update(); // show top widgetLineNumbers.setTopIndex(0); widgetLineNumbers.setSelection(0); widgetText.setTopIndex(0); widgetText.setCaretOffset(0); widgetVerticalScrollBar.setSelection(0); } }
@Override protected void updateLowerViewWidget(Object[] elements) { if (elements == null || elements.length < 1) { fTreeViewer.setInput(null); return; } fTreeViewer.setInput(elements[0]); ScrollBar bar = fTree.getVerticalBar(); if (bar != null) { bar.setSelection(0); } }
/** Restores the state of the filter actions */ public void restoreState(IMemento memento) { fMemberFilterActionGroup.restoreState(memento); getControl().setRedraw(false); refresh(); getControl().setRedraw(true); boolean showInherited = Boolean.valueOf(memento.getString(TAG_SHOWINHERITED)).booleanValue(); showInheritedMethods(showInherited); boolean showDefiningTypes = Boolean.valueOf(memento.getString(TAG_SORTBYDEFININGTYPE)).booleanValue(); sortByDefiningType(showDefiningTypes); ScrollBar bar = getTable().getVerticalBar(); if (bar != null) { Integer vScroll = memento.getInteger(TAG_VERTICAL_SCROLL); if (vScroll != null) { bar.setSelection(vScroll.intValue()); } } }
protected void doCalculations() { if (m_Model == null) { ScrollBar sb = getHorizontalBar(); if (sb != null) { sb.setMinimum(0); sb.setMaximum(1); sb.setPageIncrement(1); sb.setThumb(1); sb.setSelection(1); } sb = getVerticalBar(); if (sb != null) { sb.setMinimum(0); sb.setMaximum(1); sb.setPageIncrement(1); sb.setThumb(1); sb.setSelection(1); } return; } int m_HeaderHeight = m_Model.getFirstRowHeight(); int m_RowHeight = m_Model.getRowHeight(); Rectangle rect = getClientArea(); if (m_LeftColumn < m_Model.getFixedColumnCount()) { m_LeftColumn = m_Model.getFixedColumnCount(); } if (m_TopRow < m_Model.getFixedRowCount()) { m_TopRow = m_Model.getFixedRowCount(); } int fixedWidth = getFixedWidth(); int fixedHeight = m_HeaderHeight + (m_Model.getFixedRowCount() - 1) * m_Model.getRowHeight(); m_ColumnsVisible = 0; m_ColumnsFullyVisible = 0; if (m_Model.getColumnCount() > m_Model.getFixedColumnCount()) { int runningWidth = getColumnLeft(m_LeftColumn); for (int col = m_LeftColumn; col < m_Model.getColumnCount(); col++) { if (runningWidth < rect.width + rect.x) m_ColumnsVisible++; runningWidth += m_Model.getColumnWidth(col); if (runningWidth < rect.width + rect.x) m_ColumnsFullyVisible++; else break; } } ScrollBar sb = getHorizontalBar(); if (sb != null) { if (m_Model.getColumnCount() <= m_Model.getFixedColumnCount()) { sb.setMinimum(0); sb.setMaximum(1); sb.setPageIncrement(1); sb.setThumb(1); sb.setSelection(1); } else { sb.setMinimum(m_Model.getFixedColumnCount()); sb.setMaximum(m_Model.getColumnCount()); sb.setIncrement(1); sb.setPageIncrement(2); sb.setThumb(m_ColumnsFullyVisible); sb.setSelection(m_LeftColumn); } } m_RowsFullyVisible = Math.max(0, (rect.height - fixedHeight) / m_RowHeight); m_RowsFullyVisible = Math.min(m_RowsFullyVisible, m_Model.getRowCount() - m_Model.getFixedRowCount()); m_RowsFullyVisible = Math.max(0, m_RowsFullyVisible); m_RowsVisible = m_RowsFullyVisible + 1; if (m_TopRow + m_RowsFullyVisible > m_Model.getRowCount()) { m_TopRow = Math.max(m_Model.getFixedRowCount(), m_Model.getRowCount() - m_RowsFullyVisible); } if (m_TopRow + m_RowsFullyVisible >= m_Model.getRowCount()) { m_RowsVisible--; } sb = getVerticalBar(); if (sb != null) { if (m_Model.getRowCount() <= m_Model.getFixedRowCount()) { sb.setMinimum(0); sb.setMaximum(1); sb.setPageIncrement(1); sb.setThumb(1); sb.setSelection(1); } else { sb.setMinimum(m_Model.getFixedRowCount()); sb.setMaximum(m_Model.getRowCount()); sb.setPageIncrement(m_RowsVisible); sb.setIncrement(1); sb.setThumb(m_RowsFullyVisible); sb.setSelection(m_TopRow); } } }
private void triggerScrollbarSelectionChange(int newValue) { ScrollBar verticalBar = ((ScrolledComposite) itemUiList.getUiRoot()).getVerticalBar(); verticalBar.setSelection(newValue); SwtEventHelper.trigger(SWT.Selection).withDetail(newValue).on(verticalBar); }
protected void layout(Composite composite, boolean flushCache) { if (inLayout) { return; } ScrolledComposite sc = (ScrolledComposite) composite; if (sc.content == null) { return; } ScrollBar hBar = sc.getHorizontalBar(); ScrollBar vBar = sc.getVerticalBar(); if (hBar != null) { if (hBar.getSize().y >= sc.getSize().y) { return; } } if (vBar != null) { if (vBar.getSize().x >= sc.getSize().x) { return; } } inLayout = true; Rectangle contentRect = sc.content.getBounds(); if (!sc.alwaysShowScroll) { boolean hVisible = sc.needHScroll(contentRect, false); boolean vVisible = sc.needVScroll(contentRect, hVisible); if (!hVisible && vVisible) { hVisible = sc.needHScroll(contentRect, vVisible); } if (hBar != null) { hBar.setVisible(hVisible); } if (vBar != null) { vBar.setVisible(vVisible); } } Rectangle hostRect = sc.getClientArea(); if (sc.expandHorizontal) { contentRect.width = Math.max(sc.minWidth, hostRect.width); } if (sc.expandVertical) { contentRect.height = Math.max(sc.minHeight, hostRect.height); } if (hBar != null) { hBar.setMaximum(contentRect.width); hBar.setThumb(Math.min(contentRect.width, hostRect.width)); int hPage = contentRect.width - hostRect.width; int hSelection = hBar.getSelection(); if (hSelection >= hPage) { if (hPage <= 0) { hSelection = 0; hBar.setSelection(0); } contentRect.x = -hSelection; } } if (vBar != null) { vBar.setMaximum(contentRect.height); vBar.setThumb(Math.min(contentRect.height, hostRect.height)); int vPage = contentRect.height - hostRect.height; int vSelection = vBar.getSelection(); if (vSelection >= vPage) { if (vPage <= 0) { vSelection = 0; vBar.setSelection(0); } contentRect.y = -vSelection; } } sc.content.setBounds(contentRect); inLayout = false; }