comparison gtk/dw.c @ 973:5c5447fd0cc9

Step 1 in the modernization of the GTK1/2 module... see comments in GTK3 commit.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Thu, 05 May 2011 23:11:25 +0000
parents 61732be875c7
children e506776e7fd3
comparison
equal deleted inserted replaced
972:32830f1683c9 973:5c5447fd0cc9
108 ".ico", 108 ".ico",
109 ".jpg", 109 ".jpg",
110 ".bmp", 110 ".bmp",
111 }; 111 };
112 112
113 #define DW_THREAD_LIMIT 50
114
115 #ifndef max 113 #ifndef max
116 # define max(a,b) (((a) > (b)) ? (a) : (b)) 114 # define max(a,b) (((a) > (b)) ? (a) : (b))
117 #endif 115 #endif
118 116
119 #ifndef min 117 #ifndef min
120 # define min(a,b) (((a) < (b)) ? (a) : (b)) 118 # define min(a,b) (((a) < (b)) ? (a) : (b))
121 #endif 119 #endif
122 120
123 FILE *dbgfp = NULL; 121 FILE *dbgfp = NULL;
124 122
125 DWTID _dw_thread_list[DW_THREAD_LIMIT]; 123 pthread_key_t _dw_fg_color_key;
126 GdkColor _foreground[DW_THREAD_LIMIT]; 124 pthread_key_t _dw_bg_color_key;
127 GdkColor _background[DW_THREAD_LIMIT]; 125 pthread_key_t _dw_mutex_key;
128 int _transparent[DW_THREAD_LIMIT];
129 GtkClipboard *_clipboard_object[DW_THREAD_LIMIT];
130 gchar *_clipboard_contents[DW_THREAD_LIMIT];
131 126
132 GtkWidget *last_window = NULL, *popup = NULL; 127 GtkWidget *last_window = NULL, *popup = NULL;
133 128
134 #if GTK_MAJOR_VERSION < 2 129 #if GTK_MAJOR_VERSION < 2
135 static int _dw_file_active = 0; 130 static int _dw_file_active = 0;
136 #endif 131 #endif
137 static int _dw_ignore_click = 0, _dw_ignore_expand = 0, _dw_color_active = 0; 132 static int _dw_ignore_click = 0, _dw_ignore_expand = 0, _dw_color_active = 0;
138 static pthread_t _dw_thread = (pthread_t)-1; 133 static pthread_t _dw_thread = (pthread_t)-1;
139 static int _dw_mutex_locked[DW_THREAD_LIMIT];
140 /* Use default border size for the default enlightenment theme */ 134 /* Use default border size for the default enlightenment theme */
141 static int _dw_border_width = 12, _dw_border_height = 28; 135 static int _dw_border_width = 12, _dw_border_height = 28;
142 136
143 #define DW_MUTEX_LOCK { int index = _find_thread_index(dw_thread_id()); if(pthread_self() != _dw_thread && _dw_mutex_locked[index] == FALSE) { gdk_threads_enter(); _dw_mutex_locked[index] = TRUE; _locked_by_me = TRUE; } } 137 #define DW_MUTEX_LOCK { if(pthread_self() != _dw_thread && !pthread_getspecific(_dw_mutex_key)) { gdk_threads_enter(); pthread_setspecific(_dw_mutex_key, (void *)1); _locked_by_me = TRUE; } }
144 #define DW_MUTEX_UNLOCK { if(pthread_self() != _dw_thread && _locked_by_me == TRUE) { gdk_threads_leave(); _dw_mutex_locked[_find_thread_index(dw_thread_id())] = FALSE; _locked_by_me = FALSE; } } 138 #define DW_MUTEX_UNLOCK { if(pthread_self() != _dw_thread && _locked_by_me == TRUE) { gdk_threads_leave(); pthread_setspecific(_dw_mutex_key, NULL); _locked_by_me = FALSE; } }
145 139
146 #define DEFAULT_SIZE_WIDTH 12 140 #define DEFAULT_SIZE_WIDTH 12
147 #define DEFAULT_SIZE_HEIGHT 6 141 #define DEFAULT_SIZE_HEIGHT 6
148 #define DEFAULT_TITLEBAR_HEIGHT 22 142 #define DEFAULT_TITLEBAR_HEIGHT 22
149 143
1951 XSetWMNormalHints (GDK_DISPLAY(),GDK_WINDOW_XWINDOW (GTK_WIDGET (window)->window),&sizehints); 1945 XSetWMNormalHints (GDK_DISPLAY(),GDK_WINDOW_XWINDOW (GTK_WIDGET (window)->window),&sizehints);
1952 gdk_flush (); 1946 gdk_flush ();
1953 } 1947 }
1954 #endif 1948 #endif
1955 1949
1956 /* Find the index of a given thread */ 1950 void _init_thread(void)
1957 static int _find_thread_index(DWTID tid) 1951 {
1958 { 1952 GdkColor *foreground = malloc(sizeof(GdkColor));
1959 int z; 1953
1960 1954 foreground->pixel = foreground->red = foreground->green = foreground->blue = 0;
1961 for(z=0;z<DW_THREAD_LIMIT;z++) 1955 pthread_setspecific(_dw_fg_color_key, foreground);
1962 { 1956 pthread_setspecific(_dw_bg_color_key, NULL);
1963 if(_dw_thread_list[z] == tid)
1964 return z;
1965 }
1966 return 0;
1967 }
1968
1969 /* Add a thread id to the thread list */
1970 static void _dw_thread_add(DWTID tid)
1971 {
1972 int z;
1973
1974 for(z=0;z<DW_THREAD_LIMIT;z++)
1975 {
1976 if(_dw_thread_list[z] == tid)
1977 return;
1978
1979 if(_dw_thread_list[z] == (DWTID)-1)
1980 {
1981 _dw_thread_list[z] = tid;
1982 _foreground[z].pixel = _foreground[z].red =_foreground[z].green = _foreground[z].blue = 0;
1983 _background[z].pixel = 1;
1984 _background[z].red = _background[z].green = _background[z].blue = 0;
1985 _transparent[z] = 1;
1986 _clipboard_contents[z] = NULL;
1987 _clipboard_object[z] = NULL;
1988 return;
1989 }
1990 }
1991 }
1992
1993 /* Remove a thread id to the thread list */
1994 static void _dw_thread_remove(DWTID tid)
1995 {
1996 int z;
1997
1998 for(z=0;z<DW_THREAD_LIMIT;z++)
1999 {
2000 if(_dw_thread_list[z] == (DWTID)tid)
2001 {
2002 _dw_thread_list[z] = (DWTID)-1;
2003 if ( _clipboard_contents[z] != NULL )
2004 {
2005 g_free( _clipboard_contents[z] );
2006 _clipboard_contents[z] = NULL;;
2007 }
2008 _clipboard_object[z] = NULL;;
2009 }
2010 }
2011 } 1957 }
2012 1958
2013 /* Try to load the mozilla embed shared libary */ 1959 /* Try to load the mozilla embed shared libary */
2014 #ifdef USE_GTKMOZEMBED 1960 #ifdef USE_GTKMOZEMBED
2015 #include <ctype.h> 1961 #include <ctype.h>
2140 _dw_border_width = atoi(tmp); 2086 _dw_border_width = atoi(tmp);
2141 tmp = getenv("DW_BORDER_HEIGHT"); 2087 tmp = getenv("DW_BORDER_HEIGHT");
2142 if(tmp) 2088 if(tmp)
2143 _dw_border_height = atoi(tmp); 2089 _dw_border_height = atoi(tmp);
2144 2090
2145 for(z=0;z<DW_THREAD_LIMIT;z++) 2091 pthread_key_create(&_dw_fg_color_key, NULL);
2146 _dw_thread_list[z] = (DWTID)-1; 2092 pthread_key_create(&_dw_bg_color_key, NULL);
2093 pthread_key_create(&_dw_mutex_key, NULL);
2094
2095 _init_thread();
2147 2096
2148 gtk_rc_parse_string("style \"gtk-tooltips-style\" { bg[NORMAL] = \"#eeee00\" } widget \"gtk-tooltips\" style \"gtk-tooltips-style\""); 2097 gtk_rc_parse_string("style \"gtk-tooltips-style\" { bg[NORMAL] = \"#eeee00\" } widget \"gtk-tooltips\" style \"gtk-tooltips-style\"");
2149 2098
2150 #ifdef USE_GTKMOZEMBED 2099 #ifdef USE_GTKMOZEMBED
2151 init_mozembed(); 2100 init_mozembed();
2174 */ 2123 */
2175 void dw_main(void) 2124 void dw_main(void)
2176 { 2125 {
2177 gdk_threads_enter(); 2126 gdk_threads_enter();
2178 _dw_thread = pthread_self(); 2127 _dw_thread = pthread_self();
2179 _dw_thread_add(_dw_thread);
2180 gtk_main(); 2128 gtk_main();
2181 _dw_thread = (pthread_t)-1; 2129 _dw_thread = (pthread_t)-1;
2182 gdk_threads_leave(); 2130 gdk_threads_leave();
2183 } 2131 }
2184 2132
2228 */ 2176 */
2229 void dw_main_iteration(void) 2177 void dw_main_iteration(void)
2230 { 2178 {
2231 gdk_threads_enter(); 2179 gdk_threads_enter();
2232 _dw_thread = pthread_self(); 2180 _dw_thread = pthread_self();
2233 _dw_thread_add(_dw_thread);
2234 gtk_main_iteration(); 2181 gtk_main_iteration();
2235 _dw_thread = (pthread_t)-1; 2182 _dw_thread = (pthread_t)-1;
2236 gdk_threads_leave(); 2183 gdk_threads_leave();
2237 } 2184 }
2238 2185
7437 * green: green value. 7384 * green: green value.
7438 * blue: blue value. 7385 * blue: blue value.
7439 */ 7386 */
7440 void dw_color_foreground_set(unsigned long value) 7387 void dw_color_foreground_set(unsigned long value)
7441 { 7388 {
7442 int _locked_by_me = FALSE, index = _find_thread_index(dw_thread_id()); 7389 int _locked_by_me = FALSE;
7443 GdkColor color = _internal_color(value); 7390 GdkColor color = _internal_color(value);
7391 GdkColor *foreground = pthread_getspecific(_dw_fg_color_key);
7444 7392
7445 DW_MUTEX_LOCK; 7393 DW_MUTEX_LOCK;
7446 gdk_color_alloc(_dw_cmap, &color); 7394 gdk_color_alloc(_dw_cmap, &color);
7447 _foreground[index] = color; 7395 *foreground = color;
7448 DW_MUTEX_UNLOCK; 7396 DW_MUTEX_UNLOCK;
7449 } 7397 }
7450 7398
7451 /* Sets the current background drawing color. 7399 /* Sets the current background drawing color.
7452 * Parameters: 7400 * Parameters:
7454 * green: green value. 7402 * green: green value.
7455 * blue: blue value. 7403 * blue: blue value.
7456 */ 7404 */
7457 void dw_color_background_set(unsigned long value) 7405 void dw_color_background_set(unsigned long value)
7458 { 7406 {
7459 int _locked_by_me = FALSE, index = _find_thread_index(dw_thread_id()); 7407 int _locked_by_me = FALSE;
7460 GdkColor color = _internal_color(value); 7408 GdkColor *background = pthread_getspecific(_dw_bg_color_key);
7461 7409
7462 DW_MUTEX_LOCK;
7463 gdk_color_alloc(_dw_cmap, &color);
7464 if(value == DW_CLR_DEFAULT) 7410 if(value == DW_CLR_DEFAULT)
7465 _transparent[index] = 1; 7411 {
7412 if(background)
7413 {
7414 pthread_setspecific(_dw_bg_color_key, NULL);
7415 free(background);
7416 }
7417 }
7466 else 7418 else
7467 _transparent[index] = 0; 7419 {
7468 7420 GdkColor color = _internal_color(value);
7469 _background[index] = color; 7421
7470 DW_MUTEX_UNLOCK; 7422 DW_MUTEX_LOCK;
7423 gdk_color_alloc(_dw_cmap, &color);
7424 DW_MUTEX_UNLOCK;
7425 if(!background)
7426 {
7427 background = malloc(sizeof(GdkColor));
7428 pthread_setspecific(_dw_bg_color_key, background);
7429 }
7430 *background = color;
7431 }
7471 } 7432 }
7472 7433
7473 /* Internal function to handle the color OK press */ 7434 /* Internal function to handle the color OK press */
7474 static gint _gtk_color_ok(GtkWidget *widget, DWDialog *dwwait) 7435 static gint _gtk_color_ok(GtkWidget *widget, DWDialog *dwwait)
7475 { 7436 {
7578 } 7539 }
7579 7540
7580 GdkGC *_set_colors(GdkWindow *window) 7541 GdkGC *_set_colors(GdkWindow *window)
7581 { 7542 {
7582 GdkGC *gc = NULL; 7543 GdkGC *gc = NULL;
7583 int index = _find_thread_index(dw_thread_id()); 7544 GdkColor *foreground = pthread_getspecific(_dw_fg_color_key);
7545 GdkColor *background = pthread_getspecific(_dw_bg_color_key);
7584 7546
7585 if(!window) 7547 if(!window)
7586 return NULL; 7548 return NULL;
7587 gc = gdk_gc_new(window); 7549 gc = gdk_gc_new(window);
7588 if(gc) 7550 if(gc)
7589 { 7551 {
7590 gdk_gc_set_foreground(gc, &_foreground[index]); 7552 gdk_gc_set_foreground(gc, foreground);
7591 gdk_gc_set_background(gc, &_background[index]); 7553 if(background)
7554 gdk_gc_set_background(gc, background);
7592 } 7555 }
7593 return gc; 7556 return gc;
7594 } 7557 }
7595 7558
7596 /* Draw a point on a window (preferably a render window). 7559 /* Draw a point on a window (preferably a render window).
7760 PangoLayout *layout = pango_layout_new(context); 7723 PangoLayout *layout = pango_layout_new(context);
7761 7724
7762 if(layout) 7725 if(layout)
7763 { 7726 {
7764 int index = _find_thread_index(dw_thread_id()); 7727 int index = _find_thread_index(dw_thread_id());
7728 GdkColor *foreground = pthread_getspecific(_dw_fg_color_key);
7729 GdkColor *background = pthread_getspecific(_dw_bg_color_key);
7765 7730
7766 gdk_pango_context_set_colormap(context, _dw_cmap); 7731 gdk_pango_context_set_colormap(context, _dw_cmap);
7767 pango_layout_set_font_description(layout, font); 7732 pango_layout_set_font_description(layout, font);
7768 pango_layout_set_text(layout, text, strlen(text)); 7733 pango_layout_set_text(layout, text, strlen(text));
7769 7734
7770 if(_transparent[index]) 7735 if(background)
7736 gdk_draw_layout_with_colors(handle ? handle->window : pixmap->pixmap, gc, x, y, layout, foreground, background);
7737 else
7771 gdk_draw_layout(handle ? handle->window : pixmap->pixmap, gc, x, y, layout); 7738 gdk_draw_layout(handle ? handle->window : pixmap->pixmap, gc, x, y, layout);
7772 else
7773 gdk_draw_layout_with_colors(handle ? handle->window : pixmap->pixmap, gc, x, y, layout, &_foreground[index], &_background[index]);
7774 7739
7775 g_object_unref(layout); 7740 g_object_unref(layout);
7776 } 7741 }
7777 g_object_unref(context); 7742 g_object_unref(context);
7778 } 7743 }
7783 if(!font) 7748 if(!font)
7784 font = gdk_font_load("fixed"); 7749 font = gdk_font_load("fixed");
7785 if(font) 7750 if(font)
7786 { 7751 {
7787 gint ascent, descent, width, junk_ascent, junk_descent, junk_width; 7752 gint ascent, descent, width, junk_ascent, junk_descent, junk_width;
7788 int index = _find_thread_index(dw_thread_id()); 7753 GdkColor *foreground = pthread_getspecific(_dw_fg_color_key);
7754 GdkColor *background = pthread_getspecific(_dw_bg_color_key);
7789 7755
7790 /* gdk_text_extents() calculates ascent and descent based on the string, so 7756 /* gdk_text_extents() calculates ascent and descent based on the string, so
7791 * a string without a character with a descent or without an ascent will have 7757 * a string without a character with a descent or without an ascent will have
7792 * incorrect ascent/descent values 7758 * incorrect ascent/descent values
7793 */ 7759 */
7794 gdk_text_extents(font, text, strlen(text), NULL, NULL, &width, &junk_ascent, &junk_descent); 7760 gdk_text_extents(font, text, strlen(text), NULL, NULL, &width, &junk_ascent, &junk_descent);
7795 /* force ascent/descent to be maximum values */ 7761 /* force ascent/descent to be maximum values */
7796 gdk_text_extents(font, "(g", 2, NULL, NULL, &junk_width, &ascent, &descent); 7762 gdk_text_extents(font, "(g", 2, NULL, NULL, &junk_width, &ascent, &descent);
7797 if(!_transparent[index]) 7763 if(background)
7798 { 7764 {
7799 GdkGC *gc2 = NULL; 7765 GdkGC *gc2 = NULL;
7800 7766
7801 gc2 = gdk_gc_new(handle ? handle->window : pixmap->pixmap); 7767 gc2 = gdk_gc_new(handle ? handle->window : pixmap->pixmap);
7802 if(gc2) 7768 if(gc2)
7803 { 7769 {
7804 gdk_gc_set_foreground(gc2, &_background[index]); 7770 gdk_gc_set_foreground(gc2, background);
7805 gdk_gc_set_background(gc2, &_background[index]); 7771 gdk_gc_set_background(gc2, background);
7806 } 7772 }
7807 gdk_draw_rectangle(handle ? handle->window : pixmap->pixmap, gc2, TRUE, x, y, width, ascent + descent + 1); 7773 gdk_draw_rectangle(handle ? handle->window : pixmap->pixmap, gc2, TRUE, x, y, width, ascent + descent + 1);
7808 gdk_gc_unref(gc2); 7774 gdk_gc_unref(gc2);
7809 } 7775 }
7810 gdk_draw_text(handle ? handle->window : pixmap->pixmap, font, gc, x, y + ascent + 1, text, strlen(text)); 7776 gdk_draw_text(handle ? handle->window : pixmap->pixmap, font, gc, x, y + ascent + 1, text, strlen(text));
11509 * Pointer to an allocated string of text or NULL if clipboard empty or contents could not 11475 * Pointer to an allocated string of text or NULL if clipboard empty or contents could not
11510 * be converted to text. 11476 * be converted to text.
11511 */ 11477 */
11512 char *dw_clipboard_get_text() 11478 char *dw_clipboard_get_text()
11513 { 11479 {
11514 int _locked_by_me = FALSE, index = _find_thread_index(dw_thread_id()); 11480 int _locked_by_me = FALSE;
11515 11481 GtkClipboard *clipboard_object;
11516 DW_MUTEX_LOCK; 11482 char *ret = NULL;
11517 if ( _clipboard_object[index] == NULL ) 11483
11518 { 11484 DW_MUTEX_LOCK;
11519 _clipboard_object[index] = gtk_clipboard_get( GDK_SELECTION_CLIPBOARD ); 11485 if((clipboard_object = gtk_clipboard_get( GDK_SELECTION_CLIPBOARD )))
11520 } 11486 {
11521 if ( _clipboard_contents[index] != NULL ) 11487 gchar *clipboard_contents;
11522 { 11488
11523 g_free( _clipboard_contents[index] ); 11489 if((clipboard_contents = gtk_clipboard_wait_for_text( clipboard_object )))
11524 } 11490 {
11525 _clipboard_contents[index] = gtk_clipboard_wait_for_text( _clipboard_object[index] ); 11491 ret = strdup((char *)clipboard_contents);
11526 DW_MUTEX_UNLOCK; 11492 g_free(clipboard_contents);
11527 return (char *)_clipboard_contents[index]; 11493 }
11494 }
11495 DW_MUTEX_UNLOCK;
11496 return ret;
11528 } 11497 }
11529 11498
11530 /* 11499 /*
11531 * Sets the contents of the default clipboard to the supplied text. 11500 * Sets the contents of the default clipboard to the supplied text.
11532 * Parameters: 11501 * Parameters:
11533 * Text. 11502 * Text.
11534 */ 11503 */
11535 void dw_clipboard_set_text( char *str, int len ) 11504 void dw_clipboard_set_text( char *str, int len )
11536 { 11505 {
11537 int _locked_by_me = FALSE, index = _find_thread_index(dw_thread_id()); 11506 int _locked_by_me = FALSE;
11538 11507 GtkClipboard *clipboard_object;
11539 DW_MUTEX_LOCK; 11508
11540 if ( _clipboard_object[index] == NULL ) 11509 DW_MUTEX_LOCK;
11541 { 11510 if((clipboard_object = gtk_clipboard_get( GDK_SELECTION_CLIPBOARD )))
11542 _clipboard_object[index] = gtk_clipboard_get( GDK_SELECTION_CLIPBOARD ); 11511 {
11543 } 11512 gtk_clipboard_set_text( clipboard_object, str, len );
11544 gtk_clipboard_set_text( _clipboard_object[index], str, len ); 11513 }
11545 DW_MUTEX_UNLOCK; 11514 DW_MUTEX_UNLOCK;
11546 } 11515 }
11547 11516
11548 /* 11517 /*
11549 * Returns a pointer to a static buffer which containes the 11518 * Returns a pointer to a static buffer which containes the