QQ登录

只需一步,快速开始

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 786|回复: 1

急问:GTK+2.0的文件读取.

[复制链接]
发表于 2003-12-11 21:13:27 | 显示全部楼层 |阅读模式
我在GTK+2.0下调用了一个自定义的控件,是一个简单的文件选择控件,根常规的xmms,gedit等打开文件时的界面一样.但是我却不知道如何将所选择的文件读取出来--也就是不知信号signal如何传递到主函数啊?因为在不同的文件定义,主函数中不能直接调用控件的变量吧?     
 楼主| 发表于 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]
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

GMT+8, 2024-11-9 06:07 , Processed in 0.057563 second(s), 15 queries .

© 2021 Powered by Discuz! X3.5.

快速回复 返回顶部 返回列表