Move gtk to ui/gtk.
[metze/wireshark/wip.git] / ui / gtk / color_utils.c
1 /* color_utils.c
2  * Toolkit-dependent implementations of routines to handle colors.
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28
29 #include <string.h>
30
31 #include <gtk/gtk.h>
32
33 #include "../color.h"
34 #include "../simple_dialog.h"
35
36 #include "ui/gtk/color_utils.h"
37 #include "ui/gtk/gtkglobals.h"
38 #if 0
39 static GdkColormap*     sys_cmap;
40 static GdkColormap*     our_cmap = NULL;
41 #endif
42 GdkColor        WHITE = { 0, 65535, 65535, 65535 };
43 /*GdkColor      LTGREY = { 0, 57343, 57343, 57343 };*/
44 GdkColor        BLACK = { 0, 0, 0, 0 };
45
46 /*
47  * Initialize a color with R, G, and B values, including any toolkit-dependent
48  * work that needs to be done.
49  * Returns TRUE if it succeeds, FALSE if it fails.
50  */
51 gboolean
52 initialize_color(color_t *color, guint16 red, guint16 green, guint16 blue)
53 {
54         GdkColor gdk_color;
55
56         gdk_color.pixel = 0;
57         gdk_color.red = red;
58         gdk_color.green = green;
59         gdk_color.blue = blue;
60 #if 0
61         if (!get_color(&gdk_color))
62                 return FALSE;
63 #endif
64         gdkcolor_to_color_t(color, &gdk_color);
65         return TRUE;
66 }
67
68 /* Initialize the colors */
69 void
70 colors_init(void)
71 {
72 #if 0
73         gboolean got_white, got_black;
74
75         sys_cmap = gdk_colormap_get_system();
76
77         /* Allocate "constant" colors. */
78         got_white = get_color(&WHITE);
79         got_black = get_color(&BLACK);
80         /* Got milk? */
81         if (!got_white) {
82                 if (!got_black)
83                         simple_dialog(ESD_TYPE_WARN, ESD_BTN_OK,
84                                 "Could not allocate colors black or white.");
85                 else
86                         simple_dialog(ESD_TYPE_WARN, ESD_BTN_OK,
87                                 "Could not allocate color white.");
88         } else {
89                 if (!got_black)
90                         simple_dialog(ESD_TYPE_WARN, ESD_BTN_OK,
91                                 "Could not allocate color black.");
92         }
93 #endif
94
95 }
96 #if 0
97 /* allocate a color from the color map */
98 gboolean
99 get_color(GdkColor *new_color)
100 {
101         GdkVisual *pv;
102
103         if (!our_cmap) {
104                 if (!gdk_colormap_alloc_color (sys_cmap, new_color, FALSE,
105                         TRUE)) {
106                         pv = gdk_visual_get_best();
107                         if (!(our_cmap = gdk_colormap_new(pv, TRUE))) {
108                                 simple_dialog(ESD_TYPE_WARN, ESD_BTN_OK,
109                                         "Could not create new colormap");
110                         }
111                 } else
112                         return (TRUE);
113         }
114         return (gdk_colormap_alloc_color(our_cmap, new_color, FALSE, TRUE));
115 }
116 #endif
117 void
118 color_t_to_gdkcolor(GdkColor *target, const color_t *source)
119 {
120         target->pixel = source->pixel;
121         target->red   = source->red;
122         target->green = source->green;
123         target->blue  = source->blue;
124 }
125 #if GTK_CHECK_VERSION(3,0,0)
126 void
127 color_t_to_gdkRGBAcolor(GdkRGBA *target, const color_t *source)
128 {
129         target->alpha = 1;
130         target->red   = source->red / 65535.0;
131         target->green = source->green / 65535.0;
132         target->blue  = source->blue / 65535.0;
133 }
134 #endif
135 void
136 gdkcolor_to_color_t(color_t *target, const GdkColor *source)
137 {
138         target->pixel = source->pixel;
139         target->red   = source->red;
140         target->green = source->green;
141         target->blue  = source->blue;
142 }
143 #if GTK_CHECK_VERSION(3,0,0)
144 void
145 gdkRGBAcolor_to_color_t(color_t *target, const GdkRGBA *source)
146 {
147         target->pixel = 0;
148         target->red   = source->red*65535;
149         target->green = source->green*65535;
150         target->blue  = source->blue*65535;
151 }
152 #endif
153