window: Get rid of the window child allocation concept
authorKristian Høgsberg <krh@bitplanet.net>
Wed, 11 Jan 2012 00:11:42 +0000 (19:11 -0500)
committerKristian Høgsberg <krh@bitplanet.net>
Wed, 11 Jan 2012 00:11:42 +0000 (19:11 -0500)
12 files changed:
clients/desktop-shell.c
clients/dnd.c
clients/eventdemo.c
clients/gears.c
clients/image.c
clients/resizor.c
clients/tablet-shell.c
clients/terminal.c
clients/view.c
clients/window.c
clients/window.h
clients/wscreensaver.c

index 49aa013..09fc1c9 100644 (file)
@@ -393,9 +393,7 @@ background_configure(void *data,
        struct background *background =
                (struct background *) window_get_user_data(window);
 
-       window_set_child_size(background->window, width, height);
-       widget_set_allocation(background->widget, 0, 0, width, height);
-       window_schedule_redraw(background->window);
+       widget_schedule_resize(background->widget, width, height);
 }
 
 static void
@@ -410,7 +408,7 @@ unlock_dialog_redraw_handler(struct widget *widget, void *data)
 
        surface = window_get_surface(dialog->window);
        cr = cairo_create(surface);
-       window_get_child_allocation(dialog->window, &allocation);
+       widget_get_allocation(dialog->widget, &allocation);
        cairo_rectangle(cr, allocation.x, allocation.y,
                        allocation.width, allocation.height);
        cairo_clip(cr);
index 0498c5d..27b11c0 100644 (file)
@@ -170,7 +170,7 @@ dnd_redraw_handler(struct widget *widget, void *data)
 
        surface = window_get_surface(dnd->window);
        cr = cairo_create(surface);
-       window_get_child_allocation(dnd->window, &allocation);
+       widget_get_allocation(dnd->widget, &allocation);
        cairo_rectangle(cr, allocation.x, allocation.y,
                        allocation.width, allocation.height);
 
@@ -222,7 +222,7 @@ dnd_get_item(struct dnd *dnd, int32_t x, int32_t y)
        struct rectangle allocation;
        int i;
 
-       window_get_child_allocation(dnd->window, &allocation);
+       widget_get_allocation(dnd->widget, &allocation);
 
        x -= allocation.x;
        y -= allocation.y;
@@ -363,7 +363,7 @@ dnd_button_handler(struct widget *widget,
        struct dnd_drag *dnd_drag;
        int i;
 
-       window_get_child_allocation(dnd->window, &allocation);
+       widget_get_allocation(dnd->widget, &allocation);
        input_get_position(input, &x, &y);
        item = dnd_get_item(dnd, x, y);
        x -= allocation.x;
@@ -468,7 +468,7 @@ dnd_receive_func(void *data, size_t len, int32_t x, int32_t y, void *user_data)
                return;
        }
                
-       window_get_child_allocation(dnd->window, &allocation);
+       widget_get_allocation(dnd->widget, &allocation);
        item = item_create(dnd->display,
                           x - message->x_offset - allocation.x,
                           y - message->y_offset - allocation.y,
@@ -535,9 +535,8 @@ dnd_create(struct display *display)
 
        width = 4 * (item_width + item_padding) + item_padding;
        height = 4 * (item_height + item_padding) + item_padding;
-       window_set_child_size(dnd->window, width, height);
 
-       window_schedule_redraw(dnd->window);
+       widget_schedule_resize(dnd->widget, width, height);
 
        return dnd;
 }
index 5f70bc1..b8cb344 100644 (file)
@@ -107,7 +107,7 @@ redraw_handler(struct widget *widget, void *data)
        if (log_redraw)
                printf("redraw\n");
 
-       window_get_child_allocation(e->window, &rect);
+       widget_get_allocation(e->widget, &rect);
        surface = window_get_surface(e->window);
 
        cr = cairo_create(surface);
