Skip to content

Commit b0692ca

Browse files
ptzieglerakurtakov
authored andcommitted
[GTK4] Migrate deprecated ColorChooser to ColorDialog
The Color Chooser has been moved to the GTK3 class and all references inside the GTK4 blocks have been updated to use the new API. Note that the GTK4 API doesn't provide the means to set a custom palette, meaning that the colors specified with setRGBS(RGB[]) are effectively ignored.
1 parent b4fabe5 commit b0692ca

File tree

4 files changed

+298
-25
lines changed

4 files changed

+298
-25
lines changed

bundles/org.eclipse.swt/Eclipse SWT PI/gtk/library/gtk4.c

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,172 @@ JNIEXPORT void JNICALL GTK4_NATIVE(gtk_1check_1button_1set_1use_1underline)
436436
}
437437
#endif
438438

439+
#ifndef NO_gtk_1color_1dialog_1choose_1rgba
440+
JNIEXPORT void JNICALL GTK4_NATIVE(gtk_1color_1dialog_1choose_1rgba)
441+
(JNIEnv *env, jclass that, jlong arg0, jlong arg1, jobject arg2, jlong arg3, jlong arg4, jlong arg5)
442+
{
443+
GdkRGBA _arg2, *lparg2=NULL;
444+
GTK4_NATIVE_ENTER(env, that, gtk_1color_1dialog_1choose_1rgba_FUNC);
445+
if (arg2) if ((lparg2 = getGdkRGBAFields(env, arg2, &_arg2)) == NULL) goto fail;
446+
/*
447+
gtk_color_dialog_choose_rgba(arg0, (GtkWindow *)arg1, (GdkRGBA *)lparg2, (GCancellable *)arg3, (GAsyncReadyCallback)arg4, (gpointer)arg5);
448+
*/
449+
{
450+
GTK4_LOAD_FUNCTION(fp, gtk_color_dialog_choose_rgba)
451+
if (fp) {
452+
((void (CALLING_CONVENTION*)(jlong, GtkWindow *, GdkRGBA *, GCancellable *, GAsyncReadyCallback, gpointer))fp)(arg0, (GtkWindow *)arg1, (GdkRGBA *)lparg2, (GCancellable *)arg3, (GAsyncReadyCallback)arg4, (gpointer)arg5);
453+
}
454+
}
455+
fail:
456+
if (arg2 && lparg2) setGdkRGBAFields(env, arg2, lparg2);
457+
GTK4_NATIVE_EXIT(env, that, gtk_1color_1dialog_1choose_1rgba_FUNC);
458+
}
459+
#endif
460+
461+
#ifndef NO_gtk_1color_1dialog_1choose_1rgba_1finish
462+
JNIEXPORT jlong JNICALL GTK4_NATIVE(gtk_1color_1dialog_1choose_1rgba_1finish)
463+
(JNIEnv *env, jclass that, jlong arg0, jlong arg1, jlongArray arg2)
464+
{
465+
jlong *lparg2=NULL;
466+
jlong rc = 0;
467+
GTK4_NATIVE_ENTER(env, that, gtk_1color_1dialog_1choose_1rgba_1finish_FUNC);
468+
if (arg2) if ((lparg2 = (*env)->GetLongArrayElements(env, arg2, NULL)) == NULL) goto fail;
469+
/*
470+
rc = (jlong)gtk_color_dialog_choose_rgba_finish(arg0, (GAsyncResult *)arg1, (GError **)lparg2);
471+
*/
472+
{
473+
GTK4_LOAD_FUNCTION(fp, gtk_color_dialog_choose_rgba_finish)
474+
if (fp) {
475+
rc = (jlong)((jlong (CALLING_CONVENTION*)(jlong, GAsyncResult *, GError **))fp)(arg0, (GAsyncResult *)arg1, (GError **)lparg2);
476+
}
477+
}
478+
fail:
479+
if (arg2 && lparg2) (*env)->ReleaseLongArrayElements(env, arg2, lparg2, 0);
480+
GTK4_NATIVE_EXIT(env, that, gtk_1color_1dialog_1choose_1rgba_1finish_FUNC);
481+
return rc;
482+
}
483+
#endif
484+
485+
#ifndef NO_gtk_1color_1dialog_1get_1modal
486+
JNIEXPORT jboolean JNICALL GTK4_NATIVE(gtk_1color_1dialog_1get_1modal)
487+
(JNIEnv *env, jclass that, jlong arg0)
488+
{
489+
jboolean rc = 0;
490+
GTK4_NATIVE_ENTER(env, that, gtk_1color_1dialog_1get_1modal_FUNC);
491+
/*
492+
rc = (jboolean)gtk_color_dialog_get_modal(arg0);
493+
*/
494+
{
495+
GTK4_LOAD_FUNCTION(fp, gtk_color_dialog_get_modal)
496+
if (fp) {
497+
rc = (jboolean)((jboolean (CALLING_CONVENTION*)(jlong))fp)(arg0);
498+
}
499+
}
500+
GTK4_NATIVE_EXIT(env, that, gtk_1color_1dialog_1get_1modal_FUNC);
501+
return rc;
502+
}
503+
#endif
504+
505+
#ifndef NO_gtk_1color_1dialog_1get_1with_1alpha
506+
JNIEXPORT jboolean JNICALL GTK4_NATIVE(gtk_1color_1dialog_1get_1with_1alpha)
507+
(JNIEnv *env, jclass that, jlong arg0)
508+
{
509+
jboolean rc = 0;
510+
GTK4_NATIVE_ENTER(env, that, gtk_1color_1dialog_1get_1with_1alpha_FUNC);
511+
/*
512+
rc = (jboolean)gtk_color_dialog_get_with_alpha(arg0);
513+
*/
514+
{
515+
GTK4_LOAD_FUNCTION(fp, gtk_color_dialog_get_with_alpha)
516+
if (fp) {
517+
rc = (jboolean)((jboolean (CALLING_CONVENTION*)(jlong))fp)(arg0);
518+
}
519+
}
520+
GTK4_NATIVE_EXIT(env, that, gtk_1color_1dialog_1get_1with_1alpha_FUNC);
521+
return rc;
522+
}
523+
#endif
524+
525+
#ifndef NO_gtk_1color_1dialog_1new
526+
JNIEXPORT jlong JNICALL GTK4_NATIVE(gtk_1color_1dialog_1new)
527+
(JNIEnv *env, jclass that)
528+
{
529+
jlong rc = 0;
530+
GTK4_NATIVE_ENTER(env, that, gtk_1color_1dialog_1new_FUNC);
531+
/*
532+
rc = (jlong)gtk_color_dialog_new();
533+
*/
534+
{
535+
GTK4_LOAD_FUNCTION(fp, gtk_color_dialog_new)
536+
if (fp) {
537+
rc = (jlong)((jlong (CALLING_CONVENTION*)())fp)();
538+
}
539+
}
540+
GTK4_NATIVE_EXIT(env, that, gtk_1color_1dialog_1new_FUNC);
541+
return rc;
542+
}
543+
#endif
544+
545+
#ifndef NO_gtk_1color_1dialog_1set_1modal
546+
JNIEXPORT void JNICALL GTK4_NATIVE(gtk_1color_1dialog_1set_1modal)
547+
(JNIEnv *env, jclass that, jlong arg0, jboolean arg1)
548+
{
549+
GTK4_NATIVE_ENTER(env, that, gtk_1color_1dialog_1set_1modal_FUNC);
550+
/*
551+
gtk_color_dialog_set_modal(arg0, (gboolean)arg1);
552+
*/
553+
{
554+
GTK4_LOAD_FUNCTION(fp, gtk_color_dialog_set_modal)
555+
if (fp) {
556+
((void (CALLING_CONVENTION*)(jlong, gboolean))fp)(arg0, (gboolean)arg1);
557+
}
558+
}
559+
GTK4_NATIVE_EXIT(env, that, gtk_1color_1dialog_1set_1modal_FUNC);
560+
}
561+
#endif
562+
563+
#ifndef NO_gtk_1color_1dialog_1set_1title
564+
JNIEXPORT jlong JNICALL GTK4_NATIVE(gtk_1color_1dialog_1set_1title)
565+
(JNIEnv *env, jclass that, jlong arg0, jbyteArray arg1)
566+
{
567+
jbyte *lparg1=NULL;
568+
jlong rc = 0;
569+
GTK4_NATIVE_ENTER(env, that, gtk_1color_1dialog_1set_1title_FUNC);
570+
if (arg1) if ((lparg1 = (*env)->GetByteArrayElements(env, arg1, NULL)) == NULL) goto fail;
571+
/*
572+
rc = (jlong)gtk_color_dialog_set_title(arg0, (char *)lparg1);
573+
*/
574+
{
575+
GTK4_LOAD_FUNCTION(fp, gtk_color_dialog_set_title)
576+
if (fp) {
577+
rc = (jlong)((jlong (CALLING_CONVENTION*)(jlong, char *))fp)(arg0, (char *)lparg1);
578+
}
579+
}
580+
fail:
581+
if (arg1 && lparg1) (*env)->ReleaseByteArrayElements(env, arg1, lparg1, 0);
582+
GTK4_NATIVE_EXIT(env, that, gtk_1color_1dialog_1set_1title_FUNC);
583+
return rc;
584+
}
585+
#endif
586+
587+
#ifndef NO_gtk_1color_1dialog_1set_1with_1alpha
588+
JNIEXPORT void JNICALL GTK4_NATIVE(gtk_1color_1dialog_1set_1with_1alpha)
589+
(JNIEnv *env, jclass that, jlong arg0, jboolean arg1)
590+
{
591+
GTK4_NATIVE_ENTER(env, that, gtk_1color_1dialog_1set_1with_1alpha_FUNC);
592+
/*
593+
gtk_color_dialog_set_with_alpha(arg0, (gboolean)arg1);
594+
*/
595+
{
596+
GTK4_LOAD_FUNCTION(fp, gtk_color_dialog_set_with_alpha)
597+
if (fp) {
598+
((void (CALLING_CONVENTION*)(jlong, gboolean))fp)(arg0, (gboolean)arg1);
599+
}
600+
}
601+
GTK4_NATIVE_EXIT(env, that, gtk_1color_1dialog_1set_1with_1alpha_FUNC);
602+
}
603+
#endif
604+
439605
#ifndef NO_gtk_1combo_1box_1get_1child
440606
JNIEXPORT jlong JNICALL GTK4_NATIVE(gtk_1combo_1box_1get_1child)
441607
(JNIEnv *env, jclass that, jlong arg0)

