1a05542005-07-28Martin Nilsson /* -*- C -*- */ class G.Object; //! The base object. All GDK and GTK classes inherit from this. //! The Pango classes also inherit from this class. signal notify; void destroy()
38523f2013-01-24Chris Angelico //! Destroy this object. This is the normal way to (eg) close a window.
1a05542005-07-28Martin Nilsson { if (THIS->obj) g_object_unref(THIS->obj); THIS->obj=NULL;
ba9e802006-02-27Martin Stjernholm  pgtk2_pop_n_elems(args);
1a05542005-07-28Martin Nilsson  push_int(0); } void set_property(string property, mixed value) //! Set a property on a G.Object (and any derived types) to value. {
ba9e802006-02-27Martin Stjernholm  pgtk2_verify_inited();
1a05542005-07-28Martin Nilsson  { char *prop; struct svalue *sv; get_all_args("set_property",args,"%s%*",&prop,&sv);
ba9e802006-02-27Martin Stjernholm  pgtk2_set_property(G_OBJECT(THIS->obj),prop,sv);
1a05542005-07-28Martin Nilsson  } RETURN_THIS(); } mixed get_property(string property) //! Get a property. {
ba9e802006-02-27Martin Stjernholm  pgtk2_verify_inited();
1a05542005-07-28Martin Nilsson  { char *prop; get_all_args("get_property",args,"%s",&prop);
ba9e802006-02-27Martin Stjernholm  pgtk2_get_property(G_OBJECT(THIS->obj),prop);
1a05542005-07-28Martin Nilsson  stack_swap();
ba9e802006-02-27Martin Stjernholm  pgtk2_pop_n_elems(args);
1a05542005-07-28Martin Nilsson  } }
68d1052006-08-02Lance Dillon 
526a652006-08-24Lance Dillon int signal_connect(string signal, function callback, mixed|void callback_arg,
b29c8c2013-01-04Lance Dillon  string|void detail,int|void connect_before)
1a05542005-07-28Martin Nilsson //! Connect a signal to a pike function. The function will be called with
b29c8c2013-01-04Lance Dillon //! the last argument to this function as its last argument (defaults to 0); //! the first argument is always the widget, and any other arguments are the //! ones supplied by GTK. If connect_before is nonzero, the callback will be //! called prior to the normal handling of the signal (and can return true //! to suppress that handling), otherwise it will be called after.
1a05542005-07-28Martin Nilsson //! <p> //! The return value of this function can be used to remove a signal with //! signal_disconnect(), and block and unblock the signal with signal_block() //! and signal_unblock(). { char *a; int id; struct signal_data *b; struct svalue *tmp1,*tmp2;
68d1052006-08-02Lance Dillon  GClosure *gc;
08b2c92006-09-11Lance Dillon  GQuark det;
526a652006-08-24Lance Dillon  char *detail=NULL;
b29c8c2013-01-04Lance Dillon  int connect_before=0;
32353e2005-12-30Martin Nilsson 
1a05542005-07-28Martin Nilsson  if (args==2) { push_int(0); args++; }
b29c8c2013-01-04Lance Dillon  get_all_args("signal_connect",args,"%s%*%*.%s%d",&a,&tmp1,&tmp2,&detail,&connect_before);
1a05542005-07-28Martin Nilsson 
32353e2005-12-30Martin Nilsson  b=(struct signal_data *)g_malloc0(sizeof(struct signal_data)); if (b==NULL) SIMPLE_OUT_OF_MEMORY_ERROR("signal_connect",sizeof(struct signal_data));
1a05542005-07-28Martin Nilsson  assign_svalue_no_free(&b->cb,tmp1); assign_svalue_no_free(&b->args,tmp2); b->signal_id=g_signal_lookup(a,G_TYPE_FROM_INSTANCE(THIS->obj)); if (!b->signal_id) {
68d1052006-08-02Lance Dillon  g_free(b);
1a05542005-07-28Martin Nilsson  Pike_error("Signal \"%s\" is not defined in the '%s' class ancestry.\n", a,g_type_name(G_TYPE_FROM_INSTANCE(THIS->obj))); }
68d1052006-08-02Lance Dillon  gc=g_cclosure_new_swap(G_CALLBACK(pgtk2_signal_func_wrapper), b,(GClosureNotify)pgtk2_free_signal_data); g_closure_set_marshal(gc,pgtk2_marshaller);
08b2c92006-09-11Lance Dillon  if (detail) det=g_quark_try_string(detail); else det=0; id=g_signal_connect_closure_by_id(G_OBJECT(THIS->obj),b->signal_id,det,
b29c8c2013-01-04Lance Dillon  gc,!connect_before);
ba9e802006-02-27Martin Stjernholm  pgtk2_pop_n_elems(args);
1a05542005-07-28Martin Nilsson  push_int(id); } void signal_disconnect(int signal_id) //! Remove a signal formerly added by signal_connect. The argument is the //! return value of signal_connect(). See signal_connect() for more info. { INT_TYPE i; get_all_args("signal_disconnect",args,"%i",&i); g_signal_handler_disconnect(G_OBJECT(THIS->obj),i);
3d76632005-11-03Lance Dillon  RETURN_THIS();
1a05542005-07-28Martin Nilsson } void signal_block(int signal_id) //! Temporarily block a signal handler. No signals will be received while the //! handler is blocked. //! See signal_connect() for more info. { INT_TYPE i; get_all_args("signal_block",args,"%i",&i); g_signal_handler_block(G_OBJECT(THIS->obj),i);
3d76632005-11-03Lance Dillon  RETURN_THIS();
1a05542005-07-28Martin Nilsson } void signal_unblock(int signal_id) //! Unblock a formerly blocked signal handler. See signal_block() and //! signal_connect() for more info. { INT_TYPE i; get_all_args("signal_unblock",args,"%i",&i); g_signal_handler_unblock(G_OBJECT(THIS->obj),i);
3d76632005-11-03Lance Dillon  RETURN_THIS();
1a05542005-07-28Martin Nilsson }
526a652006-08-24Lance Dillon void signal_emit(string signal_name, string|void detail)
1a05542005-07-28Martin Nilsson //! Send the current named signal. {
68d1052006-08-02Lance Dillon  pgtk2_verify_inited(); { char *s; GQuark gq; GSignalQuery _opts; GSignalQuery *opts=&_opts; guint signal_id;
526a652006-08-24Lance Dillon  const gchar *detail=NULL;
68d1052006-08-02Lance Dillon 
526a652006-08-24Lance Dillon  get_all_args("signal_emit",args,"%s.%s",&s,&detail);
68d1052006-08-02Lance Dillon  gq=g_quark_from_string(s); signal_id=g_signal_lookup(s,G_TYPE_FROM_INSTANCE(THIS->obj)); g_signal_query(signal_id,opts); if (opts->n_params!=args-1) { pgtk2_pop_n_elems(args); /* Pike_error("Signal %s expects %d args, got %d.\n",s,opts->n_params,args-1); */ Pike_error("signal_emit: signature mismatch for signal \"%s\" in the '%s' class ancestry.\n" " expected %d args, got %d.\n",s, g_type_name(G_OBJECT_TYPE(THIS->obj)),opts->n_params,args-1); } {
b638c52008-07-19Martin Stjernholm  unsigned i; GValue *params;
e4c57f2006-08-08Lance Dillon  GValue rvalue={0};
68d1052006-08-02Lance Dillon 
09eebe2013-02-02Henrik Grubbström (Grubba)  params = alloca(sizeof(GValue)*(opts->n_params+1));
68d1052006-08-02Lance Dillon  memset(params,0,sizeof(GValue)*(opts->n_params+1)); g_value_init(params+0,G_TYPE_FROM_INSTANCE(THIS->obj)); g_value_set_object(params+0,G_OBJECT(THIS->obj)); for (i=0; i<opts->n_params; i++) { pgtk2_set_gvalue(params+i+1,opts->param_types[i],&Pike_sp[-args+(i+1)]); } if (opts->return_type!=G_TYPE_NONE) g_value_init(&rvalue,opts->return_type);
526a652006-08-24Lance Dillon  if (detail) { GQuark detail_id; detail_id=g_quark_try_string(detail); g_signal_emitv(params,signal_id,detail_id,&rvalue); } else g_signal_emitv(params,signal_id,0,&rvalue);
68d1052006-08-02Lance Dillon  pgtk2_pop_n_elems(args); if (opts->return_type!=G_TYPE_NONE) { pgtk2_push_gvalue_rt(&rvalue); } else { push_int(0); }
9a04d32008-01-29Per Hedbor  g_value_unset(&rvalue);
68d1052006-08-02Lance Dillon  g_value_unset(params); for (i=1; i<opts->n_params; i++) g_value_unset(params+i); } } } int new_signal(string name, array types, string return_type) //! Create a new signal. { pgtk2_verify_inited(); { struct pike_string *t;
0e2f102006-08-02Martin Nilsson  struct pike_string *ret_type_name;
68d1052006-08-02Lance Dillon  struct array *a; gchar *name; guint id; GType *param_types; guint n_params; GType return_type; int i;
0e2f102006-08-02Martin Nilsson  get_all_args("new_signal",args,"%t%A%t",&t,&a,&ret_type_name);
68d1052006-08-02Lance Dillon  if (a->size) { ref_push_string(t); f_string_to_utf8(1); name=g_strdup(CGSTR0(Pike_sp[-1].u.string)); if (name==NULL) { pop_stack();
0e2f102006-08-02Martin Nilsson  SIMPLE_OUT_OF_MEMORY_ERROR("new_signal",ret_type_name->len*2);
68d1052006-08-02Lance Dillon  }
0e2f102006-08-02Martin Nilsson  if (is_same_string(ret_type_name,_STR("void"))) {
68d1052006-08-02Lance Dillon  return_type=G_TYPE_NONE;
0e2f102006-08-02Martin Nilsson  } else if (is_same_string(ret_type_name,_STR("int"))) {
68d1052006-08-02Lance Dillon  return_type=G_TYPE_INT;
0e2f102006-08-02Martin Nilsson  } else if (is_same_string(ret_type_name,_STR("float"))) {
68d1052006-08-02Lance Dillon  return_type=G_TYPE_DOUBLE;
0e2f102006-08-02Martin Nilsson  } else if (is_same_string(ret_type_name,_STR("string"))) {
68d1052006-08-02Lance Dillon  return_type=G_TYPE_STRING;
0e2f102006-08-02Martin Nilsson  } else if (is_same_string(ret_type_name,_STR("object"))) {
68d1052006-08-02Lance Dillon  return_type=G_TYPE_OBJECT; } else { return_type=G_TYPE_POINTER; }
cdaada2007-01-06Martin Nilsson  param_types=malloc(sizeof(GType)*a->size);
68d1052006-08-02Lance Dillon  if (param_types==NULL) { g_free(name); SIMPLE_OUT_OF_MEMORY_ERROR("new_signal",sizeof(GType)*a->size); }
cdaada2007-01-06Martin Nilsson 
68d1052006-08-02Lance Dillon  for (i=n_params=0; i<a->size; i++) {
017b572011-10-28Henrik Grubbström (Grubba)  if (TYPEOF(ITEM(a)[i]) != PIKE_T_STRING)
68d1052006-08-02Lance Dillon  continue;
0e2f102006-08-02Martin Nilsson  if (is_same_string(ITEM(a)[i].u.string,_STR("int"))) {
68d1052006-08-02Lance Dillon  param_types[n_params]=G_TYPE_INT; n_params++;
0e2f102006-08-02Martin Nilsson  } else if (is_same_string(ITEM(a)[i].u.string,_STR("float"))) {
68d1052006-08-02Lance Dillon  param_types[n_params]=G_TYPE_DOUBLE; n_params++;
48319e2006-08-03Lance Dillon  } else if (is_same_string(ITEM(a)[i].u.string,_STR("string"))) {
68d1052006-08-02Lance Dillon  param_types[n_params]=G_TYPE_STRING; n_params++;
48319e2006-08-03Lance Dillon  } else if (is_same_string(ITEM(a)[i].u.string,_STR("object"))) {
68d1052006-08-02Lance Dillon  param_types[n_params]=G_TYPE_OBJECT; n_params++; } else { param_types[n_params]=G_TYPE_POINTER; n_params++; } } pgtk2_pop_n_elems(args); id=g_signal_newv(name,G_TYPE_FROM_INSTANCE(THIS->obj),G_SIGNAL_RUN_LAST, NULL,NULL,NULL,pgtk2_marshaller,return_type, n_params,param_types); push_int(id); xfree(param_types); g_free(name); } }
1a05542005-07-28Martin Nilsson } void notify(string property); //! Emits a "notify" signal for the named property on the object. void accel_groups_activate(int accel_key, int accel_mods) //! Finds the first accelerator in an GTK.AccelGroup attached to this object //! that matches accel_key and accel_mods, and activates that accelerator. {
ba9e802006-02-27Martin Stjernholm  pgtk2_verify_inited();
1a05542005-07-28Martin Nilsson  {
cac4202005-12-17Marcus Comstedt  INT_TYPE accel_key; INT_TYPE accel_mods;
1a05542005-07-28Martin Nilsson  get_all_args("accel_groups_activate",args,"%i%i",&accel_key,&accel_mods); gtk_accel_groups_activate(G_OBJECT(THIS->obj),accel_key,accel_mods); } RETURN_THIS(); }
68d1052006-08-02Lance Dillon require gtk210; /* object ref_sink(); //! Increase the reference count of this object, and possibly remove the //! floating reference, if this object has a floating reference. */
1a05542005-07-28Martin Nilsson 
68d1052006-08-02Lance Dillon int is_floating(); //! Checks whether this object has a floating reference. endrequire;
636ceb2010-02-09Lance Dillon  string get_docs() //! Get documentation on object {
a039fb2013-02-08Henrik Grubbström (Grubba)  pgtk2_get_doc(G_OBJECT(THIS->obj), NULL);
636ceb2010-02-09Lance Dillon }
18cfd42010-09-11Lance Dillon  %{
7038ee2013-02-15Henrik Grubbström (Grubba) void pgtk2_object_destroy_data(gpointer data) {
18cfd42010-09-11Lance Dillon  struct svalue *sv=(struct svalue *)data; free_svalue(sv); g_free(sv); } %} object set_data(string name, mixed arg) //! Gets a named field from the object. { pgtk2_verify_inited(); { char *name; struct svalue *sv,*sv1; get_all_args("set_data",args,"%s%*",&name,&sv); sv1=(struct svalue *)g_malloc(sizeof(struct svalue)); assign_svalue_no_free(sv1,sv);
7038ee2013-02-15Henrik Grubbström (Grubba)  g_object_set_data_full(G_OBJECT(THIS->obj), name, (void *)sv1, pgtk2_object_destroy_data);
18cfd42010-09-11Lance Dillon  } RETURN_THIS(); } object get_data(string name) //! Each object carries around a table of associations from strings to //! pointers. This function lets you set an association. //! //! If the object already had an association with that name, the old //! association will be destroyed. { pgtk2_verify_inited(); { struct svalue *sv; char *name; get_all_args("get_data",args,"%s",&name); sv=(struct svalue *)g_object_get_data(G_OBJECT(THIS->obj),name); pop_n_elems(args);
643f202013-12-21Chris Angelico  if (sv) push_svalue(sv); else push_undefined();
18cfd42010-09-11Lance Dillon  } }