@@ -150,7 +150,7 @@ resize_handler(struct widget *widget,
                height = height_max;
 
        /* set the new window dimensions */
-       window_set_child_size(e->window, width, height);
+       widget_set_size(e->widget, width, height);
 }
 
 /**
@@ -266,12 +266,6 @@ eventdemo_create(struct display *d)
        if(e == NULL)
                return NULL;
 
-       /* Creates a new window with the given title, width and height.
-        * To set the size of the window for a given usable areas width
-        * and height in a window decoration agnostic way use:
-        *      window_set_child_size(struct window *window,
-        *                            int32_t width, int32_t height);
-        */
        e->window = window_create(d, width, height);
        e->widget = window_add_widget(e->window, e);
        window_set_title(e->window, title);
index 5dfb47a..e94152d 100644 (file)
@@ -221,7 +221,7 @@ redraw_handler(struct widget *widget, void *data)
        struct wl_callback *callback;
        struct gears *gears = data;
 
-       window_get_child_allocation(gears->window, &allocation);
+       widget_get_allocation(gears->widget, &allocation);
        window_get_allocation(gears->window, &window_allocation);
 
        if (display_acquire_window_surface(gears->d,
@@ -232,7 +232,7 @@ redraw_handler(struct widget *widget, void *data)
        }
        
        glViewport(allocation.x,
-                  window_allocation.height - allocation.height - allocation.x,
+                  window_allocation.height - allocation.height - allocation.y,
                   allocation.width, allocation.height);
        glScissor(allocation.x,
                  window_allocation.height - allocation.height - allocation.y,
@@ -293,7 +293,7 @@ resize_handler(struct widget *widget,
                height = 300;
        }
 
-       window_set_child_size(gears->window, width, height);
+       widget_set_size(gears->widget, width, height);
 }
 
 static void
@@ -361,7 +361,7 @@ gears_create(struct display *display)
        window_set_keyboard_focus_handler(gears->window,
                                          keyboard_focus_handler);
 
-       frame_callback(gears, NULL, 0);
+       window_schedule_resize(gears->window, width, height);
 
        return gears;
 }
index 517bb47..71dd897 100644 (file)
@@ -142,7 +142,7 @@ redraw_handler(struct widget *widget, void *data)
        cairo_t *cr;
        cairo_surface_t *surface;
 
-       window_get_child_allocation(image->window, &allocation);
+       widget_get_allocation(image->widget, &allocation);
 
        pb = gdk_pixbuf_new_from_file_at_size(image->filename,
                                              allocation.width,
@@ -153,7 +153,7 @@ redraw_handler(struct widget *widget, void *data)
 
        surface = window_get_surface(image->window);
        cr = cairo_create(surface);
-       window_get_child_allocation(image->window, &allocation);
+       widget_get_allocation(image->widget, &allocation);
        cairo_rectangle(cr, allocation.x, allocation.y,
                        allocation.width, allocation.height);
        cairo_clip(cr);
@@ -215,7 +215,7 @@ image_create(struct display *display, const char *filename)
        window_set_keyboard_focus_handler(image->window,
                                          keyboard_focus_handler);
 
-       window_schedule_redraw(image->window);
+       widget_schedule_resize(image->widget, 500, 400);
 
        return image;
 }
index 945f2ae..7699bed 100644 (file)
@@ -53,9 +53,6 @@ struct resizor {
 static void
 frame_callback(void *data, struct wl_callback *callback, uint32_t time)
 {
-       static const struct wl_callback_listener listener = {
-               frame_callback
-       };
        struct resizor *resizor = data;
        double force, height;
 
@@ -79,24 +76,18 @@ frame_callback(void *data, struct wl_callback *callback, uint32_t time)
                resizor->height.previous = 200;
        }
 
-       window_set_child_size(resizor->window, resizor->width, height + 0.5);
-
-       window_schedule_redraw(resizor->window);
+       widget_schedule_resize(resizor->widget, resizor->width, height + 0.5);
 
        if (resizor->frame_callback) {
                wl_callback_destroy(resizor->frame_callback);
                resizor->frame_callback = NULL;
        }
-
-       if (fabs(resizor->height.previous - resizor->height.target) > 0.1) {
-               resizor->frame_callback =
-                       wl_surface_frame(
-                               window_get_wl_surface(resizor->window));
-               wl_callback_add_listener(resizor->frame_callback, &listener,
-                                        resizor);
-       }
 }
 
