Compare commits

...

2 Commits

Author SHA1 Message Date
Matthias Clasen
58b34f1990 Add an example using GtkApplicationList 2020-07-26 08:38:32 -04:00
Matthias Clasen
3dce8cda54 Add GtkApplicationList 2020-07-25 19:10:38 -04:00
8 changed files with 274 additions and 0 deletions

View File

@@ -76,6 +76,7 @@
<xi:include href="xml/gtkmultiselection.xml" />
</section>
<xi:include href="xml/gtkselectionfiltermodel.xml" />
<xi:include href="xml/gtkapplicationlist.xml" />
<xi:include href="xml/gtkbookmarklist.xml" />
<xi:include href="xml/gtkdirectorylist.xml" />
<xi:include href="xml/gtkstringlist.xml" />

View File

@@ -7639,3 +7639,10 @@ gtk_selection_filter_model_new_for_type
gtk_selection_filter_model_set_model
gtk_selection_filter_model_get_model
</SECTION>
<SECTION>
<FILE>gtkapplicationlist</FILE>
<TITLE>GtkApplicationList</TITLE>
GtkApplicationList
gtk_application_list_new
</SECTION>

View File

@@ -13,6 +13,7 @@ gtk_app_chooser_button_get_type
gtk_app_chooser_dialog_get_type
gtk_app_chooser_widget_get_type
gtk_application_get_type
gtk_application_list_get_type
gtk_application_window_get_type
gtk_aspect_frame_get_type
gtk_assistant_get_type

View File

@@ -43,6 +43,7 @@
#include <gtk/gtkappchooserwidget.h>
#include <gtk/gtkappchooserbutton.h>
#include <gtk/gtkapplication.h>
#include <gtk/gtkapplicationlist.h>
#include <gtk/gtkapplicationwindow.h>
#include <gtk/gtkaspectframe.h>
#include <gtk/gtkassistant.h>

163
gtk/gtkapplicationlist.c Normal file
View File

@@ -0,0 +1,163 @@
/*
* Copyright © 2020 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
* Authors: Matthias Clasen <mclasen@redhat.com>
*/
#include "config.h"
#include "gtkapplicationlist.h"
#include "gtksettings.h"
#include "gtkintl.h"
#include "gtkprivate.h"
/**
* SECTION:gtkapplicationlist
* @title: GtkApplicationList
* @short_description: A list model for applications
* @see_also: #GListModel, #GAppInfo
*
* #GtkApplicationList is a list model that wraps #GAppInfo.
* It presents a #GListModel and fills it with the #GAppInfos for
* the available applications.
*
* The #GtkApplicationList is monitoring for changes and emits
* #GListModel::items-changed when the list of applications changes.
*/
struct _GtkApplicationList
{
GObject parent_instance;
GAppInfoMonitor *monitor;
GSequence *items;
};
struct _GtkApplicationListClass
{
GObjectClass parent_class;
};
static GType
gtk_application_list_get_item_type (GListModel *list)
{
return G_TYPE_APP_INFO;
}
static guint
gtk_application_list_get_n_items (GListModel *list)
{
GtkApplicationList *self = GTK_APPLICATION_LIST (list);
return g_sequence_get_length (self->items);
}
static gpointer
gtk_application_list_get_item (GListModel *list,
guint position)
{
GtkApplicationList *self = GTK_APPLICATION_LIST (list);
GSequenceIter *iter;
iter = g_sequence_get_iter_at_pos (self->items, position);
if (g_sequence_iter_is_end (iter))
return NULL;
else
return g_object_ref (g_sequence_get (iter));
}
static void
gtk_application_list_model_init (GListModelInterface *iface)
{
iface->get_item_type = gtk_application_list_get_item_type;
iface->get_n_items = gtk_application_list_get_n_items;
iface->get_item = gtk_application_list_get_item;
}
G_DEFINE_TYPE_WITH_CODE (GtkApplicationList, gtk_application_list, G_TYPE_OBJECT,
G_IMPLEMENT_INTERFACE (G_TYPE_LIST_MODEL, gtk_application_list_model_init))
static void
apps_changed (GAppInfoMonitor *monitor,
GtkApplicationList *self)
{
guint before;
guint after;
GList *apps, *l;
before = g_sequence_get_length (self->items);
if (before > 0)
g_sequence_remove_range (g_sequence_get_begin_iter (self->items),
g_sequence_get_end_iter (self->items));
apps = g_app_info_get_all ();
for (l = apps; l; l = l->next)
{
GAppInfo *info = l->data;
g_sequence_append (self->items, g_object_ref (info));
}
g_list_free_full (apps, g_object_unref);
after = g_sequence_get_length (self->items);
g_list_model_items_changed (G_LIST_MODEL (self), 0, before, after);
}
static void
gtk_application_list_dispose (GObject *object)
{
GtkApplicationList *self = GTK_APPLICATION_LIST (object);
g_clear_pointer (&self->items, g_sequence_free);
g_signal_handlers_disconnect_by_func (self->monitor, G_CALLBACK (apps_changed), self);
g_clear_object (&self->monitor);
G_OBJECT_CLASS (gtk_application_list_parent_class)->dispose (object);
}
static void
gtk_application_list_class_init (GtkApplicationListClass *class)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (class);
gobject_class->dispose = gtk_application_list_dispose;
}
static void
gtk_application_list_init (GtkApplicationList *self)
{
self->items = g_sequence_new (g_object_unref);
self->monitor = g_app_info_monitor_get ();
g_signal_connect (self->monitor, "changed", G_CALLBACK (apps_changed), self);
apps_changed (self->monitor, self);
}
/**
* gtk_application_list_new:
*
* Creates a new #GtkApplicationList.
*
* Returns: a new #GtkApplicationList
**/
GtkApplicationList *
gtk_application_list_new (void)
{
return g_object_new (GTK_TYPE_APPLICATION_LIST, NULL);
}

