3 Copyright (C) 2000 Kh. Naba Kumar Singh
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
35 #include <libanjuta/resources.h>
38 #include "utilities.h"
40 #include "properties.h"
43 extract_filename (const gchar
* full_filename
)
51 length
= strlen (full_filename
);
54 if (full_filename
[length
- 1] == '/')
55 return &full_filename
[length
];
56 for (i
= length
- 1; i
>= 0; i
--)
57 if (full_filename
[i
] == '/')
59 if (i
== 0 && full_filename
[0] != '/')
61 return &full_filename
[++i
];
65 extract_directory(const gchar
* full_filename
)
73 length
= strlen (full_filename
);
75 return g_strdup(full_filename
);
76 if (full_filename
[length
- 1] == '/')
78 dir
= g_new0(char, length
- 1);
79 strncpy(dir
, full_filename
, length
- 2);
82 for (i
= length
- 1; i
>= 0; i
--)
83 if (full_filename
[i
] == '/')
87 dir
= g_new0(char, i
+ 1);
88 strncpy(dir
, full_filename
, i
);
94 free_string_list ( GList
* pList
)
100 for (i
= 0; i
< g_list_length (pList
); i
++)
101 g_free (g_list_nth (pList
, i
)->data
);
110 parse_error_line (const gchar
* line
, gchar
** filename
, int *lineno
)
117 while (line
[i
++] != ':')
119 if (i
>= strlen (line
) || i
>= 512 || line
[i
- 1] == ' ')
124 if (isdigit (line
[i
]))
127 while (isdigit (line
[i
++])) ;
128 dummy
= g_strndup (&line
[j
], i
- j
- 1);
129 *lineno
= atoi (dummy
);
132 dummy
= g_strndup (line
, j
- 1);
133 *filename
= g_strdup (g_strstrip (dummy
));
140 i
= strlen (line
) - 1;
141 while (isspace (line
[i
]) == FALSE
)
152 while (line
[i
++] != ':')
154 if (i
>= strlen (line
) || i
>= 512 || line
[i
- 1] == ' ')
161 if (isdigit (line
[i
]))
164 while (isdigit (line
[i
++])) ;
165 dummy
= g_strndup (&line
[j
], i
- j
- 1);
166 *lineno
= atoi (dummy
);
169 dummy
= g_strndup (&line
[k
], j
- k
- 1);
170 *filename
= g_strdup (g_strstrip (dummy
));
181 get_file_extension (gchar
* file
)
186 pos
= strrchr (file
, '.');
193 FileExtType
get_file_ext_type (gchar
* file
)
195 gchar
*pos
, *filetype_str
;
196 FileExtType filetype
;
199 return FILE_TYPE_UNKNOWN
;
200 pos
= get_file_extension (file
);
202 return FILE_TYPE_UNKNOWN
;
205 prop_get_new_expand (ANJUTA_PREFERENCES (app
->preferences
)->props
,
207 if (filetype_str
== NULL
)
208 return FILE_TYPE_UNKNOWN
;
210 if (strcmp (filetype_str
, "c") == 0)
212 filetype
= FILE_TYPE_C
;
214 else if (strcmp (filetype_str
, "cpp") == 0)
216 filetype
= FILE_TYPE_CPP
;
218 else if (strcmp (filetype_str
, "header") == 0)
220 filetype
= FILE_TYPE_HEADER
;
222 else if (strcmp (filetype_str
, "pascal") == 0)
224 filetype
= FILE_TYPE_PASCAL
;
226 else if (strcmp (filetype_str
, "rc") == 0)
228 filetype
= FILE_TYPE_RC
;
230 else if (strcmp (filetype_str
, "idl") == 0)
232 filetype
= FILE_TYPE_IDL
;
234 else if (strcmp (filetype_str
, "cs") == 0)
236 filetype
= FILE_TYPE_CS
;
238 else if (strcmp (filetype_str
, "java") == 0)
240 filetype
= FILE_TYPE_JAVA
;
242 else if (strcmp (filetype_str
, "js") == 0)
244 filetype
= FILE_TYPE_JS
;
246 else if (strcmp (filetype_str
, "conf") == 0)
248 filetype
= FILE_TYPE_CONF
;
250 else if (strcmp (filetype_str
, "hypertext") == 0)
252 filetype
= FILE_TYPE_HTML
;
254 else if (strcmp (filetype_str
, "xml") == 0)
256 filetype
= FILE_TYPE_XML
;
258 else if (strcmp (filetype_str
, "latex") == 0)
260 filetype
= FILE_TYPE_LATEX
;
262 else if (strcmp (filetype_str
, "lua") == 0)
264 filetype
= FILE_TYPE_LUA
;
266 else if (strcmp (filetype_str
, "perl") == 0)
268 filetype
= FILE_TYPE_PERL
;
270 else if (strcmp (filetype_str
, "shellscript") == 0)
272 filetype
= FILE_TYPE_SH
;
274 else if (strcmp (filetype_str
, "python") == 0)
276 filetype
= FILE_TYPE_PYTHON
;
278 else if (strcmp (filetype_str
, "ruby") == 0)
280 filetype
= FILE_TYPE_RUBY
;
282 else if (strcmp (filetype_str
, "props") == 0)
284 filetype
= FILE_TYPE_PROPERTIES
;
286 else if (strcmp (filetype_str
, "project") == 0)
288 filetype
= FILE_TYPE_PROJECT
;
290 else if (strcmp (filetype_str
, "batch") == 0)
292 filetype
= FILE_TYPE_BATCH
;
294 else if (strcmp (filetype_str
, "errorlist") == 0)
296 filetype
= FILE_TYPE_ERRORLIST
;
298 else if (strcmp (filetype_str
, "makefile") == 0)
300 filetype
= FILE_TYPE_MAKEFILE
;
302 else if (strcmp (filetype_str
, "iface") == 0)
304 filetype
= FILE_TYPE_IFACE
;
306 else if (strcmp (filetype_str
, "diff") == 0)
308 filetype
= FILE_TYPE_DIFF
;
310 else if (strcmp (filetype_str
, "icon") == 0)
312 filetype
= FILE_TYPE_ICON
;
314 else if (strcmp (filetype_str
, "image") == 0)
316 filetype
= FILE_TYPE_IMAGE
;
318 else if (strcmp (filetype_str
, "asm") == 0)
320 filetype
= FILE_TYPE_ASM
;
322 else if (strcmp (filetype_str
, "scm") == 0)
324 filetype
= FILE_TYPE_SCM
;
326 else if (strcmp (filetype_str
, "po") == 0)
328 filetype
= FILE_TYPE_PO
;
330 else if (strcmp (filetype_str
, "sql") == 0)
332 filetype
= FILE_TYPE_SQL
;
334 else if (strcmp (filetype_str
, "plsql") == 0)
336 filetype
= FILE_TYPE_PLSQL
;
338 else if (strcmp (filetype_str
, "vb") == 0)
340 filetype
= FILE_TYPE_VB
;
342 else if (strcmp (filetype_str
, "baan") == 0)
344 filetype
= FILE_TYPE_BAAN
;
346 else if (strcmp (filetype_str
, "ada") == 0)
348 filetype
= FILE_TYPE_ADA
;
350 else if (strcmp (filetype_str
, "wscript") == 0)
352 filetype
= FILE_TYPE_WSCRIPT
;
354 else if (strcmp (filetype_str
, "lisp") == 0)
356 filetype
= FILE_TYPE_LISP
;
358 else if (strcmp (filetype_str
, "matlab") == 0)
360 filetype
= FILE_TYPE_MATLAB
;
364 filetype
= FILE_TYPE_UNKNOWN
;
366 g_free (filetype_str
);
370 gboolean
write_line (FILE * stream
, gchar
* str
)
378 if (fwrite (str
, len
, sizeof (gchar
), stream
) < 0)
380 if (fwrite ("\n", 1, sizeof (gchar
), stream
) < 1)
385 gboolean
read_line (FILE * stream
, gchar
** str
)
402 buffer
[count
] = (char) ch
;
404 if (count
>= 1024 - 1)
407 buffer
[count
] = '\0';
408 *str
= g_strdup (buffer
);
412 gboolean
write_string (FILE * stream
, gchar
* t
, gchar
* str
)
418 if (fprintf (stream
, "%s :%lu: ", t
, len
) < 2)
421 if (fwrite (str
, len
, sizeof (gchar
), stream
) < 0)
423 if (fwrite ("\n", 1, sizeof (gchar
), stream
) < 1)
428 gboolean
read_string (FILE * stream
, gchar
* t
, gchar
** str
)
431 gchar
*buff
, bb
[3], token
[256];
437 if (fscanf (stream
, "%s :%lu: ", token
, &tmp
) < 2)
439 if (strcmp (token
, t
) != 0)
444 (*str
) = g_strdup ("");
447 buff
= g_malloc ((tmp
+ 1) * sizeof (char));
448 if (fread (buff
, tmp
, sizeof (gchar
), stream
) < 0)
453 if (fread (bb
, 1, sizeof (gchar
), stream
) < 1)
466 gint
compare_string_func (gconstpointer a
, gconstpointer b
)
472 return (strcmp ((char *) a
, (char *) b
));
476 file_is_regular (const gchar
* fn
)
482 ret
= stat (fn
, &st
);
485 if (S_ISREG (st
.st_mode
))
491 file_is_directory (const gchar
* fn
)
497 ret
= stat (fn
, &st
);
500 if (S_ISDIR (st
.st_mode
))
506 file_is_link (const gchar
* fn
)
512 ret
= lstat (fn
, &st
);
515 if (S_ISLNK (st
.st_mode
))
521 file_is_char_device (const gchar
* fn
)
527 ret
= stat (fn
, &st
);
530 if (S_ISCHR (st
.st_mode
))
536 file_is_block_device (const gchar
* fn
)
542 ret
= stat (fn
, &st
);
545 if (S_ISBLK (st
.st_mode
))
551 file_is_fifo (const gchar
* fn
)
557 ret
= stat (fn
, &st
);
560 if (S_ISFIFO (st
.st_mode
))
566 file_is_socket (const gchar
* fn
)
572 ret
= stat (fn
, &st
);
575 if (S_ISSOCK (st
.st_mode
))
581 file_is_readable (const gchar
* fn
)
587 ret
= stat (fn
, &st
);
590 if (S_IRUSR
& st
.st_mode
)
596 file_is_readonly (const gchar
* fn
)
598 return file_is_readable (fn
) && !file_is_writable (fn
);
602 file_is_readwrite (const gchar
* fn
)
604 return file_is_readable (fn
) && file_is_writable (fn
);
609 file_is_writable (const gchar
* fn
)
615 ret
= stat (fn
, &st
);
618 if (S_IWUSR
& st
.st_mode
)
624 file_is_executable (const gchar
* fn
)
630 ret
= stat (fn
, &st
);
633 if (S_IXUSR
& st
.st_mode
)
639 file_is_suid (const gchar
* fn
)
645 ret
= stat (fn
, &st
);
648 if (S_ISUID
& st
.st_mode
)
654 file_is_sgid (const gchar
* fn
)
660 ret
= stat (fn
, &st
);
663 if (S_ISGID
& st
.st_mode
)
669 file_is_sticky (const gchar
* fn
)
675 ret
= stat (fn
, &st
);
678 if (S_ISVTX
& st
.st_mode
)
684 copy_file (gchar
* src
, gchar
* dest
, gboolean show_error
)
686 FILE *input_fp
, *output_fp
;
687 gchar buffer
[FILE_BUFFER_SIZE
];
688 gint bytes_read
, bytes_written
;
693 input_fp
= fopen (src
, "rb");
694 if (input_fp
== NULL
)
697 anjuta_system_error (errno
, _("Unable to read file: %s."), src
);
701 output_fp
= fopen (dest
, "wb");
702 if (output_fp
== NULL
)
705 anjuta_system_error (errno
, _("Unable to create file: %s."), dest
);
712 bytes_read
= fread (buffer
, 1, FILE_BUFFER_SIZE
, input_fp
);
713 if (bytes_read
!= FILE_BUFFER_SIZE
&& ferror (input_fp
))
721 bytes_written
= fwrite (buffer
, 1, bytes_read
, output_fp
);
722 if (bytes_read
!= bytes_written
)
729 if (bytes_read
!= FILE_BUFFER_SIZE
&& feof (input_fp
))
738 if( show_error
&& (error
== FALSE
))
739 anjuta_system_error (errno
, _("Unable to complete file copy"));
746 /* Do not update gtk when launcher is busy */
747 /* This will freeze the application till the launcher is done */
748 if (anjuta_launcher_is_busy (app
->launcher
) == TRUE
)
750 if (app
->auto_gtk_update
== FALSE
)
752 while (gtk_events_pending ())
754 gtk_main_iteration ();
759 entry_set_text_n_select (GtkWidget
* entry
, gchar
* text
,
760 gboolean use_selection
)
764 if (GTK_IS_ENTRY (entry
) == FALSE
)
769 gchar
*chars
= anjuta_get_current_selection ();
772 gtk_entry_set_text (GTK_ENTRY (entry
), chars
);
773 gtk_editable_select_region (GTK_EDITABLE (entry
), 0,
774 strlen (gtk_entry_get_text
783 gtk_entry_set_text (GTK_ENTRY (entry
), text
);
784 gtk_editable_select_region (GTK_EDITABLE (entry
), 0,
785 strlen (gtk_entry_get_text
793 gtk_entry_set_text (GTK_ENTRY (entry
), text
);
794 gtk_editable_select_region (GTK_EDITABLE (entry
), 0,
795 strlen (gtk_entry_get_text
796 (GTK_ENTRY (entry
))));
801 force_create_dir (gchar
* d
)
803 if (file_is_directory (d
))
810 PangoFontDescription
*
813 static PangoFontDescription
*font_desc
;
818 font_desc
= pango_font_description_from_string ("misc medium 12");
822 // pango_font_ref (font);
825 g_warning ("Cannot load fixed(misc) font. Using default font.");
830 remove_white_spaces (gchar
* text
)
832 guint src_count
, dest_count
, tab_count
;
833 gchar buff
[2048]; /* Let us hope that it does not overflow */
836 anjuta_preferences_get_int (ANJUTA_PREFERENCES (app
->preferences
),
839 for (src_count
= 0; src_count
< strlen (text
); src_count
++)
841 if (text
[src_count
] == '\t')
844 for (j
= 0; j
< tab_count
; j
++)
845 buff
[dest_count
++] = ' ';
847 else if (isspace (text
[src_count
]))
849 buff
[dest_count
++] = ' ';
853 buff
[dest_count
++] = text
[src_count
];
856 buff
[dest_count
] = '\0';
857 return g_strdup (buff
);
861 remove_blank_lines (GList
* lines
)
867 list
= g_list_copy (lines
);
875 node
= g_list_next (node
);
878 list
= g_list_remove (list
, str
);
881 if (strlen (g_strchomp (str
)) < 1)
882 list
= g_list_remove (list
, str
);
887 gboolean
widget_is_child (GtkWidget
* parent
, GtkWidget
* child
)
889 if (GTK_IS_CONTAINER (parent
))
893 children
= gtk_container_get_children (GTK_CONTAINER (parent
));
897 if (widget_is_child (node
->data
, child
))
909 /* Assign a value to a string */
911 string_assign (gchar
** string
, const gchar
*value
)
917 *string
= g_strdup (value
);
921 move_file_if_not_same (gchar
* src
, gchar
* dest
)
925 g_return_val_if_fail (src
!= NULL
, FALSE
);
926 g_return_val_if_fail (dest
!= NULL
, FALSE
);
928 if (anjuta_is_installed ("cmp", FALSE
) == TRUE
)
934 execlp ("cmp", "cmp", "-s", src
, dest
, NULL
);
935 g_error ("Cannot execute cmp");
937 waitpid (pid
, &status
, 0);
938 if (WEXITSTATUS(status
)==0)
947 /* rename () can't work with 2 different filesystems, so we need
948 to use the couple {copy, remove}. Meanwhile, the files to copy
949 are supposed to be small, so it would spend more time to try
950 rename () to know if a copy/deletion is needed */
952 if (!same
&& !copy_file (src
, dest
, FALSE
))
959 gboolean
is_file_same(gchar
*a
, gchar
*b
)
961 struct stat st_a
,st_b
;
963 if(stat(a
, &st_a
) == -1)
965 /* printf("WARNING: Unable to stat '%s'.", a);*/
968 if(stat(b
, &st_b
) == -1)
970 /* printf("WARNING: Unable to stat '%s'.", b);*/
974 if(st_a
.st_ino
== st_b
.st_ino
)
981 get_file_as_buffer (gchar
* filename
)
988 g_return_val_if_fail ( filename
!= NULL
, NULL
);
989 ret
= stat (filename
, &s
);
992 fp
= fopen(filename
, "r");
995 buff
= g_malloc (s
.st_size
+3);
996 ret
= fread (buff
, 1, s
.st_size
, fp
);
999 /* Error is not checked whether all the file is read or not. */
1000 /* Can be checked with ferror() on return from this function */
1005 scan_files_in_dir (const char *dir
, int (*select
)(const struct dirent
*))
1007 struct dirent
**namelist
;
1008 GList
*files
= NULL
;
1011 g_return_val_if_fail (dir
!= NULL
, NULL
);
1012 if (0 > (n
= scandir (dir
, &namelist
, select
, alphasort
)))
1018 files
= g_list_append (files
, g_strdup (namelist
[n
]->d_name
));
1027 string_from_glist (GList
* list
)
1031 if (!list
) return NULL
;
1041 str
= g_strconcat (tmp
, node
->data
, " ", NULL
);
1044 node
= g_list_next (node
);
1046 if (strlen(str
) == 0)
1054 select_only_file (const struct dirent
*e
)
1056 return file_is_regular (e
->d_name
);
1059 /* Create a new hbox with an image and a label packed into it
1060 * and return the box. */
1062 create_xpm_label_box(GtkWidget
*parent
,
1063 const gchar
*xpm_filename
, gboolean gnome_pixmap
,
1064 const gchar
*label_text
)
1070 /* Create box for xpm and label */
1071 box1
= gtk_hbox_new (FALSE
, 0);
1073 /* Now on to the xpm stuff */
1074 pixmap
= anjuta_res_get_image (xpm_filename
);
1076 /* Create a label for the button */
1077 label
= gtk_label_new (label_text
);
1079 /* Pack the pixmap and label into the box */
1080 gtk_box_pack_start (GTK_BOX (box1
),
1081 pixmap
, FALSE
, FALSE
, 0);
1083 gtk_box_pack_start (GTK_BOX (box1
), label
, FALSE
, FALSE
, 3);
1085 gtk_widget_show(pixmap
);
1086 gtk_widget_show(label
);
1091 /* Excluding the final 0 */
1092 gint
calc_string_len( const gchar
*szStr
)
1096 return strlen( szStr
)*3 ; /* Leave space for the translated character */
1099 gint
calc_gnum_len(void)
1101 return 24 ; /* size of a stringfied integer */
1103 /* Allocates a struct of pointers */
1104 gchar
**string_parse_separator( const gint nItems
, gchar
*szStrIn
, const gchar chSep
)
1106 gchar
**szAllocPtrs
= (char**)g_new( gchar
*, nItems
);
1107 if( NULL
!= szAllocPtrs
)
1110 gboolean bOK
= TRUE
;
1111 gchar
*p
= szStrIn
;
1112 for( i
= 0 ; i
< nItems
; i
++ )
1115 szp
= strchr( p
, chSep
) ;
1118 szAllocPtrs
[i
] = p
;
1119 szp
[0] = '\0' ; /* Parse Operation */
1129 g_free( szAllocPtrs
);
1130 szAllocPtrs
= NULL
;
1133 return szAllocPtrs
;
1137 /* Write in file....*/
1138 gchar
*WriteBufUL( gchar
* szDst
, const gulong ulVal
)
1140 return szDst
+= sprintf( szDst
, "%lu,", ulVal
);
1143 gchar
*WriteBufI( gchar
* szDst
, const gint iVal
)
1145 return szDst
+= sprintf( szDst
, "%d,", iVal
);
1148 gchar
*WriteBufB( gchar
* szDst
, const gboolean bVal
)
1150 return szDst
+= sprintf( szDst
, "%d,", (int)bVal
);
1154 gchar
*WriteBufS( gchar
* szDst
, const gchar
* szVal
)
1158 return szDst
+= sprintf( szDst
, "," );
1161 /* Scrive il valore convertendo eventuali caratteri non alfanumerici */
1162 /*int nLen = strlen( szVal );*/
1163 const gchar
*szSrc
= szVal
;
1166 if( isalnum( szSrc
[0] ) || ('.'==szSrc
[0]) || ('/'==szSrc
[0]) || ('_'==szSrc
[0]) )
1169 } else if( '\\' == szSrc
[0] )
1175 szDst
+= sprintf( szDst
, "\\%2.2X", (0x00FF&szSrc
[0]) );
1184 #define SRCH_CHAR '\\'
1186 static int GetHexAs( const gchar c
)
1191 return toupper(c
) - 'A' + 10 ;
1194 static gchar
GetHexb( const gchar c1
, const gchar c2
)
1195 { return GetHexAs( c1
) * 16 + GetHexAs( c2
) ;
1198 gchar
* GetStrCod( const gchar
*szIn
)
1201 g_return_val_if_fail( NULL
!= szIn
, NULL
);
1202 szRet
= g_malloc( strlen( szIn
)+2 );
1205 gchar
* szDst
= szRet
;
1208 if( SRCH_CHAR
== szIn
[0] )
1210 if( SRCH_CHAR
== szIn
[1] )
1212 *szDst
++ = *szIn
++ ;
1216 *szDst
++ = GetHexb( szIn
[1], szIn
[2] ) ;
1221 *szDst
++ = *szIn
++ ;
1229 gchar
*get_relative_file_name(gchar
*dir
, gchar
*file
)
1231 gchar
*real_dir
= tm_get_real_path(dir
);
1232 gchar
*real_file
= tm_get_real_path(file
);
1233 gchar
*retval
= NULL
;
1235 if (real_dir
&& real_file
)
1237 guint dir_len
= strlen(real_dir
);
1238 if (0 == strncmp(real_file
, real_dir
, dir_len
))
1239 retval
= g_strdup(real_file
+ dir_len
+ 1);
1246 gboolean
is_file_in_dir(const gchar
*file
, const gchar
*dir
)
1248 gboolean status
= FALSE
;
1250 gchar
*real_file_name
= tm_get_real_path(file
);
1251 gchar
*real_dir_name
= tm_get_real_path(dir
);
1252 if (real_file_name
&& real_dir_name
&&
1253 (0 == strncmp(real_file_name
, real_dir_name
, strlen(real_dir_name
))))
1255 g_free(real_file_name
);
1256 g_free(real_dir_name
);
1261 anjuta_util_kill (pid_t process_id
, const gchar
* signal
)
1267 cmd
= g_strdup_printf ("kill -s %s %d", signal
, process_id
);
1268 pid
= gnome_execute_shell (getenv("HOME"), cmd
);
1271 waitpid (pid
, &status
, 0);
1279 anjuta_util_parse_args_from_string (const gchar
* string
)
1301 /* The current char was escaped */
1304 } else if (*s
== '\\') {
1305 /* Current char is an escape */
1307 } else if (*s
== quote
) {
1308 /* Current char ends a quotation */
1310 if (!isspace(*(s
+1)) && (*(s
+1) != '\0')) {
1311 /* If there is no space after the quotation or it is not
1312 the end of the string */
1313 g_warning ("Parse error while parsing program arguments");
1315 } else if ((*s
== '\"' || *s
== '\'')) {
1316 if (quote
== (gchar
)-1) {
1317 /* Current char starts a quotation */
1320 /* Just a quote char inside quote */
1323 } else if (quote
> 0){
1324 /* Any other char inside quote */
1326 } else if (isspace(*s
)) {
1327 /* Any white space outside quote */
1329 buffer
[idx
++] = '\0';
1330 args
= g_list_append (args
, g_strdup (buffer
));
1339 /* There are chars in the buffer. Flush as the last arg */
1340 buffer
[idx
++] = '\0';
1341 args
= g_list_append (args
, g_strdup (buffer
));
1345 g_warning ("Unclosed quotation encountered at the end of parsing");
1350 /* Check which gnome-terminal is installed
1351 Returns: 0 -- No gnome-terminal
1352 Returns: 1 -- Gnome1 gnome-terminal
1353 Returns: 2 -- Gnome2 gnome-terminal */
1356 anjuta_util_check_gnome_terminal (void)
1359 gchar
* term_command
= "gnome-terminal --version";
1360 gchar
* term_command2
= "gnome-terminal --disable-factory --version";
1362 gchar
* term_command
= "gnome-terminal --version > /dev/null 2> /dev/null";
1363 gchar
* term_command2
= "gnome-terminal --disable-factory --version > /dev/null 2> /dev/null";
1367 retval
= system (term_command
);
1369 /* Command failed or gnome-terminal not found */
1370 if (WEXITSTATUS(retval
) != 0)
1373 /* gnome-terminal found: Determine version 1 or 2 */
1374 retval
= system (term_command2
);
1376 /* Command failed or gnome-terminal-2 not found */
1377 if (WEXITSTATUS(retval
) != 0)
1380 /* gnome-terminal-2 found */
1385 int_from_hex_digit (const gchar ch
)
1389 else if (ch
>= 'A' && ch
<= 'F')
1390 return ch
- 'A' + 10;
1391 else if (ch
>= 'a' && ch
<= 'f')
1392 return ch
- 'a' + 10;
1398 anjuta_util_color_from_string (const gchar
* val
, guint8
* r
, guint8
* g
, guint8
* b
)
1400 *r
= int_from_hex_digit (val
[1]) * 16 + int_from_hex_digit (val
[2]);
1401 *g
= int_from_hex_digit (val
[3]) * 16 + int_from_hex_digit (val
[4]);
1402 *b
= int_from_hex_digit (val
[5]) * 16 + int_from_hex_digit (val
[6]);
1406 anjuta_util_string_from_color (guint8 r
, guint8 g
, guint8 b
)
1417 sprintf (str
, "#%06X", num
);
1418 return g_strdup (str
);
1422 anjuta_util_convert_to_utf8 (const gchar
*str
)
1424 GError
*error
= NULL
;
1425 gchar
*utf8_msg_string
= NULL
;
1427 g_return_val_if_fail (str
!= NULL
, NULL
);
1428 g_return_val_if_fail (strlen (str
) > 0, NULL
);
1430 if (g_utf8_validate(str
, -1, NULL
))
1432 utf8_msg_string
= g_strdup (str
);
1436 gsize rbytes
, wbytes
;
1437 utf8_msg_string
= g_locale_to_utf8 (str
, -1, &rbytes
, &wbytes
, &error
);
1438 if (error
!= NULL
) {
1439 g_warning ("g_locale_to_utf8 failed: %s\n", error
->message
);
1440 g_error_free (error
);
1441 g_free (utf8_msg_string
);
1445 return utf8_msg_string
;
1449 anjuta_util_toolbar_append_button (GtkWidget
*toolbar
, const gchar
*iconfile
,
1450 const gchar
*label
, const gchar
*tooltip
,
1451 GtkSignalFunc callback
, gpointer user_data
)
1453 GtkWidget
*icon
= anjuta_res_get_image (iconfile
);
1455 gtk_toolbar_append_element (GTK_TOOLBAR (toolbar
),
1456 GTK_TOOLBAR_CHILD_BUTTON
, NULL
,
1457 label
, tooltip
, NULL
,
1458 icon
, callback
, user_data
);
1459 gtk_widget_ref (item
);
1460 gtk_widget_show (item
);
1465 anjuta_util_toolbar_append_stock (GtkWidget
*toolbar
, const gchar
*stock_icon
,
1466 const gchar
*tooltip
, GtkSignalFunc callback
,
1471 gtk_toolbar_insert_stock (GTK_TOOLBAR (toolbar
),
1474 callback
, user_data
, -1);
1475 gtk_widget_ref (item
);
1476 gtk_widget_show (item
);
1481 anjuta_button_new_with_stock_image (const gchar
* text
, const gchar
* stock_id
)
1490 button
= gtk_button_new ();
1492 if (GTK_BIN (button
)->child
)
1493 gtk_container_remove (GTK_CONTAINER (button
),
1494 GTK_BIN (button
)->child
);
1496 if (gtk_stock_lookup (stock_id
, &item
))
1498 label
= gtk_label_new_with_mnemonic (text
);
1500 gtk_label_set_mnemonic_widget (GTK_LABEL (label
), GTK_WIDGET (button
));
1502 image
= gtk_image_new_from_stock (stock_id
, GTK_ICON_SIZE_BUTTON
);
1503 hbox
= gtk_hbox_new (FALSE
, 2);
1505 align
= gtk_alignment_new (0.5, 0.5, 0.0, 0.0);
1507 gtk_box_pack_start (GTK_BOX (hbox
), image
, FALSE
, FALSE
, 0);
1508 gtk_box_pack_end (GTK_BOX (hbox
), label
, FALSE
, FALSE
, 0);
1510 gtk_container_add (GTK_CONTAINER (button
), align
);
1511 gtk_container_add (GTK_CONTAINER (align
), hbox
);
1512 gtk_widget_show_all (align
);
1517 label
= gtk_label_new_with_mnemonic (text
);
1518 gtk_label_set_mnemonic_widget (GTK_LABEL (label
), GTK_WIDGET (button
));
1520 gtk_misc_set_alignment (GTK_MISC (label
), 0.5, 0.5);
1522 gtk_widget_show (label
);
1523 gtk_container_add (GTK_CONTAINER (button
), label
);
1529 anjuta_dialog_add_button (GtkDialog
*dialog
, const gchar
* text
,
1530 const gchar
* stock_id
, gint response_id
)
1534 g_return_val_if_fail (GTK_IS_DIALOG (dialog
), NULL
);
1535 g_return_val_if_fail (text
!= NULL
, NULL
);
1536 g_return_val_if_fail (stock_id
!= NULL
, NULL
);
1538 button
= anjuta_button_new_with_stock_image (text
, stock_id
);
1539 g_return_val_if_fail (button
!= NULL
, NULL
);
1541 GTK_WIDGET_SET_FLAGS (button
, GTK_CAN_DEFAULT
);
1543 gtk_widget_show (button
);
1545 gtk_dialog_add_action_widget (dialog
, button
, response_id
);