commit 317ac4bf8168d5b1479ff4e0232704384e6e1df0
parent 696ff69d34503c5cba0e98287997c089ecd36b18
Author: lumidify <nobody@lumidify.org>
Date: Mon, 2 Jan 2017 18:04:06 +0100
Handle WM_DELETE_WINDOW
The program automatically exits when there are no windows left.
Diffstat:
6 files changed, 20 insertions(+), 4 deletions(-)
diff --git a/.window.c.swp b/.window.c.swp
Binary files differ.
diff --git a/event.c b/event.c
@@ -28,8 +28,8 @@ void ltk_handle_event(XEvent event)
LtkWindow *window;
LtkWidget *root_widget;
HASH_FIND_INT(ltk_global->window_hash, &event.xany.window, window);
+ if (!window) return;
root_widget = window->root_widget;
- if (!root_widget) return;
switch (event.type)
{
case KeyPress:
@@ -37,15 +37,16 @@ void ltk_handle_event(XEvent event)
case KeyRelease:
break;
case ButtonPress:
- ltk_mouse_press_event(root_widget, event);
+ if (root_widget) ltk_mouse_press_event(root_widget, event);
break;
case ButtonRelease:
- ltk_mouse_release_event(root_widget, event);
+ if (root_widget) ltk_mouse_release_event(root_widget, event);
break;
case MotionNotify:
- ltk_motion_notify_event(root_widget, event);
+ if (root_widget) ltk_motion_notify_event(root_widget, event);
break;
default:
+ /* FIXME: users should be able to register other events like closing the window */
if (window->other_event)
window->other_event(window, event);
}
diff --git a/ltk.c b/ltk.c
@@ -32,6 +32,7 @@ void ltk_init(const char *theme_path)
ltk->colormap = DefaultColormap(ltk->display, ltk->screen);
ltk->theme = ltk_load_theme(theme_path);
ltk->window_hash = NULL;
+ ltk->wm_delete_msg = XInternAtom(ltk->display, "WM_DELETE_WINDOW", False);
}
void ltk_clean_up(void)
diff --git a/ltk.h b/ltk.h
@@ -45,6 +45,7 @@ typedef struct
int screen;
Colormap colormap;
LtkWindow *window_hash;
+ Atom wm_delete_msg;
} Ltk;
Ltk *ltk_global;
diff --git a/test1.c b/test1.c
@@ -18,6 +18,7 @@ int main(int argc, char *argv[])
{
ltk_init("themes/default.json");
LtkWindow *window1 = ltk_create_window("Cool Window!", 0, 0, 500, 500);
+ LtkWindow *window2 = ltk_create_window("Cool Window!", 0, 0, 500, 500);
LtkGrid *grid1 = ltk_create_grid(window1, 2, 2);
window1->root_widget = grid1;
ltk_set_row_weight(grid1, 0, 1);
diff --git a/window.c b/window.c
@@ -63,6 +63,7 @@ LtkWindow *ltk_create_window(const char *title, int x, int y, unsigned int w, un
XSetForeground(display, window->gc, wtheme->fg.pixel);
XSetBackground(display, window->gc, wtheme->bg.pixel);
XSetStandardProperties(display, window->xwindow, title, NULL, None, NULL, 0, NULL);
+ XSetWMProtocols(display, window->xwindow, <k_global->wm_delete_msg, 1);
window->root_widget = NULL;
window->other_event = <k_window_other_event;
@@ -81,8 +82,15 @@ LtkWindow *ltk_create_window(const char *title, int x, int y, unsigned int w, un
return window;
}
+void ltk_remove_window(LtkWindow *window)
+{
+ ltk_destroy_window(window);
+ if (!ltk_global->window_hash) ltk_quit();
+}
+
void ltk_destroy_window(LtkWindow *window)
{
+ HASH_DEL(ltk_global->window_hash, window);
LtkWidget *ptr = window->root_widget;
if (ptr) ptr->destroy(ptr);
XDestroyWindow(ltk_global->display, window->xwindow);
@@ -112,6 +120,10 @@ void ltk_window_other_event(void *widget, XEvent event)
{
ltk_redraw_window(window);
}
+ if (event.type == ClientMessage && event.xclient.data.l[0] == ltk_global->wm_delete_msg)
+ {
+ ltk_remove_window(window);
+ }
}
/*