File: confirm.c

package info (click to toggle)
gtimer 0.96-1
  • links: PTS
  • area: main
  • in suites: hamm, slink
  • size: 340 kB
  • ctags: 279
  • sloc: ansic: 3,409; makefile: 137
file content (222 lines) | stat: -rw-r--r-- 6,241 bytes parent folder | download
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
 * GTimer
 *
 * Copyright:
 *	(C) 1998 Craig Knudsen, cknudsen@radix.net
 *	See accompanying file "COPYING".
 * 
 *	This program is free software; you can redistribute it and/or
 *	modify it under the terms of the GNU General Public License
 *	as published by the Free Software Foundation; either version 2
 *	of the License, or (at your option) any later version.
 * 
 *	This program 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 General Public License for more details.
 * 
 *	You should have received a copy of the GNU General Public License
 *	along with this program; if not, write to the
 *	Free Software Foundation, Inc., 59 Temple Place,
 *	Suite 330, Boston, MA  02111-1307, USA
 *
 * Description:
 *	Helps you keep track of time spent on different tasks.
 *
 * Author:
 *	Craig Knudsen, cknudsen@radix.net, http://www.radix.net/~cknudsen
 *
 * Home Page:
 *	http://www.radix.net/~cknudsen/gtimer/
 *
 * History:
 *	18-Mar-1998	Added calls to gtk_window_set_wmclass so the windows
 *			behave better for window managers.
 *	16-Mar-1998	Changed application/about icon.
 *	03-Mar-1998	Created
 *
 ****************************************************************************/


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pwd.h>
#include <time.h>
#include <memory.h>
#include <ctype.h>

#include <gtk/gtk.h>

#include "task.h"
#include "gtimer.h"

#ifdef GTIMER_MEMDEBUG
#include "memdebug/memdebug.h"
#endif

/* icons */
#include "icons/error.xpm"
#include "icons/confirm.xpm"
#include "icons/about.xpm"

static GdkPixmap *error_icon = NULL, *confirm_icon = NULL,
  *gtimer_icon = NULL;
static GdkBitmap *error_mask, *confirm_mask, *gtimer_mask;

extern TaskData **tasks;
extern int num_tasks;

typedef struct {
  char *callback_data;
  GtkWidget *window;
  void (*ok_callback)();
  void (*cancel_callback)();
} ConfirmData;


static void confirm_ok_callback ( widget, data )
GtkWidget *widget;
gpointer data;
{
  ConfirmData *cd = (ConfirmData *) data;

  if ( cd->ok_callback )
    cd->ok_callback ( widget, (gpointer)cd->callback_data );
  gtk_widget_destroy ( cd->window );
  free ( cd );
}


static void confirm_cancel_callback ( widget, data )
GtkWidget *widget;
gpointer data;
{
  ConfirmData *cd = (ConfirmData *) data;

  if ( cd->cancel_callback )
    cd->cancel_callback ( widget, (gpointer)cd->callback_data );
  gtk_widget_destroy ( cd->window );
  free ( cd );
}




/*
** Create the add/edit window.
** It's an add if taskdata is NULL.
*/
GtkWidget *create_confirm_window ( type, title, text, ok_text, cancel_text,
  ok_callback, cancel_callback,
  callback_data )
