If it's not on-topic, it's in here.
Post a reply

gtk

Sun Feb 17, 2013 11:38 am

While fooling a bit with codeblocks, which i don't like much, i figured i could check gui-apps, for the lulz.
It didn't take long and i exited codeblocks and was doing gtk-coding for an hour or two.

I only found this how-to:
http://developer.gnome.org/gtk-tutorial/2.22/
which is way too fast, for my taste.
If i find a better how-to i might do this for a while.

I fool around a lot, these days. gdb too. valgrind. and, like said, codeblocks
By that i run into problems. Say "gcc easy-gtk.c" didn't work. I searched the WisdomWarningsWorkarounds, and found that i have to use "gcc easy-gtk $(pkg-config gtk+-2.0 --cflags) $(pkg-config gtk+-2.0 --libs)" to make it find <gtk/gtk.h>, which, for unknown reasons, is in /usr/include/gtk-2.0.

If my math is right, i still miss 9500 hours to be a badass coder ... :-) (but for the same time the grim reaper has announced a whistle-stop. Oh my! )

Re: gtk

Wed Feb 27, 2013 11:53 pm

Today i made a step in the rigtht direction:
I used a vertical_box and packed horizontal boxes inside of it.
Also was able to create a separator and a button "quit" which closes the window.

That won't make the wizards scream, but i am ok with it.
All that padding and filling is beyond me (and probably ever will be).
Code:
#include <stdio.h>
#include <stdlib.h>
#include <gtk/gtk.h>


static gboolean delete_event(GtkWidget *widget, GdkEvent *event, gpointer data)
{
   gtk_main_quit();
   return FALSE;
}


int main(int argc, char *argv[])
{
   GtkWidget *window;
   GtkWidget *vbox;
   GtkWidget *hbox;
   GtkWidget *button;
   GtkWidget *separator;

   gtk_init(&argc, &argv);

   /*----------------------------------------*/
   /* window */
   window = gtk_window_new(GTK_WINDOW_TOPLEVEL);    
   g_signal_connect(window, "delete-event", G_CALLBACK(delete_event), NULL);
   gtk_widget_show(window);


   /*----------------------------------------*/
   /* vertical box */
   vbox = gtk_vbox_new(FALSE, 0);
   gtk_widget_show(vbox);


   /*----------------------------------------*/
   /* first horizontal box */
   hbox = gtk_hbox_new(FALSE, 0);
   gtk_widget_show(hbox);
   button = gtk_button_new_with_label("mount");
   gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0);
   gtk_widget_show(button);
   
   button = gtk_button_new_with_label("fdisk");
   gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0);
   gtk_widget_show(button);


   gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);

   /*----------------------------------------*/
   /* separator */
   separator = gtk_hseparator_new();
   gtk_widget_set_size_request(separator, 400, 5);
   gtk_box_pack_start(GTK_BOX(vbox), separator, FALSE, TRUE, 5);
   gtk_widget_show(separator);    
   
   /*----------------------------------------*/
   /* second horizontal box */
   hbox = gtk_hbox_new(TRUE, 50);
   gtk_widget_show(hbox);
   button = gtk_button_new_with_label("cat");
   gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0);
   gtk_widget_show(button);

   button = gtk_button_new_with_label("less");
   gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0);
   gtk_widget_show(button);

   gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
   
      
   /*----------------------------------------*/
   /* separator */
   separator = gtk_hseparator_new();
   gtk_widget_set_size_request(separator, 400, 5);
   gtk_box_pack_start(GTK_BOX(vbox), separator, FALSE, TRUE, 5);
   gtk_widget_show(separator);
   
   /*----------------------------------------*/
   /* quit box */
   hbox = gtk_hbox_new(FALSE, 0);
   gtk_widget_show(hbox);
   button = gtk_button_new_with_label("quit");
   g_signal_connect_swapped(button, "clicked", G_CALLBACK(gtk_main_quit), window);
   gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, FALSE, 50);    
   gtk_widget_show(button);

   gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);

   /*----------------------------------------*/
   /* adding vbox to container window */
   gtk_container_add(GTK_CONTAINER(window), vbox);



   /*----------------------------------------*/
   gtk_main();


   /*----------------------------------------*/
   return EXIT_SUCCESS;
}

* in C is a lot. Here it is a pointer (not multiplicate and not dereferencing).
& is the address of a variable. If a function expects a pointer, and you got a variable, say int i = 42, you can use &i to give the function what it asks for. You could also do: int *j; j = &i; and then offer j to the function. Confusing? Yes.
Both can be ignored to understand what is getting done.

Ups: Just in case,
to compile it you will need "libgtk2.0-dev" and
gcc name_of.c $(pkg-config gtk+-2.0 --cflags) $(pkg-config gtk+-2.0 --libs)
Then click on "quit" and foo, it closes. A miracle.

Re: gtk

Thu Feb 28, 2013 12:43 am

Not sure why i posted the above.
Perhaps because packing horizontal boxes in vertical boxes
was quite hard to understand.
But the window doesn't do anything.
Thinking about it i can think of two things:
a) if i click on "mount" or "fdisk" i can see the output of said
commands (seeing it on the command line after clicking is not hard,
i mean inside the window or in a new window).
b) being able to enter text inside a window (similar but much easier than an
editor or a notekeeper).
Post a reply