+static const struct wl_callback_listener listener = {
+       frame_callback
+};
+
 static void
 redraw_handler(struct widget *widget, void *data)
 {
@@ -105,7 +96,7 @@ redraw_handler(struct widget *widget, void *data)
        cairo_t *cr;
        struct rectangle allocation;
 
-       window_get_child_allocation(resizor->window, &allocation);
+       widget_get_allocation(resizor->widget, &allocation);
 
        surface = window_get_surface(resizor->window);
 
@@ -121,6 +112,15 @@ redraw_handler(struct widget *widget, void *data)
        cairo_destroy(cr);
 
        cairo_surface_destroy(surface);
+
+       if (fabs(resizor->height.previous - resizor->height.target) > 0.1) {
+               resizor->frame_callback =
+                       wl_surface_frame(
+                               window_get_wl_surface(resizor->window));
+               wl_callback_add_listener(resizor->frame_callback, &listener,
+                                        resizor);
+       }
+
 }
 
 static void
@@ -222,10 +222,9 @@ resizor_create(struct display *display)
        resizor->height.target = resizor->height.current;
        height = resizor->height.current + 0.5;
 
-       window_set_child_size(resizor->window, resizor->width, height);
        widget_set_button_handler(resizor->widget, button_handler);
 
-       window_schedule_redraw(resizor->window);
+       widget_schedule_resize(resizor->widget, resizor->width, height);
 
        return resizor;
 }
index 2331e68..1f73ba1 100644 (file)
@@ -122,7 +122,7 @@ homescreen_draw(struct tablet_shell *shell)
        int x, y, i, width, height, vmargin, hmargin, vpadding, hpadding;
 
        window_create_surface(shell->homescreen);
-       window_get_child_allocation(shell->homescreen, &allocation);
+       window_get_allocation(shell->homescreen, &allocation);
        surface = window_get_surface(shell->homescreen);
        cr = cairo_create(surface);
 
@@ -176,7 +176,7 @@ lockscreen_draw(struct tablet_shell *shell)
        int width, height;
 
        window_create_surface(shell->lockscreen);
-       window_get_child_allocation(shell->lockscreen, &allocation);
+       window_get_allocation(shell->lockscreen, &allocation);
        surface = window_get_surface(shell->lockscreen);
        cr = cairo_create(surface);
 
index b6e3139..06a7a1f 100644 (file)
@@ -499,7 +499,7 @@ terminal_compare_position(struct terminal *terminal,
        struct rectangle allocation;
        int top_margin, side_margin, col, row, ref_x;
 
-       window_get_child_allocation(terminal->window, &allocation);
+       widget_get_allocation(terminal->widget, &allocation);
        side_margin = allocation.x + (allocation.width - terminal->width * terminal->extents.max_x_advance) / 2;
        top_margin = allocation.y + (allocation.height - terminal->height * terminal->extents.height) / 2;
 
@@ -694,7 +694,7 @@ terminal_shift_line(struct terminal *terminal, int d)
 }
 
 static void
