summaryrefslogtreecommitdiff
path: root/src/color.c
blob: be63068b70fb3454c5e7fc5c44538d8da8d598bd (plain)
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
/*
 * color.c
 *
 * Copyright (C) 2010 Berislav Kovacki
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 * Authors: Berislav Kovacki <pantokrator@pantokrator.net>
 */

#include "config.h"

#include <string.h>
#include "zenity.h"
#include "util.h"

static ZenityData *zen_data;

static void zenity_colorselection_dialog_response (GtkWidget *widget, int response, gpointer data);

void zenity_colorselection (ZenityData *data, ZenityColorData *color_data)
{
  GtkWidget *dialog;
  GtkWidget *colorsel;
  GtkWidget *button;
  GdkColor color;

  zen_data = data;

  dialog = gtk_color_selection_dialog_new (NULL);

  g_signal_connect (G_OBJECT (dialog), "response",
                    G_CALLBACK (zenity_colorselection_dialog_response),
                    color_data);

  if (data->dialog_title)
    gtk_window_set_title (GTK_WINDOW (dialog), data->dialog_title);

  colorsel = gtk_color_selection_dialog_get_color_selection (GTK_COLOR_SELECTION_DIALOG (dialog));

  if (color_data->color) {
    if (gdk_color_parse (color_data->color, &color))
      gtk_color_selection_set_current_color (GTK_COLOR_SELECTION (colorsel),
                                             &color);
  }

  if (data->extra_label) {
    gint i=0;
    while(data->extra_label[i]!=NULL){
      gtk_dialog_add_button (GTK_DIALOG (dialog), data->extra_label[i], i);
      i++;
    }
  }

  if (data->ok_label) {
    g_object_get (G_OBJECT (dialog), "ok-button", &button, NULL);
    gtk_button_set_label (GTK_BUTTON (button), data->ok_label);
    gtk_button_set_image (GTK_BUTTON (button),
                          gtk_image_new_from_stock (GTK_STOCK_OK, GTK_ICON_SIZE_BUTTON));
    g_object_unref (G_OBJECT (button));
  }

  if (data->cancel_label) {
    g_object_get (G_OBJECT (dialog), "cancel-button", &button, NULL);
    gtk_button_set_label (GTK_BUTTON (button), data->cancel_label);
    gtk_button_set_image (GTK_BUTTON (button), 
                          gtk_image_new_from_stock (GTK_STOCK_CANCEL, GTK_ICON_SIZE_BUTTON));
    g_object_unref (G_OBJECT (button));
  }

  if (data->modal)
    gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);

  gtk_color_selection_set_has_palette (GTK_COLOR_SELECTION (colorsel),
                                       color_data->show_palette);

  zenity_util_show_dialog (dialog, data->attach);

  if (data->timeout_delay > 0) {
    g_timeout_add_seconds (data->timeout_delay,
                           (GSourceFunc) zenity_util_timeout_handle,
                           dialog);
  }

  gtk_main();
}

static void
zenity_colorselection_dialog_response (GtkWidget *widget, int response, gpointer data)
{
  GtkWidget *colorsel;
  GdkColor color;

  switch (response) {
    case GTK_RESPONSE_OK:
      zenity_util_exit_code_with_data(ZENITY_OK, zen_data);      
      colorsel = gtk_color_selection_dialog_get_color_selection (GTK_COLOR_SELECTION_DIALOG (widget));
      gtk_color_selection_get_current_color (GTK_COLOR_SELECTION (colorsel), &color);
      g_print ("%s\n", gdk_color_to_string (&color));
      break;

    case GTK_RESPONSE_CANCEL:
      zen_data->exit_code = zenity_util_return_exit_code (ZENITY_CANCEL);
      break;

    default:
      if (response < g_strv_length(zen_data->extra_label))
        printf("%s\n",zen_data->extra_label[response]);
      zen_data->exit_code = zenity_util_return_exit_code (ZENITY_ESC);
      break;
  }

  gtk_main_quit ();
}
bgstack15