1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
|
#include <stdlib.h>
#include <gtk/gtk.h>
#define WIDTH 500
#define HEIGHT 300
GdkPixmap *pPixmap = NULL;
typedef struct APPLICATION
{
GtkWidget *pWin;
GtkWidget *pDrawArea;
GtkWidget *pTable;
GtkWidget *pQuitBtn;
GtkWidget *pPaddBtn;
}APPLICATION;
void draw_cb(GtkWidget *drawArea, GdkEventExpose *event, gpointer userData)
{
static int wScrn, hScrn = 0;
if (pPixmap == NULL || wScrn != drawArea->allocation.width || hScrn != drawArea->allocation.height)
{
wScrn = drawArea->allocation.width;
hScrn = drawArea->allocation.height;
pPixmap = gdk_pixmap_new(drawArea->window, wScrn, hScrn, -1);//
gdk_draw_rectangle(pPixmap, drawArea->style->white_gc, TRUE, 0, 0, wScrn, hScrn);
}
gdk_draw_drawable(drawArea->window, drawArea->style->fg_gc[GTK_WIDGET_STATE (drawArea)],
pPixmap, event->area.x, event->area.y,
event->area.x, event->area.y,
event->area.width, event->area.height);
}
int main (int argc, char *argv[])
{
APPLICATION myApp;
gtk_init(&argc, &argv);
myApp.pWin = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_default_size(GTK_WINDOW(myApp.pWin), WIDTH, HEIGHT);
gtk_window_set_title(GTK_WINDOW(myApp.pWin), "GTK-Win");
g_signal_connect(G_OBJECT(myApp.pWin), "destroy", G_CALLBACK(gtk_main_quit), NULL);
myApp.pDrawArea = gtk_drawing_area_new ();
g_signal_connect(G_OBJECT(myApp.pDrawArea),"expose_event",G_CALLBACK(draw_cb), &myApp);
myApp.pTable = gtk_table_new(2, 2, FALSE);
gtk_container_add (GTK_CONTAINER(myApp.pWin), myApp.pTable);
//création des boutons
myApp.pQuitBtn = gtk_button_new_with_label("Quitter");
myApp.pPaddBtn = gtk_button_new();
//connexion des bouton à une fonction de callback
g_signal_connect(G_OBJECT(myApp.pQuitBtn),"clicked",G_CALLBACK(gtk_main_quit), NULL);
//définit la taille des boutons
gtk_widget_set_size_request(myApp.pQuitBtn, 100, 30);
gtk_widget_set_size_request(myApp.pPaddBtn, 1, 1);
//attache les widgets sur la table
gtk_table_attach(GTK_TABLE(myApp.pTable), myApp.pQuitBtn, 0, 1, 0, 1, GTK_FILL, GTK_SHRINK, 5, 5);
gtk_table_attach(GTK_TABLE(myApp.pTable), myApp.pPaddBtn, 0, 1, 1, 2, GTK_FILL, GTK_EXPAND, 0, 0);
gtk_table_attach_defaults(GTK_TABLE(myApp.pTable), myApp.pDrawArea, 1, 2, 0, 2);
gtk_widget_set_child_visible(myApp.pPaddBtn, FALSE);
gtk_widget_show_all(myApp.pWin);
gtk_main ();
return 0;
} |
Partager