📄 dw_widget.c
字号:
/* * File: dw_widget.c * * Copyright (C) 2001-2003 Sebastian Geerken <S.Geerken@ping.de> * * 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. */#include <gtk/gtk.h>#include <stdio.h>#include <string.h>#include "msg.h"#include "dw_widget.h"#include "dw_marshal.h"#include "dw_container.h"#include "dw_gtk_viewport.h"#define DEBUG_EVENT 10#define DEBUG_SIZE 0#define DEBUG_ALLOC 0/*#define DEBUG_LEVEL 10*/#include "debug.h"static void Dw_widget_init (DwWidget *widget);static void Dw_widget_class_init (DwWidgetClass *klass);static void Dw_widget_destroy (GtkObject *object);static void Dw_widget_real_size_request (DwWidget *widget, DwRequisition *requisition);static void Dw_widget_real_get_extremes (DwWidget *widget, DwExtremes *extremes);enum{ BUTTON_PRESS_EVENT, BUTTON_RELEASE_EVENT, MOTION_NOTIFY_EVENT, ENTER_NOTIFY_EVENT, LEAVE_NOTIFY_EVENT, LAST_SIGNAL};static GtkObjectClass *parent_class;static guint widget_signals[LAST_SIGNAL] = { 0 };/* * Standard Gtk+ function */GtkType a_Dw_widget_get_type (void){ static GtkType type = 0; if (!type) { GtkTypeInfo info = { "DwWidget", sizeof (DwWidget), sizeof (DwWidgetClass), (GtkClassInitFunc) Dw_widget_class_init, (GtkObjectInitFunc) Dw_widget_init, (GtkArgSetFunc) NULL, (GtkArgGetFunc) NULL, (GtkClassInitFunc) NULL }; type = gtk_type_unique (GTK_TYPE_OBJECT, &info); } return type;}/* * Standard Gtk+ function */static void Dw_widget_init (DwWidget *widget){ widget->flags = DW_NEEDS_RESIZE | DW_EXTREMES_CHANGED | DW_HAS_CONTENT; widget->parent = NULL; widget->viewport = NULL; widget->allocation.x = -1; widget->allocation.y = -1; widget->allocation.width = 1; widget->allocation.ascent = 1; widget->allocation.descent = 0; widget->cursor = NULL; widget->style = NULL; widget->bg_color = NULL; widget->button_sensitive = TRUE; widget->button_sensitive_set = FALSE;}/* * Standard Gtk+ function */static void Dw_widget_class_init (DwWidgetClass *klass){ GtkObjectClass *object_class; parent_class = gtk_type_class (gtk_object_get_type ()); object_class = GTK_OBJECT_CLASS (klass); widget_signals[BUTTON_PRESS_EVENT] = gtk_signal_new ("button_press_event", GTK_RUN_LAST, object_class->type, GTK_SIGNAL_OFFSET (DwWidgetClass, button_press_event), p_Dw_marshal_BOOL__INT_INT_POINTER, GTK_TYPE_BOOL, 3, GTK_TYPE_INT, GTK_TYPE_INT, GTK_TYPE_GDK_EVENT); widget_signals[BUTTON_RELEASE_EVENT] = gtk_signal_new ("button_release_event", GTK_RUN_LAST, object_class->type, GTK_SIGNAL_OFFSET (DwWidgetClass, button_release_event), p_Dw_marshal_BOOL__INT_INT_POINTER, GTK_TYPE_BOOL, 3, GTK_TYPE_INT, GTK_TYPE_INT, GTK_TYPE_GDK_EVENT); widget_signals[MOTION_NOTIFY_EVENT] = gtk_signal_new ("motion_notify_event", GTK_RUN_LAST, object_class->type, GTK_SIGNAL_OFFSET (DwWidgetClass, motion_notify_event), p_Dw_marshal_BOOL__INT_INT_POINTER, GTK_TYPE_BOOL, 3, GTK_TYPE_INT, GTK_TYPE_INT, GTK_TYPE_GDK_EVENT); widget_signals[ENTER_NOTIFY_EVENT] = gtk_signal_new ("enter_notify_event", GTK_RUN_LAST, object_class->type, GTK_SIGNAL_OFFSET (DwWidgetClass, enter_notify_event), p_Dw_marshal_BOOL__POINTER_POINTER, GTK_TYPE_BOOL, 2, GTK_TYPE_POINTER, GTK_TYPE_GDK_EVENT); widget_signals[LEAVE_NOTIFY_EVENT] = gtk_signal_new ("leave_notify_event", GTK_RUN_LAST, object_class->type, GTK_SIGNAL_OFFSET (DwWidgetClass, leave_notify_event), p_Dw_marshal_BOOL__POINTER_POINTER, GTK_TYPE_BOOL, 2, GTK_TYPE_POINTER, GTK_TYPE_GDK_EVENT); gtk_object_class_add_signals (object_class, widget_signals, LAST_SIGNAL); object_class->destroy = Dw_widget_destroy; klass->size_request = Dw_widget_real_size_request; klass->get_extremes = Dw_widget_real_get_extremes; klass->size_allocate = NULL; klass->mark_size_change = NULL; klass->mark_extremes_change = NULL; klass->set_width = NULL; klass->set_ascent = NULL; klass->set_descent = NULL; klass->draw = NULL; klass->button_press_event = NULL; klass->button_release_event = NULL; klass->motion_notify_event = NULL; klass->enter_notify_event = NULL; klass->leave_notify_event = NULL; klass->iterator = NULL;}/* * Standard Gtk+ function */static void Dw_widget_destroy (GtkObject *object){ DwWidget *widget; widget = DW_WIDGET (object); /* The widget the pointer is in? */ if (widget->viewport != NULL && widget == GTK_DW_VIEWPORT(widget->viewport)->last_entered) /* todo: perhaps call the leave_notify function? */ GTK_DW_VIEWPORT(widget->viewport)->last_entered = NULL; if (widget->style) a_Dw_style_unref (widget->style); if (widget->parent) Dw_container_remove (DW_CONTAINER (widget->parent), widget); else Dw_gtk_viewport_remove_dw (GTK_DW_VIEWPORT (widget->viewport)); parent_class->destroy (object);}/* * Standard Dw function */static void Dw_widget_real_size_request (DwWidget *widget, DwRequisition *requisition){ g_warning ("DwWidget::size_request not implemented for `%s'", gtk_type_name (GTK_OBJECT_TYPE (widget))); /* return random size to prevent crashes*/ requisition->width = 50; requisition->ascent = 50; requisition->descent = 50;}/* * Standard Dw function */static void Dw_widget_real_get_extremes (DwWidget *widget, DwExtremes *extremes){ /* Simply return the requisition width */ DwRequisition requisition; p_Dw_widget_size_request (widget, &requisition); extremes->min_width = extremes->max_width = requisition.width;}/* * This function is a wrapper for DwWidget::size_request; it calls * this method only when needed. */void p_Dw_widget_size_request (DwWidget *widget, DwRequisition *requisition){ DwWidgetClass *klass; if (DW_WIDGET_NEEDS_RESIZE (widget)) { /* todo: check requisition == &(widget->requisition) and do what? */ klass = DW_WIDGET_CLASS (GTK_OBJECT(widget)->klass); (* (klass->size_request)) (widget, requisition); widget->requisition = *requisition; DW_WIDGET_UNSET_FLAGS (widget, DW_NEEDS_RESIZE); DBG_OBJ_SET_NUM (widget, "requisition.width", widget->requisition.width); DBG_OBJ_SET_NUM (widget, "requisition.ascent", widget->requisition.ascent); DBG_OBJ_SET_NUM (widget, "requisition.descent", widget->requisition.descent); } else *requisition = widget->requisition;}/* * Wrapper for DwWidget::get_extremes. */void p_Dw_widget_get_extremes (DwWidget *widget, DwExtremes *extremes){ DwWidgetClass *klass; if (DW_WIDGET_EXTREMES_CHANGED (widget)) { klass = DW_WIDGET_CLASS (GTK_OBJECT(widget)->klass); (* (klass->get_extremes)) (widget, extremes); widget->extremes = *extremes; DW_WIDGET_UNSET_FLAGS (widget, DW_EXTREMES_CHANGED); DBG_OBJ_SET_NUM (widget, "extremes.min_width", widget->extremes.min_width); DBG_OBJ_SET_NUM (widget, "extremes.max_width", widget->extremes.max_width); } else *extremes = widget->extremes;}/* * Wrapper for DwWidget::size_allocate, only called when needed. */void p_Dw_widget_size_allocate (DwWidget *widget, DwAllocation *allocation){ DwWidgetClass *klass; if (DW_WIDGET_NEEDS_ALLOCATE (widget) || allocation->x != widget->allocation.x || allocation->y != widget->allocation.y || allocation->width != widget->allocation.width || allocation->ascent != widget->allocation.ascent || allocation->descent != widget->allocation.descent) { DEBUG_MSG (DEBUG_ALLOC, "a %stop-level %s with parent_ref = %d is newly allocated " "from %d, %d, %d x %d x %d ...\n", widget->parent ? "non-" : "", gtk_type_name (GTK_OBJECT_TYPE (widget)), widget->parent_ref, widget->allocation.x, widget->allocation.y, widget->allocation.width, widget->allocation.ascent, widget->allocation.descent); klass = DW_WIDGET_CLASS (GTK_OBJECT(widget)->klass); if (klass->size_allocate) (* (klass->size_allocate)) (widget, allocation); DEBUG_MSG (DEBUG_ALLOC, "... to %d, %d, %d x %d x %d\n", widget->allocation.x, widget->allocation.y, widget->allocation.width, widget->allocation.ascent, widget->allocation.descent); widget->allocation = *allocation; DW_WIDGET_UNSET_FLAGS (widget, DW_NEEDS_ALLOCATE); DBG_OBJ_SET_NUM (widget, "allocation.x", widget->allocation.x); DBG_OBJ_SET_NUM (widget, "allocation.y", widget->allocation.y); DBG_OBJ_SET_NUM (widget, "allocation.width", widget->allocation.width); DBG_OBJ_SET_NUM (widget, "allocation.ascent", widget->allocation.ascent); DBG_OBJ_SET_NUM (widget, "allocation.descent", widget->allocation.descent); } /*DW_WIDGET_UNSET_FLAGS (widget, DW_NEEDS_RESIZE);*/}void p_Dw_widget_set_width (DwWidget *widget, gint32 width){ DwWidgetClass *klass; klass = DW_WIDGET_CLASS (GTK_OBJECT(widget)->klass); if (klass->set_width) (* (klass->set_width)) (widget, width);}void p_Dw_widget_set_ascent (DwWidget *widget, gint32 ascent){ DwWidgetClass *klass; klass = DW_WIDGET_CLASS (GTK_OBJECT(widget)->klass); if (klass->set_ascent) (* (klass->set_ascent)) (widget, ascent);}void p_Dw_widget_set_descent (DwWidget *widget, gint32 descent){ DwWidgetClass *klass; klass = DW_WIDGET_CLASS (GTK_OBJECT(widget)->klass); if (klass->set_descent) (* (klass->set_descent)) (widget, descent);}void p_Dw_widget_draw (DwWidget *widget, DwRectangle *area, GdkEventExpose *event){ /* NOTE: This function depends on that widgets are always drawn top-down. * The initial draw call is done for the top-level widget by the viewport, * all other draw calls are done for children. */ GtkDwViewport *viewport; GdkDrawable *orig_pixmap, *dest; gint x, y; DwWidgetClass *klass; orig_pixmap = widget->parent ? widget->parent->clip_pixmap : NULL; widget->clip_pixmap = orig_pixmap; klass = DW_WIDGET_CLASS (GTK_OBJECT(widget)->klass); if (klass->draw) (* (klass->draw)) (widget, area, event); if (widget->clip_pixmap && (widget->parent == NULL || widget->clip_pixmap != widget->parent->clip_pixmap)) { /* Copy clipping pixmap into backing pixmap, when this widget has called * p_Dw_widget_will_clip(). */ viewport = GTK_DW_VIEWPORT (widget->viewport); dest = orig_pixmap ? orig_pixmap : viewport->back_pixmap; x = area->x + widget->allocation.x - gtk_layout_get_hadjustment(GTK_LAYOUT(viewport))->value; y = area->y + widget->allocation.y - gtk_layout_get_vadjustment(GTK_LAYOUT(viewport))->value; gdk_draw_pixmap (dest, widget->viewport->style->black_gc, widget->clip_pixmap, x, y, x, y, area->width, area->height); gdk_pixmap_unref (widget->clip_pixmap); }}/* * Handles a mouse event. * * This function is called by Dw_gtk_viewport_mouse_event, the type of * the event is determined by event->type. x and y are world coordinates. * widget may be NULL (if the pointer is outside the top-level widget). * * When event is NULL, GDK_MOTION_NOTIFY is used as the type. This will * soon be the case when GDK_MOTION_NOTIFY events are simulated as a * result of viewport changes (bug #94) */gint Dw_widget_mouse_event (DwWidget *widget, GtkWidget *viewwidget, gint32 x, gint32 y, GdkEvent *event){ gint signal_no;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -