linux gtk

//mark

/*
 * File:   newmain.cpp
 * Author: cogent
 *
 * Created on March 20, 2009, 10:50 AM
 */

#include <stdlib.h>

#include <gtk/gtk.h>
#include <gdk/gdkevents.h>
#include <gtk/gtkobject.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 occurred/n");

    return TRUE;
}

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

///
gboolean drag = FALSE;
int nX = 0;
int nY = 0;
static gint button_press_event(GtkWidget* widget, GdkEventButton * event,
        gpointer data)
{
    if(event->button == 1) // left button'
    {
        drag = TRUE;
        nX = event->x;
        nY = event->y;
    }
    return TRUE;
}

static gint button_release_event(GtkWidget * widget, GdkEventButton *event,
        gpointer data)
{
    if(event->button == 1)
    {
        drag = FALSE;
    }
    return TRUE;
}

static gint motion_notify_event(GtkWidget * widget, GdkEventButton *event,
        gpointer data)
{
    if(drag)
    {
        int x, y;
        GtkWidget *window = (GtkWidget*)data;
        gtk_window_get_position((GtkWindow*)window, &x, &y);
        gtk_window_move((GtkWindow*)window, x + event->x - nX,
                y + event->y - nY);
    }
    return TRUE;
}
///
/*
 *
 */
#include <wchar.h>

cairo_surface_t *image = NULL;

static gboolean on_window_expose_event(GtkWidget *widget, GdkEventExpose *event,
        gpointer data)
{
    cairo_t *cr;
    cr = gdk_cairo_create(widget->window);
    cairo_set_source_surface(cr, image, 0, 0);
    cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
    cairo_paint(cr);
    cairo_destroy(cr);
    return FALSE;
}

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), 100);
/*
    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_window_set_decorated(GTK_WINDOW(window), FALSE); // remove border
    //gtk_widget_set_size_request(window, 100, 80);
    gtk_widget_set_events(window, GDK_EXPOSURE_MASK|GDK_LEAVE_NOTIFY_MASK
            |GDK_BUTTON_PRESS_MASK|GDK_BUTTON_RELEASE_MASK
            |GDK_POINTER_MOTION_MASK|GDK_POINTER_MOTION_HINT_MASK);
    gtk_signal_connect(GTK_OBJECT(window), "button_press_event",
            (GtkSignalFunc)button_press_event, window);
    gtk_signal_connect(GTK_OBJECT(window), "motion_notify_event",
            (GtkSignalFunc)motion_notify_event, window);
    gtk_signal_connect(GTK_OBJECT(window), "button_release_event",
            (GtkSignalFunc)button_release_event, window);

    //
    gtk_window_set_title(GTK_WINDOW(window), "Horz Box Container");
    GtkWidget *hbox = gtk_hbox_new(FALSE, 0);

    button = gtk_button_new_with_label("button 1");
    gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 3);

    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_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 3);

    button = gtk_button_new_with_label("button 3");
    gtk_box_pack_start(GTK_BOX(hbox), button , TRUE, TRUE, 3);

    button = gtk_button_new_with_label("button 4");
    gtk_box_pack_start(GTK_BOX(hbox), button , FALSE, FALSE, 3);

    //
    GtkWidget *fixed = gtk_fixed_new();
    gtk_widget_set_usize(fixed, 150, 150);
    button = gtk_button_new_with_label("fix b 1");
    gtk_fixed_put(GTK_FIXED(fixed), button, 5, 5);
    button = gtk_button_new_with_label("fix b 2");
    gtk_fixed_put(GTK_FIXED(fixed), button , 15, 15);

    gtk_box_pack_start(GTK_BOX(hbox), fixed , FALSE, FALSE, 3);
    //
    gtk_container_set_border_width(GTK_CONTAINER(window), 20);
    gtk_widget_set_size_request(window, 500, 300);
   
    GtkWidget *vbox = gtk_vbox_new(FALSE, 5);
   
    gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 3);
    gtk_container_add(GTK_CONTAINER(window), vbox);

    GtkWidget *valign = gtk_alignment_new(0, 1, 0, 0);
    gtk_container_add(GTK_CONTAINER(vbox), valign);

    GtkWidget *halign = gtk_alignment_new(1, 0, 0, 0);
    gtk_box_pack_start(GTK_BOX(vbox), halign, FALSE, FALSE, 0);

    GtkWidget *hbox2 = gtk_hbox_new(TRUE, 3);
    button = gtk_button_new_with_label("OK");
    gtk_widget_set_size_request(button, 70, 30);
    gtk_container_add(GTK_CONTAINER(hbox2), button);
    button = gtk_button_new_with_label("Close");
    gtk_container_add(GTK_CONTAINER(hbox2), button);

    gtk_container_add(GTK_CONTAINER(halign), hbox2);
    //
    //gtk_widget_show_all(window);
   
    //gtk_widget_show(button);

    //gtk_widget_show(window);

    //
#if 0
    GdkPixbuf *pixbuf = NULL;
    GdkBitmap *bitmap = NULL;
    GdkPixmap *pixmap = NULL;
    gtk_widget_set_app_paintable(window, TRUE);
    gtk_widget_realize(window);

    pixbuf = gdk_pixbuf_new_from_file("/home/cogent/bg.png", NULL);
    gdk_pixbuf_render_pixmap_and_mask(pixbuf, &pixmap, &bitmap, 128); // Trans < 128
    gtk_widget_shape_combine_mask(window, bitmap, 0, 0); // set trans mask
    gdk_window_set_back_pixmap(window->window, pixmap, FALSE);

    g_object_unref(pixbuf);
    g_object_unref(bitmap);
    g_object_unref(pixmap);

    // trans
    gtk_window_set_opacity(GTK_WINDOW(window), 0.8);
#endif

    GdkScreen *screen;
    GdkColormap *colormap;

    image = cairo_image_surface_create_from_png("/home/cogent/bg.png");
    gtk_widget_set_app_paintable(window, TRUE);
    //gtk_widget_realize(window);
    //gtk_window_resize(GTK_WINDOW(window), 250, 250);
    g_signal_connect(G_OBJECT(window), "expose-event",
            G_CALLBACK(on_window_expose_event), NULL);

    screen = gtk_widget_get_screen(window);
    colormap = gdk_screen_get_rgba_colormap(screen);
    gtk_widget_set_colormap(window, colormap);
    gtk_widget_show_all(window);
   
    gtk_main();

   
    return (EXIT_SUCCESS);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值