summaryrefslogtreecommitdiff
path: root/NET/worlds/console/Cursor.java
blob: a9c9f3c89c58a10c65ce4ad3c441198f0a9cb0d2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package NET.worlds.console;

import NET.worlds.network.URL;
import NET.worlds.scape.BGLoaded;
import NET.worlds.scape.BackgroundLoader;
import NET.worlds.scape.NoSuchPropertyException;
import NET.worlds.scape.Property;
import NET.worlds.scape.Restorer;
import NET.worlds.scape.Room;
import NET.worlds.scape.Saver;
import NET.worlds.scape.SuperRoot;
import NET.worlds.scape.TooNewException;
import NET.worlds.scape.URLPropertyEditor;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.Enumeration;
import java.util.Hashtable;

public class Cursor extends SuperRoot implements BGLoaded {
   private URL url = URL.make("system:DEFAULT_CURSOR");
   private int hCursor;
   private static Cursor active;
   private static Hashtable<URL, Comparable> sysCursors = new Hashtable<URL, Comparable>();
   private static int defaultCursor = retrieveSystemCursor(addCursor("DEFAULT_CURSOR", "IDC_ARROW"));
   private static Object classCookie;

   static {
      assert defaultCursor != 0;

      addCursor("CROSSHAIR_CURSOR", "IDC_CROSS");
      addCursor("TEXT_CURSOR", "IDC_IBEAM");
      addCursor("WAIT_CURSOR", "IDC_WAIT");
      addCursor("NE_RESIZE_CURSOR", "IDC_SIZENESW");
      addCursor("SW_RESIZE_CURSOR", "IDC_SIZENESW");
      addCursor("NW_RESIZE_CURSOR", "IDC_SIZENWSE");
      addCursor("SE_RESIZE_CURSOR", "IDC_SIZENWSE");
      addCursor("N_RESIZE_CURSOR", "IDC_SIZENS");
      addCursor("S_RESIZE_CURSOR", "IDC_SIZENS");
      addCursor("W_RESIZE_CURSOR", "IDC_SIZEWE");
      addCursor("E_RESIZE_CURSOR", "IDC_SIZEWE");
      addCursor("HAND_CURSOR", "IDC_UPARROW");
      addCursor("MOVE_CURSOR", "IDC_SIZEALL");
      addCursor("CANNOT_CURSOR", "IDC_NO");
      classCookie = new Object();
   }

   private static URL addCursor(String javaName, String win32Name) {
      URL url = URL.make("system:" + javaName);
      sysCursors.put(url, win32Name);
      return url;
   }

   public Cursor() {
   }

   public Cursor(URL url) {
      this.setURL(url);
   }

   public void setURL(URL url) {
      this.url = url;
      if (!url.unalias().startsWith("system:")) {
         BackgroundLoader.get(this, url);
      } else {
         this.activate();
      }
   }

   public static Cursor getActive() {
      return active;
   }

   public void activate() {
      SuperRoot owner = this.getOwner();
      if (owner != null && owner == Console.getActive()) {
         int sysCurs = retrieveSystemCursor(this.url);
         if (sysCurs != 0) {
            Window.setCursor(sysCurs);
            this.maybeDestroyCursor();
            this.hCursor = sysCurs;
         } else if (this.hCursor != 0) {
            Window.setCursor(this.hCursor);
         }

         active = this;
      }
   }

   public void deactivate() {
      if (active == this) {
         active = null;
      }
   }

   public URL getURL() {
      return this.url;
   }

   public static Enumeration<URL> getSysCursorURLs() {
      return sysCursors.keys();
   }

   public static native int getSystemCursorWidth();

   public static native int getSystemCursorHeight();

   public static native int getSystemCursorDepth();

   private static native int loadCursor(String var0);

   private static native int loadSystemCursor(String var0);

   private static native void destroyCursor(int var0);

   private static int retrieveSystemCursor(URL url) {
      int handle = 0;
      Object c = sysCursors.get(url);
      if (c != null) {
         if (c instanceof String) {
            handle = loadSystemCursor((String)c);
            sysCursors.put(url, new Integer(handle));
         } else {
            handle = (Integer)c;
         }
      }

      return handle;
   }

   private void maybeDestroyCursor() {
      if (this.hCursor != 0) {
         if (!sysCursors.contains(new Integer(this.hCursor))) {
            destroyCursor(this.hCursor);
         }

         this.hCursor = 0;
      }
   }

   @Override
   public Object asyncBackgroundLoad(String localName, URL remoteName) {
      this.deactivate();
      this.maybeDestroyCursor();
      if (localName == null) {
         return null;
      } else {
         if ((this.hCursor = loadCursor(localName)) != 0) {
            this.activate();
         }

         return null;
      }
   }

   @Override
   public boolean syncBackgroundLoad(Object obj, URL remoteURL) {
      if (this.hCursor == 0) {
         Object[] arguments = new Object[]{new String("" + this.url)};
         Console.println(MessageFormat.format(Console.message("Load-cursor"), arguments));
      }

      return false;
   }

   @Override
   public Room getBackgroundLoadRoom() {
      SuperRoot owner = this.getOwner();
      return owner != null && owner instanceof Console ? ((Console)owner).getPilot().getRoom() : null;
   }

   @Override
   public Object properties(int index, int offset, int mode, Object value) throws NoSuchPropertyException {
      Object ret = null;
      switch (index - offset) {
         case 0:
            if (mode == 0) {
               ret = URLPropertyEditor.make(new Property(this, index, "File"), "cur;ani", getSysCursorURLs());
            } else if (mode == 1) {
               ret = this.getURL();
            } else if (mode == 2) {
               this.setURL((URL)value);
            }
            break;
         default:
            ret = super.properties(index, offset + 1, mode, value);
      }

      return ret;
   }

   @Override
   public void saveState(Saver s) throws IOException {
      s.saveVersion(1, classCookie);
      super.saveState(s);
      URL.save(s, this.url);
   }

   @Override
   public void restoreState(Restorer r) throws IOException, TooNewException {
      switch (r.restoreVersion(classCookie)) {
         case 0:
            r.restoreMaybeNull();
            String s = r.restoreString();
            if (!s.endsWith(".cur") && !s.endsWith(".ani")) {
               this.setURL(URL.make("system:" + this.url));
            } else {
               this.setURL(URL.restore(r, s, null));
            }
            break;
         case 1:
            super.restoreState(r);
            this.setURL(URL.restore(r));
            break;
         default:
            throw new TooNewException();
      }
   }
}