339 lines
7.4 KiB
C++
Raw Normal View History

/***************************************************************************
CPicture.cpp
(c) 2004-2006 - Daniel Campos Fernández <dcamposf@gmail.com>
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, 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., 675 Mass Ave, Cambridge, MA 02139, USA.
***************************************************************************/
#define __CPICTURE_CPP
#include "main.h"
#include "gambas.h"
#include "widgets.h"
#include "ggambastag.h"
#include "CDraw.h"
#include "cpaint_impl.h"
#include "CScreen.h"
#include "CImage.h"
#include "CPicture.h"
CPICTURE *CPICTURE_create(gPicture *picture)
{
CPICTURE *pic;
GB.New((void **)POINTER(&pic), GB.FindClass("Picture"), 0, 0);
if (picture)
{
pic->picture->unref();
pic->picture = picture;
[CONFIGURATION] * NEW: Upgrade libtool autoconf macros and libltdl sources to the 1.5.26 version. [DEVELOPMENT ENVIRONMENT] * BUG: Control and window dimensions can go up to 4096x4096 pixels. * BUG: When unchecking GUI components in a project, the edited forms are automatically closed. * BUG: Do not use the form icon on form class editors when refreshing the project. * BUG: In the icon editor, filling with a transparent color won't enter an infinite loop anymore. * BUG: Selecting the "Collection" word while debugging does not crash the IDE anymore. * NEW: Pressing Escape now closes a debugging window. * BUG: The 'Minimize on run' option works correctly now. [INTERPRETER] * BUG: SUPER now works inside overriden static methods. [GB.DB.ODBC] * BUG: Handle ODBC drivers that can return the number of records in a query better. [GB.DEBUG] * BUG: If there is an I/O error between a debugged process and the IDE, the process is aborted. * BUG: Evaluating a class name returns better information now. [GB.EVAL] * BUG: Highlight.Analyze correctly handle code lines having non ASCII characters inside. [GB.FORM] * BUG: The Balloon does not take the focus anymore. [GB.FORM.MDI] * NEW: Starting to enhance the Action class to provide shortcuts and toolbar configuration dialog. Does nothing at the moment! [GB.GTK] * BUG: Fix a leak in font objects management. * BUG: Picture.Load() yet loads an image, but internally converts it to a pixmap. It speeds up following draws based on this picture. * BUG: Startup forms hidden at design time are not shown automatically anymore. * NEW: The Action class is now shared with gb.qt by using a symbolic link. [GB.IMAGE.INFO] * NEW: New component to get information about an image file without having to fully load it. [GB.QT] * BUG: Disable automatic extra indent of Labels. * BUG: Startup forms hidden at design time are not shown automatically anymore. * BUG: Don't allow widgets to be destroyed while processing non-input events. git-svn-id: svn://localhost/gambas/trunk@1747 867c0c6c-44f3-4631-809d-bfa615b0a4ec
2008-12-28 19:01:39 +00:00
picture->getPixmap();
picture->setTag(new gGambasTag((void *)pic));
}
return pic;
}
void* GTK_GetPicture(GdkPixbuf *buf)
{
CPICTURE *pic = CPICTURE_create(new gPicture(buf));
g_object_ref(buf);
return (void*)pic;
}
/*******************************************************************************
Picture Cache
********************************************************************************/
static GHashTable *_cache = NULL;
static void destroy_key(char *key)
{
//fprintf(stderr, "destroy_key: '%s'\n", key);
g_free(key);
}
static void destroy_value(CPICTURE *pic)
{
//fprintf(stderr, "destroy_value: %p\n", pic);
GB.Unref((void **)POINTER(&pic));
}
static CPICTURE *cache_get(char *key)
{
if (!_cache)
return NULL;
return (CPICTURE *)g_hash_table_lookup(_cache, (gconstpointer)key);
}
static void cache_add(char *key, CPICTURE *pic)
{
if (!_cache)
_cache = g_hash_table_new_full(g_str_hash, g_str_equal, (GDestroyNotify)destroy_key, (GDestroyNotify)destroy_value);
if (!key || !*key)
return;
GB.Ref((void *)pic);
//fprintf(stderr, "cache_add: '%s' %p\n", key, pic);
g_hash_table_replace(_cache, (gpointer)g_strdup(key), (gpointer)pic);
}
static void cache_flush()
{
if (!_cache)
return;
g_hash_table_destroy(_cache);
_cache = NULL;
}
/*******************************************************************************
class Picture
*******************************************************************************/
#define LOAD_IMAGE_FUNC CPICTURE_load_image
#define IMAGE_TYPE gPicture
#define CREATE_IMAGE_FROM_MEMORY(_image, _addr, _len, _ok) \
_image = gPicture::fromMemory(_addr, _len);
#define DELETE_IMAGE(_image)
#define CREATE_PICTURE_FROM_IMAGE(_cpicture, _image) \
_cpicture = CPICTURE_create(_image);
#define GET_FROM_CACHE(_key) (cache_get(_key))
#define GET_FROM_STOCK(_name, _len) gPicture::fromNamedIcon(_name, _len)
#define INSERT_INTO_CACHE(_key, _cpicture) cache_add((_key), (_cpicture))
#define APPLICATION_THEME CAPPLICATION_Theme
#include "gb.form.picture.h"
BEGIN_METHOD(CPICTURE_get, GB_STRING path)
GB.ReturnObject(get_picture(STRING(path), LENGTH(path)));
END_METHOD
BEGIN_METHOD(CPICTURE_put, GB_OBJECT picture; GB_STRING path)
set_picture(STRING(path), LENGTH(path), (CPICTURE *)VARG(picture));
END_METHOD
BEGIN_METHOD_VOID(CPICTURE_flush)
cache_flush();
END_METHOD
BEGIN_METHOD(CPICTURE_new, GB_INTEGER w; GB_INTEGER h; GB_BOOLEAN trans)
int w = VARGOPT(w, 0);
int h = VARGOPT(h, 0);
bool trans = VARGOPT(trans, false);
PICTURE = new gPicture(gPicture::SERVER, w, h, trans);
PICTURE->setTag(new gGambasTag(THIS));
END_METHOD
BEGIN_METHOD_VOID(CPICTURE_free)
if (PICTURE) PICTURE->unref();
END_METHOD
BEGIN_METHOD(CPICTURE_resize, GB_INTEGER width; GB_INTEGER height)
PICTURE->resize(VARG(width),VARG(height));
END_METHOD
BEGIN_PROPERTY(CPICTURE_width)
GB.ReturnInteger(PICTURE->width());
END_PROPERTY
BEGIN_PROPERTY(CPICTURE_height)
GB.ReturnInteger(PICTURE->height());
END_PROPERTY
BEGIN_PROPERTY(CPICTURE_depth)
GB.ReturnInteger(PICTURE->depth());
END_PROPERTY
BEGIN_METHOD(CPICTURE_load, GB_STRING path)
CPICTURE *picture;
char *addr;
int len;
if (!GB.LoadFile(STRING(path), LENGTH(path), &addr, &len))
{
gPicture *pic = gPicture::fromMemory(addr, len);
GB.ReleaseFile(addr, len);
if (pic)
{
picture = CPICTURE_create(pic);
GB.ReturnObject(picture);
return;
}
}
GB.Error("Unable to load picture");
END_METHOD
/*
BEGIN_METHOD(CPICTURE_fromMemory,GB_STRING data;)
CPICTURE *pic=NULL;
if (!LENGTH(data)) return;
GB.New((void **)&pic, GB.FindClass("Picture"), 0, 0);
pic->picture->fromMemory(STRING(data),LENGTH(data));
GB.ReturnObject(pic);
END_METHOD
*/
BEGIN_METHOD(CPICTURE_save, GB_STRING path; GB_INTEGER quality)
[INFORMER] * BUG: The informer algorithm was redesigned. Now a sub-process is launched for each component that should be analyzed, and LD_PRELOAD is used to load the component shared library before the process is launched. Otherwise, some component may crash. [GB.DRAW] * BUG: Correctly initialize color properties of the Draw class at Draw.Begin(). * NEW: Draw.FillRect() is a new method to draw a filled rectangle with the specified color. * NEW: Draw.Clear() is a new method that clears the drawing device with its background color. [GB.FORM.MDI] * NEW: Do not use BackColor and ForeColor properties anymore. [GB.GTK] * NEW: Control.Backcolor and Control.Forecolor properties were removed. * BUG: Desktop.Resolution now uses the accurate GTK+ API. * BUG: Setting the ListBox.List to NULL property does not lock the ListBox control anymore. * BUG: Fix the Font object management. Using Font properties should not crash anymore. * BUG: Image.Save() and Picture.Save() now understand the "~" shortcut in path names. [GB.QT] * NEW: Control.Backcolor and Control.Forecolor properties were removed. * NEW: Do not check if we should quit too often. * NEW: Allow windows to be closed during a WAIT instruction as in other components. I do not know why it was forbidden before. * NEW: Prevent a crash in arrangement routines if a child widget is not associated with a Gambas control anymore. [GB.QT4] * NEW: Control.Backcolor and Control.Forecolor properties were removed. * NEW: The source code is now up to date with gb.qt. But many things do not work as expected! [GB.QT4.EXT] * NEW: The source code is now up to date with gb.qt.ext. But many things do not work as expected! git-svn-id: svn://localhost/gambas/trunk@1776 867c0c6c-44f3-4631-809d-bfa615b0a4ec
2009-01-03 22:24:02 +00:00
switch (PICTURE->save(GB.FileName(STRING(path), LENGTH(path)), VARGOPT(quality, -1)))
{
case 0: break;
case -1: GB.Error("Unknown format"); break;
case -2: GB.Error("Unable to save picture"); break;
}
END_METHOD
BEGIN_METHOD_VOID(CPICTURE_clear)
PICTURE->clear();
END_METHOD
BEGIN_METHOD(CPICTURE_fill, GB_INTEGER col)
PICTURE->fill(VARG(col));
END_METHOD
BEGIN_METHOD(CPICTURE_copy, GB_INTEGER x; GB_INTEGER y; GB_INTEGER w; GB_INTEGER h)
CPICTURE *pic=NULL;
int x=0;
int y=0;
int w=PICTURE->width();
int h=PICTURE->height();
if (!MISSING(x)) x=VARG(x);
if (!MISSING(y)) y=VARG(y);
if (!MISSING(w)) w=VARG(w);
if (!MISSING(h)) h=VARG(h);
pic = CPICTURE_create(PICTURE->copy(x, y, w, h));
GB.ReturnObject(pic);
END_METHOD
BEGIN_PROPERTY(CPICTURE_image)
CIMAGE *img = CIMAGE_create(PICTURE->copy());
CIMAGE_get(img)->getPixbuf();
GB.ReturnObject((void*)img);
END_PROPERTY
BEGIN_PROPERTY(CPICTURE_transparent)
if (READ_PROPERTY) { GB.ReturnBoolean(PICTURE->isTransparent()); return; }
PICTURE->setTransparent(VPROP(GB_BOOLEAN));
END_PROPERTY
GB_DESC CPictureDesc[] =
{
GB_DECLARE("Picture", sizeof(CPICTURE)),
//GB_STATIC_METHOD("_exit", NULL, CPICTURE_flush, NULL),
******** Merged /branches/64bits r918:1003 into /trunk [CONFIGURATION] * NEW: 64 bits port. [EXAMPLES] * BUG: Fixed the AnalogWatch example. [WIKI CGI SCRIPT] * NEW: Some little cosmetic changes. [INTERPRETER] * NEW: The extern function implementation has been redesigned and is now based on libffi, so that it works on 64 bits system. Because of a flaw in the compiler design, projects that use the Pointer datatype must be recompiled to be used on a 64 bits system. This flaw will be fixed in Gambas 3. * OPT: Put some tables into read-only memory. About 1000 bytes are saved for each running interpreter, except the first one. * BUG: Does not crash anymore if a component cannot be loaded. * NEW: Spanish translation updated. * NEW: A new interpreter API for returning a pointer. [COMPILER] * BUG: Correctly compiles LONG constants inside code. [GB.DEBUG] * BUG: Compiles and links the gb.debug components with the thread libraries. [GB.DB.SQLITE3] * BUG: Getting the primary index of a table without primary index is safe now. [GB.GTK] * BUG: Modified the GLib priority of watched descriptors, as the main loop could enter in a loop in which user interface events were not managed. * BUG: Message boxes use application title without crashing now. [GB.OPENGL] * BUG: Disable dead code. [GB.QT.EXT] * BUG: TextEdit.TextWidth and TextEdit.TextHeight were not declared as read-only properties. [GB.XML.XSLT] * BUG: XSLT class is now declared as being not creatable. git-svn-id: svn://localhost/gambas/trunk@1006 867c0c6c-44f3-4631-809d-bfa615b0a4ec
2008-01-17 21:39:26 +00:00
GB_METHOD("_new", 0, CPICTURE_new, "[(Width)i(Height)i(Transparent)b]"),
GB_METHOD("_free", 0, CPICTURE_free, 0),
GB_STATIC_METHOD("_exit",0,CPICTURE_flush,0),
GB_STATIC_METHOD("_get", "Picture", CPICTURE_get, "(Path)s"),
******** Merged /branches/64bits r918:1003 into /trunk [CONFIGURATION] * NEW: 64 bits port. [EXAMPLES] * BUG: Fixed the AnalogWatch example. [WIKI CGI SCRIPT] * NEW: Some little cosmetic changes. [INTERPRETER] * NEW: The extern function implementation has been redesigned and is now based on libffi, so that it works on 64 bits system. Because of a flaw in the compiler design, projects that use the Pointer datatype must be recompiled to be used on a 64 bits system. This flaw will be fixed in Gambas 3. * OPT: Put some tables into read-only memory. About 1000 bytes are saved for each running interpreter, except the first one. * BUG: Does not crash anymore if a component cannot be loaded. * NEW: Spanish translation updated. * NEW: A new interpreter API for returning a pointer. [COMPILER] * BUG: Correctly compiles LONG constants inside code. [GB.DEBUG] * BUG: Compiles and links the gb.debug components with the thread libraries. [GB.DB.SQLITE3] * BUG: Getting the primary index of a table without primary index is safe now. [GB.GTK] * BUG: Modified the GLib priority of watched descriptors, as the main loop could enter in a loop in which user interface events were not managed. * BUG: Message boxes use application title without crashing now. [GB.OPENGL] * BUG: Disable dead code. [GB.QT.EXT] * BUG: TextEdit.TextWidth and TextEdit.TextHeight were not declared as read-only properties. [GB.XML.XSLT] * BUG: XSLT class is now declared as being not creatable. git-svn-id: svn://localhost/gambas/trunk@1006 867c0c6c-44f3-4631-809d-bfa615b0a4ec
2008-01-17 21:39:26 +00:00
GB_STATIC_METHOD("_put", 0, CPICTURE_put, "(Picture)Picture;(Path)s"),
GB_STATIC_METHOD("Flush", 0, CPICTURE_flush, 0),
GB_PROPERTY_READ("Width", "i", CPICTURE_width),
GB_PROPERTY_READ("Height", "i", CPICTURE_height),
GB_PROPERTY_READ("W", "i", CPICTURE_width),
GB_PROPERTY_READ("H", "i", CPICTURE_height),
GB_PROPERTY_READ("Depth", "i", CPICTURE_depth),
GB_PROPERTY("Transparent", "b", CPICTURE_transparent),
GB_STATIC_METHOD("Load", "Picture", CPICTURE_load, "(Path)s"),
******** Merged /branches/64bits r918:1003 into /trunk [CONFIGURATION] * NEW: 64 bits port. [EXAMPLES] * BUG: Fixed the AnalogWatch example. [WIKI CGI SCRIPT] * NEW: Some little cosmetic changes. [INTERPRETER] * NEW: The extern function implementation has been redesigned and is now based on libffi, so that it works on 64 bits system. Because of a flaw in the compiler design, projects that use the Pointer datatype must be recompiled to be used on a 64 bits system. This flaw will be fixed in Gambas 3. * OPT: Put some tables into read-only memory. About 1000 bytes are saved for each running interpreter, except the first one. * BUG: Does not crash anymore if a component cannot be loaded. * NEW: Spanish translation updated. * NEW: A new interpreter API for returning a pointer. [COMPILER] * BUG: Correctly compiles LONG constants inside code. [GB.DEBUG] * BUG: Compiles and links the gb.debug components with the thread libraries. [GB.DB.SQLITE3] * BUG: Getting the primary index of a table without primary index is safe now. [GB.GTK] * BUG: Modified the GLib priority of watched descriptors, as the main loop could enter in a loop in which user interface events were not managed. * BUG: Message boxes use application title without crashing now. [GB.OPENGL] * BUG: Disable dead code. [GB.QT.EXT] * BUG: TextEdit.TextWidth and TextEdit.TextHeight were not declared as read-only properties. [GB.XML.XSLT] * BUG: XSLT class is now declared as being not creatable. git-svn-id: svn://localhost/gambas/trunk@1006 867c0c6c-44f3-4631-809d-bfa615b0a4ec
2008-01-17 21:39:26 +00:00
GB_METHOD("Save", 0, CPICTURE_save, "(Path)s[(Quality)i]"),
GB_METHOD("Resize", 0, CPICTURE_resize, "(Width)i(Height)i"),
******** Merged /branches/64bits r918:1003 into /trunk [CONFIGURATION] * NEW: 64 bits port. [EXAMPLES] * BUG: Fixed the AnalogWatch example. [WIKI CGI SCRIPT] * NEW: Some little cosmetic changes. [INTERPRETER] * NEW: The extern function implementation has been redesigned and is now based on libffi, so that it works on 64 bits system. Because of a flaw in the compiler design, projects that use the Pointer datatype must be recompiled to be used on a 64 bits system. This flaw will be fixed in Gambas 3. * OPT: Put some tables into read-only memory. About 1000 bytes are saved for each running interpreter, except the first one. * BUG: Does not crash anymore if a component cannot be loaded. * NEW: Spanish translation updated. * NEW: A new interpreter API for returning a pointer. [COMPILER] * BUG: Correctly compiles LONG constants inside code. [GB.DEBUG] * BUG: Compiles and links the gb.debug components with the thread libraries. [GB.DB.SQLITE3] * BUG: Getting the primary index of a table without primary index is safe now. [GB.GTK] * BUG: Modified the GLib priority of watched descriptors, as the main loop could enter in a loop in which user interface events were not managed. * BUG: Message boxes use application title without crashing now. [GB.OPENGL] * BUG: Disable dead code. [GB.QT.EXT] * BUG: TextEdit.TextWidth and TextEdit.TextHeight were not declared as read-only properties. [GB.XML.XSLT] * BUG: XSLT class is now declared as being not creatable. git-svn-id: svn://localhost/gambas/trunk@1006 867c0c6c-44f3-4631-809d-bfa615b0a4ec
2008-01-17 21:39:26 +00:00
GB_METHOD("Clear", 0, CPICTURE_clear, 0),
GB_METHOD("Fill", 0, CPICTURE_fill, "(Color)i"),
GB_METHOD("Copy", "Picture", CPICTURE_copy, "[(X)i(Y)i(Width)i(Height)i]"),
GB_PROPERTY_READ("Image", "Image", CPICTURE_image),
GB_INTERFACE("Draw", &DRAW_Interface),
GB_INTERFACE("Paint", &PAINT_Interface),
GB_END_DECLARE
};