move the keyboard and mouse plugins into the kernel for mucho sexiness.
[dana/openbox.git] / plugins / menu / client_menu.c
1 #include "kernel/debug.h"
2 #include "kernel/menu.h"
3 #include "kernel/screen.h"
4 #include "kernel/client.h"
5 #include "kernel/openbox.h"
6 #include "kernel/frame.h"
7
8 #include "render/theme.h"
9
10 #include <glib.h>
11
12 static char *PLUGIN_NAME = "client_menu";
13
14 static ObMenu *send_to_menu;
15 static ObMenu *layer_menu;
16
17 typedef struct {
18     gint foo;
19 } Client_Menu_Data;
20
21 #define CLIENT_MENU(m) ((ObMenu *)m)
22 #define CLIENT_MENU_DATA(m) ((Client_Menu_Data *)((ObMenu *)m)->plugin_data)
23
24 void client_menu_clean_up(ObMenu *m) {
25 }
26
27 void client_send_to_update(ObMenu *self)
28 {
29     guint i = 0;
30     GList *it = self->entries;
31     
32     /* check if we have to update. lame */
33     while (it != NULL) {
34         if (i == screen_desktop) {
35             if (((ObMenuEntry *)it->data)->enabled)
36                 break;
37         } else {
38             if (!((ObMenuEntry *)it->data)->enabled)
39                 break;
40         }
41         if (i >= screen_num_desktops)
42             break;
43         if (strcmp(screen_desktop_names[i],
44                    ((ObMenuEntry *)it->data)->label) != 0)
45             break;
46         ++i;
47         it = it->next;
48     }
49
50     if (it != NULL || i != screen_num_desktops) {
51         menu_clear(self);
52         ob_debug("update\n");
53         for (i = 0; i < screen_num_desktops; ++i) {
54             ObMenuEntry *e;
55             ObAction *a = action_from_string("sendtodesktop");
56             a->data.sendto.desk = i;
57             a->data.sendto.follow = FALSE;
58             e = menu_entry_new(screen_desktop_names[i], a);
59             if (i == screen_desktop)
60                 e->enabled = FALSE;
61             menu_add_entry(self, e);
62         }
63         
64         menu_render(self);
65     }
66 }
67
68 void client_menu_show(ObMenu *self, int x, int y, ObClient *client)
69 {
70     guint i;
71     gint newy, newx;
72     Rect *a = NULL;
73
74     g_assert(!self->invalid);
75     g_assert(client);
76     
77     for (i = 0; i < screen_num_monitors; ++i) {
78         a = screen_physical_area_monitor(i);
79         if (RECT_CONTAINS(*a, x, y))
80             break;
81     }
82     g_assert(a != NULL);
83     self->xin_area = i;
84
85     newx = MAX(x, client->area.x);
86     newy = MAX(y, client->area.y);
87     POINT_SET(self->location,
88               MIN(newx, client->area.x + client->area.width - self->size.width),
89               MIN(newy, client->area.y + client->area.height -
90                   self->size.height));
91     
92     XMoveWindow(ob_display, self->frame, self->location.x, self->location.y);
93
94     if (!self->shown) {
95         XMapWindow(ob_display, self->frame);
96         stacking_raise(MENU_AS_WINDOW(self));
97         self->shown = TRUE;
98     } else if (self->shown && self->open_submenu) {
99         menu_hide(self->open_submenu);
100     }
101 }
102
103 void plugin_setup_config() { }
104
105 void plugin_shutdown() { }
106
107 void plugin_destroy (ObMenu *m)
108 {
109 }
110
111 void *plugin_create() /* TODO: need config */
112 {
113     ObMenu *m = menu_new_full(NULL, "client-menu", NULL,
114                             client_menu_show, NULL, NULL, NULL, NULL);
115     m->plugin = PLUGIN_NAME;
116     menu_add_entry(m, menu_entry_new_submenu("Send To Workspace",
117                                              send_to_menu));
118     send_to_menu->parent = m;
119
120     menu_add_entry(m, menu_entry_new("Iconify",
121                                      action_from_string("iconify")));
122     menu_add_entry(m, menu_entry_new("Raise",
123                                      action_from_string("raise")));
124     menu_add_entry(m, menu_entry_new("Lower",
125                                      action_from_string("lower")));
126     menu_add_entry(m, menu_entry_new("Close",
127                                      action_from_string("close")));
128     menu_add_entry(m, menu_entry_new("Shade",
129                                      action_from_string("toggleshade")));
130     menu_add_entry(m, menu_entry_new("Omnipresent",
131                                      action_from_string("toggleomnipresent")));
132     menu_add_entry(m, menu_entry_new("Decorations",
133                                      action_from_string("toggledecorations")));
134     menu_add_entry(m, menu_entry_new_submenu("Layers",
135                                              layer_menu));
136     layer_menu->parent = m;
137
138     /* send to desktop
139        iconify
140        raise
141        lower
142        close
143        kill
144        shade
145        omnipresent
146        decorations
147     */
148     return (void *)m;
149 }
150
151 void plugin_startup()
152 {
153     ObMenu *t;
154     /* create a Send To Workspace ObMenu */
155     send_to_menu = menu_new_full(NULL, "send-to-workspace",
156                           NULL, NULL, client_send_to_update, NULL, NULL, NULL);
157     
158     layer_menu = menu_new(NULL, "layer", NULL);
159     menu_add_entry(layer_menu, menu_entry_new("Top Layer",
160                                      action_from_string("sendtotoplayer")));
161     menu_add_entry(layer_menu, menu_entry_new("Normal Layer",
162                                      action_from_string("sendtonormallayer")));
163     menu_add_entry(layer_menu, menu_entry_new("Bottom Layer",
164                                      action_from_string("sendtobottomlayer")));
165                           
166     t = (ObMenu *)plugin_create("client_menu");
167 }
168