aboutsummaryrefslogtreecommitdiff
path: root/test/test1.c
blob: c2bbfacaf1ac1e648a1680e27f2b91e8a0eeca5f (plain)
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
/* 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 <cherry/application.h>
#include <cherry/event.h>
#include <cherry/window.h>

static void
app_deactivated(CherryApplication *app, void *data)
{
	printf("Deactivated\n");
}

int
listener1(CherryWindow *w, CherryEvent evt)
{
	char hello[] = "Example 1";
	char hi[] = "Hi!";

	KeySym mykey;
	char text[10];
	int i;

	switch (evt.event_id) {
		case WINDOW_DELETED:
			printf("Listener1\n");
			cherry_window_dispose_on_exit(w);
			break;
		case WINDOW_EXPOSED:
			XDrawImageString(evt.display,
			                 evt.window,
			                 w->gc,
			                 50, 50,
			                 hello, strlen(hello));
			break;
		case MOUSE_BUTTON_PRESSED:
			XDrawImageString(evt.display,
			                 evt.window,
			                 w->gc,
			                 evt.mouse.x, evt.mouse.y,
			                 hi, strlen(hi));
			break;
		case KEY_PRESSED:
			i = XLookupString(&evt.key.xkey, text, 10, &mykey, 0);
			if (i == 1 && text[0] == 'q') cherry_window_dispose_on_exit(w);
			break;
	}

	return 0;
}

  int
listener2(CherryWindow *w, CherryEvent evt)
{
	switch (evt.event_id) {
		case WINDOW_DELETED:
			printf("Listener2\n");
			break;
	}

	return 0;
}

static void
app_activated(CherryApplication *app, void *data)
{
	CherryWindow *w = cherry_window_new();
	cherry_window_set_title(w, "Hello from another World!");
	cherry_window_set_dimension(w, 350, 250);
	cherry_window_set_position(w, 200, 300);
	cherry_window_set_listener(w, listener1);

	CherryWindow *w2 = cherry_window_new();
	cherry_window_set_title(w2, "The second window");
	cherry_window_set_dimension(w2, 350, 250);
	cherry_window_set_position(w2, 500, 300);
	cherry_window_set_listener(w2, listener2);

	/* show up window */
	cherry_window_set_visible(w, 1);
	cherry_window_set_visible(w2, 1);
}

int
test1(int argc, char **argv)
{
	puts("Running test1...");

	CherryApplication *app = cherry_application_new("Just a test!");
	cherry_application_set_activated_listener(app, app_activated, NULL);
	cherry_application_set_deactivated_listener(app, app_deactivated, NULL);

	return cherry_application_run(app, argc, argv);
}