<commit_before>/*******************************************************************************
Copyright (c) 2010 Jon "ylixir" Allen <<EMAIL>>. All rights reserved.

This file is part of yAEd.

yAEd is free software: you can redistribute it and/or modify
it under the terms of the Artistic License 2.0 as published by
the Perl Foundation.

yAEd is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Artistic License 2.0 for more details.

You should have received a copy of the Artistic License 2.0
along with yAEd.  If not, see <http://www.perlfoundation.org>.
*******************************************************************************/

#include "location-bar.h"
#include "spider.h"

/*
 * private types
 */

struct YaedLocationBar
{
  GtkHBox* box;
  GtkEntry* entry;
  YaedSourceModelHandle model;
};

/*
 * private functions
 */

//called when (before) the user inserts text into the location bar
void yaedLocationBarInsertText(
  GtkEntry* entry,
  gchar* incoming_text,
  gint incoming_size,  //size in bytes, maybe -1 if NULL term
  gint* incoming_position,     //position in characters for insertion
  YaedLocationBarHandle bar)
{
  //make our compiler happy
  bar=NULL;

  //our variables, wooo
  const gchar* old_text = gtk_entry_get_text(entry);
  gchar* new_text;
  size_t incoming_length, old_length;
  size_t old_size, new_size;
  gchar* built_ptr;

  //get the number of characters that are incoming
  incoming_length = g_utf8_strlen(incoming_text, incoming_size);
  //get the size of the incoming text
  incoming_size =
    g_utf8_offset_to_pointer(incoming_text, incoming_length) - incoming_text;

  //get the old_text info
  old_length = g_utf8_strlen(old_text, -1);
  old_size = g_utf8_offset_to_pointer(old_text, old_length) - old_text;

  //for readabilit, the new_size
  new_size = old_size+incoming_size;

  //build the string
  new_text = g_slice_alloc(new_size+1);
  g_utf8_strncpy(new_text, old_text, *incoming_position);
  built_ptr = g_utf8_offset_to_pointer(new_text, *incoming_position);
  g_utf8_strncpy(built_ptr, incoming_text, incoming_length);
  g_utf8_strncpy(
    built_ptr + incoming_size,
    old_text + (built_ptr - new_text),
    old_length - *incoming_position);
    
  //check our result
  printf("%s\t%s\n",old_text, new_text);

  //cleanup
  g_slice_free1(new_size+1, new_text);
}

//called when the user deletes text from the location bar
void yaedLocationBarDeleteText(
  GtkEntry* entry,
  gint start_position,  //position in characters
  gint end_position,    //in characters, neg means delete to end
  YaedLocationBarHandle bar)
{
  //make our compiler happy
  bar=NULL;

  printf("%s\t%d\t%d\n", gtk_entry_get_text(entry),start_position,end_position);
}

//signal that handles user pressing an icon in the location bar
void yaedLocationBarIconPress(GtkEntry* entry,<commit_msg>location bar is catching and building deletions<commit_after>/*******************************************************************************
Copyright (c) 2010 Jon "ylixir" Allen <<EMAIL>>. All rights reserved.

This file is part of yAEd.

yAEd is free software: you can redistribute it and/or modify
it under the terms of the Artistic License 2.0 as published by
the Perl Foundation.

yAEd is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Artistic License 2.0 for more details.

You should have received a copy of the Artistic License 2.0
along with yAEd.  If not, see <http://www.perlfoundation.org>.
*******************************************************************************/

#include "location-bar.h"
#include "spider.h"

/*
 * private types
 */

struct YaedLocationBar
{
  GtkHBox* box;
  GtkEntry* entry;
  YaedSourceModelHandle model;
};

/*
 * private functions
 */

//called when (before) the user inserts text into the location bar
void yaedLocationBarInsertText(
  GtkEntry* entry,
  gchar* incoming_text,
  gint incoming_size,  //size in bytes, maybe -1 if NULL term
  gint* incoming_position,     //position in characters for insertion
  YaedLocationBarHandle bar)
{
  //make our compiler happy
  bar=NULL;

  //our variables, wooo
  const gchar* old_text = gtk_entry_get_text(entry);
  gchar* new_text;
  size_t incoming_length, old_length;
  size_t old_size, new_size;
  gchar* built_ptr;

  //get the number of characters that are incoming
  incoming_length = g_utf8_strlen(incoming_text, incoming_size);
  //get the size of the incoming text
  incoming_size =
    g_utf8_offset_to_pointer(incoming_text, incoming_length) - incoming_text;

  //get the old_text info
  old_length = g_utf8_strlen(old_text, -1);
  old_size = g_utf8_offset_to_pointer(old_text, old_length) - old_text;

  //for readabilit, the new_size
  new_size = old_size+incoming_size;

  //build the string
  new_text = g_slice_alloc(new_size+1);
  g_utf8_strncpy(new_text, old_text, *incoming_position);
  built_ptr = g_utf8_offset_to_pointer(new_text, *incoming_position);
  g_utf8_strncpy(built_ptr, incoming_text, incoming_length);
  g_utf8_strncpy(
    built_ptr + incoming_size,
    old_text + (built_ptr - new_text),
    old_length - *incoming_position);
    
  //check our result
  printf("%s\t%s\n",old_text, new_text);

  //cleanup
  g_slice_free1(new_size+1, new_text);
}

//called when the user deletes text from the location bar
void yaedLocationBarDeleteText(
  GtkEntry* entry,
  gint start_position,  //position in characters
  gint end_position,    //in characters, neg means delete to end
  YaedLocationBarHandle bar)
{
  //make our compiler happy
  bar=NULL;

  //the variables
  const gchar* old_text = gtk_entry_get_text(entry);
  gchar* new_text;
  gchar* end_text;
  size_t old_length, new_size, start_size;

  //get the length of the old string
  old_length = g_utf8_strlen(old_text, -1);

  //fixup any problems with the end position
  if(0 > end_position)
    end_position = old_length;
  if(start_position > end_position) //if the positions are swapped
  { //xor swap
    end_position ^= start_position;
    start_position ^= end_position;
    end_position ^= start_position;
  }

  //get the pointer for the chunk following the deletion
  end_text = g_utf8_offset_to_pointer(old_text, end_position);

  //get the size of the final string
  start_size = g_utf8_offset_to_pointer(old_text, start_position) - old_text;
  new_size = start_size +
    (g_utf8_offset_to_pointer(end_text, old_length-end_position) - end_text);

  //build the string
  new_text = g_slice_alloc(new_size+1);
  g_utf8_strncpy(new_text, old_text, start_position);
  g_utf8_strncpy(new_text+start_size, end_text, old_length - end_position);
  
  printf("%s\t%s\n", old_text, new_text);

  //cleanup
  g_slice_free1(new_size+1, new_text);
}

//signal that handles user pressing an icon in the location bar
void yaedLocationBarIconPress(GtkEntry* entry,
{'fraction_non_alphanumeric': 0.12512455516014234, 'fraction_numerical': 0.008113879003558718, 'mean_word_length': 4.5585443037974684, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3985111', 'n_tokens_mistral': 2339, 'n_tokens_neox': 2284, 'n_words': 679}