⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vcp_widgets.c

📁 Source code for an Numeric Cmputer
💻 C
📖 第 1 页 / 共 2 页
字号:
    { "expand", "0", ATTRIB_BOOL, offsetof(label_data_t, expand) },    { "padding", "0", ATTRIB_INT, offsetof(label_data_t, padding) },    { "text", NULL, ATTRIB_STRING, offsetof(label_data_t, text) },    { NULL, NULL, 0, 0 }};vcp_widget_def_t label_def = {    "label",    CL_LABEL,    CH_NONE,    label_attribs,    sizeof(label_data_t),    init_label};static int init_label ( vcp_widget_t *wp ){    label_data_t *pd;    GtkWidget *gwp;    printf ( "init label\n" );    pd = (label_data_t *)(wp->priv_data);    /* create a label */    gwp = gtk_label_new(pd->text);    wp->gtk_widget = gwp;    wp->gtk_type = NONE;    /* add the label to the parent widget */    add_to_parent(wp->parent, gwp, pd->expand, pd->padding);    gtk_widget_show(gwp);printf ( "  done\n" );    return 0;}/** BUTTON: The button widget sets a bit HAL pin TRUE while it is     pressed, and FALSE when it is released.  The button is not     labeled, but it can accept any child widget (or widgets, if    they are in a box).  Normally, a label widget would be    specified as a child.*/static int init_button(vcp_widget_t *widget);typedef struct {    int expand;    int padding;    char *halpin;    hal_bit_t pin_state;} button_data_t;typedef struct {    hal_bit_t *pin;} button_hal_t;vcp_attrib_def_t button_attribs[] = {    { "expand", "0", ATTRIB_BOOL, offsetof(button_data_t, expand) },    { "padding", "0", ATTRIB_INT, offsetof(button_data_t, padding) },    { "halpin", NULL, ATTRIB_STRING, offsetof(button_data_t, halpin) },    { NULL, NULL, 0, 0 }};vcp_widget_def_t button_def = {    "button",    CL_CONTROL,    CH_ONE | CL_LAYOUT | CL_LABEL,    button_attribs,    sizeof(button_data_t),    init_button};static void button_pressed(GtkWidget * widget, gpointer gdata){    vcp_widget_t *wp;    button_data_t *dp;    button_hal_t *hp;        wp = (vcp_widget_t *)gdata;    dp = (button_data_t *)wp->priv_data;    hp = (button_hal_t *)wp->hal_data;    dp->pin_state = 1;    *(hp->pin) = dp->pin_state;}  static void button_released(GtkWidget * widget, gpointer gdata){    vcp_widget_t *wp;    button_data_t *dp;    button_hal_t *hp;        wp = (vcp_widget_t *)gdata;    dp = (button_data_t *)wp->priv_data;    hp = (button_hal_t *)wp->hal_data;    dp->pin_state = 0;    *(hp->pin) = dp->pin_state;}static void button_refresh (vcp_widget_t *wp){    button_data_t *dp;    button_hal_t *hp;        dp = (button_data_t *)wp->priv_data;    hp = (button_hal_t *)wp->hal_data;    *(hp->pin) = dp->pin_state;}  static int init_button ( vcp_widget_t *wp ){    button_data_t *pd;    button_hal_t *hd;    int retval;    GtkWidget *gwp;    printf ( "init button\n" );    pd = (button_data_t *)(wp->priv_data);        /* allocate HAL memory for pin */    hd = hal_malloc(sizeof(button_hal_t));    if (hd == NULL) {	printf( "init_button(): unable to allocate HAL memory\n" );	return -1;    }    wp->hal_data = hd;    /* export pin */    retval = hal_pin_bit_new(pd->halpin, HAL_WR, &(hd->pin), comp_id);    if (retval != 0) {	printf( "init_button(): unable to export HAL pin '%s'\n", pd->halpin );	return retval;    }    /* does the button have a child? */    if ( wp->child != NULL ) {	/* create a plain button so the child can go inside */	gwp = gtk_button_new();    } else {	/* create a button with a blank label inside */	gwp = gtk_button_new_with_label(NULL);    }    wp->gtk_widget = gwp;    wp->gtk_type = BIN;    /* put it in its parent */    add_to_parent(wp->parent, gwp, pd->expand, pd->padding);    /* connect handler functions */    gtk_signal_connect(GTK_OBJECT(gwp), "pressed",	GTK_SIGNAL_FUNC(button_pressed), wp);    gtk_signal_connect(GTK_OBJECT(gwp), "released",	GTK_SIGNAL_FUNC(button_released), wp);    /* use a poll function for periodic refresh, even if the user        doesn't press the button */    wp->poll_funct = button_refresh;    gtk_widget_show(gwp);printf ( "  done\n" );    return 0;}/** LED: The LED widget monitors a bit HAL pin and changes color    to indicate the state of the pin.*/static int init_led(vcp_widget_t *widget);typedef struct {    int expand;    int padding;    char *halpin;} led_data_t;typedef struct {    hal_bit_t *pin;} led_hal_t;vcp_attrib_def_t led_attribs[] = {    { "expand", "0", ATTRIB_BOOL, offsetof(led_data_t, expand) },    { "padding", "0", ATTRIB_INT, offsetof(led_data_t, padding) },    { "halpin", NULL, ATTRIB_STRING, offsetof(led_data_t, halpin) },    { NULL, NULL, 0, 0 }};vcp_widget_def_t led_def = {    "LED",    CL_DISPLAY,    CH_NONE,    led_attribs,    sizeof(led_data_t),    init_led};static void led_refresh (vcp_widget_t *wp){    led_data_t *dp;    led_hal_t *hp;        dp = (led_data_t *)wp->priv_data;    hp = (led_hal_t *)wp->hal_data;    /* refresh code goes here - get busy and write it!!!!! */}  static int init_led ( vcp_widget_t *wp ){    led_data_t *pd;    led_hal_t *hd;    int retval;    GtkWidget *gwp;    printf ( "init led\n" );    pd = (led_data_t *)(wp->priv_data);        /* allocate HAL memory for pin */    hd = hal_malloc(sizeof(led_hal_t));    if (hd == NULL) {	printf( "init_led(): unable to allocate HAL memory\n" );	return -1;    }    wp->hal_data = hd;    /* export pin */    retval = hal_pin_bit_new(pd->halpin, HAL_RD, &(hd->pin), comp_id);    if (retval != 0) {	printf( "init_button(): unable to export HAL pin '%s'\n", pd->halpin );	return retval;    }    /* create the GTK widget here, see scope_disp.c for ideas */        /* eliminate warning until someone writes the correct     * thing here */    gwp = NULL;        wp->gtk_widget = gwp;    wp->gtk_type = NONE;    /* put it in its parent */    add_to_parent(wp->parent, gwp, pd->expand, pd->padding);    /* use a poll function for periodic refresh */    wp->poll_funct = led_refresh;    gtk_widget_show(gwp);printf ( "  done\n" );    return 0;}/******************* list of widget definitions ****************/vcp_widget_def_t *widget_defs[] = {    &vcp_def,    &main_window_def,    &box_def,    &label_def,    &button_def,    &led_def,    NULL};    /*********************** Local Function Code ********************/static void add_to_parent ( vcp_widget_t *parent, GtkWidget *child,			    int expand, int padding ){    if ( parent->gtk_type == BOX ) {	gtk_box_pack_start(GTK_BOX(parent->gtk_widget),	    child, expand, TRUE, padding);    } else if ( parent->gtk_type == BIN ) {	gtk_container_add(GTK_CONTAINER(parent->gtk_widget), child);    } else {	printf ( "can't pack things into '%s'\n", parent->type->name );    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -