0a99d462b192abc8da7e348d62982e3769d287b3
[platform/upstream/gstreamer.git] / editor / gsteditorpad.c
1 /* Gnome-Streamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20
21 #include <gnome.h>
22 #include <gst/gst.h>
23
24 #include "gsteditor.h"
25
26 /* class functions */
27 static void gst_editor_pad_class_init(GstEditorPadClass *klass);
28 static void gst_editor_pad_init(GstEditorPad *pad);
29 static void gst_editor_pad_set_arg(GtkObject *object,GtkArg *arg,guint id);
30 static void gst_editor_pad_get_arg(GtkObject *object,GtkArg *arg,guint id);
31 static void gst_editor_pad_realize(GstEditorPad *pad);
32
33 static void gst_editor_pad_position_changed(GstEditorPad *pad, GstEditorElement *element);
34
35 /* class implementation functions */
36 //static void gst_editor_pad_update(GnomeCanvasItem *item,double *affine,
37 //                                      ArtSVP *clip_path,int flags);
38 //static gint gst_editor_pad_event(GnomeCanvasItem *item,GdkEvent *event);
39
40 /* events fired by items within self */
41 static gint gst_editor_pad_padbox_event(GnomeCanvasItem *item,
42                                         GdkEvent *event,
43                                         GstEditorPad *pad);
44
45 /* utility functions */
46 static void gst_editor_pad_resize(GstEditorPad *pad);
47
48
49 enum {
50   ARG_0,
51   ARG_X,
52   ARG_Y,
53   ARG_WIDTH,
54   ARG_HEIGHT,
55   ARG_PAD,
56 };
57
58 enum {
59   LAST_SIGNAL
60 };
61
62 static GtkObjectClass *parent_class;
63 //static guint gst_editor_pad_signals[LAST_SIGNAL] = { 0 };
64
65 GtkType 
66 gst_editor_pad_get_type (void) 
67 {
68   static GtkType pad_type = 0;
69
70   if (!pad_type) {
71     static const GtkTypeInfo pad_info = {
72       "GstEditorPad",
73       sizeof(GstEditorPad),
74       sizeof(GstEditorPadClass),
75       (GtkClassInitFunc)gst_editor_pad_class_init,
76       (GtkObjectInitFunc)gst_editor_pad_init,
77       NULL,
78       NULL,
79       (GtkClassInitFunc)NULL,
80     };
81     pad_type = gtk_type_unique(gtk_object_get_type(),&pad_info);
82   }
83   return pad_type;
84 }
85
86 static void 
87 gst_editor_pad_class_init (GstEditorPadClass *klass) 
88 {
89   GtkObjectClass *object_class;
90
91   object_class = (GtkObjectClass*)klass;
92
93   parent_class = gtk_type_class(gtk_object_get_type());
94
95   gtk_object_add_arg_type("GstEditorPad::x",GTK_TYPE_DOUBLE,
96                           GTK_ARG_READWRITE,ARG_X);
97   gtk_object_add_arg_type("GstEditorPad::y",GTK_TYPE_DOUBLE,
98                           GTK_ARG_READWRITE,ARG_Y);
99   gtk_object_add_arg_type("GstEditorPad::width",GTK_TYPE_DOUBLE,
100                           GTK_ARG_READWRITE,ARG_WIDTH);
101   gtk_object_add_arg_type("GstEditorPad::height",GTK_TYPE_DOUBLE,
102                           GTK_ARG_READWRITE,ARG_HEIGHT);
103   gtk_object_add_arg_type("GstEditorPad::pad",GTK_TYPE_POINTER,
104                           GTK_ARG_READWRITE,ARG_PAD);
105
106   klass->realize = gst_editor_pad_realize;
107
108   object_class->set_arg = gst_editor_pad_set_arg;
109   object_class->get_arg = gst_editor_pad_get_arg;
110 }
111
112 static void 
113 gst_editor_pad_init(GstEditorPad *pad) 
114 {
115 }
116
117 GstEditorPad*
118 gst_editor_pad_new(GstEditorElement *parent,GstPad *pad,
119                    const gchar *first_arg_name, ...) 
120 {
121   GstEditorPad *editorpad;
122   va_list args;
123
124   g_return_val_if_fail(parent != NULL, NULL);
125   g_return_val_if_fail(GST_IS_EDITOR_ELEMENT(parent), NULL);
126   g_return_val_if_fail(pad != NULL, NULL);
127   g_return_val_if_fail(GST_IS_PAD(pad), NULL);
128
129   editorpad = GST_EDITOR_PAD(gtk_type_new(GST_TYPE_EDITOR_PAD));
130   editorpad->pad = pad;
131   GST_EDITOR_SET_OBJECT(pad, editorpad);
132
133   va_start(args,first_arg_name);
134   gst_editor_pad_construct(editorpad,parent,first_arg_name,args);
135   va_end(args);
136
137   if (GST_PAD_CONNECTED (pad)) {
138     GstPad *peerpad;
139     GstEditorPad *peereditorpad;
140
141     // FIXME does this need to check for ghost/real?
142     peerpad = GST_PAD_PEER(pad);
143
144     peereditorpad = GST_EDITOR_GET_OBJECT (peerpad);
145
146     if (peereditorpad) { 
147       GstEditorConnection *connection;
148
149       connection = gst_editor_connection_new (parent, editorpad);
150       gst_editor_connection_set_endpad (connection, peereditorpad);
151     }
152   }
153
154   gtk_signal_connect_object (GTK_OBJECT (parent), "position_changed", 
155                              gst_editor_pad_position_changed, editorpad);
156
157   return editorpad;
158 }
159
160 void 
161 gst_editor_pad_construct(GstEditorPad *pad,
162                          GstEditorElement *parent,
163                          const gchar *first_arg_name,va_list args) 
164 {
165   GtkObject *obj = GTK_OBJECT(pad);
166   GSList *arg_list = NULL, *info_list = NULL;
167   gchar *error;
168   GstEditorPadClass *padclass;
169   
170 //  g_print("in gst_editor_pad_construct()\n");
171       
172   error = gtk_object_args_collect(GTK_OBJECT_TYPE(obj),&arg_list,
173                                   &info_list,first_arg_name,args);
174   if (error) {
175     g_warning("gst_editor_pad_construct(): %s",error);
176     g_free(error);
177   } else {
178     GSList *arg,*info;
179 //    g_print("setting all the arguments on the pad\n");
180     for (arg=arg_list,info=info_list;arg;arg=arg->next,info=info->next)
181       gtk_object_arg_set(obj,arg->data,info->data);
182     gtk_args_collect_cleanup(arg_list,info_list);
183   }
184
185   pad->parent = parent;
186
187   padclass = GST_EDITOR_PAD_CLASS(GTK_OBJECT(pad)->klass);
188   if (padclass)
189     (padclass->realize)(pad);
190 }
191
192 static void 
193 gst_editor_pad_set_arg (GtkObject *object,GtkArg *arg,guint id) 
194 {
195   GstEditorPad *pad;
196
197   /* get the major types of this object */
198   pad = GST_EDITOR_PAD(object);
199
200   switch (id) {
201     case ARG_X:
202       pad->x = GTK_VALUE_DOUBLE(*arg);
203       break;
204     case ARG_Y:
205       pad->y = GTK_VALUE_DOUBLE(*arg);
206       break;
207     case ARG_WIDTH:
208       pad->width = GTK_VALUE_DOUBLE(*arg);
209       pad->resize = TRUE;
210       break;
211     case ARG_HEIGHT:
212       pad->height = GTK_VALUE_DOUBLE(*arg);
213       pad->resize = TRUE;
214       break;
215     case ARG_PAD:
216       /* FIXME: this is very brute force */
217       pad->pad = GTK_VALUE_POINTER(*arg);
218       break;
219     default:
220       g_warning("gsteditorpad: unknown arg!");
221       break;
222   }
223 }
224
225 static void 
226 gst_editor_pad_get_arg (GtkObject *object,GtkArg *arg,guint id) 
227 {
228   GstEditorPad *pad;
229
230   /* get the major types of this object */
231   pad = GST_EDITOR_PAD(object);
232
233   switch (id) {
234     case ARG_X:
235       GTK_VALUE_INT(*arg) = pad->x;
236       break;
237     case ARG_Y:
238       GTK_VALUE_INT(*arg) = pad->y;
239       break;
240     case ARG_WIDTH:
241       GTK_VALUE_INT(*arg) = pad->width;
242       break;
243     case ARG_HEIGHT:
244       GTK_VALUE_INT(*arg) = pad->height;
245       break;
246     default:
247       arg->type = GTK_TYPE_INVALID;
248       break;
249   }
250 }
251
252 static void 
253 gst_editor_pad_realize (GstEditorPad *pad) 
254 {
255 //  g_print("realizing editor pad %p\n",pad);
256
257   /* we must be attached to an element */
258   g_return_if_fail(pad->parent != NULL);
259
260   /* create the group and bounding box */
261   pad->group = GNOME_CANVAS_GROUP(gnome_canvas_item_new(pad->parent->group,
262     gnome_canvas_group_get_type(),"x",pad->x,"y",pad->y,NULL));
263   g_return_if_fail(pad->group != NULL);
264   GST_EDITOR_SET_OBJECT(pad->group,pad);
265
266   pad->border = gnome_canvas_item_new(pad->group,
267     gnome_canvas_rect_get_type(),
268     "width_units",1.0,"fill_color_rgba", 0xCCFFCC00,"outline_color","black", 
269     "x1",0.0,"y1",0.0,"x2",pad->width,"y2",pad->height,NULL);
270   g_return_if_fail(pad->border != NULL);
271   GST_EDITOR_SET_OBJECT(pad->border,pad);
272
273   /* create the pad box on the correct side */
274   // FIXME does this need to check for ghost/real?
275   pad->issrc = (GST_PAD_DIRECTION(pad->pad) == GST_PAD_SRC);
276   if (pad->issrc)
277     pad->padbox = gnome_canvas_item_new(pad->group,
278       gnome_canvas_rect_get_type(),
279       "width_units",1.0,"fill_color","white","outline_color","black",
280       "x1",pad->x-4.0,"y1",2.0,"x2",pad->x,"y2",pad->height-2.0,NULL);
281   else
282     pad->padbox = gnome_canvas_item_new(pad->group,
283       gnome_canvas_rect_get_type(),
284       "width_units",1.0,"fill_color","white","outline_color","black",
285       "x1",0.0,"y1",2.0,"x2",4.0,"y2",pad->height-2.0,NULL);
286   g_return_if_fail(pad->padbox != NULL);
287   GST_EDITOR_SET_OBJECT(pad->padbox,pad);
288
289   gtk_signal_connect(GTK_OBJECT(pad->group),"event",
290     GTK_SIGNAL_FUNC(gst_editor_pad_padbox_event),pad);
291
292   pad->title = gnome_canvas_item_new(pad->group,
293     gnome_canvas_text_get_type(),
294     "text",gst_pad_get_name(pad->pad),
295     "x",0.0,"y",0.0,"anchor",GTK_ANCHOR_NORTH_WEST,
296     "font_gdk",gtk_widget_get_default_style()->font,
297     NULL);
298   g_return_if_fail(pad->title != NULL);
299   GST_EDITOR_SET_OBJECT(pad->title,pad);
300
301   pad->realized = TRUE;
302   pad->resize = TRUE;
303   gst_editor_pad_repack(pad);
304 }
305
306
307 static void 
308 gst_editor_pad_resize (GstEditorPad *pad) 
309 {
310   gdouble minwidth,minheight;
311
312 //  g_print("resizing pad\n");
313
314   minwidth = 0;minheight = 0;
315
316   /* get the text size and add it into minsize */
317   minwidth = gst_util_get_double_arg(GTK_OBJECT(pad->title),
318                                      "text_width") + 2.0;
319   minheight = gst_util_get_double_arg(GTK_OBJECT(pad->title),
320                                       "text_height");
321
322   /* calculate the size of the padbox */
323   pad->boxheight = minheight - 4.0;
324   pad->boxwidth = pad->boxheight / 2.0;
325   minwidth += pad->boxwidth;
326
327   /* force the thing to grow if necessary */
328   pad->width = MAX(pad->width,minwidth);
329   pad->height = MAX(pad->height,minheight);
330
331   /* update the connection if there is one */
332 //  g_print("connection is %p\n",pad->connection);
333   if (pad->connection != NULL)
334     gst_editor_connection_resize(pad->connection);
335 }
336
337 void 
338 gst_editor_pad_repack (GstEditorPad *pad) 
339 {
340   gdouble x1,y1,x2,y2;
341
342   if (!pad->realized) return;
343
344   gst_editor_pad_resize(pad);
345
346   x1 = 0;y1 = 0;
347   x2 = x1 + pad->width;y2 = y1 + pad->height;
348 //  g_print("repacking pad at %.2fx%.2f - %.2fx%.2f\n",x1,y1,x2,y2);
349
350   /* move the group */
351   gtk_object_set(GTK_OBJECT(pad->group),"x",pad->x,"y",pad->y,NULL);
352
353   /* start by resizing the bordering box */
354   gtk_object_set(GTK_OBJECT(pad->border),
355                  "x1",x1,"y1",y1,"x2",x2,"y2",y2,NULL);
356
357   /* if we're a left-jusified sink */
358   if (pad->issrc) {
359     /* and move the pad box */
360     gtk_object_set(GTK_OBJECT(pad->padbox),
361                    "x1",x2-pad->boxwidth,"y1",y1+2.0,
362                    "x2",x2,"y2",y2-2.0,NULL);
363     /* then move the text to the right place */
364     gtk_object_set(GTK_OBJECT(pad->title),
365                    "x",x2-pad->boxwidth-1.0,"y",y1,
366                    "anchor",GTK_ANCHOR_NORTH_EAST,
367                    NULL);
368   } else {
369     /* and move the pad box */
370     gtk_object_set(GTK_OBJECT(pad->padbox),
371                    "x1",x1,"y1",y1+2.0,
372                    "x2",x1+pad->boxwidth,"y2",y2-2.0,NULL);
373     /* then move the text to the right place */
374     gtk_object_set(GTK_OBJECT(pad->title),
375                    "x",x1+pad->boxwidth+1.0,"y",y1,
376                    "anchor",GTK_ANCHOR_NORTH_WEST,
377                    NULL);
378   }
379
380   if (pad->connection != NULL) {
381     pad->connection->resize = TRUE;
382     gst_editor_connection_resize(pad->connection);
383   }
384
385   pad->resize = FALSE;
386 }
387
388
389 /*
390 static gint gst_editor_pad_event(GnomeCanvasItem *item,GdkEvent *event) {
391   GstEditorPad *pad = GST_EDITOR_PAD(item);
392   gdouble item_x,item_y;
393   GdkCursor *fleur;
394   gdouble tx,ty;
395
396   item_x = event->button.x;
397   item_y = event->button.y;
398   gnome_canvas_item_w2i(item->parent,&item_x,&item_y);
399
400   switch(event->type) {
401     case GDK_ENTER_NOTIFY:
402 //      g_print("entered pad\n");
403       break;
404     case GDK_LEAVE_NOTIFY:
405 //      g_print("left pad\n");
406       break;
407     default:
408       break;
409   }
410   return FALSE;
411 }
412 */
413
414 /* FIXME FIXME FIXME */
415 static gint 
416 gst_editor_pad_padbox_event(GnomeCanvasItem *item,
417                             GdkEvent *event,
418                             GstEditorPad *pad) 
419 {
420   GstEditorElement *element;
421   GstEditorBin *bin;
422
423 //  g_print("padbox has event %d\n",event->type);
424   g_return_val_if_fail(GST_IS_EDITOR_PAD(pad), FALSE);
425
426   element = pad->parent;
427   bin = element->parent;
428
429   switch(event->type) {
430     case GDK_ENTER_NOTIFY:
431       gtk_object_set(GTK_OBJECT(pad->border),
432                  "fill_color_rgba", 0xBBDDBB00, NULL);
433       break;
434     case GDK_LEAVE_NOTIFY:
435       gtk_object_set(GTK_OBJECT(pad->border),
436                  "fill_color_rgba", 0xCCFFCC00, NULL);
437       break;
438     case GDK_BUTTON_PRESS:
439 //      g_print("have button press in pad '%s'\n",
440 //              gst_pad_get_name(pad->pad));
441       gst_editor_bin_start_banding(bin,pad);
442       return TRUE;
443       break;
444     case GDK_MOTION_NOTIFY:
445 //      g_print("have motion in pad\n");
446       break;
447     default:
448       break;
449   }
450   return FALSE;
451 }
452
453 static void
454 gst_editor_pad_position_changed(GstEditorPad *pad, 
455                                 GstEditorElement *element)
456 {
457   GList *pads;
458
459   if (pad->connection) {
460 //      g_print("updating pad's connection\n");
461     pad->connection->resize = TRUE;
462     gst_editor_connection_resize(pad->connection);
463   }
464 }