static GtkWidget * create_submenu_entry (GtkWidget *menu, MateMenuTreeDirectory *directory) { GtkWidget *menuitem; gboolean force_categories_icon; force_categories_icon = g_object_get_data (G_OBJECT (menu), "panel-menu-force-icon-for-categories") != NULL; if (force_categories_icon) menuitem = panel_image_menu_item_new (); else menuitem = gtk_image_menu_item_new (); setup_menuitem_with_icon (menuitem, panel_menu_icon_get_size (), NULL, matemenu_tree_directory_get_icon (directory), matemenu_tree_directory_get_name (directory)); gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem); gtk_widget_show (menuitem); return menuitem; }
static void panel_menu_item_append_volume (GtkWidget *menu, GVolume *volume) { GtkWidget *item; GIcon *icon; char *title; char *tooltip; icon = g_volume_get_icon (volume); title = g_volume_get_name (volume); item = panel_image_menu_item_new (); setup_menuitem_with_icon (item, panel_menu_icon_get_size (), icon, NULL, title); g_object_unref (icon); tooltip = g_strdup_printf (_("Mount %s"), title); panel_util_set_tooltip_text (item, tooltip); g_free (tooltip); g_free (title); gtk_menu_shell_append (GTK_MENU_SHELL (menu), item); g_signal_connect_data (item, "activate", G_CALLBACK (panel_menu_item_mount_volume), g_object_ref (volume), (GClosureNotify) g_object_unref, 0); g_signal_connect (G_OBJECT (item), "button_press_event", G_CALLBACK (menu_dummy_button_press_event), NULL); }
static GtkWidget * panel_menu_items_create_action_item_full (PanelActionButtonType action_type, const char *label, const char *tooltip) { GtkWidget *item; if (panel_action_get_is_disabled (action_type)) return NULL; item = gtk_image_menu_item_new (); setup_menuitem_with_icon (item, panel_menu_icon_get_size (), NULL, panel_action_get_icon_name (action_type), label ? label : panel_action_get_text (action_type)); panel_util_set_tooltip_text (item, tooltip ? tooltip : panel_action_get_tooltip (action_type)); g_signal_connect (item, "activate", panel_action_get_invoke (action_type), NULL); g_signal_connect (G_OBJECT (item), "button_press_event", G_CALLBACK (menu_dummy_button_press_event), NULL); setup_internal_applet_drag (item, action_type); return item; }
static void panel_menu_items_append_place_item (const char *icon_name, GIcon *gicon, const char *title, const char *tooltip, GtkWidget *menu, GCallback callback, const char *uri) { GtkWidget *item; char *user_data; item = panel_image_menu_item_new (); setup_menuitem_with_icon (item, panel_menu_icon_get_size (), gicon, icon_name, title); panel_util_set_tooltip_text (item, tooltip); gtk_menu_shell_append (GTK_MENU_SHELL (menu), item); user_data = g_strdup (uri); g_signal_connect_data (item, "activate", callback, user_data, (GClosureNotify) g_free, 0); g_signal_connect (G_OBJECT (item), "button_press_event", G_CALLBACK (menu_dummy_button_press_event), NULL); if (g_str_has_prefix (uri, "file:")) /*Links only work for local files*/ setup_uri_drag (item, uri, icon_name, GDK_ACTION_LINK); }
static void create_menuitem (GtkWidget *menu, MateMenuTreeEntry *entry, MateMenuTreeDirectory *alias_directory) { GtkWidget *menuitem; menuitem = panel_image_menu_item_new (); g_object_set_data_full (G_OBJECT (menuitem), "panel-menu-tree-entry", matemenu_tree_item_ref (entry), (GDestroyNotify) matemenu_tree_item_unref); if (alias_directory) //FIXME: we should probably use this data when we do dnd or //context menu for this menu item g_object_set_data_full (G_OBJECT (menuitem), "panel-menu-tree-alias-directory", matemenu_tree_item_ref (alias_directory), (GDestroyNotify) matemenu_tree_item_unref); setup_menuitem_with_icon (menuitem, panel_menu_icon_get_size (), NULL, alias_directory ? matemenu_tree_directory_get_icon (alias_directory) : matemenu_tree_entry_get_icon (entry), alias_directory ? matemenu_tree_directory_get_name (alias_directory) : matemenu_tree_entry_get_display_name (entry)); if (alias_directory && matemenu_tree_directory_get_comment (alias_directory)) panel_util_set_tooltip_text (menuitem, matemenu_tree_directory_get_comment (alias_directory)); else if (!alias_directory && matemenu_tree_entry_get_comment (entry)) panel_util_set_tooltip_text (menuitem, matemenu_tree_entry_get_comment (entry)); else if (!alias_directory && matemenu_tree_entry_get_generic_name (entry)) panel_util_set_tooltip_text (menuitem, matemenu_tree_entry_get_generic_name (entry)); g_signal_connect_after (menuitem, "button_press_event", G_CALLBACK (menuitem_button_press_event), NULL); if (!panel_lockdown_get_locked_down ()) { static GtkTargetEntry menu_item_targets[] = { { "text/uri-list", 0, 0 } }; gtk_drag_source_set (menuitem, GDK_BUTTON1_MASK | GDK_BUTTON2_MASK, menu_item_targets, 1, GDK_ACTION_COPY); if (matemenu_tree_entry_get_icon (entry) != NULL) { const char *icon; char *icon_no_ext; icon = matemenu_tree_entry_get_icon (entry); if (!g_path_is_absolute (icon)) { icon_no_ext = panel_xdg_icon_remove_extension (icon); gtk_drag_source_set_icon_name (menuitem, icon_no_ext); g_free (icon_no_ext); } } g_signal_connect (G_OBJECT (menuitem), "drag_begin", G_CALLBACK (drag_begin_menu_cb), NULL); g_signal_connect (menuitem, "drag_data_get", G_CALLBACK (drag_data_get_menu_cb), entry); g_signal_connect (menuitem, "drag_end", G_CALLBACK (drag_end_menu_cb), NULL); } gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem); g_signal_connect (menuitem, "activate", G_CALLBACK (activate_app_def), entry); gtk_widget_show (menuitem); }
/* this is loosely based on update_places() from caja-places-sidebar.c */ static void panel_place_menu_item_append_remote_gio (PanelPlaceMenuItem *place_item, GtkWidget *menu) { GtkWidget *add_menu; GList *mounts, *l; GMount *mount; GSList *add_mounts, *sl; /* add mounts that has no volume (/etc/mtab mounts, ftp, sftp,...) */ mounts = g_volume_monitor_get_mounts (place_item->priv->volume_monitor); add_mounts = NULL; for (l = mounts; l; l = l->next) { GVolume *volume; GFile *root; mount = l->data; if (g_mount_is_shadowed (mount)) { g_object_unref (mount); continue; } volume = g_mount_get_volume (mount); if (volume != NULL) { g_object_unref (volume); g_object_unref (mount); continue; } root = g_mount_get_root (mount); if (g_file_is_native (root)) { g_object_unref (root); g_object_unref (mount); continue; } g_object_unref (root); add_mounts = g_slist_prepend (add_mounts, mount); } add_mounts = g_slist_reverse (add_mounts); if (g_slist_length (add_mounts) <= g_settings_get_uint (place_item->priv->menubar_settings, PANEL_MENU_BAR_MAX_ITEMS_OR_SUBMENU)) { add_menu = menu; } else { GtkWidget *item; item = panel_image_menu_item_new (); setup_menuitem_with_icon (item, panel_menu_icon_get_size (), NULL, PANEL_ICON_NETWORK_SERVER, _("Network Places")); gtk_menu_shell_append (GTK_MENU_SHELL (menu), item); gtk_widget_show (item); add_menu = create_empty_menu (); gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), add_menu); } for (sl = add_mounts; sl; sl = sl->next) { mount = sl->data; panel_menu_item_append_mount (add_menu, mount); g_object_unref (mount); } g_slist_free (add_mounts); g_list_free (mounts); }
/* this is loosely based on update_places() from caja-places-sidebar.c */ static void panel_place_menu_item_append_local_gio (PanelPlaceMenuItem *place_item, GtkWidget *menu) { GList *l; GList *ll; GList *drives; GDrive *drive; GList *volumes; GVolume *volume; GList *mounts; GMount *mount; GSList *items; GSList *sl; PanelGioItem *item; GtkWidget *add_menu; items = NULL; /* first go through all connected drives */ drives = g_volume_monitor_get_connected_drives (place_item->priv->volume_monitor); for (l = drives; l != NULL; l = l->next) { drive = l->data; volumes = g_drive_get_volumes (drive); if (volumes != NULL) { for (ll = volumes; ll != NULL; ll = ll->next) { volume = ll->data; mount = g_volume_get_mount (volume); item = g_slice_new (PanelGioItem); if (mount != NULL) { item->type = PANEL_GIO_MOUNT; item->u.mount = mount; } else { /* Do show the unmounted volumes; this * is so the user can mount it (in case * automounting is off). * * Also, even if automounting is * enabled, this gives a visual cue * that the user should remember to * yank out the media if he just * unmounted it. */ item->type = PANEL_GIO_VOLUME; item->u.volume = g_object_ref (volume); } items = g_slist_prepend (items, item); g_object_unref (volume); } g_list_free (volumes); } else { if (g_drive_is_media_removable (drive) && !g_drive_is_media_check_automatic (drive)) { /* If the drive has no mountable volumes and we * cannot detect media change.. we display the * drive so the user can manually poll the * drive by clicking on it..." * * This is mainly for drives like floppies * where media detection doesn't work.. but * it's also for human beings who like to turn * off media detection in the OS to save * battery juice. */ item = g_slice_new (PanelGioItem); item->type = PANEL_GIO_DRIVE; item->u.drive = g_object_ref (drive); items = g_slist_prepend (items, item); } } g_object_unref (drive); } g_list_free (drives); /* add all volumes that is not associated with a drive */ volumes = g_volume_monitor_get_volumes (place_item->priv->volume_monitor); for (l = volumes; l != NULL; l = l->next) { volume = l->data; drive = g_volume_get_drive (volume); if (drive != NULL) { g_object_unref (volume); g_object_unref (drive); continue; } mount = g_volume_get_mount (volume); item = g_slice_new (PanelGioItem); if (mount != NULL) { item->type = PANEL_GIO_MOUNT; item->u.mount = mount; } else { /* see comment above in why we add an icon for an * unmounted mountable volume */ item->type = PANEL_GIO_VOLUME; item->u.volume = g_object_ref (volume); } items = g_slist_prepend (items, item); g_object_unref (volume); } g_list_free (volumes); /* add mounts that has no volume (/etc/mtab mounts, ftp, sftp,...) */ mounts = g_volume_monitor_get_mounts (place_item->priv->volume_monitor); for (l = mounts; l != NULL; l = l->next) { GFile *root; mount = l->data; if (g_mount_is_shadowed (mount)) { g_object_unref (mount); continue; } volume = g_mount_get_volume (mount); if (volume != NULL) { g_object_unref (volume); g_object_unref (mount); continue; } root = g_mount_get_root (mount); if (!g_file_is_native (root)) { g_object_unref (root); g_object_unref (mount); continue; } g_object_unref (root); item = g_slice_new (PanelGioItem); item->type = PANEL_GIO_MOUNT; item->u.mount = mount; items = g_slist_prepend (items, item); } g_list_free (mounts); /* now that we have everything, add the items inline or in a submenu */ items = g_slist_reverse (items); if (g_slist_length (items) <= g_settings_get_uint (place_item->priv->menubar_settings, PANEL_MENU_BAR_MAX_ITEMS_OR_SUBMENU)) { add_menu = menu; } else { GtkWidget *item; item = gtk_image_menu_item_new (); setup_menuitem_with_icon (item, panel_menu_icon_get_size (), NULL, PANEL_ICON_REMOVABLE_MEDIA, _("Removable Media")); gtk_menu_shell_append (GTK_MENU_SHELL (menu), item); gtk_widget_show (item); add_menu = create_empty_menu (); gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), add_menu); } for (sl = items; sl; sl = sl->next) { item = sl->data; switch (item->type) { case PANEL_GIO_DRIVE: panel_menu_item_append_drive (add_menu, item->u.drive); g_object_unref (item->u.drive); break; case PANEL_GIO_VOLUME: panel_menu_item_append_volume (add_menu, item->u.volume); g_object_unref (item->u.volume); break; case PANEL_GIO_MOUNT: panel_menu_item_append_mount (add_menu, item->u.mount); g_object_unref (item->u.mount); break; default: g_assert_not_reached (); } g_slice_free (PanelGioItem, item); } g_slist_free (items); }
static void panel_place_menu_item_append_gtk_bookmarks (GtkWidget *menu, guint max_items_or_submenu) { typedef struct { char *full_uri; char *label; } PanelBookmark; GtkWidget *add_menu; char *filename; GIOChannel *io_channel; GHashTable *table; int i; GSList *lines = NULL; GSList *add_bookmarks, *l; PanelBookmark *bookmark; filename = g_build_filename (g_get_home_dir (), BOOKMARKS_FILENAME, NULL); io_channel = g_io_channel_new_file (filename, "r", NULL); g_free (filename); if (!io_channel) return; /* We use a hard limit to avoid having users shooting their * own feet, and to avoid crashing the system if a misbehaving * application creates a big bookmarks file. */ for (i = 0; i < MAX_BOOKMARK_ITEMS; i++) { char *contents; gsize length; gsize terminator_pos; GIOStatus status; status = g_io_channel_read_line (io_channel, &contents, &length, &terminator_pos, NULL); if (status != G_IO_STATUS_NORMAL) break; if (length == 0) break; /* Clear the line terminator (\n), if any */ if (terminator_pos > 0) contents[terminator_pos] = '\0'; lines = g_slist_prepend (lines, contents); } g_io_channel_shutdown (io_channel, FALSE, NULL); g_io_channel_unref (io_channel); if (!lines) return; lines = g_slist_reverse (lines); table = g_hash_table_new (g_str_hash, g_str_equal); add_bookmarks = NULL; for (l = lines; l; l = l->next) { char *line = (char*) l->data; if (line[0] && !g_hash_table_lookup (table, line)) { GFile *file; char *space; char *label; gboolean keep; g_hash_table_insert (table, line, line); space = strchr (line, ' '); if (space) { *space = '\0'; label = g_strdup (space + 1); } else { label = NULL; } keep = FALSE; if (g_str_has_prefix (line, "x-caja-search:")) keep = TRUE; if (!keep) { file = g_file_new_for_uri (line); keep = !g_file_is_native (file) || g_file_query_exists (file, NULL); g_object_unref (file); } if (!keep) { if (label) g_free (label); continue; } bookmark = g_malloc (sizeof (PanelBookmark)); bookmark->full_uri = g_strdup (line); bookmark->label = label; add_bookmarks = g_slist_prepend (add_bookmarks, bookmark); } } g_hash_table_destroy (table); g_slist_foreach (lines, (GFunc) g_free, NULL); g_slist_free (lines); add_bookmarks = g_slist_reverse (add_bookmarks); if (g_slist_length (add_bookmarks) <= max_items_or_submenu) { add_menu = menu; } else { GtkWidget *item; item = gtk_image_menu_item_new (); setup_menuitem_with_icon (item, panel_menu_icon_get_size (), NULL, PANEL_ICON_BOOKMARKS, _("Bookmarks")); gtk_menu_shell_append (GTK_MENU_SHELL (menu), item); gtk_widget_show (item); add_menu = create_empty_menu (); gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), add_menu); } for (l = add_bookmarks; l; l = l->next) { char *display_name; char *tooltip; char *label; char *icon; GFile *file; GIcon *gicon; bookmark = l->data; file = g_file_new_for_uri (bookmark->full_uri); display_name = g_file_get_parse_name (file); g_object_unref (file); /* Translators: %s is a URI */ tooltip = g_strdup_printf (_("Open '%s'"), display_name); g_free (display_name); label = NULL; if (bookmark->label) { label = g_strdup (g_strstrip (bookmark->label)); if (!label [0]) { g_free (label); label = NULL; } } if (!label) { label = panel_util_get_label_for_uri (bookmark->full_uri); if (!label) { g_free (tooltip); g_free (bookmark->full_uri); if (bookmark->label) g_free (bookmark->label); g_free (bookmark); continue; } } icon = panel_util_get_icon_for_uri (bookmark->full_uri); /*FIXME: we should probably get a GIcon if possible, so that we * have customized icons for cd-rom, eg */ if (!icon) icon = g_strdup (PANEL_ICON_FOLDER); gicon = g_themed_icon_new_with_default_fallbacks (icon); //FIXME: drag and drop will be broken for x-caja-search uris panel_menu_items_append_place_item (icon, gicon, label, tooltip, add_menu, G_CALLBACK (activate_uri), bookmark->full_uri); g_free (icon); g_object_unref (gicon); g_free (tooltip); g_free (label); g_free (bookmark->full_uri); if (bookmark->label) g_free (bookmark->label); g_free (bookmark); } g_slist_free (add_bookmarks); }
static void panel_menu_items_append_from_desktop (GtkWidget *menu, char *path, char *force_name, gboolean use_icon) { GKeyFile *key_file; gboolean loaded; GtkWidget *item; char *path_freeme; char *full_path; char *uri; char *type; gboolean is_application; char *tryexec; char *icon; char *name; char *comment; path_freeme = NULL; key_file = g_key_file_new (); if (g_path_is_absolute (path)) { loaded = g_key_file_load_from_file (key_file, path, G_KEY_FILE_NONE, NULL); full_path = path; } else { char *lookup_file; char *desktop_path; if (!g_str_has_suffix (path, ".desktop")) { desktop_path = g_strconcat (path, ".desktop", NULL); } else { desktop_path = path; } lookup_file = g_strconcat ("applications", G_DIR_SEPARATOR_S, desktop_path, NULL); loaded = g_key_file_load_from_data_dirs (key_file, lookup_file, &path_freeme, G_KEY_FILE_NONE, NULL); full_path = path_freeme; g_free (lookup_file); if (desktop_path != path) g_free (desktop_path); } if (!loaded) { g_key_file_free (key_file); if (path_freeme) g_free (path_freeme); return; } /* For Application desktop files, respect TryExec */ type = panel_key_file_get_string (key_file, "Type"); if (!type) { g_key_file_free (key_file); if (path_freeme) g_free (path_freeme); return; } is_application = (strcmp (type, "Application") == 0); g_free (type); if (is_application) { tryexec = panel_key_file_get_string (key_file, "TryExec"); if (tryexec) { char *prog; prog = g_find_program_in_path (tryexec); g_free (tryexec); if (!prog) { /* FIXME: we could add some file monitor magic, * so that the menu items appears when the * program appears, but that's really complex * for not a huge benefit */ g_key_file_free (key_file); if (path_freeme) g_free (path_freeme); return; } g_free (prog); } } /* Now, simply build the menu item */ icon = panel_key_file_get_locale_string (key_file, "Icon"); comment = panel_key_file_get_locale_string (key_file, "Comment"); if (PANEL_GLIB_STR_EMPTY (force_name)) name = panel_key_file_get_locale_string (key_file, "Name"); else name = g_strdup (force_name); if (use_icon) { item = panel_image_menu_item_new (); } else { item = gtk_image_menu_item_new (); } setup_menuitem_with_icon (item, panel_menu_icon_get_size (), NULL, icon, name); panel_util_set_tooltip_text (item, comment); gtk_menu_shell_append (GTK_MENU_SHELL (menu), item); g_signal_connect_data (item, "activate", G_CALLBACK (panel_menu_item_activate_desktop_file), g_strdup (full_path), (GClosureNotify) g_free, 0); g_signal_connect (G_OBJECT (item), "button_press_event", G_CALLBACK (menu_dummy_button_press_event), NULL); uri = g_filename_to_uri (full_path, NULL, NULL); setup_uri_drag (item, uri, icon, GDK_ACTION_COPY); g_free (uri); g_key_file_free (key_file); if (icon) g_free (icon); if (name) g_free (name); if (comment) g_free (comment); if (path_freeme) g_free (path_freeme); }