From: Tay R. C. <rc...@us...> - 2005-07-24 00:14:33
|
Update of /cvsroot/actionstep/actionstep/src/org/actionstep In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv16315/actionstep/org/actionstep Modified Files: ASAlertPanel.as Log Message: reworked resizing and positioning and controls code Index: ASAlertPanel.as =================================================================== RCS file: /cvsroot/actionstep/actionstep/src/org/actionstep/ASAlertPanel.as,v retrieving revision 1.7 retrieving revision 1.8 diff -C2 -d -r1.7 -r1.8 *** ASAlertPanel.as 18 Jul 2005 03:01:03 -0000 1.7 --- ASAlertPanel.as 24 Jul 2005 00:13:48 -0000 1.8 *************** *** 32,437 **** import org.actionstep.NSApplication; import org.actionstep.NSButton; - import org.actionstep.NSTextField; - import org.actionstep.NSScrollView; import org.actionstep.NSRect; - import org.actionstep.NSControl; import org.actionstep.NSView; import org.actionstep.NSFont; ! import org.actionstep.NSImage; ! import org.actionstep.NSBox; import org.actionstep.NSSize; - //import org.actionstep.NSWindow; - import org.actionstep.NSScroller; import org.actionstep.NSPoint; - //import org.actionstep.NSColor; import org.actionstep.NSModalSession; - import org.actionstep.constants.NSBorderType; - import org.actionstep.constants.NSTitlePosition; - import org.actionstep.constants.NSTextAlignment; import org.actionstep.constants.NSAlertReturn; - //import org.actionstep.test.ASTestView; - - //below is from GNUStep - /* - These functions may be called "recursively". For example, from a - timed event. Therefore, there may be several alert panel active - at the same time, but only the first one will be THE - m_standardAlertPanel, which will not be released once finished - with, but which will be kept for future use. - - +---------+---------+---------+---------+---------+ - | std !=0 | std act | pan=std | pan=new | std=new | - +---------+---------+---------+---------+---------+ - a: | F | N/A | | X | X | - +---------+---------+---------+---------+---------+ - b: | V | F | X | | | - +---------+---------+---------+---------+---------+ - c: | V | V | | X | | - +---------+---------+---------+---------+---------+ - */ - - - /* - TODO: Check if this discrepancy is wanted and needed. - If not, we could merge these parameters, even - for the alert panel, setting its window title to "Alert". - */ - class org.actionstep.ASAlertPanel extends NSPanel { ! private var m_app:NSApplication; ! private static var m_standardAlertPanel:ASAlertPanel; private static var m_informationalAlertPanel:ASAlertPanel; private static var m_criticalAlertPanel:ASAlertPanel; ! private static var m_def:NSButton; ! private static var m_alt:NSButton; ! private static var m_oth:NSButton; ! private static var m_ico:NSButton; ! private static var m_titleField:NSTextField; ! private static var m_msgField:NSTextField; ! private static var m_scroll:NSScrollView; ! private static var m_result:NSAlertReturn; ! private static var m_green:Boolean; ! private static var m_defTitle:String="Alert"; ! private static var m_callback:Object; ! private static var m_selector:String; ! // TODO: Check this value. ! private static var WTitleHeight:Number = 0; ! private static var WinMinWidth:Number = 400;//362 ! private static var WinMinHeight:Number = 161; ! private static var IconSide:Number = 48; ! // from the top of the window. ! private static var IconBottom:Number = -56; ! private static var IconLeft:Number = 8; ! private static var TitleLeft:Number = 64; ! private static var TitleMinRight:Number = 8; ! private static var LineHeight:Number = 2; ! // from the top of the window. ! private static var LineBottom:Number = -66; ! private static var LineLeft:Number = 0; ! // in either direction. ! //private static var ScrollMinSize:Number = 48; private static var MessageFont:NSFont; //messageFontOfSize(14); - // 5 is too little margin. - private static var MessageHorzMargin:Number = 8; - //private static var MessageMinHeight:Number = 20; - // from the top of the buttons. - private static var MessageVertMargin:Number = 6; - // from the top of the window; - private static var MessageTop:Number = -72; - - // from the bottom of the window. - private static var ButtonBottom:Number = 8; - private static var ButtonMargin:Number = 8; - private static var ButtonInterspace:Number = 10; - private static var ButtonMinHeight:Number = 24; - private static var ButtonMinWidth:Number = 72; ! public function init ():ASAlertPanel { //super.init(); ! m_app = NSApplication.sharedApplication(); ! MessageFont = NSFont.systemFontOfSize(14); ! var rect:NSRect = NSRect.ZeroRect, ! image:NSImage, ! box:NSBox, ! r:NSRect = new NSRect(0, 0, WinMinWidth, WinMinHeight), ! titleFont:NSFont = NSFont.systemFontOfSize(18); ! ! var titleHeight:Number = titleFont.pointSize(); //assume in pixels ! ! initWithContentRectStyleMask(r, NSTitledWindowMask); ! setTitle(""); - - var content:NSView = contentView(); // we're an ATTENTION panel, therefore: //setHidesOnDeactivate(false); setBecomesKeyOnlyIfNeeded(false); ! // Title ! rect.size.height = 0; // will be sized to fit anyway. ! rect.size.width = 0; // will be sized to fit anyway. ! rect.origin.y = r.origin.y + r.size.height + IconBottom + (IconSide - titleHeight)/2;; ! rect.origin.x = TitleLeft; ! var m_titleField:NSTextField = (new NSTextField()).initWithFrame(rect); ! //m_titleField.setAutoresizingMask(NSViewMinYMargin); ! m_titleField.setEditable(true); ! m_titleField.setSelectable(true); ! m_titleField.setBezeled(false); ! m_titleField.setDrawsBackground(false); ! m_titleField.setStringValue(""); ! m_titleField.setFont(titleFont); ! // Horizontal line ! rect.size.height = LineHeight; ! rect.size.width = r.size.width; ! rect.origin.y = r.origin.y + r.size.height + LineBottom; ! rect.origin.x = LineLeft; ! box = (new NSBox()).initWithFrame(rect); ! //box.setAutoresizingMask(NSViewWidthSizable | NSViewMinYMargin); ! box.setTitlePosition(NSTitlePosition.NSNoTitle); ! box.setBorderType(NSBorderType.NSGrooveBorder); ! content.addSubview(box); ! // Then, make the subviews that'll be sized by sizePanelToFit; ! rect.reset(0, 0, 0, 0); ! m_msgField = (new NSTextField()).initWithFrame(rect); ! m_msgField.setEditable(true); ! m_msgField.setSelectable(false); ! m_msgField.setBezeled(false); ! m_msgField.setDrawsBackground(false); ! //m_msgField.setBackgroundColor(NSColor.controlBackgroundColor()); ! m_msgField.setAlignment(NSTextAlignment.NSCenterTextAlignment); ! m_msgField.setStringValue(""); ! m_msgField.setFont(MessageFont); ! m_def = _makeButtonWithRect(rect); ! //m_def.setKeyEquivalent("\r"); ! //m_def.setHighlightsBy(NSPushInCellMask | NSChangeGrayCellMask | NSContentsCellMask); ! //m_def.setImagePosition(NSCellImagePosition.NSImageRight); ! //m_def.setImage(NSImage.imageNamed("common_ret")); ! //m_def.setAlternateImage(NSImage.imageNamed("common_retH")); ! m_alt = _makeButtonWithRect(rect); ! m_oth = _makeButtonWithRect(rect); ! rect.size.height = 80; ! m_scroll = makeScrollViewWithRect(rect); ! m_result = NSAlertReturn.NSError; ! m_green = true; ! return this; ! } ! ! public static function makeScrollViewWithRect(rect:NSRect):NSScrollView { ! var lineHeight:Number = MessageFont.pointSize(); ! var m_scroll:NSScrollView = (new NSScrollView()).initWithFrame(rect); ! ! m_scroll.setBorderType(NSBorderType.NSLineBorder); ! //m_scroll.setBackgroundColor(NSColor.controlBackgroundColor()); ! m_scroll.setHasHorizontalScroller(true) ! m_scroll.setHasVerticalScroller(true); ! m_scroll.setScrollsDynamically(true); ! m_scroll.setLineScroll(lineHeight); ! m_scroll.setPageScroll(lineHeight*10); ! return m_scroll; } ! public function _makeButtonWithRect (rect:NSRect):NSButton { var button:NSButton = (new NSButton()).initWithFrame(rect) - //button.setAutoresizingMask(NSViewMinXMargin | NSViewMaxYMargin) //button.setButtonType(NSButtonType.NSMomentaryPushButton); - button.setTitle(""); button.setTarget(this); button.setAction("buttonAction"); ! return button; } ! //macro ! private static function useControl(control:NSView):Boolean { ! return (control.superview()!=null) ! } ! ! public static function setControl(content:NSView, control:NSControl, title:String):Void { ! //orginal uses respondsToSelector ! if (title != null) { ! if (control["setTitle"]!=null) { ! control["setTitle"](title); ! } else if (control["setStringValue"]!=null) { ! control["setStringValue"](title); ! } ! control.sizeToFit(); ! if (!useControl(control)) { ! content.addSubview(control); ! } ! } else if (useControl(control)) { ! control.removeFromSuperview(); ! } ! } ! ! public function sizePanelToFit():Void { ! var bounds:NSRect, ! ssize:NSSize = new NSSize(0, 0), // screen size (corrected). ! bsize:NSSize = new NSSize(0, 0), // button size (max of the three). ! wsize:NSSize = new NSSize(0, 0), // window size (computed). ! content:NSView, ! buttons:Array=[], ! position:Number = 0, ! numberOfButtons:Number, ! needsScroll:Boolean, ! couldNeedScroll:Boolean, ! mask:Number = styleMask(); ! ! /* ! * Set size to the size of a content rectangle of a panel ! * that completely fills the screen. ! */ ! bounds = new NSRect(0, 0, Stage.width, Stage.height); ! ssize = bounds.size; ! ! // Let's size the title. ! if (useControl(m_titleField)) { ! var rect:NSRect = m_titleField.frame(); ! var width:Number = TitleLeft + rect.size.width + TitleMinRight; ! if (wsize.width < width) { ! wsize.width = width; ! // ssize.width < width = > the title will be silently clipped. ! } ! } ! ! wsize.height = -LineBottom; ! ! // Let's count the buttons. ! bsize.width = ButtonMinWidth; ! bsize.height = ButtonMinHeight; ! buttons[0] = m_def; ! buttons[1] = m_alt; ! buttons[2] = m_oth; ! numberOfButtons = 0; ! var i = buttons.length; ! while(i--) { ! if (useControl(buttons[i])) { ! var rect:NSRect = buttons[i].frame(); ! if (bsize.width < rect.size.width) ! bsize.width = rect.size.width; ! if (bsize.height < rect.size.height) ! bsize.height = rect.size.height; ! numberOfButtons++; ! } ! } ! ! if (numberOfButtons > 0) { ! // (with NSGetAlertPanel, there could be zero buttons). ! var width:Number = (bsize.width + ButtonInterspace) * numberOfButtons - ButtonInterspace + ButtonMargin * 2; ! /* ! * If the buttons are too wide or too high to fit in the screen, ! * then too bad! Thought it would be simple enough to put them ! * in the m_scroll view with the m_msgField. ! * TODO: See if we raise an exception here or if we let the ! * QA people detect this kind of problem. ! */ ! if (wsize.width < width) wsize.width = width; ! wsize.height += ButtonBottom + bsize.height; ! } ! ! // Let's see the size of the m_msgField and how to place it. ! needsScroll = false; ! couldNeedScroll = useControl(m_msgField); ! if (couldNeedScroll) { ! var rect:NSRect = m_msgField.frame() ! var width:Number = rect.size.width + 2*MessageHorzMargin; ! ! if (wsize.width < width) wsize.width = width; ! // The title could be large too, without implying a m_scroll view. ! needsScroll = (ssize.width < width); ! /* ! * But only the m_msgField can impose a great height, therefore ! * we check it along in the next paragraph. ! */ ! wsize.height += rect.size.height + 2 * MessageVertMargin; ! } else { ! wsize.height += MessageVertMargin; ! } ! ! // Strategically placed here, we resize the window. ! if (ssize.height < wsize.height) { ! wsize.height = ssize.height; ! needsScroll = couldNeedScroll; ! } else if (wsize.height < WinMinHeight) { ! wsize.height = WinMinHeight; ! } ! if (needsScroll) { ! wsize.width += Number(NSScroller.scrollerWidth + 4); ! } ! if (ssize.width < wsize.width) { ! wsize.width = ssize.width; ! } else if (wsize.width < WinMinWidth) { ! wsize.width = WinMinWidth; ! } ! bounds = new NSRect(0, 0, wsize.width, wsize.height); ! //bounds = NSWindow.contentRectForFrameRectStyleMask(bounds, mask); ! //setMaxSize(bounds.size); ! //setMinSize(bounds.size); ! setContentSize(wsize); ! content = contentView(); ! bounds = content.bounds(); ! TRACE(bounds); ! TRACE(frame()); ! ! // now we can place the buttons. ! if (numberOfButtons > 0) { ! position = bounds.origin.x + bounds.size.width - ButtonMargin; ! for (i = 0; i < 3; i++) { ! if (useControl(buttons[i])) { ! var rect:NSRect = NSRect.ZeroRect; ! position -= bsize.width; ! rect.origin.x = position; ! rect.origin.y = bounds.maxY() - (bsize.height*2 + ButtonBottom); ! rect.size.width = bsize.width; ! rect.size.height = bsize.height; ! buttons[i].setFrame(rect); ! position -= ButtonInterspace; ! } ! } ! } ! ! // Finaly, place the message. ! if (useControl(m_msgField)) { ! var mrect:NSRect = m_msgField.frame(); ! if (needsScroll) { ! var srect:NSRect = NSRect.ZeroRect; ! ! // The m_scroll view takes all the space that is available. ! srect.origin.x = bounds.origin.x + MessageHorzMargin; ! if (numberOfButtons > 0) { ! srect.origin.y = bounds.origin.y + ButtonBottom + bsize.height + MessageVertMargin; ! } else { ! srect.origin.y = bounds.origin.y + MessageVertMargin; ! } ! srect.size.width = bounds.size.width - 2 * MessageHorzMargin; ! srect.size.height = bounds.origin.y + bounds.size.height + MessageTop - srect.origin.y; ! m_scroll.setFrame(srect) ! if (!useControl(m_scroll)) content.addSubview(m_scroll); ! m_msgField.removeFromSuperview(); ! mrect.origin.x = srect.origin.x + srect.size.width - mrect.size.width; ! mrect.origin.y = srect.origin.y + srect.size.height - mrect.size.height; ! m_msgField.setFrame(mrect); ! m_scroll.setDocumentView(m_msgField); ! (m_scroll.contentView()).scrollToPoint( ! new NSPoint (mrect.origin.x, mrect.origin.y + mrect.size.height - (m_scroll.contentView()).bounds().size.height)); ! m_scroll.reflectScrolledClipView(m_scroll.contentView()); ! } else { ! /* ! * We must center vertically the m_msgField because ! * the window has a minimum size, thus may be greated ! * than expected. ! */ ! mrect.origin.x = (wsize.width - mrect.size.width)/2; ! var vmargin:Number = bounds.size.height + LineBottom-mrect.size.height; ! ! if (numberOfButtons > 0) vmargin -= ButtonBottom + bsize.height; ! ! vmargin/= 2; // if negative, it'll bite up and down. ! mrect.origin.y = bounds.origin.y + vmargin; ! if (numberOfButtons > 0) mrect.origin.y += ButtonBottom + bsize.height; ! m_msgField.setFrame(mrect); ! } ! } else if (useControl(m_scroll)) { ! m_scroll.removeFromSuperview(); ! } ! ! m_green = false; ! content.display(); } --- 32,218 ---- import org.actionstep.NSApplication; import org.actionstep.NSButton; import org.actionstep.NSRect; import org.actionstep.NSView; import org.actionstep.NSFont; ! //import org.actionstep.NSImage; import org.actionstep.NSSize; import org.actionstep.NSPoint; import org.actionstep.constants.NSAlertReturn; class org.actionstep.ASAlertPanel extends NSPanel { + private static var g_init:Boolean; ! private static var m_app:NSApplication ! private static var m_defTitle:String="Alert"; ! ; private static var m_standardAlertPanel:ASAlertPanel; private static var m_informationalAlertPanel:ASAlertPanel; private static var m_criticalAlertPanel:ASAlertPanel; ! private var m_def:NSButton; ! private var m_alt:NSButton; ! private var m_oth:NSButton; ! private var m_ico:NSButton; ! private var m_btns:Array; ! //private var m_titleField:NSTextField; ! private var m_msgField:TextField; ! private var m_msg:String; ! private var m_infoField:TextField; ! private var m_info:String; ! private var m_result:NSAlertReturn; ! private var m_green:Boolean; ! ! private var m_callback:Object; ! private var m_selector:String; + // TODO: Check this value. + private static var WinPos:NSPoint; + private static var WinSize:NSSize; private static var MessageFont:NSFont; //messageFontOfSize(14); ! private static var IconLeft:Number = 24; ! private static var IconRight:Number = 16; ! private static var IconTop:Number = 15; ! ! private static var BtnTop:Number = 10; ! private static var BtnBottom:Number = 20; ! private static var BtnRight:Number = 24; ! private static var BtnMinHeight:Number = 24; ! private static var BtnMinWidth:Number = 72; ! private static var BtnInterspace:Number = 12; ! ! private static var MsgWidth = 200; ! private static var InfoWidth = 170; ! private static var InfoTop:Number = 8; ! private static var TFDiff = MsgWidth-InfoWidth; ! ! public function init():ASAlertPanel { //super.init(); ! var rect:NSRect = NSRect.ZeroRect; ! initWithContentRectStyleMask(rect, NSTitledWindowMask); setTitle(""); // we're an ATTENTION panel, therefore: //setHidesOnDeactivate(false); setBecomesKeyOnlyIfNeeded(false); + m_result = NSAlertReturn.NSError; + m_green = true; ! //Icon will never change position ! var content:NSView = contentView(); ! //image is screwed ! //var image:NSImage = NSImage.imageNamed("ASAlertIcon"); ! var isize:NSSize = new NSSize(64, 64); ! rect.reset(IconLeft, IconTop, isize.width, isize.height); ! m_ico = (new NSButton()).initWithFrame(rect); ! m_ico.setBordered(false); ! //m_ico.setImagePosition(NSCellImagePosition.NSImageOnly); ! //m_ico.setImage(image); ! m_ico.setTitle("Ico"); ! TRACE(m_ico); ! TRACE(content); ! TRACE(content.subviews()); ! content.addSubview(m_ico); ! ! //position will be set later ! rect.reset(0, 0, BtnMinWidth, BtnMinHeight); ! m_def = makeButtonWithRect(rect); ! //m_def.setKeyEquivalent("\r"); ! m_alt = makeButtonWithRect(rect); ! m_oth = makeButtonWithRect(rect); ! m_btns = [m_def, m_alt, m_oth]; ! ! if(g_init==null) { ! m_app = NSApplication.sharedApplication(); ! WinPos = new NSPoint(100, 100); ! WinSize = new NSSize(MsgWidth+isize.width+IconLeft+IconRight+BtnRight, 200); ! MessageFont = NSFont.systemFontOfSize(14); ! g_init = true; ! } ! setFrame(rect); ! return this; ! } ! private function positionElements() { ! var rect:NSRect = NSRect.ZeroRect; ! if(m_msgField==null) { //assume that m_infoField is also null ! var fmt:TextFormat = MessageFont.textFormat(); ! //position will be set later ! rect.origin.x = m_ico.frame().maxX() + IconRight; ! rect.origin.y = IconTop; ! rect.size.width = MsgWidth ! m_msgField = makeTF(rect, "m_msgField"); ! fmt.bold = true; ! m_msgField.setTextFormat(fmt); ! ! rect.size.width = InfoWidth ! m_infoField = makeTF(rect, "m_infoField"); ! fmt.bold = false; ! m_infoField.setTextFormat(fmt); ! } ! m_msgField.text = m_msg; ! m_infoField.text = m_info; ! var w:Number=0, i, btn; ! for(i in m_btns) { ! btn = m_btns[i]; ! w+=btn.frame().size.width; ! } ! w+= BtnInterspace * (m_btns.length-1) + IconLeft + BtnRight; ! if(w>m_msgField._width+m_msgField._x+BtnRight) { ! m_msgField._width = w-m_msgField._x-BtnRight; ! m_infoField._width = m_msgField._width-TFDiff; ! } ! m_infoField._y = m_msgField._height+m_msgField._y+InfoTop; ! var pt:NSPoint = new NSPoint(0, m_infoField._y+m_infoField._height+BtnTop); ! var icoy = m_ico.frame().maxY()+BtnTop ! if(pt.y<icoy) pt.y = icoy; ! w-=IconLeft-m_ico.frame().size.width+BtnRight ! for(i=0;i<3;i++) { ! btn = m_btns[i]; ! w-=btn.frame().size.width; ! pt.x = w; ! btn.setFrameOrigin(pt); ! w-=BtnInterspace; ! } ! //resize window ! rect.origin = WinPos.copy(); ! rect.size.width = m_msgField._width + m_msgField._x + BtnRight; ! rect.size.height = m_def.frame().maxY() + BtnBottom + rootView().titleRect().size.height; ! setFrame(rect); } ! private function makeButtonWithRect (rect:NSRect):NSButton { var button:NSButton = (new NSButton()).initWithFrame(rect) //button.setButtonType(NSButtonType.NSMomentaryPushButton); button.setTarget(this); button.setAction("buttonAction"); ! contentView().addSubview(button) ! return button; } ! private function makeTF(rect:NSRect, title:String):TextField { ! var content:NSView = contentView(); ! var mc:MovieClip = content.mcBounds(); ! //location is not impt ! mc.createTextField(title, mc.getNextHighestDepth(), rect.origin.x, rect.origin.y, rect.size.width, rect.size.height); ! var txt:TextField = mc[title] ! txt.type = "dynamic"; ! txt.selectable = true; ! txt.wordWrap = true; ! txt.autoSize = true; ! txt.border = true; ! return txt; } *************** *** 463,467 **** public function runModal(call:Object, sel:String):Void { ! if (m_green) sizePanelToFit(); m_callback = call; --- 244,248 ---- public function runModal(call:Object, sel:String):Void { ! //if (m_green) sizePanelToFit(); m_callback = call; *************** *** 477,495 **** } ! public function setTitleMessageDefAltOther (title:String, message:String, defaultButton:String, alternateButton:String, otherButton:String):Void { ! var content:NSView = contentView(); ! ! setControl(content, m_titleField, title); ! if (useControl(m_scroll)) { ! // TODO: Remove the following line once NSView is corrected. ! m_scroll.setDocumentView(null); ! m_scroll.removeFromSuperview(); ! m_msgField.removeFromSuperview(); } - setControl(content, m_msgField, message); - setControl(content, m_def, defaultButton); - setControl(content, m_alt, alternateButton); - setControl(content, m_oth, otherButton); if (useControl(m_def)) { --- 258,274 ---- } ! //macro ! private static function useControl(control:NSView):Boolean { ! return (control.superview()!=null) ! } ! ! private function setTitleMessageDefAltOther (title:String, message:String, defaultButton:String, alternateButton:String, otherButton:String):Void { ! setTitle(title); ! m_msg = message; ! m_info = "Not supported yet."; ! for(var i in m_btns) { ! m_btns[i].setTitle(arguments[parseInt(i)+2]); } if (useControl(m_def)) { *************** *** 540,544 **** } ! public static function getSomePanel( instance:ASAlertPanel, m_defTitle:String, --- 319,323 ---- } ! private static function getSomePanel( instance:ASAlertPanel, m_defTitle:String, *************** *** 568,576 **** panel.setTitleMessageDefAltOther (title, message, defaultButton, alternateButton, otherButton); - panel.sizePanelToFit(); return panel; } public static function NSGetAlertPanel( title:String, --- 347,359 ---- panel.setTitleMessageDefAltOther (title, message, defaultButton, alternateButton, otherButton); return panel; } + public function display() { + super.display(); + positionElements(); + } + public static function NSGetAlertPanel( title:String, |