From ae877b484d8d85a2e88b9a0d2cd52185faec6372 Mon Sep 17 00:00:00 2001 From: Liu Chao Date: Tue, 22 Dec 2015 22:57:43 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E9=83=A8=E4=BB=B6=E4=BD=8D?= =?UTF-8?q?=E7=BD=AE=E8=AE=A1=E7=AE=97=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/gui/css_parser.c | 4 ++ src/gui/widget_base.c | 98 +++++++++++++++++++++++++------------------ 2 files changed, 61 insertions(+), 41 deletions(-) diff --git a/src/gui/css_parser.c b/src/gui/css_parser.c index 38f793df..70f84d70 100644 --- a/src/gui/css_parser.c +++ b/src/gui/css_parser.c @@ -545,6 +545,10 @@ static KeyNameGroup style_option_map[] = { static LCUI_StyleParser style_parser_map[] = { { key_width, NULL, OnParseNumber }, { key_height, NULL, OnParseNumber }, + { key_top, NULL, OnParseNumber }, + { key_right, NULL, OnParseNumber }, + { key_bottom, NULL, OnParseNumber }, + { key_left, NULL, OnParseNumber }, { key_position, NULL, OnParseStyleOption }, { key_display, NULL, OnParseStyleOption }, { key_background_color, NULL, OnParseColor }, diff --git a/src/gui/widget_base.c b/src/gui/widget_base.c index 0a8bccd2..ebf5f010 100755 --- a/src/gui/widget_base.c +++ b/src/gui/widget_base.c @@ -340,44 +340,67 @@ void Widget_SetTitleW( LCUI_Widget w, const wchar_t *title ) Widget_AddTask( w, WTT_TITLE ); } -/** 计算坐标 */ -static void Widget_ComputePosition( LCUI_Widget w ) +static int ComputePositionOffset( LCUI_Widget w, int key ) { - /* 如果有指定定位方式为绝对定位,则以样式表中指定的位置为准 */ - if( w->computed_style.position == SV_ABSOLUTE ) { - switch( w->cached_style->sheet[key_left].type ) { - case SVT_SCALE: - if( !w->parent ) { - break; - } - w->x = w->parent->box.content.width; - w->x *= w->cached_style->sheet[key_left].scale; - break; - case SVT_PX: - w->x = w->cached_style->sheet[key_left].px; - break; - case SVT_NONE: - case SVT_AUTO: - default: - w->x = 0; + LCUI_Style *s = &w->style->sheet[key]; + switch( s->type ) { + case SVT_SCALE: + if( !w->parent ) { break; } - switch( w->cached_style->sheet[key_top].type ) { - case SVT_SCALE: - if( !w->parent ) { - break; + return w->parent->box.content.width * s->scale; + case SVT_PX: return s->px; + case SVT_NONE: + case SVT_AUTO: + default: break; + } + return 0; +} + +static int ComputeStyleOption( LCUI_Widget w, int key, int default_value ) +{ + if( !w->style->sheet[key].is_valid ) { + return default_value; + } + if( w->style->sheet[key].type != SVT_STYLE ) { + return default_value; + } + return w->style->sheet[key].style; +} + +void Widget_FlushPosition( LCUI_Widget w ) +{ + LCUI_Rect rect = w->box.graph; + LCUI_Style *s = &w->style->sheet[key_position]; + int position = ComputeStyleOption( w, key_position, SV_STATIC ); + w->computed_style.left = ComputePositionOffset( w, key_left ); + w->computed_style.top = ComputePositionOffset( w, key_top ); + w->computed_style.right = ComputePositionOffset( w, key_right ); + w->computed_style.bottom = ComputePositionOffset( w, key_bottom ); + if( w->parent && w->computed_style.position != position ) { + Widget_AddTask( w->parent, WTT_LAYOUT ); + } + w->computed_style.position = position; + if( position == SV_ABSOLUTE ) { + w->x = 0; + w->y = 0; + if( w->style->sheet[key_left].is_valid ) { + w->x = w->computed_style.left; + } else if( w->style->sheet[key_right].is_valid ) { + if( w->parent ) { + w->x = w->parent->box.content.width; + w->x -= w->width; } - w->y = w->parent->box.content.height; - w->y *= w->cached_style->sheet[key_top].scale; - break; - case SVT_PX: - w->y = w->cached_style->sheet[key_top].px; - break; - case SVT_NONE: - case SVT_AUTO: - default: - w->y = 0; - break; + w->x -= w->computed_style.right; + } + if( w->style->sheet[key_top].is_valid ) { + w->x = w->computed_style.top; + } else if( w->style->sheet[key_bottom].is_valid ) { + if( w->parent ) { + w->y = w->parent->box.content.height; + w->y -= w->height; + } + w->y -= w->computed_style.bottom; } } /* 以x、y为基础 */ @@ -396,13 +419,6 @@ static void Widget_ComputePosition( LCUI_Widget w ) w->box.outer.y -= w->margin.top; w->box.graph.x -= BoxShadow_GetBoxX(&w->computed_style.shadow); w->box.graph.y -= BoxShadow_GetBoxY(&w->computed_style.shadow); -} - -void Widget_FlushPosition( LCUI_Widget w ) -{ - LCUI_Rect rect; - rect = w->box.graph; - Widget_ComputePosition( w ); if( w->parent ) { DEBUG_MSG("new-rect: %d,%d,%d,%d\n", w->box.graph.x, w->box.graph.y, w->box.graph.w, w->box.graph.h); DEBUG_MSG("old-rect: %d,%d,%d,%d\n", rect.x, rect.y, rect.w, rect.h);