aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAlessandro Iezzi <aiezzi@alessandroiezzi.it>2023-05-22 00:18:46 +0200
committerAlessandro Iezzi <aiezzi@alessandroiezzi.it>2023-05-22 00:18:46 +0200
commit4612136849351e11eff84ad77d7d1f1b620a49a3 (patch)
treeeac0d60d04dc73a9c753030dedd0f75cce1e836b /src
parent200d91d042a73c5844bccebbd80b5bee4c8cf577 (diff)
downloadcherry-4612136849351e11eff84ad77d7d1f1b620a49a3.tar.gz
cherry-4612136849351e11eff84ad77d7d1f1b620a49a3.zip
Rewrite the test with new APIs
Diffstat (limited to 'src')
-rw-r--r--src/main.c174
1 files changed, 60 insertions, 114 deletions
diff --git a/src/main.c b/src/main.c
index 2e95378..07b3b92 100644
--- a/src/main.c
+++ b/src/main.c
@@ -7,6 +7,7 @@
#include <X11/Xutil.h>
#include "application.h"
+#include "event.h"
#include "window.h"
static void
@@ -15,129 +16,74 @@ app_deactivated(CherryApplication *app, void *data)
printf("Deactivated\n");
}
-static void
-app_activated(CherryApplication *app, void *data)
+int
+listener1(CherryWindow *w, CherryEvent evt)
{
- char hello[] = "Hello World!";
-
- 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_visible(w, 1);
-
-
- char hi[] = "hi!";
-
- Window mywindow;
-
- GC mygc;
-
- XEvent myevent;
- KeySym mykey;
-
- XSizeHints myhint;
-
- unsigned long myforeground, mybackground;
- int i;
- char text[10];
- int done;
-
- Display *mydisplay = app->display;
- int myscreen = app->screen;
- int depth = app->depth;
- Visual *visual = app->visual;
-
-
-
-
- XSetWindowAttributes attributes;
- attributes.background_pixel = XWhitePixel(mydisplay, myscreen);
-
- /* drawing contexts for an window */
- myforeground = BlackPixel(mydisplay, myscreen);
- mybackground = WhitePixel(mydisplay, myscreen);
- myhint.x = 200;
- myhint.y = 300;
- myhint.width = 350;
- myhint.height = 250;
- myhint.flags = PPosition|PSize;
+ char hello[] = "Example 1";
+ char hi[] = "Hi!";
- /* create window */
-// mywindow = XCreateSimpleWindow(mydisplay, DefaultRootWindow(mydisplay),
-// myhint.x, myhint.y,
-// myhint.width, myhint.height,
-// 5, myforeground, mybackground);
+ KeySym mykey;
+ char text[10];
+ int i;
- mywindow = XCreateWindow(mydisplay, XRootWindow(mydisplay, myscreen),
- myhint.x, myhint.y, myhint.width, myhint.height, 5, depth, InputOutput,
- visual ,CWBackPixel, &attributes);
-
- /* window manager properties (yes, use of StdProp is obsolete) */
- XSetStandardProperties(mydisplay, mywindow, hello, hello,
- None, NULL, 0, &myhint);
-
- /* graphics context */
- mygc = XCreateGC(mydisplay, mywindow, 0, 0);
- XSetBackground(mydisplay, mygc, mybackground);
- XSetForeground(mydisplay, mygc, myforeground);
-
- /* allow receiving mouse events */
- XSelectInput(mydisplay,mywindow,
- ButtonPressMask|KeyPressMask|ExposureMask);
-
- /* show up window */
- XMapRaised(mydisplay, mywindow);
+ 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;
+ }
- Atom wmDelete=XInternAtom(app->display, "WM_DELETE_WINDOW", True);
- XSetWMProtocols(app->display, mywindow, &wmDelete, 1);
+ return 0;
+}
- /* event loop */
- done = 0;
- while(done==0){
+ int
+listener2(CherryWindow *w, CherryEvent evt)
+{
+ switch (evt.event_id) {
+ case WINDOW_DELETED:
+ printf("Listener2\n");
+ break;
+ }
- /* fetch event */
- XNextEvent(mydisplay, &myevent);
+ return 0;
+}
- switch(myevent.type){
- case ClientMessage:
- printf("Closed window\n");
- done = 1;
- break;
+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);
- case Expose:
- /* Window was showed. */
- if(myevent.xexpose.count==0)
- XDrawImageString(myevent.xexpose.display,
- myevent.xexpose.window,
- mygc,
- 50, 50,
- hello, strlen(hello));
- break;
- case MappingNotify:
- /* Modifier key was up/down. */
- XRefreshKeyboardMapping(&myevent.xmapping);
- break;
- case ButtonPress:
- /* Mouse button was pressed. */
- XDrawImageString(myevent.xbutton.display,
- myevent.xbutton.window,
- mygc,
- myevent.xbutton.x, myevent.xbutton.y,
- hi, strlen(hi));
- break;
- case KeyPress:
- /* Key input. */
- i = XLookupString(&myevent.xkey, text, 10, &mykey, 0);
- if(i==1 && text[0]=='q') done = 1;
- break;
- }
- }
+ 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);
- /* finalization */
- XFreeGC(mydisplay,mygc);
- XDestroyWindow(mydisplay, mywindow);
-// XCloseDisplay(app->display);
+ /* show up window */
+ cherry_window_set_visible(w, 1);
+ cherry_window_set_visible(w2, 1);
}
int