Package totalcross.ui
Class MainWindow
- java.lang.Object
-
- totalcross.ui.gfx.GfxSurface
-
- totalcross.ui.Control
-
- totalcross.ui.Container
-
- totalcross.ui.Window
-
- totalcross.ui.MainWindow
-
- All Implemented Interfaces:
MainClass
- Direct Known Subclasses:
Conduit,GameEngine,TestSuite
public class MainWindow extends Window implements MainClass
MainWindow is the main window of a UI-based application.All TotalCross programs with an user-interface must have one and only one main window.
Here is an example showing a basic application:
public class MyProgram extends MainWindow { Edit edName; public void initUI() { ... initialization code ... add(new Label("Name:"), LEFT,TOP+2); add(edName = new Edit(""), AFTER,SAME-2); } }
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class totalcross.ui.Control
Control.TranslucentShape
-
-
Field Summary
Fields Modifier and Type Field Description protected TimerEventfirstTimer-
Fields inherited from class totalcross.ui.Window
_controlEvent, _dragEvent, _focus, _keyEvent, _mouseEvent, _multiEvent, _penEvent, androidBorderThickness, beepIfOut, blocking, borderStyle, cancelPenUp, canDrag, dragThreshold, fadeOtherWindows, fadeValue, firstFocus, flickEnabled, focusOnPenUp, footerColor, footerH, gradientTitleEndColor, gradientTitleStartColor, headerColor, highlighted, highResPrepared, HORIZONTAL_GRADIENT, ignoreEventOfType, keyHook, lastShiftY, lastSwappedContainer, mainSwapContainer, menubar, multiTouching, needsPaint, NO_BORDER, ORIENTATION_INVERTED, ORIENTATION_LANDSCAPE, ORIENTATION_PORTRAIT, popped, RECT_BORDER, repaintOnSwap, robot, ROUND_BORDER, rTitle, sameBackgroundColor, shiftH, shiftY, SIP_BOTTOM, SIP_HIDE, SIP_SHOW, SIP_TOP, TAB_BORDER, TAB_ONLY_BORDER, tempTitle, title, titleAlign, titleColor, titleFont, titleGap, topMost, VERTICAL_GRADIENT, zStack
-
Fields inherited from class totalcross.ui.Container
alwaysEraseBackground, BACKGROUND_CYLINDRIC_SHADED, BACKGROUND_SHADED, BACKGROUND_SHADED_INV, BACKGROUND_SOLID, backgroundStyle, BORDER_LOWERED, BORDER_NONE, BORDER_RAISED, BORDER_ROUNDED, BORDER_SIMPLE, BORDER_TOP, borderColor, children, controlFound, finishedStart, ignoreOnAddAgain, ignoreOnRemove, insets, lastH, lastScreenWidth, lastW, started, tabOrder, tail, TRANSITION_CLOSE, TRANSITION_FADE, TRANSITION_NONE, TRANSITION_OPEN, TRANSITION_TIME, transitionEffect
-
Fields inherited from class totalcross.ui.Control
AFTER, alphaValue, appId, appObj, asContainer, asWindow, backColor, BEFORE, BOTTOM, BOTTOM_OF, BRIGHTER_BACKGROUND, callListenersOnAllTargets, CENTER, CENTER_OF, clearValueInt, clearValueStr, DARKER_BACKGROUND, effect, enableUpdateScreen, esce, eventsEnabled, FILL, fillColor, FIT, fm, fmH, focusHandler, focusLess, focusOnPenDown, focusTraversable, font, FONTSIZE, foreColor, height, ignoreInsets, isHighlighting, isTablet, KEEP, keepDisabled, keepEnabled, LEFT, nextTabControl, offscreen, offscreen0, onEventFirst, parent, PARENTSIZE, PARENTSIZEMAX, PARENTSIZEMIN, PREFERRED, RANGE, repositionAllowed, RIGHT, RIGHT_OF, SAME, SCREENSIZE, SCREENSIZEMAX, SCREENSIZEMIN, setFont, setH, setRel, setW, setX, setY, tempW, textShadowColor, TOP, translucentShape, transparentBackground, uiAdjustmentsBasedOnFontHeightIsSupported, uiAndroid, UICONST, uiFlat, uiHolo, uiMaterial, uiVista, visible, width, WILL_RESIZE, x, y
-
-
Constructor Summary
Constructors Constructor Description MainWindow()Constructs a main window with no title and no border.MainWindow(java.lang.String title, byte style)Constructs a main window with the given title and border style.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void_onMinimize()protected void_onRestore()void_onTimerTick(boolean canUpdate)Called by the VM to process timer interrupts.protected TimerEventaddTimer(Control target, int millis)Adds a timer to a control.protected voidaddTimer(TimerEvent t, Control target, int millis)Adds the timer t to the target control.protected voidaddTimer(TimerEvent te, Control target, int millis, boolean append)Adds the timer t to the target control.voidappEnding()Called by the system so we can finish things correctly.voidappStarting(int timeAvail)Called by the VM when the application is starting.static voidexit(int exitCode)Notifies the application that it should stop executing and exit.protected voidfillBackground(Graphics g, int b)protected voidgetClientRect(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.static java.lang.StringgetCommandLine()Returns the command line passed by the application that called this application in the Vm.exec method.static FontgetDefaultFont()Returns the default font.static MainWindowgetMainWindow()Returns the instance of the current main window.static ImagegetScreenShot()Takes a screen shot of the current screen.protected FirebaseInstanceIdServiceinitFirebaseInstanceIdService()Register your own FirebaseInstanceIdService when initializing the appstatic booleanisMainThread()Returns true if this is the main thread.static voidminimize()Notifies the application that it should be minimized, that is, transfered to the background.voidonExit()Called just before an application exits.voidonMinimize()Called just after the application is minimized.voidonRestore()Called just after the application is restored.booleanremoveTimer(TimerEvent timer)Removes the given timer from the timers queue.static voidrestore()Notifies the application that it should be restored, that is, transfered to the foreground.voidrunOnMainThread(java.lang.Runnable r)The same ofrunOnMainThread(r, true).voidrunOnMainThread(java.lang.Runnable r, boolean singleInstance)Runs the given code in the main thread.static voidsetDefaultFont(Font newFont)Sets the default font used in all controls created.voidsetRect(int x, int y, int width, int height, Control relative, boolean screenChanged)This method can't be called for a MainWindowvoidsetUIStyle(byte style)Changes the user interface style to the given one.-
Methods inherited from class totalcross.ui.Window
_doPaint, _postEvent, destroyZStack, drawHighlight, getBorderStyle, getClientRect, getDefaultDragThreshold, getFocus, getHighlighted, getPopupCount, getPreferredHeight, getPreferredWidth, getSafeAreaInsets, getTitleFont, getTopMost, handleFocusChangeKeys, isScreenShifted, isSipShown, isSipShown4D, isTopMost, isVisible, loadBehind, makeUnmovable, onClickedOutside, onPopup, onRobotKey, onUnpop, paintTitle, paintWindowBackground, popup, popupMenuBar, popupNonBlocking, postPopup, postPressedEvent, postUnpop, pumpEvents, removeFocus, repaintActiveWindows, resize, resizeHeight, resizeWidth, screenResized, setBorderStyle, setDeviceTitle, setFocus, setGrabPenEvents, setHighlighted, setMenuBar, setOrientation, setSIP, setTitle, setTitleFont, shiftScreen, swap, swapFocus, unpop, validate
-
Methods inherited from class totalcross.ui.Container
add, add, add, add, add, applyTransitionEffect, broadcastEvent, clear, fillBackground, findChild, findNearestChild, findNextFocusControl, getChildren, getChildrenCount, getFirstChild, getFocusableControls, getInsets, incLastX, incLastY, initUI, isPressed, moveFocusToNextControl, moveFocusToNextEditable, onAddAgain, onColorsChanged, onPaint, onRemove, onSwapFinished, paintChildren, remove, removeAll, setEnabled, setFocusTraversable, setHighlighting, setInsets, setNextTransitionEffect, setPressColor, setPressed, swapToTopmostWindow
-
Methods inherited from class totalcross.ui.Control
_onEvent, addEnabledStateListener, addFocusListener, addGridListener, addHighlightListener, addKeyListener, addListContainerListener, addMouseListener, addMultiTouchListener, addPenListener, addPressListener, addPushNotificationListener, addTimer, addTimer, addTimerListener, addWindowListener, bringToFront, changeHighlighted, contains, drawTranslucentBackground, getAbsoluteRect, getBackColor, getEffectH, getEffectW, getEffectX, getEffectY, getEventListeners, getFont, getForeColor, getGap, getGraphics, getHeight, getNext, getParent, getParentWindow, getPos, getPressedEvent, getPrev, getRect, getSize, getTextShadowColor, getWidth, getX, getX2, getY, getY2, hadParentScrolled, handleGeographicalFocusChangeKeys, hasFocus, internalSetEnabled, intXYWH, isActionEvent, isChildOf, isDisplayed, isEnabled, isInsideOrNear, isObscured, isVisibleAndInside, onBoundsChanged, onEvent, onFontChanged, onWindowPaintFinished, post, postEvent, releaseScreenShot, removeEnabledStateListener, removeFocusListener, removeGridListener, removeHighlightListener, removeKeyListener, removeListContainerListener, removeMouseListener, removeMultiTouchListener, removePenListener, removePressListener, removePushNotificationListener, removeTimerListener, removeWindowListener, repaint, repaintNow, reposition, reposition, repositionChildren, requestFocus, resetSetPositions, safeRepaintNow, safeUpdateScreen, sendToBack, setBackColor, setBackForeColors, setFocusLess, setFont, setForeColor, setRect, setRect, setRect, setSet, setTextShadowColor, setTranslucent, setVisible, showTip, takeInitialScreenShot, takeScreenShot, translateFromOrigin, uiStyleChanged, updateScreen, updateTemporary, willOpenKeyboard
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface totalcross.MainClass
_postEvent
-
-
-
-
Field Detail
-
firstTimer
protected TimerEvent firstTimer
-
-
Constructor Detail
-
MainWindow
public MainWindow()
Constructs a main window with no title and no border.
-
MainWindow
public MainWindow(java.lang.String title, byte style)Constructs a main window with the given title and border style.- Since:
- SuperWaba 2.0b4
- See Also:
Window.NO_BORDER,Window.RECT_BORDER,Window.ROUND_BORDER,Window.TAB_BORDER,Window.TAB_ONLY_BORDER
-
-
Method Detail
-
getClientRect
protected void getClientRect(Rect r)
Description copied from class:WindowReturns 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:
getClientRectin classWindow
-
fillBackground
protected void fillBackground(Graphics g, int b)
- Overrides:
fillBackgroundin classContainer
-
initFirebaseInstanceIdService
protected FirebaseInstanceIdService initFirebaseInstanceIdService()
Register your own FirebaseInstanceIdService when initializing the app- Returns:
-
isMainThread
public static boolean isMainThread()
Returns true if this is the main thread.- Since:
- TotalCross 2.0
-
setDefaultFont
public static void setDefaultFont(Font newFont)
Sets the default font used in all controls created. To change the default font, assign it to this member in the MainWindow constructor, making it the FIRST LINE in the constructor; you'll not be able to use super(title,border): change by setBorderStyle and setTitle, after the defaultFont assignment. Example:public MyApp() { MainWindow.setDefaultFont(Font.getFont(false, Font.NORMAL_SIZE+2)); setBorderStyle(TAB_ONLY_BORDER); setTitle("My application"); }- Since:
- TotalCross 1.0 beta3
-
getDefaultFont
public static Font getDefaultFont()
Returns the default font.- Since:
- TotalCross 1.0 beta3
-
setUIStyle
public void setUIStyle(byte style)
Changes the user interface style to the given one. This method must be called in the MainWindow's constructor, and only once. E.g.:public class Foo extends MainWindow { public Foo() { super("Hi bar",TAB_ONLY_BORDER); setUIStyle(totalcross.sys.Settings.Flat);Changing to Android style will also set Settings.fingerTouch to true. If you don't like such behaviour in non finger devices, set this property to false after calling setUIStyle.- Since:
- SuperWaba 5.05
- See Also:
Settings.Flat,Settings.Vista,Settings.Android,Settings.Holo,Settings.Material
-
exit
public static final void exit(int exitCode)
Notifies the application that it should stop executing and exit. It will exit after executing any pending events. If the underlying system supports it, the exitCode passed is returned to the program that started the app. Note: On AppletViewer/Browser the exitCode is useless. Note 2: On Android, you can exit softly by using SOFT_EXIT as the exit code.If you want your code to be called when the VM exits, extend the onExit method.
- See Also:
onExit()
-
minimize
public static void minimize()
Notifies the application that it should be minimized, that is, transfered to the background. Whenever the application is minimized, the following call back function will be called:onMinimize(). Note: On Android, callingminimize()will pause the application execution and it can only be restored manually by the user. This method is also supported on Windows 32.- Since:
- TotalCross 1.10
- See Also:
onMinimize(),onRestore()
-
restore
public static void restore()
Notifies the application that it should be restored, that is, transfered to the foreground. Whenever the application is restored, the following call back function will be called:onRestore(). Note: This method is supported on Android but the user must restore the application manually. This method is also supported on Windows 32.- Since:
- TotalCross 1.10
-
getMainWindow
public static MainWindow getMainWindow()
Returns the instance of the current main window. You can use it to get access to methods of theMainWindowclass from outside the class. It is also possible to cast the returned class to the class that is extendingMainWindow(this is a normal Java behavior). So, if UiGadgets is running, it is correct to do:UIGadgets instance = (UIGadgets)MainWindow.getMainWindow();
-
addTimer
protected TimerEvent addTimer(Control target, int millis)
Adds a timer to a control. This method is protected, the public method to add a timer to a control is the addTimer() method in the Control class. The Timer event will be issued to the target every millis milliseconds.
-
addTimer
protected void addTimer(TimerEvent t, Control target, int millis)
Adds the timer t to the target control. This method is protected, the public method to add a timer to a control is the addTimer() method in the Control class. The Timer event will be issued to the target every millis milliseconds.
-
addTimer
protected void addTimer(TimerEvent te, Control target, int millis, boolean append)
Adds the timer t to the target control. This method is protected, the public method to add a timer to a control is the addTimer() method in the Control class. The Timer event will be issued to the target every millis milliseconds.
-
removeTimer
public boolean removeTimer(TimerEvent timer)
Removes the given timer from the timers queue. This method returns true if the timer was found and removed and false if the given timer could not be found. Thetargetmember is set to null.- Overrides:
removeTimerin classControl
-
appStarting
public final void appStarting(int timeAvail)
Called by the VM when the application is starting. Setups a timer that will call initUI after the event loop is started. Never call this method directly; this method is not private to prevent the compiler from removing it during optimization. The timeAvail parameter is passed by the vm to show how much time the user have to keep testing the demo vm. Even if this value is not shown to the user, it is internally computed and the vm will exit when the counter reaches 0.- Specified by:
appStartingin interfaceMainClass
-
appEnding
public final void appEnding()
Called by the system so we can finish things correctly. Never call this method directly; this method is not private to prevent the compiler from removing it during optimization.
-
_onMinimize
protected final void _onMinimize()
-
_onRestore
protected final void _onRestore()
-
onExit
public void onExit()
Called just before an application exits. When this is called, all threads are already killed. You should return from this method as soon as possible, because the OS can kill the application if it takes too much to return. Note that on Windows Phone this method is NEVER called.
-
onMinimize
public void onMinimize()
Called just after the application is minimized. If the user press the home key and then forces the application to stop. So, its a good thing to close IO operations and recover them in the onRestore method.
When the onMinimize is called, the screen will only be able to be updated after it resumes (in other words, calling repaint or repaintNow from the onMinimize method has no effect). On Windows Phone, the onMinimize is called and, if the user don't call the application again within 10 seconds, the application is KILLED without notifications. So, you should save all your application's state in this method and restore it in the onRestore method.- Since:
- TotalCross 1.10
- See Also:
minimize()
-
onRestore
public void onRestore()
Called just after the application is restored.- Since:
- TotalCross 1.10
- See Also:
onRestore()
-
_onTimerTick
public final void _onTimerTick(boolean canUpdate)
Called by the VM to process timer interrupts. This method is not private to prevent the compiler from removing it during optimization.- Specified by:
_onTimerTickin interfaceMainClass
-
getCommandLine
public static final java.lang.String getCommandLine()
Returns the command line passed by the application that called this application in the Vm.exec method. In Android, you can start an application using adb:adb shell am start -a android.intent.action.MAIN -n totalcross.app.uigadgets/.UIGadgets -e cmdline "Hello world"
In the sample above, we're starting UIGadgets. Your app should be: totalcross.app.yourMainWindowClass/.yourMainWindowClass Note: When you click on the application's icon, there's no command line.
-
setRect
public void setRect(int x, int y, int width, int height, Control relative, boolean screenChanged)This method can't be called for a MainWindow- Overrides:
setRectin classControl- Parameters:
x- One of the relative positioning constants: LEFT, RIGHT, SAME, BEFORE, AFTER, CENTER, with a small adjustment. You can also use an absolute value, but this is strongly discouraged.y- One of the relative positioning constants: TOP, BOTTOM, SAME, BEFORE, AFTER, CENTER, with a small adjustment. You can also use an absolute value, but this is strongly discouraged.width- One of the relative positioning constants: PREFERRED, FILL, FIT, SAME. You can also use an absolute value, but this is strongly discouraged.height- One of the relative positioning constants: PREFERRED, FILL, FIT, SAME. You can also use an absolute value, but this is strongly discouraged.relative- To whom the position should be relative to; or null to be relative to the last control.screenChanged- Indicates that a screen change (resize, collapse) occured and therepositionmethod is calling this method. Set by the system. If you call this method directly, always pass false to it.- See Also:
Control.LEFT,Control.TOP,Control.RIGHT,Control.BOTTOM,Control.BEFORE,Control.AFTER,Control.CENTER,Control.SAME,Control.FILL,Control.PREFERRED,Control.FIT,Control.CENTER_OF,Control.RIGHT_OF,Control.BOTTOM_OF,Control.SCREENSIZE,Control.SCREENSIZEMIN,Control.SCREENSIZEMAX,Control.PARENTSIZE,Control.PARENTSIZEMIN,Control.PARENTSIZEMAX,Control.FONTSIZE,Container.add(Control, int, int),Container.add(Control, int, int, Control)
-
getScreenShot
public static Image getScreenShot()
Takes a screen shot of the current screen. Since TotalCross 3.06, it uses Control.takeScreenShot. Here's a sample:Image img = MainWindow.getScreenShot(); File f = new File(Settings.onJavaSE ? "screen.png" : "/sdcard/screen.png",File.CREATE_EMPTY); img.createPng(f); f.close();
Note that the font varies from device to device and even to desktop. So, if you want to compare a device's screen shot with one taken at desktop, be sure to set the default font in both to the same, like usingsetDefaultFont(Font.getFont(false,20)).- Since:
- TotalCross 1.3
-
runOnMainThread
public void runOnMainThread(java.lang.Runnable r)
The same ofrunOnMainThread(r, true).- See Also:
Note that this
-
runOnMainThread
public void runOnMainThread(java.lang.Runnable r, boolean singleInstance)Runs the given code in the main thread. As of TotalCross 2.0, a thread cannot update the screen. So, asking the code to be called in the main thread solves the problem. Of course, this call is asynchronous, ie, the thread may run again before the screen is updated. This method is thread-safe. If singleInstance is true and the array contains an element of this class, it is replaced by the given one. Note that passing as false may result in memory leak. Sample:new Thread() { public void run() { while (true) { Vm.sleep(1000); MainWindow.getMainWindow().runOnMainThread(new Runnable() { public void run() { log("babi "+ ++contador); } }); } } }.start();- Since:
- TotalCross 2.1
-
-