Fix EM support, use char height

This commit is contained in:
Dave Davenport 2016-12-31 23:00:06 +01:00
parent 068592414e
commit 52e850dc33
5 changed files with 31 additions and 21 deletions

View file

@ -75,15 +75,6 @@ void listview_set_selected ( listview *lv, unsigned int selected );
*/ */
unsigned int listview_get_selected ( listview *lv ); unsigned int listview_get_selected ( listview *lv );
/**
* @param lv The listview handle
*
* Get the desired height of the listview widget.
*
* @returns the desired height.
*/
unsigned int listview_get_desired_height ( listview *lv );
/** /**
* @param lv The listview handle * @param lv The listview handle
* *

View file

@ -196,6 +196,7 @@ int textbox_get_font_width ( const textbox *tb );
* @returns the width of a character in pixels. * @returns the width of a character in pixels.
*/ */
double textbox_get_estimated_char_width ( void ); double textbox_get_estimated_char_width ( void );
double textbox_get_estimated_char_height ( void );
/** /**
* @param tb Handle to the textbox * @param tb Handle to the textbox
@ -217,13 +218,14 @@ void textbox_delete ( textbox *tb, int pos, int dlen );
* TODO remove for #widget_resize and #widget_move * TODO remove for #widget_resize and #widget_move
*/ */
void textbox_moveresize ( textbox *tb, int x, int y, int w, int h ); void textbox_moveresize ( textbox *tb, int x, int y, int w, int h );
/** /**
* @param tb Handle to the textbox * @param tb Handle to the textbox
* Get the (estimated) with of a character, can be used to calculate window width. * Get the (estimated) with of a character, can be used to calculate window width.
* *
* @returns the estimated width of a character. * @returns the estimated width of a character.
*/ */
int textbox_get_estimated_char_height ( const textbox *tb, int eh ); int textbox_get_estimated_height ( const textbox *tb, int eh );
/** /**
* @param p The new default PangoContext * @param p The new default PangoContext
* *

View file

@ -77,6 +77,8 @@ struct _listview
void *mouse_activated_data; void *mouse_activated_data;
}; };
static int listview_get_desired_height ( widget *wid );
static void listview_free ( widget *wid ) static void listview_free ( widget *wid )
{ {
listview *lv = (listview *) wid; listview *lv = (listview *) wid;
@ -340,7 +342,7 @@ listview *listview_create ( const char *name, listview_update_callback cb, void
// //
char *tb_name = g_strjoin (".", lv->widget.name,"element", NULL); char *tb_name = g_strjoin (".", lv->widget.name,"element", NULL);
textbox *tb = textbox_create ( tb_name, 0, NORMAL, "" ); textbox *tb = textbox_create ( tb_name, 0, NORMAL, "" );
lv->element_height = textbox_get_estimated_char_height (tb, lv->eh); lv->element_height = textbox_get_estimated_height (tb, lv->eh);
g_free(tb_name); g_free(tb_name);
lv->callback = cb; lv->callback = cb;
@ -452,8 +454,9 @@ void listview_nav_page_next ( listview *lv )
widget_queue_redraw ( WIDGET ( lv ) ); widget_queue_redraw ( WIDGET ( lv ) );
} }
unsigned int listview_get_desired_height ( listview *lv ) static int listview_get_desired_height ( widget *wid )
{ {
listview *lv = (listview *)wid;
if ( lv == NULL || lv->widget.enabled == FALSE ) { if ( lv == NULL || lv->widget.enabled == FALSE ) {
return 0; return 0;
} }

View file

@ -757,13 +757,27 @@ int textbox_get_font_width ( const textbox *tb )
return width; return width;
} }
double textbox_get_estimated_char_width ( void ) static double char_height = -1;
double textbox_get_estimated_char_height ( void )
{ {
int width = pango_font_metrics_get_approximate_char_width ( p_metrics ); if ( char_height < 0 ){
return ( width ) / (double) PANGO_SCALE; int height = pango_font_metrics_get_ascent ( p_metrics ) + pango_font_metrics_get_descent ( p_metrics );
char_height = ( height ) / (double) PANGO_SCALE;
}
return char_height;
} }
int textbox_get_estimated_char_height ( const textbox *tb, int eh ) static double char_width = -1;
double textbox_get_estimated_char_width ( void )
{
if ( char_width < 0 ){
int width = pango_font_metrics_get_approximate_char_width ( p_metrics );
char_width = ( width ) / (double) PANGO_SCALE;
}
return char_width;
}
int textbox_get_estimated_height ( const textbox *tb, int eh )
{ {
int height = pango_font_metrics_get_ascent ( p_metrics ) + pango_font_metrics_get_descent ( p_metrics ); int height = pango_font_metrics_get_ascent ( p_metrics ) + pango_font_metrics_get_descent ( p_metrics );
return ( eh*height ) / PANGO_SCALE + widget_padding_get_padding_height ( WIDGET ( tb ) ); return ( eh*height ) / PANGO_SCALE + widget_padding_get_padding_height ( WIDGET ( tb ) );

View file

@ -212,32 +212,32 @@ void widget_set_name ( widget *wid, const char *name )
} }
// External // External
double textbox_get_estimated_char_width ( void ); double textbox_get_estimated_char_height ( void );
int widget_padding_get_left ( const widget *wid ) int widget_padding_get_left ( const widget *wid )
{ {
if ( wid->pad.left.type == PW_EM ){ if ( wid->pad.left.type == PW_EM ){
return wid->pad.left.distance*textbox_get_estimated_char_width(); return wid->pad.left.distance*textbox_get_estimated_char_height();
} }
return wid->pad.left.distance; return wid->pad.left.distance;
} }
int widget_padding_get_right ( const widget *wid ) int widget_padding_get_right ( const widget *wid )
{ {
if ( wid->pad.right.type == PW_EM ){ if ( wid->pad.right.type == PW_EM ){
return wid->pad.right.distance*textbox_get_estimated_char_width(); return wid->pad.right.distance*textbox_get_estimated_char_height();
} }
return wid->pad.right.distance; return wid->pad.right.distance;
} }
int widget_padding_get_top ( const widget *wid ) int widget_padding_get_top ( const widget *wid )
{ {
if ( wid->pad.top.type == PW_EM ){ if ( wid->pad.top.type == PW_EM ){
return wid->pad.top.distance*textbox_get_estimated_char_width(); return wid->pad.top.distance*textbox_get_estimated_char_height();
} }
return wid->pad.top.distance; return wid->pad.top.distance;
} }
int widget_padding_get_bottom ( const widget *wid ) int widget_padding_get_bottom ( const widget *wid )
{ {
if ( wid->pad.bottom.type == PW_EM ){ if ( wid->pad.bottom.type == PW_EM ){
return wid->pad.bottom.distance*textbox_get_estimated_char_width(); return wid->pad.bottom.distance*textbox_get_estimated_char_height();
} }
return wid->pad.bottom.distance; return wid->pad.bottom.distance;
} }