Add translation (Complete)
[dana/obconf.git] / src / mouse.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3    mouse.c for ObConf, the configuration tool for Openbox
4    Copyright (c) 2007        Dana Jansens
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    See the COPYING file for a copy of the GNU General Public License.
17 */
18
19 #include "main.h"
20 #include "tree.h"
21 #include "gettext.h"
22 #include <obt/xml.h>
23
24 static gboolean   mapping = FALSE;
25 static xmlNodePtr saved_custom = NULL;
26
27 #define TITLEBAR_MAXIMIZE 0
28 #define TITLEBAR_SHADE    1
29 #define TITLEBAR_CUSTOM   2
30
31 static gint read_doubleclick_action();
32 static void write_doubleclick_action(gint a);
33 static void enable_stuff();
34
35 void mouse_setup_tab()
36 {
37     GtkWidget *w;
38     gint a;
39
40     mapping = TRUE;
41
42     w = get_widget("focus_mouse");
43     gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w),
44                                  tree_get_bool("focus/followMouse", FALSE));
45
46     w = get_widget("focus_delay");
47     gtk_spin_button_set_value(GTK_SPIN_BUTTON(w),
48                               tree_get_int("focus/focusDelay", 0));
49
50     w = get_widget("focus_raise");
51     gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w),
52                                  tree_get_bool("focus/raiseOnFocus", FALSE));
53
54     w = get_widget("focus_notlast");
55     gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w),
56                                  !tree_get_bool("focus/focusLast", TRUE));
57
58     w = get_widget("focus_under_mouse");
59     gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w),
60                                  tree_get_bool("focus/underMouse", FALSE));
61
62     w = get_widget("doubleclick_time");
63     gtk_spin_button_set_value(GTK_SPIN_BUTTON(w),
64                               tree_get_int("mouse/doubleClickTime", 200));
65
66
67     w = get_widget("titlebar_doubleclick");
68     a = read_doubleclick_action();
69     if (a == TITLEBAR_CUSTOM) {
70         gtk_combo_box_text_append_text
71             (GTK_COMBO_BOX_TEXT(w), _("Custom actions"));
72     }
73     gtk_combo_box_set_active(GTK_COMBO_BOX(w), a);
74
75     enable_stuff();
76
77     mapping = FALSE;
78 }
79
80 static void enable_stuff()
81 {
82     GtkWidget *w;
83     gboolean b;
84
85     w = get_widget("focus_mouse");
86     b = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w));
87
88     w = get_widget("focus_delay");
89     gtk_widget_set_sensitive(w, b);
90     w = get_widget("focus_delay_label");
91     gtk_widget_set_sensitive(w, b);
92     w = get_widget("focus_delay_label_units");
93     gtk_widget_set_sensitive(w, b);
94     w = get_widget("focus_raise");
95     gtk_widget_set_sensitive(w, b);
96     w = get_widget("focus_notlast");
97     gtk_widget_set_sensitive(w, b);
98     w = get_widget("focus_under_mouse");
99     gtk_widget_set_sensitive(w, b);
100 }
101
102 void on_focus_mouse_toggled(GtkToggleButton *w, gpointer data)
103 {
104     gboolean b;
105
106     if (mapping) return;
107
108     b = gtk_toggle_button_get_active(w);
109     tree_set_bool("focus/followMouse", b);
110
111     enable_stuff();
112 }
113
114 void on_focus_delay_value_changed(GtkSpinButton *w, gpointer data)
115 {
116     if (mapping) return;
117
118     tree_set_int("focus/focusDelay", gtk_spin_button_get_value_as_int(w));
119 }
120
121 void on_focus_raise_toggled(GtkToggleButton *w, gpointer data)
122 {
123     if (mapping) return;
124
125     tree_set_bool("focus/raiseOnFocus", gtk_toggle_button_get_active(w));
126 }
127
128 void on_focus_notlast_toggled(GtkToggleButton *w, gpointer data)
129 {
130     if (mapping) return;
131
132     tree_set_bool("focus/focusLast", !gtk_toggle_button_get_active(w));
133 }
134
135 void on_focus_under_mouse_toggled(GtkToggleButton *w, gpointer data)
136 {
137     if (mapping) return;
138
139     tree_set_bool("focus/underMouse", gtk_toggle_button_get_active(w));
140 }
141
142 void on_titlebar_doubleclick_changed(GtkComboBox *w, gpointer data)
143 {
144     if (mapping) return;
145
146     switch (gtk_combo_box_get_active(w)) {
147     case 0:
148       write_doubleclick_action(TITLEBAR_MAXIMIZE);
149       break;
150     case 1:
151       write_doubleclick_action(TITLEBAR_SHADE);
152       break;
153     case 2:
154       write_doubleclick_action(TITLEBAR_CUSTOM);
155       break;
156     }
157 }
158
159 void on_doubleclick_time_value_changed(GtkSpinButton *w, gpointer data)
160 {
161     if (mapping) return;
162
163     tree_set_int("mouse/doubleClickTime",
164                  gtk_spin_button_get_value_as_int(w));
165 }
166
167 static gint read_doubleclick_action()
168 {
169     xmlNodePtr n, top, c;
170     gint max = 0, shade = 0, other = 0;
171
172     top = tree_get_node("mouse/context:name=Titlebar"
173                         "/mousebind:button=Left:action=DoubleClick", NULL);
174     n = top->children;
175
176     /* save the current state */
177     saved_custom = xmlCopyNode(top, 1);
178
179     /* remove the namespace from all the nodes under saved_custom..
180        without recursion! */
181     c = saved_custom;
182     while (c) {
183         xmlSetNs(c, NULL);
184         if (c->children)
185             c = c->children;
186         else if (c->next)
187             c = c->next;
188         while (c->parent && !c->parent->next)
189             c = c->parent;
190         if (!c->parent)
191             c = NULL;
192     }
193
194     while (n) {
195         if (!xmlStrcmp(n->name, (const xmlChar*)"action")) {
196             if (obt_xml_attr_contains(n, "name", "ToggleMaximizeFull"))
197                 ++max;
198             else if (obt_xml_attr_contains(n, "name", "ToggleShade"))
199                 ++shade;
200             else
201                 ++other;
202             
203         }
204         n = n->next;
205     }
206
207     if (max == 1 && shade == 0 && other == 0)
208         return TITLEBAR_MAXIMIZE;
209     if (max == 0 && shade == 1 && other == 0)
210         return TITLEBAR_SHADE;
211
212     return TITLEBAR_CUSTOM;
213 }
214
215 static void write_doubleclick_action(gint a)
216 {
217     xmlNodePtr n;
218
219     n = tree_get_node("mouse/context:name=Titlebar"
220                       "/mousebind:button=Left:action=DoubleClick", NULL);
221
222     /* remove all children */
223     while (n->children) {
224         xmlUnlinkNode(n->children);
225         xmlFreeNode(n->children);
226     }
227
228     if (a == TITLEBAR_MAXIMIZE) {
229         n = xmlNewChild(n, NULL, "action", NULL);
230         xmlSetProp(n, "name", "ToggleMaximizeFull");
231     } else if (a == TITLEBAR_SHADE) {
232         n = xmlNewChild(n, NULL, "action", NULL);
233         xmlSetProp(n, "name", "ToggleShade");
234     } else {
235         xmlNodePtr c = saved_custom->children;
236         while (c) {
237             xmlAddChild(n, xmlCopyNode(c, 1));
238             c = c->next;
239         }
240     }
241
242     tree_apply();
243 }