예제 #1
0
  /**
   * Write the device independent image array stored in the specified loader to the specified output
   * stream using the specified file format.
   */
  public static void save(OutputStream os, int format, ImageLoader loader) {
    if (format < 0 || format >= FORMATS.length) SWT.error(SWT.ERROR_UNSUPPORTED_FORMAT);
    if (FORMATS[format] == null) SWT.error(SWT.ERROR_UNSUPPORTED_FORMAT);
    if (loader.data == null || loader.data.length < 1) SWT.error(SWT.ERROR_INVALID_ARGUMENT);

    LEDataOutputStream stream = new LEDataOutputStream(os);
    FileFormat fileFormat = null;
    try {
      Class clazz = Class.forName(FORMAT_PACKAGE + '.' + FORMATS[format] + FORMAT_SUFFIX);
      fileFormat = (FileFormat) clazz.newInstance();
    } catch (Exception e) {
      SWT.error(SWT.ERROR_UNSUPPORTED_FORMAT);
    }
    if (format == SWT.IMAGE_BMP_RLE) {
      switch (loader.data[0].depth) {
        case 8:
          fileFormat.compression = 1;
          break;
        case 4:
          fileFormat.compression = 2;
          break;
      }
    }
    fileFormat.unloadIntoStream(loader, stream);
  }
 /** Read a palette from the input stream. */
 PaletteData readPalette(int numColors) {
   byte[] bytes = new byte[numColors * 3];
   try {
     if (inputStream.read(bytes) != bytes.length) SWT.error(SWT.ERROR_INVALID_IMAGE);
   } catch (IOException e) {
     SWT.error(SWT.ERROR_IO, e);
   }
   RGB[] colors = new RGB[numColors];
   for (int i = 0; i < numColors; i++)
     colors[i] = new RGB(bytes[i * 3] & 0xFF, bytes[i * 3 + 1] & 0xFF, bytes[i * 3 + 2] & 0xFF);
   return new PaletteData(colors);
 }
 void decompressData(byte[] src, byte[] dest, int stride, int cmp) {
   if (cmp == 1) { // BMP_RLE8_COMPRESSION
     if (decompressRLE8Data(src, src.length, stride, dest, dest.length) <= 0)
       SWT.error(SWT.ERROR_INVALID_IMAGE);
     return;
   }
   if (cmp == 2) { // BMP_RLE4_COMPRESSION
     if (decompressRLE4Data(src, src.length, stride, dest, dest.length) <= 0)
       SWT.error(SWT.ERROR_INVALID_IMAGE);
     return;
   }
   SWT.error(SWT.ERROR_INVALID_IMAGE);
 }
예제 #4
0
 /**
  * Read the specified input stream, and return the device independent image array represented by
  * the stream.
  */
 public ImageData[] loadFromStream(LEDataInputStream stream) {
   try {
     inputStream = stream;
     return loadFromByteStream();
   } catch (Exception e) {
     if (e instanceof IOException) {
       SWT.error(SWT.ERROR_IO, e);
     } else {
       SWT.error(SWT.ERROR_INVALID_IMAGE, e);
     }
     return null;
   }
 }
 int[] loadFileHeader() {
   int[] header = new int[5];
   try {
     header[0] = inputStream.readShort();
     header[1] = inputStream.readInt();
     header[2] = inputStream.readShort();
     header[3] = inputStream.readShort();
     header[4] = inputStream.readInt();
   } catch (IOException e) {
     SWT.error(SWT.ERROR_IO, e);
   }
   if (header[0] != 0x4D42) SWT.error(SWT.ERROR_INVALID_IMAGE);
   return header;
 }
