1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
|
/* See LICENSE file for copyright and license details. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include "application.h"
#include "window.h"
#include "color.h"
static void
cherry_window_draw(CherryWidget *widget)
{
CherryWindow *window = (CherryWindow *) widget;
CherryApplication *app = cherry_application_get_running_app();
XSizeHints hints;
cherry_window_get_position(window, &hints.x, &hints.y);
cherry_window_get_dimension(window, &hints.width, &hints.height);
hints.flags = PPosition|PSize;
int border_width = 5;
XSetWindowAttributes attributes;
/* attributes.background_pixel = XWhitePixel(app->display, app->screen); */
attributes.background_pixel = RGB(100, 10, 10);
window->window_handler = XCreateWindow(app->display,
XRootWindow(app->display, app->screen),
hints.x, hints.y,
hints.width, hints.height,
border_width,
app->depth,
InputOutput,
app->visual,
CWBackPixel,
&attributes);
char window_name[] = "";
char icon_name[] = "";
/* window manager properties (yes, use of StdProp is obsolete) */
XSetStandardProperties(app->display,
window->window_handler,
window->title,
icon_name,
None, NULL, 0,
&hints);
/* allow receiving mouse events */
XSelectInput(app->display,
window->window_handler,
ButtonPressMask | KeyPressMask | ExposureMask);
Atom wmDelete = XInternAtom(app->display, "WM_DELETE_WINDOW", True);
XSetWMProtocols(app->display, window->window_handler, &wmDelete, 1);
Atom wmDispose = XInternAtom(app->display, "CHERRY_DISPOSE_ON_EXIT", True);
XSetWMProtocols(app->display, window->window_handler, &wmDelete, 1);
window->gc = XCreateGC(app->display, window->window_handler, 0, 0);
XSetBackground(app->display, window->gc, WhitePixel(app->display, app->screen));
XSetForeground(app->display, window->gc, BlackPixel(app->display, app->screen));
/* XSetForeground(app->display, window->gc, RGB(255,0,127)); */
clist_add(&(app->windows), window);
XSaveContext(app->display, window->window_handler, app->context, (XPointer) window);
window->base.draw((CherryWidget *) window);
}
CherryWindow *
cherry_window_new(void)
{
CherryWindow *window = malloc(sizeof(*window));
window->base = cherry_widget_new();
window->title = NULL;
window->listener = NULL;
window->draw = cherry_window_draw;
int offset = 10;
int x, y, width, height;
cherry_widget_get_position((CherryWidget *) window, &x, &y);
cherry_widget_set_position((CherryWidget *) window, x + offset, y + offset);
cherry_widget_get_dimension((CherryWidget *) window, &width, &height);
cherry_widget_set_dimension((CherryWidget *) window, width + offset, height + offset);
return window;
}
void
cherry_window_dispose_on_exit(CherryWindow *w)
{
CherryApplication *app = cherry_application_get_running_app();
XEvent evt;
evt.xclient.type = ClientMessage;
evt.xclient.serial = 0;
evt.xclient.send_event = 1;
evt.xclient.message_type = XInternAtom(app->display, "WM_PROTOCOLS", 0);
evt.xclient.format = 32;
evt.xclient.window = w->window_handler;
evt.xclient.data.l[0] = XInternAtom(app->display, "CHERRY_DISPOSE_ON_EXIT", 0);
XSendEvent(app->display, w->window_handler, 0, NoEventMask, &evt);
}
char *
cherry_window_get_title(CherryWindow *w)
{
char *wnd_name;
CherryApplication *app = cherry_application_get_running_app();
XFetchName(app->display, w->window_handler, &wnd_name);
return wnd_name;
}
void
cherry_window_set_title(CherryWindow *w, char *title)
{
w->title = strdup(title);
CherryApplication *app = cherry_application_get_running_app();
XStoreName(app->display, w->window_handler, w->title);
}
void
cherry_window_get_dimension(CherryWindow *window, int *width, int *height)
{
CherryWidget *widget = (CherryWidget *) window;
cherry_widget_get_dimension(widget, width, height);
}
void
cherry_window_set_dimension(CherryWindow *window, int width, int height)
{
CherryWidget *widget = (CherryWidget *) window;
cherry_widget_set_dimension(widget, width, height);
if (widget->drawn) {
int x, y;
cherry_widget_get_position(widget, &x, &y);
CherryApplication *app = cherry_application_get_running_app();
XMoveResizeWindow(app->display, window->window_handler,
x, y,
width, height);
}
}
void
cherry_window_get_position(CherryWindow *window, int *x, int *y)
{
CherryWidget *widget = (CherryWidget *) window;
cherry_widget_get_position(widget, x, y);
}
void
cherry_window_set_position(CherryWindow *window, int x, int y)
{
CherryWidget *widget = (CherryWidget *) window;
cherry_widget_set_position(widget, x, y);
if (widget->drawn) {
int width, height;
cherry_widget_get_dimension(widget, &width, &height);
CherryApplication *app = cherry_application_get_running_app();
XMoveResizeWindow(app->display, window->window_handler,
x, y,
width, height);
}
}
void
cherry_window_set_visible(CherryWindow *window, int visible)
{
CherryApplication *app = cherry_application_get_running_app();
window->draw((CherryWidget *) window);
if (visible) {
XMapRaised(app->display, window->window_handler);
}
}
void
cherry_window_set_listener(CherryWindow *w,
int (*listener)(struct CherryWindow *, CherryEvent))
{
w->listener = listener;
}
|