]> Pileus Git - ~andy/gtk/blob - gdk/quartz/gdkvisual-quartz.c
quartz: Null check title before setting it
[~andy/gtk] / gdk / quartz / gdkvisual-quartz.c
1 /* gdkvisual-quartz.c
2  *
3  * Copyright (C) 2005 Imendio AB
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #include "config.h"
20
21 #include "gdkvisualprivate.h"
22 #include "gdkquartzvisual.h"
23 #include "gdkprivate-quartz.h"
24
25
26 struct _GdkQuartzVisual
27 {
28   GdkVisual visual;
29 };
30
31 struct _GdkQuartzVisualClass
32 {
33   GdkVisualClass visual_class;
34 };
35
36
37 static GdkVisual *system_visual;
38 static GdkVisual *rgba_visual;
39 static GdkVisual *gray_visual;
40
41 static void
42 gdk_visual_decompose_mask (gulong  mask,
43                            gint   *shift,
44                            gint   *prec)
45 {
46   *shift = 0;
47   *prec = 0;
48
49   while (!(mask & 0x1))
50     {
51       (*shift)++;
52       mask >>= 1;
53     }
54
55   while (mask & 0x1)
56     {
57       (*prec)++;
58       mask >>= 1;
59     }
60 }
61
62 static GdkVisual *
63 create_standard_visual (GdkScreen *screen,
64                         gint       depth)
65 {
66   GdkVisual *visual = g_object_new (GDK_TYPE_QUARTZ_VISUAL, NULL);
67
68   visual->screen = screen;
69
70   visual->depth = depth;
71   visual->byte_order = GDK_MSB_FIRST; /* FIXME: Should this be different on intel macs? */
72   visual->colormap_size = 0;
73
74   visual->type = GDK_VISUAL_TRUE_COLOR;
75
76   visual->red_mask = 0xff0000;
77   visual->green_mask = 0xff00;
78   visual->blue_mask = 0xff;
79   
80   gdk_visual_decompose_mask (visual->red_mask,
81                              &visual->red_shift,
82                              &visual->red_prec);
83   gdk_visual_decompose_mask (visual->green_mask,
84                              &visual->green_shift,
85                              &visual->green_prec);
86   gdk_visual_decompose_mask (visual->blue_mask,
87                              &visual->blue_shift,
88                              &visual->blue_prec);
89
90   return visual;
91 }
92
93 static GdkVisual *
94 create_gray_visual (GdkScreen *screen)
95 {
96   GdkVisual *visual = g_object_new (GDK_TYPE_QUARTZ_VISUAL, NULL);
97
98   visual->screen = screen;
99
100   visual->depth = 1;
101   visual->byte_order = GDK_MSB_FIRST;
102   visual->colormap_size = 0;
103
104   visual->type = GDK_VISUAL_STATIC_GRAY;
105
106   return visual;
107 }
108
109
110 G_DEFINE_TYPE (GdkQuartzVisual, gdk_quartz_visual, GDK_TYPE_VISUAL)
111
112 static void
113 gdk_quartz_visual_init (GdkQuartzVisual *quartz_visual)
114 {
115 }
116
117 static void
118 gdk_quartz_visual_class_init (GdkQuartzVisualClass *class)
119 {
120 }
121
122 /* We prefer the system visual for now ... */
123 gint
124 _gdk_quartz_screen_visual_get_best_depth (GdkScreen *screen)
125 {
126   return system_visual->depth;
127 }
128
129 GdkVisualType
130 _gdk_quartz_screen_visual_get_best_type (GdkScreen *screen)
131 {
132   return system_visual->type;
133 }
134
135 GdkVisual *
136 _gdk_quartz_screen_get_rgba_visual (GdkScreen *screen)
137 {
138   return rgba_visual;
139 }
140
141 GdkVisual*
142 _gdk_quartz_screen_get_system_visual (GdkScreen *screen)
143 {
144   return system_visual;
145 }
146
147 GdkVisual*
148 _gdk_quartz_screen_visual_get_best (GdkScreen *screen)
149 {
150   return system_visual;
151 }
152
153 GdkVisual*
154 _gdk_quartz_screen_visual_get_best_with_depth (GdkScreen *screen,
155                                                gint       depth)
156 {
157   GdkVisual *visual = NULL;
158
159   switch (depth)
160     {
161       case 32:
162         visual = rgba_visual;
163         break;
164
165       case 24:
166         visual = system_visual;
167         break;
168
169       case 1:
170         visual = gray_visual;
171         break;
172
173       default:
174         visual = NULL;
175     }
176
177   return visual;
178 }
179
180 GdkVisual*
181 _gdk_quartz_screen_visual_get_best_with_type (GdkScreen     *screen,
182                                               GdkVisualType  visual_type)
183 {
184   if (system_visual->type == visual_type)
185     return system_visual;
186   else if (gray_visual->type == visual_type)
187     return gray_visual;
188
189   return NULL;
190 }
191
192 GdkVisual*
193 _gdk_quartz_screen_visual_get_best_with_both (GdkScreen     *screen,
194                                               gint           depth,
195                                               GdkVisualType  visual_type)
196 {
197   if (system_visual->depth == depth
198       && system_visual->type == visual_type)
199     return system_visual;
200   else if (rgba_visual->depth == depth
201            && rgba_visual->type == visual_type)
202     return rgba_visual;
203   else if (gray_visual->depth == depth
204            && gray_visual->type == visual_type)
205     return gray_visual;
206
207   return NULL;
208 }
209
210 /* For these, we also prefer the system visual */
211 void
212 _gdk_quartz_screen_query_depths  (GdkScreen  *screen,
213                                   gint      **depths,
214                                   gint       *count)
215 {
216   *count = 1;
217   *depths = &system_visual->depth;
218 }
219
220 void
221 _gdk_quartz_screen_query_visual_types (GdkScreen      *screen,
222                                        GdkVisualType **visual_types,
223                                        gint           *count)
224 {
225   *count = 1;
226   *visual_types = &system_visual->type;
227 }
228
229 void
230 _gdk_quartz_screen_init_visuals (GdkScreen *screen)
231 {
232   system_visual = create_standard_visual (screen, 24);
233   rgba_visual = create_standard_visual (screen, 32);
234   gray_visual = create_gray_visual (screen);
235 }
236
237 GList*
238 _gdk_quartz_screen_list_visuals (GdkScreen *screen)
239 {
240   GList *visuals = NULL;
241
242   visuals = g_list_append (visuals, system_visual);
243   visuals = g_list_append (visuals, rgba_visual);
244   visuals = g_list_append (visuals, gray_visual);
245
246   return visuals;
247 }