bundles/org.eclipse.swt/Eclipse SWT PI/gtk/library/gtk4_stats.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@ typedef enum {
5858
gtk_1check_1button_1set_1group_FUNC,
5959
gtk_1check_1button_1set_1inconsistent_FUNC,
6060
gtk_1check_1button_1set_1use_1underline_FUNC,
61+
gtk_1color_1dialog_1choose_1rgba_FUNC,
62+
gtk_1color_1dialog_1choose_1rgba_1finish_FUNC,
63+
gtk_1color_1dialog_1get_1modal_FUNC,
64+
gtk_1color_1dialog_1get_1with_1alpha_FUNC,
65+
gtk_1color_1dialog_1new_FUNC,
66+
gtk_1color_1dialog_1set_1modal_FUNC,
67+
gtk_1color_1dialog_1set_1title_FUNC,
68+
gtk_1color_1dialog_1set_1with_1alpha_FUNC,
6169
gtk_1combo_1box_1get_1child_FUNC,
6270
gtk_1css_1provider_1load_1from_1data_FUNC,
6371
gtk_1drag_1source_1new_FUNC,

bundles/org.eclipse.swt/Eclipse SWT PI/gtk/org/eclipse/swt/internal/gtk4/GTK4.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,54 @@ public class GTK4 {
318318
*/
319319
public static final native long gtk_font_dialog_set_title(long self, byte[] title);
320320

321+
/* GtkColorDialog */
322+
/**
323+
* @method flags=dynamic
324+
*/
325+
public static final native long gtk_color_dialog_new();
326+
/**
327+
* @method flags=dynamic
328+
*
329+
* @param title cast=(char *)
330+
*/
331+
public static final native long gtk_color_dialog_set_title(long self, byte[] title);
332+
/**
333+
* @method flags=dynamic
334+
*
335+
* @param with_alpha cast=(gboolean)
336+
*/
337+
public static final native void gtk_color_dialog_set_with_alpha(long self, boolean with_alpha);
338+
/**
339+
* @method flags=dynamic
340+
*/
341+
public static final native boolean gtk_color_dialog_get_with_alpha(long self);
342+
/**
343+
* @method flags=dynamic
344+
*
345+
* @param modal cast=(gboolean)
346+
*/
347+
public static final native void gtk_color_dialog_set_modal(long self, boolean modal);
348+
/**
349+
* @method flags=dynamic
350+
*/
351+
public static final native boolean gtk_color_dialog_get_modal(long self);
352+
/**
353+
* @method flags=dynamic
354+
*
355+
* @param parent cast=(GtkWindow *)
356+
* @param initial_color cast=(GdkRGBA *)
357+
* @param cancellable cast=(GCancellable *)
358+
* @param callback cast=(GAsyncReadyCallback)
359+
* @param user_data cast=(gpointer)
360+
*/
361+
public static final native void gtk_color_dialog_choose_rgba(long self, long parent, GdkRGBA initial_color, long cancellable, long callback, long user_data);
362+
/**
363+
* @method flags=dynamic
364+
*
365+
* @param result cast=(GAsyncResult *)
366+
* @param error cast=(GError **)
367+
*/
368+
public static final native long gtk_color_dialog_choose_rgba_finish(long self, long result, long[] error);
321369

322370
/* GtkScrolledWindow */
323371
public static final native long gtk_scrolled_window_new();

bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/ColorDialog.java

Lines changed: 76 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,18 @@ public RGB[] getRGBs() {
131131
*/
132132
public RGB open () {
133133
byte[] buffer = Converter.javaStringToCString(title);
134-
long handle = GTK.gtk_color_chooser_dialog_new(buffer, parent.topHandle());
134+
long handle;
135+
if (GTK.GTK_VERSION >= OS.VERSION(4, 10, 0)) {
136+
handle = GTK4.gtk_color_dialog_new();
137+
} else {
138+
handle = GTK.gtk_color_chooser_dialog_new(buffer, parent.topHandle());
139+
}
140+
if (handle == 0) error (SWT.ERROR_NO_HANDLES);
141+
if (GTK.GTK_VERSION >= OS.VERSION(4, 10, 0)) {
142+
GTK4.gtk_color_dialog_set_modal(handle, true);
143+
GTK4.gtk_color_dialog_set_title(handle, buffer);
144+
}
145+
135146
Display display = parent != null ? parent.getDisplay(): Display.getCurrent();
136147

137148
GdkRGBA rgba = new GdkRGBA();
@@ -141,34 +152,46 @@ public RGB open () {
141152
rgba.blue = (double) rgb.blue / 255;
142153
rgba.alpha = 1;
143154
}
144-
GTK.gtk_color_chooser_set_rgba (handle, rgba);
145-
if (rgbs != null) {
146-
int colorsPerRow = 9;
147-
long gdkRGBAS = OS.g_malloc(GdkRGBA.sizeof * rgbs.length);
148-
rgba = new GdkRGBA ();
149-
for (int i=0; i<rgbs.length; i++) {
150-
RGB rgbS = rgbs[i];
151-
if (rgbS != null) {
152-
rgba.red = (double) rgbS.red / 255;
153-
rgba.green = (double) rgbS.green / 255;
154-
rgba.blue = (double) rgbS.blue / 255;
155-
OS.memmove (gdkRGBAS + i * GdkRGBA.sizeof, rgba, GdkRGBA.sizeof);
156-
}
155+
156+
if (GTK.GTK_VERSION >= OS.VERSION(4, 10, 0)) {
157+
// TODO The ColorDialog API doesn't support setting a custom palette
158+
// See https://gitlab.gnome.org/GNOME/gtk/-/issues/5936
159+
// > The new GtkColorDialog api doesn't have this feature atm. We might
160+
// > want to bring it back, but judging from application use of color
161+
// > choosers, setting custom palettes is not very popular.
162+
if (GTK4.gtk_color_dialog_get_with_alpha(handle)) {
163+
GTK4.gtk_color_dialog_set_with_alpha(handle, false);
157164
}
158-
GTK.gtk_color_chooser_add_palette(handle, GTK.GTK_ORIENTATION_HORIZONTAL, colorsPerRow,
159-
rgbs.length, gdkRGBAS);
165+
} else {
160166
GTK.gtk_color_chooser_set_rgba (handle, rgba);
167+
if (rgbs != null) {
168+
int colorsPerRow = 9;
169+
long gdkRGBAS = OS.g_malloc(GdkRGBA.sizeof * rgbs.length);
170+
rgba = new GdkRGBA ();
171+
for (int i=0; i<rgbs.length; i++) {
172+
RGB rgbS = rgbs[i];
173+
if (rgbS != null) {
174+
rgba.red = (double) rgbS.red / 255;
175+
rgba.green = (double) rgbS.green / 255;
176+
rgba.blue = (double) rgbS.blue / 255;
177+
OS.memmove (gdkRGBAS + i * GdkRGBA.sizeof, rgba, GdkRGBA.sizeof);
178+
}
179+
}
180+
GTK.gtk_color_chooser_add_palette(handle, GTK.GTK_ORIENTATION_HORIZONTAL, colorsPerRow,
181+
rgbs.length, gdkRGBAS);
182+
GTK.gtk_color_chooser_set_rgba (handle, rgba);
161183

162184

163-
if (GTK.gtk_color_chooser_get_use_alpha(handle)) {
164-
GTK.gtk_color_chooser_set_use_alpha (handle, false);
185+
if (GTK.gtk_color_chooser_get_use_alpha(handle)) {
186+
GTK.gtk_color_chooser_set_use_alpha (handle, false);
187+
}
188+
OS.g_free (gdkRGBAS);
165189
}
166-
OS.g_free (gdkRGBAS);
167190
}
168191

169192
display.addIdleProc();
170193
Dialog oldModal = null;
171-
if (GTK.gtk_window_get_modal(handle)) {
194+
if ((GTK.GTK_VERSION >= OS.VERSION(4, 10, 0)) ? GTK4.gtk_color_dialog_get_modal(handle) : GTK.gtk_window_get_modal(handle)) {
172195
oldModal = display.getModalDialog();
173196
display.setModalDialog(this);
174197
}
@@ -181,7 +204,31 @@ public RGB open () {
181204

182205
int response;
183206
if (GTK.GTK4) {
184-
response = SyncDialogUtil.run(display, handle, false);
207+
if (GTK.GTK_VERSION >= OS.VERSION(4, 10, 0)) {
208+
long shellHandle = parent != null ? parent.topHandle() : 0;
209+
GdkRGBA initialColor = rgba;
210+
long gdkRGBA = SyncDialogUtil.run(display, new AsyncReadyCallback() {
211+
@Override
212+
public void async(long callback) {
213+
GTK4.gtk_color_dialog_choose_rgba(handle, shellHandle, initialColor, 0, callback, 0);
214+
}
215+
216+
@Override
217+
public long await(long result) {
218+
return GTK4.gtk_color_dialog_choose_rgba_finish(handle, result, null);
219+
}
220+
});
221+
222+
if (gdkRGBA != 0) {
223+
OS.memmove(rgba, gdkRGBA, GdkRGBA.sizeof);
224+
GDK.gdk_rgba_free(gdkRGBA);
225+
response = GTK.GTK_RESPONSE_OK;
226+
} else {
227+
response = GTK.GTK_RESPONSE_CANCEL;
228+
}
229+
} else {
230+
response = SyncDialogUtil.run(display, handle, false);
231+
}
185232
} else {
186233
display.externalEventLoop = true;
187234
display.sendPreExternalEventDispatchEvent();
@@ -193,16 +240,18 @@ public RGB open () {
193240
if ((style & SWT.RIGHT_TO_LEFT) != 0) {
194241
OS.g_signal_remove_emission_hook(signalId, hookId);
195242
}
196-
if (GTK.gtk_window_get_modal (handle)) {
243+
if ((GTK.GTK_VERSION >= OS.VERSION(4, 10, 0)) ? GTK4.gtk_color_dialog_get_modal(handle) : GTK.gtk_window_get_modal(handle)) {
197244
display.setModalDialog(oldModal);
198245
}
199246
boolean success = response == GTK.GTK_RESPONSE_OK;
200247
if (success) {
201248
int red = 0;
202249
int green = 0;
203250
int blue = 0;
204-
rgba = new GdkRGBA();
205-
GTK.gtk_color_chooser_get_rgba(handle, rgba);
251+
if (GTK.GTK_VERSION < OS.VERSION(4, 10, 0)) {
252+
rgba = new GdkRGBA();
253+
GTK.gtk_color_chooser_get_rgba(handle, rgba);
254+
}
206255
red = (int) (rgba.red * 255);
207256
green = (int) (rgba.green * 255);
208257
blue = (int) (rgba.blue * 255);
@@ -213,7 +262,9 @@ public RGB open () {
213262

214263
display.removeIdleProc();
215264
if (GTK.GTK4) {
216-
GTK4.gtk_window_destroy(handle);
265+
if (GTK.GTK_VERSION < OS.VERSION(4, 10, 0)) {
266+
GTK4.gtk_window_destroy(handle);
267+
}
217268
} else {
218269
GTK3.gtk_widget_destroy(handle);
219270
}

0 commit comments

Comments
 (0)