Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
3.7 kB
1
Indexable
Never
// Include gtk
#include <gtk/gtk.h>


void btn_clicked_1 (GtkButton *self, GPtrArray *textos);
void btn_clicked_2 (GtkButton *self, GPtrArray *textos);


static void on_activate (GtkApplication *app)
{
  GtkWidget *window = gtk_application_window_new (app);

  gtk_window_set_position (GTK_WINDOW(window),GTK_WIN_POS_CENTER);
  gtk_window_set_default_size(GTK_WINDOW(window), 300,200);
  //gtk_window_set_default_size(GTK_WINDOW(window), 800,500);
  gtk_container_set_border_width(GTK_CONTAINER(window),20);

  GtkWidget *header = g_object_new(
    GTK_TYPE_HEADER_BAR,
    "visible", TRUE,
    "show-close-button", TRUE,
    "title", "C & C++",
    NULL
  );

  GtkWidget *box_principal = g_object_new(
    GTK_TYPE_BOX,
    "visible", TRUE,
    "orientation", GTK_ORIENTATION_VERTICAL,
    "spacing", 10,
    NULL
  );

  GtkWidget *btn1 = g_object_new(
    GTK_TYPE_BUTTON,
    "visible", TRUE,
    "label", "Windows",
    "halign", GTK_ALIGN_CENTER,
    "valign", GTK_ALIGN_CENTER,
    NULL
  );

  GtkWidget *btn2 = g_object_new(
    GTK_TYPE_BUTTON,
    "visible", TRUE,
    "label", "Salir",
    "halign", GTK_ALIGN_CENTER,
    "valign", GTK_ALIGN_CENTER,
    NULL
  );

  GtkWidget *box_textos= g_object_new(
    GTK_TYPE_BOX,
    "visible", TRUE,
    "orientation", GTK_ORIENTATION_VERTICAL,
    "spacing", 10,
    NULL
  );

  GtkWidget *box_botones= g_object_new(
    GTK_TYPE_BOX,
    "visible", TRUE,
    "orientation", GTK_ORIENTATION_HORIZONTAL,
    "spacing", 10,
    NULL
  );

  GtkWidget *label1 = g_object_new(
    GTK_TYPE_LABEL,
    "visible", TRUE,
    "label", "En Windows también funciona",
    "halign", GTK_ALIGN_CENTER,
    "valign", GTK_ALIGN_CENTER,
    NULL
  );

  GtkWidget *label2 = g_object_new(
    GTK_TYPE_LABEL,
    "visible", TRUE,
    "label", "GTK 3",
    "halign", GTK_ALIGN_CENTER,
    "valign", GTK_ALIGN_CENTER,
    NULL
  );

  GPtrArray *textos = g_ptr_array_new();
  g_ptr_array_add(textos, label1);
  g_ptr_array_add(textos, label2);

  gtk_window_set_titlebar(GTK_WINDOW(window), header);
  gtk_box_pack_start(GTK_BOX(box_principal), box_textos, TRUE, FALSE, 0);
  gtk_box_pack_start(GTK_BOX(box_principal), box_botones, TRUE, FALSE, 0);

  gtk_box_pack_start(GTK_BOX(box_textos), label1, TRUE, FALSE, 0);
  gtk_box_pack_start(GTK_BOX(box_textos), label2, TRUE, FALSE, 0);
  gtk_box_pack_start(GTK_BOX(box_botones), btn1, TRUE, FALSE, 0);
  gtk_box_pack_start(GTK_BOX(box_botones), btn2, TRUE, FALSE, 0);
  gtk_container_add(GTK_CONTAINER(window), box_principal);




  g_signal_connect(btn1, "clicked", G_CALLBACK(btn_clicked_1), textos);
  g_signal_connect(btn2, "clicked", G_CALLBACK(btn_clicked_2), textos);
  g_signal_connect_swapped (G_OBJECT(window), "destroy", G_CALLBACK (gtk_window_close), window);
  gtk_widget_show_all (window);

}

int main (int argc, char *argv[]) 
{

  GtkApplication *app = gtk_application_new ("com.github.CodigoCristo.Arcris",
                                             G_APPLICATION_FLAGS_NONE);
  g_signal_connect (app, "activate", G_CALLBACK (on_activate), NULL);
  return g_application_run (G_APPLICATION (app), argc, argv);
}





void btn_clicked_1 (GtkButton *self, GPtrArray *textos) {

  gtk_label_set_label (GTK_LABEL ( g_ptr_array_index(textos,0) ), "Acción 1");
  gtk_label_set_label (GTK_LABEL ( g_ptr_array_index(textos,1) ), "Acción 1");

}

void btn_clicked_2 (GtkButton *self, GPtrArray *textos) {

  gtk_label_set_label (GTK_LABEL ( g_ptr_array_index(textos,0) ), "Acción 2");
  gtk_label_set_label (GTK_LABEL ( g_ptr_array_index(textos,1) ), "Acción 2");

}