source: mainline/uspace/app/vdemo/vdemo.c@ 10cb47e

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 10cb47e was 10cb47e, checked in by Martin Decky <martin@…>, 9 years ago

add support for custom client data into widgets

  • Property mode set to 100644
File size: 4.7 KB
Line 
1/*
2 * Copyright (c) 2012 Petr Koupy
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 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup vdemo
30 * @{
31 */
32/** @file
33 */
34
35#include <stdbool.h>
36#include <stdio.h>
37#include <malloc.h>
38#include <io/pixel.h>
39#include <task.h>
40
41#include <window.h>
42#include <grid.h>
43#include <button.h>
44#include <label.h>
45
46#define NAME "vdemo"
47
48typedef struct my_label {
49 label_t label;
50 slot_t confirm;
51 slot_t cancel;
52} my_label_t;
53
54static void deinit_my_label(my_label_t *lbl)
55{
56 deinit_label(&lbl->label);
57}
58
59static void my_label_destroy(widget_t *widget)
60{
61 my_label_t *lbl = (my_label_t *) widget;
62
63 deinit_my_label(lbl);
64
65 free(lbl);
66}
67
68static void on_confirm(widget_t *widget, void *data)
69{
70 my_label_t *lbl = (my_label_t *) widget;
71 const char *confirmed = "Confirmed";
72 lbl->label.rewrite(&lbl->label.widget, (void *) confirmed);
73}
74
75static void on_cancel(widget_t *widget, void *data)
76{
77 my_label_t *lbl = (my_label_t *) widget;
78 const char *cancelled = "Cancelled";
79 lbl->label.rewrite(&lbl->label.widget, (void *) cancelled);
80}
81
82static bool init_my_label(my_label_t *lbl, widget_t *parent,
83 const char *caption, uint16_t points, pixel_t background, pixel_t foreground)
84{
85 lbl->confirm = on_confirm;
86 lbl->cancel = on_cancel;
87 bool initialized = init_label(&lbl->label, parent, NULL, caption,
88 points, background, foreground);
89 lbl->label.widget.destroy = my_label_destroy;
90 return initialized;
91}
92
93static my_label_t *create_my_label(widget_t *parent,
94 const char *caption, uint16_t points, pixel_t background, pixel_t foreground)
95{
96 my_label_t *lbl = (my_label_t *) malloc(sizeof(my_label_t));
97 if (!lbl) {
98 return NULL;
99 }
100
101 if (init_my_label(lbl, parent, caption, points, background, foreground)) {
102 return lbl;
103 } else {
104 free(lbl);
105 return NULL;
106 }
107}
108
109int main(int argc, char *argv[])
110{
111 if (argc >= 2) {
112 window_t *main_window = window_open(argv[1], NULL,
113 WINDOW_MAIN | WINDOW_DECORATED | WINDOW_RESIZEABLE, "vdemo");
114 if (!main_window) {
115 printf("Cannot open main window.\n");
116 return 1;
117 }
118
119 pixel_t grd_bg = PIXEL(255, 240, 240, 240);
120
121 pixel_t btn_bg = PIXEL(255, 240, 240, 240);
122 pixel_t btn_fg = PIXEL(255, 186, 186, 186);
123 pixel_t btn_text = PIXEL(255, 0, 0, 0);
124
125 pixel_t lbl_bg = PIXEL(255, 240, 240, 240);
126 pixel_t lbl_text = PIXEL(255, 0, 0, 0);
127
128 my_label_t *lbl_action = create_my_label(NULL, "Hello there!", 16,
129 lbl_bg, lbl_text);
130 button_t *btn_confirm = create_button(NULL, NULL, "Confirm", 16,
131 btn_bg, btn_fg, btn_text);
132 button_t *btn_cancel = create_button(NULL, NULL, "Cancel", 16,
133 btn_bg, btn_fg, btn_text);
134 grid_t *grid = create_grid(window_root(main_window), NULL, 2, 2,
135 grd_bg);
136 if (!lbl_action || !btn_confirm || !btn_cancel || !grid) {
137 window_close(main_window);
138 printf("Cannot create widgets.\n");
139 return 1;
140 }
141
142 sig_connect(
143 &btn_confirm->clicked,
144 &lbl_action->label.widget,
145 lbl_action->confirm);
146 sig_connect(
147 &btn_cancel->clicked,
148 &lbl_action->label.widget,
149 lbl_action->cancel);
150
151 grid->add(grid, &lbl_action->label.widget, 0, 0, 2, 1);
152 grid->add(grid, &btn_confirm->widget, 0, 1, 1, 1);
153 grid->add(grid, &btn_cancel->widget, 1, 1, 1, 1);
154 window_resize(main_window, 0, 0, 200, 76,
155 WINDOW_PLACEMENT_CENTER);
156
157 window_exec(main_window);
158 task_retval(0);
159 async_manager();
160 return 1;
161 } else {
162 printf("Compositor server not specified.\n");
163 return 1;
164 }
165}
166
167/** @}
168 */
Note: See TracBrowser for help on using the repository browser.