summaryrefslogtreecommitdiffstats
path: root/kspread/kspread_canvas.h
blob: 95647e7533bdca335668443d269edeaef103329c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
/* This file is part of the KDE project

   Copyright 2006 Stefan Nikolaus <[email protected]>
   Copyright 1999-2002,2004 Laurent Montel <[email protected]>
   Copyright 2002-2005 Ariya Hidayat <[email protected]>
   Copyright 1999-2001,2003 David Faure <[email protected]>
   Copyright 2001-2003 Philipp Mueller <[email protected]>
   Copyright 2002-2003 Norbert Andres <[email protected]>
   Copyright 2000-2001 Werner Trobin <[email protected]>
   Copyright 2002 Harri Porten <[email protected]>
   Copyright 2002 John Dailey <[email protected]>
   Copyright 1999-2000 Torben Weis <[email protected]>
   Copyright 2000 Wilco Greven <[email protected]>
   Copyright 1999 Boris Wedl <[email protected]>
   Copyright 1999 Reginald Stadlbauer <[email protected]>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library 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 GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/

#ifndef KSPREAD_CANVAS
#define KSPREAD_CANVAS

#include <vector>

#include <tqlineedit.h>
#include <tqtooltip.h>
#include <tqpen.h>

#include <ksharedptr.h>

#include <koffice_export.h>

#include "kspread_util.h"

#define YBORDER_WIDTH 50
#define XBORDER_HEIGHT 20

class TQWidget;
class TQTimer;
class TQButton;
class TQPainter;
class TQLabel;
class TQScrollBar;
class KoRect;
class KoPoint;

namespace KSpread
{

class Cell;
class EditWidget;
class Canvas;
class HBorder;
class VBorder;
class Sheet;
class Doc;
class Point;
class Range;
class Region;
class View;
class Selection;
class CellEditor;
class LocationEditWidget;
class ComboboxLocationEditWidget;
class EmbeddedObject;


/**
 * The canvas builds a part of the GUI of KSpread.
 * It contains the borders, scrollbars,
 * editwidget and of course it displays the sheet.
 * Especially most of the user interface logic is implemented here.
 * That means that this class knows what to do when a key is pressed
 * or if the mouse button was clicked.
 */
class KSPREAD_EXPORT Canvas : public TQWidget
{
    friend class HBorder;
    friend class VBorder;
    friend class View;

    TQ_OBJECT
  
public:
    /**
     * The current action associated with the mouse.
     * Default is 'NoAction'.
     */
    enum MouseActions
    {
      /** No mouse action (default) */
      NoAction,
      /** Marking action */
      Mark,
      /** Merging cell */
      ResizeCell,
      /** Autofilling */
      AutoFill,
      /** Resizing the selection */
      ResizeSelection
    };
    enum EditorType { CellEditor, EditWidget };

    Canvas (View *_view);
    ~Canvas( );

    View* view() const;
    Doc* doc() const;

    KSpread::EditWidget* editWidget() const;
    KSpread::CellEditor* editor() const;

    /**
     * @return the usual selection of cells
     */
    Selection* selectionInfo() const;
    /**
     * @return a selection of cells used in formulas
     */
    Selection* choice() const;

    /**
     * @deprecated use selectionInfo()
     * Selections are no longer contiguous in general.
     * @see Selection::lastRange()
     * @see Selection::activeElement()
    */
    TQRect selection() const;
    /**
     * convenience function.
     * @return selection's marker
     * @see Selection::marker()
     */
    TQPoint marker() const;
    /**
     * convenience function.
     * @return selection's marker's column
     * @see Selection::marker()
     */
    int markerColumn() const;
    /**
     * convenience function.
     * @return selection's marker's row
     * @see Selection::marker()
     */
    int markerRow() const;

    /**
     * @return the pen, the default grid is painted with (light gray)
     */
    const TQPen& defaultGridPen() const;

    /**
     * convenience function
     * @see View::zoom()
     */
    double zoom() const;

    /**
     * @return the width of the columns before the current screen
     */
    double xOffset() const;
    /**
     * @return the height of the rows before the current screen
     */
    double yOffset() const;
    /**
     * Sets the width of the columns before the current screen
     */
    void  setXOffset( double _xOffset );
    /**
     * Sets the height of the rows before the current screen
     */
    void  setYOffset( double _yOffset );

