Пример #1
0
/* Remove newlines from string */
GString *remove_newlines_string(GString *string)
{
	int i = 0;
	while (i < string->len)
	{
		if (string->str[i] == '\n')
		g_string_overwrite(string, i, " ");
		i++;
	}
	return string;
}
Пример #2
0
void last_mod_callback(GPid pid, gint status, gpointer data)
{
  int * out_fd = (int *)data;
  gchar * comma_pos = 0, * newline_pos = 0;
  GString * msg = NULL;
  tree_node n;

  memset(&n, 0, sizeof(tree_node));

  msg = util_get_output(*out_fd);

  close(*out_fd);
  free(out_fd);

  if (!msg)
    return;

  if (g_str_has_prefix(msg->str, "Error:"))
  {
    g_string_truncate(msg, 2);
    g_string_overwrite(msg, 0, "ra");
  }
  else if (NULL != (comma_pos = g_strstr_len(msg->str, -1, ",")))
    *comma_pos = '\0';
  else if (NULL != (newline_pos = g_strstr_len(msg->str, -1, "\n")))
    *newline_pos = '\0';

  n.key = msg->str;

  gtk_tree_model_foreach(GTK_TREE_MODEL(tree_store), find_mod, &n);

  if (n.node_path)
  {
    GtkTreePath * path;
    path = gtk_tree_path_new_from_string(n.node_path);
    if (path == NULL)
      g_warning("Invalid Path");
    gtk_tree_view_expand_to_path(tree, path);
    gtk_tree_view_set_cursor(tree, path, NULL, FALSE);
    gtk_tree_path_free(path);
    g_free(n.node_path);
  }

  g_string_free(msg, TRUE);
}
Пример #3
0
gchar *
cut_utils_inspect_memory (const void *memory, size_t size)
{
    const guchar *binary = memory;
    GString *buffer;
    size_t i, n_printable_characters;
    size_t max_size = 1024;

    if (memory == NULL || size == 0)
        return g_strdup("(null)");

    buffer = g_string_sized_new(size * strlen("0xXX") +
                                (size - 1) * strlen(" ") +
                                strlen(": ") +
                                size);
    max_size = MIN(size, max_size);
    n_printable_characters = 0;
    for (i = 0; i < max_size; i++) {
        g_string_append_printf(buffer, "0x%02x ", binary[i]);
        if (g_ascii_isprint(binary[i]))
            n_printable_characters++;
    }
    if (size > max_size)
        g_string_append(buffer, "... ");

    if (n_printable_characters >= max_size * 0.3) {
        g_string_overwrite(buffer, buffer->len - 1, ": ");
        for (i = 0; i < max_size; i++) {
            if (g_ascii_isprint(binary[i])) {
                g_string_append_c(buffer, binary[i]);
            } else {
                g_string_append_c(buffer, '.');
            }
        }
        if (size > max_size)
            g_string_append(buffer, "...");
    } else {
        g_string_truncate(buffer, buffer->len - 1);
    }

    return g_string_free(buffer, FALSE);
}
Пример #4
0
char* pixbuf_to_canvas_data(GdkPixbuf* pixbuf)
{
    guchar* buf = NULL;
    int size = 0;

    int width = gdk_pixbuf_get_width(pixbuf);
    int height = gdk_pixbuf_get_height(pixbuf);
    int stride = gdk_pixbuf_get_rowstride(pixbuf);
    int pix_bit = stride / width;

    int offset = 0;
    buf = gdk_pixbuf_get_pixels_with_length(pixbuf, (guint*)&size);

    g_assert(buf != NULL);
    GString* string = g_string_sized_new(height * stride + 10);
    g_string_append_c(string, '[');

    if (pix_bit == 4) {
        for (int i=0; i<height; i++) {
            for (int j=0; j<width; j++) {
                offset = i * stride + j*4;
                g_string_append_printf(string, "%d,%d,%d,%d,",
                                       buf[offset], buf[offset+1],
                                       buf[offset+2], buf[offset+3]);
            }
        }
    } else if (pix_bit == 3) {
        for (int i=0; i<height; i++) {
            for (int j=0; j<width; j++) {
                offset = i * stride + j*3;
                g_string_append_printf(string, "%d,%d,%d,255,", buf[offset],
                                       buf[offset+1], buf[offset+2]);
            }
        }
    }

    g_string_overwrite(string, string->len-1, "]");
    return g_string_free(string, FALSE);
}
Пример #5
0
static void
do_simple_message_box(ESD_TYPE_E type, gboolean *notagain,
                      const char *secondary_msg, const char *msg_format,
                      va_list ap)
{
  GtkMessageType  gtk_message_type;
  GString        *message = g_string_new("");
  GtkWidget      *msg_dialog;
  GtkWidget      *checkbox = NULL;

  if (notagain != NULL) {
    if (*notagain) {
      /*
       * The user had checked the "Don't show this message again" checkbox
       * in the past; don't bother showing it.
       */
      return;
    }
  }

  switch (type) {

  case ESD_TYPE_INFO:
    gtk_message_type = GTK_MESSAGE_INFO;
    break;

  case ESD_TYPE_WARN:
    gtk_message_type = GTK_MESSAGE_WARNING;
    break;

  case ESD_TYPE_ERROR:
    gtk_message_type = GTK_MESSAGE_ERROR;
    break;

  default:
    g_assert_not_reached();
    gtk_message_type = GTK_MESSAGE_INFO;
    break;
  }

  /* Format the message. */
  g_string_vprintf(message, msg_format, ap);

  if (g_utf8_strlen(message->str, message->len) > MAX_MESSAGE_LEN) {
    const gchar *end = message->str + MAX_MESSAGE_LEN;
    g_utf8_validate(message->str, MAX_MESSAGE_LEN, &end);
    g_string_truncate(message, end - message->str);
    g_string_append(message, UTF8_HORIZONTAL_ELLIPSIS);
  }

  msg_dialog = gtk_message_dialog_new(GTK_WINDOW(top_level),
                                      (GtkDialogFlags)(GTK_DIALOG_MODAL|GTK_DIALOG_DESTROY_WITH_PARENT),
                                      gtk_message_type,
                                      GTK_BUTTONS_OK,
                                      "%s", message->str);

  if (secondary_msg != NULL) {
    g_string_overwrite(message, 0, secondary_msg);
    if (g_utf8_strlen(message->str, message->len) > MAX_SECONDARY_MESSAGE_LEN) {
      const gchar *end = message->str + MAX_SECONDARY_MESSAGE_LEN;
      g_utf8_validate(message->str, MAX_SECONDARY_MESSAGE_LEN, &end);
      g_string_truncate(message, end - message->str);
      g_string_append(message, UTF8_HORIZONTAL_ELLIPSIS);
    }
    gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(msg_dialog),
                                             "%s", message->str);
  }

  g_string_free(message, TRUE);

  if (notagain != NULL) {
    checkbox = gtk_check_button_new_with_label("Don't show this message again.");
    gtk_container_set_border_width(GTK_CONTAINER(checkbox), 12);
    gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(msg_dialog))),
                       checkbox, TRUE, TRUE, 0);
    gtk_widget_show(checkbox);
  }

  gtk_dialog_run(GTK_DIALOG(msg_dialog));
  if (notagain != NULL) {
    /*
     * OK, did they check the checkbox?
     */
    *notagain = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(checkbox));
  }
  gtk_widget_destroy(msg_dialog);
}
Пример #6
0
static gboolean
on_captured_event (ClutterText *text,
                   ClutterEvent *event,
                   gpointer      dummy G_GNUC_UNUSED)
{
  gboolean is_unicode_mode = FALSE;
  gunichar c;
  guint keyval;

  if (event->type != CLUTTER_KEY_PRESS)
    return FALSE;

  is_unicode_mode = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (text),
                                                        "unicode-mode"));

  c = clutter_event_get_key_unicode (event);
  keyval = clutter_event_get_key_symbol (event);
  if (keyval == CLUTTER_KEY_U)
    {
      if (is_unicode_mode)
        {
          GString *str = g_object_get_data (G_OBJECT (text), "unicode-str");

          clutter_text_set_preedit_string (text, NULL, NULL, 0);

          g_object_set_data (G_OBJECT (text), "unicode-mode",
                             GINT_TO_POINTER (FALSE));
          g_object_set_data (G_OBJECT (text), "unicode-str",
                             NULL);

          g_string_free (str, TRUE);

          return FALSE;
        }

      if (clutter_event_has_control_modifier (event))
        {
          PangoAttrList *attrs;
          PangoAttribute *a;
          GString *str = g_string_sized_new (5);

          g_string_append (str, "u");

          g_object_set_data (G_OBJECT (text),
                             "unicode-mode",
                             GINT_TO_POINTER (TRUE));
          g_object_set_data (G_OBJECT (text),
                             "unicode-str",
                             str);

          attrs = pango_attr_list_new ();

          a = pango_attr_underline_new (PANGO_UNDERLINE_SINGLE);
          a->start_index = 0;
          a->end_index = str->len;
          pango_attr_list_insert (attrs, a);

          clutter_text_set_preedit_string (text, str->str, attrs, str->len);

          pango_attr_list_unref (attrs);

          return TRUE;
        }

      return FALSE;
    }
  else if (is_unicode_mode && is_hex_digit (c))
    {
      GString *str = g_object_get_data (G_OBJECT (text), "unicode-str");
      PangoAttrList *attrs;
      PangoAttribute *a;
      gchar buf[8];
      gsize len;

      len = g_unichar_to_utf8 (c, buf);
      buf[len] = '\0';

      g_string_append (str, buf);

      g_print ("added '%s' to '%s' (len:%d)\n",
               buf,
               str->str,
               (int) str->len);

      attrs = pango_attr_list_new ();

      a = pango_attr_underline_new (PANGO_UNDERLINE_SINGLE);
      a->start_index = 0;
      a->end_index = str->len;
      pango_attr_list_insert (attrs, a);

      clutter_text_set_preedit_string (text, str->str, attrs, str->len);

      pango_attr_list_unref (attrs);

      return TRUE;
    }
  else if (is_unicode_mode && (keyval == CLUTTER_KEY_BackSpace))
    {
      GString *str = g_object_get_data (G_OBJECT (text), "unicode-str");
      PangoAttrList *attrs;
      PangoAttribute *a;

      g_string_truncate (str, str->len - 1);

      attrs = pango_attr_list_new ();

      a = pango_attr_underline_new (PANGO_UNDERLINE_SINGLE);
      a->start_index = 0;
      a->end_index = str->len;
      pango_attr_list_insert (attrs, a);

      clutter_text_set_preedit_string (text, str->str, attrs, str->len);

      pango_attr_list_unref (attrs);

      return TRUE;
    }
  else if (is_unicode_mode &&
           (keyval == CLUTTER_KEY_Return ||
            keyval == CLUTTER_KEY_KP_Enter ||
            keyval == CLUTTER_KEY_ISO_Enter ||
            keyval == CLUTTER_KEY_KP_Space))
    {
      GString *str = g_object_get_data (G_OBJECT (text), "unicode-str");
      const gchar *contents = clutter_text_get_text (text);
      gunichar uchar = 0;
      gchar ch;
      gint i;

      clutter_text_set_preedit_string (text, NULL, NULL, 0);

      g_object_set_data (G_OBJECT (text), "unicode-mode",
                         GINT_TO_POINTER (FALSE));
      g_object_set_data (G_OBJECT (text), "unicode-str",
                         NULL);

      for (i = 0; i < str->len; i++)
        {
          ch = str->str[i];

          if (is_hex_digit (ch))
            uchar += ((gunichar) to_hex_digit (ch) << ((4 - i) * 4));
        }

      g_assert (g_unichar_validate (uchar));

      g_string_overwrite (str, 0, contents);
      g_string_insert_unichar (str,
                               clutter_text_get_cursor_position (text),
                               uchar);

      i = clutter_text_get_cursor_position (text);
      clutter_text_set_text (text, str->str);

      if (i >= 0)
        i += 1;
      else
        i = -1;

      clutter_text_set_cursor_position (text, i);
      clutter_text_set_selection_bound (text, i);

      g_string_free (str, TRUE);

      return TRUE;
    }
  else
    return FALSE;
}