| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
#include <connman-ui-gtk.h> |
| 23 |
#include <gtkservice.h> |
| 24 |
|
| 25 |
#define CUI_LEFT_MENU_UI_PATH CUI_UI_PATH "/left_menu.ui" |
| 26 |
|
| 27 |
static GtkMenu *cui_left_menu = NULL; |
| 28 |
static GtkMenu *cui_more_menu = NULL; |
| 29 |
static GtkMenuItem *cui_list_more_item = NULL; |
| 30 |
static GHashTable *service_items = NULL; |
| 31 |
static GtkMenuItem *cui_scan_spinner = NULL; |
| 32 |
|
| 33 |
static void add_or_update_service(const char *path, int position) |
| 34 |
{ |
| 35 |
GtkService *s; |
| 36 |
|
| 37 |
s = gtk_service_new(path); |
| 38 |
|
| 39 |
if (position > 9) |
| 40 |
gtk_menu_shell_append(GTK_MENU_SHELL(cui_more_menu), |
| 41 |
(GtkWidget *)s); |
| 42 |
else |
| 43 |
gtk_menu_shell_insert(GTK_MENU_SHELL(cui_left_menu), |
| 44 |
(GtkWidget *)s, position); |
| 45 |
|
| 46 |
gtk_widget_set_visible((GtkWidget *)s, TRUE); |
| 47 |
gtk_widget_show((GtkWidget *)s); |
| 48 |
|
| 49 |
g_hash_table_insert(service_items, s->path, s); |
| 50 |
} |
| 51 |
|
| 52 |
static void remove_service_cb(const char *path) |
| 53 |
{ |
| 54 |
g_hash_table_remove(service_items, path); |
| 55 |
|
| 56 |
|
| 57 |
gtk_menu_reposition(cui_left_menu); |
| 58 |
} |
| 59 |
|
| 60 |
static void accumulate_menu_size(GtkWidget* widget, gpointer data) |
| 61 |
{ |
| 62 |
GtkRequisition *menu_size = (GtkRequisition *)data; |
| 63 |
GtkRequisition item_size; |
| 64 |
gtk_widget_get_preferred_size(widget, NULL, &item_size); |
| 65 |
menu_size->width = MAX(item_size.width, menu_size->width); |
| 66 |
menu_size->height += item_size.height; |
| 67 |
} |
| 68 |
|
| 69 |
static void menu_position_func(GtkMenu *menu, gint *out_x, gint *out_y, |
| 70 |
gboolean *push_in, gpointer user_data) |
| 71 |
{ |
| 72 |
|
| 73 |
GtkWidget *widget = GTK_WIDGET(menu); |
| 74 |
GtkStatusIcon *trayicon = GTK_STATUS_ICON(user_data); |
| 75 |
GtkRequisition requisition; |
| 76 |
gint x, y; |
| 77 |
gint space_left, space_right, space_above, space_below; |
| 78 |
gint needed_width, needed_height, monitor_num; |
| 79 |
GdkScreen *screen; |
| 80 |
GdkRectangle area, monitor; |
| 81 |
GtkBorder padding, margin; |
| 82 |
GtkStyleContext *context; |
| 83 |
GtkStateFlags state; |
| 84 |
gboolean rtl = (gtk_widget_get_direction(widget) == GTK_TEXT_DIR_RTL); |
| 85 |
|
| 86 |
|
| 87 |
if (gtk_status_icon_get_geometry(trayicon, NULL, &area, NULL)) |
| 88 |
{ |
| 89 |
x = area.x + area.width / 2; |
| 90 |
y = area.y + area.height / 2; |
| 91 |
} |
| 92 |
else |
| 93 |
{ |
| 94 |
x = 0; |
| 95 |
y = 0; |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
requisition.width = 0; |
| 100 |
requisition.height = 0; |
| 101 |
gtk_container_foreach(GTK_CONTAINER(cui_left_menu), |
| 102 |
accumulate_menu_size, &requisition); |
| 103 |
gtk_widget_set_size_request(GTK_WIDGET(cui_left_menu), |
| 104 |
requisition.width, requisition.height); |
| 105 |
|
| 106 |
|
| 107 |
context = gtk_widget_get_style_context(widget); |
| 108 |
state = gtk_widget_get_state_flags(widget); |
| 109 |
gtk_style_context_get_padding(context, state, &padding); |
| 110 |
gtk_style_context_get_margin(context, state, &margin); |
| 111 |
|
| 112 |
screen = gtk_widget_get_screen(widget); |
| 113 |
monitor_num = gdk_screen_get_monitor_at_point(screen, x, y); |
| 114 |
gdk_screen_get_monitor_workarea(screen, monitor_num, &monitor); |
| 115 |
|
| 116 |
space_left = x - monitor.x; |
| 117 |
space_right = monitor.x + monitor.width - x - 1; |
| 118 |
space_above = y - monitor.y; |
| 119 |
space_below = monitor.y + monitor.height - y - 1; |
| 120 |
|
| 121 |
|
| 122 |
needed_width = requisition.width - padding.left; |
| 123 |
if (needed_width <= space_left || |
| 124 |
needed_width <= space_right) |
| 125 |
{ |
| 126 |
if ((rtl && needed_width <= space_left) || |
| 127 |
(!rtl && needed_width > space_right)) |
| 128 |
x = x - margin.left + padding.left - requisition.width + 1; |
| 129 |
else |
| 130 |
x = x + margin.right - padding.right; |
| 131 |
} |
| 132 |
else if (requisition.width <= monitor.width) |
| 133 |
{ |
| 134 |
if (space_left > space_right) |
| 135 |
x = monitor.x; |
| 136 |
else |
| 137 |
x = monitor.x + monitor.width - requisition.width; |
| 138 |
} |
| 139 |
else |
| 140 |
{ |
| 141 |
if (rtl) |
| 142 |
x = monitor.x + monitor.width - requisition.width; |
| 143 |
else |
| 144 |
x = monitor.x; |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
needed_height = requisition.height - padding.top; |
| 149 |
if (needed_height <= space_above || |
| 150 |
needed_height <= space_below) |
| 151 |
{ |
| 152 |
if (needed_height <= space_below) |
| 153 |
y = y + margin.top - padding.top; |
| 154 |
else |
| 155 |
y = y - margin.bottom + padding.bottom - requisition.height + 1; |
| 156 |
y = CLAMP(y, monitor.y, |
| 157 |
monitor.y + monitor.height - requisition.height); |
| 158 |
} |
| 159 |
else if (needed_height > space_below && needed_height > space_above) |
| 160 |
{ |
| 161 |
if (space_below >= space_above) |
| 162 |
y = monitor.y + monitor.height - requisition.height; |
| 163 |
else |
| 164 |
y = monitor.y; |
| 165 |
} |
| 166 |
else |
| 167 |
{ |
| 168 |
y = monitor.y; |
| 169 |
} |
| 170 |
|
| 171 |
*out_x = x; |
| 172 |
*out_y = y; |
| 173 |
} |
| 174 |
|
| 175 |
static void get_services_cb(void *user_data) |
| 176 |
{ |
| 177 |
GSList *services, *list; |
| 178 |
int item_position = 2; |
| 179 |
|
| 180 |
services = connman_service_get_services(); |
| 181 |
if (services == NULL) |
| 182 |
return; |
| 183 |
|
| 184 |
gtk_widget_hide(GTK_WIDGET(cui_list_more_item)); |
| 185 |
|
| 186 |
for (list = services; list; list = list->next, item_position++) |
| 187 |
add_or_update_service(list->data, item_position); |
| 188 |
|
| 189 |
if (item_position > 10) |
| 190 |
gtk_widget_show(GTK_WIDGET(cui_list_more_item)); |
| 191 |
|
| 192 |
g_slist_free(services); |
| 193 |
|
| 194 |
|
| 195 |
gtk_menu_reposition(cui_left_menu); |
| 196 |
} |
| 197 |
|
| 198 |
static void scanning_cb(void *user_data) |
| 199 |
{ |
| 200 |
GtkSpinner *spin; |
| 201 |
|
| 202 |
spin = (GtkSpinner *)gtk_bin_get_child(GTK_BIN(cui_scan_spinner)); |
| 203 |
|
| 204 |
gtk_spinner_stop(spin); |
| 205 |
gtk_widget_hide((GtkWidget *)cui_scan_spinner); |
| 206 |
gtk_widget_hide((GtkWidget *)spin); |
| 207 |
|
| 208 |
|
| 209 |
gtk_menu_reposition(cui_left_menu); |
| 210 |
} |
| 211 |
|
| 212 |
static void delete_service_item(gpointer data) |
| 213 |
{ |
| 214 |
GtkWidget *item = data; |
| 215 |
|
| 216 |
gtk_widget_destroy(item); |
| 217 |
} |
| 218 |
|
| 219 |
static void cui_popup_left_menu(GtkStatusIcon *trayicon, |
| 220 |
gpointer user_data) |
| 221 |
{ |
| 222 |
GtkSpinner *spin; |
| 223 |
|
| 224 |
spin = (GtkSpinner *)gtk_bin_get_child(GTK_BIN(cui_scan_spinner)); |
| 225 |
|
| 226 |
gtk_widget_hide(GTK_WIDGET(cui_list_more_item)); |
| 227 |
gtk_widget_show((GtkWidget *)cui_scan_spinner); |
| 228 |
gtk_widget_show((GtkWidget *)spin); |
| 229 |
|
| 230 |
gtk_spinner_start(spin); |
| 231 |
|
| 232 |
connman_service_set_removed_callback(remove_service_cb); |
| 233 |
|
| 234 |
connman_service_refresh_services_list(get_services_cb, |
| 235 |
scanning_cb, user_data); |
| 236 |
|
| 237 |
gtk_menu_popup(cui_left_menu, NULL, NULL, |
| 238 |
menu_position_func, trayicon, 1, 0); |
| 239 |
} |
| 240 |
|
| 241 |
static void cui_popdown_left_menu(GtkMenu *menu, gpointer user_data) |
| 242 |
{ |
| 243 |
connman_service_set_removed_callback(NULL); |
| 244 |
g_hash_table_remove_all(service_items); |
| 245 |
connman_service_free_services_list(); |
| 246 |
} |
| 247 |
|
| 248 |
gint cui_load_left_menu(GtkBuilder *builder, GtkStatusIcon *trayicon) |
| 249 |
{ |
| 250 |
GError *error = NULL; |
| 251 |
|
| 252 |
gtk_builder_add_from_file(builder, CUI_LEFT_MENU_UI_PATH, &error); |
| 253 |
if (error != NULL) { |
| 254 |
printf("Error: %s\n", error->message); |
| 255 |
g_error_free(error); |
| 256 |
|
| 257 |
return -EINVAL; |
| 258 |
} |
| 259 |
|
| 260 |
cui_left_menu = (GtkMenu *) gtk_builder_get_object(builder, |
| 261 |
"cui_left_menu"); |
| 262 |
cui_more_menu = (GtkMenu *) gtk_builder_get_object(builder, |
| 263 |
"cui_more_menu"); |
| 264 |
cui_list_more_item = (GtkMenuItem *) gtk_builder_get_object(builder, |
| 265 |
"cui_list_more_item"); |
| 266 |
cui_scan_spinner = (GtkMenuItem *) gtk_builder_get_object(builder, |
| 267 |
"cui_scan_spinner"); |
| 268 |
|
| 269 |
gtk_container_add(GTK_CONTAINER(cui_scan_spinner), gtk_spinner_new()); |
| 270 |
|
| 271 |
g_signal_connect(cui_left_menu, "deactivate", |
| 272 |
G_CALLBACK(cui_popdown_left_menu), NULL); |
| 273 |
|
| 274 |
service_items = g_hash_table_new_full(g_str_hash, g_str_equal, |
| 275 |
NULL, delete_service_item); |
| 276 |
|
| 277 |
cui_tray_hook_left_menu(cui_popup_left_menu); |
| 278 |
|
| 279 |
return 0; |
| 280 |
} |