File: gtkcolorpickerquartz.c

package info (click to toggle)
gtk4 4.20.3%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 187,060 kB
  • sloc: ansic: 779,084; xml: 3,093; javascript: 3,054; python: 1,911; java: 752; sh: 682; makefile: 315; perl: 162; cpp: 21
file content (154 lines) | stat: -rw-r--r-- 4,507 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/* GTK - The GIMP Toolkit
 * Copyright (C) 2024 the GTK team
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 */

#include "config.h"

#include <AppKit/AppKit.h>

#include "gtkcolorpickerquartzprivate.h"

#include <gio/gio.h>

struct _GtkColorPickerQuartz
{
  GObject parent_instance;

  NSColorSampler *sampler;
  GTask *task;
};

struct _GtkColorPickerQuartzClass
{
  GObjectClass parent_class;
};

static GInitableIface *initable_parent_iface;
static void gtk_color_picker_quartz_initable_iface_init (GInitableIface *iface);
static void gtk_color_picker_quartz_iface_init (GtkColorPickerInterface *iface);

G_DEFINE_TYPE_WITH_CODE (GtkColorPickerQuartz, gtk_color_picker_quartz, G_TYPE_OBJECT,
                         G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, gtk_color_picker_quartz_initable_iface_init)
                         G_IMPLEMENT_INTERFACE (GTK_TYPE_COLOR_PICKER, gtk_color_picker_quartz_iface_init))

static gboolean
gtk_color_picker_quartz_initable_init (GInitable     *initable,
                                       GCancellable  *cancellable,
                                       GError       **error)
{
  GtkColorPickerQuartz *picker = GTK_COLOR_PICKER_QUARTZ (initable);

  picker->sampler = [[NSColorSampler alloc] init];

  return TRUE;
}

static void
gtk_color_picker_quartz_initable_iface_init (GInitableIface *iface)
{
  initable_parent_iface = g_type_interface_peek_parent (iface);
  iface->init = gtk_color_picker_quartz_initable_init;
}

static void
gtk_color_picker_quartz_init (GtkColorPickerQuartz *picker)
{
}

static void
gtk_color_picker_quartz_finalize (GObject *object)
{
  GtkColorPickerQuartz *picker = GTK_COLOR_PICKER_QUARTZ (object);

  g_clear_object (&picker->task);
  if (picker->sampler != NULL)
    {
      [picker->sampler release];
      picker->sampler = NULL;
    }

  G_OBJECT_CLASS (gtk_color_picker_quartz_parent_class)->finalize (object);
}

static void
gtk_color_picker_quartz_class_init (GtkColorPickerQuartzClass *class)
{
  GObjectClass *object_class = G_OBJECT_CLASS (class);

  object_class->finalize = gtk_color_picker_quartz_finalize;
}

GtkColorPicker *
gtk_color_picker_quartz_new (void)
{
  return GTK_COLOR_PICKER (g_initable_new (GTK_TYPE_COLOR_PICKER_QUARTZ, NULL, NULL, NULL));
}


static void
gtk_color_picker_quartz_pick (GtkColorPicker      *cp,
                              GAsyncReadyCallback  callback,
                              gpointer             user_data)
{
  GtkColorPickerQuartz *picker = GTK_COLOR_PICKER_QUARTZ (cp);

  if (picker->task)
    return;

  picker->task = g_task_new (picker, NULL, callback, user_data);

  [picker->sampler showSamplerWithSelectionHandler:^(NSColor *selectedColor) {
    if (selectedColor == NULL)
      {
        g_task_return_new_error (picker->task,
                                 G_IO_ERROR,
                                 G_IO_ERROR_FAILED,
                                 "No color received");
      }
    else
      {
        GdkRGBA rgba = (GdkRGBA){
          [selectedColor redComponent],
          [selectedColor greenComponent],
          [selectedColor blueComponent],
          [selectedColor alphaComponent],
        };

        g_task_return_pointer (picker->task,
                               gdk_rgba_copy (&rgba),
                               (GDestroyNotify) gdk_rgba_free);
      }

    g_clear_object (&picker->task);
  }];
}

static GdkRGBA *
gtk_color_picker_quartz_pick_finish (GtkColorPicker  *cp,
                                     GAsyncResult    *res,
                                     GError         **error)
{
  g_return_val_if_fail (g_task_is_valid (res, cp), NULL);

  return g_task_propagate_pointer (G_TASK (res), error);
}

static void
gtk_color_picker_quartz_iface_init (GtkColorPickerInterface *iface)
{
  iface->pick = gtk_color_picker_quartz_pick;
  iface->pick_finish = gtk_color_picker_quartz_pick_finish;
}