关闭

用C实现C++的多态---剖析GTK的"对象" (四)

标签: gtkc++cobjectstructnull
880人阅读 评论(0) 收藏 举报
分类:

七. 写自己的子对象

/**************************************************************

 子对象RenderObject,继承于CObject-------render_object.h

***************************************************************/

#ifndef _H_RENDER_OBJECT_H_
#define _H_RENDER_OBJECT_H_


#define RENDER_OBJECT_TYPE   GetRenderObjectType()
#define IS_RENDER_OBJECT_TYPE(obj) IsObjectType(obj,RENDER_OBJECT_TYPE)
#define RENDER_OBJECT_CLASS(obj) ((RenderObjectClass*)GetObjectClass(obj))

typedef struct _RenderObject  RenderObject;
typedef struct _RenderObjectClass RenderObjectClass;


#ifdef __cplusplus
extern "C" {
#endif


enum
{
 RENDER_BOX  = 1,
 RENDER_TEXT 
};

struct _RenderObject
{
 CObject   base; 
 RenderObject* parent;
 RenderObject* children;
 RenderObject* prev;
 RenderObject* next;
 unsigned short x;
 unsigned short y;
 unsigned short width;
 unsigned short height;
 unsigned short min_width;
 unsigned short max_width;
 unsigned char  render_type;
 unsigned char  have_calc_width:1;
 unsigned char  have_calc_height:1;
 unsigned char  have_layout:1;
 unsigned char  reserved:5; 
};

struct _RenderObjectClass
{
 CObjectClass base;

 void (*draw)(void*);
 int (*calc_width)(void*);
 int (*calc_height)(void*);
 int (*calc_WxH)(void*);
 int (*layout)(void*);
};


int
GetRenderObjectType(void);

RenderObject *
NewRenderObject(void);

void
renderDraw(RenderObject *render);

int
renderCalcWidth(RenderObject *render);

int
renderCalcHeight(RenderObject *render);

int
renderCalcWxH(RenderObject *render);

int
renderLayout(RenderObject *render);

#ifdef __cplusplus
}
#endif
#endif

 

 

/**************************************************************

 子对象RenderObject,继承于CObject-------render_object.c

***************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "render_object.h"


static void
InitRenderObjectClass(RenderObjectClass *vclass)
{
 if(vclass==NULL)
  return;

 vclass->draw = renderDraw;
 vclass->calc_width = renderCalcWidth;
 vclass->calc_height = renderCalcHeight;
 vclass->calc_WxH = renderCalcWxH;
 vclass->layout = renderLayout;
}

static void
InitRenderObject(RenderObject *object)
{
 if(object==NULL)
  return;

 object->x = 0;
 object->y = 0;
 object->width = 0;
 object->height = 0;
 object->min_width = 0;
 object->max_width = 0;
 object->parent = NULL;
 object->children = NULL;
 object->prev = NULL;
 object->next = NULL;
 object->render_type = RENDER_BOX;
 object->have_calc_width = 0;
 object->have_calc_height = 0;
 object->have_layout = 0;
}

int
GetRenderObjectType(void)
{
 static int type = 0;

 if( type==0 )
 {
  static ClassInfo classinfo =
  {
   "RENDEROBJECT",
   sizeof(RenderObject),
   sizeof(RenderObjectClass),
   (pInitObjectCallback)InitRenderObject,
   (pInitClassCallback)InitRenderObjectClass,
  };

  type = RegisterClassType(&classinfo, BASE_OBJECT_TYPE);
 }

 return type;
}


RenderObject*
NewRenderObject(void)
{
 return (RenderObject*)NewClassType(RENDER_OBJECT_TYPE);
}

void
renderDraw(RenderObject *render)
{
}

int
renderCalcWidth(RenderObject *render)
{
 return 0;
}

int
renderCalcHeight(RenderObject *render)
{
 return 0;
}

int
renderCalcWxH(RenderObject *render)
{
 return 0;
}

int
renderLayout(RenderObject *render)
{
 return 0;
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:43399次
    • 积分:626
    • 等级:
    • 排名:千里之外
    • 原创:17篇
    • 转载:2篇
    • 译文:0篇
    • 评论:6条
    最新评论