-terminal_resize(struct terminal *terminal, int width, int height)
+terminal_resize_cells(struct terminal *terminal, int width, int height)
 {
        size_t size;
        union utf8_char *data;
@@ -704,7 +704,6 @@ terminal_resize(struct terminal *terminal, int width, int height)
        int i, l, total_rows;
        struct rectangle allocation;
        struct winsize ws;
-       int32_t pixel_width, pixel_height;
 
        if (width < 1)
                width = 1;
@@ -713,15 +712,6 @@ terminal_resize(struct terminal *terminal, int width, int height)
        if (terminal->width == width && terminal->height == height)
                return;
 
-       if (!terminal->fullscreen) {
-               pixel_width = width *
-                       terminal->extents.max_x_advance + 2 * terminal->margin;
-               pixel_height = height *
-                       terminal->extents.height + 2 * terminal->margin;
-               window_set_child_size(terminal->window,
-                                     pixel_width, pixel_height);
-       }
-
        data_pitch = width * sizeof(union utf8_char);
        size = data_pitch * height;
        data = malloc(size);
@@ -771,12 +761,46 @@ terminal_resize(struct terminal *terminal, int width, int height)
        /* Update the window size */
        ws.ws_row = terminal->height;
        ws.ws_col = terminal->width;
-       window_get_child_allocation(terminal->window, &allocation);
+       widget_get_allocation(terminal->widget, &allocation);
        ws.ws_xpixel = allocation.width;
        ws.ws_ypixel = allocation.height;
        ioctl(terminal->master, TIOCSWINSZ, &ws);
 }
 
+static void
+resize_handler(struct widget *widget,
+              int32_t width, int32_t height, void *data)
+{
+       struct terminal *terminal = data;
+       int32_t columns, rows, m;
+
+       m = 2 * terminal->margin;
+       columns = (width - m) / (int32_t) terminal->extents.max_x_advance;
+       rows = (height - m) / (int32_t) terminal->extents.height;
+
+       if (!terminal->fullscreen) {
+               width = columns * terminal->extents.max_x_advance + m;
+               height = rows * terminal->extents.height + m;
+               widget_set_size(terminal->widget, width, height);
+       }
+
+       terminal_resize_cells(terminal, columns, rows);
+}
+
+static void
+terminal_resize(struct terminal *terminal, int columns, int rows)
+{
+       int32_t width, height, m;
+
+       if (terminal->fullscreen)
+               return;
+
+       m = 2 * terminal->margin;
+       width = columns * terminal->extents.max_x_advance + m;
+       height = rows * terminal->extents.height + m;
+       widget_schedule_resize(terminal->widget, width, height);
+}
+
 struct color_scheme DEFAULT_COLORS = {
        {
                {0,    0,    0,    1}, /* black */
@@ -915,7 +939,7 @@ redraw_handler(struct widget *widget, void *data)
        cairo_font_extents_t extents;
 
        surface = window_get_surface(terminal->window);
-       window_get_child_allocation(terminal->window, &allocation);
+       widget_get_allocation(terminal->widget, &allocation);
        cr = cairo_create(surface);
        cairo_rectangle(cr, allocation.x, allocation.y,
                        allocation.width, allocation.height);
@@ -1011,25 +1035,6 @@ terminal_write(struct terminal *terminal, const char *data, size_t length)
 }
 
 static void
-resize_handler(struct widget *widget,
-              int32_t pixel_width, int32_t pixel_height, void *data)
-{
-       struct terminal *terminal = data;
-       int32_t width, height;
-
-       width = (pixel_width - 2 * terminal->margin) /
-               (int32_t) terminal->extents.max_x_advance;
-       height = (pixel_height - 2 * terminal->margin) /
-               (int32_t) terminal->extents.height;
-
-       if (terminal->fullscreen)
-               window_set_child_size(terminal->window,
-                                     pixel_width, pixel_height);
-
-       terminal_resize(terminal, width, height);
-}
-
-static void
 terminal_data(struct terminal *terminal, const char *data, size_t length);
 
 static void
@@ -1488,10 +1493,8 @@ handle_escape(struct terminal *terminal)
                switch (args[0]) {
                case 4:  /* resize px */
                        if (set[1] && set[2]) {
-                               window_set_child_size(terminal->window,
-                                                     args[2], args[1]);
-                               resize_handler(terminal->widget,
-                                              args[2], args[1], terminal);
+                               widget_schedule_resize(terminal->widget,
+                                                      args[2], args[1]);
                        }
                        break;
                case 8:  /* resize ch */
@@ -1500,13 +1503,13 @@ handle_escape(struct terminal *terminal)
                        }
                        break;
                case 13: /* report position */
-                       window_get_child_allocation(terminal->window, &allocation);
+                       widget_get_allocation(terminal->widget, &allocation);
                        snprintf(response, MAX_RESPONSE, "\e[3;%d;%dt",
                                 allocation.x, allocation.y);
                        terminal_write(terminal, response, strlen(response));
                        break;
                case 14: /* report px */
-                       window_get_child_allocation(terminal->window, &allocation);
+                       widget_get_allocation(terminal->widget, &allocation);
                        snprintf(response, MAX_RESPONSE, "\e[4;%d;%dt",
                                 allocation.height, allocation.width);
                        terminal_write(terminal, response, strlen(response));
@@ -2354,8 +2357,6 @@ terminal_run(struct terminal *terminal, const char *path)
        if (!terminal->fullscreen)
                terminal_resize(terminal, 80, 24);
 
-       window_schedule_redraw(terminal->window);
-
        return 0;
 }
 