    /**
     * @return a rect indicating which cell range is currently visible onscreen
     */
    TQRect visibleCells() const;

    /**
     * @return a pointer to the active sheet
     */
    Sheet* activeSheet() const;
    /**
     * convenience function
     * @see Map::findSheet()
     */
    Sheet* findSheet( const TQString& _name ) const;


    /**
     * Validates the selected cell.
     */
    void validateSelection();


    /**
     * Paint all visible cells that have a paint dirty flag set
     */
    void paintUpdates();


    /**
     * Makes sure a cell is visible onscreen by scrolling up/down and left/right
     *
     * @param location the cell coordinates to scroll to
     */
    void scrollToCell(TQPoint location) const;
    /**
     * Chooses the correct EditorType by looking at
     * the current cells value. By default CellEditor is chosen.
     */
    void createEditor( bool captureArrowKeys=false );
    bool createEditor( EditorType type, bool addFocus = true, bool captureArrowKeys=false );
    /**
     * Deletes the current cell editor.
     *
     * @see #createEditor
     * @see #editor
     * @param saveChanges if true, the edited text is stored in the cell.
     *                    if false, the changes are discarded.
     * @param array if true, array formula was entered
     */
    void deleteEditor(bool saveChanges, bool array = false);

    /**
     * Called from @ref EditWidget and CellEditor
     * if they loose the focus because the user started a "choose selection".
     * This is done because the editor wants to get its focus back afterwards.
     * But somehow Canvas must know whether the EditWidget or the CellEditor
     * lost the focus when the user clicked on the canvas.
     */
    void setLastEditorWithFocus( EditorType type );

    /**
     * Switches to choose mode and sets the initial selection to the
     * position returned by marker().
     * Clears the choice.
     */
    void startChoose();
    /**
     * Switches to choose mode and sets the initial @p selection.
     */
    void startChoose( const TQRect& selection );
    /**
     * Switches to selection mode.
     * Clear the choice.
     */
    void endChoose();
    /**
     * Switches the choose mode on and off.
     * Does not clear the choice.
     */
    void setChooseMode(bool state);
    /**
     * @return @c true if choose mode is enabled, @c false otherwise
     */
    bool chooseMode() const;

    void equalizeRow();
    void equalizeColumn();

    /**
     * Updates the position widget.
     */
    void updatePosWidget();
    /**
     * Close the cell editor and saves changes.
     * @see deleteEditor()
     */
    void closeEditor();

    // Created by the view since it's layout is managed there,
    // but is in fact a sibling of the canvas, which needs to know about it.
    void setEditWidget( KSpread::EditWidget * ew );

    virtual bool focusNextPrevChild( bool );

    /**
     * Depending on the offset in "zoomed" screen pixels
     * for the horizontal direction,
     * the function returns the steps in unzoomed points
     * for the autoscroll acceleration
     */
    double autoScrollAccelerationX( int offset );
    /**
     * Depending on the offset in "zoomed" screen pixels
     * for the vertical direction,
     * the function returns the steps in unzoomed points
     * for the autoscroll acceleration
     */
    double autoScrollAccelerationY( int offset );

    //TODO: These embedded-object related methods need API documentation!
    EmbeddedObject* getObject( const TQPoint &pos, Sheet *_sheet );
    void selectAllObjects();
    void deselectAllObjects();
    void selectObject( EmbeddedObject* );
    void deselectObject( EmbeddedObject* );
    void setMouseSelectedObject(bool b);
    bool isObjectSelected();

    /**
     * @brief Move object by mouse
     *
     * @param pos The position of the mouse
     * @param keepXorYunchanged if true keep x or y position unchanged
     */
    void moveObjectsByMouse( KoPoint &pos, bool keepXorYunchanged );

    //---- stuff needed for resizing ----
    /// resize the m_resizeObject
    void resizeObject( ModifyType _modType, const KoPoint & point, bool keepRatio );
    /// create KPrResizeCmd
    void finishResizeObject( const TQString &name, bool layout = true );

