2006-08-30 Michael Dominic Kostrzewa <michael.kostrzewa@nokia.com>
[hildon] / hildon-widgets / hildon-composite-widget.c
1 /*
2  * This file is part of hildon-libs
3  *
4  * Copyright (C) 2005, 2006 Nokia Corporation.
5  *
6  * Contact: Michael Dominic Kostrzewa <michael.kostrzewa@nokia.com>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation; version 2.1 of
11  * the License.
12  *
13  * This library is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21  * 02110-1301 USA
22  *
23  */
24
25 #include <gtk/gtkwidget.h>
26 #include <gtk/gtkwindow.h>
27 #include "hildon-composite-widget.h"
28
29 /* This function is a private function of hildon-libs. It hadles focus 
30  * changing for composite hildon widgets: HildonDateEditor, 
31  * HildonNumberEditor, HildonTimeEditor, HildonWeekdayPicker. 
32  * Its purpose is to focus the first widget (from left) inside the container 
33  * regardless of where the focus is coming from.
34  */
35 gboolean
36 hildon_composite_widget_focus (GtkWidget *widget, GtkDirectionType direction)
37 {
38   GtkWidget *toplevel = NULL;
39   GtkWidget *focus_widget = NULL;
40   
41   /* Get the topmost parent widget */  
42   toplevel = gtk_widget_get_toplevel (widget);
43   if (!GTK_IS_WINDOW(toplevel))
44     return GTK_WIDGET_CLASS (g_type_class_peek_parent (
45                      GTK_WIDGET_GET_CLASS (widget)))->focus (widget, direction);
46   /* Get focus widget in the topmost parent widget */
47   focus_widget = GTK_WINDOW (toplevel)->focus_widget;
48
49   if (!GTK_IS_WIDGET (focus_widget))
50     return TRUE;
51
52   if (!gtk_widget_is_ancestor (focus_widget, widget))
53     /* Containers grab_focus grabs the focus to the correct widget */
54     gtk_widget_grab_focus (widget);
55   else
56     return GTK_WIDGET_CLASS (g_type_class_peek_parent (
57                      GTK_WIDGET_GET_CLASS(widget)))->focus (widget, direction);
58
59   return TRUE;
60 }