예제 #6
0
  /**
   * Sets the font that the receiver will use to paint textual information for the specified cell in
   * this item to the font specified by the argument, or to the default font for that kind of
   * control if the argument is null.
   *
   * @param index the column index
   * @param font the new font (or null)
   * @exception IllegalArgumentException
   *     <ul>
   *       <li>ERROR_INVALID_ARGUMENT - if the argument has been disposed
   *     </ul>
   *
   * @exception SWTException
   *     <ul>
   *       <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed
   *       <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
   *     </ul>
   *
   * @since 3.0
   */
  public void setFont(int index, Font font) {
    checkWidget();
    if (font != null && font.isDisposed()) {
      SWT.error(SWT.ERROR_INVALID_ARGUMENT);
    }
    int count = Math.max(1, parent.getColumnCount());
    if (0 > index || index > count - 1) return;
    if (cellFont == null) {
      if (font == null) return;
      cellFont = new Font[count];
    }
    Font oldFont = cellFont[index];
    if (oldFont == font) return;
    cellFont[index] = font;
    if (oldFont != null && oldFont.equals(font)) return;

    int modelIndex =
        parent.columnCount == 0 ? Table.FIRST_COLUMN : parent.columns[index].modelIndex;
    int /*long*/ fontHandle = font != null ? font.handle : 0;
    OS.gtk_list_store_set(parent.modelHandle, handle, modelIndex + Table.CELL_FONT, fontHandle, -1);
    /*
     * Bug in GTK.  When using fixed-height-mode,
     * row changes do not cause the row to be repainted.  The fix is to
     * invalidate the row when it is cleared.
     */
    if ((parent.style & SWT.VIRTUAL) != 0) {
      if (OS.GTK_VERSION >= OS.VERSION(2, 3, 2) && OS.GTK_VERSION < OS.VERSION(2, 6, 3)) {
        redraw();
      }
    }
    cached = true;

    if (font != null) {
      boolean customDraw =
          (parent.columnCount == 0) ? parent.firstCustomDraw : parent.columns[index].customDraw;
      if (!customDraw) {
        if ((parent.style & SWT.VIRTUAL) == 0) {
          int /*long*/ parentHandle = parent.handle;
          int /*long*/ column = 0;
          if (parent.columnCount > 0) {
            column = parent.columns[index].handle;
          } else {
            column = OS.gtk_tree_view_get_column(parentHandle, index);
          }
          if (column == 0) return;
          int /*long*/ textRenderer = parent.getTextRenderer(column);
          int /*long*/ imageRenderer = parent.getPixbufRenderer(column);
          OS.gtk_tree_view_column_set_cell_data_func(
              column, textRenderer, display.cellDataProc, parentHandle, 0);
          OS.gtk_tree_view_column_set_cell_data_func(
              column, imageRenderer, display.cellDataProc, parentHandle, 0);
        }
        if (parent.columnCount == 0) {
          parent.firstCustomDraw = true;
        } else {
          parent.columns[index].customDraw = true;
        }
      }
    }
  }
