summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: ea20810)
raw | patch | inline | side by side (parent: ea20810)
author | Eric Wertz <ericdwertz@gmail.com> | |
Sat, 22 Jul 2017 01:17:38 +0000 (21:17 -0400) | ||
committer | Eric Wertz <ericdwertz@gmail.com> | |
Sat, 22 Jul 2017 01:17:38 +0000 (21:17 -0400) |
main.c | patch | blob | history |
index bf9be8d63fac08ed6e4ef5802bcb2743fdfec23f..a678dc8d5afcc66b638457351ea1497575d33573 100644 (file)
--- a/main.c
+++ b/main.c
#define BUFFER_SIZE 65536
+#define MSG_NORMAL 0
+#define MSG_ERROR 1
+#define MSG_SUCCESS 2
+
typedef struct {
char query_string[BUFFER_SIZE];
MYSQL* con;
} query_data;
+typedef struct {
+ char* message;
+ int type;
+} log_message;
+
GtkWidget* result_window;
GtkWidget* result_tabs;
GtkWidget* log_text;
int query_count = 1;
+GtkTextTag* text_normal;
+GtkTextTag* text_success;
+GtkTextTag* text_error;
+
gboolean log_text_append( gpointer data )
{
+ log_message* msg = (log_message*)data;
+
+ GtkTextTag* tag;
+ switch( msg->type )
+ {
+ case MSG_NORMAL:
+ tag = text_normal;
+ break;
+ case MSG_ERROR:
+ tag = text_error;
+ break;
+ case MSG_SUCCESS:
+ tag = text_success;
+ break;
+ default:
+ tag = text_normal;
+ break;
+ }
+
GtkTextIter iter;
GtkTextBuffer* text_buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW( log_text ) );
+ GtkTextMark* mark = gtk_text_buffer_get_insert( text_buffer );
gtk_text_buffer_get_end_iter( text_buffer, &iter );
- gtk_text_buffer_insert( text_buffer, &iter, (const char*)data, -1 );
+ gtk_text_buffer_move_mark( text_buffer, mark, &iter );
+ gtk_text_buffer_insert_with_tags( text_buffer, &iter, msg->message, -1, tag, NULL );
+
+ gtk_text_view_scroll_to_mark( GTK_TEXT_VIEW( log_text ), mark, 0.0, TRUE, 0.0, 0.0 );
- free( data );
+ free( msg->message );
+ free( msg );
return FALSE;
}
-void print_message( const char* str, ... )
+void print_message( const char* str, int type, ... )
{
- char* buffer = malloc( sizeof(char)*1024 );
+ log_message* msg = malloc( sizeof(log_message) );
+ msg->message = malloc( sizeof(char)*BUFFER_SIZE );
+
va_list args;
- va_start( args, str );
- vsprintf( buffer, str, args );
+ va_start( args, type );
+ vsprintf( msg->message, str, args );
va_end( args );
+ msg->type = type;
- gdk_threads_add_idle( log_text_append, (gpointer)buffer );
+ gdk_threads_add_idle_full( G_PRIORITY_HIGH_IDLE, log_text_append, (gpointer)msg, NULL );
}
void print_error( MYSQL* con )
{
- print_message( "MySQL Error: %s\n", mysql_error(con) );
+ print_message( "MySQL Error: %s\n", MSG_ERROR, mysql_error(con) );
}
GtkWidget* create_query_result_grid( MYSQL_RES* result )
g_value_init( &col_values[i], G_TYPE_STRING );
}
- //Create the list store and store the query results, it is waaayyyy faster to do this before
- //creating the widgets and attaching the list to them.
- list_store=gtk_list_store_newv( num_fields, list_types );
- while( row = mysql_fetch_row( result ) )
- {
- gtk_list_store_append( list_store, &iter );
- for( i = 0; i< num_fields; i++ )
- {
- g_value_set_string( &col_values[i], row[i] ? row[i] : "NULL" );
- }
- gtk_list_store_set_valuesv( list_store, &iter, col_index, col_values, num_fields );
- }
-
- //Create the widgets
- sortmodel=gtk_tree_model_sort_new_with_model(GTK_TREE_MODEL(list_store));
- listwidget=gtk_tree_view_new_with_model(sortmodel);
+ listwidget=gtk_tree_view_new();
//Create column headings inside grid
for( i = 0; i < num_fields; i++ )
//gtk_tree_view_column_set_sort_column_id( col,i );
//gtk_tree_view_column_pack_start( col, renderer, TRUE );
}
-
GtkWidget* scrollarea=gtk_scrolled_window_new(NULL,NULL);
gtk_container_add(GTK_CONTAINER(scrollarea),listwidget);
+ gtk_widget_show_all( scrollarea );
- gtk_tree_view_columns_autosize(GTK_TREE_VIEW(listwidget));
+ //Create the list store and store the query results, it is waaayyyy faster to do this before
+ //creating the widgets and attaching the list to them.
+ list_store=gtk_list_store_newv( num_fields, list_types );
+ while( row = mysql_fetch_row( result ) )
+ {
+ gtk_list_store_append( list_store, &iter );
+ for( i = 0; i< num_fields; i++ )
+ {
+ g_value_set_string( &col_values[i], row[i] ? row[i] : "NULL" );
+ }
+ gtk_list_store_set_valuesv( list_store, &iter, col_index, col_values, num_fields );
+ }
+ gtk_tree_view_set_model( GTK_TREE_VIEW(listwidget), GTK_TREE_MODEL(list_store) );
+
+ //gtk_tree_view_columns_autosize(GTK_TREE_VIEW(listwidget));
return scrollarea;
}
void create_query_result_tab( MYSQL_RES* result )
{
char tab_title[64];
+ int i;
sprintf( tab_title, "Result %i", query_count++ );
GtkWidget* tab_contents = create_query_result_grid( result );
- gtk_notebook_append_page( GTK_NOTEBOOK(result_tabs), tab_contents, gtk_label_new( tab_title ) );
+ i = gtk_notebook_append_page( GTK_NOTEBOOK(result_tabs), tab_contents, gtk_label_new( tab_title ) );
+ i = gtk_notebook_get_n_pages( GTK_NOTEBOOK(result_tabs) ) - i;
+
+ while( i > 0 )
+ {
+ gtk_notebook_next_page( GTK_NOTEBOOK(result_tabs) );
+ i--;
+ }
+
gtk_widget_show_all( tab_contents );
}
if(keyevent->keyval==GDK_KEY_Escape) gtk_main_quit();
}
+//Init the various tags for formatting text in the log container
+void init_text_tags()
+{
+ GtkTextBuffer* text_buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW( log_text ) );
+ text_normal = gtk_text_buffer_create_tag( text_buffer, "normal", "foreground", "#000000", NULL );
+ text_error = gtk_text_buffer_create_tag( text_buffer, "error", "foreground", "#CC0000", NULL );
+ text_success = gtk_text_buffer_create_tag( text_buffer, "success", "foreground", "#007700", NULL );
+}
+
//Create a gtk window to hold our query output
void create_query_window()
{
gtk_widget_show_all(result_window);
- GtkTextIter iter;
- GtkTextBuffer* text_buffer;
-
result_tabs = gtk_notebook_new();
gtk_notebook_set_show_tabs( GTK_NOTEBOOK( result_tabs ), TRUE );
gtk_widget_set_hexpand( result_tabs, TRUE );
gtk_widget_set_size_request( log_text, -1, 192 );
gtk_text_view_set_cursor_visible( GTK_TEXT_VIEW( log_text ), FALSE );
+ GtkWidget* scrollarea = gtk_scrolled_window_new( NULL, NULL );
+ gtk_container_add( GTK_CONTAINER(scrollarea), log_text );
+
//Split the window into two pieces
GtkPaned* pane = GTK_PANED( gtk_paned_new( GTK_ORIENTATION_VERTICAL ) );
gtk_paned_add1( pane, result_tabs );
- gtk_paned_add2( pane, log_text );
+ gtk_paned_add2( pane, scrollarea );
gtk_paned_set_position( pane, 500 );
gtk_container_add( GTK_CONTAINER( result_window ), GTK_WIDGET( pane ) );
g_signal_connect(G_OBJECT(result_window), "delete-event", gtk_main_quit, NULL);
gtk_widget_show_all(result_window);
+
+ init_text_tags();
}
gboolean fetch_query( gpointer result )
return (double)dt.tv_sec + ((double)dt.tv_usec/1000000.0);
}
-void run_query( gpointer data )
+gboolean run_query( gpointer data )
{
query_data* sql_data = (query_data*)data;
time_diff( TRUE );
- print_message( "Running Query: %s\n", sql_data->query_string );
+ print_message( "Running Query: %s\n", MSG_NORMAL, sql_data->query_string );
if( mysql_query( sql_data->con, sql_data->query_string ) )
{
print_error( sql_data->con );
- return;
+ return FALSE;
}
MYSQL_RES* result = mysql_store_result( sql_data->con );
if( result == NULL )
{
- print_message( "Query Successful (%.4fs)\n", time_diff( FALSE ) );
- return;
+ print_message( "Query Successful (%.4fs)\n", MSG_SUCCESS, time_diff( FALSE ) );
+ return TRUE;
}
- print_message( "Query Successful, %i rows returned (%.4fs)\n", mysql_num_rows( result ), time_diff( FALSE ) );
- gdk_threads_add_idle( fetch_query, (gpointer)result );
+ print_message( "Query Successful, %'i rows returned (%.4fs)\n", MSG_SUCCESS, mysql_num_rows( result ), time_diff( FALSE ) );
+ gdk_threads_add_idle_full( G_PRIORITY_HIGH_IDLE, fetch_query, (gpointer)result, NULL );
- return;
+ return TRUE;
}
gpointer execute_input( gpointer data )
strcpy( sql_data.query_string, token );
if( strlen( sql_data.query_string ) > 1 )
- run_query( (gpointer)&sql_data );
+ if( !run_query( (gpointer)&sql_data ) )
+ break;
token = strtok( NULL, ";" );
}
int main( int argc, char* argv[] )
{
+ //Fork into background
+ if( fork() != 0 )
+ exit( 0 );
+
gtk_init( &argc, &argv );
char buffer[BUFFER_SIZE];