+
+ if (menu.getId() == TMenu.MID_TILE) {
+ tileWindows();
+ repaint = true;
+ return true;
+ }
+ if (menu.getId() == TMenu.MID_CASCADE) {
+ cascadeWindows();
+ repaint = true;
+ return true;
+ }
+ if (menu.getId() == TMenu.MID_CLOSE_ALL) {
+ closeAllWindows();
+ repaint = true;
+ return true;
+ }
+ return false;
+ }
+
+ /**
+ * Method that TApplication subclasses can override to handle keystrokes.
+ *
+ * @param keypress keystroke event
+ * @return if true, this event was consumed
+ */
+ protected boolean onKeypress(final TKeypressEvent keypress) {
+ // Default: only menu shortcuts
+
+ // Process Alt-F, Alt-E, etc. menu shortcut keys
+ if (!keypress.getKey().getIsKey()
+ && keypress.getKey().getAlt()
+ && !keypress.getKey().getCtrl()
+ && (activeMenu == null)
+ ) {
+
+ assert (subMenus.size() == 0);
+
+ for (TMenu menu: menus) {
+ if (Character.toLowerCase(menu.getMnemonic().getShortcut())
+ == Character.toLowerCase(keypress.getKey().getCh())
+ ) {
+ activeMenu = menu;
+ menu.setActive(true);
+ repaint = true;
+ return true;
+ }
+ }
+ }
+
+ return false;
+ }
+
+ /**
+ * Add a keyboard accelerator to the global hash.
+ *
+ * @param item menu item this accelerator relates to
+ * @param keypress keypress that will dispatch a TMenuEvent
+ */
+ public final void addAccelerator(final TMenuItem item,
+ final TKeypress keypress) {
+
+ // System.err.printf("addAccelerator: key %s item %s\n", keypress, item);
+
+ synchronized (accelerators) {
+ assert (accelerators.get(keypress) == null);
+ accelerators.put(keypress, item);
+ }
+ }
+
+ /**
+ * Recompute menu x positions based on their title length.
+ */
+ public final void recomputeMenuX() {
+ int x = 0;
+ for (TMenu menu: menus) {
+ menu.setX(x);
+ x += menu.getTitle().length() + 2;
+ }
+ }
+
+ /**
+ * Post an event to process and turn off the menu.
+ *
+ * @param event new event to add to the queue
+ */
+ public final void addMenuEvent(final TInputEvent event) {
+ synchronized (fillEventQueue) {
+ fillEventQueue.add(event);
+ }
+ closeMenu();
+ }
+
+ /**
+ * Add a sub-menu to the list of open sub-menus.
+ *
+ * @param menu sub-menu
+ */
+ public final void addSubMenu(final TMenu menu) {
+ subMenus.add(menu);
+ }
+
+ /**
+ * Convenience function to add a top-level menu.
+ *
+ * @param title menu title
+ * @return the new menu
+ */
+ public final TMenu addMenu(final String title) {
+ int x = 0;
+ int y = 0;
+ TMenu menu = new TMenu(this, x, y, title);
+ menus.add(menu);
+ recomputeMenuX();
+ return menu;
+ }
+
+ /**
+ * Convenience function to add a default "File" menu.
+ *
+ * @return the new menu
+ */
+ public final TMenu addFileMenu() {
+ TMenu fileMenu = addMenu("&File");
+ fileMenu.addDefaultItem(TMenu.MID_OPEN_FILE);
+ fileMenu.addSeparator();
+ fileMenu.addDefaultItem(TMenu.MID_SHELL);
+ fileMenu.addDefaultItem(TMenu.MID_EXIT);
+ return fileMenu;
+ }
+
+ /**
+ * Convenience function to add a default "Edit" menu.
+ *
+ * @return the new menu
+ */
+ public final TMenu addEditMenu() {
+ TMenu editMenu = addMenu("&Edit");
+ editMenu.addDefaultItem(TMenu.MID_CUT);
+ editMenu.addDefaultItem(TMenu.MID_COPY);
+ editMenu.addDefaultItem(TMenu.MID_PASTE);
+ editMenu.addDefaultItem(TMenu.MID_CLEAR);
+ return editMenu;
+ }
+
+ /**
+ * Convenience function to add a default "Window" menu.
+ *
+ * @return the new menu
+ */
+ public final TMenu addWindowMenu() {
+ TMenu windowMenu = addMenu("&Window");
+ windowMenu.addDefaultItem(TMenu.MID_TILE);
+ windowMenu.addDefaultItem(TMenu.MID_CASCADE);
+ windowMenu.addDefaultItem(TMenu.MID_CLOSE_ALL);
+ windowMenu.addSeparator();
+ windowMenu.addDefaultItem(TMenu.MID_WINDOW_MOVE);
+ windowMenu.addDefaultItem(TMenu.MID_WINDOW_ZOOM);
+ windowMenu.addDefaultItem(TMenu.MID_WINDOW_NEXT);
+ windowMenu.addDefaultItem(TMenu.MID_WINDOW_PREVIOUS);
+ windowMenu.addDefaultItem(TMenu.MID_WINDOW_CLOSE);
+ return windowMenu;
+ }
+
+ /**
+ * Close all open windows.
+ */
+ private void closeAllWindows() {
+ // Don't do anything if we are in the menu
+ if (activeMenu != null) {
+ return;
+ }
+ for (TWindow window: windows) {
+ closeWindow(window);
+ }
+ }
+
+ /**
+ * Re-layout the open windows as non-overlapping tiles. This produces
+ * almost the same results as Turbo Pascal 7.0's IDE.
+ */
+ private void tileWindows() {
+ // Don't do anything if we are in the menu
+ if (activeMenu != null) {
+ return;
+ }
+ int z = windows.size();
+ if (z == 0) {
+ return;
+ }
+ int a = 0;
+ int b = 0;
+ a = (int)(Math.sqrt(z));
+ int c = 0;
+ while (c < a) {
+ b = (z - c) / a;
+ if (((a * b) + c) == z) {
+ break;
+ }
+ c++;
+ }
+ assert (a > 0);
+ assert (b > 0);
+ assert (c < a);
+ int newWidth = (getScreen().getWidth() / a);
+ int newHeight1 = ((getScreen().getHeight() - 1) / b);
+ int newHeight2 = ((getScreen().getHeight() - 1) / (b + c));
+ // System.err.printf("Z %s a %s b %s c %s newWidth %s newHeight1 %s newHeight2 %s",
+ // z, a, b, c, newWidth, newHeight1, newHeight2);
+
+ List<TWindow> sorted = new LinkedList<TWindow>(windows);
+ Collections.sort(sorted);
+ Collections.reverse(sorted);
+ for (int i = 0; i < sorted.size(); i++) {
+ int logicalX = i / b;
+ int logicalY = i % b;
+ if (i >= ((a - 1) * b)) {
+ logicalX = a - 1;
+ logicalY = i - ((a - 1) * b);
+ }
+
+ TWindow w = sorted.get(i);
+ w.setX(logicalX * newWidth);
+ w.setWidth(newWidth);
+ if (i >= ((a - 1) * b)) {
+ w.setY((logicalY * newHeight2) + 1);
+ w.setHeight(newHeight2);
+ } else {
+ w.setY((logicalY * newHeight1) + 1);
+ w.setHeight(newHeight1);
+ }
+ }
+ }
+
+ /**
+ * Re-layout the open windows as overlapping cascaded windows.
+ */
+ private void cascadeWindows() {
+ // Don't do anything if we are in the menu
+ if (activeMenu != null) {
+ return;
+ }
+ int x = 0;
+ int y = 1;
+ List<TWindow> sorted = new LinkedList<TWindow>(windows);
+ Collections.sort(sorted);
+ Collections.reverse(sorted);
+ for (TWindow window: sorted) {
+ window.setX(x);
+ window.setY(y);
+ x++;
+ y++;
+ if (x > getScreen().getWidth()) {
+ x = 0;
+ }
+ if (y >= getScreen().getHeight()) {
+ y = 1;
+ }
+ }
+ }
+
+ /**
+ * Convenience function to add a timer.
+ *
+ * @param duration number of milliseconds to wait between ticks
+ * @param recurring if true, re-schedule this timer after every tick
+ * @param action function to call when button is pressed
+ * @return the timer
+ */
+ public final TTimer addTimer(final long duration, final boolean recurring,
+ final TAction action) {
+
+ TTimer timer = new TTimer(duration, recurring, action);
+ synchronized (timers) {
+ timers.add(timer);
+ }
+ return timer;
+ }
+
+ /**
+ * Convenience function to remove a timer.
+ *
+ * @param timer timer to remove
+ */
+ public final void removeTimer(final TTimer timer) {
+ synchronized (timers) {
+ timers.remove(timer);
+ }