Package totalcross.ui

Class 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);
        }
     }
     
    • Method Detail

      • getClientRect

        protected void getClientRect​(Rect r)
        Description copied from class: Window
        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 Window
      • initFirebaseInstanceIdService

        protected FirebaseInstanceIdService initFirebaseInstanceIdService()
        Register your own FirebaseInstanceIdService when initializing the app
        Returns:
      • initFirebaseMessagingService

        protected FirebaseMessagingService initFirebaseMessagingService()
        Register a FireBaseMessagingService when initializing the app
      • 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, calling minimize() 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 the MainWindow class from outside the class. It is also possible to cast the returned class to the class that is extending MainWindow (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. The target member is set to null.
        Overrides:
        removeTimer in class Control
      • 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:
        appStarting in interface MainClass
      • 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.
        Specified by:
        appEnding in interface MainClass
      • _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:
        _onTimerTick in interface MainClass
      • 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.
      • 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.appPath + "/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 using setDefaultFont(Font.getFont(false,20)).
        Since:
        TotalCross 1.3
      • addUpdateListener

        public void addUpdateListener​(UpdateListener listener)
        Registers a listener for Update events, which SHOULD be unregistered as soon as its done processing. Listeners are registered as weak references, and therefore are automatically removed by the GC when they are no longer reachable. Which means that a strong reference for the listener must be kept before the registration and kept until the processing is done. This is a protection to prevent leaks and release CPU and memory resources, but registered listeners should ALWAYS be explicitly unregistered by the user once its work is done.
        See Also:
        UpdateListener
      • removeUpdateListener

        public void removeUpdateListener​(UpdateListener listener)
        Removes a listener for Update events
        Since:
        TotalCross 5.00
        See Also:
        totalcross.ui.event.UpdateEventListener
      • runOnMainThread

        public void runOnMainThread​(java.lang.Runnable r)
        The same of runOnMainThread(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