/* This is the function that creates the GListModel that we need. *GTKlistwidgetsneedaGListModeltodisplay,asmodelssupportchange *notifications. *UnfortunatelyvariousolderAPIsdonotprovidelistmodels,sowecreate *ourown.
*/ static GListModel *
create_application_list (void)
{
GListStore *store;
GList *apps, *l;
/* We use a GListStore here, which is a simple array-like list implementation *formanualmanagement. *Listmodelsneedtoknowwhattypeofdatatheyprovide,soweneedto *providethetypehere.Aswewanttodoalistofapplications,GAppInfo *istheobjectweprovide.
*/
store = g_list_store_new (G_TYPE_APP_INFO);
apps = g_app_info_get_all ();
for (l = apps; l; l = l->next)
g_list_store_append (store, l->data);
g_list_free_full (apps, g_object_unref);
return G_LIST_MODEL (store);
}
/* This is the function we use for setting up new listitems to display. *WeaddjustanGtkImageandaGtkLabelheretodisplaytheapplication's *iconandname,asthisisjustasimpledemo.
*/ staticvoid
setup_listitem_cb (GtkListItemFactory *factory,
GtkListItem *list_item)
{
GtkWidget *box;
GtkWidget *image;
GtkWidget *label;
/* Here we need to prepare the listitem for displaying its item. We get the *listitemalreadysetupfromthepreviousfunction,sowecanreusethe *GtkImagewidgetwesetupabove. *Wegettheitem-whichweknowisaGAppInfobecauseitcomesoutof *themodelwesetupabove,grabitsiconanddisplayit.
*/ staticvoid
bind_listitem_cb (GtkListItemFactory *factory,
GtkListItem *list_item)
{
GtkWidget *image;
GtkWidget *label;
GAppInfo *app_info;
/* In more complex code, we would also need functions to unbind and teardown *thelistitem,butthisissimplecode,sothedefaultimplementationsare *enough.Ifwehadconnectedsignals,thisstepwouldhavebeennecessary. * *TheGtkSignalListItemFactorydocumentationcontainsmoreinformationabout *thisstep.
*/
/* This function is called whenever an item in the list is activated. This is *thesimplewaytoallowreactingtotheEnterkeyordouble-clickingona *listitem. *Ofcourse,itispossibletousefarmorecomplexinteractionsbyturning *offactivationandaddingbuttonsorotherwidgetsinthesetupfunction *above,butthisisasimpledemo,sowe'llusethesimpleway.
*/ staticvoid
activate_cb (GtkListView *list,
guint position,
gpointer unused)
{
GAppInfo *app_info;
GdkAppLaunchContext *context;
GError *error = NULL;
/* Prepare the context for launching the application and launch it. This *codeisexplainedindetailinthedocumentationforGdkAppLaunchContext *andGAppInfo.
*/
context = gdk_display_get_app_launch_context (gtk_widget_get_display (GTK_WIDGET (list))); if (!g_app_info_launch (app_info,
NULL,
G_APP_LAUNCH_CONTEXT (context),
&error))
{
GtkAlertDialog *dialog;
/* And because error handling is important, even a simple demo has it: *Wedisplayanerrordialogthatsomethingwentwrong.
*/
dialog = gtk_alert_dialog_new ("Could not launch %s", g_app_info_get_display_name (app_info));
gtk_alert_dialog_set_detail (dialog, error->message);
gtk_alert_dialog_show (dialog, GTK_WINDOW (gtk_widget_get_root (GTK_WIDGET (list))));
g_object_unref (dialog);
g_clear_error (&error);
}
/* Create a window and set a few defaults */
window = gtk_window_new ();
gtk_window_set_default_size (GTK_WINDOW (window), 640, 320);
gtk_window_set_display (GTK_WINDOW (window),
gtk_widget_get_display (do_widget));
gtk_window_set_title (GTK_WINDOW (window), "Application Launcher");
g_object_add_weak_pointer (G_OBJECT (window), (gpointer *) &window);
/* The GtkListitemFactory is what is used to create GtkListItems *todisplaythedatafromthemodel.Soitisabsolutelynecessary *tocreateone. *WewilluseaGtkSignalListItemFactorybecauseitisthesimplest *onetouse.Differentonesareavailablefordifferentusecases. *ThemostpowerfuloneisGtkBuilderListItemFactorywhichuses *GtkBuilder.uifiles,soitrequireslittlecode.
*/
factory = gtk_signal_list_item_factory_new ();
g_signal_connect (factory, "setup", G_CALLBACK (setup_listitem_cb), NULL);
g_signal_connect (factory, "bind", G_CALLBACK (bind_listitem_cb), NULL);
/* And of course we need to set the data model. Here we call the function *wewroteabovethatgivesusthelistofapplications.Thenweset *itonthelistwidget. *Thelistwillnowtakeitemsfromthemodelandusethefactory *tocreateasmanylistitemsasitneedstoshowitselftotheuser.
*/
model = create_application_list ();
/* Create the list widget here.
*/
list = gtk_list_view_new (GTK_SELECTION_MODEL (gtk_single_selection_new (model)), factory);
/* We connect the activate signal here. It's the function we defined *aboveforlaunchingtheselectedapplication.
*/
g_signal_connect (list, "activate", G_CALLBACK (activate_cb), NULL);
/* List widgets should always be contained in a GtkScrolledWindow, *becauseotherwisetheymightgettoolargeortheymightnot *bescrollable.
*/
sw = gtk_scrolled_window_new ();
gtk_window_set_child (GTK_WINDOW (window), sw);
gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (sw), list);
}
if (!gtk_widget_get_visible (window))
gtk_widget_set_visible (window, TRUE); else
gtk_window_destroy (GTK_WINDOW (window));
return window;
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.12 Sekunden
(vorverarbeitet am 2026-07-02)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.