1 11 package org.eclipse.swt.widgets; 12 13 14 import org.eclipse.swt.internal.win32.*; 15 import org.eclipse.swt.*; 16 17 37 public class MessageBox extends Dialog { 38 String message = ""; 39 40 53 public MessageBox (Shell parent) { 54 this (parent, SWT.OK | SWT.ICON_INFORMATION | SWT.APPLICATION_MODAL); 55 } 56 57 80 public MessageBox (Shell parent, int style) { 81 super (parent, checkStyle (style)); 82 checkSubclass (); 83 } 84 85 static int checkStyle (int style) { 86 if ((style & (SWT.PRIMARY_MODAL | SWT.APPLICATION_MODAL | SWT.SYSTEM_MODAL)) == 0) style |= SWT.APPLICATION_MODAL; 87 int mask = (SWT.YES | SWT.NO | SWT.OK | SWT.CANCEL | SWT.ABORT | SWT.RETRY | SWT.IGNORE); 88 int bits = style & mask; 89 if (bits == SWT.OK || bits == SWT.CANCEL || bits == (SWT.OK | SWT.CANCEL)) return style; 90 if (bits == SWT.YES || bits == SWT.NO || bits == (SWT.YES | SWT.NO) || bits == (SWT.YES | SWT.NO | SWT.CANCEL)) return style; 91 if (bits == (SWT.RETRY | SWT.CANCEL) || bits == (SWT.ABORT | SWT.RETRY | SWT.IGNORE)) return style; 92 style = (style & ~mask) | SWT.OK; 93 return style; 94 } 95 96 103 public String getMessage () { 104 return message; 105 } 106 107 119 public int open () { 120 121 122 int buttonBits = 0; 123 if ((style & SWT.OK) == SWT.OK) buttonBits = OS.MB_OK; 124 if ((style & (SWT.OK | SWT.CANCEL)) == (SWT.OK | SWT.CANCEL)) buttonBits = OS.MB_OKCANCEL; 125 if ((style & (SWT.YES | SWT.NO)) == (SWT.YES | SWT.NO)) buttonBits = OS.MB_YESNO; 126 if ((style & (SWT.YES | SWT.NO | SWT.CANCEL)) == (SWT.YES | SWT.NO | SWT.CANCEL)) buttonBits = OS.MB_YESNOCANCEL; 127 if ((style & (SWT.RETRY | SWT.CANCEL)) == (SWT.RETRY | SWT.CANCEL)) buttonBits = OS.MB_RETRYCANCEL; 128 if ((style & (SWT.ABORT | SWT.RETRY | SWT.IGNORE)) == (SWT.ABORT | SWT.RETRY | SWT.IGNORE)) buttonBits = OS.MB_ABORTRETRYIGNORE; 129 if (buttonBits == 0) buttonBits = OS.MB_OK; 130 131 int iconBits = 0; 132 if ((style & SWT.ICON_ERROR) != 0) iconBits = OS.MB_ICONERROR; 133 if ((style & SWT.ICON_INFORMATION) != 0) iconBits = OS.MB_ICONINFORMATION; 134 if ((style & SWT.ICON_QUESTION) != 0) iconBits = OS.MB_ICONQUESTION; 135 if ((style & SWT.ICON_WARNING) != 0) iconBits = OS.MB_ICONWARNING; 136 if ((style & SWT.ICON_WORKING) != 0) iconBits = OS.MB_ICONINFORMATION; 137 138 139 int modalBits = 0; 140 if (OS.IsWinCE) { 141 if ((style & (SWT.PRIMARY_MODAL | SWT.APPLICATION_MODAL | SWT.SYSTEM_MODAL)) != 0) { 142 modalBits = OS.MB_APPLMODAL; 143 } 144 } else { 145 if ((style & SWT.PRIMARY_MODAL) != 0) modalBits = OS.MB_APPLMODAL; 146 if ((style & SWT.APPLICATION_MODAL) != 0) modalBits = OS.MB_TASKMODAL; 147 if ((style & SWT.SYSTEM_MODAL) != 0) modalBits = OS.MB_SYSTEMMODAL; 148 } 149 150 int bits = buttonBits | iconBits | modalBits; 151 if ((style & SWT.RIGHT_TO_LEFT) != 0) bits |= OS.MB_RTLREADING; 152 if ((style & (SWT.LEFT_TO_RIGHT | SWT.RIGHT_TO_LEFT)) == 0) { 153 if (parent != null && (parent.style & SWT.MIRRORED) != 0) { 154 bits |= OS.MB_RTLREADING; 155 } 156 } 157 158 163 if ((bits & OS.MB_SYSTEMMODAL) != 0) { 164 bits |= OS.MB_TASKMODAL; 165 bits &= ~OS.MB_SYSTEMMODAL; 166 167 bits |= OS.MB_TOPMOST; 168 } 169 170 178 int hwndOwner = parent != null ? parent.handle : 0; 179 Shell oldModal = null; 180 Display display = null; 181 if ((bits & OS.MB_TASKMODAL) != 0) { 182 display = parent.getDisplay (); 183 oldModal = display.getModalDialogShell (); 184 display.setModalDialogShell (parent); 185 } 186 187 188 189 TCHAR buffer1 = new TCHAR (0, message, true); 190 TCHAR buffer2 = new TCHAR (0, title, true); 191 int code = OS.MessageBox (hwndOwner, buffer1, buffer2, bits); 192 193 194 if ((bits & OS.MB_TASKMODAL) != 0) { 195 display.setModalDialogShell (oldModal); 196 } 197 198 204 206 207 if (code != 0) { 208 int type = bits & 0x0F; 209 if (type == OS.MB_OK) return SWT.OK; 210 if (type == OS.MB_OKCANCEL) { 211 return (code == OS.IDOK) ? SWT.OK : SWT.CANCEL; 212 } 213 if (type == OS.MB_YESNO) { 214 return (code == OS.IDYES) ? SWT.YES : SWT.NO; 215 } 216 if (type == OS.MB_YESNOCANCEL) { 217 if (code == OS.IDYES) return SWT.YES; 218 if (code == OS.IDNO) return SWT.NO; 219 return SWT.CANCEL; 220 } 221 if (type == OS.MB_RETRYCANCEL) { 222 return (code == OS.IDRETRY) ? SWT.RETRY : SWT.CANCEL; 223 } 224 if (type == OS.MB_ABORTRETRYIGNORE) { 225 if (code == OS.IDRETRY) return SWT.RETRY; 226 if (code == OS.IDABORT) return SWT.ABORT; 227 return SWT.IGNORE; 228 } 229 } 230 return SWT.CANCEL; 231 } 232 233 244 public void setMessage (String string) { 245 if (string == null) error (SWT.ERROR_NULL_ARGUMENT); 246 message = string; 247 } 248 249 } 250 | Popular Tags |