1 /*
2  * rofi
3  *
4  * MIT/X11 License
5  * Copyright © 2013-2020 Qball Cow <qball@gmpclient.org>
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining
8  * a copy of this software and associated documentation files (the
9  * "Software"), to deal in the Software without restriction, including
10  * without limitation the rights to use, copy, modify, merge, publish,
11  * distribute, sublicense, and/or sell copies of the Software, and to
12  * permit persons to whom the Software is furnished to do so, subject to
13  * the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be
16  * included in all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
22  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25  *
26  */
27 
28 /** The log domain of this widget. */
29 #define G_LOG_DOMAIN    "Widgets.Window"
30 
31 #include <config.h>
32 #include <stdio.h>
33 #include "widgets/widget.h"
34 #include "widgets/widget-internal.h"
35 #include "widgets/container.h"
36 #include "theme.h"
37 
38 struct _window
39 {
40     widget widget;
41     widget *child;
42 };
43 
44 static void container_update ( widget *wid  );
45 
container_get_desired_height(widget * widget)46 static int container_get_desired_height ( widget *widget )
47 {
48     container *b     = (container *) widget;
49     int       height = 0;
50     if ( b->child ) {
51         height += widget_get_desired_height ( b->child );
52     }
53     height += widget_padding_get_padding_height ( widget );
54     return height;
55 }
56 
container_draw(widget * wid,cairo_t * draw)57 static void container_draw ( widget *wid, cairo_t *draw )
58 {
59     container *b = (container *) wid;
60 
61     widget_draw ( b->child, draw );
62 }
63 
container_free(widget * wid)64 static void container_free ( widget *wid )
65 {
66     container *b = (container *) wid;
67 
68     widget_free ( b->child );
69     g_free ( b );
70 }
71 
container_add(container * container,widget * child)72 void container_add ( container *container, widget *child )
73 {
74     if ( container == NULL ) {
75         return;
76     }
77     container->child = child;
78     g_assert ( child->parent == WIDGET ( container  ) );
79     widget_update ( WIDGET ( container ) );
80 }
81 
container_resize(widget * widget,short w,short h)82 static void container_resize ( widget *widget, short w, short h )
83 {
84     container *b = (container *) widget;
85     if ( b->widget.w != w || b->widget.h != h ) {
86         b->widget.w = w;
87         b->widget.h = h;
88         widget_update ( widget );
89     }
90 }
91 
container_find_mouse_target(widget * wid,WidgetType type,gint x,gint y)92 static widget *container_find_mouse_target ( widget *wid, WidgetType type, gint x, gint y )
93 {
94     container *b = (container *) wid;
95     if ( !widget_intersect ( b->child, x, y ) ) {
96         return NULL;
97     }
98 
99     x -= b->child->x;
100     y -= b->child->y;
101     return widget_find_mouse_target ( b->child, type, x, y );
102 }
103 
container_set_state(widget * wid,const char * state)104 static void container_set_state ( widget *wid, const char *state )
105 {
106     container *b = (container *) wid;
107     widget_set_state ( b->child, state );
108 }
109 
container_create(widget * parent,const char * name)110 container * container_create ( widget *parent, const char *name )
111 {
112     container *b = g_malloc0 ( sizeof ( container ) );
113     // Initialize widget.
114     widget_init ( WIDGET ( b ), parent, WIDGET_TYPE_UNKNOWN, name );
115     b->widget.draw               = container_draw;
116     b->widget.free               = container_free;
117     b->widget.resize             = container_resize;
118     b->widget.update             = container_update;
119     b->widget.find_mouse_target  = container_find_mouse_target;
120     b->widget.get_desired_height = container_get_desired_height;
121     b->widget.set_state          = container_set_state;
122     return b;
123 }
124 
container_update(widget * wid)125 static void container_update ( widget *wid  )
126 {
127     container *b = (container *) wid;
128     if ( b->child && b->child->enabled ) {
129         widget_resize ( WIDGET ( b->child ),
130                         widget_padding_get_remaining_width  ( WIDGET ( b ) ),
131                         widget_padding_get_remaining_height ( WIDGET ( b ) )
132                         );
133         widget_move ( WIDGET ( b->child ),
134                       widget_padding_get_left ( WIDGET ( b ) ),
135                       widget_padding_get_top ( WIDGET ( b ) )
136                       );
137     }
138 }
139