001: /*
002:  *  dde-graph2d.c
003:  *  dde
004:  *
005:  *  Created by ashley on 05/12/2009.
006:  *  Copyright 2009 __MyCompanyName__. All rights reserved.
007:  *
008:  */
009: 
010: #include <pango/pangocairo.h>
011: #include "dde-graph2d.h"
012: 
013: #define GRAH2D_DEFAULT_WIDTH 600
014: #define GRAH2D_DEFAULT_HEIGHT 480
015: 
016: static void dde_graph2d_class_init(GtkGraph2dClass *klass);
017: static void dde_graph2d_init(GtkGraph2d *dde_graph2d);
018: static void dde_graph2d_size_request(GtkWidget *widget, GtkRequisition *requisition);
019: static void dde_graph2d_size_allocate(GtkWidget *widget, GtkAllocation *allocation);
020: static void dde_graph2d_realize(GtkWidget *widget);
021: static gboolean dde_graph2d_expose(GtkWidget *widget, GdkEventExpose *event);
022: static void dde_graph2d_paint(GtkWidget *widget, int width, int height);
023: static void dde_graph2d_destroy(GtkObject *object);
024: 
025: 
026: GtkType dde_graph2d_get_type(void)
027: {
028:         static GtkType dde_graph2d_type = 0;
029:         
030:         
031:         if (!dde_graph2d_type) {
032:                 static const GtkTypeInfo dde_graph2d_info = {
033:                         "GtkGraph2d",
034:                         sizeof(GtkGraph2d),
035:                         sizeof(GtkGraph2dClass),
036:                         (GtkClassInitFunc) dde_graph2d_class_init,
037:                         (GtkObjectInitFunc) dde_graph2d_init,
038:                         NULL,
039:                         NULL,
040:                         (GtkClassInitFunc) NULL
041:                 };
042:                 dde_graph2d_type = gtk_type_unique(GTK_TYPE_WIDGET, &dde_graph2d_info);
043:         }
044:         
045:         
046:         return dde_graph2d_type;
047: }
048: 
049: void dde_graph2d_set_state(GtkGraph2d *dde_graph2d, gint num)
050: {
051:         dde_graph2d->sel = num;
052: //      dde_graph2d_paint(GTK_WIDGET(dde_graph2d));
053: }
054: 
055: 
056: GtkWidget *dde_graph2d_new()
057: {
058:         return GTK_WIDGET(gtk_type_new(dde_graph2d_get_type()));
059: }
060: 
061: 
062: static void dde_graph2d_class_init(GtkGraph2dClass *klass)
063: {
064:         GtkWidgetClass *widget_class;
065:         GtkObjectClass *object_class;
066:         
067:         
068:         widget_class = (GtkWidgetClass *)klass;
069:         object_class = (GtkObjectClass *)klass;
070:         
071:         widget_class->realize = dde_graph2d_realize;
072:         widget_class->size_request = dde_graph2d_size_request;
073:         widget_class->size_allocate = dde_graph2d_size_allocate;
074:         widget_class->expose_event = dde_graph2d_expose;
075:         
076:         object_class->destroy = dde_graph2d_destroy;
077: }
078: 
079: 
080: static void dde_graph2d_init(GtkGraph2d *dde_graph2d)
081: {
082:         dde_graph2d->sel = 0;
083: }
084: 
085: 
086: static void dde_graph2d_size_request(GtkWidget *widget, GtkRequisition *requisition)
087: {
088:         g_return_if_fail(widget != NULL);
089:         g_return_if_fail(DDE_IS_GRAPH2D(widget));
090:         g_return_if_fail(requisition != NULL);
091:         
092:         requisition->width = GRAH2D_DEFAULT_WIDTH;
093:         requisition->height = GRAH2D_DEFAULT_HEIGHT;
094: }
095: 
096: 
097: static void dde_graph2d_size_allocate(GtkWidget *widget, GtkAllocation *allocation)
098: {
099: //      g_print("dde_graph2d_size_allocate\n");
100: 
101:         g_return_if_fail(widget != NULL);
102:         g_return_if_fail(DDE_IS_GRAPH2D(widget));
103:         g_return_if_fail(allocation != NULL);
104:         
105:         widget->allocation = *allocation;
106:         
107:         if (GTK_WIDGET_REALIZED(widget)) {
108:                 gdk_window_move_resize(widget->window, allocation->x, allocation->y, allocation->width, allocation->height);
109:         }
110: }
111: 
112: 
113: static void dde_graph2d_realize(GtkWidget *widget)
114: {
115:         GdkWindowAttr attributes;
116:         guint attributes_mask;
117:         
118:         g_return_if_fail(widget != NULL);
119:         g_return_if_fail(DDE_IS_GRAPH2D(widget));
120:         
121:         GTK_WIDGET_SET_FLAGS(widget, GTK_REALIZED);
122:         
123:         attributes.window_type = GDK_WINDOW_CHILD;
124:         attributes.x = widget->allocation.x;
125:         attributes.y = widget->allocation.y;
126:         attributes.width = GRAH2D_DEFAULT_WIDTH;
127:         attributes.height = GRAH2D_DEFAULT_HEIGHT;
128:         
129:         attributes.wclass = GDK_INPUT_OUTPUT;
130:         attributes.event_mask = gtk_widget_get_events(widget) | GDK_EXPOSURE_MASK;
131:         
132:         attributes_mask = GDK_WA_X | GDK_WA_Y;
133:         
134:         widget->window = gdk_window_new(gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
135:         
136:         gdk_window_set_user_data(widget->window, widget);
137:         
138:         widget->style = gtk_style_attach(widget->style, widget->window);
139:         gtk_style_set_background(widget->style, widget->window, GTK_STATE_NORMAL);
140: }
141: 
142: 
143: static gboolean dde_graph2d_expose(GtkWidget *widget, GdkEventExpose *event)
144: {
145:     GdkWindow *window = gtk_widget_get_parent_window(widget);
146:         int width, height;
147:         gdk_drawable_get_size(GDK_WINDOW(window), &width, &height);
148:         g_print("expose_event: %d %d\n", width, height); 
149: 
150:         g_return_val_if_fail(widget!=NULL, FALSE);
151:         g_return_val_if_fail(DDE_IS_GRAPH2D(widget), FALSE);
152:         g_return_val_if_fail(event!=NULL, FALSE);
153:         
154:         dde_graph2d_paint(widget, width, height);
155:         
156:         return FALSE;
157: }
158: 
159: 
160: static void dde_graph2d_paint(GtkWidget *widget, int width, int height)
161: {
162:         g_print("dde_graph2d_paint\n"); 
163:         cairo_t *cr;
164:         
165:         cr = gdk_cairo_create(widget->window);
166:         
167: //      PangoContext *pan = gtk_widget_get_pango_context(GTK_WIDGET(widget));
168: //      pango_context_set_base_gravity(pan, PANGO_GRAVITY_EAST);
169: 
170:         PangoLayout *layout;
171:         PangoFontDescription *font_description;
172:         
173:         font_description = pango_font_description_new();
174:         pango_font_description_set_family(font_description, "serif");
175:         pango_font_description_set_weight(font_description, PANGO_WEIGHT_BOLD);
176:         pango_font_description_set_absolute_size(font_description, 32 * PANGO_SCALE);
177:         
178:         layout = pango_cairo_create_layout(cr);
179:         PangoContext *context = pango_layout_get_context (layout);
180:         pango_context_set_gravity_hint(context, PANGO_GRAVITY_HINT_STRONG);
181:         pango_layout_set_font_description(layout, font_description);
182: //      pango_context_set_base_gravity(context, PANGO_GRAVITY_EAST);
183:         cairo_rotate(cr, 90 / (180.0 / G_PI));
184:         pango_layout_context_changed(layout);
185:         pango_layout_set_text(layout, "Hello, world", -1);
186:         
187:         cairo_set_source_rgb(cr, 0.0, 0.0, 1.0);
188:         cairo_move_to(cr, 10.0, 50.0);
189:         pango_cairo_show_layout(cr, layout);
190:         
191:         g_object_unref(layout);
192:         pango_font_description_free(font_description);
193: 
194:         cairo_destroy(cr);
195: }
196: 
197: 
198: static void dde_graph2d_destroy(GtkObject *object)
199: {
200:         GtkGraph2d *dde_graph2d;
201:         GtkGraph2dClass *klass;
202:         
203:         g_return_if_fail(object != NULL);
204:         g_return_if_fail(DDE_IS_GRAPH2D(object));
205:         
206:         dde_graph2d = DDE_GRAPH2D(object);
207:         
208:         klass = gtk_type_class(gtk_widget_get_type());
209:         
210:         if (GTK_OBJECT_CLASS(klass)->destroy) {
211:                 (*GTK_OBJECT_CLASS(klass)->destroy) (object);
212:         }
213: }