1 /*
2 * This file is part of XForms.
3 *
4 * XForms is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License as
6 * published by the Free Software Foundation; either version 2.1, or
7 * (at your option) any later version.
8 *
9 * XForms is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with XForms; see the file COPYING. If not, write to
16 * the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
17 * MA 02111-1307, USA.
18 *
19 * Author: Jens Thoms Toerring <jt@toerring.de>
20 */
21
22
23 /* This demo program is called "strange button" since the important
24 button used here doesn't really look like one. The program is
25 meant to demonstrate how one could use the ability to determine
26 on which character of the label of an object the mouse is on can
27 be used. Within the callback for the object this functionality is
28 used to modify the label in a useful way. (Mis-) Using a button
29 is necessary since a kind of object is needed that receives events
30 and thus can have callbacks (in principle also a box or frame might
31 do but they are static objects and thus can't have callbacks). */
32
33
34 #ifdef HAVE_CONFIG_H
35 #include "config.h"
36 #endif
37
38 #include <stdlib.h>
39 #include "include/forms.h"
40
41
42 typedef struct {
43 FL_FORM * x;
44 void * vdata;
45 char * cdata;
46 long ldata;
47 FL_OBJECT * c;
48 } FD_x;
49
50
51 /***************************************
52 ***************************************/
53
54 static void
ccb(FL_OBJECT * ob,long data FL_UNUSED_ARG)55 ccb( FL_OBJECT * ob,
56 long data FL_UNUSED_ARG )
57 {
58 unsigned int button = fl_mouse_button( );
59 int idx = fl_get_label_char_at_mouse( ob );
60 char digit;
61 const char * label = fl_get_object_label( ob );
62 char * str;
63
64 /* If the mouse isn't over one of the digits return */
65
66 if ( idx == -1 )
67 return;
68
69 /* Get the digit it's on */
70
71 digit = label[ idx ];
72
73 /* Left button or sroll up increment the value of the digit (with wrap
74 around if we would get beyond '9') and right button and scroll down
75 reduce its value (again with wrap around if it would drop below '1') */
76
77 if ( button == FL_LEFT_MOUSE || button == FL_SCROLLUP_MOUSE )
78 {
79 if ( digit == '9' )
80 digit = '0';
81 else
82 ++digit;
83 }
84 else if ( button == FL_RIGHT_MOUSE || button == FL_SCROLLDOWN_MOUSE)
85 {
86 if ( digit == '0' )
87 digit = '9';
88 else
89 --digit;
90 }
91
92 /* Make a copy of the label and replace the digit we want to change */
93
94 str = fl_strdup( label );
95 str[ idx ] = digit;
96
97 /* Set this as the new label and get rid of the memory we allocated */
98
99 fl_set_object_label( ob, str );
100 fl_free( str );
101 }
102
103
104 /***************************************
105 ***************************************/
106
107 static FD_x *
create_form_x(void)108 create_form_x( void )
109 {
110 FL_OBJECT *obj;
111 FD_x *fdui = ( FD_x * ) fl_malloc( sizeof *fdui );
112
113 fdui->vdata = fdui->cdata = NULL;
114 fdui->ldata = 0;
115
116 fdui->x = fl_bgn_form( FL_FLAT_BOX, 170, 175 );
117
118 obj = fl_add_text( FL_NORMAL_TEXT, 20, 10, 150, 50,
119 "Click on the digits...\n(left or right button)" );
120 fl_set_object_lsize( obj, FL_NORMAL_SIZE );
121
122 /* Create a button that rather looks like an embossed box (but boxes
123 don't get events, so we need some object like a button that does. */
124
125 fdui->c = obj = fl_add_button( FL_TOUCH_BUTTON, 25, 65, 120, 40, "012345" );
126 fl_set_object_boxtype( obj, FL_EMBOSSED_BOX );
127 fl_set_object_color( obj, FL_YELLOW, FL_YELLOW );
128 fl_set_object_lstyle( obj, FL_FIXED_STYLE | FL_EMBOSSED_STYLE );
129 fl_set_object_lsize( obj, FL_LARGE_SIZE );
130 fl_set_object_callback( obj, ccb, 0 );
131 fl_set_button_mouse_buttons( obj, 1 | 4 | 8 | 16 );
132 fl_set_object_helper( obj, "May not look like a button but it's one...\n"
133 "(Also try the scroll wheel if you have.)" );
134
135 obj = fl_add_button( FL_RETURN_BUTTON, 50, 130, 70, 30, "Exit" );
136 fl_set_button_mouse_buttons( obj, 1 );
137
138 fl_end_form( );
139
140 /* Just to make sure the label fits completely into the button */
141
142 fl_adjust_form_size( fdui->x );
143
144 fdui->x->fdui = fdui;
145
146 return fdui;
147 }
148
149
150 /***************************************
151 ***************************************/
152
153 int
main(int argc,char * argv[])154 main( int argc,
155 char * argv[ ] )
156 {
157 FD_x *fd_x;
158
159 fl_initialize( &argc, argv, 0, 0, 0 );
160 fd_x = create_form_x( );
161
162 fl_show_form( fd_x->x, FL_PLACE_CENTERFREE, FL_FULLBORDER,
163 "strange button" );
164
165 fl_do_forms( );
166
167 if ( fl_form_is_visible( fd_x->x ) )
168 fl_hide_form( fd_x->x );
169
170 fl_free( fd_x );
171 fl_finish( );
172
173 return 0;
174 }
175
176
177 /*
178 * Local variables:
179 * tab-width: 4
180 * indent-tabs-mode: nil
181 * End:
182 */
183