Re: new object, with variables





On Thu, 2008-06-12 at 11:12 +0200, Kees Scherpenhuijzen wrote:
2008/6/12, James Scott Jr <skoona verizon net>:
Kees,

I cannot seem figure out what your trying to do.  The code I see in the
notes is not something I understand; and I am an experienced gtk
developer.

Please back away from the code for a minute and try to describe what it
is your trying to achieve with this interface or application that
spawned this series of notes.  I'm hoping you just started off
completely wrong with the wrong assumptions about how to achieve your
goal.  In which case I/we can put you back on track.  But for now I
cannot keep up.

Why do I say this?
1. "i trying to make an object inherited from GTK_ACTION, this all is
managed."
Response:
* If you want to inherit from GTK_ACTION you make a completely new
widget.  Look for info on writing widgets from scratch.

* Wrapping a function around a g_object_new as you have done in both
examples is miss-leading at best.  You cannot make an existing
gtk_object do something it was not designed to; i.e. add a struct in its
object_new() parms.  You can g_object_set|get_data() on an existing
object; register a function for signal from an object so you can modify
its external behaviour, on and on...

Ok.  Back to "tell me what your trying to do?"

James,




On Wed, 2008-06-11 at 23:54 +0200, Kees Scherpenhuijzen wrote:
2008/5/14 Kees Scherpenhuijzen <noik00 gmail com>:
heyy,

@ the moment i trying to make an object inherited from GTK_ACTION,
this all is managed.
Only now i'm trying to initialize the object with an variable,  a
struct. Ie read the manual about the wrapper that is used, only i
can't seem
to find a way in which i can add the struct to g_object_new or otherwise.

to be clear, in this function:
GtkAction*
menu_action_new (const gchar *name,
                const gchar *label)
{
       GtkAction* action;
       _thunar_return_val_if_fail (name != NULL, NULL);
       _thunar_return_val_if_fail (label != NULL, NULL);

       action = g_object_new (MENU_TYPE_ACTION,
                                          "hide-if-empty", FALSE,
                                          "label", label,
                                          "name", name,
                                          NULL);
       return action;
}

i want to add the struct so i don't need any get of set functions, is
this possible?

the thing i want to do is to init :
 menu_thing    *item;
in:
struct _MenuTemplatesAction
{
 GtkAction             __parent__;
 menu_thing    *item;
};

I hope this enough info to get me close to an answer


--
Kees


Hi,

I've saw some examples about this problem and tried it with no luck. i
saw that the object was cast and just added.
i've tried:
GtkAction*
menu_action_new (const gchar *name,
                const gchar *label,
                Model *model)
{
       GtkAction* action;
       _thunar_return_val_if_fail (name != NULL, NULL);
       _thunar_return_val_if_fail (label != NULL, NULL);

       action = g_object_new (MENU_TYPE_ACTION,
                                          "hide-if-empty", FALSE,
                                          "label", label,
                                          "name", name,
                                          NULL);

       MENU_ACTION(action)->items = model;

       if(G_TYPE_FUNDAMENTAL (action) == G_TYPE_OBJECT)
       {
               g_message(model->menu->name);
               g_message(action->items);
               g_message("test");
       }
       return action;
}
but it seems the second g_message won't print, it gives an segmentation fault.

extra info: #define MENU_ACTION(obj)
(G_TYPE_CHECK_INSTANCE_CAST ((obj), MENU_TYPE_ACTION,
MenuTemplatesAction))

What am i missing here?

Thanks in advance



Hey,

I'm sorry if i'm not clear enough. maybe it's my english :P.

But to make everything clear:
I'm  trying to make a widget which can make menuitems out of a struct.
The struct isn't thought out so i'm not getting to deep in that.
The template i'm rewriting is found @
http://svn.xfce.org/svn/xfce/thunar/trunk/thunar/thunar-templates-action.c

What i'm trying to is when the action is created, I wan't to
initialize the object with a variable, so when the object is
shown(thunar_templates_action_menu_shown is used) the variable(struct)
is initialized.

I hope this clears the situation a bit.

And thanks for the help

Kees,

Your English is fine.  It was really the code throwing me off.

I will try to answer some of the memory related questions first.
Assuming you want to add a pointer, to a gtk_action object, of an
allocated memory structure containing instance data; there are two ways
I can thing of getting that done before writing my own widget -- which
makes all things possible.

1. Create the gtk_action first then append a user-data pointer to the
object with g_object_set_data() or g_object_set_data_full().  The full
varient frees the allocated memory when the object is destroyed.

2. Most certainly we will connect a routine to the "activate" signal of
the gtk_action object.  When we connect the signal attach a pointer to
our user-data to the signal using g_object_connect();


typedef struct _your_struct {
gchar   my_string[50];
GtkWidget *my_widget;
gpointer  reserved;
} your_struct;

typedef your_struct *ptr_your_struct;

object = gtk_action_new();
gpointer gptr_your_struct = g_new0(your_struct,1);


Item 1.
g_object_set_data(object,"any-but-same-label", gptr_your_struct)
...
gptr_your_struct = g_object_get_data(object,"any-but-same-label")

Item 2.
g_object_connect(object, "activate",fn_handler, gptr_your_struct, NULL);
...
void fn_handler(GtkAction *action, gpointer user_data)
{
 gptr_your_struct  pData = NULL;

 pData = (gptr_your_struct)user_data;
...
 g_message ("My data is %s", pData->my_string);
...
}

These methods work.  Now if the above is not enough functionality then
you will be creating your own true Widget.  For that all I can say is
"Creating a GTK widget from scratch" is already will documented.
However, look over the tools gtk-demo (its likely already installed),
and devhelp (local api documentation).

Why the segfault? I need to see more code before I can say definitively
- but it seems your approach to menu_action_new() is incorrect, as is
your attempt at creating a widget.  There is more code and structure to
creating a widget.  The one or two routines you posted is not enough for
me (maybe someone else).  I think the whole (widget) thing with the
header will be needed, please post more code so I can see the structure
of the whole widget.

Here is a hint: menu_action_new(0 should create a menu_action object
first! then inside the class init routine create any supporting objects,
like the gtk_action.

James,




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]