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
|
/*
* widgets.c
*
* Copyright (C) 2006 Simon Wunderlich <dotslash@packetmixer.de>
*
* This file is part of s3d, a 3d network display server.
* See http://s3d.berlios.de/ for more updates.
*
* s3d is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* s3d is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with s3d; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <s3d.h>
#include <s3dw.h>
#include <stdio.h> /* NULL */
#include <time.h> /* nanosleep() */
#include <stdlib.h> /* free() */
#include <string.h> /* strlen() */
#define S3DUNUSED(x) x
static s3dw_surface *surface;
static s3dw_input *input;
static struct timespec t = {
0, 33*1000*1000
}; /* 33 mili seconds */
static void mainloop(void)
{
/* keep this in your mainloop. this will do smooth animations for you ... */
s3dw_ani_mate();
nanosleep(&t, NULL);
}
/* you should always put the s3dw-handler in your own event handler,
* if you want s3dw to react on clicks or keys ... and i'm sure you
* want that ... */
static int click(struct s3d_evt *evt)
{
return(s3dw_handle_click(evt));
}
static void key_button(s3dw_widget *button)
{
s3dw_delete(button->parent); /* parent =surface. this means close containing window */
}
static int key(struct s3d_evt *evt)
{
struct s3d_key_event *key = (struct s3d_key_event *)evt->buf;
char string[8];
s3dw_surface *miniwin;
s3dw_button *button;
s3dw_handle_key(evt);
/* okay, that's a little bit insane ... ;)
* we create some little windows with the actual key pressed. */
if (key->unicode != 0) {
miniwin = s3dw_surface_new("Key", 6, 6);
sprintf(string, "%c", key->unicode);
s3dw_label_new(miniwin, string, 1, 2);
button = s3dw_button_new(miniwin, "OK", 2, 4);
/* clicking on the button will exit ... */
button->onclick = key_button;
/* of couse, show it */
s3dw_show(S3DWIDGET(miniwin));
}
return(0);
}
static void done_button(s3dw_widget *S3DUNUSED(dummy))
{
s3d_quit();
}
static void okay_button(s3dw_widget *S3DUNUSED(dummy))
{
s3dw_button *button;
char string[32];
char *age;
/* get the input of the text ... before its destroyed, of course*/
age = s3dw_input_gettext(input);
/* delete the old surface with it subwidgets */
s3dw_delete(S3DWIDGET(surface));
/* and create a new one ... */
surface = s3dw_surface_new("Ah!", 10, 7);
/* just cutting the string if it's too long */
if (strlen(age) > 8) age[8] = 0;
/* assemble the string ..*/
sprintf(string, "I see, %s!!", age);
s3dw_label_new(surface, string, 1, 2);
button = s3dw_button_new(surface, "Great", 4, 4);
/* clicking on the button will exit ... */
button->onclick = done_button;
/* of couse, show it */
s3dw_show(S3DWIDGET(surface));
/* we don't need it anymore. always free strings, don't leak around */
free(age);
}
static void no_button(s3dw_widget *S3DUNUSED(dummy))
{
s3dw_button *button;
s3dw_delete(S3DWIDGET(surface));
surface = s3dw_surface_new("Well ...", 10, 7);
s3dw_label_new(surface, "If you don't want to tell me ...", 1, 2);
button = s3dw_button_new(surface, "Bye", 4, 4);
/* clicking on the button will exit ... */
button->onclick = done_button;
/* of couse, show it */
s3dw_show(S3DWIDGET(surface));
}
static const char *text = "okay\nn2\n3\nfooobarfooobar ...\noh no\n its too loooong\n";
int main(int argc, char **argv)
{
s3dw_button *button;
if (!s3d_init(&argc, &argv, "widgettest")) {
s3d_set_callback(S3D_EVENT_OBJ_CLICK, click);
s3d_set_callback(S3D_EVENT_KEY, key);
s3d_set_callback(S3D_EVENT_OBJ_INFO, s3dw_object_info);
/* this creates the "window" */
surface = s3dw_surface_new("Hello World", 20, 20);
/* put a label (which is simply text) at position x=1, y=2 */
s3dw_label_new(surface, "How old are you?", 1, 2);
/* put an input box right below. we grab the pointer because we want to focus it (need for reference) */
input = s3dw_input_new(surface, 8, 1, 4);
/* we want the input-field be focused on our widget */
s3dw_focus(S3DWIDGET(input));
/* create the okay button */
button = s3dw_button_new(surface, "OK", 1, 7);
/* define the callback when the button is clicked. in our case, okay_button() will handle the event */
button->onclick = okay_button;
/* another button */
button = s3dw_button_new(surface, "Won't tell you", 10, 7);
/* we will tell him how sad we are ... */
button->onclick = no_button;
/* create some textbox at (1,10) widh width 18 and height 8 */
s3dw_textbox_new(surface, text, 1, 10, 18, 8);
/* this widget is focused (of course, it's our only one ... */
s3dw_focus(S3DWIDGET(surface));
/* show it. without showing, things would be boring... */
s3dw_show(S3DWIDGET(surface));
s3d_mainloop(mainloop);
s3d_quit();
}
return(0);
}
|