GTK基础:从资源中学习

GTK基础:从资源中学习,gtk,Gtk,我是Gtk gnome linux C应用程序编程新手,我正试图从一些源代码和在线gnome文档中学习,但我不能这样做,因为我认为这是非常糟糕的。我在理解以下来源时遇到了一个问题:在创建了GAApplication类型的GObject之后,这个主函数如何调用程序的其他部分?(这来自gedit源代码)我了解到GaApplication不包含任何指向某些代码的指针 /* * gedit.c * This file is part of gedit * * Copyright (C) 2005

我是Gtk gnome linux C应用程序编程新手,我正试图从一些源代码和在线gnome文档中学习,但我不能这样做,因为我认为这是非常糟糕的。我在理解以下来源时遇到了一个问题:在创建了GAApplication类型的GObject之后,这个主函数如何调用程序的其他部分?(这来自gedit源代码)我了解到GaApplication不包含任何指向某些代码的指针

/*
 * gedit.c
 * This file is part of gedit
 *
 * Copyright (C) 2005 - Paolo Maggi
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "gedit-app.h"
#ifdef OS_OSX
#include "gedit-app-osx.h"
#else
#ifdef G_OS_WIN32
#include "gedit-app-win32.h"
#else
#include "gedit-app-x11.h"
#endif
#endif

#include <glib.h>
#include <locale.h>
#include <libintl.h>

#include "gedit-dirs.h"
#include "gedit-debug.h"

#ifdef G_OS_WIN32
#include <gmodule.h>
static GModule *libgedit_dll = NULL;

/* This code must live in gedit.exe, not in libgedit.dll, since the whole
 * point is to find and load libgedit.dll.
 */
static gboolean
gedit_w32_load_private_dll (void)
{
    gchar *dllpath;
    gchar *prefix;

    prefix = g_win32_get_package_installation_directory_of_module (NULL);

    if (prefix != NULL)
    {
        /* Instead of g_module_open () it may be possible to do any of the
         * following:
         * A) Change PATH to "${dllpath}/lib/gedit;$PATH"
         * B) Call SetDllDirectory ("${dllpath}/lib/gedit")
         * C) Call AddDllDirectory ("${dllpath}/lib/gedit")
         * But since we only have one library, and its name is known, may as well
         * use gmodule.
         */
        dllpath = g_build_filename (prefix, "lib", "gedit", "libgedit.dll", NULL);
        g_free (prefix);

        libgedit_dll = g_module_open (dllpath, 0);
        if (libgedit_dll == NULL)
        {
            g_printerr ("Failed to load '%s': %s\n",
                        dllpath, g_module_error ());
        }

        g_free (dllpath);
    }

    if (libgedit_dll == NULL)
    {
        libgedit_dll = g_module_open ("libgedit.dll", 0);
        if (libgedit_dll == NULL)
        {
            g_printerr ("Failed to load 'libgedit.dll': %s\n",
                        g_module_error ());
        }
    }

    return (libgedit_dll != NULL);
}

static void
gedit_w32_unload_private_dll (void)
{
    if (libgedit_dll)
    {
        g_module_close (libgedit_dll);
        libgedit_dll = NULL;
    }
}
#endif

int
main (int argc, char *argv[])
{
    GType type;
    GeditApp *app;
    gint status;
    const gchar *dir;

#ifdef OS_OSX
    type = GEDIT_TYPE_APP_OSX;
#else
#ifdef G_OS_WIN32
    if (!gedit_w32_load_private_dll ())
    {
        return 1;
    }

    type = GEDIT_TYPE_APP_WIN32;
#else
    type = GEDIT_TYPE_APP_X11;
#endif
#endif

    /* NOTE: we should not make any calls to the gedit api before the
     * private library is loaded */
    gedit_dirs_init ();

    /* Setup locale/gettext */
    setlocale (LC_ALL, "");

    dir = gedit_dirs_get_gedit_locale_dir ();
    bindtextdomain (GETTEXT_PACKAGE, dir);

    bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
    textdomain (GETTEXT_PACKAGE);

    app = g_object_new (type,
                        "application-id", "org.gnome.gedit",
                        "flags", G_APPLICATION_HANDLES_COMMAND_LINE | G_APPLICATION_HANDLES_OPEN,
                        NULL);

    status = g_application_run (G_APPLICATION (app), argc, argv);

    /* Break reference cycles caused by the PeasExtensionSet
     * for GeditAppActivatable which holds a ref on the GeditApp
     */
    g_object_run_dispose (G_OBJECT (app));

    g_object_add_weak_pointer (G_OBJECT (app), (gpointer *) &app);
    g_object_unref (app);

    if (app != NULL)
    {
        gedit_debug_message (DEBUG_APP, "Leaking with %i refs",
                             G_OBJECT (app)->ref_count);
    }

#ifdef G_OS_WIN32
    gedit_w32_unload_private_dll ();
#endif

    return status;
}

