Author Topic: GTK+ with C - code completation not for all functions  (Read 10253 times)

Offline Meiner

  • Multiple posting newcomer
  • *
  • Posts: 12
GTK+ with C - code completation not for all functions
« on: February 14, 2012, 11:49:34 am »
Hi,

i started learning GTK+ last week using the CodeBlocks Nightly Build 7671, GCC and the GTK project with a modified script for gtk 2.2. Anything works fine but the code completation. Only a small amount of gtk-functions are available in the completation context menu, e.g. i can't select the gtk_window_new() function. Adding the GTK folder to the additional search paths in the C/C++ parser options had no effect. Am i doing something wrong?

I would be grateful if someone can help me.



(Sorry for mistakes, i'm not a native speaker).
« Last Edit: February 14, 2012, 03:18:38 pm by Meiner »

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 6035
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: GTK+ with C - code completation not for all functions
« Reply #1 on: February 14, 2012, 02:28:25 pm »
It looks like our parser failed to parse the header file containing the "gtk_window_new()" function declaration. Can you show this header files, then I can test the parser to see whether it works OK.
If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.

Offline Meiner

  • Multiple posting newcomer
  • *
  • Posts: 12
Re: GTK+ with C - code completation not for all functions
« Reply #2 on: February 14, 2012, 02:53:35 pm »
Thanks for your answer. I attached the main header file gtk.h and the gtkwindow.h which contains gtk_windows_new().
When there is a need I can give more examples or header files.

Offline Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7256
Re: GTK+ with C - code completation not for all functions
« Reply #3 on: February 14, 2012, 03:04:35 pm »
And please, please, please tell us which version of C::B you use.

Otherwise a usefule hel can not be given, because cc changed a lot since release 10.05 .

Offline Meiner

  • Multiple posting newcomer
  • *
  • Posts: 12
Re: GTK+ with C - code completation not for all functions
« Reply #4 on: February 14, 2012, 03:13:30 pm »
Oh sorry forgot the exact version. I'm using C::B SVN 7671.

EDIT: I've just seen that there is an newer Nightly Build. I will try this later.
« Last Edit: February 14, 2012, 03:20:57 pm by Meiner »

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 6035
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: GTK+ with C - code completation not for all functions
« Reply #5 on: February 14, 2012, 04:02:38 pm »
Thanks for your answer. I attached the main header file gtk.h and the gtkwindow.h which contains gtk_windows_new().
When there is a need I can give more examples or header files.
Here, the name should be:  gtk_window_new NOT gtk_windows_new, So, please be more "careful".

Now, the header file contains some code snippet:
Code
#ifdef G_OS_WIN32
/* Reserve old names for DLL ABI backward compatibility */
#define gtk_window_set_icon_from_file gtk_window_set_icon_from_file_utf8
#define gtk_window_set_default_icon_from_file gtk_window_set_default_icon_from_file_utf8
#endif

GType      gtk_window_get_type                 (void) G_GNUC_CONST;
GtkWidget* gtk_window_new                      (GtkWindowType        type);
void       gtk_window_set_title                (GtkWindow           *window,
const gchar         *title);

But here G_OS_WIN32 is not defined. So, if you have enable the codecompletion's option:
Parse preprocessor directives, which means the code:
Code
#ifdef G_OS_WIN32
will be evaluated, and it return "false", so this branch does not parsed.

Many solutions:
1, find where G_OS_WIN32 was defined, suppose it was defined in "aaa.h", then you can add "aaa.h" to priority headers set, so the "aaa.h" will have a more precedence, and when parsing gtkwindow.h, this preprocessor directive will become true.

2, disable the option: Parse preprocessor directives in CC's option

3, maybe, you can use gcc's preprocessor feature, like gcc -E to generate some dummy header files, which contains all the preprocessed texts, and put such dummy header file in your projects.

Note: The parser in our CC does not do a full parsing like a compiler did. A full parsing contains too many things for a single translate unit, like it should expand all the header files and evaluate and expand all the macro expansions. Our parser just only parser every file once.
If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.

Offline Meiner

  • Multiple posting newcomer
  • *
  • Posts: 12
Re: GTK+ with C - code completation not for all functions
« Reply #6 on: February 14, 2012, 08:05:13 pm »
Thanks for your answer. I attached the main header file gtk.h and the gtkwindow.h which contains gtk_windows_new().
When there is a need I can give more examples or header files.
Here, the name should be:  gtk_window_new NOT gtk_windows_new, So, please be more "careful".

I'm sorry for that. Perhaps I should not post while sitting in a lecture ;).


Many solutions:
1, find where G_OS_WIN32 was defined, suppose it was defined in "aaa.h", then you can add "aaa.h" to priority headers set, so the "aaa.h" will have a more precedence, and when parsing gtkwindow.h, this preprocessor directive will become true.

2, disable the option: Parse preprocessor directives in CC's option

3, maybe, you can use gcc's preprocessor feature, like gcc -E to generate some dummy header files, which contains all the preprocessed texts, and put such dummy header file in your projects.


I tried the second method first (now with Nightly Build 7789), but I have a problem. I can't unselect the option "Parse preprocessor directives". Everytime I unchecked the box leaving the configure editor the option resets automatically. There is no write protection on the config files and it does not work on a second computer with an older C::B Nightly Build either. Is there another protection for the configuration?
I also tried the first suggestion with a reparse of the whole project,  but there is no change in the CC behaviour.



EDIT: I realized that I can change the "Parse preprocessor directives" option only when no project is open. Although then there is the error "No active project found. Settings not saved." it seems that the settings are saved. When there is a project open the option resets itself everytime. However, deactivating the option seems to change nothing (with regard to my problem).
« Last Edit: February 14, 2012, 09:08:02 pm by Meiner »

Offline Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7256
Re: GTK+ with C - code completation not for all functions
« Reply #7 on: February 14, 2012, 08:53:11 pm »
I just tried it on linux with latest trunk.
I can see it in symbols browser, but autocomplete does not work.

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 6035
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: GTK+ with C - code completation not for all functions
« Reply #8 on: February 15, 2012, 02:31:05 am »
This is what I did to solve this problem:
I have see that I should have a macro defined, so I did:
1, create a dummy header file under my project root folder, and named "aaa.h".
aaa.h contains only one line:
Code
#define    G_OS_WIN32 1
2, add this file to project. Also, add "aaa.h" to priority headers list in CC option.
3, Add one token replacement(replace GSEAL(X) to X) in CC option.
See: http://wiki.codeblocks.org/index.php?title=Code_Completion_Design#AAAAA_-.3E_.2A
Code
GSEAL  -> *
Otherwise, this macro will let our parser fail in parsing such statement in "gtkwindow.h"
Code
struct _GtkWindow
{
  GtkBin bin;

  gchar *GSEAL (title);
  gchar *GSEAL (wmclass_name);
  gchar *GSEAL (wmclass_class);
  gchar *GSEAL (wm_role);
So, the parser see it like below:
Code

struct _GtkWindow
{
  GtkBin bin;

  gchar *title;
  gchar *wmclass_name;
  gchar *wmclass_class;
  gchar *wm_role;

Now, it works Here (windowsXP, c::b trunk) screen shot below:



PS: it looks like the priority header change will take effect when I restart C::B. (I will look into in the future :))

If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.

Offline Meiner

  • Multiple posting newcomer
  • *
  • Posts: 12
Re: GTK+ with C - code completation not for all functions
« Reply #9 on: February 15, 2012, 09:00:31 pm »
Thank you very much for your help :D , the token replacement works wonders.

But I got the warning "G_OS_WIN32" redefinend" (glibconfig.h, line 163) when adding such a dummy header file. Moreover I don't see any changes, so I think it makes no difference whether adding the header file or not. Did I miss someting?
« Last Edit: February 15, 2012, 09:23:02 pm by Meiner »

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 6035
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: GTK+ with C - code completation not for all functions
« Reply #10 on: February 16, 2012, 01:04:19 am »
But I got the warning "G_OS_WIN32" redefinend" (glibconfig.h, line 163) when adding such a dummy header file. Moreover I don't see any changes, so I think it makes no difference whether adding the header file or not. Did I miss someting?
Ok, I find that G_OS_WIN32 was defined in the file: glibconfig.h, so you can only put the filename: glibconfig.h in the cc's option->priority header files list, did you do this? then restart C::B.

My suggestion in my previous post did create a dummy header file "aaa.h", but you don't need to include this file in any of your translate unit, adding the dummy header file to your project is just to let our CC's parser to parse it. It is NOT used in building your projects.

As you said, glibconfig.h has already G_OS_WIN32 defined, so you don't need this "aaa.h" file anymore.



If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.

Offline Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7256
Re: GTK+ with C - code completation not for all functions
« Reply #11 on: February 16, 2012, 06:37:17 am »
On linux it seems to be enough to do the token-replacement stuff.
And obviously G_OS_WIN32 is not defined in glibconfig.h on linux anyway.

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9702
Re: GTK+ with C - code completation not for all functions
« Reply #12 on: February 16, 2012, 08:55:55 am »
On linux it seems to be enough to do the token-replacement stuff.
Actually this should really be enough. However, if not - a better way to do it is to provide this as compiler define switch, not as a file.
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: https://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: https://wiki.codeblocks.org/index.php?title=FAQ