first commit
This commit is contained in:
BIN
gtk透明列表/exe
Executable file
BIN
gtk透明列表/exe
Executable file
Binary file not shown.
142
gtk透明列表/list.c
Normal file
142
gtk透明列表/list.c
Normal file
@@ -0,0 +1,142 @@
|
||||
#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);
|
||||
}/*}}}*/
|
||||
|
30
gtk透明列表/list.h
Normal file
30
gtk透明列表/list.h
Normal file
@@ -0,0 +1,30 @@
|
||||
#ifndef __COLBEN_LIST__
|
||||
#define __COLBEN_LIST__
|
||||
|
||||
#include <gtk/gtk.h>
|
||||
#define NORMAL_COLOR "black" //非选中颜色
|
||||
#define HIGH_COLOR "white" //选中颜色
|
||||
|
||||
typedef struct //list句柄
|
||||
{
|
||||
gint total; //标签总数量
|
||||
gint tab_show; //标签显示数量
|
||||
gint tab_width; //标签宽度
|
||||
gint tab_height; //标签高度
|
||||
gint tab_select; //当前选中标签
|
||||
gpointer out; //双击事件回调参数
|
||||
GtkWidget *event_box; //底层事件盒子,需手动添加到自定义布局中
|
||||
GtkWidget *fixed_bottom; //中层固定布局
|
||||
GtkWidget *fixed_top; //上层固定布局
|
||||
GtkWidget **label; //每一行的信息
|
||||
}LIST;
|
||||
|
||||
extern LIST *cgtk_list_new(char **,int,int,int,gpointer); //创建列表,需指定指针数组,列表宽度,单行高度,双击回调参数指针
|
||||
extern int cgtk_list_refresh(LIST *,char **); //刷新列表,需指定列表和新指针数组,双击回调参数指针
|
||||
extern int cgtk_list_set_select_num(LIST *,int); //设置选中行号
|
||||
extern int cgtk_list_get_select_num(LIST *); //获取选中行号
|
||||
extern char *cgtk_list_get_select_text(LIST *); //获取选中行文本
|
||||
extern void cgtk_list_destroy(LIST *); //释放列表内存空间
|
||||
extern void list_select_back(gpointer); //双击事件回调,需外部实现
|
||||
|
||||
#endif
|
46
gtk透明列表/main.c
Normal file
46
gtk透明列表/main.c
Normal file
@@ -0,0 +1,46 @@
|
||||
#include <gtk/gtk.h>
|
||||
#include <glade/glade.h>
|
||||
#include "list.h"
|
||||
|
||||
char *name1[]={"Colben","Shelly","Saferin","Ada","Obo","Kathy",NULL}; //之前
|
||||
char *name2[]={"Colben","Shelly","Bather","Ada","Obo","Kathy","Jathy","Rassol","Colben","Shelly","Bather","Rassol","Colben","Shelly","Bather","Ada","Obo","Kathy",NULL}; //之后
|
||||
|
||||
void list_select_back(gpointer data) //双击选中回调
|
||||
{
|
||||
LIST *list=(LIST *)data;
|
||||
gtk_label_set_text(GTK_LABEL(list->out),cgtk_list_get_select_text(list));
|
||||
printf("\n>>>>>>----------%d----------<<<<<<\n",cgtk_list_get_select_num(list));
|
||||
printf("\n>>>>>>----------%s----------<<<<<<\n",cgtk_list_get_select_text(list));
|
||||
}
|
||||
|
||||
void button_refresh_list(GtkWidget *widget,gpointer data) //按钮单击回调
|
||||
{
|
||||
LIST *list=(LIST *)data;
|
||||
cgtk_list_refresh(list,name2);
|
||||
}
|
||||
|
||||
int main(int argc,char *argv[])
|
||||
{
|
||||
gtk_init(&argc,&argv);
|
||||
GladeXML *gxml=glade_xml_new("test.glade",NULL,NULL);
|
||||
GtkWidget *main_window=glade_xml_get_widget(gxml,"window1");
|
||||
g_signal_connect(main_window,"destroy",G_CALLBACK(gtk_main_quit),NULL);
|
||||
|
||||
GtkWidget *fixed=glade_xml_get_widget(gxml,"fixed1");
|
||||
GtkWidget *label=glade_xml_get_widget(gxml,"label1");
|
||||
|
||||
LIST *list=cgtk_list_new(name1,240,40,8,label); //创建列表,指针即可,无需分配空间
|
||||
if(NULL == list)
|
||||
{
|
||||
printf("\n>>>>>> !!! Memory not enough !!! <<<<<<\n");
|
||||
return -1;
|
||||
}
|
||||
gtk_fixed_put(GTK_FIXED(fixed),list->event_box,500,80); //把列表添加到主窗口的固定布局中
|
||||
|
||||
GtkWidget *button=glade_xml_get_widget(gxml,"button1");
|
||||
g_signal_connect(button,"clicked",G_CALLBACK(button_refresh_list),list); //按钮信号注册
|
||||
|
||||
gtk_widget_show_all(main_window);
|
||||
gtk_main();
|
||||
return 0;
|
||||
}
|
39
gtk透明列表/makefile
Normal file
39
gtk透明列表/makefile
Normal file
@@ -0,0 +1,39 @@
|
||||
#==============================================
|
||||
# 文件名:makefile
|
||||
# 作者: 巴拉克-侯赛因-奥巴马
|
||||
# 创建时间:2014-05-23 00:21:34
|
||||
# 地址: 美国华盛顿市区中心宾西法尼亚大街1600号
|
||||
#==============================================
|
||||
|
||||
cc=gcc
|
||||
|
||||
target=exe
|
||||
|
||||
object_h=list.h
|
||||
|
||||
object_o=main.o list.o
|
||||
|
||||
flags=`pkg-config --cflags --libs gtk+-2.0 libglade-2.0`
|
||||
|
||||
$(target): $(object_o)
|
||||
@$(cc) -o $@ $^ $(flags)
|
||||
@echo "成功生成可执行文件: $@ !!"
|
||||
@echo '^_^ ^_^ ^_^'
|
||||
|
||||
%.o: %.c $(object_h)
|
||||
@$(cc) -o $@ -c $< $(flags)
|
||||
@echo '^_^'
|
||||
@echo "成功编译 $< 生成 $@ 文件!!"
|
||||
|
||||
clean:
|
||||
@rm -rf $(object_o)
|
||||
@echo '^_^'
|
||||
@echo "成功删除所有目标文件!!"
|
||||
@echo '^_^'
|
||||
|
||||
clear:
|
||||
@rm -rf $(object_o) $(target)
|
||||
@echo '^_^'
|
||||
@echo "成功删除所有非源码文件!!"
|
||||
@echo '^_^'
|
||||
|
46
gtk透明列表/test.glade
Normal file
46
gtk透明列表/test.glade
Normal file
@@ -0,0 +1,46 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<glade-interface>
|
||||
<!-- interface-requires gtk+ 2.24 -->
|
||||
<!-- interface-naming-policy project-wide -->
|
||||
<widget class="GtkWindow" id="window1">
|
||||
<property name="width_request">800</property>
|
||||
<property name="height_request">480</property>
|
||||
<property name="can_focus">False</property>
|
||||
<property name="resizable">False</property>
|
||||
<property name="window_position">center-always</property>
|
||||
<child>
|
||||
<widget class="GtkFixed" id="fixed1">
|
||||
<property name="visible">True</property>
|
||||
<property name="can_focus">False</property>
|
||||
<child>
|
||||
<widget class="GtkButton" id="button1">
|
||||
<property name="label" translatable="yes">Refresh</property>
|
||||
<property name="width_request">100</property>
|
||||
<property name="height_request">80</property>
|
||||
<property name="visible">True</property>
|
||||
<property name="can_focus">True</property>
|
||||
<property name="receives_default">True</property>
|
||||
<property name="use_action_appearance">False</property>
|
||||
</widget>
|
||||
<packing>
|
||||
<property name="x">76</property>
|
||||
<property name="y">295</property>
|
||||
</packing>
|
||||
</child>
|
||||
<child>
|
||||
<widget class="GtkLabel" id="label1">
|
||||
<property name="width_request">100</property>
|
||||
<property name="height_request">80</property>
|
||||
<property name="visible">True</property>
|
||||
<property name="can_focus">False</property>
|
||||
<property name="label" translatable="yes">label</property>
|
||||
</widget>
|
||||
<packing>
|
||||
<property name="x">87</property>
|
||||
<property name="y">74</property>
|
||||
</packing>
|
||||
</child>
|
||||
</widget>
|
||||
</child>
|
||||
</widget>
|
||||
</glade-interface>
|
Reference in New Issue
Block a user