    /**
     * @brief Display object above the other objects in editiong mode
     *
     * This is used to bring a single slected object to front, so it is easier 
     * to modify.
     *
     * @param object which should be displayed above the other objects
     */
    void raiseObject( EmbeddedObject *object );
    /**
     * @brief Don't display an object above the others
     */
    void lowerObject();
    /**
     * @brief Get the list of objects in the order they should be displayed.
     *
     * This takes into acount the object set in raiseObject so that it is 
     * the last one in the list returned (the one that is displayed above all
     * the others).
     *
     * @return List of objects
     */
    void displayObjectList( TQPtrList<EmbeddedObject> &list );

    KoRect objectRect( bool all ) const;

    void repaintObject( EmbeddedObject *obj );

    /**
     * This is intended to copy the selected objects to the clipboard so that they can be pasted into other
     * applications.  However, until at least KWord, KSpread, KPresenter, KChart and KFormula have consistant
     * support for copying and pasting of OASIS objects the selected objects will just be copied in the form
     * of raster graphics
     */
    void copyOasisObjects();
    //void insertOasisData();

public slots:
    void slotScrollVert( int _value );
    void slotScrollHorz( int _value );

    void slotMaxColumn( int _max_column );
    void slotMaxRow( int _max_row );

signals:
    void objectSelectedChanged();
    void objectSizeChanged();

protected:
    virtual void keyPressEvent ( TQKeyEvent* _ev );
    virtual void paintEvent ( TQPaintEvent* _ev );
    virtual void mousePressEvent( TQMouseEvent* _ev );
    virtual void mouseReleaseEvent( TQMouseEvent* _ev );
    virtual void mouseMoveEvent( TQMouseEvent* _ev );
    virtual void mouseDoubleClickEvent( TQMouseEvent* );
    virtual void wheelEvent( TQWheelEvent* );
    virtual void focusInEvent( TQFocusEvent* );
    virtual void focusOutEvent( TQFocusEvent* );
    virtual void resizeEvent( TQResizeEvent * _ev );
    virtual void dragMoveEvent(TQDragMoveEvent * _ev);
    virtual void dropEvent(TQDropEvent * _ev);
    virtual void dragLeaveEvent(TQDragLeaveEvent * _ev);

    /**
     * Checks to see if there is a size grip for a highlight range at a given position. 
     * Note that both X and Y coordinates are UNZOOMED.  To translate from a zoomed coordinate (eg. position of a mouse event) to
     * an unzoomed coordinate, use Doc::unzoomItX and Doc::unzoomItY.  The document object
     * can be accessed via view()->doc()
     * @param x Unzoomed x coordinate to check
     * @param y Unzoomed y coordinate to check
     * @return @c true if there is a size grip at the specified position, @c false otherwise.
     */
    bool highlightRangeSizeGripAt(double x, double y);

private slots:

    /**
     * Scroll canvas when receiving this signal
     */
    void slotAutoScroll(const TQPoint &scrollDist);

    void doAutoScroll();
    void speakCell(TQWidget* w, const TQPoint& p, uint flags);

private:
    virtual bool eventFilter( TQObject *o, TQEvent *e );

    HBorder* hBorderWidget() const;
    VBorder* vBorderWidget() const;
    TQScrollBar* horzScrollBar() const;
    TQScrollBar* vertScrollBar() const;

    /**
     * Returns the area of the document currently visible in a painter's
     * window, calculated by taking the painter's window() property and 
     * translating it by the current x and y offset of the Canvas (taking
     * the zoom level into account)
     */
    TQRect painterWindowGeometry( const TQPainter& painter ) const;
    
    /**
     * Enables clipping and removes the areas on the canvas widget occupied by embedded objects from
     * the clip region.  This ensures that subsequent drawing operations using the given painter
     * don't paint over the area occupied by embedded objects
     */
    void clipoutChildren( TQPainter& painter ) const;

    /**
     * Returns the range of cells which appear in the specified area of the Canvas widget
     * For example, cellsInArea( TQRect(0,0,width(),height()) ) returns a range containing all visible cells
     * 
     * @param area The area (in pixels) on the Canvas widget 
     */
    TQRect cellsInArea( const TQRect area ) const;
    
    /**
     * Paints the children
     */
    void paintChildren( TQPainter& painter, TQWMatrix& matrix );

