Merge branch 'backport' into work
[dana/openbox.git] / openbox / actions / maximize.c
1 #include "openbox/actions.h"
2 #include "openbox/client.h"
3
4 /* These match the values for client_maximize */
5 typedef enum {
6     BOTH = 0,
7     HORZ = 1,
8     VERT = 2
9 } MaxDirection;
10
11 typedef struct {
12     MaxDirection dir;
13 } Options;
14
15 static gpointer setup_func(xmlNodePtr node);
16 static gboolean run_func_on(ObActionsData *data, gpointer options);
17 static gboolean run_func_off(ObActionsData *data, gpointer options);
18 static gboolean run_func_toggle(ObActionsData *data, gpointer options);
19 /* 3.4-compatibility */
20 static gpointer setup_both_func(xmlNodePtr node);
21 static gpointer setup_horz_func(xmlNodePtr node);
22 static gpointer setup_vert_func(xmlNodePtr node);
23
24 void action_maximize_startup(void)
25 {
26     actions_register("Maximize", setup_func, g_free, run_func_on);
27     actions_register("Unmaximize", setup_func, g_free, run_func_off);
28     actions_register("ToggleMaximize", setup_func, g_free, run_func_toggle);
29     /* 3.4-compatibility */
30     actions_register("MaximizeFull", setup_both_func, g_free,
31                      run_func_on);
32     actions_register("UnmaximizeFull", setup_both_func, g_free,
33                      run_func_off);
34     actions_register("ToggleMaximizeFull", setup_both_func, g_free,
35                      run_func_toggle);
36     actions_register("MaximizeHorz", setup_horz_func, g_free,
37                      run_func_on);
38     actions_register("UnmaximizeHorz", setup_horz_func, g_free,
39                      run_func_off);
40     actions_register("ToggleMaximizeHorz", setup_horz_func, g_free,
41                      run_func_toggle);
42     actions_register("MaximizeVert", setup_vert_func, g_free,
43                      run_func_on);
44     actions_register("UnmaximizeVert", setup_vert_func, g_free,
45                      run_func_off);
46     actions_register("ToggleMaximizeVert", setup_vert_func, g_free,
47                      run_func_toggle);
48 }
49
50 static gpointer setup_func(xmlNodePtr node)
51 {
52     xmlNodePtr n;
53     Options *o;
54
55     o = g_new0(Options, 1);
56     o->dir = BOTH;
57
58     if ((n = obt_xml_find_node(node, "direction"))) {
59         gchar *s = obt_xml_node_string(n);
60         if (!g_ascii_strcasecmp(s, "vertical") ||
61             !g_ascii_strcasecmp(s, "vert"))
62             o->dir = VERT;
63         else if (!g_ascii_strcasecmp(s, "horizontal") ||
64                  !g_ascii_strcasecmp(s, "horz"))
65             o->dir = HORZ;
66         g_free(s);
67     }
68
69     return o;
70 }
71
72 /* Always return FALSE because its not interactive */
73 static gboolean run_func_on(ObActionsData *data, gpointer options)
74 {
75     Options *o = options;
76     if (data->client) {
77         actions_client_move(data, TRUE);
78         client_maximize(data->client, TRUE, o->dir);
79         actions_client_move(data, FALSE);
80     }
81     return FALSE;
82 }
83
84 /* Always return FALSE because its not interactive */
85 static gboolean run_func_off(ObActionsData *data, gpointer options)
86 {
87     Options *o = options;
88     if (data->client) {
89         actions_client_move(data, TRUE);
90         client_maximize(data->client, FALSE, o->dir);
91         actions_client_move(data, FALSE);
92     }
93     return FALSE;
94 }
95
96 /* Always return FALSE because its not interactive */
97 static gboolean run_func_toggle(ObActionsData *data, gpointer options)
98 {
99     Options *o = options;
100     if (data->client) {
101         gboolean toggle;
102         actions_client_move(data, TRUE);
103         toggle = ((o->dir == HORZ && !data->client->max_horz) ||
104                   (o->dir == VERT && !data->client->max_vert) ||
105                   (o->dir == BOTH &&
106                    !(data->client->max_horz && data->client->max_vert)));
107         client_maximize(data->client, toggle, o->dir);
108         actions_client_move(data, FALSE);
109     }
110     return FALSE;
111 }
112
113 /* 3.4-compatibility */
114 static gpointer setup_both_func(xmlNodePtr node)
115 {
116     Options *o = g_new0(Options, 1);
117     o->dir = BOTH;
118     return o;
119 }
120
121 static gpointer setup_horz_func(xmlNodePtr node)
122 {
123     Options *o = g_new0(Options, 1);
124     o->dir = HORZ;
125     return o;
126 }
127
128 static gpointer setup_vert_func(xmlNodePtr node)
129 {
130     Options *o = g_new0(Options, 1);
131     o->dir = VERT;
132     return o;
133 }
134