1 /*
2    This file is part of darktable,
3    Copyright (C) 2015-2020 darktable developers.
4 
5    darktable is free software: you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation, either version 3 of the License, or
8    (at your option) any later version.
9 
10    darktable is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14 
15    You should have received a copy of the GNU General Public License
16    along with darktable.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 #include "lua/types.h"
19 #include "lua/widget/common.h"
20 
21 static dt_lua_widget_type_t check_button_type = {
22   .name = "check_button",
23   .gui_init = NULL,
24   .gui_cleanup = NULL,
25   .alloc_size = sizeof(dt_lua_widget_t),
26   .parent= &widget_type
27 };
28 
clicked_callback(GtkButton * widget,gpointer user_data)29 static void clicked_callback(GtkButton *widget, gpointer user_data)
30 {
31   dt_lua_async_call_alien(dt_lua_widget_trigger_callback,
32       0,NULL,NULL,
33       LUA_ASYNC_TYPENAME,"lua_widget",user_data,
34       LUA_ASYNC_TYPENAME,"const char*","clicked",
35       LUA_ASYNC_DONE);
36 }
37 
label_member(lua_State * L)38 static int label_member(lua_State *L)
39 {
40   lua_check_button check_button;
41   luaA_to(L,lua_check_button,&check_button,1);
42   if(lua_gettop(L) > 2) {
43     const char * label = luaL_checkstring(L,3);
44     gtk_button_set_label(GTK_BUTTON(check_button->widget),label);
45     return 0;
46   }
47   lua_pushstring(L,gtk_button_get_label(GTK_BUTTON(check_button->widget)));
48   return 1;
49 }
50 
value_member(lua_State * L)51 static int value_member(lua_State *L)
52 {
53   lua_check_button check_button;
54   luaA_to(L,lua_check_button,&check_button,1);
55   if(lua_gettop(L) > 2) {
56     luaL_checktype(L,3,LUA_TBOOLEAN);
57     gboolean value = lua_toboolean(L,3);
58     gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check_button->widget),value);
59     return 0;
60   }
61   lua_pushboolean(L,gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(check_button->widget)));
62   return 1;
63 }
64 
tostring_member(lua_State * L)65 static int tostring_member(lua_State *L)
66 {
67   lua_check_button widget;
68   luaA_to(L, lua_check_button, &widget, 1);
69   const gchar *text = gtk_button_get_label(GTK_BUTTON(widget->widget));
70   gchar *res = g_strdup_printf("%s (\"%s\")", G_OBJECT_TYPE_NAME(widget->widget), text ? text : "");
71   lua_pushstring(L, res);
72   g_free(res);
73   return 1;
74 }
75 
dt_lua_init_widget_check_button(lua_State * L)76 int dt_lua_init_widget_check_button(lua_State* L)
77 {
78   dt_lua_init_widget_type(L,&check_button_type,lua_check_button,GTK_TYPE_CHECK_BUTTON);
79 
80   lua_pushcfunction(L, tostring_member);
81   dt_lua_gtk_wrap(L);
82   dt_lua_type_setmetafield(L, lua_check_button, "__tostring");
83   lua_pushcfunction(L,value_member);
84   dt_lua_gtk_wrap(L);
85   dt_lua_type_register(L, lua_check_button, "value");
86   lua_pushcfunction(L,label_member);
87   dt_lua_gtk_wrap(L);
88   dt_lua_type_register(L, lua_check_button, "label");
89   dt_lua_widget_register_gtk_callback(L,lua_check_button,"clicked","clicked_callback",G_CALLBACK(clicked_callback));
90 
91   return 0;
92 }
93 // modelines: These editor modelines have been set for all relevant files by tools/update_modelines.sh
94 // vim: shiftwidth=2 expandtab tabstop=2 cindent
95 // kate: tab-indents: off; indent-width 2; replace-tabs on; indent-mode cstyle; remove-trailing-spaces modified;
96