source: mainline/uspace/app/vdemo/vdemo.c@ bae1e1f

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

resize only those windows that declare and support resizing capabilities
introduce generic window flags

  • 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(
88 &lbl->label, parent, caption, 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],
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, "Confirm", 16, btn_bg,
131 btn_fg, btn_text);
132 button_t *btn_cancel = create_button(NULL, "Cancel", 16, btn_bg,
133 btn_fg, btn_text);
134 grid_t *grid = create_grid(window_root(main_window), 2, 2, grd_bg);
135 if (!lbl_action || !btn_confirm || !btn_cancel || !grid) {
136 window_close(main_window);
137 printf("Cannot create widgets.\n");
138 return 1;
139 }
140
141 sig_connect(
142 &btn_confirm->clicked,
143 &lbl_action->label.widget,
144 lbl_action->confirm);
145 sig_connect(
146 &btn_cancel->clicked,
147 &lbl_action->label.widget,
148 lbl_action->cancel);
149
150 grid->add(grid, &lbl_action->label.widget, 0, 0, 2, 1);
151 grid->add(grid, &btn_confirm->widget, 0, 1, 1, 1);
152 grid->add(grid, &btn_cancel->widget, 1, 1, 1, 1);
153 window_resize(main_window, 0, 0, 200, 76,
154 WINDOW_PLACEMENT_CENTER);
155
156 window_exec(main_window);
157 task_retval(0);
158 async_manager();
159 return 1;
160 } else {
161 printf("Compositor server not specified.\n");
162 return 1;
163 }
164}
165
166/** @}
167 */
Note: See TracBrowser for help on using the repository browser.