    /**
     * @see #setLastEditorWithFocus
     */
    EditorType lastEditorWithFocus() const;

private:
  void moveObject( int x, int y, bool key );

  void startTheDrag();

  /* helpers for the paintUpdates function */
  void paintNormalMarker(TQPainter& painter, const KoRect &viewRect);

  /**
  * Paint the highlighted ranges of cells.  When the user is editing a formula in a text box, cells and ranges referenced
  * in the formula are highlighted on the canvas.
  * @param painter The painter on which to draw the highlighted ranges
  * @param viewRect The area currently visible on the canvas
  */
  void paintHighlightedRanges(TQPainter& painter, const KoRect& viewRect);

  /**
  * Calculates the visible region on the canvas occupied by a range of cells on the currently active sheet.
  * This is used for drawing the thick border around the current selection or highlights around cell range
  * references.
  * The results do not take into account the current zoom factor of the sheet,
  * use Doc::zoomRect on @p visibleRect after calling this function to get a new rectangle taking
  * the zoom level into account.
  * @param sheetArea The range of cells on the current sheet
  * @param visibleRect This is set to the visible region occupied by the given range of cells
  *
  */
  void sheetAreaToVisibleRect( const TQRect& sheetArea,
			       KoRect& visibleRect ); 

  /** 
  * Calculates the physical region on the canvas widget occupied by a range of cells on
  * the currently active sheet.
  * Unlike @see sheetAreaToVisibleRect , scrolling the view does not affect sheetAreaToRect.
  *
  * @param sheetArea The range of cells on the current sheet
  * @param visibleRect This is set to the physical region occupied by the given range of cells
  */
  void sheetAreaToRect( const TQRect& sheetArea, KoRect& rect );


  /**
   * helper function in drawing the marker and choose marker.
   * @param marker the rectangle that represents the marker being drawn
   *               (cell coordinates)
   * @param viewRect the visible area on the canvas
   * @param positions output parameter where the viewable left, top, right, and
   *                  bottom of the marker will be.  They are stored in the array
   *                  in that order, and take into account cropping due to part
   *                  of the marker being off screen.  This array should have
   *                  at least a size of 4 pre-allocated.
   * @param paintSides booleans indicating whether a particular side is visible.
   *                   Again, these are in the order left, top, right, bottom.
   *                   This should be preallocated with a size of at least 4.
   */
  void retrieveMarkerInfo( const TQRect &marker, const KoRect &viewRect,
                           double positions[], bool paintSides[] );




  bool formatKeyPress( TQKeyEvent * _ev );

  /** helper method for formatKeyPress */
  bool formatCellByKey(Cell *cell, int key, const TQRect &rect);

  void processClickSelectionHandle(TQMouseEvent *event);
  void processLeftClickAnchor();


  /** current cursor position, be it marker or choose marker */
  TQPoint cursorPos();

  /**
   * returns the rect that needs to be redrawn
   */
  TQRect moveDirection(KSpread::MoveTo direction, bool extendSelection);

  void processEnterKey(TQKeyEvent *event);
  void processArrowKey(TQKeyEvent *event);
  void processEscapeKey(TQKeyEvent *event);
  bool processHomeKey(TQKeyEvent *event);
  bool processEndKey(TQKeyEvent *event);
  bool processPriorKey(TQKeyEvent *event);
  bool processNextKey(TQKeyEvent *event);
  void processDeleteKey(TQKeyEvent *event);
  void processF2Key(TQKeyEvent *event);
  void processF4Key(TQKeyEvent *event);
  void processOtherKey(TQKeyEvent *event);
  bool processControlArrowKey(TQKeyEvent *event);

  void processIMEvent( TQIMEvent * event );

  /**
   * Used in choose mode. Shows/hides the editor depending on the selected
   * sheet. Triggers an update of the regions shown in the CellEditor.
   * @see CellEditor::updateChoice()
   */
  void updateEditor();

  /**
   * This function sets the paint dirty flag for a @p changedRegion in a
   * @p sheet .
   * The calculation which cells really should look different with the new
   * selection rather than repainting the entire area has to be done before.
   * @param sheet the sheet, which contains the cells
   * @param changedRegion the cell region to be set as dirty
   */
  void setSelectionChangePaintDirty(Sheet* sheet, const Region& changedRegion);

private:
  class Private;
  Private* d;
};



