[6d5e378] | 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 |
|
---|
[3e6a98c5] | 35 | #include <stdbool.h>
|
---|
[6d5e378] | 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 |
|
---|
| 48 | typedef struct my_label {
|
---|
| 49 | label_t label;
|
---|
| 50 | slot_t confirm;
|
---|
| 51 | slot_t cancel;
|
---|
| 52 | } my_label_t;
|
---|
| 53 |
|
---|
| 54 | static void deinit_my_label(my_label_t *lbl)
|
---|
| 55 | {
|
---|
| 56 | deinit_label(&lbl->label);
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | static 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 |
|
---|
| 68 | static 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 |
|
---|
| 75 | static 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 |
|
---|
| 82 | static 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 |
|
---|
| 93 | static 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 |
|
---|
| 109 | int main(int argc, char *argv[])
|
---|
| 110 | {
|
---|
| 111 | if (argc >= 2) {
|
---|
[fa98b26a] | 112 | window_t *main_window = window_open(argv[1], true, true, "vdemo", 0, 0);
|
---|
[6d5e378] | 113 | if (!main_window) {
|
---|
| 114 | printf("Cannot open main window.\n");
|
---|
| 115 | return 1;
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | pixel_t grd_bg = PIXEL(255, 240, 240, 240);
|
---|
| 119 | pixel_t btn_bg = PIXEL(255, 0, 0, 0);
|
---|
| 120 | pixel_t btn_fg = PIXEL(255, 240, 240, 240);
|
---|
| 121 | pixel_t lbl_bg = PIXEL(255, 240, 240, 240);
|
---|
| 122 | pixel_t lbl_fg = PIXEL(255, 0, 0, 0);
|
---|
| 123 |
|
---|
| 124 | my_label_t *lbl_action = create_my_label(NULL, "Hello there!", 16, lbl_bg, lbl_fg);
|
---|
| 125 | button_t *btn_confirm = create_button(NULL, "Confirm", 16, btn_bg, btn_fg);
|
---|
| 126 | button_t *btn_cancel = create_button(NULL, "Cancel", 16, btn_bg, btn_fg);
|
---|
| 127 | grid_t *grid = create_grid(window_root(main_window), 2, 2, grd_bg);
|
---|
| 128 | if (!lbl_action || !btn_confirm || !btn_cancel || !grid) {
|
---|
| 129 | window_close(main_window);
|
---|
| 130 | printf("Cannot create widgets.\n");
|
---|
| 131 | return 1;
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | sig_connect(
|
---|
| 135 | &btn_confirm->clicked,
|
---|
| 136 | &lbl_action->label.widget,
|
---|
| 137 | lbl_action->confirm);
|
---|
| 138 | sig_connect(
|
---|
| 139 | &btn_cancel->clicked,
|
---|
| 140 | &lbl_action->label.widget,
|
---|
| 141 | lbl_action->cancel);
|
---|
| 142 |
|
---|
| 143 | grid->add(grid, &lbl_action->label.widget, 0, 0, 1, 2);
|
---|
| 144 | grid->add(grid, &btn_confirm->widget, 1, 0, 1, 1);
|
---|
| 145 | grid->add(grid, &btn_cancel->widget, 1, 1, 1, 1);
|
---|
| 146 | window_resize(main_window, 200, 70);
|
---|
| 147 |
|
---|
| 148 | window_exec(main_window);
|
---|
| 149 | task_retval(0);
|
---|
| 150 | async_manager();
|
---|
| 151 | return 1;
|
---|
| 152 | } else {
|
---|
| 153 | printf("Compositor server not specified.\n");
|
---|
| 154 | return 1;
|
---|
| 155 | }
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | /** @}
|
---|
| 159 | */
|
---|