1 /*
2  * clib/widget.h - widget managing header
3  *
4  * Copyright © 2010 Mason Larobina <mason.larobina@gmail.com>
5  * Copyright © 2007-2009 Julien Danjou <julien@danjou.info>
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef LUAKIT_CLIB_WIDGET_H
23 #define LUAKIT_CLIB_WIDGET_H
24 
25 typedef struct widget_t widget_t;
26 
27 #include "common/util.h"
28 #include "common/luaclass.h"
29 #include "common/luaobject.h"
30 #include "luah.h"
31 #include "globalconf.h"
32 
33 #include <gtk/gtk.h>
34 
35 #define GOBJECT_LUAKIT_WIDGET_DATA_KEY "luakit_widget_data"
36 
37 #define GOBJECT_TO_LUAKIT_WIDGET(gtk_widget) ((widget_t*)g_object_get_data(G_OBJECT(gtk_widget), \
38             GOBJECT_LUAKIT_WIDGET_DATA_KEY))
39 
40 typedef widget_t *(widget_constructor_t)(lua_State *L, widget_t *, luakit_token_t);
41 typedef void (widget_destructor_t)(widget_t *);
42 
43 widget_constructor_t widget_box;
44 widget_constructor_t widget_entry;
45 widget_constructor_t widget_eventbox;
46 widget_constructor_t widget_label;
47 widget_constructor_t widget_notebook;
48 widget_constructor_t widget_paned;
49 widget_constructor_t widget_webview;
50 widget_constructor_t widget_window;
51 widget_constructor_t widget_overlay;
52 widget_constructor_t widget_scrolled;
53 widget_constructor_t widget_image;
54 widget_constructor_t widget_spinner;
55 widget_constructor_t widget_drawing_area;
56 widget_constructor_t widget_stack;
57 
58 typedef const struct {
59     luakit_token_t tok;
60     const gchar *name;
61     widget_constructor_t *wc;
62 } widget_info_t;
63 
64 /* Widget */
65 struct widget_t
66 {
67     LUA_OBJECT_HEADER
68     /* Widget type information */
69     widget_info_t *info;
70     /* Widget destructor */
71     widget_destructor_t *destructor;
72     /* Index function */
73     gint (*index)(lua_State *, widget_t *, luakit_token_t);
74     /* Newindex function */
75     gint (*newindex)(lua_State *, widget_t *, luakit_token_t);
76     /* Lua object ref */
77     gpointer ref;
78     /* Main gtk widget */
79     GtkWidget *widget;
80 #if GTK_CHECK_VERSION(3,16,0)
81     /* CSS provider for this widget */
82     GtkCssProvider *provider;
83 #endif
84     /* Previous width and height, for resize signal */
85     gint prev_width, prev_height;
86     /* Misc private data */
87     gpointer data;
88 };
89 
90 extern lua_class_t widget_class;
91 void widget_class_setup(lua_State *);
92 void widget_set_css_properties(widget_t *, ...);
93 gint luaH_widget_new(lua_State *L);
94 
95 static inline widget_t*
luaH_checkwidget(lua_State * L,gint udx)96 luaH_checkwidget(lua_State *L, gint udx)
97 {
98     widget_t *w = luaH_checkudata(L, udx, &widget_class);
99     if (!w->widget)
100         luaL_error(L, "widget %p (%s) has been destroyed", w, w->info->name);
101     g_assert(GTK_IS_WIDGET(w->widget));
102     return w;
103 }
104 
105 static inline widget_t*
luaH_checkwidgetornil(lua_State * L,gint udx)106 luaH_checkwidgetornil(lua_State *L, gint udx)
107 {
108     if (lua_isnil(L, udx))
109         return NULL;
110     return luaH_checkwidget(L, udx);
111 }
112 
113 #define luaH_towidget(L, udx) luaH_toudata(L, udx, &widget_class)
114 
115 #endif
116 
117 // vim: ft=c:et:sw=4:ts=8:sts=4:tw=80
118