From Alejandro Vaquero
[obnox/wireshark/wip.git] / gtk / colors.c
1 /* colors.c
2  * Routines for colors
3  *
4  * $Id$
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@ethereal.com>
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 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include <gtk/gtk.h>
29
30 #include <string.h>
31
32 #include "color.h"      /* to declare "color_t" */
33
34 #include "colors.h"
35 #include "simple_dialog.h"
36 #include "gtkglobals.h"
37
38 static GdkColormap*     sys_cmap;
39 static GdkColormap*     our_cmap = NULL;
40
41 GdkColor        WHITE = { 0, 65535, 65535, 65535 };
42 GdkColor        BLACK = { 0, 0, 0, 0 };
43
44 /* Initialize the colors */
45 void
46 colors_init(void)
47 {
48         gboolean got_white, got_black;
49
50         sys_cmap = gdk_colormap_get_system();
51
52         /* Allocate "constant" colors. */
53         got_white = get_color(&WHITE);
54         got_black = get_color(&BLACK);
55
56         /* Got milk? */
57         if (!got_white) {
58                 if (!got_black)
59                         simple_dialog(ESD_TYPE_WARN, ESD_BTN_OK,
60                             "Could not allocate colors black or white.");
61                 else
62                         simple_dialog(ESD_TYPE_WARN, ESD_BTN_OK,
63                             "Could not allocate color white.");
64         } else {
65                 if (!got_black)
66                         simple_dialog(ESD_TYPE_WARN, ESD_BTN_OK,
67                             "Could not allocate color black.");
68         }
69 }
70
71 /* allocate a color from the color map */
72 gboolean
73 get_color(GdkColor *new_color)
74 {
75         GdkVisual *pv;
76
77         if (!our_cmap) {
78                 if (!gdk_colormap_alloc_color (sys_cmap, new_color, FALSE,
79                     TRUE)) {
80                         pv = gdk_visual_get_best();
81                         if (!(our_cmap = gdk_colormap_new(pv, TRUE))) {
82                                 simple_dialog(ESD_TYPE_WARN, ESD_BTN_OK,
83                                     "Could not create new colormap");
84                         }
85                 } else
86                         return (TRUE);
87         }
88         return (gdk_colormap_alloc_color(our_cmap, new_color, FALSE, TRUE));
89 }
90
91 void
92 color_t_to_gdkcolor(GdkColor *target, color_t *source)
93 {
94         target->pixel = source->pixel;
95         target->red   = source->red;
96         target->green = source->green;
97         target->blue  = source->blue;
98 }
99
100 void
101 gdkcolor_to_color_t(color_t *target, GdkColor *source)
102 {
103         target->pixel = source->pixel;
104         target->red   = source->red;
105         target->green = source->green;
106         target->blue  = source->blue;
107 }