PatchworkOS
Loading...
Searching...
No Matches
popup.c
Go to the documentation of this file.
1#include "internal.h"
2
3typedef struct
4{
6 const char* text;
8} popup_t;
9
10static uint64_t popup_procedure(window_t* win, element_t* elem, const event_t* event)
11{
12 popup_t* popup = element_get_private(elem);
13 const theme_t* theme = element_get_theme(elem);
14
15 switch (event->type)
16 {
17 case LEVENT_INIT:
18 {
20
21 rect_t middleButtonRect = RECT_INIT_DIM(RECT_WIDTH(&rect) / 2 - POPUP_BUTTON_WIDTH / 2,
24
25 rect_t leftButtonRect = middleButtonRect;
26 leftButtonRect.left -= POPUP_BUTTON_WIDTH + theme->bigPadding;
27 leftButtonRect.right -= POPUP_BUTTON_WIDTH + theme->bigPadding;
28
29 rect_t rightButtonRect = middleButtonRect;
30 rightButtonRect.left += POPUP_BUTTON_WIDTH + theme->bigPadding;
31 rightButtonRect.right += POPUP_BUTTON_WIDTH + theme->bigPadding;
32
33 switch (popup->type)
34 {
35 case POPUP_OK:
36 {
37 button_new(elem, POPUP_RES_OK, &rightButtonRect, "Ok", ELEMENT_NONE);
38 }
39 break;
41 {
42 button_new(elem, POPUP_RES_RETRY, &middleButtonRect, "Retry", ELEMENT_NONE);
43 button_new(elem, POPUP_RES_CANCEL, &rightButtonRect, "Cancel", ELEMENT_NONE);
44 }
45 break;
46 case POPUP_YES_NO:
47 {
48 button_new(elem, POPUP_RES_YES, &middleButtonRect, "Yes", ELEMENT_NONE);
49 button_new(elem, POPUP_RES_NO, &rightButtonRect, "No", ELEMENT_NONE);
50 }
51 break;
52 }
53 }
54 break;
55 case LEVENT_REDRAW:
56 {
61
62 drawable_t draw;
63 element_draw_begin(elem, &draw);
64
65 draw_rect(&draw, &rect, theme->deco.backgroundNormal);
67
68 element_draw_end(elem, &draw);
69 }
70 break;
71 case LEVENT_ACTION:
72 {
73 if (event->lAction.type != ACTION_RELEASE)
74 {
75 break;
76 }
77
78 popup->result = event->lAction.source;
80 }
81 break;
82 }
83
84 return 0;
85}
86
87popup_result_t popup_open(const char* text, const char* title, popup_type_t type)
88{
89 display_t* disp = display_new();
90 if (disp == NULL)
91 {
92 return POPUP_RES_ERROR;
93 }
94
97
98 popup_t popup = {
100 .text = text,
101 .type = type,
102 };
103
106 window_t* win =
108 if (win == NULL)
109 {
110 return POPUP_RES_ERROR;
111 }
112
113 event_t event = {0};
114 while (display_next(disp, &event, CLOCKS_NEVER) != ERR)
115 {
116 display_dispatch(disp, &event);
117 }
118
119 window_free(win);
120 display_free(disp);
121
122 return popup.result;
123}
#define CLOCKS_NEVER
Definition clock_t.h:16
static rect_t screenRect
Definition compositor.c:10
void display_disconnect(display_t *disp)
Disconnect the display connection.
Definition display.c:161
uint64_t display_next(display_t *disp, event_t *event, clock_t timeout)
Retrieve the next event from the display connection.
Definition display.c:213
uint64_t display_dispatch(display_t *disp, const event_t *event)
Dispatch an event to the appropriate surface.
Definition display.c:440
uint64_t display_get_screen(display_t *disp, rect_t *rect, uint64_t index)
Get the rectangle of a screen.
Definition display.c:603
void display_free(display_t *disp)
Free a display connection.
Definition display.c:113
display_t * display_new(void)
Create a new display connection.
Definition display.c:36
void draw_text_multiline(drawable_t *draw, const rect_t *rect, const font_t *font, align_t xAlign, align_t yAlign, pixel_t pixel, const char *text)
Draw multiline text to a drawable.
Definition drawable.c:814
void draw_rect(drawable_t *draw, const rect_t *rect, pixel_t pixel)
Draw a filled rectangle.
Definition drawable.c:7
@ ALIGN_MIN
Definition drawable.h:51
@ ALIGN_CENTER
Definition drawable.h:49
theme_t * element_get_theme(element_t *elem)
Get the theme of an element.
Definition element.c:401
void element_draw_end(element_t *elem, drawable_t *draw)
End drawing to an element.
Definition element.c:427
#define ELEMENT_NONE
Definition element.h:43
rect_t element_get_content_rect(element_t *elem)
Get the element's rectangle in local coordinates.
Definition element.c:213
void element_draw_begin(element_t *elem, drawable_t *draw)
Begin drawing to an element.
Definition element.c:411
void * element_get_private(element_t *elem)
Get private data for an element.
Definition element.c:173
#define LEVENT_REDRAW
Definition event.h:101
#define LEVENT_INIT
Definition event.h:99
#define LEVENT_ACTION
Definition event.h:102
@ ACTION_RELEASE
Definition event.h:50
@ SURFACE_WINDOW
Definition surface.h:35
element_t * button_new(element_t *parent, element_id_t id, const rect_t *rect, const char *text, element_flags_t flags)
Create a new button element.
Definition button.c:308
window_t * window_new(display_t *disp, const char *name, const rect_t *rect, surface_type_t type, window_flags_t flags, procedure_t procedure, void *private)
Allocate and initialize a new window.
Definition window.c:301
void window_free(window_t *win)
Free a window.
Definition window.c:415
display_t * window_get_display(window_t *win)
Get the display associated with the window.
Definition window.c:467
@ WINDOW_NO_CONTROLS
Disable controls (close/minimize buttons), only applies if WINDOW_DECO is set.
Definition window.h:48
@ WINDOW_DECO
Enable decorations (titlebar, close/minimize buttons, etc).
Definition window.h:46
#define NULL
Pointer error value.
Definition NULL.h:23
#define ERR
Integer error value.
Definition ERR.h:17
popup_result_t popup_open(const char *text, const char *title, popup_type_t type)
Definition popup.c:87
static uint64_t popup_procedure(window_t *win, element_t *elem, const event_t *event)
Definition popup.c:10
#define POPUP_BUTTON_WIDTH
Definition popup.h:18
#define POPUP_BUTTON_AREA_HEIGHT
Definition popup.h:15
#define POPUP_BUTTON_HEIGHT
Definition popup.h:17
#define POPUP_HORIZONTAL_PADDING
Definition popup.h:16
#define POPUP_WIDTH
Definition popup.h:14
#define POPUP_HEIGHT
Definition popup.h:13
popup_type_t
Definition popup.h:21
@ POPUP_RETRY_CANCEL
Definition popup.h:23
@ POPUP_OK
Definition popup.h:22
@ POPUP_YES_NO
Definition popup.h:24
popup_result_t
Definition popup.h:28
@ POPUP_RES_CLOSE
Definition popup.h:34
@ POPUP_RES_RETRY
Definition popup.h:30
@ POPUP_RES_OK
Definition popup.h:29
@ POPUP_RES_CANCEL
Definition popup.h:31
@ POPUP_RES_NO
Definition popup.h:33
@ POPUP_RES_ERROR
Definition popup.h:35
@ POPUP_RES_YES
Definition popup.h:32
#define RECT_INIT_DIM(x, y, width, height)
Definition rect.h:32
#define RECT_HEIGHT(rect)
Definition rect.h:39
#define RECT_WIDTH(rect)
Definition rect.h:38
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
Opaque display structure.
Definition internal.h:61
Drawable structure.
Definition drawable.h:35
Opaque element structure.
Definition internal.h:23
Event structure.
Definition event.h:271
event_type_t type
Definition event.h:272
levent_action_t lAction
Definition event.h:288
action_type_t type
Definition event.h:246
Definition popup.c:4
popup_result_t result
Definition popup.c:5
popup_type_t type
Definition popup.c:7
const char * text
Definition popup.c:6
Definition rect.h:13
int32_t bottom
Definition rect.h:17
int32_t right
Definition rect.h:16
int32_t left
Definition rect.h:14
pixel_t backgroundNormal
Definition theme.h:35
pixel_t foregroundNormal
Definition theme.h:40
Theme structure.
Definition theme.h:71
theme_color_set_t deco
Definition theme.h:76
theme_color_set_t view
Definition theme.h:73
int64_t bigPadding
Definition theme.h:87
Opaque window structure.
Definition internal.h:44
static theme_t theme
Definition theme.c:12