⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 canvashandler1.java

📁 类似于Windows上的Excel
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * MC2 -- j2me spreadsheet
 *
 * Copyright (c) 2004-2006 Michael Zemljanukha (mixaz@mail.ru)
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

package com.wapindustrial.calc;
import java.util.*;

import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

/**
 * Extends FunctionObject to implement spreadshet ops such as sheet ops, cells, etc
 * to remove them from simple classes
 * Abstraction layer to handle different types of Canvas - Canvas, GameCanvas, FullCanvas, etc
 *
 * May be extended to handle AWT, etc types, by adding additional interface instead of Canvas
 *
 */
public class CanvasHandler1 extends LispObject implements CommandListener 
{
    
    static public CanvasHandler1 canvasHandler;  // singleton
    
    public MIDlet midlet;
    public Canvas canvas;
    Font font = Font.getDefaultFont();
    public Display display;
    boolean started = false;

    private static final String POUNDS = "#######";
    
//    final static int MAX_COLUMNS_ROWS[] = { 8, 32 };
    
    // directions of cursor's movement
    final static int CURSOR_UP_LEFT     = -1;
    final static int CURSOR_NONE        = 0;
    final static int CURSOR_DOWN_RIGHT  = 1;
    
    /* 2 axis-depended things
     */
    /** left-top absolute window (in abs. pixels) */
    int windowx1,windowy1;
    /** right-bottom absolute window (in abs. pixels) */
    int windowx2,windowy2;
    /** cursor's location, I = Y, J = X */
    int cursorx,cursory;
    /** top-left selection */
    int selectionx1,selectiony1;
    /** right-bottom selection */
    int selectionx2,selectiony2;
    /** font size*/
    int fontWidth, fontHeight;
    int fontSize[] = new int[2];
    /** screen size */
    int canvasWidth, canvasHeight;
    /** header row/columns ( column header may be used by statusLine if not null )
     * row/column names
     */
    int headerWidth, headerHeight;
    
    int headerFontColor = 0x000000;
    int headerBackColor = 0xffffff;
    int cellFontColor = 0x000000;
    int cellBackColor = 0xffffff;
    int gridColor = 0x00;
    int gridSelectedColor = 0xffffff;
    int selectionFontColor = 0xffffff;
    int selectionBackColor = 0x00;
    
    /** active sheet of active workbook */
    public Sheet sheet;

    /** normal mode of screenmode */
    private static final int MODE_NORMAL = 1;   // screen mode
    /** entering numeric constant */
    private static final int MODE_NUMERIC = 2;
    /** showing cell formula */
    private static final int MODE_FORMULA = 4;
    /** screen mode */
    int screenmode;
    
    /** entering gold keys */
    boolean goldmode;
    int goldkey = Canvas.KEY_POUND;
    // used in MODE_GOLD
    final short goldkeys[] = new short[10];
    int ngoldkeys;
    
    // used in MODE_NUMERIC & MODE_FORMULA
    String formula;

    /** debug level to print s-exp being executed */
    static int debugLevel = 1;
    
//#ifdef HISTORY    
    /** max history size */
    private static final int MAX_HISTORY = 30;
    
    /** history, of String objects */
    static final Vector history = new Vector( MAX_HISTORY );
    
    public static final void addToHistory( String ss ) {
        if( ss == null )
            ss = "<null>";
        if( history.size() >= MAX_HISTORY )
            history.removeElementAt( 0 );
        history.addElement( ss );
    }
//#endif
    
    public CanvasHandler1( MIDlet _midlet, Canvas _canvas ) {
        midlet = _midlet;
        canvas = _canvas;
    }
    public void setScreenMode( int mode ) {
        screenmode = mode;
        formula = "";
    }
    
    public void startApp() {
        if( !started ) {

            started = true;

            fontSize[0] = fontWidth = font.charWidth( '#' );
            fontSize[1] = fontHeight = font.getHeight();
            canvasWidth = canvas.getWidth();
            canvasHeight = canvas.getHeight();
            
            headerWidth = fontWidth * 2;
            headerHeight = fontHeight;
            
            setScreenMode( MODE_NORMAL );
            
            canvasHandler = this;

            ModuleHandler.MODULEHANDLER.initializeNames();
            
            display = Display.getDisplay( midlet );
            display.setCurrent( canvas );
            
            // set sheet before evaluating startup code because it uses it (sheet-activate)
            sheet = new Sheet();
            sheetInitialize();
            
            // startup routines
            // built-in java code
//#ifdef SUNONE
            String startup = "(startup.Startup.startup)";
//#else            
//#            String startup = midlet.getAppProperty("startup");
//#endif            
            
            LispObject startupcode = null;
            if( startup != null ) {
                try {
                    debug( "evaluating " + startup );
                    Runtime.getRuntime().gc();
                    startupcode = LispObject.parse(startup);
//                    startupcode.interpret();
                }
                catch( Exception ee ) {
                    debug( "exception when processing startapp:" );
                    debug( ee.getMessage() );
                    ee.printStackTrace();
                }
            }
            
            LispTask.LispMachine = new LispTask();
            LispTask.LispMachine.start();
            
            if(startupcode!=null) {
                LispTask.LispMachine.putCommand(startupcode);
            }
           
            
        }
        else {
            // restoring after pause
        }
    }
    
    public void pauseApp() {
    }
    