index 6c12f97..fd8b056 100644 (file)
@@ -66,7 +66,7 @@ view_draw(struct view *view)
 
        window_draw(view->window);
 
-       window_get_child_allocation(view->window, &allocation);
+       widget_get_allocation(view->widget, &allocation);
 
        surface = window_get_surface(view->window);
 
@@ -124,8 +124,7 @@ resize_handler(struct widget *widget,
 {
        struct view *view = data;
 
-       window_set_child_size(view->window, width, height);
-       window_schedule_redraw(window);
+       widget_set_size(view->widget, width, height);
 }
 
 static void
index c81aed3..492d951 100644 (file)
@@ -1112,6 +1112,26 @@ widget_get_allocation(struct widget *widget, struct rectangle *allocation)
 }
 
 void
+widget_set_size(struct widget *widget, int32_t width, int32_t height)
+{
+       struct window *window = widget->window;
+
+       widget->allocation.width = width;
+       widget->allocation.height = height;
+
+       window->allocation.x = 0;
+       window->allocation.y = 0;
+       if (widget->window->decoration) {
+               window->allocation.width = width + 20 + widget->window->margin;
+               window->allocation.height =
+                       height + 60 + widget->window->margin;
+       } else {
+               window->allocation.width = width;
+               window->allocation.height = height;
+       }
+}
+
+void
 widget_set_allocation(struct widget *widget,
                      int32_t x, int32_t y, int32_t width, int32_t height)
 {
@@ -1911,12 +1931,17 @@ static void
 window_resize(struct window *window, int32_t width, int32_t height)
 {
        struct rectangle allocation;
+       struct widget *widget;
+       int decoration_width, decoration_height;
+
+       decoration_width = 20 + window->margin * 2;
+       decoration_height = 60 + window->margin * 2;
 
        if (window->decoration) {
-               allocation.x = 20;
-               allocation.y = 60;
-               allocation.width = width - 20 - window->margin * 2;
-               allocation.height = height - 60 - window->margin * 2;
+               allocation.x = 10 + window->margin;
+               allocation.y = 50 + window->margin;
+               allocation.width = width - decoration_width;
+               allocation.height = height - decoration_height;
        } else {
                allocation.x = 0;
                allocation.y = 0;
@@ -1924,17 +1949,26 @@ window_resize(struct window *window, int32_t width, int32_t height)
                allocation.height = height;
        }
 
-       window->allocation.width = width;
-       window->allocation.height = height;
-
-       widget_set_allocation(window->widget, allocation.x, allocation.y,
+       widget = window->widget;
+       widget_set_allocation(widget, allocation.x, allocation.y,
                              allocation.width, allocation.height);
 
-       if (window->widget->resize_handler)
-               window->widget->resize_handler(window->widget,
-                                              allocation.width,
-                                              allocation.height,
-                                              window->widget->user_data);
+       if (widget->resize_handler)
+               widget->resize_handler(widget,
+                                      allocation.width,
+                                      allocation.height,
+                                      widget->user_data);
+
+       if (window->decoration) {
+               window->allocation.x = 0;
+               window->allocation.y = 0;
+               window->allocation.width =
+                       widget->allocation.width + decoration_width;
+               window->allocation.height =
+                       widget->allocation.height + decoration_height;
+       } else {
+               window->allocation = widget->allocation;
+       }
 
        window_schedule_redraw(window);
 }
@@ -1962,6 +1996,21 @@ window_schedule_resize(struct window *window, int width, int height)
        }
 }
 
