root/sweep/trunk/src/interface.c

Revision 698, 7.7 kB (checked in by erikd, 2 years ago)

Further reduce memory leaks.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 /*
2  * Sweep, a sound wave editor.
3  *
4  * Copyright (C) 2000 Conrad Parker
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #  include <config.h>
23 #endif
24
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <string.h>
30
31 #include <gdk/gdkkeysyms.h>
32 #include <gtk/gtk.h>
33
34 #include <sweep/sweep_i18n.h>
35 #include <sweep/sweep_types.h>
36 #include "sweep_app.h"
37
38 #include "driver.h"
39 #include "callbacks.h"
40 #include "file_dialogs.h"
41 #include "interface.h"
42 #include "about_dialog.h"
43 #include "pixmaps.h"
44 #include "../pixmaps/sweep_app_icon.xpm"  /* wm / taskbar icon */
45
46 GtkStyle * style_wb;
47 GtkStyle * style_bw;
48 GtkStyle * style_LCD;
49 GtkStyle * style_light_grey;
50 GtkStyle * style_green_grey;
51 GtkStyle * style_red_grey;
52 GtkStyle * style_dark_grey;
53 GtkStyle * style_red;
54
55 #if GTK_CHECK_VERSION (2, 10, 0)
56 GtkRecentManager *recent_manager = NULL;
57 #endif
58
59
60 static void
61 init_recent_manager(void)
62 {
63 #if GTK_CHECK_VERSION (2, 10, 0)
64
65   recent_manager = gtk_recent_manager_get_default();
66    
67   /* good idea / bad idea? */
68   if (!recent_manager)
69     recent_manager = gtk_recent_manager_new();
70    
71 #endif   
72 }
73
74 void
75 recent_manager_add_item (gchar *path)
76 {
77 #if GTK_CHECK_VERSION (2, 10, 0)
78
79   gchar *uri;
80    
81   //uri = g_strconcat("file:/", path, NULL);
82   uri = g_filename_to_uri(path, NULL, NULL);
83    
84   if (recent_manager != NULL)
85     gtk_recent_manager_add_item (recent_manager, uri); 
86    
87   g_free(uri);
88 #endif   
89 }
90
91 static void
92 init_accels (void)
93 {
94   gchar * accels_path;
95  
96   accels_path = (char *)g_get_home_dir ();
97   accels_path = g_strconcat (accels_path, "/.sweep/keybindings", NULL);
98   gtk_accel_map_load (accels_path);
99        
100 }
101
102 void
103 save_accels (void)
104 {
105   gchar * accels_path;
106  
107   accels_path = (char *)g_get_home_dir ();
108   accels_path = g_strconcat (accels_path, "/.sweep/keybindings", NULL);
109   gtk_accel_map_save (accels_path);
110        
111 }
112
113
114 void
115 sweep_set_window_icon (GtkWindow *window)
116 {
117   GdkPixbuf * window_icon;
118  
119   if (!GTK_IS_WINDOW(window))
120         return;
121  
122   window_icon = gdk_pixbuf_new_from_xpm_data((const char **)sweep_app_icon_xpm);
123  
124   if (window_icon)
125    {
126       gtk_window_set_icon (GTK_WINDOW (window), window_icon);
127       gdk_pixbuf_unref (window_icon);
128    }
129 }
130
131 GtkWidget *
132 create_widget_from_xpm (GtkWidget * widget, gchar **xpm_data)
133 {
134   GdkBitmap * mask;
135   GdkPixmap * pixmap_data;
136   GdkWindow * window;
137   GdkColormap * colormap;
138
139   window = widget ? widget->window : NULL;
140   colormap = widget ? gtk_widget_get_colormap (widget) :
141     gtk_widget_get_default_colormap();
142
143   pixmap_data = gdk_pixmap_colormap_create_from_xpm_d
144     (window, colormap, &mask, NULL, xpm_data);
145
146   return gtk_pixmap_new (pixmap_data, mask);
147 }
148
149 /*
150  * create_color (r, g, b)
151  *
152  * Ripped from grip, Copyright (c) 1998-2002 Mike Oliphant
153  */
154 GdkColor *
155 create_color (int red, int green, int blue)
156 {
157   GdkColor *c;
158
159   c=(GdkColor *)g_malloc(sizeof(GdkColor));
160   c->red=red;
161   c->green=green;
162   c->blue=blue;
163
164   gdk_color_alloc(gdk_colormap_get_system(),c);
165
166   return c;
167 }
168
169 static gfloat style_color_mods[5]={0.0,-0.1,0.2,-0.2};
170
171 /*
172  * create_style()
173  *
174  * Ripped from grip, Copyright (c) 1998-2002 Mike Oliphant
175  */
176 GtkStyle *
177 create_style (GdkColor * fg, GdkColor * bg, gboolean do_grade)
178 {
179   GtkStyle *def;
180   GtkStyle *sty;
181   int state;
182
183   def=gtk_widget_get_default_style();
184   sty=gtk_style_copy(def);
185
186   for(state=0;state<5;state++) {
187     if(fg) sty->fg[state]=*fg;
188
189     if(bg) sty->bg[state]=*bg;
190
191     if(bg && do_grade) {
192       sty->bg[state].red+=sty->bg[state].red*style_color_mods[state];
193       sty->bg[state].green+=sty->bg[state].green*style_color_mods[state];
194       sty->bg[state].blue+=sty->bg[state].blue*style_color_mods[state];
195     }
196   }
197
198   return sty;
199 }
200
201 void
202 init_styles (void)
203 {
204   GdkColor gdkblack;
205   GdkColor gdkwhite;
206   GdkColor * color_LCD;
207   GdkColor * color_light_grey;
208   GdkColor * color_green_grey;
209   GdkColor * color_red_grey;
210   GdkColor * color_dark_grey;
211   GdkColor * color_red;
212
213   gdk_color_white(gdk_colormap_get_system(),&gdkwhite);
214   gdk_color_black(gdk_colormap_get_system(),&gdkblack);
215  
216   color_LCD = create_color (33686,38273,29557);
217   color_light_grey = create_color (0xaaaa, 0xaaaa, 0xaaaa);
218   color_green_grey = create_color (0xaaaa, 0xbbbb, 0xaaaa);
219   color_red_grey = create_color (0xd3d3, 0x9898, 0x9898);
220   /*  color_dark_grey = create_color (0x4444,0x4444,0x4444);*/
221   color_dark_grey = create_color (0x5555, 0x5555, 0x5555);
222   color_red = create_color (0xffff, 0x0000, 0x0000);
223
224   style_wb = create_style(&gdkwhite,&gdkblack,FALSE);
225   style_bw = create_style(&gdkblack,&gdkwhite,FALSE);
226   style_LCD = create_style(color_LCD, color_LCD, FALSE);
227   style_light_grey = create_style (color_light_grey, color_light_grey, TRUE);
228   style_green_grey = style_light_grey;
229
230   style_red_grey = create_style (color_red_grey, color_red_grey, TRUE);
231
232   style_dark_grey = style_light_grey;
233
234   style_red = create_style (&gdkblack, color_red, FALSE);
235 }
236
237 GtkWidget *
238 create_pixmap_button (GtkWidget * widget, gchar ** xpm_data,
239                       const gchar * tip_text, GtkStyle * style,
240                       sw_toolbar_button_type button_type,
241                       GCallback clicked,
242                       GCallback pressed, GCallback released,
243                       gpointer data)
244 {
245   GtkWidget * pixmap;
246   GtkWidget * button;
247   GtkTooltips * tooltips;
248
249   switch (button_type) {
250   case SW_TOOLBAR_TOGGLE_BUTTON:
251     button = gtk_toggle_button_new ();
252     break;
253   case SW_TOOLBAR_RADIO_BUTTON:
254     button = gtk_radio_button_new (NULL);
255     break;
256   case SW_TOOLBAR_BUTTON:
257   default:
258     button = gtk_button_new ();
259     break;
260   }
261
262   if (xpm_data != NULL) {
263     pixmap = create_widget_from_xpm (widget, xpm_data);
264     gtk_widget_show (pixmap);
265     gtk_container_add (GTK_CONTAINER (button), pixmap);
266   }
267
268   if (tip_text != NULL) {
269     tooltips = gtk_tooltips_new ();
270     gtk_tooltips_set_tip (tooltips, button, tip_text, NULL);
271   }
272
273   if (style != NULL) {
274     gtk_widget_set_style (button, style);
275   }
276
277   if (clicked != NULL) {
278     g_signal_connect (G_OBJECT (button), "clicked",
279                         G_CALLBACK(clicked), data);
280   }
281
282   if (pressed != NULL) {
283     g_signal_connect (G_OBJECT(button), "pressed",
284                         G_CALLBACK(pressed), data);
285   }
286
287   if (released != NULL) {
288     g_signal_connect (G_OBJECT(button), "released",
289                         G_CALLBACK(released), data);
290   }
291
292   return button;
293 }
294
295 /*
296  * close the window when Ctrl and w are pressed.
297  *      accel key combo is static. perhaps there is a better
298  *      way to do this?
299  */
300 void attach_window_close_accel(GtkWindow *window)
301 {
302   GClosure *gclosure;
303   GtkAccelGroup *accel_group;
304        
305   accel_group = gtk_accel_group_new ();
306   gclosure = g_cclosure_new  ((GCallback)close_window_cb, NULL, NULL);
307   gtk_accel_group_connect (accel_group,
308                                              GDK_w,
309                                              GDK_CONTROL_MASK,
310                                              0, /* non of the GtkAccelFlags seem suitable? */
311                                              gclosure);
312   gtk_window_add_accel_group (GTK_WINDOW(window), accel_group);
313 }
314
315 static void
316 release_ui (void)
317 {
318   g_object_unref (recent_manager);
319 }
320
321
322 void init_ui (void)
323 {
324   init_accels();
325   init_styles();
326   init_recent_manager();
327
328   atexit (release_ui);
329 }
Note: See TracBrowser for help on using the browser.