Cleanup sokoke

This commit is contained in:
Christian Dywan 2008-06-01 17:39:57 +02:00
parent 84036dc39e
commit 5fcb26b123
2 changed files with 156 additions and 124 deletions

View file

@ -98,59 +98,66 @@ sokoke_entry_append_completion (GtkEntry* entry, const gchar* text)
gtk_list_store_set (GTK_LIST_STORE (completion_store), &iter, 0, text, -1); gtk_list_store_set (GTK_LIST_STORE (completion_store), &iter, 0, text, -1);
} }
void sokoke_combo_box_add_strings(GtkComboBox* combobox void
, const gchar* labelFirst, ...) sokoke_combo_box_add_strings (GtkComboBox* combobox,
const gchar* label_first, ...)
{ {
// Add a number of strings to a combobox, terminated with NULL // Add a number of strings to a combobox, terminated with NULL
// This works only for text comboboxes // This works only for text comboboxes
va_list args; va_list args;
va_start(args, labelFirst); va_start (args, label_first);
const gchar* label; const gchar* label;
for(label = labelFirst; label; label = va_arg(args, const gchar*)) for (label = label_first; label; label = va_arg (args, const gchar*))
gtk_combo_box_append_text(combobox, label); gtk_combo_box_append_text (combobox, label);
va_end(args); va_end (args);
} }
void sokoke_widget_set_visible(GtkWidget* widget, gboolean visible) void sokoke_widget_set_visible (GtkWidget* widget, gboolean visible)
{ {
// Show or hide the widget // Show or hide the widget
if(visible) if (visible)
gtk_widget_show(widget); gtk_widget_show (widget);
else else
gtk_widget_hide(widget); gtk_widget_hide (widget);
} }
void sokoke_container_show_children(GtkContainer* container) void
sokoke_container_show_children (GtkContainer* container)
{ {
// Show every child but not the container itself // Show every child but not the container itself
gtk_container_foreach(container, (GtkCallback)(gtk_widget_show_all), NULL); gtk_container_foreach (container, (GtkCallback)(gtk_widget_show_all), NULL);
} }
void sokoke_widget_set_tooltip_text(GtkWidget* widget, const gchar* text) void
sokoke_widget_set_tooltip_text (GtkWidget* widget, const gchar* text)
{ {
#if GTK_CHECK_VERSION(2, 12, 0) #if GTK_CHECK_VERSION(2, 12, 0)
gtk_widget_set_tooltip_text(widget, text); gtk_widget_set_tooltip_text (widget, text);
#else #else
static GtkTooltips* tooltips; static GtkTooltips* tooltips;
if(!tooltips) if (!tooltips)
tooltips = gtk_tooltips_new(); tooltips = gtk_tooltips_new ();
gtk_tooltips_set_tip(tooltips, widget, text, NULL); gtk_tooltips_set_tip (tooltips, widget, text, NULL);
#endif #endif
} }
void void
sokoke_tool_item_set_tooltip_text (GtkToolItem* toolitem, const gchar* text) sokoke_tool_item_set_tooltip_text (GtkToolItem* toolitem, const gchar* text)
{ {
// TODO: Use 2.12 api if available if (text && *text)
{
#if GTK_CHECK_VERSION(2, 12, 0)
gtk_tool_item_set_tooltip_text (toolitem, text);
#else
static GtkTooltips* tooltips = NULL; static GtkTooltips* tooltips = NULL;
if (G_UNLIKELY (!tooltips)) if (G_UNLIKELY (!tooltips))
tooltips = gtk_tooltips_new(); tooltips = gtk_tooltips_new();
if (text && *text)
gtk_tool_item_set_tooltip (toolitem, tooltips, text, NULL); gtk_tool_item_set_tooltip (toolitem, tooltips, text, NULL);
#endif
}
} }
typedef struct typedef struct
@ -243,17 +250,18 @@ typedef enum
SOKOKE_DESKTOP_UNKNOWN SOKOKE_DESKTOP_UNKNOWN
} SokokeDesktop; } SokokeDesktop;
static SokokeDesktop sokoke_get_desktop(void) static SokokeDesktop
sokoke_get_desktop (void)
{ {
static SokokeDesktop desktop = SOKOKE_DESKTOP_UNTESTED; static SokokeDesktop desktop = SOKOKE_DESKTOP_UNTESTED;
if(G_UNLIKELY(desktop == SOKOKE_DESKTOP_UNTESTED)) if (G_UNLIKELY (desktop == SOKOKE_DESKTOP_UNTESTED))
{ {
// Are we running in Xfce? // Are we running in Xfce?
gint result; gchar* out; gchar* err; gint result; gchar* out; gchar* err;
gboolean success = g_spawn_command_line_sync( gboolean success = g_spawn_command_line_sync (
"xprop -root _DT_SAVE_MODE | grep -q xfce4" "xprop -root _DT_SAVE_MODE | grep -q xfce4",
, &out, &err, &result, NULL); &out, &err, &result, NULL);
if(success && !result) if (success && !result)
desktop = SOKOKE_DESKTOP_XFCE; desktop = SOKOKE_DESKTOP_XFCE;
else else
desktop = SOKOKE_DESKTOP_UNKNOWN; desktop = SOKOKE_DESKTOP_UNKNOWN;
@ -262,35 +270,39 @@ static SokokeDesktop sokoke_get_desktop(void)
return desktop; return desktop;
} }
gpointer sokoke_xfce_header_new(const gchar* icon, const gchar* title) GtkWidget*
sokoke_xfce_header_new (const gchar* icon,
const gchar* title)
{ {
// Create an xfce header with icon and title // Create an xfce header with icon and title
// This returns NULL if the desktop is not xfce // This returns NULL if the desktop is not xfce
if(sokoke_get_desktop() == SOKOKE_DESKTOP_XFCE) if (sokoke_get_desktop () == SOKOKE_DESKTOP_XFCE)
{ {
GtkWidget* entry = gtk_entry_new(); GtkWidget* entry = gtk_entry_new ();
gchar* markup; gchar* markup;
GtkWidget* xfce_heading = gtk_event_box_new(); GtkWidget* xfce_heading = gtk_event_box_new ();
gtk_widget_modify_bg(xfce_heading, GTK_STATE_NORMAL gtk_widget_modify_bg (xfce_heading, GTK_STATE_NORMAL,
, &entry->style->base[GTK_STATE_NORMAL]); &entry->style->base[GTK_STATE_NORMAL]);
GtkWidget* hbox = gtk_hbox_new(FALSE, 12); GtkWidget* hbox = gtk_hbox_new (FALSE, 12);
gtk_container_set_border_width(GTK_CONTAINER(hbox), 6); gtk_container_set_border_width (GTK_CONTAINER (hbox), 6);
GtkWidget* image = gtk_image_new_from_icon_name(icon, GTK_ICON_SIZE_DIALOG); GtkWidget* image = gtk_image_new_from_icon_name (icon,
gtk_box_pack_start(GTK_BOX(hbox), image, FALSE, FALSE, 0); GTK_ICON_SIZE_DIALOG);
GtkWidget* label = gtk_label_new(NULL); gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
gtk_widget_modify_fg(label, GTK_STATE_NORMAL GtkWidget* label = gtk_label_new (NULL);
gtk_widget_modify_fg (label, GTK_STATE_NORMAL
, &entry->style->text[GTK_STATE_NORMAL]); , &entry->style->text[GTK_STATE_NORMAL]);
markup = g_strdup_printf("<span size='large' weight='bold'>%s</span>", title); markup = g_strdup_printf ("<span size='large' weight='bold'>%s</span>",
gtk_label_set_markup(GTK_LABEL(label), markup); title);
gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0); gtk_label_set_markup (GTK_LABEL (label), markup);
gtk_container_add(GTK_CONTAINER(xfce_heading), hbox); gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
g_free(markup); gtk_container_add (GTK_CONTAINER (xfce_heading), hbox);
g_free (markup);
return xfce_heading; return xfce_heading;
} }
return NULL; return NULL;
} }
gpointer GtkWidget*
sokoke_superuser_warning_new (void) sokoke_superuser_warning_new (void)
{ {
// Create a horizontal bar with a security warning // Create a horizontal bar with a security warning
@ -315,31 +327,34 @@ sokoke_superuser_warning_new (void)
return NULL; return NULL;
} }
GtkWidget* sokoke_hig_frame_new(const gchar* title) GtkWidget*
sokoke_hig_frame_new (const gchar* title)
{ {
// Create a frame with no actual frame but a bold label and indentation // Create a frame with no actual frame but a bold label and indentation
GtkWidget* frame = gtk_frame_new(NULL); GtkWidget* frame = gtk_frame_new (NULL);
gchar* titleBold = g_strdup_printf("<b>%s</b>", title); gchar* title_bold = g_strdup_printf ("<b>%s</b>", title);
GtkWidget* label = gtk_label_new(NULL); GtkWidget* label = gtk_label_new (NULL);
gtk_label_set_markup(GTK_LABEL(label), titleBold); gtk_label_set_markup (GTK_LABEL (label), title_bold);
g_free(titleBold); g_free (title_bold);
gtk_frame_set_label_widget(GTK_FRAME(frame), label); gtk_frame_set_label_widget (GTK_FRAME (frame), label);
gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_NONE); gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_NONE);
return frame; return frame;
} }
void sokoke_widget_set_pango_font_style(GtkWidget* widget, PangoStyle style) void
sokoke_widget_set_pango_font_style (GtkWidget* widget,
PangoStyle style)
{ {
// Conveniently change the pango font style // Conveniently change the pango font style
// For some reason we need to reset if we actually want the normal style // For some reason we need to reset if we actually want the normal style
if(style == PANGO_STYLE_NORMAL) if (style == PANGO_STYLE_NORMAL)
gtk_widget_modify_font(widget, NULL); gtk_widget_modify_font (widget, NULL);
else else
{ {
PangoFontDescription* fontDescription = pango_font_description_new(); PangoFontDescription* font_description = pango_font_description_new ();
pango_font_description_set_style(fontDescription, PANGO_STYLE_ITALIC); pango_font_description_set_style (font_description, PANGO_STYLE_ITALIC);
gtk_widget_modify_font(widget, fontDescription); gtk_widget_modify_font (widget, font_description);
pango_font_description_free(fontDescription); pango_font_description_free (font_description);
} }
} }
@ -374,7 +389,7 @@ sokoke_on_entry_focus_out_event (GtkEntry* entry,
gtk_entry_set_text (entry, defaultText); gtk_entry_set_text (entry, defaultText);
g_object_set_data (G_OBJECT(entry), g_object_set_data (G_OBJECT(entry),
"sokoke_has_default", GINT_TO_POINTER (1)); "sokoke_has_default", GINT_TO_POINTER (1));
sokoke_widget_set_pango_font_style (GTK_WIDGET(entry), sokoke_widget_set_pango_font_style (GTK_WIDGET (entry),
PANGO_STYLE_ITALIC); PANGO_STYLE_ITALIC);
} }
return FALSE; return FALSE;
@ -403,19 +418,27 @@ sokoke_entry_set_default_text (GtkEntry* entry,
G_CALLBACK (sokoke_on_entry_focus_out_event), NULL); G_CALLBACK (sokoke_on_entry_focus_out_event), NULL);
} }
gchar* sokoke_key_file_get_string_default(GKeyFile* keyFile gchar*
, const gchar* group, const gchar* key, const gchar* defaultValue, GError** error) sokoke_key_file_get_string_default (GKeyFile* key_file,
const gchar* group,
const gchar* key,
const gchar* default_value,
GError** error)
{ {
gchar* value = g_key_file_get_string(keyFile, group, key, error); gchar* value = g_key_file_get_string (key_file, group, key, error);
return value == NULL ? g_strdup(defaultValue) : value; return value == NULL ? g_strdup (default_value) : value;
} }
gint sokoke_key_file_get_integer_default(GKeyFile* keyFile gint
, const gchar* group, const gchar* key, const gint defaultValue, GError** error) sokoke_key_file_get_integer_default (GKeyFile* key_file,
const gchar* group,
const gchar* key,
const gint default_value,
GError** error)
{ {
if(!g_key_file_has_key(keyFile, group, key, NULL)) if (!g_key_file_has_key (key_file, group, key, NULL))
return defaultValue; return default_value;
return g_key_file_get_integer(keyFile, group, key, error); return g_key_file_get_integer (key_file, group, key, error);
} }
gdouble gdouble
@ -442,42 +465,34 @@ sokoke_key_file_get_boolean_default (GKeyFile* key_file,
return g_key_file_get_boolean (key_file, group, key, error); return g_key_file_get_boolean (key_file, group, key, error);
} }
gboolean sokoke_key_file_save_to_file(GKeyFile* keyFile gboolean
, const gchar* filename, GError** error) sokoke_key_file_save_to_file (GKeyFile* key_file,
const gchar* filename,
GError** error)
{ {
gchar* data = g_key_file_to_data(keyFile, NULL, error); gchar* data = g_key_file_to_data (key_file, NULL, error);
if(!data) if (!data)
return FALSE; return FALSE;
FILE* fp; FILE* fp;
if(!(fp = fopen(filename, "w"))) if (!(fp = fopen (filename, "w")))
{ {
*error = g_error_new(G_FILE_ERROR, G_FILE_ERROR_ACCES *error = g_error_new (G_FILE_ERROR, G_FILE_ERROR_ACCES,
, _("Writing failed.")); _("Writing failed."));
return FALSE; return FALSE;
} }
fputs(data, fp); fputs (data, fp);
fclose(fp); fclose (fp);
g_free(data); g_free (data);
return TRUE; return TRUE;
} }
void sokoke_widget_get_text_size(GtkWidget* widget, const gchar* text void
, gint* w, gint* h) sokoke_widget_get_text_size (GtkWidget* widget,
const gchar* text,
gint* width,
gint* height)
{ {
PangoLayout* layout = gtk_widget_create_pango_layout(widget, text); PangoLayout* layout = gtk_widget_create_pango_layout (widget, text);
pango_layout_get_pixel_size(layout, w, h); pango_layout_get_pixel_size (layout, width, height);
g_object_unref(layout); g_object_unref (layout);
}
void sokoke_menu_item_set_accel(GtkMenuItem* menuitem, const gchar* path
, const gchar* key, GdkModifierType modifiers)
{
if(path && *path)
{
gchar* accel = g_strconcat("<", g_get_prgname(), ">/", path, NULL);
gtk_menu_item_set_accel_path(GTK_MENU_ITEM(menuitem), accel);
guint keyVal = key ? gdk_keyval_from_name(key) : 0;
gtk_accel_map_add_entry(accel, keyVal, modifiers);
g_free(accel);
}
} }

