AndrewBelt / osdialog

A cross platform wrapper for OS dialogs like file save, open, message boxes, inputs, color picking, etc.
Creative Commons Zero v1.0 Universal
122 stars 19 forks source link

GTK3 Implementation #12

Closed PolyMeilex closed 5 years ago

PolyMeilex commented 5 years ago

I'm writing rust lib based on osdialog, and it is really annoying to deal with all "Deprecated" warning messages in GTK2, so I implemented new version that replaces all deprecated functions.

New version is completely parallel to GTK2 and it uses ARCH=gtk3 parameter

It is also worth noting that I'm not C programmer and it is possible that I made some terrible mistake while writing it.

AndrewBelt commented 5 years ago

This looks very similar to osdialog_gtk2.c, so it would make me want to mirror it when making changes. Is there a way to make that easier?

PolyMeilex commented 5 years ago

Indeed, they are nearly the same.

In file chooser there is only one deprecated function and it is GTK_STOCK_CANCEL we could just update it in gtk2.c because solution recommended by gtk should be backwards compatible, they recommend using "_Cancel" (Doc)

But for color picker things are little bit different, because whole gtk_color_selection_dialog_new() is deprecated and gtk_color_chooser_dialog_new() (Doc) should be used instead, however I'm not sure how backwards compatible it is. Maybe we should use some conditional compilation just inside of color picker.

PolyMeilex commented 5 years ago

I'm not a big fan of this solution, it makes code a little bit messy, but there is no need for needless mirrored files. What are your thoughts?

int osdialog_color_picker(osdialog_color *color, int opacity) {
    if (!color)
        return 0;
    assert(gtk_init_check(NULL, NULL));

    #ifdef gtk3
        GtkWidget *dialog = gtk_color_chooser_dialog_new ("Color",NULL);
        GtkColorChooser *colorsel = GTK_COLOR_CHOOSER(dialog);
        gtk_color_chooser_set_use_alpha(colorsel, opacity);
    #elif gtk2
        GtkWidget *dialog = gtk_color_selection_dialog_new("Color");
        GtkColorSelection *colorsel = GTK_COLOR_SELECTION(gtk_color_selection_dialog_get_color_selection(GTK_COLOR_SELECTION_DIALOG(dialog)));
        gtk_color_selection_set_has_opacity_control(colorsel, opacity);
    #endif

    int result = 0;
    if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_OK) {
        #ifdef gtk3
            GdkRGBA c;
            gtk_color_chooser_get_rgba(colorsel, &c);
            color->r = c.red * 65535 + 0.5;
            color->g = c.green * 65535 + 0.5;
            color->b = c.blue * 65535 + 0.5;
            color->a = c.alpha * 65535 + 0.5;
        #elif gtk2
            GdkColor c;
            gtk_color_selection_get_current_color(colorsel, &c);
            color->r = c.red >> 8;
            color->g = c.green >> 8;
            color->b = c.blue >> 8;
            color->a = gtk_color_selection_get_current_alpha(colorsel) >> 8;
        #endif

        result = 1;
    }

    gtk_widget_destroy(dialog);

    while (gtk_events_pending())
        gtk_main_iteration();
    return result;
}
AndrewBelt commented 5 years ago

Okay sure, could you make osdialog_gtk3.c say

#define OSDIALOG_GTK3
#include "osdialog_gtk2.c"

with the appropriate changes to osdialog_gtk2.c and I'll merge.

PolyMeilex commented 5 years ago

I'm not sure if I understand correctly, You want me to write is so gtk3.c contain only color chooser function and imports everything else from gtk2.c? Did I understand it correctly?

never mind gtk3.c should be empty and point to gtk2 that contains conditional compiling

AndrewBelt commented 5 years ago

Thanks!