GTK+ Forums Forum Index GTK+ Forums
Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

GtkIndicator -- a flashing light toggle button

 
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Example Code
Author Message
Mk27
GTK+ Guru


Joined: 07 Aug 2008
Posts: 100
Location: NYC

PostPosted: Fri Oct 17, 2008 3:37 pm    Post subject: GtkIndicator -- a flashing light toggle button Reply with quote

I came up with this because I wanted a toggle button for starting/stopping a server, but with something more substantially visual to indicate the toggle is depressed and the server is up and running.

The demo here uses xpm images for simplicity, but if you use a higher quality png it looks pretty sharp. It starts with a normal button; when you click it, a flashing light replaces the button. Click on the light to get the button back. Alternately, you could just leave the image of the light off and not use a labelled button at all (or use a third image, like a little globe, clock, etc.)

The button used is not actually a toggle, but the blinkbutton struct has a "status" member that can be used the same way you would check a toggle's state. In the demo, toggling the button doesn't do anything but start the light flashing; in implimentation you would want to add some further purpose in the toggle_indicator() function.

I put the struct spec and functions in a seperate header to the demo program for convenience. A second header contains the xpm images for the demo.

Code: (C)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45

#include <gtk/gtk.h>
#include "indicator.h"
#include "images.h"  /* the xpm's */


int main () {
    GdkPixbuf *pbuf;
    GtkWidget *window, *button, *on1_img, *on2_img, *blab;
    blinkbutton mybutton;

        gtk_init (NULL,NULL);
       
        /*initialize images*/
   
pbuf=gdk_pixbuf_new_from_xpm_data((const char**)on1_xpm);
    on1_img=gtk_image_new_from_pixbuf(pbuf);
    g_object_ref(G_OBJECT(on1_img));                /* add one to the reference count */
   
pbuf=gdk_pixbuf_new_from_xpm_data((const char**)on2_xpm);    /* otherwise the widget will get */
   
on2_img=gtk_image_new_from_pixbuf(pbuf);            /* deleted the first time it's removed */
   
g_object_ref(G_OBJECT(on2_img));
   
        window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
        g_signal_connect (G_OBJECT (window), "destroy",
              G_CALLBACK (gtk_main_quit), NULL);
        gtk_container_set_border_width (GTK_CONTAINER (window), 10);
    gtk_window_set_title(GTK_WINDOW (window), "GtkIndicator");
   
    button = gtk_button_new();        /* don't use _with_label because the */
   
blab=gtk_label_new("click");        /* label must be a seperate widget  */
   
g_object_ref(G_OBJECT(blab));        /* add one to the reference count here too */
   
mybutton.wgt=button; mybutton.lab=blab; mybutton.image1=on1_img; mybutton.image2=on2_img; mybutton.status=0;
    /* see indicator.h for the struct spec...define everything but blinkbutton.timer */
   
gtk_container_add(GTK_CONTAINER(mybutton.wgt),mybutton.lab);
    gtk_container_add(GTK_CONTAINER(window),button);
    g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(toggle_indicator),&mybutton);
   
    gtk_widget_show(on1_img);      /* the images are not contained in the window */
   
gtk_widget_show(on2_img);     /* so must be realized seperately */   
       
gtk_widget_show_all (window);
   
        gtk_main ();
   
        return 0;
}


indicator.h
Code: (C)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

#include <gtk/gtk.h>

typedef struct _blink_button blinkbutton;
struct _blink_button {
    GtkWidget *wgt;        /*the button*/
   
GtkWidget *lab;        /*the button label*/
   
GtkWidget *image1;    /*first image of blink*/
   
GtkWidget *image2;    /*second*/
   
short int status;    /*contents of button: 0=lab,1=image1,2=image2*/
   
guint timer;        /*the g_timeout*/
};

gboolean flashblink (blinkbutton *button) {
    if (button->status==1) {
        gtk_container_remove(GTK_CONTAINER(button->wgt),button->image1);
        gtk_container_add(GTK_CONTAINER(button->wgt),button->image2);
        button->status=2;
    } else {
        gtk_container_remove(GTK_CONTAINER(button->wgt),button->image2);
        gtk_container_add(GTK_CONTAINER(button->wgt),button->image1);
        button->status=1;
    }
    return TRUE;
}