enum_confirm_type type;
char *title;
char *text;
char *ok_text, *cancel_text;
void (*ok_callback)();
void (*cancel_callback)();
char *callback_data;
{
  GtkWidget *window;
  /*GtkWidget *vbox, *hbox, *table;*/
  GtkWidget *table;
  GtkWidget *pixmap, *label, *ok_button, *cancel_button;
  ConfirmData *cd;
  GdkPixmap *icon = NULL, *mask = NULL;

  cd = (ConfirmData *) malloc ( sizeof ( ConfirmData ) );
  cd->callback_data = callback_data;
  cd->ok_callback = ok_callback;
  cd->cancel_callback = cancel_callback;
  cd->window = window = gtk_dialog_new ();
  gtk_window_set_wmclass ( GTK_WINDOW ( cd->window ), "GTimer", "gtimer" );
  gtk_window_set_title (GTK_WINDOW (window), title );
  gtk_window_position ( GTK_WINDOW(window), GTK_WIN_POS_MOUSE );
  gtk_grab_add ( window );
  gtk_widget_realize ( window );

  table = gtk_table_new (1, 2, FALSE);
  gtk_table_set_row_spacings (GTK_TABLE (table), 4);
  gtk_table_set_col_spacings (GTK_TABLE (table), 8);
  gtk_container_border_width (GTK_CONTAINER (table), 6);
  gtk_box_pack_start ( GTK_BOX ( GTK_DIALOG (window)->vbox ),
    table, TRUE, TRUE, 5 );

  /* Add pixmap */
  switch ( type ) {
    case CONFIRM_ABOUT:
      if ( gtimer_icon == NULL )
        gtimer_icon = gdk_pixmap_create_from_xpm_d (
          GTK_WIDGET ( window )->window, &gtimer_mask,
          &window->style->white, about_xpm );
      icon = gtimer_icon;
      mask = gtimer_mask;
      break;
    case CONFIRM_ERROR:
    case CONFIRM_WARNING:
      if ( error_icon == NULL )
        error_icon = gdk_pixmap_create_from_xpm_d (
          GTK_WIDGET ( window )->window, &error_mask,
          &window->style->white, error_xpm );
      icon = error_icon;
      mask = error_mask;
      break;
    case CONFIRM_CONFIRM:
      if ( confirm_icon == NULL )
        confirm_icon = gdk_pixmap_create_from_xpm_d (
          GTK_WIDGET ( window )->window, &confirm_mask,
          &window->style->white, confirm_xpm );
      icon = confirm_icon;
      mask = confirm_mask;
      break;
    case CONFIRM_MESSAGE:
      break;
  }
  if ( icon ) {
    pixmap = gtk_pixmap_new ( icon, mask );
    gtk_misc_set_alignment (GTK_MISC (pixmap), 0.0, 0.5);
    gtk_table_attach (GTK_TABLE (table), pixmap, 0, 1, 0, 1,
      GTK_FILL, GTK_FILL, 0, 0);
    gtk_widget_show ( pixmap );
  }

  /* Add message */
  label = gtk_label_new ( text );
  gtk_table_attach_defaults (GTK_TABLE (table), label, 1, 2, 0, 1);
  gtk_widget_show ( label );

  gtk_widget_show ( table );
  
  /* add command buttons */

  if ( ok_text ) {
    ok_button = gtk_button_new_with_label ( ok_text );
    gtk_box_pack_start (GTK_BOX (GTK_DIALOG (window)->action_area),
      ok_button, FALSE, FALSE, 10);
    gtk_signal_connect (GTK_OBJECT (ok_button), "clicked",
      GTK_SIGNAL_FUNC (confirm_ok_callback), cd);
    GTK_WIDGET_SET_FLAGS ( ok_button, GTK_CAN_DEFAULT );
    gtk_widget_grab_default ( ok_button );
    gtk_widget_show (ok_button);
  }

  if ( cancel_text ) {
    cancel_button = gtk_button_new_with_label ( cancel_text );
    gtk_box_pack_start (GTK_BOX (GTK_DIALOG (window)->action_area),
      cancel_button, FALSE, FALSE, 10);
    gtk_signal_connect (GTK_OBJECT (cancel_button), "clicked",
      GTK_SIGNAL_FUNC (confirm_cancel_callback), cd);
    if ( ! ok_text ) {
      GTK_WIDGET_SET_FLAGS ( cancel_button, GTK_CAN_DEFAULT );
      gtk_widget_grab_default ( cancel_button );
    }
    gtk_widget_show (cancel_button);
  }

  gtk_widget_show (window);

  return ( window );
}