Re: GtkEntry in GtkCList



Hi,

I know that I can not directly add widget to clist.
I have tried it and is working but I have some problems.

I am adding clist in scrollwindow.
scrollwindow in vbox.
entry in vbox
vbox in toplevel window.

On double click of clist I am finding x, y, width and height of cell
clicked.
then  calling

     gdk_window_reparent( entry->window,  clist->window,  x,  y );

then entry's gdkwindow appears in clist, you can edit it.
But I am not able to remove the empty space at bottom of the vbox,
created because of reparenting of gdkwindow of entry.

my source code is here

/*****************************************/
#include <stdio.h>
#include <stdlib.h>
#include <gtk/gtk.h>


GtkWidget *clist_entry = NULL;
gboolean  reparented = FALSE;

/* this defigns the base grid spacing */
#define CELL_SPACING 1

/* added the horizontal space at the beginning and end of a row*/
#define COLUMN_INSET 3

#define TESTGTK_CLIST_COLUMNS 12

/* returns the top pixel of the given row in the context of
 * the list height */
#define ROW_TOP(clist, row)        (((clist)->row_height + CELL_SPACING)
* (row))

/* returns the left pixel of the given column in the context of
 * the list width */
#define COLUMN_LEFT(clist, colnum) ((clist)->column[(colnum)].area.x)

/* returns the total width of the list */
static inline gint
LIST_WIDTH (GtkCList * clist)
{
    gint last_column;

    for (last_column = clist->columns - 1;
           last_column >= 0 && !clist->column[last_column].visible;
last_column--);

    if (last_column >= 0)
         return (clist->column[last_column].area.x +

    clist->column[last_column].area.width +
    COLUMN_INSET + CELL_SPACING);

    return 0;
}

static void
clist_entry_activate_cb ( GtkWidget *widget, gpointer data_  )
{
    g_print("\n clist_entry_activate_cb ");
    GtkCList *clist = (GtkCList *)data_;

    gint *row = (gint *)gtk_object_get_data( GTK_OBJECT(widget),
"editfield_row" );
    gint *clm = (gint *)gtk_object_get_data( GTK_OBJECT(widget),
"editfield_clm" );

    gchar *entry_text = gtk_entry_get_text(GTK_ENTRY(widget));
    gtk_clist_set_text ( clist, *row, *clm, entry_text );

    gdk_window_hide( widget->window );
}

/************** Double click on clist to get entry *******************/
static gboolean
clist_button_press_cb ( GtkWidget *clist_, GdkEvent *gdkevent, gpointer
clist_parent_ )
{
    GdkEventButton *bevent = (GdkEventButton *)gdkevent ;
    GtkCList *clist = GTK_CLIST(clist_);

    if( reparented )
         gdk_window_hide( clist_entry->window );

    gint row;
    gint clm;
    gtk_clist_get_selection_info( clist, (gint)bevent->x,
(gint)bevent->y, &row, &clm );

    if( bevent->button == 1 && bevent->type == GDK_2BUTTON_PRESS )
    {
         gfloat hadjval = clist->hadjustment->value;
         gfloat vadjval = clist->vadjustment->value;
         gint x = COLUMN_LEFT( clist, clm );
         gint y = ROW_TOP( clist, row );
         gint width = 0, nextcx = 0;
         width = 0;
         nextcx = 0;
         if( clm == ( clist->columns - 1 ) )
             width = LIST_WIDTH(clist) - x;
         else
         {
                 nextcx = COLUMN_LEFT( clist, clm+1 );
                 width = nextcx - x;
         }

         x = x - hadjval;
         y = y - vadjval + 27;
         gint height = clist->row_height;

         if(  ! reparented )
         {
              gint *trow = g_new( gint, 1 );
             gint *tclm = g_new( gint, 1 );
             *trow = row;
             *tclm = clm;
             gtk_object_set_data( GTK_OBJECT(clist_entry),
"editfield_row", trow );
             gtk_object_set_data( GTK_OBJECT(clist_entry),
"editfield_clm", tclm );

             gdk_window_reparent( clist_entry->window,
GTK_WIDGET(clist)->window, x, y );

             reparented = TRUE;
         }

         if( clist_entry )
         {
             gint *prow = (gint *)gtk_object_get_data(
GTK_OBJECT(clist_entry), "editfield_row" );
             gint *pclm = (gint *)gtk_object_get_data(
GTK_OBJECT(clist_entry), "editfield_clm" );
             *prow = row;
             *pclm = clm;
             gdk_window_move_resize( clist_entry->window, x, y, width,
height-1 );
             gdk_window_raise( clist_entry->window );
             gdk_window_show( clist_entry->window );
             gchar *text;
             gtk_clist_get_text ( clist, row, clm, &text );
             gtk_entry_set_text( (GtkEntry*)clist_entry, text );
         }
    }

    return TRUE;
}

