commit 626774f7dfbfa20af2cf3e093141f4da1a1e83d7
parent 8f2f0f5ff6fc903dfe04eca44a53419a319ca80a
Author: lumidify <nobody@lumidify.org>
Date: Tue, 7 Apr 2020 17:27:39 +0200
Various cleanups
Diffstat:
M | button.c | | | 10 | +++------- |
M | button.h | | | 6 | +++--- |
M | grid.c | | | 28 | ++++++++++------------------ |
M | grid.h | | | 26 | +++++++++++++------------- |
M | ltk.c | | | 64 | ++++++++++++++++++++++++++++++---------------------------------- |
M | ltk.h | | | 5 | ++--- |
6 files changed, 61 insertions(+), 78 deletions(-)
diff --git a/button.c b/button.c
@@ -141,9 +141,7 @@ LtkButton *ltk_create_button(LtkWindow *window, const char *text, void (*callbac
ltk_fatal("ERROR: Unable to allocate memory for LtkButton.\n");
}
- /* FIXME: shouldn't widget already have been allocated by allocating space for
- the whole button? */
- button->widget = ltk_create_widget(window, <k_draw_button, <k_destroy_button, 1);
+ ltk_fill_widget_defaults(&button->widget, window, <k_draw_button, <k_destroy_button, 1);
button->widget.mouse_release = <k_button_mouse_release;
button->callback = callback;
@@ -160,9 +158,8 @@ LtkButton *ltk_create_button(LtkWindow *window, const char *text, void (*callbac
return button;
}
-void ltk_destroy_button(void *widget)
+void ltk_destroy_button(LtkButton *button)
{
- LtkButton *button = (LtkButton *) widget;
if (!button) {
printf("WARNING: Tried to destroy NULL button.\n");
}
@@ -177,9 +174,8 @@ void ltk_destroy_button(void *widget)
/* FIXME: is the fixme below supposed to be for the function above? */
/* FIXME: ungrid button if gridded */
-void ltk_button_mouse_release(void *widget, XEvent event)
+void ltk_button_mouse_release(LtkButton *button, XEvent event)
{
- LtkButton *button = widget;
if (button->widget.state == LTK_HOVERACTIVE && button->callback) {
button->callback();
}
diff --git a/button.h b/button.h
@@ -59,12 +59,12 @@ typedef struct LtkButtonTheme {
XColor fill_disabled;
} LtkButtonTheme;
-void ltk_draw_button(LtkButton * button);
+void ltk_draw_button(LtkButton *button);
LtkButton *ltk_create_button(LtkWindow * window, const char *text, void (*callback) (void));
-void ltk_destroy_button(void *widget);
+void ltk_destroy_button(LtkButton *button);
-void ltk_button_mouse_release(void *widget, XEvent event);
+void ltk_button_mouse_release(LtkButton *button, XEvent event);
#endif
diff --git a/grid.c b/grid.c
@@ -48,13 +48,11 @@ void ltk_draw_grid(LtkGrid * grid)
}
}
-LtkGrid *ltk_create_grid(LtkWindow * window, int rows, int columns)
+LtkGrid *ltk_create_grid(LtkWindow *window, int rows, int columns)
{
LtkGrid *grid = malloc(sizeof(LtkGrid));
- grid->widget =
- ltk_create_widget(window, <k_draw_grid, <k_destroy_grid,
- 0);
+ ltk_fill_widget_defaults(&grid->widget, window, <k_draw_grid, <k_destroy_grid, 0);
grid->widget.mouse_press = <k_grid_mouse_press;
grid->widget.mouse_release = <k_grid_mouse_release;
grid->widget.motion_notify = <k_grid_motion_notify;
@@ -91,9 +89,8 @@ LtkGrid *ltk_create_grid(LtkWindow * window, int rows, int columns)
return grid;
}
-void ltk_destroy_grid(void *widget)
+void ltk_destroy_grid(LtkGrid *grid)
{
- LtkGrid *grid = widget;
LtkWidget *ptr;
int i;
for (i = 0; i < grid->rows * grid->columns; i++) {
@@ -112,9 +109,8 @@ void ltk_destroy_grid(void *widget)
free(grid);
}
-void ltk_recalculate_grid(void *widget)
+void ltk_recalculate_grid(LtkGrid *grid)
{
- LtkGrid *grid = widget;
unsigned int height_static = 0, width_static = 0;
unsigned int total_row_weight = 0, total_column_weight = 0;
float height_unit = 0, width_unit = 0;
@@ -197,9 +193,8 @@ void ltk_recalculate_grid(void *widget)
}
}
-void ltk_grid_widget(void *ptr, LtkGrid * grid, int row, int column, int row_span, int column_span, unsigned short sticky)
+void ltk_grid_widget(LtkWidget *widget, LtkGrid *grid, int row, int column, int row_span, int column_span, unsigned short sticky)
{
- LtkWidget *widget = ptr;
widget->sticky = sticky;
widget->row = row;
widget->column = column;
@@ -216,7 +211,7 @@ void ltk_grid_widget(void *ptr, LtkGrid * grid, int row, int column, int row_spa
ltk_recalculate_grid(grid);
}
-int ltk_grid_find_nearest_column(LtkGrid * grid, int x)
+int ltk_grid_find_nearest_column(LtkGrid *grid, int x)
{
int i;
for (i = 0; i < grid->columns; i++) {
@@ -227,7 +222,7 @@ int ltk_grid_find_nearest_column(LtkGrid * grid, int x)
return -1;
}
-int ltk_grid_find_nearest_row(LtkGrid * grid, int y)
+int ltk_grid_find_nearest_row(LtkGrid *grid, int y)
{
int i;
for (i = 0; i < grid->rows; i++) {
@@ -238,9 +233,8 @@ int ltk_grid_find_nearest_row(LtkGrid * grid, int y)
return -1;
}
-void ltk_grid_mouse_press(void *widget, XEvent event)
+void ltk_grid_mouse_press(LtkGrid *grid, XEvent event)
{
- LtkGrid *grid = widget;
int x = event.xbutton.x;
int y = event.xbutton.y;
int row = ltk_grid_find_nearest_row(grid, y);
@@ -253,9 +247,8 @@ void ltk_grid_mouse_press(void *widget, XEvent event)
}
}
-void ltk_grid_mouse_release(void *widget, XEvent event)
+void ltk_grid_mouse_release(LtkGrid *grid, XEvent event)
{
- LtkGrid *grid = widget;
int x = event.xbutton.x;
int y = event.xbutton.y;
int row = ltk_grid_find_nearest_row(grid, y);
@@ -273,9 +266,8 @@ void ltk_grid_mouse_release(void *widget, XEvent event)
}
}
-void ltk_grid_motion_notify(void *widget, XEvent event)
+void ltk_grid_motion_notify(LtkGrid *grid, XEvent event)
{
- LtkGrid *grid = widget;
short pressed = (event.xmotion.state & Button1Mask) == Button1Mask;
if (pressed)
return;
diff --git a/grid.h b/grid.h
@@ -33,7 +33,7 @@ typedef struct LtkGrid {
LtkWidget widget;
unsigned int rows;
unsigned int columns;
- void **widget_grid;
+ LtkWidget **widget_grid;
unsigned int *row_heights;
unsigned int *column_widths;
unsigned int *row_weights;
@@ -74,20 +74,20 @@ LtkGrid *ltk_create_grid(LtkWindow * window, int rows, int columns);
/*
* Destroy a grid.
- * widget: Pointer to the grid.
+ * grid: Pointer to the grid.
*/
-void ltk_destroy_grid(void *widget);
+void ltk_destroy_grid(LtkGrid *grid);
/*
* Recalculate the positions and dimensions of the
* columns, rows, and widgets in a grid.
- * widget: Pointer to the grid.
+ * grid: Pointer to the grid.
*/
-void ltk_recalculate_grid(void *widget);
+void ltk_recalculate_grid(LtkGrid *grid);
/*
* Grid a widget.
- * ptr: Pointer to the widget.
+ * widget: Pointer to the widget.
* grid: The grid.
* row: The row to grid the widget in.
* column: The column to grid the widget in.
@@ -95,28 +95,28 @@ void ltk_recalculate_grid(void *widget);
* columnspan: The amount of columns the widget should span.
* sticky: Mask of the sticky values (LTK_STICKY_*).
*/
-void ltk_grid_widget(void *ptr, LtkGrid * grid, int row, int column,
+void ltk_grid_widget(LtkWidget *widget, LtkGrid *grid, int row, int column,
int rowspan, int columnspan, unsigned short sticky);
/*
* Delegate a mouse press event on the grid to the proper widget.
- * widget: The grid.
+ * grid: The grid.
* event: The event to be handled.
*/
-void ltk_grid_mouse_press(void *widget, XEvent event);
+void ltk_grid_mouse_press(LtkGrid *grid, XEvent event);
/*
* Delegate a mouse release event on the grid to the proper widget.
- * widget: The grid.
+ * grid: The grid.
* event: The event to be handled.
*/
-void ltk_grid_mouse_release(void *widget, XEvent event);
+void ltk_grid_mouse_release(LtkGrid *grid, XEvent event);
/*
* Delegate a mouse motion event on the grid to the proper widget.
- * widget: The grid.
+ * grid: The grid.
* event: The event to be handled.
*/
-void ltk_grid_motion_notify(void *widget, XEvent event);
+void ltk_grid_motion_notify(LtkGrid *grid, XEvent event);
#endif
diff --git a/ltk.c b/ltk.c
@@ -335,41 +335,37 @@ void ltk_remove_hover_widget(void *widget)
}
}
-LtkWidget ltk_create_widget(LtkWindow *window, void (*draw) (void *),
- void (*destroy) (void *),
- unsigned int needs_redraw)
+void ltk_fill_widget_defaults(LtkWidget *widget, LtkWindow *window,
+ void (*draw) (void *), void (*destroy) (void *), unsigned int needs_redraw)
{
- LtkWidget widget;
- widget.window = window;
- widget.active_widget = NULL;
- widget.hover_widget = NULL;
- widget.parent = NULL;
-
- widget.key_press = NULL;
- widget.key_release = NULL;
- widget.mouse_press = NULL;
- widget.mouse_release = NULL;
- widget.motion_notify = NULL;
-
- widget.resize = NULL;
- widget.draw = draw;
- widget.destroy = destroy;
-
- widget.needs_redraw = needs_redraw;
- widget.state = LTK_NORMAL;
- widget.row = 0;
- widget.rect.x = 0;
- widget.rect.y = 0;
- widget.rect.w = 100;
- widget.rect.h = 100;
-
- widget.row = NULL;
- widget.column = NULL;
- widget.row_span = NULL;
- widget.column_span = NULL;
- widget.sticky = 0;
-
- return widget;
+ widget->window = window;
+ widget->active_widget = NULL;
+ widget->hover_widget = NULL;
+ widget->parent = NULL;
+
+ widget->key_press = NULL;
+ widget->key_release = NULL;
+ widget->mouse_press = NULL;
+ widget->mouse_release = NULL;
+ widget->motion_notify = NULL;
+
+ widget->resize = NULL;
+ widget->draw = draw;
+ widget->destroy = destroy;
+
+ widget->needs_redraw = needs_redraw;
+ widget->state = LTK_NORMAL;
+ widget->row = 0;
+ widget->rect.x = 0;
+ widget->rect.y = 0;
+ widget->rect.w = 100;
+ widget->rect.h = 100;
+
+ widget->row = NULL;
+ widget->column = NULL;
+ widget->row_span = NULL;
+ widget->column_span = NULL;
+ widget->sticky = 0;
}
void ltk_mouse_press_event(void *widget, XEvent event)
diff --git a/ltk.h b/ltk.h
@@ -154,9 +154,8 @@ void ltk_remove_active_widget(void *widget);
void ltk_remove_hover_widget(void *widget);
-LtkWidget ltk_create_widget(LtkWindow * window, void (*draw) (void *),
- void (*destroy) (void *),
- unsigned int needs_redraw);
+void ltk_fill_widget_defaults(LtkWidget *widget, LtkWindow * window,
+ void (*draw) (void *), void (*destroy) (void *), unsigned int needs_redraw);
void ltk_mouse_press_event(void *widget, XEvent event);