45
gtk/gtkapplicationlist.h Normal file
View File

@@ -0,0 +1,45 @@
/*
* Copyright © 2020 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
* Authors: Matthias Clasen <mclasen@redhat.com>
*/
#ifndef __GTK_APPLICATION_LIST_H__
#define __GTK_APPLICATION_LIST_H__
#if !defined (__GTK_H_INSIDE__) && !defined (GTK_COMPILATION)
#error "Only <gtk/gtk.h> can be included directly."
#endif
#include <gio/gio.h>
/* for GDK_AVAILABLE_IN_ALL */
#include <gdk/gdk.h>
G_BEGIN_DECLS
#define GTK_TYPE_APPLICATION_LIST (gtk_application_list_get_type ())
GDK_AVAILABLE_IN_ALL
G_DECLARE_FINAL_TYPE (GtkApplicationList, gtk_application_list, GTK, APPLICATION_LIST, GObject)
GDK_AVAILABLE_IN_ALL
GtkApplicationList * gtk_application_list_new (void);
G_END_DECLS
#endif /* __GTK_APPLICATION_LIST_H__ */

View File

@@ -159,6 +159,7 @@ gtk_public_sources = files([
'gtkappchooserdialog.c',
'gtkappchooserwidget.c',
'gtkapplication.c',
'gtkapplicationlist.c',
'gtkapplicationwindow.c',
'gtkaspectframe.c',
'gtkassistant.c',
@@ -446,6 +447,7 @@ gtk_public_headers = files([
'gtkappchooserdialog.h',
'gtkappchooserwidget.h',
'gtkapplication.h',
'gtkapplicationlist.h',
'gtkapplicationwindow.h',
'gtkaspectframe.h',
'gtkassistant.h',

View File

@@ -431,6 +431,45 @@ selected_changed2 (GtkDropDown *dropdown,
g_object_unref (pair);
}
static void
setup_app_item (GtkSignalListItemFactory *factory,
GtkListItem *item)
{
GtkWidget *box;
GtkWidget *label;
GtkWidget *icon;
box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 10);
icon = gtk_image_new ();
gtk_box_append (GTK_BOX (box), icon);
label = gtk_label_new ("");
gtk_label_set_xalign (GTK_LABEL (label), 0);
gtk_box_append (GTK_BOX (box), label);
gtk_list_item_set_child (item, box);
}
static void
bind_app_item (GtkSignalListItemFactory *factory,
GtkListItem *item)
{
GAppInfo *info;
GtkWidget *box;
GtkWidget *label;
GtkWidget *icon;
info = gtk_list_item_get_item (item);
box = gtk_list_item_get_child (item);
icon = gtk_widget_get_first_child (box);
label = gtk_widget_get_next_sibling (icon);
gtk_image_set_from_gicon (GTK_IMAGE (icon), g_app_info_get_icon (info));
gtk_label_set_label (GTK_LABEL (label), g_app_info_get_display_name (info));
}
int
main (int argc, char *argv[])
{
@@ -454,6 +493,7 @@ main (int argc, char *argv[])
GtkListItemFactory *factory;
GtkWidget *entry;
GtkWidget *hbox;
GtkApplicationList *apps;
gtk_init ();
@@ -560,6 +600,20 @@ main (int argc, char *argv[])
g_object_unref (store);
button = gtk_drop_down_new ();
apps = gtk_application_list_new ();
gtk_drop_down_set_model (GTK_DROP_DOWN (button), G_LIST_MODEL (apps));
g_object_unref (apps);
factory = gtk_signal_list_item_factory_new ();
g_signal_connect (factory, "setup", G_CALLBACK (setup_app_item), NULL);
g_signal_connect (factory, "bind", G_CALLBACK (bind_app_item), NULL);
gtk_drop_down_set_factory (GTK_DROP_DOWN (button), factory);
g_object_unref (factory);
gtk_box_append (GTK_BOX (box), button);
gtk_window_present (GTK_WINDOW (window));
while (!quit)