Package totalcross.ui

Class Window

  • Direct Known Subclasses:
    CalculatorBox, CalendarBox, ColorChooserBox, ComboBoxDropDown, CompareBox, ControlBox, FileChooserBox, InputBox, KeyboardBox, MainWindow, MenuBar, MenuBarDropDown, MessageBox, PopupMenu, TimeBox, TopMenu, VirtualKeyboard

    public class Window
    extends Container
    Window is a floating top-level window.

    A window cannot be added to a container; use popup or popupNonBlocking to show it on screen. Trying to add it to a container will raise a RuntimeException.

    The following example creates a popup window class:

      class TestWindow extends Window
      {
         Button btnHi;
         public TestWindow()
         {
            super("Test",RECT_BORDER); // with caption and borders
            setRect(CENTER,CENTER,Settings.screenWidth/2,Settings.screenHeight/4);
            add(btnHi=new Button("Hi!"),CENTER,CENTER);
         }
         public void onEvent(Event event)
         {
            if (event.type == ControlEvent.PRESSED && event.target == btnHi)
               unpop(); // a WINDOW_CLOSED event will be posted to this PARENT window.
         }
      }
    
    To use it in the normal way (blocking):
      public class Launcher extends MainWindow
      {
         Button btn;
         public void onEvent(Event e)
         {
            if (e.target == btn)
            {
               TestWindow tw = new TestWindow();
               tw.popup();
               // this line is only executed after the window is closed.
            }
         }
      }
    
    To use it non-blocking (the execution continues right after the popup command, even with the window still open):
      public class Launcher extends MainWindow
      {
         TestWindow tw;
         public void initUI()
         {
            tw = new TestWindow();
            tw.popupNonBlocking();
            // this line is executed immediately
         }
         public void onEvent(Event event)
         {
            if (event.target == tw && event.type == ControlEvent.WINDOW_CLOSED)
            {
               // any stuff
               break;
            }
         }
      }
    
    Blocking popup may be use in InputBox/MessageBox classes, while non-blocking popup is used in MenuBar and other classes.

    Important note: you can't use popup with a delay to unpop it. In this case, the correct would be to use popupNonBlocking:

     mb = new MessageBox(...);
     mb.popupNonBlocking();
     Vm.sleep(5000); // or do something else
     mb.unpop;
     
    If you use popup in this specific case, the Vm will hang.
    • Field Detail

      • needsPaint

        public static boolean needsPaint
        True if some area of any window is invalidated
      • title

        protected java.lang.String title
        Window's title
      • beepIfOut

        protected boolean beepIfOut
        When this window is the top most and the user clicks outside, a beep is thrown. Set this to false do disable the beep.
      • topMost

        protected static Window topMost
        The last poped up window.
      • titleFont

        protected Font titleFont
        Font for the title. Usually, MainWindow.defaultFont bolded
      • borderStyle

        protected byte borderStyle
        The window's border style
      • menubar

        protected Control menubar
        The window's menu bar
      • canDrag

        protected boolean canDrag
        If true (default), the user can drag this window around
      • highResPrepared

        @Deprecated
        protected boolean highResPrepared
        Deprecated.
      • tempTitle

        public java.lang.String tempTitle
        A temporary title that will be displayed when this Windows pops up. It will be replaced by the original title when it is closed.
        Since:
        TotalCross 1.53
      • flickEnabled

        @Deprecated
        public static boolean flickEnabled
        Deprecated.
        Flick is now enabled by default; just remove the reference to it.
      • focusOnPenUp

        protected Control focusOnPenUp
      • firstFocus

        public Control firstFocus
        the control that should get focus when a focus traversal key is pressed and none have focus
      • rTitle

        protected Rect rTitle
      • highlighted

        protected Control highlighted
      • footerH

        protected int footerH
      • sameBackgroundColor

        public boolean sameBackgroundColor
        Use the same color in border and in body
      • cancelPenUp

        public static boolean cancelPenUp
        If true, the next pen_up event will be ignored. This is used when a pen_down cancels a flick, or if a drag-scrollable control needs to cancel the next pen_up during a drag-scrolling interaction.
      • blocking

        protected boolean blocking
        True if the last popup mode was blocking.
      • titleColor

        public int titleColor
        The title color. The title color depends on the border type: it will be the foreground color if NO_BORDER is set, otherwise will be the background color.
      • titleGap

        public int titleGap
        A vertical gap used to increase the title area. Defaults to fmH/2 in Android, 0 on other user interface styles.
        Since:
        TotalCross 1.3.4
      • titleAlign

        public int titleAlign
        The title horizontal alignment in the Window's title area. It can be LEFT, CENTER or RIGHT, and you can use an adjustment on the value (E.G.: LEFT+5).
        Since:
        TotalCross 1.3
      • headerColor

        public int headerColor
        Set the header and the footer colors when in Android style and border type is ROUND_BORDER. Not used in other styles.
        Since:
        TotalCross 1.3
      • footerColor

        public int footerColor
        Set the header and the footer colors when in Android style and border type is ROUND_BORDER. Not used in other styles.
        Since:
        TotalCross 1.3
      • fadeOtherWindows

        public boolean fadeOtherWindows
        Set to true to make the other windows be faded when this window appears.
        Since:
        TotalCross 1.2
      • fadeValue

        public static int fadeValue
        The value used to fade the other windows. Defaults to 128.
        Since:
        TotalCross 1.2
      • robot

        public static UIRobot robot
        The UIRobot instance that is being used to record or play events.
      • repaintOnSwap

        public static boolean repaintOnSwap
        During a swap, the container is repainted. If the swap is being done under the presence of a (wait) Window, the container will be drawn but the window dont. To fix this, set this flag to false.
      • androidBorderThickness

        protected static int androidBorderThickness
      • popped

        protected boolean popped
        Used in popup
      • mainSwapContainer

        protected Container mainSwapContainer
        Used in the swap method
      • lastSwappedContainer

        protected Container lastSwappedContainer
        Used in the swap method
      • NO_BORDER

        public static final byte NO_BORDER
        To be used in setBorderStyle
        See Also:
        Constant Field Values
      • RECT_BORDER

        public static final byte RECT_BORDER
        To be used in setBorderStyle
        See Also:
        Constant Field Values
      • ROUND_BORDER

        public static final byte ROUND_BORDER
        To be used in setBorderStyle.
        See Also:
        Constant Field Values
      • TAB_BORDER

        public static final byte TAB_BORDER
        To be used in setBorderStyle
        See Also:
        Constant Field Values
      • TAB_ONLY_BORDER

        public static final byte TAB_ONLY_BORDER
        To be used in setBorderStyle
        See Also:
        Constant Field Values
      • SIP_HIDE

        public static final int SIP_HIDE
        Used to hide the virtual keyboard
        See Also:
        Constant Field Values
      • SIP_TOP

        public static final int SIP_TOP
        Used to place the virtual keyboard on top of screen.
        See Also:
        Constant Field Values
      • SIP_BOTTOM

        public static final int SIP_BOTTOM
        Used to place the virtual keyboard on bottom of screen.
        See Also:
        Constant Field Values
      • SIP_SHOW

        public static final int SIP_SHOW
        Used to show the virtual keyboard, without changing the position
        See Also:
        Constant Field Values
      • zStack

        public static Vector zStack
        Stack of poped up windows. Dont mess with this, unless you want to crash the VM!
      • ignoreEventOfType

        public static int ignoreEventOfType
        Used internally.
        See Also:
        Event.clearQueue(int)
      • _keyEvent

        protected KeyEvent _keyEvent
      • _penEvent

        protected PenEvent _penEvent
      • multiTouching

        protected boolean multiTouching
      • shiftY

        public static int shiftY
      • shiftH

        public static int shiftH
      • lastShiftY

        public static int lastShiftY
      • dragThreshold

        public static int dragThreshold
      • keyHook

        public static KeyListener keyHook
        A key listener that have priority over all other listeners.
      • ORIENTATION_PORTRAIT

        public static final int ORIENTATION_PORTRAIT
        Force the orientation during runtime. Use one of ORIENTATION_PORTRAIT / ORIENTATION_LANDSCAPE. Can be ored with ORIENTATION_INVERTED flag.
        See Also:
        Constant Field Values
    • Method Detail

      • setGrabPenEvents

        public void setGrabPenEvents​(Control c)
        Set to a control to redirect all pen events directly to it. This speeds up pen event processing. Used in Whiteboard class.
        Since:
        TotalCross 1.0
      • setTitleFont

        public void setTitleFont​(Font titleFont)
        Sets the title font
      • getTitleFont

        public Font getTitleFont()
        Gets the title font
      • setTitle

        public void setTitle​(java.lang.String title)
        Sets the title and call repaint.
      • setDeviceTitle

        public static final void setDeviceTitle​(java.lang.String title)
        Sets the title text in the task bar for non-Palm OS operating systems. Does not work in full-screen mode.
        Since:
        TotalCross 1.0
      • setOrientation

        public static final void setOrientation​(int orientation)
      • getBorderStyle

        public byte getBorderStyle()
        Returns the border style of this window.
        Overrides:
        getBorderStyle in class Container
      • makeUnmovable

        public void makeUnmovable()
        Makes this window static, i.e., the user will not be able to move it around the screen by dragging the title area. It just set the canDrag member to false.
      • setFocus

        public void setFocus​(Control c)
        Sets focus to the given control. When a user types a key, the control with focus get the key event. At any given time, only one control in a window can have focus. Calling this method will cause a FOCUS_OUT control event to be posted to the window's current focus control (if one exists) and will cause a FOCUS_IN control event to be posted to the new focus control.
      • removeFocus

        public void removeFocus()
        Calling this method will cause a FOCUS_OUT control event to be posted to the window's current focus control (if one exists)
      • swapFocus

        protected void swapFocus​(Control c)
        Changes the focus to the desired control, without posting FOCUS_IN and FOCUS_OUT events
        Since:
        SuperWaba 4.01
      • getFocus

        public Control getFocus()
        Returns the focus control for this window. This will be the same of focusedControl, unless the last one has been changed manually.
        See Also:
        setFocus(totalcross.ui.Control)
      • onClickedOutside

        protected boolean onClickedOutside​(PenEvent event)
        called when the user clicks outside the bounds of this window. must return true if the event was handled, false otherwise. If false is returned and beepIfOut is true, then a beep is played and nothing more happens.
        Since:
        SuperWaba 1.2
      • validate

        public void validate()
        Calls _doPaint if the window needs painting.
        Since:
        SuperWaba 4.0
      • _postEvent

        public final void _postEvent​(int type,
                                     int key,
                                     int x,
                                     int y,
                                     int modifiers,
                                     int timeStamp)
        Called by the VM to post key and pen events.
      • getSafeAreaInsets

        public static Insets getSafeAreaInsets()
      • getClientRect

        protected void getClientRect​(Rect r)
        Returns the client rect, ie, the rect minus the border and title area, in relative coords In this version, you provide the created Rect to be filled with the coords.
        Overrides:
        getClientRect in class Container
      • paintTitle

        protected void paintTitle​(java.lang.String title,
                                  Graphics gg)
        Paints the title and border.
      • paintWindowBackground

        public void paintWindowBackground​(Graphics gg)
      • _doPaint

        public void _doPaint()
        Called by the VM to repaint an area.
      • unpop

        public void unpop()
        Hides this window. Calling unpop when only the MainWindow is active does nothing.
      • pumpEvents

        public static void pumpEvents()
        Let the VM handle all the events in the event queue. This method is used to implement a blocking Window. Here is an example:
         while(someCondition)
         {
            Event.pumpEvents();
         }
         
      • isTopMost

        public boolean isTopMost()
        Returns true if this window is the top-level window
        Overrides:
        isTopMost in class Control
      • getTopMost

        public static Window getTopMost()
        Returns the topmost window
      • onPopup

        protected void onPopup()
        Placeholder called imediatly before the popup began. The default implementation does nothing.
      • postPopup

        protected void postPopup()
        Placeholder called after the popup is done and after the repaint of this window. The default implementation does nothing.
      • onUnpop

        protected void onUnpop()
        Placeholder called imediatly before the unpop began. The default implementation does nothing.
      • postUnpop

        protected void postUnpop()
        Placeholder called after the unpop is done and after the repaint of the other window. The default implementation does nothing.
      • isVisible

        public boolean isVisible()
        True if this is the topmost window
        Overrides:
        isVisible in class Control
      • setMenuBar

        public void setMenuBar​(Control menubar)
        Sets the menu bar for this window
      • popupMenuBar

        public void popupMenuBar()
        Open the menu bar that is associated with this window
      • loadBehind

        protected void loadBehind()
        This method repaints all windows from bottom to top. It is automaticaly used in the unpop method.
        Since:
        SuperWaba 2.0 beta 4
      • destroyZStack

        public static void destroyZStack()
        This method is for internal use only. calling it directly will cause the VM to crash.
      • swap

        public void swap​(Container newContainer)
        Used to swap containers from the Window. To restore the first container, pass null as parameter. Note that null cannot be used for the first swap. See samples/ui/ContainerSwitch. This method correctly handles screen rotation for containers that were already swapped.
      • getPreferredWidth

        public int getPreferredWidth()
        Returns the size of the title if any plus the size of the border. Note that the value returned here does not handle the controls inside the window.
        Overrides:
        getPreferredWidth in class Control
      • getPreferredHeight

        public int getPreferredHeight()
        Returns the size of the title if any plus the size of the border. Note that the value returned here does not handle the controls inside the window.
        Overrides:
        getPreferredHeight in class Control
      • repaintActiveWindows

        public static void repaintActiveWindows()
        Repaints the window stack from 0 to zStack.size().
      • handleFocusChangeKeys

        protected boolean handleFocusChangeKeys​(KeyEvent ke)
        Called by the main event handler to handle the focus change keys. Only called when Settings.keyboardFocusNavigation is true.
        Since:
        SuperWaba 5.5
      • setHighlighted

        public void setHighlighted​(Control c)
        Sets the currently highlighted control which will gain focus if the ACTION button is pressed. It may be the same of the one that holds focus.
        Since:
        SuperWaba 5.5
      • getHighlighted

        public Control getHighlighted()
        Gets the currently highlighted control.
        Since:
        SuperWaba 5.5
      • drawHighlight

        public void drawHighlight()
        Avoid drawing the highlight on a Window
      • popupNonBlocking

        public void popupNonBlocking()
        Pops up this window, but the program execution continues right after, before the window is dismissed.
        Since:
        SuperWaba 5.5
      • popup

        public void popup()
        Pops up this window, blocking the execution until the window closes.
        Since:
        SuperWaba 5.5
      • postPressedEvent

        public void postPressedEvent()
        Posts a ControlEvent.PRESSED event on the focused control.
        Overrides:
        postPressedEvent in class Control
        Since:
        SuperWaba 5.8
      • screenResized

        public void screenResized()
        Called when the screen is resized, probably caused by a rotation. ATTENTION: THIS CALL CANNOT BE BLOCKED OR THE SYSTEM WILL LOCK!
      • resize

        public void resize()
        This method resizes the Window to the needed bounds, based on added childs. It changes only the size, keeping the x,y coordinates passed on setRect. You can add spaces at right and bottom using the insets.right/bottom properties. Sample:
         public class TestWindow extends Window
         {
            public TestWindow()
            {
               ... call constructor
               setRect(CENTER,CENTER,Screen.width-40,1000); // height will be resized later
            }
            public void initUI()
            {
               setInsets(5,5,5,5);
               ... add controls
               resize();
            }   
         }
         
        In this sample, since the height is variable, you can't use BOTTOM on y nor FILL on height, otherwise resize will not work as expected. Same counts if you have a variable width.
        Overrides:
        resize in class Container
        Since:
        TotalCross 1.14
        See Also:
        Container.resizeWidth(), Container.resizeHeight()
      • resizeWidth

        public void resizeWidth()
        Description copied from class: Container
        This method resizes the Container's width to the needed bounds, based on added childs. You can add spaces at right using the insets.right property. Sample:
           // this sample will center two buttons of different sizes on screen 
           Container c = new Container();
           add(c, CENTER,BOTTOM,1000,1000);
           c.add(new Button("Ok"),LEFT,TOP);
           c.add(new Button("Cancel"),AFTER+5,SAME);
           c.resize();
         
        Note: differently of Window.resize, this method does not call setRect again, it only changes the width by direct assignment.
        Overrides:
        resizeWidth in class Container
        See Also:
        Container.resize(), Container.resizeHeight()
      • resizeHeight

        public void resizeHeight()
        Description copied from class: Container
        This method resizes the Container's width to the needed bounds, based on added childs. You can add spaces at bottom using the insets.bottom property. Sample:
           // this sample will center two buttons of different sizes on screen 
           Container c = new Container();
           add(c, CENTER,BOTTOM,1000,1000);
           c.add(new Button("Ok"),LEFT,TOP);
           c.add(new Button("Cancel"),AFTER+5,SAME);
           c.resize();
         
        Note: differently of Window.resize, this method does not call setRect again, it only changes the height by direct assignment.
        Overrides:
        resizeHeight in class Container
        See Also:
        Container.resizeWidth(), Container.resize()
      • getDefaultDragThreshold

        public static int getDefaultDragThreshold()
      • getPopupCount

        public static int getPopupCount()
        Returns the number of windows that are popped up. If there's only a MainWindow, returns 0.
        Since:
        TotalCross 1.27
      • onRobotKey

        public static void onRobotKey()
        Called when a robot key is pressed. Don't call this method directly, use Settings.deviceRobotSpecialKey instead.
        See Also:
        Settings.deviceRobotSpecialKey
      • shiftScreen

        public void shiftScreen​(Control c,
                                int deltaY)
      • isScreenShifted

        public static boolean isScreenShifted()
      • isSipShown

        public static boolean isSipShown()
      • isSipShown4D

        public static boolean isSipShown4D()