Cleanup sokoke
This commit is contained in:
parent
84036dc39e
commit
5fcb26b123
2 changed files with 156 additions and 124 deletions
129
src/sokoke.c
129
src/sokoke.c
|
@ -98,16 +98,17 @@ sokoke_entry_append_completion (GtkEntry* entry, const gchar* text)
|
|||
gtk_list_store_set (GTK_LIST_STORE (completion_store), &iter, 0, text, -1);
|
||||
}
|
||||
|
||||
void sokoke_combo_box_add_strings(GtkComboBox* combobox
|
||||
, const gchar* labelFirst, ...)
|
||||
void
|
||||
sokoke_combo_box_add_strings (GtkComboBox* combobox,
|
||||
const gchar* label_first, ...)
|
||||
{
|
||||
// Add a number of strings to a combobox, terminated with NULL
|
||||
// This works only for text comboboxes
|
||||
va_list args;
|
||||
va_start(args, labelFirst);
|
||||
va_start (args, label_first);
|
||||
|
||||
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);
|
||||
|
||||
va_end (args);
|
||||
|
@ -122,13 +123,15 @@ void sokoke_widget_set_visible(GtkWidget* widget, gboolean visible)
|
|||
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
|
||||
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)
|
||||
gtk_widget_set_tooltip_text (widget, text);
|
||||
|
@ -143,14 +146,18 @@ void sokoke_widget_set_tooltip_text(GtkWidget* widget, const gchar* text)
|
|||
void
|
||||
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;
|
||||
|
||||
if (G_UNLIKELY (!tooltips))
|
||||
tooltips = gtk_tooltips_new();
|
||||
|
||||
if (text && *text)
|
||||
gtk_tool_item_set_tooltip (toolitem, tooltips, text, NULL);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
typedef struct
|
||||
|
@ -243,7 +250,8 @@ typedef enum
|
|||
SOKOKE_DESKTOP_UNKNOWN
|
||||
} SokokeDesktop;
|
||||
|
||||
static SokokeDesktop sokoke_get_desktop(void)
|
||||
static SokokeDesktop
|
||||
sokoke_get_desktop (void)
|
||||
{
|
||||
static SokokeDesktop desktop = SOKOKE_DESKTOP_UNTESTED;
|
||||
if (G_UNLIKELY (desktop == SOKOKE_DESKTOP_UNTESTED))
|
||||
|
@ -251,8 +259,8 @@ static SokokeDesktop sokoke_get_desktop(void)
|
|||
// Are we running in Xfce?
|
||||
gint result; gchar* out; gchar* err;
|
||||
gboolean success = g_spawn_command_line_sync (
|
||||
"xprop -root _DT_SAVE_MODE | grep -q xfce4"
|
||||
, &out, &err, &result, NULL);
|
||||
"xprop -root _DT_SAVE_MODE | grep -q xfce4",
|
||||
&out, &err, &result, NULL);
|
||||
if (success && !result)
|
||||
desktop = SOKOKE_DESKTOP_XFCE;
|
||||
else
|
||||
|
@ -262,7 +270,9 @@ static SokokeDesktop sokoke_get_desktop(void)
|
|||
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
|
||||
// This returns NULL if the desktop is not xfce
|
||||
|
@ -271,16 +281,18 @@ gpointer sokoke_xfce_header_new(const gchar* icon, const gchar* title)
|
|||
GtkWidget* entry = gtk_entry_new ();
|
||||
gchar* markup;
|
||||
GtkWidget* xfce_heading = gtk_event_box_new ();
|
||||
gtk_widget_modify_bg(xfce_heading, GTK_STATE_NORMAL
|
||||
, &entry->style->base[GTK_STATE_NORMAL]);
|
||||
gtk_widget_modify_bg (xfce_heading, GTK_STATE_NORMAL,
|
||||
&entry->style->base[GTK_STATE_NORMAL]);
|
||||
GtkWidget* hbox = gtk_hbox_new (FALSE, 12);
|
||||
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_ICON_SIZE_DIALOG);
|
||||
gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
|
||||
GtkWidget* label = gtk_label_new (NULL);
|
||||
gtk_widget_modify_fg (label, 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>",
|
||||
title);
|
||||
gtk_label_set_markup (GTK_LABEL (label), markup);
|
||||
gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
|
||||
gtk_container_add (GTK_CONTAINER (xfce_heading), hbox);
|
||||
|
@ -290,7 +302,7 @@ gpointer sokoke_xfce_header_new(const gchar* icon, const gchar* title)
|
|||
return NULL;
|
||||
}
|
||||
|
||||
gpointer
|
||||
GtkWidget*
|
||||
sokoke_superuser_warning_new (void)
|
||||
{
|
||||
// Create a horizontal bar with a security warning
|
||||
|
@ -315,20 +327,23 @@ sokoke_superuser_warning_new (void)
|
|||
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
|
||||
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);
|
||||
gtk_label_set_markup(GTK_LABEL(label), titleBold);
|
||||
g_free(titleBold);
|
||||
gtk_label_set_markup (GTK_LABEL (label), title_bold);
|
||||
g_free (title_bold);
|
||||
gtk_frame_set_label_widget (GTK_FRAME (frame), label);
|
||||
gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_NONE);
|
||||
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
|
||||
// For some reason we need to reset if we actually want the normal style
|
||||
|
@ -336,10 +351,10 @@ void sokoke_widget_set_pango_font_style(GtkWidget* widget, PangoStyle style)
|
|||
gtk_widget_modify_font (widget, NULL);
|
||||
else
|
||||
{
|
||||
PangoFontDescription* fontDescription = pango_font_description_new();
|
||||
pango_font_description_set_style(fontDescription, PANGO_STYLE_ITALIC);
|
||||
gtk_widget_modify_font(widget, fontDescription);
|
||||
pango_font_description_free(fontDescription);
|
||||
PangoFontDescription* font_description = pango_font_description_new ();
|
||||
pango_font_description_set_style (font_description, PANGO_STYLE_ITALIC);
|
||||
gtk_widget_modify_font (widget, font_description);
|
||||
pango_font_description_free (font_description);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -403,19 +418,27 @@ sokoke_entry_set_default_text (GtkEntry* entry,
|
|||
G_CALLBACK (sokoke_on_entry_focus_out_event), NULL);
|
||||
}
|
||||
|
||||
gchar* sokoke_key_file_get_string_default(GKeyFile* keyFile
|
||||
, const gchar* group, const gchar* key, const gchar* defaultValue, GError** error)
|
||||
gchar*
|
||||
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);
|
||||
return value == NULL ? g_strdup(defaultValue) : value;
|
||||
gchar* value = g_key_file_get_string (key_file, group, key, error);
|
||||
return value == NULL ? g_strdup (default_value) : value;
|
||||
}
|
||||
|
||||
gint sokoke_key_file_get_integer_default(GKeyFile* keyFile
|
||||
, const gchar* group, const gchar* key, const gint defaultValue, GError** error)
|
||||
gint
|
||||
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))
|
||||
return defaultValue;
|
||||
return g_key_file_get_integer(keyFile, group, key, error);
|
||||
if (!g_key_file_has_key (key_file, group, key, NULL))
|
||||
return default_value;
|
||||
return g_key_file_get_integer (key_file, group, key, error);
|
||||
}
|
||||
|
||||
gdouble
|
||||
|
@ -442,17 +465,19 @@ sokoke_key_file_get_boolean_default (GKeyFile* key_file,
|
|||
return g_key_file_get_boolean (key_file, group, key, error);
|
||||
}
|
||||
|
||||
gboolean sokoke_key_file_save_to_file(GKeyFile* keyFile
|
||||
, const gchar* filename, GError** error)
|
||||
gboolean
|
||||
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)
|
||||
return FALSE;
|
||||
FILE* fp;
|
||||
if (!(fp = fopen (filename, "w")))
|
||||
{
|
||||
*error = g_error_new(G_FILE_ERROR, G_FILE_ERROR_ACCES
|
||||
, _("Writing failed."));
|
||||
*error = g_error_new (G_FILE_ERROR, G_FILE_ERROR_ACCES,
|
||||
_("Writing failed."));
|
||||
return FALSE;
|
||||
}
|
||||
fputs (data, fp);
|
||||
|
@ -461,23 +486,13 @@ gboolean sokoke_key_file_save_to_file(GKeyFile* keyFile
|
|||
return TRUE;
|
||||
}
|
||||
|
||||
void sokoke_widget_get_text_size(GtkWidget* widget, const gchar* text
|
||||
, gint* w, gint* h)
|
||||
void
|
||||
sokoke_widget_get_text_size (GtkWidget* widget,
|
||||
const gchar* text,
|
||||
gint* width,
|
||||
gint* height)
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
57
src/sokoke.h
57
src/sokoke.h
|
@ -35,45 +35,60 @@ typedef enum {
|
|||
} SokokeMenuPos;
|
||||
|
||||
void
|
||||
sokoke_combo_box_add_strings(GtkComboBox*, const gchar*, ...);
|
||||
sokoke_combo_box_add_strings (GtkComboBox* combobox,
|
||||
const gchar* label_first, ...);
|
||||
|
||||
void
|
||||
sokoke_widget_set_visible(GtkWidget*, gboolean);
|
||||
sokoke_widget_set_visible (GtkWidget* widget,
|
||||
gboolean visible);
|
||||
|
||||
void
|
||||
sokoke_container_show_children(GtkContainer*);
|
||||
sokoke_container_show_children (GtkContainer* container);
|
||||
|
||||
void
|
||||
sokoke_widget_set_tooltip_text(GtkWidget*, const gchar*);
|
||||
sokoke_widget_set_tooltip_text (GtkWidget* widget,
|
||||
const gchar* text);
|
||||
|
||||
void
|
||||
sokoke_tool_item_set_tooltip_text(GtkToolItem*, const gchar*);
|
||||
sokoke_tool_item_set_tooltip_text (GtkToolItem* toolitem,
|
||||
const gchar* text);
|
||||
|
||||
void
|
||||
sokoke_widget_popup(GtkWidget*, GtkMenu*, GdkEventButton*, SokokeMenuPos pos);
|
||||
sokoke_widget_popup (GtkWidget* widget,
|
||||
GtkMenu* menu,
|
||||
GdkEventButton* event,
|
||||
SokokeMenuPos pos);
|
||||
|
||||
gpointer
|
||||
sokoke_xfce_header_new(const gchar*, const gchar*);
|
||||
GtkWidget*
|
||||
sokoke_xfce_header_new (const gchar* icon,
|
||||
const gchar* title);
|
||||
|
||||
gpointer
|
||||
GtkWidget*
|
||||
sokoke_superuser_warning_new (void);
|
||||
|
||||
GtkWidget*
|
||||
sokoke_hig_frame_new(const gchar*);
|
||||
sokoke_hig_frame_new (const gchar* title);
|
||||
|
||||
void
|
||||
sokoke_widget_set_pango_font_style(GtkWidget*, PangoStyle);
|
||||
sokoke_widget_set_pango_font_style (GtkWidget* widget,
|
||||
PangoStyle style);
|
||||
|
||||
void
|
||||
sokoke_entry_set_default_text(GtkEntry*, const gchar*);
|
||||
|
||||
gchar*
|
||||
sokoke_key_file_get_string_default(GKeyFile*, const gchar*, const gchar*
|
||||
, const gchar*, GError**);
|
||||
sokoke_key_file_get_string_default (GKeyFile* key_file,
|
||||
const gchar* group,
|
||||
const gchar* key,
|
||||
const gchar* default_value,
|
||||
GError** error);
|
||||
|
||||
gint
|
||||
sokoke_key_file_get_integer_default(GKeyFile*, const gchar*, const gchar*
|
||||
, const gint, GError**);
|
||||
sokoke_key_file_get_integer_default (GKeyFile* key_file,
|
||||
const gchar* group,
|
||||
const gchar* key,
|
||||
const gint default_value,
|
||||
GError** error);
|
||||
|
||||
gdouble
|
||||
sokoke_key_file_get_double_default (GKeyFile* key_file,
|
||||
|
@ -90,12 +105,14 @@ sokoke_key_file_get_boolean_default (GKeyFile* key_file,
|
|||
GError** error);
|
||||
|
||||
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
|
||||
sokoke_widget_get_text_size(GtkWidget*, const gchar*, gint*, gint*);
|
||||
|
||||
void
|
||||
sokoke_menu_item_set_accel(GtkMenuItem*, const gchar*, const gchar*, GdkModifierType);
|
||||
sokoke_widget_get_text_size (GtkWidget* widget,
|
||||
const gchar* text,
|
||||
gint* width,
|
||||
gint* height);
|
||||
|
||||
#endif /* !__SOKOKE_H__ */
|
||||
|
|
Loading…
Reference in a new issue