    public void destroyApp() {
    	LispTask.LispMachine.stop = true;
    }
    
//#ifdef MIDP
    public void keyPressed(int keycode) {
//        debug("keycode="+keycode);
//        System.out.println(keycode);
        if( keycode == goldkey ) {
            // do nothing if other mode
            goldmode = !goldmode;
            ngoldkeys = 0;
//            formula = "";    // clear formulabar
            repaintFormulaBar();
            return;
        }
        if( goldmode ) {
            goldkeys[ngoldkeys++] = (short)keycode;
            Assigment ass = ScreenLite.findGoldBinding( goldkeys, ngoldkeys );
            if( ass != null ) {
                goldmode = false;
            	LispTask.LispMachine.putCommand( ass.code );
            }
            repaintFormulaBar();
            return;
        }
        
        int gameaction = canvas.getGameAction( keycode );
        Assigment ass = ScreenLite.findKeyBinding( screenmode, gameaction, keycode );
        
        if( ass != null ) {
        	LispTask.LispMachine.putCommand( ass.code );
        }
        else {
            debug("unbinded keycode received, action="+gameaction+" key="+keycode);
        }
        
    }
    
    public void keyRepeated(int keycode) {
        keyPressed( keycode );
    }

//#ifdef POINTER    
    public void pointerPressed(int x, int y) {
        LispTask.LispMachine.putCommand( 
            new FunctorList2(
                ScreenLite.SCREENLITE.table[ScreenLite.INDEX_POINTER_PRESSED],
                new ShortAtom(x),
                new ShortAtom(y)
            ) 
        );
    }
//#endif    
    
    public void commandAction( Command cmd, Displayable dsp ) { 
        Assigment ass = ScreenLite.findCommandBinding( cmd );
        if( ass != null ) {
        	LispTask.LispMachine.putCommand( ass.code );
        }
        else {
            debug("unbinded command received");
        }
    }
//#endif

    /* ==========================================================================================
     * Window ops
     * ==========================================================================================
     */

    private void setWindowX( int _cursorx, int _dir) {

        int size = canvasWidth - headerWidth;

        cursorx = _cursorx;
        if (_dir == CURSOR_DOWN_RIGHT) {
            windowx1 = getCellCoord( 0, _cursorx );
            windowx2 = windowx1 + size;
        } else if (_dir == CURSOR_UP_LEFT) {
            windowx2 = getCellCoord( 0, _cursorx + 1);
            windowx1 = windowx2 - size;
        }

        // low border reached
        if (windowx1 < 0) {
            windowx1 = 0;
            windowx2 = size;
        }
        // high border reached
        else {
            int nn = getCellCoord( 0, sheet.visibleX );
            if (windowx2 > nn && nn > size) {
                windowx2 = nn;
                windowx1 = windowx2 - size;
            }
        }
    }

    private void setWindowY( int _cursory, int _dir) {

        int size = canvasHeight - headerHeight;

        cursory = _cursory;

        if (_dir == CURSOR_DOWN_RIGHT) {
            windowy1 = getCellCoord( 1, _cursory );
            windowy2 = windowy1 + size;
        } else if (_dir == CURSOR_UP_LEFT) {
            windowy2 = getCellCoord( 1, _cursory + 1);
            windowy1 = windowy2 - size;
        }

        // low border reached
        if (windowy1 < 0) {
            windowy1 = 0;
            windowy2 = size;
        }
        // high border reached
        else {
            int nn = getCellCoord( 1, sheet.visibleY );
            if (windowy2 > nn && nn > size) {
                windowy2 = nn;
                windowy1 = windowy2 - size;
            }
        }
    }

    void repaintCell(int i, int j) {
        canvas.repaint(
            getScreenCellCoordX(j), 
            getScreenCellCoordY(i), 
            getRowColumnSize( 0,j ),
            getRowColumnSize( 1,i )
        );
    }

//#ifdef MIDP
    void repaint() {
        canvas.repaint();
    }
    void repaintFormulaBar() {
        canvas.repaint( 0, 0, canvasWidth, headerHeight );
    }
//#endif    

    // returs 0 if no change, 1 if cell repaint, 2 if total repaint
    int setCursorX( int j ) {
        // no need to change
        if( cursorx == j )
            return 0;
        
        int dirij = CURSOR_NONE;
        boolean willRepaint = false;
        int maxsize = sheet.visibleX;

        j = (j+maxsize) % maxsize;
        int cellcoord = getCellCoord( 0, j );
        if( cellcoord < windowx1 ) {
            willRepaint = true;
            dirij = CURSOR_UP_LEFT;
        }
        else {
            int nextcoord = cellcoord + getRowColumnSize( 0, j+1 );
            if( nextcoord >= windowx2 ) {
                willRepaint = true;
                dirij = CURSOR_DOWN_RIGHT;
            }
        }
        if( willRepaint ) {
            setWindowX( j, dirij );
            return 2;
        }
        // only one cell repaint
        cursorx = j;
        return 1;
    }
    
    int setCursorY( int i ) {
        // no need to change
        if( cursory == i )
            return 0;
        
        int dirij = CURSOR_NONE;
        boolean willRepaint = false;
        int maxsize = sheet.visibleY;

        i = (i+maxsize) % maxsize;
        int cellcoord = getCellCoord( 1, i );
        if( cellcoord < windowy1 ) {
            willRepaint = true;
            dirij = CURSOR_UP_LEFT;
        }
        else {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -