/* 初始化window部件相关数据 */ static void Window_Init( LCUI_Widget *win_p ) { LCUI_Widget *titlebar; LCUI_Widget *client_area; LCUI_Widget *btn_close; LCUI_Window *win; static LCUI_Graph btn_bg; win = (LCUI_Window*)Widget_NewPrivData(win_p, sizeof(LCUI_Window)); titlebar = Widget_New("titlebar"); client_area = Widget_New(NULL); btn_close = Widget_New("button"); titlebar->focus = FALSE; Widget_SetFocus( client_area ); Graph_Init( &btn_bg ); /* 载入按钮背景 */ Window_GetCloseButtonBG( &btn_bg ); /* 显示在左上角 */ Widget_SetAlign(btn_close, ALIGN_TOP_RIGHT, Pos(0, 0)); /* 将尺寸改成和图片一样 */ Widget_SetAutoSize( btn_close, FALSE, 0 ); Widget_Resize(btn_close, Size(44, 20)); Widget_SetBackgroundImage( btn_close, &btn_bg ); Widget_SetBackgroundLayout( btn_close, LAYOUT_CENTER ); /* 没有背景图就填充背景色 */ Widget_SetBackgroundTransparent( win_p, FALSE ); /* 部件的风格ID */ Widget_SetStyleID( win_p, WINDOW_STYLE_PURE_BLUE ); Widget_SetStyleID( btn_close, BUTTON_STYLE_FLAT ); /* 保存部件指针 */ win->client_area = client_area; win->titlebar = titlebar; win->btn_close = btn_close; /* 放入至容器 */ Widget_Container_Add(titlebar, btn_close); Widget_Container_Add(win_p, titlebar); Widget_Container_Add(win_p, client_area); /* 窗口初始尺寸 */ Widget_Resize(win_p, Size(100, 50)); Widget_Show(btn_close); /* 关联拖动事件,让鼠标能够拖动标题栏并使窗口移动 */ Widget_Event_Connect(titlebar, EVENT_DRAG, Window_ExecMove ); /* * 由于需要在窗口获得/失去焦点时进行相关处理,因此需要将回调函数 与部件 * 的FOCUS_IN和FOCUS_OUT事件 进行关联 * */ Widget_Event_Connect( win_p, EVENT_FOCUSOUT, Window_FocusOut ); Widget_Event_Connect( win_p, EVENT_FOCUSIN, Window_FocusIn ); /* 设置窗口部件的初始位置 */ Widget_SetAlign( win_p, ALIGN_MIDDLE_CENTER, Pos(0,0) ); }
/** * 功能:为部件设置焦点 * 说明:上个获得焦点的部件会得到EVENT_FOCUSOUT事件,而当前获得焦点的部件会得到 * EVENT_FOCUSIN事件。 * */ LCUI_API LCUI_BOOL Widget_SetFocus( LCUI_Widget *widget ) { LCUI_Widget **focus_widget; LCUI_WidgetEvent event; if( widget ) { /* 先处理上级部件的焦点 */ if( widget->parent ) { Widget_SetFocus( widget->parent ); } if( !widget->focus ) { return FALSE; } } else { return FALSE; } if( widget->parent ) { focus_widget = &widget->parent->focus_widget; } else { focus_widget = &RootWidget_GetSelf()->focus_widget; } if( *focus_widget ) { /* 若之前获得焦点的是模态部件,则不能移动焦点 */ if( (*focus_widget)->modal ) { return FALSE; } /* 如果上次和这次的部件不一样 */ if( *focus_widget != widget ) { event.type = EVENT_FOCUSOUT; Widget_DispatchEvent( *focus_widget, &event ); } } event.type = EVENT_FOCUSIN; Widget_DispatchEvent( widget, &event ); /* 保存新焦点位置 */ *focus_widget = widget; return TRUE; }
/** 响应鼠标按键按下事件 */ static void LCUI_HandleMouseButtonDown( LCUI_MouseButtonEvent *event ) { LCUI_Widget *widget, *tmp_widget; LCUI_Pos pos; LCUI_WidgetEvent wdg_event; if( !event || event->state != LCUIKEYSTATE_PRESSED ) { return; } pos.x = event->x; pos.y = event->y; old_cursor_pos.x = pos.x; old_cursor_pos.y = pos.y; widget = Widget_At( NULL, pos ); if( !Widget_IsAllowResponseEvent(widget) ) { return; } /* 获取能够响应此事件的部件 */ tmp_widget = GetWidgetOfResponseEvent( widget, EVENT_MOUSEBUTTON ); if( widget && tmp_widget ) { /* 开始准备事件数据 */ pos = Widget_ToRelPos( tmp_widget, pos ); wdg_event.type = EVENT_MOUSEBUTTON; wdg_event.mouse_button.x = pos.x; wdg_event.mouse_button.y = pos.y; wdg_event.mouse_button.button = event->button; wdg_event.mouse_button.state = event->state; /* 派发事件 */ Widget_DispatchEvent( tmp_widget, &wdg_event ); } if( event->button != LCUIKEY_LEFTBUTTON ) { return; } tmp_widget = GetWidgetOfResponseEvent( widget, EVENT_CLICKED ); if( tmp_widget ) { click_widget = tmp_widget; tmp_widget = GetWidgetOfResponseEvent( widget, EVENT_DRAG ); if( click_widget == tmp_widget ) { DEBUG_MSG("start drag\n"); /* 开始处理部件的拖动 */ _DragEvent_Start( tmp_widget, event ); /* 设置标记,表示可以进行部件拖动 */ can_drag_widget = TRUE; } else { /* 设置标记,表示不能进行部件拖动 */ can_drag_widget = FALSE; } } else { click_widget = widget; tmp_widget = GetWidgetOfResponseEvent( widget, EVENT_DRAG ); if( widget && tmp_widget ) { DEBUG_MSG("start drag\n"); /* 开始处理部件的拖动 */ _DragEvent_Start( tmp_widget, event ); can_drag_widget = TRUE; } } /* 焦点转移给该部件 */ Widget_SetFocus( click_widget ); WidgetRecord_SetWidgetState( widget, WIDGET_STATE_ACTIVE ); }
/** 初始化window部件相关数据 */ static void Window_OnInit( LCUI_Widget *window ) { LCUI_Window *wnd; LCUI_Graph btn_bg; LCUI_TextStyle style; LCUI_Border border; wnd = Widget_NewPrivateData( window, LCUI_Window ); wnd->theme_color = COLOR_EMERALD; wnd->theme_color.a = 128; wnd->titlebar = Widget_New(NULL); wnd->client_area = Widget_New(NULL); wnd->btn_close = Widget_New(WIDGET_CLOSE_BUTTON); wnd->icon = Widget_New(NULL); wnd->text = Label_New(); wnd->titlebar->focus = FALSE; wnd->icon->focus = FALSE; wnd->text->focus = FALSE; Widget_SetFocus( wnd->client_area ); Widget_SetClickable( wnd->icon, FALSE ); Widget_SetClickable( wnd->text, FALSE ); Widget_SetClickable( wnd->titlebar, FALSE ); Widget_SetClickable( wnd->client_area, FALSE ); Graph_Init( &btn_bg ); /* 载入按钮背景 */ LoadCloseButtonIMG( &btn_bg ); /* close按钮显示在左上角 */ Widget_SetAlign( wnd->btn_close, ALIGN_TOP_RIGHT, Pos(0,0) ); Widget_Resize( wnd->btn_close, Size(30, 30) ); Widget_SetBackgroundImage( wnd->btn_close, &btn_bg ); Widget_SetBackgroundLayout( wnd->btn_close, LAYOUT_CENTER ); Widget_SetBackgroundTransparent( wnd->icon, TRUE ); Widget_SetBackgroundTransparent( wnd->titlebar, TRUE ); Widget_SetBackgroundTransparent( wnd->client_area, FALSE ); Widget_SetBackgroundTransparent( window, FALSE ); Widget_SetBackgroundColor( window, RGB(255,255,255) ); Widget_SetBackgroundLayout( wnd->icon, LAYOUT_ZOOM ); Widget_SetDock( wnd->titlebar, DOCK_TYPE_TOP ); Widget_SetDock( wnd->client_area, DOCK_TYPE_BOTTOM ); Widget_SetPadding( window, Padding(TOP_PANDDING,1,1,1) ); Widget_SetPadding( wnd->client_area, Padding(1,1,1,1) ); Widget_SetSize( wnd->titlebar, "100%", "35px" ); Border_Init( &border ); border.top_width = 0; border.bottom_width = 1; border.left_width = 1; border.right_width = 1; border.bottom_color = RGB(200,200,200); border.left_color = RGB(255,255,255); border.right_color = RGB(255,255,255); Widget_SetBorder( wnd->titlebar, border ); TextStyle_Init( &style ); TextStyle_FontSize( &style, 18 ); Label_SetTextStyle( wnd->text, style ); /* 放入至容器 */ Widget_Container_Add( wnd->titlebar, wnd->icon ); Widget_Container_Add( wnd->titlebar, wnd->text ); Widget_Container_Add( wnd->titlebar, wnd->btn_close ); Widget_Container_Add( window, wnd->titlebar ); Widget_Container_Add( window, wnd->client_area ); Widget_Resize( window, Size(100, 50) ); Widget_Resize( wnd->icon, Size(24,24) ); Widget_SetAlign( window, ALIGN_MIDDLE_CENTER, Pos(0,0) ); Widget_SetAlign( wnd->icon, ALIGN_MIDDLE_LEFT, Pos(6,0) ); Widget_SetAlign( wnd->text, ALIGN_MIDDLE_LEFT, Pos(6,0) ); /* 关联拖动事件,让鼠标能够拖动标题栏并使窗口移动 */ Widget_ConnectEvent( window, EVENT_DRAG, Window_ExecMove ); /* * 由于需要在窗口获得/失去焦点时进行相关处理,因此需要将回调函数 与部件 * 的FOCUS_IN和FOCUS_OUT事件 进行关联 * */ Widget_ConnectEvent( window, EVENT_FOCUSOUT, Window_OnFocusOut ); Widget_ConnectEvent( window, EVENT_FOCUSIN, Window_OnFocusIn ); Widget_Show( wnd->btn_close ); Widget_Show( wnd->icon ); Widget_Show( wnd->text ); Widget_Show( wnd->titlebar ); Widget_Show( wnd->client_area ); }
static void Window_OnShow( LCUI_Widget *widget ) { Widget_SetFocus( widget ); }