872a16aae0ae1c3906814b7c0814a159e4db4151
[obnox/wireshark/wip.git] / gtk / colors.c
1 /* colors.c
2  * Routines for 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 #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        LTGREY = { 0, 57343, 57343, 57343 };
43 GdkColor        BLACK = { 0, 0, 0, 0 };
44
45 /* Initialize the colors */
46 void
47 colors_init(void)
48 {
49         gboolean got_white, got_black;
50
51         sys_cmap = gdk_colormap_get_system();
52
53         /* Allocate "constant" colors. */
54         got_white = get_color(&WHITE);
55         got_black = get_color(&BLACK);
56
57         /* Got milk? */
58         if (!got_white) {
59                 if (!got_black)
60                         simple_dialog(ESD_TYPE_WARN, ESD_BTN_OK,
61                             "Could not allocate colors black or white.");
62                 else
63                         simple_dialog(ESD_TYPE_WARN, ESD_BTN_OK,
64                             "Could not allocate color white.");
65         } else {
66                 if (!got_black)
67                         simple_dialog(ESD_TYPE_WARN, ESD_BTN_OK,
68                             "Could not allocate color black.");
69         }
70 }
71
72 /* allocate a color from the color map */
73 gboolean
74 get_color(GdkColor *new_color)
75 {
76         GdkVisual *pv;
77
78         if (!our_cmap) {
79                 if (!gdk_colormap_alloc_color (sys_cmap, new_color, FALSE,
80                     TRUE)) {
81                         pv = gdk_visual_get_best();
82                         if (!(our_cmap = gdk_colormap_new(pv, TRUE))) {
83                                 simple_dialog(ESD_TYPE_WARN, ESD_BTN_OK,
84                                     "Could not create new colormap");
85                         }
86                 } else
87                         return (TRUE);
88         }
89         return (gdk_colormap_alloc_color(our_cmap, new_color, FALSE, TRUE));
90 }
91
92 void
93 color_t_to_gdkcolor(GdkColor *target, color_t *source)
94 {
95         target->pixel = source->pixel;
96         target->red   = source->red;
97         target->green = source->green;
98         target->blue  = source->blue;
99 }
100
101 void
102 gdkcolor_to_color_t(color_t *target, GdkColor *source)
103 {
104         target->pixel = source->pixel;
105         target->red   = source->red;
106         target->green = source->green;
107         target->blue  = source->blue;
108 }