+void
+widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
+{
+       struct window *window = widget->window;
+
+       if (widget->window->decoration) {
+               window_schedule_resize(window,
+                                      width + 20 + 2 * window->margin,
+                                      height + 60 + 2 * window->margin);
+
+       } else {
+               window_schedule_resize(window, width, height);
+       }
+}
+
 static void
 handle_configure(void *data, struct wl_shell_surface *shell_surface,
                 uint32_t time, uint32_t edges,
@@ -2001,38 +2050,6 @@ window_get_allocation(struct window *window,
        *allocation = window->allocation;
 }
 
-void
-window_get_child_allocation(struct window *window,
-                           struct rectangle *allocation)
-{
-       if (!window->decoration) {
-               *allocation = window->allocation;
-       } else {
-               allocation->x = window->margin + 10;
-               allocation->y = window->margin + 50;
-               allocation->width =
-                       window->allocation.width - 20 - window->margin * 2;
-               allocation->height =
-                       window->allocation.height - 60 - window->margin * 2;
-       }
-}
-
-void
-window_set_child_size(struct window *window, int32_t width, int32_t height)
-{
-       if (window->decoration) {
-               window->allocation.x = 20 + window->margin;
-               window->allocation.y = 60 + window->margin;
-               window->allocation.width = width + 20 + window->margin * 2;
-               window->allocation.height = height + 60 + window->margin * 2;
-       } else {
-               window->allocation.x = 0;
-               window->allocation.y = 0;
-               window->allocation.width = width;
-               window->allocation.height = height;
-       }
-}
-
 static void
 widget_redraw(struct widget *widget)
 {
index 0bc7e5a..3252c89 100644 (file)
@@ -222,28 +222,13 @@ struct widget *
 window_get_focus_widget(struct window *window);
 struct display *
 window_get_display(struct window *window);
-struct widget *
-window_get_widget(struct window *window);
-
 void
 window_move(struct window *window, struct input *input, uint32_t time);
-
 void
-window_draw(struct window *window);
-
-void
-window_get_allocation(struct window *window,
-                     struct rectangle *allocation);
-
-void
-window_get_child_allocation(struct window *window,
-                           struct rectangle *allocation);
-
+window_get_allocation(struct window *window, struct rectangle *allocation);
 void
 window_set_transparent(struct window *window, int transparent);
 void
-window_set_child_size(struct window *window, int32_t width, int32_t height);
-void
 window_schedule_redraw(struct window *window);
 void
 window_schedule_resize(struct window *window, int width, int height);
@@ -334,6 +319,10 @@ widget_get_allocation(struct widget *widget, struct rectangle *allocation);
 void
 widget_set_allocation(struct widget *widget,
                      int32_t x, int32_t y, int32_t width, int32_t height);
+void
+widget_set_size(struct widget *widget, int32_t width, int32_t height);
+void
+widget_schedule_resize(struct widget *widget, int32_t width, int32_t height);
 
 void *
 widget_get_user_data(struct widget *widget);
index d283125..308019b 100644 (file)
@@ -90,7 +90,7 @@ redraw_handler(struct widget *widget, void *data)
 
        mi->swap_buffers = 0;
 
-       window_get_child_allocation(mi->window, &drawarea);
+       widget_get_allocation(mi->widget, &drawarea);
        window_get_allocation(mi->window, &winarea);
 
        if (display_acquire_window_surface(wscr->display,
@@ -182,7 +182,7 @@ create_modeinfo(struct wscreensaver *wscr, struct window *window)
        if (!mi)
                return NULL;
 
-       window_get_child_allocation(window, &drawarea);
+       window_get_allocation(window, &drawarea);
 
        mi->priv = wscr;
        mi->eglctx = EGL_NO_CONTEXT;