blob: c30ca3a47d25e8efa61d7fdbb91053ee83ea8f17 [file] [log] [blame]
adamdunkels2f5291c2003-04-09 12:55:06 +00001/*
2 * Copyright (c) 2003, Adam Dunkels.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following
12 * disclaimer in the documentation and/or other materials provided
13 * with the distribution.
14 * 3. All advertising materials mentioning features or use of this
15 * software must display the following acknowledgement:
16 * This product includes software developed by Adam Dunkels.
17 * 4. The name of the author may not be used to endorse or promote
18 * products derived from this software without specific prior
19 * written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
22 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
25 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 *
33 * This an example program for the Contiki desktop OS
34 *
adamdunkelse9900d82003-04-10 07:02:05 +000035 * $Id: hello.c,v 1.3 2003/04/10 07:02:05 adamdunkels Exp $
adamdunkels2f5291c2003-04-09 12:55:06 +000036 *
37 */
38
adamdunkelscaebe7d2003-04-09 19:15:25 +000039/* This is an example of how to write programs for Contiki. It
40 displays a window with the famous message "Hello world" and two
41 buttons; one which changes the message slightly and one which quits
42 the application. */
adamdunkels2f5291c2003-04-09 12:55:06 +000043
44#include "ctk.h"
45#include "dispatcher.h"
46#include "loader.h"
47
48static struct ctk_window window;
49
50static struct ctk_label hellolabel =
51 {CTK_LABEL(0, 0, 8, 1, "Hello")};
52static struct ctk_label worldlabel =
53 {CTK_LABEL(8, 1, 6, 1, "world!")};
54
55static struct ctk_button updatebutton =
56 {CTK_BUTTON(0, 3, 6, "Update")};
57static struct ctk_button closebutton =
58 {CTK_BUTTON(8, 3, 5, "Close")};
59
adamdunkelscaebe7d2003-04-09 19:15:25 +000060static DISPATCHER_SIGHANDLER(hello_sighandler, s, data);
adamdunkels2f5291c2003-04-09 12:55:06 +000061static struct dispatcher_proc p =
62 {DISPATCHER_PROC("Hello world", NULL, hello_sighandler, NULL)};
63static ek_id_t id;
64
65/*-----------------------------------------------------------------------------------*/
66LOADER_INIT_FUNC(hello_init)
67{
68 if(id == EK_ID_NONE) {
69 id = dispatcher_start(&p);
70
71 ctk_window_new(&window, 15, 4, "Hello");
adamdunkels2f5291c2003-04-09 12:55:06 +000072
73 CTK_WIDGET_ADD(&window, &hellolabel);
74 CTK_WIDGET_ADD(&window, &worldlabel);
75
76 CTK_WIDGET_ADD(&window, &updatebutton);
77 CTK_WIDGET_ADD(&window, &closebutton);
78
79 CTK_WIDGET_FOCUS(&window, &updatebutton);
80
81 dispatcher_listen(ctk_signal_button_activate);
82 dispatcher_listen(ctk_signal_window_close);
83 }
84 ctk_window_open(&window);
85}
86/*-----------------------------------------------------------------------------------*/
87static void
88hello_quit(void)
89{
90 dispatcher_exit(&p);
91 id = EK_ID_NONE;
92 LOADER_UNLOAD();
93 ctk_redraw();
94}
95/*-----------------------------------------------------------------------------------*/
adamdunkelscaebe7d2003-04-09 19:15:25 +000096static DISPATCHER_SIGHANDLER(hello_sighandler, s, data)
adamdunkels2f5291c2003-04-09 12:55:06 +000097{
adamdunkelscaebe7d2003-04-09 19:15:25 +000098 DISPATCHER_SIGHANDLER_ARGS(s, data);
99
adamdunkels2f5291c2003-04-09 12:55:06 +0000100 if(s == ctk_signal_button_activate) {
101 if(data == (ek_data_t)&updatebutton) {
102 ctk_label_set_text(&hellolabel, "Good bye");
103 CTK_WIDGET_REDRAW(&hellolabel);
104 } else if(data == (ek_data_t)&closebutton) {
105 ctk_window_close(&window);
106 hello_quit();
107 }
108 } else if(s == ctk_signal_window_close &&
109 data == (ek_data_t)&window) {
110 hello_quit();
111 }
112}
113/*-----------------------------------------------------------------------------------*/