|
楼主 |
发表于 2003-12-11 21:17:20
|
显示全部楼层
源代码如下:
simple.h
[code:1]
#ifndef __SIMPLE_H__
#define __SIMPLE_H__
#include <gdk/gdk.h>
#include <gtk/gtkvbox.h>
#ifdef __cplusplus
extern "C"{
#endif
#define SIMPLE(obj) GTK_CHECK_CAST(obj,simple_get_type(),Simple)
#define SIMPLE_CLASS(klass) GTK_CHECK_CLASS_CAST(klass,simple_get_type(),SimpleClass)
#define IS_SIMPLE(obj) GTK_CHECK_TYPE(obj,simple_get_type())
typedef struct _Simple Simple;
typedef struct _SimpleClass SimpleClass;
struct _Simple
{
GtkHBox hbox;
GtkWidget *button;
GtkWidget *entry;
};
struct _SimpleClass
{
GtkHBoxClass parent_class;
void(* simple)(Simple *sss);
};
GtkType simple_get_type (void);
GtkWidget* simple_new (void);
#ifdef __cplusplus
}
#endif
#endif
[/code:1]
simple.c
[code:1]
#include<gtk/gtk.h>
#include"simple.h"
enum{
LAST_SIGNAL
};
static GtkWidget *dialog=NULL;
static void savename (GtkButton *button,Simple *sss);
static void simple_class_init (SimpleClass *klass);
static void simple_init (Simple *sss);
static void simple_clicked (GtkWidget *widget,Simple *sss);
GType simple_get_type(void)
{
static GType sss_type=0;
if (!sss_type)
{
static const GTypeInfo sss_info=
{
sizeof(SimpleClass),
NULL,NULL,
(GClassInitFunc)simple_class_init,
NULL,NULL,sizeof(Simple),
0,(GInstanceInitFunc)simple_init,
};
sss_type=g_type_register_static(GTK_TYPE_HBOX,"Simple",&sss_info,0);
}
return sss_type;
}
static void simple_class_init(SimpleClass *class)
{
GtkObjectClass *object_class;
object_class=(GtkObjectClass*) class;
class->simple=NULL;
}
static void simple_init(Simple *sss)
{
sss->entry=gtk_entry_new();
gtk_box_pack_start(GTK_BOX(sss),sss->entry,TRUE,TRUE,1);
sss->button=gtk_button_new_with_label("...");
gtk_box_pack_start(GTK_BOX(sss),sss->button,FALSE,FALSE,0);
g_signal_connect(GTK_OBJECT(sss->button),"clicked",G_CALLBACK(simple_clicked),sss);
gtk_widget_show_all(sss);
}
GtkWidget* simple_new(void)
{
return GTK_WIDGET (g_object_new (simple_get_type(),NULL));
}
static void simple_clicked(GtkWidget *widget,Simple *sss)
{
dialog=gtk_file_selection_new("请选择一个文件:");
g_signal_connect(G_OBJECT(GTK_FILE_SELECTION(dialog)->ok_button),"clicked",G_CALLBACK(savename),sss);
g_signal_connect_swapped(G_OBJECT(GTK_FILE_SELECTION(dialog)->ok_button),"clicked",G_CALLBACK(gtk_widget_destroy),(gpointer)dialog);
//g_signal_connect(G_OBJECT(GTK_FILE_SELECTION(dialog)->cancel_button),"clicked",G_CALLBACK(gtk_widget_destroy),(gpointer)dialog);
g_signal_connect_swapped(G_OBJECT(GTK_FILE_SELECTION(dialog)->cancel_button),"clicked",G_CALLBACK(gtk_widget_destroy),(gpointer)dialog);
gtk_widget_show(dialog);
}
static void savename(GtkButton *button,Simple *sss)
{
gtk_entry_set_text(GTK_ENTRY(sss->entry),
gtk_file_selection_get_filename(GTK_FILE_SELECTION(dialog)))
;
}
[/code:1]
主函数
[code:1]
#include <gtk/gtk.h>
#include "simple.h"
static gchar *JIA=" 加密 ";
static gchar *JIE=" 解密 ";
void on_button_clicked(GtkWidget* button,gint data)
{
g_print("你选择的是:");
switch((int)data)
{
case 1:g_print("%s",JIA);break;
case 2:g_print("%s",JIE);break;
}
g_print("\n");
}
int main (int argc,char *argv[])
{
GtkWidget *window;
GtkWidget *sss;
GtkWidget *box;
GtkWidget *frame1;
GtkWidget *frame2;
GtkWidget *frame3;
GtkWidget *box1;
GtkWidget *button1;
GtkWidget *button2;
// GSList *group;
gtk_init(&argc,&argv);
window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window),"简单控件测试程序");
gtk_window_set_default_size(GTK_WINDOW(window),300,150);
g_signal_connect(G_OBJECT(window),"destroy",G_CALLBACK(gtk_main_quit),NULL);
gtk_container_set_border_width(GTK_CONTAINER(window),20);
box=gtk_vbox_new(FALSE,0);
gtk_container_add(GTK_CONTAINER(window),box);
frame1=gtk_frame_new("源文件为:");
gtk_container_add(GTK_CONTAINER(box),frame1);
sss=simple_new();
gtk_container_add(GTK_CONTAINER(frame1),sss);
// g_signal_connect(G_OBJECT(savename),"clicked",G_CALLBACK(on_1_clicked),(gpointer)1);
gtk_entry_set_text(GTK_ENTRY(sss),gtk_file_selection_get_filename(GTK_FILE_SELECTION(sss)));
gtk_widget_show(sss);
frame2=gtk_frame_new("目标文件为:");
gtk_container_add(GTK_CONTAINER(box),frame2);
sss=simple_new();
gtk_container_add(GTK_CONTAINER(frame2),sss);
// g_signal_connect(G_OBJECT(savename),"clicked",G_CALLBACK(on_1_clicked),(gpointer)1);
// gtk_entry_set_text(GTK_ENTRY(sss->entry),gtk_file_selection_get_filename(GTK_FILE_SELECTION(dialog)));
gtk_widget_show(sss);
frame3=gtk_frame_new("选择:");
gtk_container_add(GTK_CONTAINER(box),frame3);
// gtk_frame_set_shadow_type(GTK_FRAME(frame3),GTK_SHADOW_ETCHED_OUT);
gtk_box_pack_start(GTK_BOX(box),frame3,FALSE,FALSE,5);
box1=gtk_hbox_new(FALSE,115);
gtk_container_set_border_width(GTK_CONTAINER(box1),10);
gtk_container_add(GTK_CONTAINER(frame3),box1);
button1=gtk_button_new_with_label(JIA);
g_signal_connect(G_OBJECT(button1),"clicked",G_CALLBACK(on_button_clicked),(gpointer)1);
gtk_box_pack_start(GTK_BOX(box1),button1,FALSE,FALSE,5);
// group=gtk_radio_button_get_group(GTK_RADIO_BUTTON(radio1));
button2=gtk_button_new_with_label(JIE);
g_signal_connect(G_OBJECT(button2),"clicked",G_CALLBACK(on_button_clicked),(gpointer)2);
gtk_box_pack_start(GTK_BOX(box1),button2,FALSE,FALSE,5);
// gtk_widget_show(frame1);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
[/code:1] |
|