/* ex:set ts=8 noet: */
/*
*gedit.c
*此文件是gedit的一部分
*
*版权所有(C)2005-保罗·马吉
*
*这个程序是自由软件;您可以重新分发和/或修改它
*它是根据GNU通用公共许可证的条款发布的
*自由软件基金会;许可证的第2版,或
*(由您选择)任何更高版本。
*
*这个节目的发布是希望它会有用,
*但无任何保证;甚至没有任何关于
*适销性或适合某一特定目的。见
*有关更多详细信息,请参阅GNU通用公共许可证。
*
*您应该已经收到GNU通用公共许可证的副本
*与此同时,;如果没有,请参阅。
*/
#如果定义有配置
#包括
#恩迪夫
#包括“gedit app.h”
#ifdef OS_OSX
#包括“gedit应用程序osx.h”
#否则
#ifdef G_OS_WIN32
#包括“gedit-app-win32.h”
#否则
#包括“gedit-app-x11.h”
#恩迪夫
#恩迪夫
#包括
#包括
#包括
#包括“gedit dirs.h”
#包括“gedit debug.h”
#ifdef G_OS_WIN32
#包括
静态GModule*libgedit_dll=NULL;
/*此代码必须位于gedit.exe中,而不是libgedit.dll中,因为整个
*关键是找到并加载libgedit.dll。
*/
静态gboolean
gedit_w32_load_private_dll(无效)
{
gchar*dllpath;
gchar*前缀;
prefix=g_win32_get_package_installation_directory_of_module(NULL);
if(前缀!=NULL)
{
/*代替g_module_open(),可以执行以下任何操作
*以下:
*A)将路径更改为“${dllpath}/lib/gedit;$PATH”
*B)调用SetDllDirectory(${dllpath}/lib/gedit”)
*C)调用AddDllDirectory(${dllpath}/lib/gedit”)
*但既然我们只有一个图书馆,而且它的名字是众所周知的,那么我们也可以
*使用gmodule。
*/
dllpath=g_build_文件名(前缀为“lib”、“gedit”、“libgedit.dll”,NULL);
g_free(前缀);
libgedit_dll=g_模块_打开(dllpath,0);
if(libgedit_dll==NULL)
{
g_printerr(“未能加载“%s”:%s\n”,
dllpath,g_模块_错误());
}
g_-free(dllpath);
}
if(libgedit_dll==NULL)
{
libgedit_dll=g_模块_打开(“libgedit.dll”,0);
if(libgedit_dll==NULL)
{
g_printerr(“未能加载'libgedit.dll':%s\n”,
g_模块_错误());
}
}
返回(libgedit_dll!=NULL);
}
静态空隙
gedit_w32_unload_private_dll(无效)
{
if(libgedit_dll)
{
g_模块_关闭(libgedit_dll);
libgedit_dll=NULL;
}
}
#恩迪夫
int
main(int argc,char*argv[])
{
G型;
GeditApp*应用程序;
基特状态;
常量gchar*dir;
#ifdef OS_OSX
type=GEDIT\u type\u APP\u OSX;
#否则
#ifdef G_OS_WIN32
如果(!gedit_w32_load_private_dll())
{
返回1;
}
type=GEDIT\u type\u APP\u WIN32;
#否则
类型=GEDIT\U类型\U应用程序\U X11;
#恩迪夫
#恩迪夫
/*注意:我们不应该在
*已加载专用库*/
gedit_dirs_init();
/*设置区域设置/gettext*/
setlocale(LC_ALL,“”);
dir=gedit_dirs_get_gedit_locale_dir();
bindtextdomain(GETTEXT_包,dir);
bind_textdomain_代码集(GETTEXT_包,“UTF-8”);
textdomain(GETTEXT_包);
app=g_对象_新(类型,
“应用程序id”、“org.gnome.gedit”,
“标志”,G|U应用程序句柄|命令行| G|U应用程序句柄|打开,
无效);
状态=g_应用程序运行(g_应用程序(应用程序)、argc、argv);
/*中断由事件引发的引用循环
*对于GEDITAppativatable,它在GEDITApps上保存一个ref
*/
g_object_run_dispose(g_object(app));
g_对象添加弱指针(g_对象(app),(gpointer*)和app);
g_object_unref(应用程序);
如果(应用程序!=NULL)
{
gedit_debug_消息(debug_应用程序,“泄漏,引用%i”,
G_对象(应用)->参考计数);
}
#ifdef G_OS_WIN32
gedit_w32_unload_private_dll();
#恩迪夫
返回状态;
}
/*例如:设置ts=8 noet:*/

main函数不需要调用程序的任何其他部分--
g\u application\u run()
启动mainloop,mainloop将调用需要运行的所有其他部分(计时器、信号处理程序等)。gaapplication实现(在您的示例中可能是GeditAppX11)将是您查看mainloop可能调用的具体内容的起点。找到剩余程序的gaapplication库是什么?哦,我不知道!但是请告诉我它是如何工作的不确定您的意思是什么,“查找剩余的程序”--您可以看到它如何包含gedit-app-x11.h(假设为x11):它将定义一个在g_object_new()行中实例化的GeditAppX11类。GeditAppX11可能会包含并使用很多其他类。。。我没看见!请原谅我的问题!