LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   xalign/justify properties are not working on GtkLabel (https://www.linuxquestions.org/questions/linux-newbie-8/xalign-justify-properties-are-not-working-on-gtklabel-777671/)

deepthi_rapaka 12-23-2009 07:42 AM

xalign/justify properties are not working on GtkLabel
 
Hi,

I have my gtk label is packed like:


-HBox
-Event Box
- HBox
- Gtk Label

I already did a gtk_misc_set_alignment() to left on the label at the time of creation.

Now,at run time time i want to change the alignment of my label based on the xalign/yalign using gtk_misc_set_alignment(), this is not working.

joel2001k 12-26-2009 05:57 PM

a gtk_misc_set_alignment example
 
I wrote an example for you to demonstrate how I could work. Did you pack the label correctly? Can you tell me if the code below works for you?

Code:

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

void buttons_create(GtkHBox *hbox);
void button_up_callback(GtkButton *button, gpointer data);
void button_left_callback(GtkButton *button, gpointer data);
void button_right_callback(GtkButton *button, gpointer data);
void button_down_callback(GtkButton *button, gpointer data);
gboolean delete_event_callback(GtkWidget *widget, GdkEvent *event, GtkWindow *window);

GtkButton **button;
GtkLabel *label;
gboolean up, left;

void
buttons_create(GtkHBox *hbox)
{
  GtkAlignment *alignment;
  GtkTable *table;
  GtkArrow *arrow;

  alignment = (GtkAlignment *) gtk_alignment_new(0.0, 0.0, 1.0, 1.0);
  gtk_box_pack_start((GtkBox *) hbox, (GtkWidget *) alignment, FALSE, FALSE, 0);

  table = (GtkTable *) gtk_table_new(3, 3, TRUE);
  gtk_container_add((GtkContainer *) alignment, (GtkWidget *) table);

  /* create and pack buttons */
  button = (GtkButton **) malloc(4 * sizeof(GtkButton*));

  button[0] = (GtkButton *) gtk_button_new();
  gtk_table_attach_defaults(table,
                            (GtkWidget *) button[0],
                            1, 2,
                            0, 1);

  arrow = (GtkArrow *) gtk_arrow_new(GTK_ARROW_UP,
                                    GTK_SHADOW_NONE);
  gtk_container_add((GtkContainer *) button[0], (GtkWidget *) arrow);

  button[1] = (GtkButton *) gtk_button_new();
  gtk_table_attach_defaults(table,
                            (GtkWidget *) button[1],
                            0, 1,
                            1, 2);

  arrow = (GtkArrow *) gtk_arrow_new(GTK_ARROW_LEFT,
                                    GTK_SHADOW_NONE);
  gtk_container_add((GtkContainer *) button[1], (GtkWidget *) arrow);

  button[2] = (GtkButton *) gtk_button_new();
  gtk_table_attach_defaults(table,
                            (GtkWidget *) button[2],
                            2, 3,
                            1, 2);

  arrow = (GtkArrow *) gtk_arrow_new(GTK_ARROW_RIGHT,
                                    GTK_SHADOW_NONE);
  gtk_container_add((GtkContainer *) button[2], (GtkWidget *) arrow);

  button[3] = (GtkButton *) gtk_button_new();
  gtk_table_attach_defaults(table,
                            (GtkWidget *) button[3],
                            1, 2,
                            2, 3);

  arrow = (GtkArrow *) gtk_arrow_new(GTK_ARROW_DOWN,
                                    GTK_SHADOW_NONE);
  gtk_container_add((GtkContainer *) button[3], (GtkWidget *) arrow);

  /* connect buttons */
  g_signal_connect((GObject *) button[0], "clicked\0",
                  G_CALLBACK(button_up_callback), NULL);

  g_signal_connect((GObject *) button[1], "clicked\0",
                  G_CALLBACK(button_left_callback), NULL);

  g_signal_connect((GObject *) button[2], "clicked\0",
                  G_CALLBACK(button_right_callback), NULL);

  g_signal_connect((GObject *) button[3], "clicked\0",
                  G_CALLBACK(button_down_callback), NULL);
}

void
button_up_callback(GtkButton *button, gpointer data)
{
  if(left)
    gtk_misc_set_alignment((GtkMisc *) label, 0.0, 0.0);
  else
    gtk_misc_set_alignment((GtkMisc *) label, 1.0, 0.0);

  up = TRUE;
}

void
button_left_callback(GtkButton *button, gpointer data)
{
  if(up)
    gtk_misc_set_alignment((GtkMisc *) label, 0.0, 0.0);
  else
    gtk_misc_set_alignment((GtkMisc *) label, 0.0, 1.0);

  left = TRUE;
}

void
button_right_callback(GtkButton *button, gpointer data)
{
  if(up)
    gtk_misc_set_alignment((GtkMisc *) label, 1.0, 0.0);
  else
    gtk_misc_set_alignment((GtkMisc *) label, 1.0, 1.0);

  left = FALSE;
}

void
button_down_callback(GtkButton *button, gpointer data)
{
  if(left)
    gtk_misc_set_alignment((GtkMisc *) label, 0.0, 1.0);
  else
    gtk_misc_set_alignment((GtkMisc *) label, 1.0, 1.0);

  up = FALSE;
}

gboolean
delete_event_callback(GtkWidget *widget, GdkEvent *event, GtkWindow *window)
{
  free(button);
  gtk_main_quit();

  return(FALSE);
}

int
main(int argc, char **argv)
{
  GtkWindow *window;
  GtkHBox *hbox;

  gtk_init(&argc, &argv);

  window = (GtkWindow *) gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_window_set_default_size(window, 600, 400);

  hbox = (GtkHBox *) gtk_hbox_new(FALSE, 0);
  gtk_container_add((GtkContainer *) window, (GtkWidget *) hbox);

  up = TRUE;
  left = TRUE;

  buttons_create(hbox);

  label = (GtkLabel *) gtk_label_new("some text\0");
  gtk_box_pack_start((GtkBox *) hbox, (GtkWidget *) label, TRUE, TRUE, 0);
  gtk_misc_set_alignment((GtkMisc *) label, 0.0, 0.0);

  gtk_widget_show_all((GtkWidget *) window);
  g_signal_connect((GObject *) window, "delete-event\0",
                  G_CALLBACK(delete_event_callback), window);

  gtk_main();

  return(0);
}


deepthi_rapaka 12-28-2009 11:03 PM

Hi joel,

The example you gave is working :), thanks , I packed the label correctly but i was doing a gtk_widget_set_size_requset() on the label to occupy the space inside the container....tats y the alignment thing was not working.


All times are GMT -5. The time now is 08:59 AM.