Skip to content

Become C++-friendly - #16

Open
madera wants to merge 7 commits into
zserge:masterfrom
madera:master
Open

Become C++-friendly#16
madera wants to merge 7 commits into
zserge:masterfrom
madera:master

Conversation

@madera

@madera madera commented Mar 27, 2021

Copy link
Copy Markdown

Set of commits to allow C++ programs to use the library, while simplifying and securing minor code points.

@KaiH-0

KaiH-0 commented Oct 16, 2021

Copy link
Copy Markdown

Hi Madera, I am currently looking for a tray library like this but I'm using C++, how is your fork going?

@madera

madera commented Oct 18, 2021

Copy link
Copy Markdown
Author

Hey there, @KaiH-0 !

I made the code that is on this branch, and never touched it again. It's working, so basically the definition of "no news is good news".

This is not surprising, considering that the API was created before 1995 :)

@KaiH-0

KaiH-0 commented Oct 18, 2021

Copy link
Copy Markdown

Ahh okay, thanks for confirming, I did try your branch as well. I'm guessing its because the code would take so much time to upgrade for modern c++? Thanks for answering.

@madera

madera commented Oct 18, 2021

Copy link
Copy Markdown
Author

Well, since this project isn't mine, I just created the minimum acceptable patch as a PR to be usable with C++. Without this patch the C++ compiler will just get angry at you.

In order to make it "modern" we would need to kill C support and a good rewrite (which would demand time) introducing breaking changes, resulting in a PR that would change 80+% of the project. That would not be a patch anymore.

Anyway, since this does the job and lives well with plain C, I don't see a need for such an endeavor, unless it's purely for fun! XD

@KaiH-0

KaiH-0 commented Oct 18, 2021

Copy link
Copy Markdown

How do you use your C++ patch? As I may of done it wrong. I still seem to get errors when using the code from the example on the readme.md and also get errors if I use the code from example.c in a cpp file, is there anyway to use it in a cpp file?

@madera

madera commented Oct 18, 2021

Copy link
Copy Markdown
Author

Let me extract the code portion that uses it. In 24 hours max I'll post it here for you.

In the meantime, what errors are you getting?

@KaiH-0

KaiH-0 commented Oct 18, 2021

Copy link
Copy Markdown

Thank you so much! Also here are the errors I'm getting:
This line in tray.h: .cbSize = sizeof(MENUITEMINFO), .fMask = MIIM_ID | MIIM_DATA, Error: use of designated initializers requires at least '/std:c++20'.
In toggle_cb: tray_update(&tray); Error: 'tray': illegal use of this type as an expression.
In the struct tray tray: .icon = "icon.png", .menu = menus, Error: use of designated initializers requires at least '/std:c++20'.

This is from using the tray.h file and taking the example from the readme.md

@madera

madera commented Oct 18, 2021

Copy link
Copy Markdown
Author

Is using /std:c++latest not an option?

@KaiH-0

KaiH-0 commented Oct 18, 2021

Copy link
Copy Markdown

That worked! The only error I have now is: tray_update(&tray);
Error: 'tray': illegal use of this type as an expression

@madera

madera commented Oct 18, 2021

Copy link
Copy Markdown
Author

Make sure you are including the tray.h on the top of the example file.

Your compiler might be confusing the type instance (struct tray tray) with the type (struct type).

In order to avoid confusion, rename the tray type to something like "windows_tray", "tray_type", or whatever else you like. Or rename the instance itself, going from "struct tray tray" to "struct tray my_tray" or something.

EDIT: We are going C++-only, so remove the struct from the type. Also, you might consider making it clear:

struct tray tray;

becomes:

::tray tray;

@madera

madera commented Oct 18, 2021

Copy link
Copy Markdown
Author

That's a problem outside of the library, I'm afraid.

@KaiH-0

KaiH-0 commented Oct 18, 2021

Copy link
Copy Markdown

struct tray_menu menus[] = {
{ "Toggle me", 0, 0, toggle_cb, NULL },
{ "-" , 0, 0, NULL , NULL },
{ "Quit" , 0, 0, quit_cb , NULL },
{ NULL , 0, 0, NULL , NULL }
};

I think it means this isn't declared.

@madera

madera commented Oct 18, 2021

Copy link
Copy Markdown
Author

Remove struct from the statement, since you are using C++. Same for all other typedefs.

@madera

madera commented Oct 18, 2021

Copy link
Copy Markdown
Author

Edited previous comment for clarity.

@KaiH-0

KaiH-0 commented Oct 18, 2021

Copy link
Copy Markdown

struct tray_menu menus[] = {
{ "Toggle me", 0, 0, toggle_cb, NULL },
{ "-" , 0, 0, NULL , NULL },
{ "Quit" , 0, 0, quit_cb , NULL },
{ NULL , 0, 0, NULL , NULL }
};

::tray tray = {
.icon = "icon.png",
.menu = menus,
};

void toggle_cb(struct tray_menu *item) {
item->checked = !item->checked;
tray_update(&tray);
}

void quit_cb(struct tray_menu *item) {
tray_exit();
}

In this layout it works fine except it can't find toggle_cb and quit_cb. And I'm not quite sure why. But if I place them above tray_menu menus[], then the tray_update(&tray) is illegal.

@madera

madera commented Oct 18, 2021

Copy link
Copy Markdown
Author

Add a declaration of the functions on the top:

#include <tray.h>
void toggle_cb(struct tray_menu *item);
void quit_cb(struct tray_menu *item);

struct tray_menu menus[] = {
{ "Toggle me", 0, 0, toggle............

@KaiH-0

KaiH-0 commented Oct 18, 2021

Copy link
Copy Markdown

That worked! Thank you so much! I can finally use this in my project!

@IsakTheHacker IsakTheHacker left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be merged!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants