gambas-source-code/gb.gtk/src/gdrawingarea.cpp

264 lines
5.8 KiB
C++
Raw Normal View History

/***************************************************************************
gdraw.cpp
(c) 2004-2006 - Daniel Campos Fernández <dcamposf@gmail.com>
Gtkmae "GTK+ made easy" classes
Realizado para la Junta de Extremadura.
Consejería de Educación Ciencia y Tecnología.
Proyecto gnuLinEx
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 1, 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.
***************************************************************************/
#include "widgets.h"
#include "gdrawingarea.h"
/****************************************************************************************
gDrawingArea Widget
*****************************************************************************************/
static gboolean cb_expose(GtkWidget *wid, GdkEventExpose *e, gDrawingArea *data)
{
if (data->cached())
{
[DEVELOPMENT ENVIRONMENT] * NEW: English and french tips were updated. A new tip was added. * NEW: Files that were opened at project close are automatically reopened   when the project is loaded again. * NEW: A warning message is displayed when the GNU translation tools are not installed. * BUG: The code editor method combo-box is correctly updated now. * BUG: Some fixes in the automatic completion. * BUG: Replace points by dash in the name of packages generated by the IDE packager. * NEW: Updated russian translation * NEW: Updated french translation [DATABASE MANAGER] * NEW: Updated russian translation [EXAMPLES] * BUG: Fixed the Gravity and the GameOfLife examples so that they do not   use public form controls anymore. [INTERPRETER] * OPT: Many optimizations in the string substitution routines, the internal datatype conversions, the INPUT and LINE INPUT instructions, the error messages generation, the object and string reference counting, and the memory allocation routines. * NEW: Opening a device file in direct mode (FOR READ/WRITE) is now automatically non blocking. * OPT: Lof() now only tries its different methods (ioctl and lseek) once. * BUG: Val() now ignores thousand separators characters at the end of the number. * NEW: A new flag for enabling the stack trace generation at each error. [GB.DEBUG] * BUG: The gb.debug component interface declaration was not 64-bits aware. [GB.EVAL] * BUG: The Highlight.Purge() method now correctly deals with non-ASCII characters. [GB.FORM] * BUG: TableView.Edit() does not raise a "read-only combo-box" error anymore. [GB.FORM.DIALOG] * BUG: Dialog buttons now are never cut. [GB.GTK] * BUG: Cached drawing areas are correctly redrawn now. * BUG: Loading big images now works correctly. There is apparently a bug in the GTK+ image loader, and I found a workaround. * BUG: Message boxes correctly display the text of their buttons now. [GB.QT] * BUG: The Open, and initial Move and Resize event of embedded forms are now always raised when you call the Show method or if you set the Visible property. Before, it was raised when the embedded form was actually shown. [GB.SETTINGS] * NEW: Settings are now stored in ~/.config/gambasX, where X is the gambas version number. * BUG: Strings are correctly quoted inside the settings file now. [GB.WEB] * NEW: Application.Protocol is a new property that allows to tell the   component that the protocol is not necessarily "http". git-svn-id: svn://localhost/gambas/trunk@1153 867c0c6c-44f3-4631-809d-bfa615b0a4ec
2008-03-11 13:29:47 +00:00
gdk_window_clear(GTK_LAYOUT(wid)->bin_window);
}
else
{
if (data->onExpose)
data->onExpose(data,e->area.x,e->area.y,e->area.width,e->area.height);
data->drawBorder();
}
return false;
}
static void cb_size(GtkWidget *wid, GtkAllocation *a, gDrawingArea *data)
{
data->updateCache();
}
[DEVELOPMENT ENVIRONMENT] * BUG: Use TextEdit.RichText insted of TextEdit.Text. * BUG: END SUB can be the end of a method. The class analyze now takes   that into account. [HELP] * BUG: Fixed the generated treeview. [COMPILER] * OPT: The NOT operator used just at the beginning of a conditional expression is optimized. Consequently, an expression like 'IF NOT 2' is now equivalent to 'IF 2 = 0' and not to 'IF (NOT 2) <> 0' as before. In other words, the boolean conversion is now done before the NOT, and not after. The following instructions are concerned: IF, WHILE, UNTIL. * NEW: BYREF is new keyword that is a more readable synonymous of '@'. [GB.DB.FORM] * BUG: Correctly manage data controls inside TabStrip-like containers. * BUG: Setting the focus on a non-initialized DataControl does not raise an error anymore. [GB.GTK] * BUG: HSplitter.Layout and VSplitter.Layout now work correctly. It is a list of children widths, hidden children having a zero width. * BUG: Window arrangement is done before the Open event is raised, as in gb.qt. * BUG: Keyboard, focus and mouse events now work correctly on Window and DrawingArea controls. [GB.QT] * BUG: HSplitter.Layout and VSplitter.Layout now work correctly. It is a list of children widths, hidden children having a zero width. * BUG: Many warning fixes. * BUG: Now the Control.Visible property works like in gb.gtk, i.e. it returns if the control was not explicitely hidden. git-svn-id: svn://localhost/gambas/trunk@1060 867c0c6c-44f3-4631-809d-bfa615b0a4ec
2008-02-06 00:25:48 +00:00
static gboolean cb_button_press(GtkWidget *wid, GdkEventButton *event, gDrawingArea *data)
{
if (data->canFocus())
data->setFocus();
return false;
}
gDrawingArea::gDrawingArea(gContainer *parent) : gContainer(parent)
{
g_typ = Type_gDrawingArea;
_cached = false;
buffer = NULL;
_old_bg_id = 0;
_resize_cache = false;
border = gtk_event_box_new();
widget = gtk_layout_new(0,0);
realize(false);
//gtk_event_box_set_visible_window(GTK_EVENT_BOX(border), false);
gtk_widget_add_events(widget, GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK
| GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK
| GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK | GDK_POINTER_MOTION_MASK);
[DEVELOPMENT ENVIRONMENT] * BUG: Use TextEdit.RichText insted of TextEdit.Text. * BUG: END SUB can be the end of a method. The class analyze now takes   that into account. [HELP] * BUG: Fixed the generated treeview. [COMPILER] * OPT: The NOT operator used just at the beginning of a conditional expression is optimized. Consequently, an expression like 'IF NOT 2' is now equivalent to 'IF 2 = 0' and not to 'IF (NOT 2) <> 0' as before. In other words, the boolean conversion is now done before the NOT, and not after. The following instructions are concerned: IF, WHILE, UNTIL. * NEW: BYREF is new keyword that is a more readable synonymous of '@'. [GB.DB.FORM] * BUG: Correctly manage data controls inside TabStrip-like containers. * BUG: Setting the focus on a non-initialized DataControl does not raise an error anymore. [GB.GTK] * BUG: HSplitter.Layout and VSplitter.Layout now work correctly. It is a list of children widths, hidden children having a zero width. * BUG: Window arrangement is done before the Open event is raised, as in gb.qt. * BUG: Keyboard, focus and mouse events now work correctly on Window and DrawingArea controls. [GB.QT] * BUG: HSplitter.Layout and VSplitter.Layout now work correctly. It is a list of children widths, hidden children having a zero width. * BUG: Many warning fixes. * BUG: Now the Control.Visible property works like in gb.gtk, i.e. it returns if the control was not explicitely hidden. git-svn-id: svn://localhost/gambas/trunk@1060 867c0c6c-44f3-4631-809d-bfa615b0a4ec
2008-02-06 00:25:48 +00:00
GTK_WIDGET_SET_FLAGS (widget, GTK_CAN_FOCUS);
_event_mask = gtk_widget_get_events(widget);
onExpose = NULL;
g_signal_connect(G_OBJECT(widget), "expose-event", G_CALLBACK(cb_expose), (gpointer)this);
g_signal_connect(G_OBJECT(widget), "size-allocate", G_CALLBACK(cb_size), (gpointer)this);
[DEVELOPMENT ENVIRONMENT] * BUG: Use TextEdit.RichText insted of TextEdit.Text. * BUG: END SUB can be the end of a method. The class analyze now takes   that into account. [HELP] * BUG: Fixed the generated treeview. [COMPILER] * OPT: The NOT operator used just at the beginning of a conditional expression is optimized. Consequently, an expression like 'IF NOT 2' is now equivalent to 'IF 2 = 0' and not to 'IF (NOT 2) <> 0' as before. In other words, the boolean conversion is now done before the NOT, and not after. The following instructions are concerned: IF, WHILE, UNTIL. * NEW: BYREF is new keyword that is a more readable synonymous of '@'. [GB.DB.FORM] * BUG: Correctly manage data controls inside TabStrip-like containers. * BUG: Setting the focus on a non-initialized DataControl does not raise an error anymore. [GB.GTK] * BUG: HSplitter.Layout and VSplitter.Layout now work correctly. It is a list of children widths, hidden children having a zero width. * BUG: Window arrangement is done before the Open event is raised, as in gb.qt. * BUG: Keyboard, focus and mouse events now work correctly on Window and DrawingArea controls. [GB.QT] * BUG: HSplitter.Layout and VSplitter.Layout now work correctly. It is a list of children widths, hidden children having a zero width. * BUG: Many warning fixes. * BUG: Now the Control.Visible property works like in gb.gtk, i.e. it returns if the control was not explicitely hidden. git-svn-id: svn://localhost/gambas/trunk@1060 867c0c6c-44f3-4631-809d-bfa615b0a4ec
2008-02-06 00:25:48 +00:00
g_signal_connect(G_OBJECT(border), "button-press-event",G_CALLBACK(cb_button_press),(gpointer)this);
//resize(100,30);
}
gDrawingArea::~gDrawingArea()
{
setCached(false);
}
void gDrawingArea::resize(int w, int h)
{
// TODO Do not resize cache if the DrawingArea is being painted
gContainer::resize(w,h);
//updateCache();
}
bool gDrawingArea::canFocus()
{
return GTK_WIDGET_CAN_FOCUS(widget);
}
void gDrawingArea::setCanFocus(bool vl)
{
if (vl) GTK_WIDGET_SET_FLAGS(widget,GTK_CAN_FOCUS);
else GTK_WIDGET_UNSET_FLAGS(widget,GTK_CAN_FOCUS);
}
void gDrawingArea::updateEventMask()
{
if (!enabled())
{
gtk_widget_set_events(widget, GDK_EXPOSURE_MASK);
return;
}
gtk_widget_set_events(widget, _event_mask);
}
void gDrawingArea::setEnabled(bool vl)
{
gContainer::setEnabled(vl);
updateEventMask();
}
void gDrawingArea::setCached(bool vl)
{
if (vl == cached()) return;
_cached = vl;
gtk_widget_set_double_buffered(widget, !_cached);
if (!_cached)
{
g_object_unref(G_OBJECT(buffer));
buffer = NULL;
set_gdk_bg_color(widget, background());
return;
}
gtk_widget_realize(widget);
resizeCache();
}
void gDrawingArea::resizeCache()
{
int bw, bh;
int w, h;
GdkPixmap *buf;
GdkGC *gc2;
GdkWindow *win;
win = GTK_LAYOUT(widget)->bin_window;
if (!win)
return;
//gdk_drawable_get_size(border->window, &w, &h);
w = width();
h = height();
if (buffer)
gdk_drawable_get_size(buffer, &bw, &bh);
else
bw = bh = 0;
if (bw != w || bh != h)
{
buf = gdk_pixmap_new(win, w, h, -1);
gc2 = gdk_gc_new(buf);
gdk_gc_set_foreground(gc2, &widget->style->bg[GTK_STATE_NORMAL]);
if (w > bw || h > bh || !buffer)
gdk_draw_rectangle(buf, gc2, true, 0, 0, w, h);
if (buffer)
{
if (bw > w) bw = w;
if (bh > h) bh = h;
gdk_draw_drawable(buf, gc2, buffer, 0, 0, 0, 0, bw, bh);
g_object_unref(buffer);
}
buffer = buf;
g_object_unref(gc2);
}
}
void gDrawingArea::setCache()
{
GdkWindow *win;
win = GTK_LAYOUT(widget)->bin_window;
if (!win)
return;
drawBorder(buffer);
gdk_window_set_back_pixmap(win, buffer, FALSE);
//gdk_window_set_back_pixmap(border->window, buffer, FALSE);
//gdk_window_set_back_pixmap(widget->window, NULL, TRUE);
//gdk_window_set_back_pixmap(win, NULL, TRUE);
refreshCache();
}
static gboolean resize_cache(gDrawingArea *data)
{
//fprintf(stderr, "resize_cache\n");
data->resizeCache();
data->setCache();
data->_resize_cache = false;
return false;
}
void gDrawingArea::updateCache()
{
if (!_cached)
return;
if (!_resize_cache)
{
_resize_cache = true;
g_timeout_add(10, (GSourceFunc)resize_cache, (gpointer)this);
}
}
void gDrawingArea::clear()
{
GdkGC *gc2;
if (_cached && buffer)
{
gc2=gdk_gc_new(buffer);
gdk_gc_set_foreground(gc2,&widget->style->bg[GTK_STATE_NORMAL]);
gdk_draw_rectangle(buffer,gc2,true,0,0,width(),height());
g_object_unref(G_OBJECT(gc2));
//updateCache();
return;
}
refresh(); //gdk_window_clear(GTK_LAYOUT(widget)->bin_window);
}
void gDrawingArea::refreshCache()
{
if (_cached)
{
if (GTK_LAYOUT(widget)->bin_window)
gdk_window_clear(GTK_LAYOUT(widget)->bin_window);
}
}