/**
 * HBorder
 */
class HBorder : public TQWidget
{
    TQ_OBJECT
  
public:
    HBorder( TQWidget *_parent, Canvas *_canvas, View *_view  );
    ~HBorder();

    int markerColumn() const { return  m_iSelectionAnchor; }
    void equalizeColumn( double resize );

    void updateColumns( int from, int to );

    TQSize sizeHint() const;

private slots:
    void doAutoScroll();

protected:
    virtual void paintEvent ( TQPaintEvent* _ev );
    virtual void mousePressEvent( TQMouseEvent* _ev );
    virtual void mouseReleaseEvent( TQMouseEvent* _ev );
    virtual void mouseDoubleClickEvent( TQMouseEvent* _ev );
    virtual void mouseMoveEvent( TQMouseEvent* _ev );
    virtual void wheelEvent( TQWheelEvent* );
    virtual void focusOutEvent( TQFocusEvent* ev );
    virtual void resizeEvent( TQResizeEvent * _ev );
    void paintSizeIndicator( int mouseX, bool firstTime );

private:
    Canvas *m_pCanvas;
    View *m_pView;
    TQTimer * m_scrollTimer;

    /**
     * Flag that inidicates whether the user wants to mark columns.
     * The user may mark columns by dragging the mouse around in th XBorder widget.
     * If he is doing that right now, this flag is true. Mention that the user may
     * also resize columns by dragging the mouse. This case is not covered by this flag.
     */
    bool m_bSelection;

    /**
     * The column over which the user pressed the mouse button.
     * If the user marks columns in the XBorder widget, then this is the initial
     * column on which he pressed the mouse button.
     */
    int m_iSelectionAnchor;

    /**
     * Flag that indicates whether the user resizes a column
     * The user may resize columns by dragging the mouse around in the HBorder widget.
     * If he is doing that right now, this flag is true.
     */
    bool m_bResize;

    /**
     * The column over which the user pressed the mouse button.
     * The user may resize columns by dragging the mouse around the XBorder widget.
     * This is the column over which he pressed the mouse button. This column is going
     * to be resized.
      */
    int m_iResizedColumn;

    /**
     * Last position of the mouse, when resizing.
     */
    int m_iResizePos;

    /**
     * The label used for showing the current size, when resizing
     */
    TQLabel *m_lSize;

    /**
     * True when the mouse button is pressed
     */
    bool m_bMousePressed;

private:
};



/**
 * VBorder
 */
class VBorder : public TQWidget
{
    TQ_OBJECT
  
public:
    VBorder( TQWidget *_parent, Canvas *_canvas, View *_view );
    ~VBorder();

    int markerRow() const { return  m_iSelectionAnchor; }
    void equalizeRow( double resize );
    void updateRows( int from, int to );

    TQSize sizeHint() const;

private slots:
    void doAutoScroll();

protected:
    virtual void paintEvent ( TQPaintEvent* _ev );
    virtual void mousePressEvent( TQMouseEvent* _ev );
    virtual void mouseReleaseEvent( TQMouseEvent* _ev );
    virtual void mouseMoveEvent( TQMouseEvent* _ev );
    virtual void mouseDoubleClickEvent( TQMouseEvent* _ev );
    virtual void wheelEvent( TQWheelEvent* );
    virtual void focusOutEvent( TQFocusEvent* ev );
    void paintSizeIndicator( int mouseY, bool firstTime );

private:
    Canvas *m_pCanvas;
    View *m_pView;
    TQTimer * m_scrollTimer;

    bool m_bSelection;
    int m_iSelectionAnchor;
    bool m_bResize;
    int m_iResizedRow;
    int m_iResizePos;
    /**
     * The label used for showing the current size, when resizing
     */
    TQLabel *m_lSize;

    /**
     * True when the mouse button is pressed
     */
    bool m_bMousePressed;
};



/**
 * Tooltip, which displays the comment and cell content, when it's too short
 */
class ToolTip : public TQToolTip
{
public:
    ToolTip( Canvas* canvas );

protected:
    /**
     * @reimp
     */
    void maybeTip( const TQPoint& p );

private:
    Canvas* m_canvas;
};

} // namespace KSpread

#endif // KSPREAD_CANVAS