~ubuntu-branches/ubuntu/quantal/gnumeric/quantal

« back to all changes in this revision

Viewing changes to src/tools/dao.c

  • Committer: Bazaar Package Importer
  • Author(s): Gauvain Pocentek
  • Date: 2009-06-07 11:10:47 UTC
  • mfrom: (1.1.19 upstream) (2.1.2 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090607111047-l3rtbzfjxvmi1kx0
Tags: 1.9.8-1ubuntu1
* Merge from debian unstable, remaining changes:
  - Promoted gnumeric-doc to Recommends in gnumeric package for help to be
    installed automatically
  - gnumeric-gtk is a transitional package
  - gnumeric conflicts with gnumeric-gtk << 1.8.3-3ubuntu1
  - call initltool-update in po*
  - remove psiconv support (psiconv is in universe):
    o debian/control: remove B-D on libpsiconv-dev
    o debian/rules: don't pass --with-psiconv to ./configure

Show diffs side-by-side

added added

removed removed

Lines of Context:
36
36
#include "style.h"
37
37
#include "sheet-style.h"
38
38
#include "workbook.h"
 
39
#include "workbook-view.h"
39
40
#include "workbook-control.h"
40
41
#include "command-context.h"
41
42
#include "gnm-format.h"
42
43
#include "sheet-object-cell-comment.h"
43
44
#include "style-color.h"
 
45
#include "graph.h"
44
46
#include <goffice/app/go-doc.h>
 
47
#include <goffice/utils/go-glib-extras.h>
45
48
 
46
49
#include <glib.h>
47
50
#include <glib/gi18n-lib.h>
62
65
dao_init (data_analysis_output_t *dao,
63
66
          data_analysis_output_type_t type)
64
67
{
65
 
        if (dao == NULL)
 
68
        if (dao == NULL) {
66
69
                dao = g_new (data_analysis_output_t, 1);
 
70
                dao->use_gfree = TRUE;
 
71
        } else
 
72
                dao->use_gfree = FALSE;
67
73
 
68
74
        dao->type              = type;
69
75
        dao->start_col         = 0;
70
76
        dao->start_row         = 0;
71
77
        dao->offset_col        = 0;
72
78
        dao->offset_row        = 0;
73
 
        dao->cols              = gnm_sheet_get_max_cols (NULL);
74
 
        dao->rows              = gnm_sheet_get_max_rows (NULL);
 
79
        dao->cols              = 1;  /* Fixed in dao_prepare_output */
 
80
        dao->rows              = 1;
75
81
        dao->sheet             = NULL;
76
82
        dao->autofit_flag      = TRUE;
77
83
        dao->clear_outputrange = TRUE;
78
84
        dao->retain_format     = FALSE;
79
85
        dao->retain_comments   = FALSE;
80
86
        dao->put_formulas      = FALSE;
 
87
        dao->sos               = NULL;
 
88
        dao->omit_so           = FALSE;
81
89
 
82
90
        return dao;
83
91
}
84
92
 
85
93
data_analysis_output_t *
 
94
dao_init_new_sheet (data_analysis_output_t *dao)
 
95
{
 
96
        return dao_init (dao, NewSheetOutput);
 
97
}
 
98
 
 
99
void dao_free (data_analysis_output_t *dao)
 
100
{
 
101
        go_slist_free_custom (dao->sos, g_object_unref);
 
102
        dao->sos = NULL;
 
103
 
 
104
        if (dao->use_gfree)
 
105
                g_free (dao);
 
106
}
 
107
 
 
108
data_analysis_output_t *
86
109
dao_load_from_value (data_analysis_output_t *dao,
87
110
                     GnmValue *output_range)
88
111
{
213
236
                dao->wbc = wbc;
214
237
 
215
238
        if (dao->type == NewSheetOutput) {
 
239
                Sheet *old_sheet = wb_control_cur_sheet (dao->wbc);
216
240
                Workbook *wb = wb_control_get_workbook (dao->wbc);
217
241
                char *name_with_counter = g_strdup_printf ("%s (1)", name);
218
242
                unique_name = workbook_sheet_get_free_name
219
243
                        (wb, name_with_counter, FALSE, TRUE);
220
244
                g_free (name_with_counter);
221
 
                dao->sheet = sheet_new (wb, unique_name);
 
245
                dao->rows = gnm_sheet_get_max_rows (old_sheet);
 
246
                dao->cols = gnm_sheet_get_max_cols (old_sheet);
 
247
                dao->sheet = sheet_new (wb, unique_name, dao->cols, dao->rows);
222
248
                g_free (unique_name);
223
249
                dao->start_col = dao->start_row = 0;
224
 
                dao->rows = gnm_sheet_get_max_rows (dao->sheet);
225
 
                dao->cols = gnm_sheet_get_max_cols (dao->sheet);
226
250
                workbook_sheet_attach (wb, dao->sheet);
227
251
        } else if (dao->type == NewWorkbookOutput) {
 
252
                Sheet *old_sheet = wb_control_cur_sheet (dao->wbc);
228
253
                Workbook *wb = workbook_new ();
229
 
                dao->sheet = sheet_new (wb, name);
 
254
                dao->rows = gnm_sheet_get_max_rows (old_sheet);
 
255
                dao->cols = gnm_sheet_get_max_cols (old_sheet);
 
256
                dao->sheet = sheet_new (wb, name, dao->cols, dao->rows);
230
257
                dao->start_col = dao->start_row = 0;
231
 
                dao->rows = gnm_sheet_get_max_rows (dao->sheet);
232
 
                dao->cols = gnm_sheet_get_max_cols (dao->sheet);
233
258
                workbook_sheet_attach (wb, dao->sheet);
234
259
                dao->wbc = wb_control_wrapper_new (dao->wbc, NULL, wb, NULL);
235
260
        }
 
261
        wb_view_sheet_focus (wb_control_view (dao->wbc), dao->sheet);
 
262
 
236
263
        if (dao->rows == 0 || (dao->rows == 1 && dao->cols == 1))
237
264
                dao->rows = gnm_sheet_get_max_rows (dao->sheet) - dao->start_row;
238
265
        if (dao->cols == 0 || (dao->rows == 1 && dao->cols == 1))
304
331
 *
305
332
 */
306
333
void 
 
334
dao_set_array_expr (data_analysis_output_t *dao,
 
335
                    int col, int row, int cols, int rows,
 
336
                    GnmExpr const *expr)
 
337
{
 
338
        GnmExprTop const *texpr;
 
339
        int col_end;
 
340
        int row_end;
 
341
 
 
342
        col += dao->offset_col;
 
343
        row += dao->offset_row;
 
344
        col_end = col + cols - 1;
 
345
        row_end = row + rows - 1;
 
346
 
 
347
        /* Check that the output is in the given range, but allow singletons
 
348
         * to expand
 
349
         */
 
350
        if (dao->type == RangeOutput && (dao->cols > 1 || dao->rows > 1)) {
 
351
                if (col >= dao->cols || row >= dao->rows) {
 
352
                        gnm_expr_free (expr);
 
353
                        return;
 
354
                }
 
355
                if (col_end >= dao->cols)
 
356
                        col_end = dao->cols - 1;
 
357
                if (row_end >= dao->rows)
 
358
                        row_end = dao->rows - 1;
 
359
        }
 
360
 
 
361
        col += dao->start_col;
 
362
        row += dao->start_row;
 
363
        col_end += dao->start_col;
 
364
        row_end += dao->start_row;
 
365
        if (col >= gnm_sheet_get_max_cols (dao->sheet) 
 
366
            || row >= gnm_sheet_get_max_rows (dao->sheet)) {
 
367
                gnm_expr_free (expr);
 
368
                return;
 
369
        }
 
370
        if (col_end >= gnm_sheet_get_max_cols (dao->sheet))
 
371
                col_end = gnm_sheet_get_last_col (dao->sheet);
 
372
        if (row_end >= gnm_sheet_get_max_rows (dao->sheet))
 
373
                row_end = gnm_sheet_get_last_row (dao->sheet);
 
374
 
 
375
        texpr = gnm_expr_top_new (expr);
 
376
        gnm_cell_set_array_formula (dao->sheet, 
 
377
                                    col, row, 
 
378
                                    col_end, row_end,
 
379
                                    texpr);
 
380
}
 
381
/*
 
382
 * dao_set_cell_array_expr absorbs the reference for the expr.
 
383
 *
 
384
 */
 
385
void 
307
386
dao_set_cell_array_expr (data_analysis_output_t *dao, int col, int row,
308
387
                         GnmExpr const *expr)
309
388
{
310
 
        GnmExprTop const *texpr;
311
 
 
312
 
        col += dao->offset_col;
313
 
        row += dao->offset_row;
314
 
 
315
 
        /* Check that the output is in the given range, but allow singletons
316
 
         * to expand
317
 
         */
318
 
        if (dao->type == RangeOutput &&
319
 
            (dao->cols > 1 || dao->rows > 1) &&
320
 
            (col >= dao->cols || row >= dao->rows)) {
321
 
                gnm_expr_free (expr);
322
 
                return;
323
 
        }
324
 
 
325
 
        col += dao->start_col;
326
 
        row += dao->start_row;
327
 
        if (col >= gnm_sheet_get_max_cols (dao->sheet) || row >= gnm_sheet_get_max_rows (dao->sheet)) {
328
 
                gnm_expr_free (expr);
329
 
                return;
330
 
        }
331
 
 
332
 
        texpr = gnm_expr_top_new (expr);
333
 
        gnm_cell_set_array_formula (dao->sheet, 
334
 
                                    col, row, col, row,
335
 
                                    texpr);
 
389
        dao_set_array_expr (dao, col, row, 1, 1, expr);
336
390
}
337
391
 
338
 
 
339
392
/*
340
393
 * dao_set_cell_expr absorbs the reference for the expr.
341
394
 *
577
630
 
578
631
        pos.col = col;
579
632
        pos.row = row;
580
 
        cell_set_comment (dao->sheet, &pos, author, comment);
 
633
        cell_set_comment (dao->sheet, &pos, author, comment, NULL);
581
634
}
582
635
 
583
636
 
598
651
        actual_col = dao->start_col + col;
599
652
 
600
653
        ideal_size = sheet_col_size_fit_pixels (dao->sheet, actual_col,
601
 
                                                0, gnm_sheet_get_max_rows (dao->sheet) - 1,
 
654
                                                0, gnm_sheet_get_last_row (dao->sheet),
602
655
                                                FALSE);
603
656
        if (ideal_size == 0)
604
657
                return;
806
859
}
807
860
 
808
861
/**
 
862
 * dao_set_format:
 
863
 * @dao:
 
864
 * @col1:
 
865
 * @row1:
 
866
 * @col2:
 
867
 * @row2:
 
868
 * @format:
 
869
 *
 
870
 * set the given cell range to given format
 
871
 *
 
872
 *
 
873
 **/
 
874
void
 
875
dao_set_format (data_analysis_output_t *dao, int col1, int row1,
 
876
                int col2, int row2,
 
877
                char const * format)
 
878
{
 
879
        GnmStyle *mstyle;
 
880
 
 
881
        mstyle = gnm_style_new ();
 
882
        gnm_style_set_format_text (mstyle, format);
 
883
        dao_set_style (dao, col1, row1,
 
884
                       col2, row2, mstyle);
 
885
}
 
886
 
 
887
/**
809
888
 * dao_set_colors:
810
889
 * @dao:
811
890
 * @col1:
1055
1134
        dao_convert_to_values (dao);
1056
1135
        sheet_redraw_range (dao->sheet, &r);
1057
1136
}
 
1137
 
 
1138
 
 
1139
GnmExpr const  *
 
1140
dao_get_cellref (data_analysis_output_t *dao, int x, int y)
 
1141
{
 
1142
        GnmCellRef r;
 
1143
        r.sheet = dao->sheet;
 
1144
        r.col = x + dao->start_col + dao->offset_col;
 
1145
        r.col_relative = FALSE;
 
1146
        r.row = y + dao->start_row + dao->offset_row;
 
1147
        r.row_relative = FALSE;
 
1148
        return gnm_expr_new_cellref (&r);
 
1149
}
 
1150
 
 
1151
GnmExpr const  *
 
1152
dao_get_rangeref (data_analysis_output_t *dao, int ax, int ay,  int bx, int by)
 
1153
{
 
1154
        GnmValue *v;
 
1155
        GnmCellRef ar;
 
1156
        GnmCellRef br;
 
1157
 
 
1158
        ar.sheet = dao->sheet;
 
1159
        ar.col = ax + dao->start_col + dao->offset_col;
 
1160
        ar.col_relative = FALSE;
 
1161
        ar.row = ay + dao->start_row + dao->offset_row;
 
1162
        ar.row_relative = FALSE;
 
1163
 
 
1164
        br.sheet = dao->sheet;
 
1165
        br.col = bx + dao->start_col + dao->offset_col;
 
1166
        br.col_relative = FALSE;
 
1167
        br.row = by + dao->start_row + dao->offset_row;
 
1168
        br.row_relative = FALSE;
 
1169
 
 
1170
        v = value_new_cellrange (&ar, &br, 0, 0);
 
1171
        return gnm_expr_new_constant (v);
 
1172
}
 
1173
 
 
1174
 
 
1175
void 
 
1176
dao_set_sheet_object (data_analysis_output_t *dao, int col, int row, SheetObject* so)
 
1177
{
 
1178
        SheetObjectAnchor anchor;
 
1179
        GnmRange          anchor_r;
 
1180
 
 
1181
        g_return_if_fail (so != NULL);
 
1182
 
 
1183
        if (dao->omit_so) {
 
1184
                g_object_unref (so);
 
1185
                return;
 
1186
        }
 
1187
                
 
1188
        range_init (&anchor_r, dao->start_col + col, dao->start_row + row,
 
1189
                    dao->start_col + ((dao->cols < 5) ? dao->cols : 5),
 
1190
                    dao->start_row + ((dao->rows < 20) ? dao->rows : 20));
 
1191
 
 
1192
        sheet_object_anchor_init (&anchor, &anchor_r, 0, GOD_ANCHOR_DIR_UNKNOWN);
 
1193
        sheet_object_set_anchor (so, &anchor);
 
1194
        sheet_object_set_sheet (so, dao->sheet);
 
1195
 
 
1196
        dao->sos = g_slist_prepend (dao->sos, so);
 
1197
}
 
1198
 
 
1199
GOData  *
 
1200
dao_go_data_vector (data_analysis_output_t *dao, int ax, int ay,  int bx, int by)
 
1201
{
 
1202
        return gnm_go_data_vector_new_expr (dao->sheet, gnm_expr_top_new (dao_get_rangeref (dao, ax, ay, bx, by)));
 
1203
}
 
1204
 
 
1205
GSList *
 
1206
dao_surrender_so (data_analysis_output_t *dao)
 
1207
{
 
1208
        GSList *l = dao->sos;
 
1209
        dao->sos = NULL;
 
1210
 
 
1211
        return l; 
 
1212
}
 
1213
 
 
1214
void    
 
1215
dao_set_omit_so (data_analysis_output_t *dao, gboolean omit)
 
1216
{
 
1217
        dao->omit_so = omit;
 
1218
}