Commit 23a4b5f4 authored by Claes Sjofors's avatar Claes Sjofors

Spreadsheet editor, export to text file and import from textfile added

parent f07a18ab
......@@ -51,3 +51,6 @@ attrinvalid <Invalid attribute type> /error
attrinvisible <Attribute invisible> /error
nonextattr <No next attribute> /error
noprevattr <No previous attribute> /error
nofile <Unable to open file> /error
syntax <Syntax error> /error
noattr <Attribute not found> /error
......@@ -181,12 +181,28 @@ void WdaGtk::activate_exit( GtkWidget *w, gpointer data)
else
delete wda;
}
void WdaGtk::activate_print( GtkWidget *w, gpointer data)
{
Wda *wda = (Wda *)data;
wda->print();
}
void WdaGtk::activate_export_text( GtkWidget *w, gpointer data)
{
Wda *wda = (Wda *)data;
wda->print_textfile();
}
void WdaGtk::activate_import_text( GtkWidget *w, gpointer data)
{
Wda *wda = (Wda *)data;
wda->import_textfile();
}
void WdaGtk::activate_setclass( GtkWidget *w, gpointer data)
{
Wda *wda = (Wda *)data;
......@@ -427,7 +443,7 @@ void WdaGtk::activate_cmd_input( GtkWidget *w, gpointer data)
void WdaGtk::activate_cmd_scrolled_ok( GtkWidget *w, gpointer data)
{
WdaGtk *wda = (WdaGtk *)data;
gchar *text;
gchar *textutf8, *text;
unsigned char *s;
int sts;
......@@ -436,8 +452,11 @@ void WdaGtk::activate_cmd_scrolled_ok( GtkWidget *w, gpointer data)
gtk_text_buffer_get_start_iter( wda->cmd_scrolled_buffer, &start_iter);
gtk_text_buffer_get_end_iter( wda->cmd_scrolled_buffer, &end_iter);
text = gtk_text_buffer_get_text( wda->cmd_scrolled_buffer, &start_iter, &end_iter,
FALSE);
textutf8 = gtk_text_buffer_get_text( wda->cmd_scrolled_buffer, &start_iter, &end_iter,
FALSE);
text = g_convert( textutf8, -1, "ISO8859-1", "UTF-8", NULL, NULL, NULL);
g_free( textutf8);
// Replace ctrl characters with space
for ( s = (unsigned char *) text; *s; s++) {
if ( *s < ' ' && *s != 10 && *s != 13)
......@@ -583,6 +602,14 @@ WdaGtk::WdaGtk(
gtk_widget_add_accelerator( file_prev_attr, "activate", accel_g,
'p', GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE);
GtkWidget *file_export_text = gtk_menu_item_new_with_mnemonic( "Export to Textfile");
g_signal_connect( file_export_text, "activate",
G_CALLBACK(activate_export_text), this);
GtkWidget *file_import_text = gtk_menu_item_new_with_mnemonic( "Import from Textfile");
g_signal_connect( file_import_text, "activate",
G_CALLBACK(activate_import_text), this);
GtkWidget *file_print = gtk_image_menu_item_new_from_stock(GTK_STOCK_PRINT, NULL);
g_signal_connect(file_print, "activate", G_CALLBACK(activate_print), this);
......@@ -594,6 +621,8 @@ WdaGtk::WdaGtk(
gtk_menu_shell_append(GTK_MENU_SHELL(file_menu), file_select_attr);
gtk_menu_shell_append(GTK_MENU_SHELL(file_menu), file_next_attr);
gtk_menu_shell_append(GTK_MENU_SHELL(file_menu), file_prev_attr);
gtk_menu_shell_append(GTK_MENU_SHELL(file_menu), file_export_text);
gtk_menu_shell_append(GTK_MENU_SHELL(file_menu), file_import_text);
gtk_menu_shell_append(GTK_MENU_SHELL(file_menu), file_print);
gtk_menu_shell_append(GTK_MENU_SHELL(file_menu), file_close);
......
......@@ -93,6 +93,8 @@ class WdaGtk : public Wda {
static void activate_change_value( GtkWidget *w, gpointer data);
static void activate_close_changeval( GtkWidget *w, gpointer data);
static void activate_exit( GtkWidget *w, gpointer data);
static void activate_export_text( GtkWidget *w, gpointer data);
static void activate_import_text( GtkWidget *w, gpointer data);
static void activate_print( GtkWidget *w, gpointer data);
static void activate_setclass( GtkWidget *w, gpointer data);
static void activate_setattr( GtkWidget *w, gpointer data);
......
......@@ -76,8 +76,8 @@ void Wda::change_value_cb( void *wda)
void Wda::print()
{
char filename[80] = "pwrp_tmp:wda.ps";
char cmd[200];
pwr_tFileName filename = "$pwrp_tmp/wda.ps";
pwr_tCmd cmd;
int sts;
dcli_translate_filename( filename, filename);
......@@ -87,6 +87,74 @@ void Wda::print()
sts = system( cmd);
}
void Wda::print_textfile()
{
message( ' ', "");
wow->CreateInputDialog( this, "Save as", "Enter filename",
file_selected_cb, 0, 40, 0, 0);
}
void Wda::file_selected_cb( void *ctx, void *data, char *text)
{
pwr_tFileName filename;
Wda *wda = (Wda *)ctx;
int sts;
if ( strchr( text, '/'))
strcpy( filename, text);
else {
strcpy( filename, "$pwrp_login/");
strcat( filename, text);
}
if ( strchr( filename, '.') == 0)
strcat( filename, ".wda_txt");
dcli_translate_filename( filename, filename);
sts = wda->wdanav->print_textfile( filename);
if ( EVEN(sts))
wda->message( 'E', "Unable to open file");
else {
char msg[300];
strcpy( msg, "Exported to ");
strcat( msg, filename);
wda->message( 'I', msg);
}
}
void Wda::import_textfile()
{
if ( !editmode) {
message( 'E', "Not in edit mode");
return;
}
message( ' ', "");
wow->CreateFileList( "Import Spreadsheet", "$pwrp_login", "*", "wda_txt",
import_file_cb, 0, this, 1);
}
void Wda::import_file_cb( void *ctx, char *text)
{
pwr_tFileName filename;
Wda *wda = (Wda *)ctx;
int sts;
if ( !wda->editmode) {
wda->message( 'E', "Not in edit mode");
return;
}
sprintf( filename, "$pwrp_login/%s.wda_txt", text);
dcli_translate_filename( filename, filename);
sts = wda->wdanav->import_textfile(filename);
if ( EVEN(sts)) {
wda->message( 'E', "Import error, see message window");
}
}
void Wda::set_editmode( int editmode, ldh_tSesContext ldhses)
{
this->ldhses = ldhses;
......
......@@ -106,10 +106,14 @@ class Wda {
int next_attr();
int prev_attr();
void print();
void print_textfile();
void import_textfile();
static void message_cb( void *wda, char severity, const char *message);
static void change_value_cb( void *wda);
static void set_attr_cb( void *ctx, char *text);
static void file_selected_cb( void *ctx, void *data, char *text);
static void import_file_cb( void *ctx, char *text);
};
......
......@@ -63,6 +63,7 @@
#include "wb_wnav.h"
#include "wb_wnav_brow.h"
#include "wb_wnav_item.h"
#include "cow_msgwindow.h"
static char null_str[] = "";
......@@ -1100,6 +1101,248 @@ int WdaNav::select_by_name( char *name)
return WDA__SUCCESS;
}
//
// Print content to textfile
//
int WdaNav::print_textfile( char *filename)
{
WItem *base_item;
int sts;
brow_tObject *object_list;
int object_cnt;
int i;
char *value, *valp;
pwr_tOName oname;
int size;
FILE *fp;
fp = fopen( filename, "w");
if ( !fp)
return WDA__NOFILE;
brow_GetObjectList( brow->ctx, &object_list, &object_cnt);
for ( i = 0; i < object_cnt; i++) {
brow_GetUserData( object_list[i], (void **)&base_item);
switch( base_item->type) {
case wnav_eItemType_Attr:
case wnav_eItemType_AttrInput:
case wnav_eItemType_AttrInputF:
case wnav_eItemType_AttrInputInv:
case wnav_eItemType_AttrOutput:
case wnav_eItemType_AttrArrayElem: {
WItemBaseAttr *item = (WItemBaseAttr *)base_item;
sts = ldh_ObjidToName( ldhses, item->objid, cdh_mName_volumeStrict, oname,
sizeof(oname), &size);
if ( EVEN(sts)) return sts;
switch ( item->type_id) {
case pwr_eType_Text:
sts = item->get_value( &value);
if ( EVEN(sts)) return sts;
fprintf( fp, "%s.%s \"%s\"\n", oname, item->attr, value);
break;
case pwr_eType_String:
sts = item->get_value( &value);
if ( EVEN(sts)) return sts;
fprintf( fp, "%s.%s \"%s\"\n", oname, item->attr, value);
break;
default:
sts = item->get_value( &valp);
wnav_attrvalue_to_string( ldhses, item->type_id, valp, &value, &size);
fprintf( fp, "%s.%s %s\n", oname, item->attr, value);
}
break;
}
default: ;
}
}
fclose( fp);
return WDA__SUCCESS;
}
//
// Print content to textfile
//
int WdaNav::import_textfile( char *filename)
{
WItem *base_item;
int sts;
brow_tObject *object_list;
int object_cnt;
int i;
FILE *fp;
char line[600];
char value[600];
pwr_tAName name;
char *s;
int row;
int len;
pwr_tAttrRef aref;
pwr_tOName attr;
int found;
int status = WDA__SUCCESS;
char msg[400];
brow_GetObjectList( brow->ctx, &object_list, &object_cnt);
fp = fopen( filename, "r");
if ( !fp)
return WDA__NOFILE;
brow_SetNodraw( brow->ctx);
brow_Redraw( brow->ctx, 0);
row = 0;
while ( dcli_read_line( line, sizeof( line), fp)) {
row++;
dcli_trim( line, line);
if ( line[0] == '#' || line[0] == 0)
continue;
for ( s = line; *s != 32 && *s != 9 && *s != 0; s++) ;
if ( *s == 0)
continue;
len = s - line;
if ( len > (int) sizeof(name)-1) {
sprintf( msg, "** Syntax error, %s, line %d\n", filename, row);
MsgWindow::message( 'E', msg, msgw_ePop_Default);
status = WDA__SYNTAX;
continue;
}
strncpy( name, line, len);
name[len] = 0;
strcpy( value, s);
dcli_trim( value, value);
sts = ldh_NameToAttrRef( ldhses, name, &aref);
if ( EVEN(sts)) {
sprintf( msg, "** Syntax error, %s, line %d\n", filename, row);
MsgWindow::message( 'E', msg, msgw_ePop_Default);
status = WDA__SYNTAX;
continue;
}
if ((s = strchr( name, '.')))
strcpy( attr, s+1);
else {
sprintf( msg, "** Syntax error, %s, line %d\n", filename, row);
MsgWindow::message( 'E', msg, msgw_ePop_Default);
status = WDA__SYNTAX;
continue;
}
found = 0;
for ( i = 0; i < object_cnt; i++) {
brow_GetUserData( object_list[i], (void **)&base_item);
switch( base_item->type) {
case wnav_eItemType_Attr:
case wnav_eItemType_AttrInput:
case wnav_eItemType_AttrInputF:
case wnav_eItemType_AttrInputInv:
case wnav_eItemType_AttrOutput:
case wnav_eItemType_AttrArrayElem: {
WItemBaseAttr *item = (WItemBaseAttr *)base_item;
if ( cdh_ObjidIsEqual( item->objid, aref.Objid) &&
cdh_NoCaseStrcmp( item->attr, attr) == 0) {
// Item found
found = 1;
switch ( item->type_id) {
case pwr_eType_Text: {
char str[1024];
int end_found = 0;
int len;
strcpy( str, &value[1]);
len = strlen(str);
if ( str[len-1] != '\"') {
str[len] = '\n';
str[len+1] = 0;
// Continue on next line
while ( dcli_read_line( line, sizeof( line), fp)) {
if ( strlen(str) + strlen(line) > sizeof(str)) {
sprintf( msg, "** Syntax error, %s, line %d\n", filename, row);
MsgWindow::message( 'E', msg, msgw_ePop_Default);
brow_ResetNodraw( brow->ctx);
brow_Redraw( brow->ctx, 0);
return WDA__SYNTAX;
}
strcat( str, line);
len = strlen(str);
if ( str[len-1] != '\"') {
str[len] = '\n';
str[len+1] = 0;
}
else {
str[len-1] = 0;
strcpy( value, str);
end_found = 1;
break;
}
}
}
else {
str[strlen(str)-1] = 0;
strcpy( value, str);
end_found = 1;
}
if ( !end_found) {
sprintf( msg, "** Syntax error, %s, line %d\n", filename, row);
MsgWindow::message( 'E', msg, msgw_ePop_Default);
brow_ResetNodraw( brow->ctx);
brow_Redraw( brow->ctx, 0);
return WDA__SYNTAX;
}
break;
}
case pwr_eType_String: {
// Remove ""
char str[600];
strcpy( str, &value[1]);
str[strlen(str)-1] = 0;
strcpy( value, str);
break;
}
default: ;
}
sts = set_attr_value( item->node, item->attr, value);
break;
}
}
default: ;
}
}
if ( !found) {
sprintf( msg, "** Attribute not found %s, row %d\n", name, row);
MsgWindow::message( 'E', msg, msgw_ePop_Default);
status = WDA__NOATTR;
continue;
}
}
fclose( fp);
brow_ResetNodraw( brow->ctx);
brow_Redraw( brow->ctx, 0);
return status;
}
......
......@@ -118,6 +118,8 @@ class WdaNav {
char *new_attribute, int new_attrobjects);
int find_by_objid( pwr_tObjid oi, brow_tObject *object);
int print( char *filename);
int print_textfile( char *filename);
int import_textfile( char *filename);
static int brow_cb( FlowCtx *ctx, flow_tEvent event);
static int init_brow_cb( FlowCtx *fctx, void *client_data);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment