Chinaunix首页 | 论坛 | 博客
  • 博客访问: 216332
  • 博文数量: 82
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 18
  • 用 户 组: 普通用户
  • 注册时间: 2012-12-28 13:26
个人简介

当做事的时候,也是在学习的时候!

文章分类

全部博文(82)

文章存档

2016年(9)

2015年(55)

2014年(7)

2013年(10)

2012年(1)

我的朋友

分类: 系统运维

2015-05-20 16:24:41

原文地址:GTK中的信号机制 作者:typhoon85

一、            为了方便用户使用,定义了如下的宏

/* --- 便于使用的宏定义*/

#define g_signal_connect(instance, detailed_signal, c_handler, data) \

    g_signal_connect_data ((instance), (detailed_signal), (c_handler), (data), NULL, (GConnectFlags) 0)

#define g_signal_connect_after(instance, detailed_signal, c_handler, data) \

    g_signal_connect_data ((instance), (detailed_signal), (c_handler), (data), NULL, G_CONNECT_AFTER)

#define g_signal_connect_swapped(instance, detailed_signal, c_handler, data) \

    g_signal_connect_data ((instance), (detailed_signal), (c_handler), (data), NULL, G_CONNECT_SWAPPED)

#define          g_signal_handlers_disconnect_by_func(instance, func, data)                                                                 \

    g_signal_handlers_disconnect_matched ((instance),                                                                                     \

                                                            (GSignalMatchType) (G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA), \

                                                            0, 0, NULL, (func), (data))

#define          g_signal_handlers_block_by_func(instance, func, data)                                                                           \

    g_signal_handlers_block_matched      ((instance),                                                                                    \

                                                         (GSignalMatchType) (G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA),        \

                                                         0, 0, NULL, (func), (data))

#define          g_signal_handlers_unblock_by_func(instance, func, data)                                                                       \

    g_signal_handlers_unblock_matched    ((instance),                                                                                    \

                                                         (GSignalMatchType) (G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA),        \

                                                         0, 0, NULL, (func), (data))

二、 /* 信号连接实现函数*/

gulong

g_signal_connect_data (gpointer       instance,//发送信号的对象

                     const gchar   *detailed_signal,//信号名称

                     GCallback      c_handler,//回调函数

                     gpointer       data,//传递给回调函数的参数

                     GClosureNotify destroy_data,//销毁数据的函数

                     GConnectFlags  connect_flags)//信号连接的选项

{

       1/****************为了安全起见,做如下检查******************/

     //检查发送信号的对象是否存在

     g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);

     //检查发送的信号是否存在

     g_return_val_if_fail (detailed_signal != NULL, 0);

     //检查回调函数是否存在

     g_return_val_if_fail (c_handler != NULL, 0);

       2//根据信号名称获取信号id

     signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);

       3//根据信号id 获取信号节点

      SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);

       4//根据信号节点判断信号的有效性

      if (detail && !(node->flags & G_SIGNAL_DETAILED))

       g_warning ("%s: signal `%s' does not support details", G_STRLOC, detailed_signal);

      else if (!g_type_is_a (itype, node->itype))

       g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);

      else

       {

         5//生成信号处理器

         Handler *handler = handler_new (after);

 

         //信号处理器 序号

         handler_seq_no = handler->sequential_number;

         handler->detail = detail;

         //信号处理函数

         handler->closure = g_closure_ref ((swapped ? g_cclosure_new_swap : g_cclosure_new) (c_handler, data, destroy_data));

         g_closure_sink (handler->closure);

         6//根据信号id,发出信号的对象,信号处理器插入到信号处理器列表中

         handler_insert (signal_id, instance, handler);

}

三、            信号的触发(信号处理器的执行)

       gtk_main()进入事件循环,生成GMainLoop对象,

       GMainLoop管理GMainContext对象,而GMainContext则管理则所有的事件源!

       当相应的事件发生时候,查找信号处理器列表,然后执行。

阅读(1506) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~