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 "gui/gtk.h"
19 #include "lua/types.h"
20 #include "lua/widget/common.h"
21 
22 static void entry_init(lua_State* L);
23 static void entry_cleanup(lua_State* L,lua_widget widget);
24 static dt_lua_widget_type_t entry_type = {
25   .name = "entry",
26   .gui_init = entry_init,
27   .gui_cleanup = entry_cleanup,
28   .alloc_size = sizeof(dt_lua_widget_t),
29   .parent= &widget_type
30 };
31 
32 
entry_init(lua_State * L)33 static void entry_init(lua_State* L)
34 {
35   lua_entry entry;
36   luaA_to(L,lua_entry,&entry,1);
37 }
38 
entry_cleanup(lua_State * L,lua_widget widget)39 static void entry_cleanup(lua_State* L,lua_widget widget)
40 {
41 }
42 
43 
text_member(lua_State * L)44 static int text_member(lua_State *L)
45 {
46   lua_entry entry;
47   luaA_to(L,lua_entry,&entry,1);
48   if(lua_gettop(L) > 2) {
49     const char * text = luaL_checkstring(L,3);
50     gtk_entry_set_text(GTK_ENTRY(entry->widget),text);
51     return 0;
52   }
53   lua_pushstring(L,gtk_entry_get_text(GTK_ENTRY(entry->widget)));
54   return 1;
55 }
56 
is_password_member(lua_State * L)57 static int is_password_member(lua_State *L)
58 {
59   lua_entry entry;
60   luaA_to(L,lua_entry,&entry,1);
61   if(lua_gettop(L) > 2) {
62     const gboolean visibility = lua_toboolean(L,3);
63     gtk_entry_set_visibility(GTK_ENTRY(entry->widget),!visibility);
64     return 0;
65   }
66   lua_pushboolean(L,gtk_entry_get_visibility(GTK_ENTRY(entry->widget)));
67   return 1;
68 }
69 
placeholder_member(lua_State * L)70 static int placeholder_member(lua_State *L)
71 {
72   lua_entry entry;
73   luaA_to(L,lua_entry,&entry,1);
74   if(lua_gettop(L) > 2) {
75     const char * placeholder = luaL_checkstring(L,3);
76     gtk_entry_set_placeholder_text(GTK_ENTRY(entry->widget),placeholder);
77     return 0;
78   }
79   lua_pushstring(L,gtk_entry_get_placeholder_text(GTK_ENTRY(entry->widget)));
80   return 1;
81 }
82 
editable_member(lua_State * L)83 static int editable_member(lua_State *L)
84 {
85   lua_entry entry;
86   luaA_to(L,lua_entry,&entry,1);
87   gboolean editable;
88   if(lua_gettop(L) > 2) {
89     editable = lua_toboolean(L,3);
90     g_object_set(G_OBJECT(entry->widget), "editable", editable, (gchar *)0);
91     return 0;
92   }
93   g_object_get(G_OBJECT(entry->widget),"editable",&editable,NULL);
94   lua_pushboolean(L,editable);
95   return 1;
96 }
97 
tostring_member(lua_State * L)98 static int tostring_member(lua_State *L)
99 {
100   lua_entry widget;
101   luaA_to(L, lua_entry, &widget, 1);
102   const gchar *text = gtk_entry_get_text(GTK_ENTRY(widget->widget));
103   gchar *res = g_strdup_printf("%s (\"%s\")", G_OBJECT_TYPE_NAME(widget->widget), text ? text : "");
104   lua_pushstring(L, res);
105   g_free(res);
106   return 1;
107 }
108 
dt_lua_init_widget_entry(lua_State * L)109 int dt_lua_init_widget_entry(lua_State* L)
110 {
111   dt_lua_init_widget_type(L,&entry_type,lua_entry,GTK_TYPE_ENTRY);
112 
113   lua_pushcfunction(L, tostring_member);
114   dt_lua_gtk_wrap(L);
115   dt_lua_type_setmetafield(L, lua_entry, "__tostring");
116 
117   lua_pushcfunction(L,text_member);
118   dt_lua_gtk_wrap(L);
119   dt_lua_type_register(L, lua_entry, "text");
120 
121   lua_pushcfunction(L,is_password_member);
122   dt_lua_gtk_wrap(L);
123   dt_lua_type_register(L, lua_entry, "is_password");
124 
125   lua_pushcfunction(L,placeholder_member);
126   dt_lua_gtk_wrap(L);
127   dt_lua_type_register(L, lua_entry, "placeholder");
128 
129   lua_pushcfunction(L,editable_member);
130   dt_lua_gtk_wrap(L);
131   dt_lua_type_register(L, lua_entry, "editable");
132 
133   return 0;
134 }
135 // modelines: These editor modelines have been set for all relevant files by tools/update_modelines.sh
136 // vim: shiftwidth=2 expandtab tabstop=2 cindent
137 // kate: tab-indents: off; indent-width 2; replace-tabs on; indent-mode cstyle; remove-trailing-spaces modified;
138