zserge / tray

Cross-platform, super tiny C99 implementation of a system tray icon with a popup menu.
MIT License
511 stars 87 forks source link

Become C++-friendly #16

Open madera opened 3 years ago

madera commented 3 years ago

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

KaiH-0 commented 3 years ago

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

madera commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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

KaiH-0 commented 3 years ago

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

madera commented 3 years ago

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 commented 3 years ago

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

KaiH-0 commented 3 years ago

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 commented 3 years ago

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

madera commented 3 years ago

Edited previous comment for clarity.

KaiH-0 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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