LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-03-2017, 05:24 AM   #1
Unaaaaab
Member
 
Registered: Jan 2017
Posts: 32

Rep: Reputation: Disabled
Gtk learning from source


I am new at programming Gtk gnome linux C application, and I am trying to learn from some source reagind and online gnome documentation but I can't do it because this is in my opinion very poor and bad. I have a problem understanding this following source: after creating a GObject of GApplication type, how can this main function call other parts of programs? (This is from gedit source) I learnt that GApplication doesn't include any pointer to some code.




/*
* 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: */
 
Old 07-03-2017, 12:27 PM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Please place your code snippets inside [CODE]...[/CODE] tags for better readability. You may type those yourself or click the "#" button in the edit controls.

The Gtk application is instantiated and run from these lines...

Code:
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);
The online documentation for Gtk is usually complete and very useful, although you will need to learn your way around the class/object hierarchy, fromwhich you may find the reference for g_object_new(...) and g_application_run(...).

The complete GApplications reference should be very helpful and lead you to similar pages for each class and their properties and use.

Please become familiar with the structure of GApplications and the online documentation - it is complete and well indexed and linked. If there is a specific part of it that you do not understand please describe your specific difficulty and we will try to help further.

Last edited by astrogeek; 07-03-2017 at 12:29 PM. Reason: Forgot g_object_new link
 
Old 07-03-2017, 12:54 PM   #3
Unaaaaab
Member
 
Registered: Jan 2017
Posts: 32

Original Poster
Rep: Reputation: Disabled
My problem is where this source file can call another; if it not call another object (compiled) file then it wiil not be linked.
How from this source everyone will obtain Gedit?
Please reply!
 
Old 07-03-2017, 04:57 PM   #4
Mill J
Senior Member
 
Registered: Feb 2017
Location: @127.0.0.1
Distribution: Mint, Void, MX, Haiku, PMOS, Plasma Mobile, and many others
Posts: 1,258
Blog Entries: 2

Rep: Reputation: 542Reputation: 542Reputation: 542Reputation: 542Reputation: 542Reputation: 542
Generally you use a makefile. just cd to gedit source directory than run ./configure if you have all the dependencies installed a makefile should be created than run ./make this should be compile and link it.

However if all else fails read directions should be a readme file.
 
1 members found this post helpful.
Old 07-03-2017, 05:48 PM   #5
Unaaaaab
Member
 
Registered: Jan 2017
Posts: 32

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Mill J View Post
Generally you use a makefile. just cd to gedit source directory than run ./configure if you have all the dependencies installed a makefile should be created than run ./make this should be compile and link it.

However if all else fails read directions should be a readme file.


Read carefully: this is not a compile error, but a misunderstand in learning. I am not trying to use gnuautotools but only reading source.
In the source there is that main function that will become entry point of final executable; but how is possible linking whole program (gedit)? What is the function that call from main the remaining of program?

Last edited by Unaaaaab; 07-03-2017 at 05:53 PM.
 
Old 07-03-2017, 11:27 PM   #6
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Quote:
Originally Posted by Unaaaaab View Post
Read carefully: this is not a compile error, but a misunderstand in learning. I am not trying to use gnuautotools but only reading source.
In the source there is that main function that will become entry point of final executable; but how is possible linking whole program (gedit)? What is the function that call from main the remaining of program?
At compile time and link time glib.h, gedit-app.h, and everything they define and include is in scope and is used to build and link the necessary parts of the application. There are various flags and compile-time options mentioned in the above links which determine when static executables, dynamically linked executables and runtime inter-process communication (D-Bus) are used.

Everything at runtime is handled by g_application_run(...), please read the above linked pages for documentation specific to that function. Main is the entry point for all C/C++ programs, and it includes the call to g_application_run(...), which takes a pointer to the application object as an argument, to execute the application.

Code:
main () {
    app = g_object_new(...)
    g_application_run(app)
    exit
}
As Mill J has pointed out, you would generally build with a Makefile configured for the application, whether generated by autotools or of some other origin. The point being that the Makefile would define various compile and link options.

For a complete overview of Gtk application structure, have a look at this page, Overview of GTK+ and its Libraries and this one, GNOME application development overview, for all the details of designing and building Gtk applications, how the libraries interact and the various options available to you.

Last edited by astrogeek; 07-04-2017 at 12:49 AM. Reason: added explicit pointer reference
 
Old 07-05-2017, 07:25 AM   #7
Unaaaaab
Member
 
Registered: Jan 2017
Posts: 32

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by astrogeek View Post
At compile time and link time glib.h, gedit-app.h, and everything they define and include is in scope and is used to build and link the necessary parts of the application. There are various flags and compile-time options mentioned in the above links which determine when static executables, dynamically linked executables and runtime inter-process communication (D-Bus) are used.

Everything at runtime is handled by g_application_run(...), please read the above linked pages for documentation specific to that function. Main is the entry point for all C/C++ programs, and it includes the call to g_application_run(...), which takes a pointer to the application object as an argument, to execute the application.

Code:
main () {
    app = g_object_new(...)
    g_application_run(app)
    exit
}
As Mill J has pointed out, you would generally build with a Makefile configured for the application, whether generated by autotools or of some other origin. The point being that the Makefile would define various compile and link options.

For a complete overview of Gtk application structure, have a look at this page, Overview of GTK+ and its Libraries and this one, GNOME application development overview, for all the details of designing and building Gtk applications, how the libraries interact and the various options available to you.

Okay, I knew so!
But when main function call g_application_run how can it call the remaining program?
There is no pointer to some function, maybe the library has internally some symbol name for call another function??
 
Old 07-05-2017, 02:36 PM   #8
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Please read and try to understand the Gtk application reference material already linked.

Quote:
Originally Posted by Unaaaaab View Post
Okay, I knew so!
But when main function call g_application_run how can it call the remaining program?
There is no pointer to some function, maybe the library has internally some symbol name for call another function??
Yes there is a pointer to the application object...

Code:
GeditApp *app
app = g_object_new(...)
...passed to the function which knows the interface offered by that object...

Code:
g_application_run(app)
I understand that learning a new programming paradigm or library structure can be time consuming and requires effort, but you must make that effort yourself, we cannot do that for you.

Last edited by astrogeek; 07-05-2017 at 02:39 PM.
 
  


Reply

Tags
gtk



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Help learning Dolphin source rhklinux Linux - Newbie 1 05-31-2013 08:22 AM
Learning C with Glade/for GTK+ J_K9 Programming 4 10-31-2005 02:21 PM
GTK source install doesn't give gtk-config Feebles Linux - Software 4 06-15-2005 12:51 PM
building and learning the source narender Linux - Newbie 2 04-16-2004 06:37 AM
GTK Programming Worth Learning drdroid Programming 3 04-14-2004 04:37 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:41 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration