Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1403,7 +1403,9 @@ LRESULT wmNotifyChild (NMHDR hdr, long wParam, long lParam) {
gc.dispose ();
}

left += isRadioOrCheck() ? radioOrCheckTextPadding : 0;
left += isRadioOrCheck()
? radioOrCheckTextPadding
: image != null ? MARGIN : 0;
RECT textRect = new RECT ();
OS.SetRect (textRect, left, nmcd.top + border, right, nmcd.bottom - border);

Expand All @@ -1420,20 +1422,14 @@ LRESULT wmNotifyChild (NMHDR hdr, long wParam, long lParam) {
}
OS.DrawText(nmcd.hdc, buffer, buffer.length, textRect, flags | OS.DT_CALCRECT);
OS.OffsetRect(textRect, 0, Math.max(0, (nmcd.bottom - textRect.bottom - border) / 2));
if (image != null) {
// The default button with an image doesn't respect the text alignment. So we do the same for styled buttons.
flags |= OS.DT_LEFT;
if (!isRadioOrCheck()) {
OS.OffsetRect(textRect, Math.max(MARGIN, (right - textRect.right) / 2 + 1), 0);
}
} else if ((style & SWT.LEFT) != 0) {
if ((style & SWT.LEFT) != 0) {
flags |= OS.DT_LEFT;
} else if ((style & SWT.RIGHT) != 0) {
flags |= OS.DT_RIGHT;
OS.OffsetRect(textRect, right - textRect.right, 0);
OS.OffsetRect(textRect, right - textRect.right - 2, 0);
} else {
flags |= OS.DT_CENTER;
OS.OffsetRect(textRect, (right - textRect.right) / 2, 0);
OS.OffsetRect(textRect, (right - textRect.right) / 2 - 1, 0);
}
OS.SetBkMode(nmcd.hdc, OS.TRANSPARENT);
OS.SetTextColor(nmcd.hdc, foreground);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package org.eclipse.swt.tests.manual;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

Expand All @@ -20,36 +22,27 @@ public static void main(String[] args) {
Image image = new Image(display, "data/eclipse32.png");

// Create a button with text and image
Button button;
button = new Button(shell, SWT.PUSH);
button.setText("Right Text");
button.setImage(image);
button.setSize(200, 50);
button.setAlignment(SWT.RIGHT);
button.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
Button btnRight = createButton("Right Text", image, shell);
btnRight.setAlignment(SWT.RIGHT);

button = new Button(shell, SWT.PUSH);
button.setText("Left Text");
button.setImage(image);
button.setSize(200, 50);
button.setAlignment(SWT.LEFT);
button.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
Button btnLeft = createButton("Left Text", image, shell);
btnLeft.setAlignment(SWT.LEFT);

button = new Button(shell, SWT.PUSH);
button.setText("Center Text");
button.setImage(image);
button.setSize(200, 50);
button.setAlignment(SWT.CENTER);
Button btnCenter = createButton("Center Text", image, shell);
btnCenter.setAlignment(SWT.CENTER);

// Set layout data to take up the whole shell
button.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
Button btnNoAlign = createButton("No Alignment assumes CENTER", image, shell);

button = new Button(shell, SWT.PUSH);
button.setText("No Alignment assumes CENTER");
button.setImage(image);
button.setSize(200, 50);
//button.setAlignment(SWT.CENTER);
button.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
Button enabledCheckbox = new Button(shell, SWT.CHECK);
enabledCheckbox.setText("Enable");
enabledCheckbox.setSelection(true);
enabledCheckbox.addListener(SWT.Selection, event -> {
boolean enabled = enabledCheckbox.getSelection();
btnRight.setEnabled(enabled);
btnLeft.setEnabled(enabled);
btnCenter.setEnabled(enabled);
btnNoAlign.setEnabled(enabled);
});

shell.setSize(500, 300);
shell.open();
Expand All @@ -64,4 +57,14 @@ public static void main(String[] args) {
image.dispose();
display.dispose();
}

private static Button createButton(String text, Image image, Composite parent) {
Button button = new Button(parent, SWT.PUSH);
button.setText(text);
button.setImage(image);
button.setForeground(new Color(0, 0, 128));
button.setSize(200, 50);
button.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
return button;
}
}
Loading