View file

@ -35,45 +35,60 @@ typedef enum {
} SokokeMenuPos; } SokokeMenuPos;
void void
sokoke_combo_box_add_strings(GtkComboBox*, const gchar*, ...); sokoke_combo_box_add_strings (GtkComboBox* combobox,
const gchar* label_first, ...);
void void
sokoke_widget_set_visible(GtkWidget*, gboolean); sokoke_widget_set_visible (GtkWidget* widget,
gboolean visible);
void void
sokoke_container_show_children(GtkContainer*); sokoke_container_show_children (GtkContainer* container);
void void
sokoke_widget_set_tooltip_text(GtkWidget*, const gchar*); sokoke_widget_set_tooltip_text (GtkWidget* widget,
const gchar* text);
void void
sokoke_tool_item_set_tooltip_text(GtkToolItem*, const gchar*); sokoke_tool_item_set_tooltip_text (GtkToolItem* toolitem,
const gchar* text);
void void
sokoke_widget_popup(GtkWidget*, GtkMenu*, GdkEventButton*, SokokeMenuPos pos); sokoke_widget_popup (GtkWidget* widget,
GtkMenu* menu,
gpointer GdkEventButton* event,
sokoke_xfce_header_new(const gchar*, const gchar*); SokokeMenuPos pos);
gpointer
sokoke_superuser_warning_new(void);
GtkWidget* GtkWidget*
sokoke_hig_frame_new(const gchar*); sokoke_xfce_header_new (const gchar* icon,
const gchar* title);
GtkWidget*
sokoke_superuser_warning_new (void);
GtkWidget*
sokoke_hig_frame_new (const gchar* title);
void void
sokoke_widget_set_pango_font_style(GtkWidget*, PangoStyle); sokoke_widget_set_pango_font_style (GtkWidget* widget,
PangoStyle style);
void void
sokoke_entry_set_default_text(GtkEntry*, const gchar*); sokoke_entry_set_default_text(GtkEntry*, const gchar*);
gchar* gchar*
sokoke_key_file_get_string_default(GKeyFile*, const gchar*, const gchar* sokoke_key_file_get_string_default (GKeyFile* key_file,
, const gchar*, GError**); const gchar* group,
const gchar* key,
const gchar* default_value,
GError** error);
gint gint
sokoke_key_file_get_integer_default(GKeyFile*, const gchar*, const gchar* sokoke_key_file_get_integer_default (GKeyFile* key_file,
, const gint, GError**); const gchar* group,
const gchar* key,
const gint default_value,
GError** error);
gdouble gdouble
sokoke_key_file_get_double_default (GKeyFile* key_file, sokoke_key_file_get_double_default (GKeyFile* key_file,
@ -90,12 +105,14 @@ sokoke_key_file_get_boolean_default (GKeyFile* key_file,
GError** error); GError** error);
gboolean gboolean
sokoke_key_file_save_to_file(GKeyFile*, const gchar*, GError**); sokoke_key_file_save_to_file (GKeyFile* key_file,
const gchar* filename,
GError** error);
void void
sokoke_widget_get_text_size(GtkWidget*, const gchar*, gint*, gint*); sokoke_widget_get_text_size (GtkWidget* widget,
const gchar* text,
void gint* width,
sokoke_menu_item_set_accel(GtkMenuItem*, const gchar*, const gchar*, GdkModifierType); gint* height);
#endif /* !__SOKOKE_H__ */ #endif /* !__SOKOKE_H__ */