static void
clist_window_destroy_cb( GtkWidget *widget_, gpointer )
{
    gtk_widget_destroy( widget_ );
    gtk_exit(0);
}

int main(int argc, char * argv[])
{
    GtkWidget * window;
    GtkWidget *vbox;
    GtkWidget *clist;
    GtkWidget *scrolled_win;

    int i = 0;
    int clist_rows = 0;
    static char *titles[] =
    {
        "auto resize", "not resizeable", "max width 100", "min width
50",
        "hide column", "Title 5", "Title 6", "Title 7",
        "Title 8",  "Title 9",  "Title 10", "Title 11"
    };

    char text[TESTGTK_CLIST_COLUMNS][50];
    char *texts[TESTGTK_CLIST_COLUMNS];

    gtk_init(&argc, &argv);

    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_signal_connect (GTK_OBJECT (window), "destroy",
        GTK_SIGNAL_FUNC (clist_window_destroy_cb), NULL );
    gtk_window_set_title (GTK_WINDOW (window), "clistedit");

    scrolled_win = gtk_scrolled_window_new ((GtkAdjustment *)NULL,
(GtkAdjustment *)NULL);
    gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
         GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);

    clist = gtk_clist_new_with_titles (TESTGTK_CLIST_COLUMNS, titles);

    for (i = 1; i < TESTGTK_CLIST_COLUMNS; i++)
         gtk_clist_set_column_width (GTK_CLIST (clist), i, 80);

    for (i = 0; i < TESTGTK_CLIST_COLUMNS; i++)
    {
             texts[i] = text[i];
             sprintf (text[i], "Column %d", i);
    }

    sprintf (text[1], "Right");
    sprintf (text[2], "Center");

    for (i = 0; i < 10; i++)
    {
         sprintf (text[0], "CListRow %d", clist_rows++);
         gtk_clist_append (GTK_CLIST (clist), texts);
    }

    gtk_container_add (GTK_CONTAINER (scrolled_win), clist);

    vbox = gtk_vbox_new( FALSE, 0 );
    gtk_box_pack_start( GTK_BOX(vbox), scrolled_win, TRUE, TRUE, 0 );

     clist_entry = gtk_entry_new();
      gtk_entry_set_editable( (GtkEntry*)clist_entry, TRUE );
      gtk_signal_connect (GTK_OBJECT(clist_entry), "activate",
                 GTK_SIGNAL_FUNC (clist_entry_activate_cb), clist );
      gtk_box_pack_start( GTK_BOX(vbox),  clist_entry, FALSE, FALSE, 0
);

     gtk_signal_connect_after (GTK_OBJECT (clist), "button_press_event",

                    GTK_SIGNAL_FUNC (clist_button_press_cb), vbox );

    gtk_container_add (GTK_CONTAINER (window), vbox);

    gtk_widget_show(clist);
    gtk_widget_show(scrolled_win);
    gtk_widget_show(clist_entry);
    gtk_widget_show(vbox);
    gtk_widget_show(window);
    gtk_main();

    return 0;
}

/*******************************************/


Havoc Pennington wrote:

Hi,

The CList doesn't support child widgets in the rows.

You have to do something like put an entry next to the clist which is
used to edit the currently-selected clist row.

Havoc

--

---------------------------------------------------------------------
        VIJAYKUMAR S. KALE
ESG, TTIL, Telco Campus,PUNE - 411 018
email:  vskale telco co in
phone:  (off.)  +91-212-7402049 (Direct)
                +91-212-7402947 (Direct)
        (resi.) +91-212-4358085
----------------------------------------------------------------------
~


Attachment: vsk.vcf
Description: Card for VijayaKumar S. Kale



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