void toggle_indicator (GtkWidget *widget, blinkbutton *button) {
    if (button->status==0) { /*toggle on*/
       
gtk_container_remove(GTK_CONTAINER(button->wgt),button->lab);
        gtk_button_set_relief(GTK_BUTTON(button->wgt),GTK_RELIEF_NONE);
        button->status=1;
        gtk_container_add(GTK_CONTAINER(button->wgt),button->image1);
        button->timer=g_timeout_add((750), (GSourceFunc)flashblink, button);
    } else { /*toggle off*/
       
g_source_remove(button->timer);
                /* remove the correct image*/
       
if (button->status == 1) gtk_container_remove(GTK_CONTAINER(button->wgt),button->image1);
        else gtk_container_remove(GTK_CONTAINER(button->wgt),button->image2);
        button->status=0;
        gtk_button_set_relief(GTK_BUTTON(button->wgt),GTK_RELIEF_NORMAL);
        gtk_container_add(GTK_CONTAINER(button->wgt),button->lab);
    }
}


images.h
Code: (C)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219

/* XPM */
static char * on1_xpm[] = {
"20 20 54 1",
"     c None",
".    c #D02C10",
"+    c #CB210C",
"@    c #C61708",
"#    c #C21207",
"$    c #C31207",
"%    c #C51708",
"&    c #CA210C",
"*    c #C81D0A",
"=    c #B80803",
"-    c #A90100",
";    c #9C0000",
">    c #950000",
",    c #C11206",
"'    c #AA0201",
")    c #900000",
"!    c #7C0000",
"~    c #6E0000",
"{    c #680000",
"]    c #6D0000",
"^    c #C21206",
"/    c #A20000",
"(    c #820000",
"_    c #660000",
":    c #590000",
"<    c #530000",
"[    c #510000",
"}    c #AB0201",
"|    c #630000",
"1    c #4B0000",
"2    c #460000",
"3    c #430000",
"4    c #420000",
"5    c #450000",
"6    c #4C0000",
"7    c #C81C0A",
"8    c #480000",
"9    c #3F0000",
"0    c #370000",
"a    c #340000",
"b    c #330000",
"c    c #380000",
"d    c #540000",
"e    c #7D0000",
"f    c #2B0000",
"g    c #260000",
"h    c #1F0000",
"i    c #180000",
"j    c #170000",
"k    c #0C0000",
"l    c #500000",
"m    c #960000",
"n    c #0B0000",
"o    c #CB210B",
"                    ",
"      .+@#$%&.      ",
"     *=-;>>;-=*     ",
"    ,')!~{{]!)'^    ",
"   ^/(_:<[[<:_(/^   ",
"  *}(|<123456<|(}7  ",
" .=)_<890abc98d_)=. ",
" +-e:69afggfa96:e-+ ",
" %;~<20fhijhf02<~;% ",
" $>{[3agjkkjga4l{m$ ",
" $m{[3bgjknjga3[{>$ ",
" %;]<50fhjjhf02<~;% ",
" o-e:19afggfa96:!-& ",
" .=)_<890aa098d_)=. ",
"  7'(|<623326d|(}7  ",
"   ^/(_:<[l<:_(/,   ",
"    ,')!~{{~!)}^    ",
"     *=-;mm;-=7     ",
"      .&%$$%&.      ",
"                    "};
/* XPM */
static char *on2_xpm[] = {
/* columns rows colors chars-per-pixel */
"20 20 114 2",
"   c #753D00",
".  c #763E00",
"X  c #7B3E01",
"o  c #7A3F01",
"O  c #7B3F01",
"+  c #7F3E01",
"@  c #505F02",
"#  c #555D02",
"$  c #5E5C01",
"%  c #5F5C01",
"&  c #694600",
"*  c #684700",
"=  c #624F01",
"-  c #634F01",
";  c #794001",
":  c #7A4001",
">  c #714B01",
",  c #714C01",
"<  c #635801",
"1  c #635901",
"2  c #4D6501",
"3  c #4F6702",
"4  c #4F6A02",
"5  c #506002",
"6  c #536602",
"7  c #536702",
"8  c #437702",
"9  c #447702",
"0  c #4B7102",
"q  c #4B7202",
"w  c #487A03",
"e  c #8A2F00",
"r  c #902F00",
"t  c #922E01",
"y  c #8C3701",
"u  c #8D3701",
"i  c #813800",
"p  c #823800",
"a  c #803D01",
"s  c #833D01",
"d  c #843D01",
"f  c #8C3A01",
"g  c #8D3A01",
"h  c #903000",
"j  c #9E3203",
"k  c #A22B03",
"l  c #AC2F06",
"z  c #AD2F06",
"x  c #A73706",
"c  c #AB3307",
"v  c #AC3207",
"b  c #A83706",
"n  c #AE3607",
"m  c #AF3607",
"M  c #B03307",
"N  c #B03407",
"B  c #B53407",
"V  c #B53507",
"C  c #B13A07",
"Z  c #B13B07",
"A  c #B53B09",
"S  c #B73A08",
"D  c #B63A09",
"F  c #B73A09",
"G  c #B33E09",
"H  c #B43E09",
"J  c #B14009",
"K  c #B8460D",
"L  c #B9470D",
"P  c #B44E0D",
"I  c #B3510D",
"U  c #B2510E",
"Y  c #3E8603",
"T  c #3F8603",
"R  c #3E8B02",
"E  c #3B9703",
"W  c #3C9503",
"Q  c #3D9503",
"!  c #3C9703",
"~  c #3A9A03",
"^  c #3B9A03",
"/  c #3C9A03",
"(  c #3C9B03",
")  c #38A303",
"_  c #38A304",
"`  c #35AA04",
"'  c #36B304",
"]  c #34B704",
"[  c #33BD04",
"{  c #34BD04",
"}  c #418703",
"|  c #33C204",
" . c #32C304",
".. c #33C305",
"X. c #32C405",
"o. c #31C705",
"O. c #32C704",
"+. c #32C805",
"@. c #2FD905",
"#. c #2FDA05",
"$. c #30D605",
"%. c #2FE305",
"&. c #2FE006",
"*. c #2FE306",
"=. c #2FE605",
"-. c #2FE706",
";. c #2FEA06",
":. c #2EED06",
">. c #2EF306",
",. c #2EF406",
"<. c #2EF506",
"1. c #2EF706",
"2. c #2EF806",
"3. c None",
/* pixels */
"3.3.3.3.3.3.3.3.3.3.3.3.3.3.3.3.3.3.3.3.",
"3.3.3.3.3.3.P H m c v v F K 3.3.3.3.3.3.",
"3.3.3.3.3.Z j y s ; o + h k V 3.3.3.3.3.",
"3.3.3.3.x f , % 6 4 3 # - . t z 3.3.3.3.",
"3.3.3.b s < 0 } E ~ E R 9 5 * e z 3.3.3.",
"3.3.Z f < w ( ' | o. .] ) Y 3 * t B 3.3.",
"3.P j , 0 ( [ $.*.=.*.@.o.` Y # . k L 3.",
"3.H y % } ' $.;.>.>.>.>.#.o.) 8 - h F 3.",
"3.n + 6 !  .*.>.1.1.1.1.:.@.] R # i v 3.",
"3.c ; 4 E o.=.>.2.2.2.1.>.*. .E 3 o v 3.",
"3.v o 6 E  .=.>.1.1.1.2.1.-.o.~ 4 ; v 3.",
"3.N i # R ] @.:.1.1.2.2.>.*. .Q 7 s n 3.",
"3.F h - 8 _ o.#.:.>.1.>.;.$.' } % y H 3.",
"3.K k . 2 T ` o.@.*.-.*.$.{ ( 0 > j P 3.",
"3.3.V t * 2 T _ ]  .+. .' ( w < f C 3.3.",
"3.3.3.z e * # 9 R ! ~ Q } 0 < d x 3.3.3.",
"3.3.3.3.v t . - # 4 4 6 % > f b 3.3.3.3.",
"3.3.3.3.3.V k e i + o + y j Z 3.3.3.3.3.",
"3.3.3.3.3.3.L F N v v N J P 3.3.3.3.3.3.",
"3.3.3.3.3.3.3.3.3.3.3.3.3.3.3.3.3.3.3.3."
};
Back to top
Display posts from previous:   
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Example Code All times are GMT
Page 1 of 1

 


Powered by phpBB © 2001, 2005 phpBB Group
CodeBB 1.0 Beta 2
Protected by Anti-Spam ACP