예제 #7
0
 /**
  * Adds the listener to the collection of listeners who will be notified when the user changes the
  * receiver's selection, by sending it one of the messages defined in the <code>SelectionListener
  * </code> interface.
  *
  * <p>When <code>widgetSelected</code> is called, the item field of the event object is valid. If
  * the receiver has <code>SWT.CHECK</code> style set and the check selection changes, the event
  * object detail field contains the value <code>SWT.CHECK</code>. <code>widgetDefaultSelected
  * </code> is typically called when an item is double-clicked.
  *
  * @param listener the listener which should be notified when the user changes the receiver's
  *     selection
  * @exception IllegalArgumentException
  *     <ul>
  *       <li>ERROR_NULL_ARGUMENT - if the listener is null
  *     </ul>
  *
  * @exception SWTException
  *     <ul>
  *       <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed
  *       <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
  *     </ul>
  *
  * @see SelectionListener
  * @see SelectionEvent
  * @see #removeSelectionListener(SelectionListener)
  */
 public void addSelectionListener(SelectionListener listener) {
   checkWidget();
   if (listener == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
   TypedListener typedListener = new TypedListener(listener);
   addListener(SWT.Selection, typedListener);
   addListener(SWT.DefaultSelection, typedListener);
 }
 /** Write out a GraphicsControlBlock to describe the specified device independent image. */
 void writeGraphicsControlBlock(ImageData image) {
   try {
     outputStream.write(GIF_EXTENSION_BLOCK_ID);
     outputStream.write(GIF_GRAPHICS_CONTROL_BLOCK_ID);
     byte[] gcBlock = new byte[4];
     gcBlock[0] = 0;
     gcBlock[1] = 0;
     gcBlock[2] = 0;
     gcBlock[3] = 0;
     if (image.transparentPixel != -1) {
       gcBlock[0] = (byte) 0x01;
       gcBlock[3] = (byte) image.transparentPixel;
     }
     if (image.disposalMethod != 0) {
       gcBlock[0] |= (byte) ((image.disposalMethod & 0x07) << 2);
     }
     if (image.delayTime != 0) {
       gcBlock[1] = (byte) (image.delayTime & 0xFF);
       gcBlock[2] = (byte) ((image.delayTime >> 8) & 0xFF);
     }
     outputStream.write((byte) gcBlock.length);
     outputStream.write(gcBlock);
     outputStream.write(0); // Block terminator
   } catch (IOException e) {
     SWT.error(SWT.ERROR_IO, e);
   }
 }
 /**
  * We have just read the GraphicsControl extension identifier from the input stream. Read in the
  * control information, store it, and return it.
  */
 byte[] readGraphicsControlExtension() {
   try {
     // Read size of block = 0x04.
     inputStream.read();
     // Read the control block.
     byte[] controlBlock = new byte[4];
     inputStream.read(controlBlock);
     byte bitField = controlBlock[0];
     // Store the user input field.
     // userInput = (bitField & 0x02) != 0;
     // Store the disposal method.
     disposalMethod = (bitField >> 2) & 0x07;
     // Store the delay time.
     delayTime = (controlBlock[1] & 0xFF) | ((controlBlock[2] & 0xFF) << 8);
     // Store the transparent color.
     if ((bitField & 0x01) != 0) {
       transparentPixel = controlBlock[3] & 0xFF;
     } else {
       transparentPixel = -1;
     }
     return controlBlock;
   } catch (Exception e) {
     SWT.error(SWT.ERROR_IO, e);
     return null;
   }
 }
예제 #10
0
 /**
  * Finds the program that is associated with an extension. The extension may or may not begin with
  * a '.'. Note that a <code>Display</code> must already exist to guarantee that this method
  * returns an appropriate result.
  *
  * @param extension the program extension
  * @return the program or <code>null</code>
  * @exception IllegalArgumentException
  *     <ul>
  *       <li>ERROR_NULL_ARGUMENT when extension is null
  *     </ul>
  */
 public static Program findProgram(String extension) {
   NSAutoreleasePool pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
   try {
     if (extension == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
     if (extension.length() == 0) return null;
     Program program = null;
     char[] chars;
     if (extension.charAt(0) != '.') {
       chars = new char[extension.length()];
       extension.getChars(0, chars.length, chars, 0);
     } else {
       chars = new char[extension.length() - 1];
       extension.getChars(1, extension.length(), chars, 0);
     }
     NSString ext = NSString.stringWithCharacters(chars, chars.length);
     if (ext != null) {
       byte[] fsRef = new byte[80];
       if (OS.LSGetApplicationForInfo(
               OS.kLSUnknownType, OS.kLSUnknownCreator, ext.id, OS.kLSRolesAll, fsRef, null)
           == OS.noErr) {
         long /*int*/ url = OS.CFURLCreateFromFSRef(OS.kCFAllocatorDefault(), fsRef);
         if (url != 0) {
           NSString bundlePath = new NSURL(url).path();
           NSBundle bundle = NSBundle.bundleWithPath(bundlePath);
           if (bundle != null) program = getProgram(bundle);
           OS.CFRelease(url);
         }
       }
     }
     return program;
   } finally {
     pool.release();
   }
 }
 /**
  * Unload the given image's data into the given byte stream using the given compression strategy.
  * Answer the number of bytes written. Method modified to use the passed data if it is not null.
  */
 int unloadData(ImageData image, byte[] data, OutputStream out, int comp) {
   int totalSize = 0;
   try {
     if (comp == 0) return unloadDataNoCompression(image, data, out);
     int bpl = (image.width * image.depth + 7) / 8;
     int bmpBpl = (bpl + 3) / 4 * 4; // BMP pads scanlines to multiples of 4 bytes
     int imageBpl = image.bytesPerLine;
     // Compression can actually take twice as much space, in worst case
     byte[] buf = new byte[bmpBpl * 2];
     int srcOffset = imageBpl * (image.height - 1); // Start at last line
     if (data == null) data = image.data;
     totalSize = 0;
     byte[] buf2 = new byte[32768];
     int buf2Offset = 0;
     for (int y = image.height - 1; y >= 0; y--) {
       int lineSize = compress(comp, data, srcOffset, bpl, buf, y == 0);
       if (buf2Offset + lineSize > buf2.length) {
         out.write(buf2, 0, buf2Offset);
         buf2Offset = 0;
       }
       System.arraycopy(buf, 0, buf2, buf2Offset, lineSize);
       buf2Offset += lineSize;
       totalSize += lineSize;
       srcOffset -= imageBpl;
     }
     if (buf2Offset > 0) out.write(buf2, 0, buf2Offset);
   } catch (IOException e) {
     SWT.error(SWT.ERROR_IO, e);
   }
   return totalSize;
 }
  /**
   * Creates an instance of a ControlExample embedded inside the supplied parent Composite.
   *
   * @param parent the container of the example
   */
  public ControlExample(Composite parent) {
    initResources();
    tabFolder = new TabFolder(parent, SWT.NONE);
    tabs = createTabs();
    for (Tab tab : tabs) {
      TabItem item = new TabItem(tabFolder, SWT.NONE);
      item.setText(tab.getTabText());
      item.setControl(tab.createTabFolderPage(tabFolder));
      item.setData(tab);
    }

    /* Workaround: if the tab folder is wider than the screen,
     * Mac platforms clip instead of somehow scrolling the tab items.
     * We try to recover some width by using shorter tab names. */
    Point size = parent.computeSize(SWT.DEFAULT, SWT.DEFAULT);
    Rectangle monitorArea = parent.getMonitor().getClientArea();
    boolean isMac = SWT.getPlatform().equals("cocoa");
    if (size.x > monitorArea.width && isMac) {
      TabItem[] tabItems = tabFolder.getItems();
      for (int i = 0; i < tabItems.length; i++) {
        tabItems[i].setText(tabs[i].getShortTabText());
      }
    }
    startup = false;
  }
 /** Read and return the next block or extension identifier from the file. */
 int readID() {
   try {
     return inputStream.read();
   } catch (IOException e) {
     SWT.error(SWT.ERROR_IO, e);
   }
   return -1;
 }
예제 #14
0
 /**
  * Positions the TableCursor over the cell at the given row and column in the parent table.
  *
  * @param row the index of the row for the cell to select
  * @param column the index of column for the cell to select
  * @exception SWTException
  *     <ul>
  *       <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed
  *       <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
  *     </ul>
  */
 public void setSelection(int row, int column) {
   checkWidget();
   int columnCount = table.getColumnCount();
   int maxColumnIndex = columnCount == 0 ? 0 : columnCount - 1;
   if (row < 0 || row >= table.getItemCount() || column < 0 || column > maxColumnIndex)
     SWT.error(SWT.ERROR_INVALID_ARGUMENT);
   setRowColumn(row, column, false);
 }
예제 #15
0
 /**
  * Positions the TableCursor over the cell at the given row and column in the parent table.
  *
  * @param row the TableItem of the row for the cell to select
  * @param column the index of column for the cell to select
  * @exception SWTException
  *     <ul>
  *       <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed
  *       <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
  *     </ul>
  */
 public void setSelection(TableItem row, int column) {
   checkWidget();
   int columnCount = table.getColumnCount();
   int maxColumnIndex = columnCount == 0 ? 0 : columnCount - 1;
   if (row == null || row.isDisposed() || column < 0 || column > maxColumnIndex)
     SWT.error(SWT.ERROR_INVALID_ARGUMENT);
   setRowColumn(table.indexOf(row), column, false);
 }
예제 #16
0
 /**
  * Removes the listener from the collection of listeners who will be notified when the user
  * changes the receiver's selection.
  *
  * @param listener the listener which should no longer be notified
  * @exception IllegalArgumentException
  *     <ul>
  *       <li>ERROR_NULL_ARGUMENT - if the listener is null
  *     </ul>
  *
  * @exception SWTException
  *     <ul>
  *       <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed
  *       <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
  *     </ul>
  *
  * @see SelectionListener
  * @see #addSelectionListener(SelectionListener)
  * @since 3.0
  */
 public void removeSelectionListener(SelectionListener listener) {
   checkWidget();
   if (listener == null) {
     SWT.error(SWT.ERROR_NULL_ARGUMENT);
   }
   removeListener(SWT.Selection, listener);
   removeListener(SWT.DefaultSelection, listener);
 }
예제 #17
0
 /**
  * Set the horizontal alignment of the CLabel. Use the values LEFT, CENTER and RIGHT to align
  * image and text within the available space.
  *
  * @param align the alignment style of LEFT, RIGHT or CENTER
  * @exception SWTException
  *     <ul>
  *       <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed
  *       <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
  *       <li>ERROR_INVALID_ARGUMENT - if the value of align is not one of SWT.LEFT, SWT.RIGHT or
  *           SWT.CENTER
  *     </ul>
  */
 public void setAlignment(int align) {
   checkWidget();
   if (align != SWT.LEFT && align != SWT.RIGHT && align != SWT.CENTER) {
     SWT.error(SWT.ERROR_INVALID_ARGUMENT);
   }
   if (this.align != align) {
     this.align = align;
     redraw();
   }
 }
 /**
  * Compress numBytes bytes of image data from src, storing in dest (starting at 0), using the
  * technique specified by comp. If last is true, this indicates the last line of the image. Answer
  * the size of the compressed data.
  */
 int compress(int comp, byte[] src, int srcOffset, int numBytes, byte[] dest, boolean last) {
   if (comp == 1) { // BMP_RLE8_COMPRESSION
     return compressRLE8Data(src, srcOffset, numBytes, dest, last);
   }
   if (comp == 2) { // BMP_RLE4_COMPRESSION
     return compressRLE4Data(src, srcOffset, numBytes, dest, last);
   }
   SWT.error(SWT.ERROR_INVALID_IMAGE);
   return 0;
 }
 PaletteData loadPalette(byte[] infoHeader) {
   int depth = (infoHeader[14] & 0xFF) | ((infoHeader[15] & 0xFF) << 8);
   if (depth <= 8) {
     int numColors =
         (infoHeader[32] & 0xFF)
             | ((infoHeader[33] & 0xFF) << 8)
             | ((infoHeader[34] & 0xFF) << 16)
             | ((infoHeader[35] & 0xFF) << 24);
     if (numColors == 0) {
       numColors = 1 << depth;
     } else {
       if (numColors > 256) numColors = 256;
     }
     byte[] buf = new byte[numColors * 4];
     try {
       if (inputStream.read(buf) != buf.length) SWT.error(SWT.ERROR_INVALID_IMAGE);
     } catch (IOException e) {
       SWT.error(SWT.ERROR_IO, e);
     }
     return paletteFromBytes(buf, numColors);
   }
   if (depth == 16) {
     if (this.compression == 3) {
       try {
         return new PaletteData(
             inputStream.readInt(), inputStream.readInt(), inputStream.readInt());
       } catch (IOException e) {
         SWT.error(SWT.ERROR_IO, e);
       }
     }
     return new PaletteData(0x7C00, 0x3E0, 0x1F);
   }
   if (depth == 24) return new PaletteData(0xFF, 0xFF00, 0xFF0000);
   if (this.compression == 3) {
     try {
       return new PaletteData(inputStream.readInt(), inputStream.readInt(), inputStream.readInt());
     } catch (IOException e) {
       SWT.error(SWT.ERROR_IO, e);
     }
   }
   return new PaletteData(0xFF00, 0xFF0000, 0xFF000000);
 }
예제 #20
0
 /**
  * Executes the program with the file as the single argument in the operating system. It is the
  * responsibility of the programmer to ensure that the file contains valid data for this program.
  *
  * @param fileName the file or program name
  * @return <code>true</code> if the file is launched, otherwise <code>false</code>
  * @exception IllegalArgumentException
  *     <ul>
  *       <li>ERROR_NULL_ARGUMENT when fileName is null
  *     </ul>
  */
 public boolean execute(String fileName) {
   if (fileName == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
   NSAutoreleasePool pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
   try {
     NSWorkspace workspace = NSWorkspace.sharedWorkspace();
     NSURL url = getURL(fileName);
     NSArray urls = NSArray.arrayWithObject(url);
     return workspace.openURLs(urls, NSString.stringWith(identifier), 0, null, 0);
   } finally {
     pool.release();
   }
 }
 /** Read a control extension. Return the extension block data. */
 byte[] readExtension() {
   int extensionID = readID();
   if (extensionID == GIF_COMMENT_BLOCK_ID) return readCommentExtension();
   if (extensionID == GIF_PLAIN_TEXT_BLOCK_ID) return readPlainTextExtension();
   if (extensionID == GIF_GRAPHICS_CONTROL_BLOCK_ID) return readGraphicsControlExtension();
   if (extensionID == GIF_APPLICATION_EXTENSION_BLOCK_ID) return readApplicationExtension();
   // Otherwise, we don't recognize the block. If the
   // field size is correct, we can just skip over
   // the block contents.
   try {
     int extSize = inputStream.read();
     if (extSize < 0) {
       SWT.error(SWT.ERROR_INVALID_IMAGE);
     }
     byte[] ext = new byte[extSize];
     inputStream.read(ext, 0, extSize);
     return ext;
   } catch (IOException e) {
     SWT.error(SWT.ERROR_IO, e);
     return null;
   }
 }
예제 #22
0
 /**
  * Write the device independent image array stored in the specified loader to the specified output
  * stream.
  */
 public void unloadIntoStream(ImageLoader loader, LEDataOutputStream stream) {
   try {
     outputStream = stream;
     unloadIntoByteStream(loader);
     outputStream.flush();
   } catch (Exception e) {
     try {
       outputStream.flush();
     } catch (Exception f) {
     }
     SWT.error(SWT.ERROR_IO, e);
   }
 }
 byte[] loadData(byte[] infoHeader, int stride) {
   int height =
       (infoHeader[8] & 0xFF)
           | ((infoHeader[9] & 0xFF) << 8)
           | ((infoHeader[10] & 0xFF) << 16)
           | ((infoHeader[11] & 0xFF) << 24);
   if (height < 0) height = -height;
   int dataSize = height * stride;
   byte[] data = new byte[dataSize];
   int cmp =
       (infoHeader[16] & 0xFF)
           | ((infoHeader[17] & 0xFF) << 8)
           | ((infoHeader[18] & 0xFF) << 16)
           | ((infoHeader[19] & 0xFF) << 24);
   if (cmp == 0 || cmp == 3) { // BMP_NO_COMPRESSION
     try {
       if (inputStream.read(data) != dataSize) SWT.error(SWT.ERROR_INVALID_IMAGE);
     } catch (IOException e) {
       SWT.error(SWT.ERROR_IO, e);
     }
   } else {
     int compressedSize =
         (infoHeader[20] & 0xFF)
             | ((infoHeader[21] & 0xFF) << 8)
             | ((infoHeader[22] & 0xFF) << 16)
             | ((infoHeader[23] & 0xFF) << 24);
     byte[] compressed = new byte[compressedSize];
     try {
       if (inputStream.read(compressed) != compressedSize) SWT.error(SWT.ERROR_INVALID_IMAGE);
     } catch (IOException e) {
       SWT.error(SWT.ERROR_IO, e);
     }
     decompressData(compressed, data, stride, cmp);
   }
   return data;
 }
 /** Write the specified palette to the output stream. */
 void writePalette(PaletteData palette, int depth) {
   byte[] bytes = new byte[(1 << depth) * 3];
   int offset = 0;
   for (int i = 0; i < palette.colors.length; i++) {
     RGB color = palette.colors[i];
     bytes[offset] = (byte) color.red;
     bytes[offset + 1] = (byte) color.green;
     bytes[offset + 2] = (byte) color.blue;
     offset += 3;
   }
   try {
     outputStream.write(bytes);
   } catch (IOException e) {
     SWT.error(SWT.ERROR_IO, e);
   }
 }
 /**
  * We have just read the Comment extension identifier from the input stream. Read in the rest of
  * the comment and return it. GIF comment blocks are variable size.
  */
 byte[] readCommentExtension() {
   try {
     byte[] comment = new byte[0];
     byte[] block = new byte[255];
     int size = inputStream.read();
     while ((size > 0) && (inputStream.read(block, 0, size) != -1)) {
       byte[] oldComment = comment;
       comment = new byte[oldComment.length + size];
       System.arraycopy(oldComment, 0, comment, 0, oldComment.length);
       System.arraycopy(block, 0, comment, oldComment.length, size);
       size = inputStream.read();
     }
     return comment;
   } catch (Exception e) {
     SWT.error(SWT.ERROR_IO, e);
     return null;
   }
 }
예제 #26
0
 /**
  * Read the specified input stream using the specified loader, and return the device independent
  * image array represented by the stream.
  */
 public static ImageData[] load(InputStream is, ImageLoader loader) {
   FileFormat fileFormat = null;
   LEDataInputStream stream = new LEDataInputStream(is);
   for (int i = 1; i < FORMATS.length; i++) {
     if (FORMATS[i] != null) {
       try {
         fileFormat = getFileFormat(stream, FORMATS[i]);
         if (fileFormat != null) break;
       } catch (ClassNotFoundException e) {
         FORMATS[i] = null;
       } catch (Exception e) {
       }
     }
   }
   if (fileFormat == null) SWT.error(SWT.ERROR_UNSUPPORTED_FORMAT);
   fileFormat.loader = loader;
   return fileFormat.loadFromStream(stream);
 }
 /**
  * Prepare the given image's data for unloading into a byte stream using no compression strategy.
  * Answer the number of bytes written. Method modified to use the passed data if it is not null.
  */
 int unloadDataNoCompression(ImageData image, byte[] data, OutputStream out) {
   int bmpBpl = 0;
   try {
     int bpl = (image.width * image.depth + 7) / 8;
     bmpBpl = (bpl + 3) / 4 * 4; // BMP pads scanlines to multiples of 4 bytes
     int linesPerBuf = 32678 / bmpBpl;
     byte[] buf = new byte[linesPerBuf * bmpBpl];
     if (data == null) data = image.data;
     int imageBpl = image.bytesPerLine;
     int dataIndex = imageBpl * (image.height - 1); // Start at last line
     if (image.depth == 16) {
       for (int y = 0; y < image.height; y += linesPerBuf) {
         int count = image.height - y;
         if (linesPerBuf < count) count = linesPerBuf;
         int bufOffset = 0;
         for (int i = 0; i < count; i++) {
           for (int wIndex = 0; wIndex < bpl; wIndex += 2) {
             buf[bufOffset + wIndex + 1] = data[dataIndex + wIndex + 1];
             buf[bufOffset + wIndex] = data[dataIndex + wIndex];
           }
           bufOffset += bmpBpl;
           dataIndex -= imageBpl;
         }
         out.write(buf, 0, bufOffset);
       }
     } else {
       for (int y = 0; y < image.height; y += linesPerBuf) {
         int tmp = image.height - y;
         int count = tmp < linesPerBuf ? tmp : linesPerBuf;
         int bufOffset = 0;
         for (int i = 0; i < count; i++) {
           System.arraycopy(data, dataIndex, buf, bufOffset, bpl);
           bufOffset += bmpBpl;
           dataIndex -= imageBpl;
         }
         out.write(buf, 0, bufOffset);
       }
     }
   } catch (IOException e) {
     SWT.error(SWT.ERROR_IO, e);
   }
   return bmpBpl * image.height;
 }
예제 #28
0
 /**
  * Launches the operating system executable associated with the file or URL (http:// or https://).
  * If the file is an executable then the executable is launched. The program is launched with the
  * specified working directory only when the <code>workingDir</code> exists and <code>fileName
  * </code> is an executable. Note that a <code>Display</code> must already exist to guarantee that
  * this method returns an appropriate result.
  *
  * @param fileName the file name or program name or URL (http:// or https://)
  * @param workingDir the name of the working directory or null
  * @return <code>true</code> if the file is launched, otherwise <code>false</code>
  * @exception IllegalArgumentException
  *     <ul>
  *       <li>ERROR_NULL_ARGUMENT when fileName is null
  *     </ul>
  *
  * @since 3.6
  */
 public static boolean launch(String fileName, String workingDir) {
   if (fileName == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
   NSAutoreleasePool pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
   try {
     if (workingDir != null && isExecutable(fileName)) {
       try {
         Compatibility.exec(new String[] {fileName}, null, workingDir);
         return true;
       } catch (IOException e) {
         return false;
       }
     }
     NSURL url = getURL(fileName);
     NSWorkspace workspace = NSWorkspace.sharedWorkspace();
     return workspace.openURL(url);
   } finally {
     pool.release();
   }
 }
예제 #29
0
 /**
  * Sets the selection state of the receiver.
  *
  * <p>When the receiver is of type <code>CHECK</code> or <code>RADIO</code>, it is selected when
  * it is checked.
  *
  * @param selected the new selection state
  * @exception SWTException
  *     <ul>
  *       <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed
  *       <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
  *     </ul>
  */
 public void setSelection(boolean selected) {
   checkWidget();
   if ((style & (SWT.CHECK | SWT.RADIO)) == 0) return;
   if ((OS.IsPPC || OS.IsSP) && parent.hwndCB != 0) return;
   long /*int*/ hMenu = parent.handle;
   if (OS.IsWinCE) {
     int index = parent.indexOf(this);
     if (index == -1) return;
     int uCheck = OS.MF_BYPOSITION | (selected ? OS.MF_CHECKED : OS.MF_UNCHECKED);
     OS.CheckMenuItem(hMenu, index, uCheck);
   } else {
     MENUITEMINFO info = new MENUITEMINFO();
     info.cbSize = MENUITEMINFO.sizeof;
     info.fMask = OS.MIIM_STATE;
     boolean success = OS.GetMenuItemInfo(hMenu, id, false, info);
     if (!success) error(SWT.ERROR_CANNOT_SET_SELECTION);
     info.fState &= ~OS.MFS_CHECKED;
     if (selected) info.fState |= OS.MFS_CHECKED;
     success = OS.SetMenuItemInfo(hMenu, id, false, info);
     if (!success) {
       /*
        * Bug in Windows.  For some reason SetMenuItemInfo(),
        * returns a fail code when setting the enabled or
        * selected state of a default item, but sets the
        * state anyway.  The fix is to ignore the error.
        *
        * NOTE:  This only happens on Vista.
        */
       if (!OS.IsWinCE && OS.WIN32_VERSION >= OS.VERSION(6, 0)) {
         success = id == OS.GetMenuDefaultItem(hMenu, OS.MF_BYCOMMAND, OS.GMDI_USEDISABLED);
       }
       if (!success) {
         int error = OS.GetLastError();
         SWT.error(
             SWT.ERROR_CANNOT_SET_SELECTION,
             null,
             " [GetLastError=0x" + Integer.toHexString(error) + "]"); // $NON-NLS-1$ $NON-NLS-2$
       }
     }
   }
   parent.redraw();
 }
예제 #30
0
 /**
  * Sets the receiver's background color to the color specified by the argument, or to the default
  * system color for the item if the argument is null.
  *
  * @param color the new color (or null)
  * @exception IllegalArgumentException
  *     <ul>
  *       <li>ERROR_INVALID_ARGUMENT - if the argument has been disposed
  *     </ul>
  *
  * @exception SWTException
  *     <ul>
  *       <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed
  *       <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
  *     </ul>
  *
  * @since 2.0
  */
 public void setBackground(Color color) {
   checkWidget();
   if (color != null && color.isDisposed()) {
     SWT.error(SWT.ERROR_INVALID_ARGUMENT);
   }
   if (_getBackground().equals(color)) return;
   GdkColor gdkColor = color != null ? color.handle : null;
   OS.gtk_list_store_set(parent.modelHandle, handle, Table.BACKGROUND_COLUMN, gdkColor, -1);
   /*
    * Bug in GTK.  When using fixed-height-mode,
    * row changes do not cause the row to be repainted.  The fix is to
    * invalidate the row when it is cleared.
    */
   if ((parent.style & SWT.VIRTUAL) != 0) {
     if (OS.GTK_VERSION >= OS.VERSION(2, 3, 2) && OS.GTK_VERSION < OS.VERSION(2, 6, 3)) {
       redraw();
     }
   }
   cached = true;
 }