Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Become C++-friendly #16

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open

Become C++-friendly #16

wants to merge 7 commits into from

Conversation

madera
Copy link

@madera madera commented Mar 27, 2021

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

@KaiH-0
Copy link

KaiH-0 commented Oct 16, 2021

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

@madera
Copy link
Author

madera commented Oct 18, 2021

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
Copy link

KaiH-0 commented Oct 18, 2021

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
Copy link
Author

madera commented Oct 18, 2021

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
Copy link

KaiH-0 commented Oct 18, 2021

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
Copy link
Author

madera commented Oct 18, 2021

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
Copy link

KaiH-0 commented Oct 18, 2021

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
Copy link
Author

madera commented Oct 18, 2021

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

@KaiH-0
Copy link

KaiH-0 commented Oct 18, 2021

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

@madera
Copy link
Author

madera commented Oct 18, 2021

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
Copy link
Author

madera commented Oct 18, 2021

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

@KaiH-0
Copy link

KaiH-0 commented Oct 18, 2021

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
Copy link
Author

madera commented Oct 18, 2021

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

@madera
Copy link
Author

madera commented Oct 18, 2021

Edited previous comment for clarity.

@KaiH-0
Copy link

KaiH-0 commented Oct 18, 2021

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
Copy link
Author

madera commented Oct 18, 2021

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
Copy link

KaiH-0 commented Oct 18, 2021

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

Copy link

@IsakTheHacker IsakTheHacker left a comment

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