143 lines
4.9 KiB
C
143 lines
4.9 KiB
C
#include <stdio.h>
|
||
#include <stdlib.h>
|
||
#include "list.h"
|
||
|
||
//列表滑动、选中控制/*{{{*/
|
||
static int press_y,previous_y,current_y; //滑动控制变量
|
||
gboolean deal_press_event(GtkWidget *widget,GdkEventButton *event,gpointer data) //按下
|
||
{
|
||
GdkColor color;
|
||
LIST *list=(LIST *)data;
|
||
if(GDK_2BUTTON_PRESS == event->type) //双击选中
|
||
{
|
||
gdk_color_parse(NORMAL_COLOR, &color);
|
||
gtk_widget_modify_fg(list->label[list->tab_select], GTK_STATE_NORMAL, &color);
|
||
list->tab_select=(int)(event->y-current_y)/(list->tab_height);
|
||
gdk_color_parse(HIGH_COLOR, &color);
|
||
gtk_widget_modify_fg(list->label[list->tab_select], GTK_STATE_NORMAL, &color);
|
||
list_select_back(list); //双击事件回调,由外部实现
|
||
}
|
||
press_y=event->y;
|
||
previous_y=current_y;
|
||
return TRUE;
|
||
}
|
||
|
||
gboolean deal_motion_event(GtkWidget *widget,GdkEventMotion *event,gpointer data) //拖动
|
||
{
|
||
LIST *list=(LIST *)data;
|
||
current_y=previous_y+event->y-press_y;
|
||
gtk_fixed_move(GTK_FIXED(list->fixed_bottom),list->fixed_top,0,current_y);
|
||
return TRUE;
|
||
}
|
||
|
||
gboolean deal_release_event(GtkWidget *widget,GdkEventButton *event,gpointer data) //释放
|
||
{
|
||
LIST *list=(LIST *)data;
|
||
if((list->tab_show*list->tab_height >= (list->total)*(list->tab_height)) || (current_y >=0))
|
||
current_y=0;
|
||
else if(list->tab_show*list->tab_height > current_y+(list->total)*(list->tab_height))
|
||
current_y=list->tab_show*list->tab_height-(list->total)*(list->tab_height);
|
||
else;
|
||
gtk_fixed_move(GTK_FIXED(list->fixed_bottom),list->fixed_top,0,current_y);
|
||
return TRUE;
|
||
}/*}}}*/
|
||
|
||
LIST *cgtk_list_new(char **name,int width,int height,int show,gpointer data) //创建列表/*{{{*/
|
||
{
|
||
int i;
|
||
GdkColor color;
|
||
LIST *list=(LIST *)calloc(1,sizeof(LIST)); //分配列表空间
|
||
list->label=(GtkWidget **)calloc(0,sizeof(GtkWidget *)); //分配列表行空间,初始为0
|
||
list->tab_show=show;
|
||
list->tab_width=width;
|
||
list->tab_height=height;
|
||
list->out=data;
|
||
list->event_box=gtk_event_box_new();
|
||
gtk_widget_set_size_request(list->fixed_bottom,width,show*height);
|
||
list->fixed_bottom=gtk_fixed_new();
|
||
gtk_widget_set_size_request(list->fixed_bottom,width,show*height);
|
||
list->fixed_top=gtk_fixed_new();
|
||
//gtk_widget_set_size_request(list->fixed_bottom,width,show*height);
|
||
gtk_container_add(GTK_CONTAINER(list->event_box),list->fixed_bottom);
|
||
gtk_fixed_put(GTK_FIXED(list->fixed_bottom),list->fixed_top,0,0);
|
||
|
||
g_signal_connect(GTK_OBJECT(list->event_box), "button_press_event", G_CALLBACK(deal_press_event),list);
|
||
g_signal_connect(GTK_OBJECT(list->event_box), "motion_notify_event", G_CALLBACK(deal_motion_event),list);
|
||
g_signal_connect(GTK_OBJECT(list->event_box), "button_release_event", G_CALLBACK(deal_release_event),list);
|
||
|
||
gdk_color_parse(NORMAL_COLOR, &color);
|
||
for(i=0;NULL != name[i];i++)
|
||
{
|
||
if(NULL == (list->label=realloc(list->label,(++(list->total))*sizeof(GtkWidget *))))
|
||
{
|
||
printf("\n>>>Can not get more memory space!!!<<<\n");
|
||
return NULL;
|
||
}
|
||
list->label[i]=gtk_label_new(name[i]);
|
||
gtk_widget_modify_fg(list->label[i], GTK_STATE_NORMAL, &color);
|
||
gtk_widget_set_size_request(list->label[i],width,height);
|
||
gtk_fixed_put(GTK_FIXED(list->fixed_top),list->label[i],0,height*i);
|
||
}
|
||
return list;
|
||
}/*}}}*/
|
||
|
||
int cgtk_list_refresh(LIST *list,char **name) //刷新列表/*{{{*/
|
||
{
|
||
int i;
|
||
GdkColor color;
|
||
for(i=0;i<list->total;i++)
|
||
gtk_widget_destroy(list->label[i]);
|
||
list->total=0;
|
||
free(list->label);
|
||
list->label=(GtkWidget **)calloc(0,sizeof(GtkWidget *)); //分配列表行空间,初始为0
|
||
gdk_color_parse(NORMAL_COLOR, &color);
|
||
for(i=0;NULL != name[i];i++)
|
||
{
|
||
if(NULL == (list->label=realloc(list->label,(++(list->total))*sizeof(GtkWidget *))))
|
||
{
|
||
printf("\n>>>Can not get more memory space!!!<<<\n");
|
||
return -2;
|
||
}
|
||
list->label[i]=gtk_label_new(name[i]);
|
||
gtk_widget_modify_fg(list->label[i], GTK_STATE_NORMAL, &color);
|
||
gtk_widget_set_size_request(list->label[i],list->tab_width,list->tab_height);
|
||
gtk_fixed_put(GTK_FIXED(list->fixed_top),list->label[i],0,list->tab_height*i);
|
||
gtk_widget_show(list->label[i]);
|
||
}
|
||
return 0;
|
||
}/*}}}*/
|
||
|
||
int cgtk_list_set_select_num(LIST *list,int row_num) //修改选中行行号/*{{{*/
|
||
{
|
||
if(0>row_num || list->total<=row_num)
|
||
{
|
||
printf("\n>>>Set Wrong row_num !!! <<<\n");
|
||
return -1;
|
||
}
|
||
GdkColor color;
|
||
gdk_color_parse(NORMAL_COLOR, &color);
|
||
gtk_widget_modify_fg(list->label[list->tab_select], GTK_STATE_NORMAL, &color);
|
||
list->tab_select=row_num;
|
||
gdk_color_parse(HIGH_COLOR, &color);
|
||
gtk_widget_modify_fg(list->label[list->tab_select], GTK_STATE_NORMAL, &color);
|
||
return 0;
|
||
}/*}}}*/
|
||
|
||
int cgtk_list_get_select_num(LIST *list) //获取选中行行号/*{{{*/
|
||
{
|
||
return list->tab_select;
|
||
}/*}}}*/
|
||
|
||
char *cgtk_list_get_select_text(LIST *list) //获取选中行文本/*{{{*/
|
||
{
|
||
char *text=(char *)gtk_label_get_text(GTK_LABEL(list->label[list->tab_select]));
|
||
return text;
|
||
}/*}}}*/
|
||
|
||
void cgtk_list_destroy(LIST *list) //释放列表占用的空间/*{{{*/
|
||
{
|
||
free(list->label);
|
||
free(list);
|
||
}/*}}}*/
|
||
|