mirror of
https://github.com/labwc/labwc.git
synced 2025-11-02 09:01:47 -05:00
Make font_texture_create() more generic
Move font_texture_create() to font.c so it can be used for purposes other than rendering the menu, for example server side decoration. Refactor menu.c and menu.h to use this more generic font_texture_create()
This commit is contained in:
parent
8fc6f795db
commit
5ecf0e1e7e
5 changed files with 104 additions and 72 deletions
|
|
@ -1,11 +1,8 @@
|
|||
#define _POSIX_C_SOURCE 200809L
|
||||
#include <assert.h>
|
||||
#include <cairo.h>
|
||||
#include <ctype.h>
|
||||
#include <drm_fourcc.h>
|
||||
#include <libxml/parser.h>
|
||||
#include <libxml/tree.h>
|
||||
#include <pango/pangocairo.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
|
@ -21,61 +18,14 @@
|
|||
#include "menu/menu.h"
|
||||
#include "theme.h"
|
||||
|
||||
static const char font[] = "Sans 8";
|
||||
|
||||
/* state-machine variables for processing <item></item> */
|
||||
static bool in_item = false;
|
||||
static struct menuitem *current_item;
|
||||
|
||||
#define MENUWIDTH (120)
|
||||
#define MENUWIDTH (110)
|
||||
#define MENUHEIGHT (25)
|
||||
#define MENU_PADDING_WIDTH (7)
|
||||
|
||||
static void
|
||||
texture_create(struct server *server, struct wlr_texture **texture,
|
||||
struct wlr_box *geo, const char *text, float *bg, float *fg)
|
||||
{
|
||||
if (*texture) {
|
||||
wlr_texture_destroy(*texture);
|
||||
*texture = NULL;
|
||||
}
|
||||
|
||||
cairo_surface_t *surf = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
|
||||
geo->width, geo->height);
|
||||
cairo_t *cairo = cairo_create(surf);
|
||||
|
||||
cairo_set_source_rgb(cairo, bg[0], bg[1], bg[2]);
|
||||
cairo_paint(cairo);
|
||||
cairo_set_source_rgba(cairo, fg[0], fg[1], fg[2], fg[3]);
|
||||
cairo_move_to(cairo, 0, 0);
|
||||
|
||||
PangoLayout *layout = pango_cairo_create_layout(cairo);
|
||||
pango_layout_set_width(layout, geo->width * PANGO_SCALE);
|
||||
pango_layout_set_text(layout, text, -1);
|
||||
|
||||
PangoFontDescription *desc = pango_font_description_from_string(font);
|
||||
pango_layout_set_font_description(layout, desc);
|
||||
pango_font_description_free(desc);
|
||||
pango_cairo_update_layout(cairo, layout);
|
||||
|
||||
/* center-align vertically */
|
||||
int height;
|
||||
pango_layout_get_pixel_size(layout, NULL, &height);
|
||||
cairo_move_to(cairo, MENU_PADDING_WIDTH, (geo->height - height) / 2);
|
||||
|
||||
pango_cairo_show_layout(cairo, layout);
|
||||
g_object_unref(layout);
|
||||
|
||||
cairo_surface_flush(surf);
|
||||
unsigned char *data = cairo_image_surface_get_data(surf);
|
||||
*texture = wlr_texture_from_pixels(server->renderer, DRM_FORMAT_ARGB8888,
|
||||
cairo_image_surface_get_stride(surf), geo->width,
|
||||
geo->height, data);
|
||||
|
||||
cairo_destroy(cairo);
|
||||
cairo_surface_destroy(surf);
|
||||
}
|
||||
|
||||
static struct menuitem *
|
||||
menuitem_create(struct server *server, struct menu *menu, const char *text)
|
||||
{
|
||||
|
|
@ -84,13 +34,20 @@ menuitem_create(struct server *server, struct menu *menu, const char *text)
|
|||
return NULL;
|
||||
}
|
||||
struct theme *theme = server->theme;
|
||||
menuitem->geo_box.width = MENUWIDTH;
|
||||
menuitem->geo_box.height = MENUHEIGHT;
|
||||
texture_create(server, &menuitem->active_texture, &menuitem->geo_box,
|
||||
text, theme->menu_items_active_bg_color,
|
||||
theme->menu_items_active_text_color);
|
||||
texture_create(server, &menuitem->inactive_texture, &menuitem->geo_box,
|
||||
text, theme->menu_items_bg_color, theme->menu_items_text_color);
|
||||
menuitem->box.width = MENUWIDTH;
|
||||
menuitem->box.height = MENUHEIGHT;
|
||||
|
||||
/* TODO: use rc.font_menu_item */
|
||||
font_texture_create(server, &menuitem->texture.active, MENUWIDTH,
|
||||
text, rc.font_name_activewindow, theme->menu_items_active_text_color);
|
||||
font_texture_create(server, &menuitem->texture.inactive, MENUWIDTH,
|
||||
text, rc.font_name_activewindow, theme->menu_items_text_color);
|
||||
|
||||
/* center align vertically */
|
||||
menuitem->texture.offset_y =
|
||||
(menuitem->box.height - menuitem->texture.active->height) / 2;
|
||||
menuitem->texture.offset_x = MENU_PADDING_WIDTH;
|
||||
|
||||
wl_list_insert(&menu->menuitems, &menuitem->link);
|
||||
return menuitem;
|
||||
}
|
||||
|
|
@ -267,6 +224,10 @@ menu_finish(struct menu *menu)
|
|||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* TODO: call this menu_configure and return the size of the menu so that
|
||||
* a background color can be rendered
|
||||
*/
|
||||
void
|
||||
menu_move(struct menu *menu, int x, int y)
|
||||
{
|
||||
|
|
@ -276,9 +237,9 @@ menu_move(struct menu *menu, int x, int y)
|
|||
int offset = 0;
|
||||
struct menuitem *menuitem;
|
||||
wl_list_for_each_reverse (menuitem, &menu->menuitems, link) {
|
||||
menuitem->geo_box.x = menu->x;
|
||||
menuitem->geo_box.y = menu->y + offset;
|
||||
offset += menuitem->geo_box.height;
|
||||
menuitem->box.x = menu->x;
|
||||
menuitem->box.y = menu->y + offset;
|
||||
offset += menuitem->box.height;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -288,7 +249,7 @@ menu_set_selected(struct menu *menu, int x, int y)
|
|||
struct menuitem *menuitem;
|
||||
wl_list_for_each (menuitem, &menu->menuitems, link) {
|
||||
menuitem->selected =
|
||||
wlr_box_contains_point(&menuitem->geo_box, x, y);
|
||||
wlr_box_contains_point(&menuitem->box, x, y);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue