Markus Rechberger / Gtk
 
StartSeite | MarkusRechberger/ | Neues | TestSeite | ForumSeite | Teilnehmer | Kategorien | Index | Hilfe | Einstellungen | Ändern

GTK Programming

Eines meiner ersten Beispiele.. mittlerweile würde ich eher auf Glade zurückgreifen da die Entwicklung um einiges schneller vorangeht..

#include <gtk/gtk.h>

static void hello( GtkWidget *widget,
                gpointer data)
{
        g_print("Hello World\n");
}

static gboolean delete_event (GtkWidget *widget,
                GdkEvent *event,
                gpointer data)
{
        g_print("delete event occured\n");
        return TRUE;

}


static void destroy (GtkWidget *widget,
                gpointer data)
{
        gtk_main_quit();
}


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

        GtkWidget *window;
        GtkWidget *button;

        gtk_init(&argc, &argv);

        window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

        g_signal_connect(G_OBJECT(window),"delete_event",
                        G_CALLBACK(delete_event), NULL);

        g_signal_connect(G_OBJECT(window),"destroy",
                        G_CALLBACK(destroy), NULL);

        gtk_container_set_border_width(GTK_CONTAINER(window), 10);

        button = gtk_button_new_with_label("Hello World");

        g_signal_connect(G_OBJECT(button),"clicked",
                        G_CALLBACK(hello), NULL);

        g_signal_connect_swapped(G_OBJECT(button),"clicked",
                        G_CALLBACK(gtk_widget_destroy),
                        G_OBJECT(window));

        gtk_container_add (GTK_CONTAINER(window),button);

        gtk_widget_show(button);

        gtk_widget_show(window);

        gtk_main();

        return 0;
}

compilen mit:
gcc `gtk-config --cflags` -lgtk file.c (gegen gtk 1.2 linken)
oder
gcc `pkg-config --cflags --libs gtk+-2.0` (gegen gtk 2.0 linken)
oder
gcc `gtk-config --cflags --libs gtk+` (gegen gtk 1.2 linken)

Skinnable GTK1.2 Applikation

test.png ist in diesem Fall ein Transparentes PNG File erstellt mit Gimp

main.c
#include <gtk/gtk.h>
#include <stdio.h>
#include <gdk/gdk.h>
#include <gdk_imlib.h>

int main(int argc, char *argv[]){
        GtkWidget *mainwindow;
        GdkImlibImage *im;
        GdkPixmap *pixmap;
        gint nheight,nwidth;
        GdkBitmap *mask;
        GdkWindow *win;

        gtk_init(&argc,&argv);
        gdk_imlib_init();

        gtk_widget_push_visual(gdk_imlib_get_visual());
        gtk_widget_push_colormap(gdk_imlib_get_colormap());

        im=gdk_imlib_load_image("test.png");
        if(!im){
                fprintf(stderr,"Couldn't load image");
                throw;
        }
        nwidth=im->rgb_width;
        nheight=im->rgb_height;

        fprintf(stderr,"width: %d height: %d\n", nwidth,nheight);
        gdk_imlib_render(im,nwidth,nheight); //nwidth,nheight);
        mask = gdk_imlib_move_mask(im);
        pixmap = gdk_imlib_move_image(im);
        mainwindow = gtk_window_new(GTK_WINDOW_TOPLEVEL);
        gtk_widget_set_app_paintable(mainwindow,TRUE);
        gtk_window_set_title(GTK_WINDOW(mainwindow),"mainwindow");
        gtk_window_set_policy(GTK_WINDOW(mainwindow),FALSE,FALSE,FALSE);
        gtk_widget_realize(mainwindow);
        gdk_window_set_back_pixmap(mainwindow->window,pixmap,0);
        gdk_window_shape_combine_mask(mainwindow->window,mask,0,0);


        gdk_window_set_decorations(mainwindow->window,(GdkWMDecoration)0);
        gtk_widget_set_usize(mainwindow,nwidth,nheight); //500,500);
        gtk_widget_show(mainwindow);

        gtk_main();
        return 0;
}

Makefile
GCC=gcc
LIBS=`pkg-config --cflags --libs gtk+` `pkg-config --cflags --libs imlibgdk`
DEBUG=-g
all:
        $(GCC) main.c $(LIBS) $(DEBUG) -o main
clean:
        rm main


StartSeite | MarkusRechberger/ | Neues | TestSeite | ForumSeite | Teilnehmer | Kategorien | Index | Hilfe | Einstellungen | Ändern
Text dieser Seite ändern (zuletzt geändert: 14. Oktober 2004 4:06 (diff))
Suchbegriff: gesucht wird
im Titel
im Text