commit 9ad6cdddb14e3bc4594142edb39360a6588c9660
parent 323ceb5cbc54b4d70002a4f4e6e5ae9d308793c4
Author: lumidify <nobody@lumidify.org>
Date: Sun, 12 Aug 2018 21:36:36 +0200
Improve positioning of text on button
Diffstat:
5 files changed, 38 insertions(+), 81 deletions(-)
diff --git a/NOTES b/NOTES
@@ -4,3 +4,4 @@ Maybe use XCheckWindowEvent? - this would eliminate need for hash (could just st
HarfBuzz - need to add option to LTK functions to allow user to choose language system manually
LtkTextSegment - just use array instead of linked list - just need to get actual number of glyphs in advance from hb
Add void* to LtkWidget to hold specific widget instead of doing weird casts to simulate OOP
+When the screen size is set differently by the window manager on startup, the original drawing stays on screen - any way to fix this?
diff --git a/button.c b/button.c
@@ -31,14 +31,8 @@ void ltk_button_ini_handler(LtkTheme *theme, const char *prop, const char *value
theme->button->border_width = atoi(value);
} else if (strcmp(prop, "font_size") == 0) {
theme->button->font_size = atoi(value);
- } else if (strcmp(prop, "padl") == 0) {
- theme->button->padl = atoi(value);
- } else if (strcmp(prop, "padr") == 0) {
- theme->button->padr = atoi(value);
- } else if (strcmp(prop, "padt") == 0) {
- theme->button->padt = atoi(value);
- } else if (strcmp(prop, "padb") == 0) {
- theme->button->padb = atoi(value);
+ } else if (strcmp(prop, "pad") == 0) {
+ theme->button->pad = atoi(value);
} else if (strcmp(prop, "border") == 0) {
theme->button->border = ltk_create_xcolor(value);
} else if (strcmp(prop, "fill") == 0) {
@@ -71,9 +65,11 @@ void ltk_draw_button(LtkButton *button)
LtkButtonTheme *theme = ltk_global->theme->button;
LtkWindow *window = button->widget.window;
LtkRect rect = button->widget.rect;
+ int bw = theme->border_width;
XColor border;
XColor fill;
XImage *img;
+ int text_x, text_y;
switch (button->widget.state) {
case LTK_NORMAL:
border = theme->border;
@@ -107,10 +103,10 @@ void ltk_draw_button(LtkButton *button)
XSetForeground(ltk_global->display, window->gc, fill.pixel);
XFillRectangle(ltk_global->display, window->xwindow, window->gc, rect.x, rect.y, rect.w, rect.h);
/* FIXME: Why did I do this? */
- if (theme->border_width < 1) return;
+ if (bw < 1) return;
XSetForeground(ltk_global->display, window->gc, border.pixel);
- XSetLineAttributes(ltk_global->display, window->gc, theme->border_width, LineSolid, CapButt, JoinMiter);
- XDrawRectangle(ltk_global->display, window->xwindow, window->gc, rect.x, rect.y, rect.w, rect.h);
+ XSetLineAttributes(ltk_global->display, window->gc, bw, LineSolid, CapButt, JoinMiter);
+ XDrawRectangle(ltk_global->display, window->xwindow, window->gc, rect.x + bw / 2, rect.y + bw / 2, rect.w - bw, rect.h - bw);
if (!img) {
img = ltk_render_text_segment(button->ts, ltk_global->display, window->xwindow, window->gc, ltk_global->colormap, theme->text_color, fill);
/* FIXME: any nicer way to do this? */
@@ -133,7 +129,9 @@ void ltk_draw_button(LtkButton *button)
break;
}
}
- XPutImage(ltk_global->display, window->xwindow, window->gc, img, 0, 0, rect.x + theme->border_width, rect.y + theme->border_width, button->ts->w, button->ts->h);
+ text_x = rect.x + (rect.w - button->ts->w) / 2;
+ text_y = rect.y + (rect.h - button->ts->h) / 2;
+ XPutImage(ltk_global->display, window->xwindow, window->gc, img, 0, 0, text_x, text_y, button->ts->w, button->ts->h);
}
LtkButton *ltk_create_button(LtkWindow *window, const char *text,
@@ -151,8 +149,8 @@ LtkButton *ltk_create_button(LtkWindow *window, const char *text,
button->callback = callback;
LtkTheme *theme = ltk_global->theme;
button->ts = ltk_create_text_segment(ltk_global->tm, text, ltk_global->default_font, theme->button->font_size);
- button->widget.rect.w = button->ts->w + theme->button->border_width * 2;
- button->widget.rect.h = button->ts->h + theme->button->border_width * 2;
+ button->widget.rect.w = button->ts->w + (theme->button->border_width + theme->button->pad) * 2;
+ button->widget.rect.h = button->ts->h + (theme->button->border_width + theme->button->pad) * 2;
button->text = NULL;
button->text_pressed = NULL;
button->text_hover = NULL;
diff --git a/button.h b/button.h
@@ -41,10 +41,7 @@ typedef struct LtkButtonTheme {
int border_width;
int font_size;
XColor text_color;
- int padl;
- int padr;
- int padt;
- int padb;
+ int pad;
XColor border;
XColor fill;
diff --git a/grid.c b/grid.c
@@ -133,20 +133,15 @@ void ltk_recalculate_grid(void *widget)
}
}
if (total_row_weight > 0) {
- height_unit =
- (float) (grid->widget.rect.h -
- height_static) / (float) total_row_weight;
+ height_unit = (float) (grid->widget.rect.h - height_static) / (float) total_row_weight;
}
if (total_column_weight > 0) {
- width_unit =
- (float) (grid->widget.rect.w -
- width_static) / (float) total_column_weight;
+ width_unit = (float) (grid->widget.rect.w - width_static) / (float) total_column_weight;
}
for (i = 0; i < grid->rows; i++) {
grid->row_pos[i] = currenty;
if (grid->row_weights[i] > 0) {
- grid->row_heights[i] =
- grid->row_weights[i] * height_unit;
+ grid->row_heights[i] = grid->row_weights[i] * height_unit;
}
currenty += grid->row_heights[i];
}
@@ -154,8 +149,7 @@ void ltk_recalculate_grid(void *widget)
for (i = 0; i < grid->columns; i++) {
grid->column_pos[i] = currentx;
if (grid->column_weights[i] > 0) {
- grid->column_widths[i] =
- grid->column_weights[i] * width_unit;
+ grid->column_widths[i] = grid->column_weights[i] * width_unit;
}
currentx += grid->column_widths[i];
}
@@ -167,69 +161,43 @@ void ltk_recalculate_grid(void *widget)
if (!grid->widget_grid[i * grid->columns + j]) {
continue;
}
- LtkWidget *ptr =
- grid->widget_grid[i * grid->columns + j];
+ LtkWidget *ptr = grid->widget_grid[i * grid->columns + j];
orig_width = ptr->rect.w;
orig_height = ptr->rect.h;
end_row = i + ptr->row_span;
end_column = j + ptr->column_span;
- if ((ptr->
- sticky & (LTK_STICKY_LEFT | LTK_STICKY_RIGHT))
- == (LTK_STICKY_LEFT | LTK_STICKY_RIGHT)) {
- ptr->rect.w =
- grid->column_pos[end_column] -
- grid->column_pos[j];
+ if (ptr->sticky & LTK_STICKY_LEFT && ptr->sticky & LTK_STICKY_RIGHT) {
+ ptr->rect.w = grid->column_pos[end_column] - grid->column_pos[j];
}
- if ((ptr->
- sticky & (LTK_STICKY_TOP | LTK_STICKY_BOTTOM))
- == (LTK_STICKY_TOP | LTK_STICKY_BOTTOM)) {
- ptr->rect.h =
- grid->row_pos[end_row] -
- grid->row_pos[i];
+ if (ptr->sticky & LTK_STICKY_TOP && ptr->sticky & LTK_STICKY_BOTTOM) {
+ ptr->rect.h = grid->row_pos[end_row] - grid->row_pos[i];
}
- if (orig_width != ptr->rect.w
- || orig_height != ptr->rect.h) {
+ if (orig_width != ptr->rect.w || orig_height != ptr->rect.h) {
if (ptr->resize) {
ptr->resize(ptr);
}
}
- if ((ptr->sticky & LTK_STICKY_RIGHT) ==
- LTK_STICKY_RIGHT) {
- ptr->rect.x =
- grid->column_pos[end_column] -
- ptr->rect.w;
- } else if ((ptr->sticky & LTK_STICKY_LEFT) ==
- LTK_STICKY_LEFT) {
+ if (ptr->sticky & LTK_STICKY_RIGHT) {
+ ptr->rect.x = grid->column_pos[end_column] - ptr->rect.w;
+ } else if (ptr->sticky & LTK_STICKY_LEFT) {
ptr->rect.x = grid->column_pos[j];
} else {
- ptr->rect.x =
- grid->column_pos[j] +
- ((grid->column_pos[end_column] -
- grid->column_pos[j]) / 2 -
- ptr->rect.w / 2);
+ ptr->rect.x = grid->column_pos[j] + ((grid->column_pos[end_column] - grid->column_pos[j]) / 2 - ptr->rect.w / 2);
}
- if ((ptr->sticky & LTK_STICKY_BOTTOM) ==
- LTK_STICKY_BOTTOM) {
- ptr->rect.y =
- grid->row_pos[end_row] - ptr->rect.h;
- } else if ((ptr->sticky & LTK_STICKY_TOP) ==
- LTK_STICKY_TOP) {
+ if (ptr->sticky & LTK_STICKY_BOTTOM) {
+ ptr->rect.y = grid->row_pos[end_row] - ptr->rect.h;
+ } else if (ptr->sticky & LTK_STICKY_TOP) {
ptr->rect.y = grid->row_pos[i];
} else {
- ptr->rect.y =
- grid->row_pos[i] +
- ((grid->row_pos[end_row] -
- grid->row_pos[i]) / 2 -
- ptr->rect.h / 2);
+ ptr->rect.y = grid->row_pos[i] + ((grid->row_pos[end_row] - grid->row_pos[i]) / 2 - ptr->rect.h / 2);
}
}
}
}
-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(void *ptr, LtkGrid * grid, int row, int column, int row_span, int column_span, unsigned short sticky)
{
LtkWidget *widget = ptr;
widget->sticky = sticky;
@@ -237,16 +205,13 @@ void ltk_grid_widget(void *ptr, LtkGrid * grid, int row, int column,
widget->column = column;
widget->row_span = row_span;
widget->column_span = column_span;
- if (grid->column_weights[column] == 0
- && widget->rect.w > grid->column_widths[column]) {
+ if (grid->column_weights[column] == 0 && widget->rect.w > grid->column_widths[column]) {
grid->column_widths[column] = widget->rect.w;
}
- if (grid->row_weights[row] == 0
- && widget->rect.h > grid->row_heights[row]) {
+ if (grid->row_weights[row] == 0 && widget->rect.h > grid->row_heights[row]) {
grid->row_heights[row] = widget->rect.h;
}
- grid->widget_grid[widget->row * grid->columns + widget->column] =
- widget;
+ grid->widget_grid[widget->row * grid->columns + widget->column] = widget;
widget->parent = grid;
ltk_recalculate_grid(grid);
}
@@ -255,8 +220,7 @@ int ltk_grid_find_nearest_column(LtkGrid * grid, int x)
{
int i;
for (i = 0; i < grid->columns; i++) {
- if (grid->column_pos[i] <= x
- && grid->column_pos[i + 1] >= x) {
+ if (grid->column_pos[i] <= x && grid->column_pos[i + 1] >= x) {
return i;
}
}
diff --git a/themes/default.ini b/themes/default.ini
@@ -8,10 +8,7 @@ font = Lumidify_Casual.ttf
border_width = 2
font_size = 30
text_color = #FFFFFF
-padl = 5
-padr = 5
-padt = 5
-padb = 5
+pad = 5
border = #339999
fill = #113355
border_hover = #FFFFFF