~ubuntu-branches/ubuntu/utopic/pgadmin3/utopic-proposed

« back to all changes in this revision

Viewing changes to pgadmin/ctl/ctlSQLGrid.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Gerfried Fuchs
  • Date: 2011-06-07 23:03:54 UTC
  • mfrom: (1.3.1 upstream) (13 sid)
  • mto: This revision was merged to the branch mainline in revision 14.
  • Revision ID: james.westby@ubuntu.com-20110607230354-3td4j9y71u4ahcvj
Tags: 1.14.0~beta1-1
* New upstream development release, adding Build-Depends on
  postgresql-server-dev-all >= 117~.
* Add Build-Depends on quilt, (un)patch to debian/rules and patch for fixing
  the include for kwlist.h in pgadmin/db/keywords.c.
* Add pg_config --includedir-server output to CPPFLAGS.
* Remove unrecognized configure options: --with-wx-config,
  --with-pgsql-include, --enable-gtk2, --enable-unicode.
* Clean up manually the files that are left behind after the broken
  distclean.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
//////////////////////////////////////////////////////////////////////////
2
2
//
3
3
// pgAdmin III - PostgreSQL Tools
4
 
// 
5
 
// Copyright (C) 2002 - 2010, The pgAdmin Development Team
 
4
//
 
5
// Copyright (C) 2002 - 2011, The pgAdmin Development Team
6
6
// This software is released under the PostgreSQL Licence
7
7
//
8
8
// ctlSQLGrid.cpp - SQL Query result window
21
21
#include "frm/frmExport.h"
22
22
 
23
23
 
 
24
#define EXTRAEXTENT_HEIGHT 6
 
25
#define EXTRAEXTENT_WIDTH  6
 
26
 
24
27
BEGIN_EVENT_TABLE(ctlSQLGrid, wxGrid)
25
 
    EVT_MENU(MNU_COPY, ctlSQLGrid::OnCopy)
 
28
        EVT_MENU(MNU_COPY, ctlSQLGrid::OnCopy)
 
29
        EVT_MOUSEWHEEL(ctlSQLGrid::OnMouseWheel)
26
30
END_EVENT_TABLE()
27
31
 
28
32
IMPLEMENT_DYNAMIC_CLASS(ctlSQLGrid, wxGrid)
31
35
{
32
36
}
33
37
 
34
 
ctlSQLGrid::ctlSQLGrid(wxWindow *parent, wxWindowID id, const wxPoint& pos, const wxSize& size)
35
 
: wxGrid(parent, id, pos, size, wxWANTS_CHARS|wxVSCROLL|wxHSCROLL)
36
 
{
37
 
    // Set cells font
38
 
    wxFont fntCells(settings->GetSQLFont());
39
 
    SetDefaultCellFont(fntCells);
40
 
    // Set labels font
41
 
    wxFont fntLabel(settings->GetSystemFont());
42
 
    fntLabel.SetWeight(wxBOLD);
43
 
    SetLabelFont(fntLabel);
44
 
    SetColLabelAlignment(wxALIGN_LEFT, wxALIGN_CENTER);
45
 
    SetRowLabelSize(50);
46
 
    SetColLabelSize(fntLabel.GetPointSize() *4);
47
 
    SetDefaultCellOverflow(false);
48
 
 
49
 
    wxAcceleratorEntry entries[1];
50
 
    entries[0].Set(wxACCEL_CTRL,                (int)'C',      MNU_COPY);
51
 
    wxAcceleratorTable accel(1, entries);
52
 
    SetAcceleratorTable(accel);
53
 
 
54
 
    Connect(wxID_ANY, wxEVT_GRID_LABEL_LEFT_DCLICK, wxGridEventHandler(ctlSQLGrid::OnLabelDoubleClick));
55
 
}
56
 
 
57
 
void ctlSQLGrid::OnCopy(wxCommandEvent& ev)
58
 
{
59
 
    Copy();
 
38
ctlSQLGrid::ctlSQLGrid(wxWindow *parent, wxWindowID id, const wxPoint &pos, const wxSize &size)
 
39
        : wxGrid(parent, id, pos, size, wxWANTS_CHARS | wxVSCROLL | wxHSCROLL)
 
40
{
 
41
        // Set cells font
 
42
        wxFont fntCells(settings->GetSQLFont());
 
43
        SetDefaultCellFont(fntCells);
 
44
        // Set labels font
 
45
        wxFont fntLabel(settings->GetSystemFont());
 
46
        fntLabel.SetWeight(wxBOLD);
 
47
        SetLabelFont(fntLabel);
 
48
        SetColLabelAlignment(wxALIGN_LEFT, wxALIGN_CENTER);
 
49
        SetRowLabelSize(50);
 
50
        SetColLabelSize(fntLabel.GetPointSize() * 4);
 
51
        SetDefaultCellOverflow(false);
 
52
 
 
53
        wxAcceleratorEntry entries[1];
 
54
        entries[0].Set(wxACCEL_CTRL,                (int)'C',      MNU_COPY);
 
55
        wxAcceleratorTable accel(1, entries);
 
56
        SetAcceleratorTable(accel);
 
57
 
 
58
        Connect(wxID_ANY, wxEVT_GRID_LABEL_LEFT_DCLICK, wxGridEventHandler(ctlSQLGrid::OnLabelDoubleClick));
 
59
}
 
60
 
 
61
void ctlSQLGrid::OnCopy(wxCommandEvent &ev)
 
62
{
 
63
        Copy();
 
64
}
 
65
 
 
66
void ctlSQLGrid::OnMouseWheel(wxMouseEvent &event)
 
67
{
 
68
        if (event.ControlDown())
 
69
        {
 
70
                wxFont fontlabel = GetLabelFont();
 
71
                wxFont fontcells = GetDefaultCellFont();
 
72
                if (event.GetWheelRotation() > 0)
 
73
                {
 
74
                        fontlabel.SetPointSize(fontlabel.GetPointSize() - 1);
 
75
                        fontcells.SetPointSize(fontcells.GetPointSize() - 1);
 
76
                }
 
77
                else
 
78
                {
 
79
                        fontlabel.SetPointSize(fontlabel.GetPointSize() + 1);
 
80
                        fontcells.SetPointSize(fontcells.GetPointSize() + 1);
 
81
                }
 
82
                SetLabelFont(fontlabel);
 
83
                SetDefaultCellFont(fontcells);
 
84
                SetColLabelSize(fontlabel.GetPointSize() * 4);
 
85
                SetDefaultRowSize(fontcells.GetPointSize() * 2);
 
86
                for (int index = 0; index < GetNumberCols(); index++)
 
87
                        SetColSize(index, -1);
 
88
                ForceRefresh();
 
89
        }
 
90
        else
 
91
                event.Skip();
60
92
}
61
93
 
62
94
wxString ctlSQLGrid::GetExportLine(int row)
63
95
{
64
 
    return GetExportLine(row, 0, GetNumberCols() - 1);
 
96
        return GetExportLine(row, 0, GetNumberCols() - 1);
65
97
}
66
98
 
67
99
 
68
100
wxString ctlSQLGrid::GetExportLine(int row, int col1, int col2)
69
101
{
70
 
    wxArrayInt cols;
71
 
    wxString str;
72
 
    int i;
73
 
 
74
 
    if (col2 < col1)
75
 
        return str;
76
 
 
77
 
    cols.Alloc(col2 - col1 + 1);
78
 
    for (i = col1; i <= col2; i++) 
 
102
        wxArrayInt cols;
 
103
        wxString str;
 
104
        int i;
 
105
 
 
106
        if (col2 < col1)
 
107
                return str;
 
108
 
 
109
        cols.Alloc(col2 - col1 + 1);
 
110
        for (i = col1; i <= col2; i++)
79
111
        {
80
 
        cols.Add(i);
81
 
    }
 
112
                cols.Add(i);
 
113
        }
82
114
 
83
 
    return GetExportLine(row, cols);
 
115
        return GetExportLine(row, cols);
84
116
}
85
117
 
86
118
wxString ctlSQLGrid::GetExportLine(int row, wxArrayInt cols)
87
119
{
88
 
    wxString str;
89
 
    unsigned int col;
90
 
 
91
 
    if (GetNumberCols() == 0)
92
 
        return str;
93
 
 
94
 
    for (col=0 ; col < cols.Count() ; col++)
95
 
    {
96
 
        if (col > 0)
97
 
            str.Append(settings->GetCopyColSeparator());
98
 
 
99
 
        wxString text = GetCellValue(row, cols[col]);
 
120
        wxString str;
 
121
        unsigned int col;
 
122
 
 
123
        if (GetNumberCols() == 0)
 
124
                return str;
 
125
 
 
126
        for (col = 0 ; col < cols.Count() ; col++)
 
127
        {
 
128
                if (col > 0)
 
129
                        str.Append(settings->GetCopyColSeparator());
 
130
 
 
131
                wxString text = GetCellValue(row, cols[col]);
100
132
 
101
133
                bool needQuote  = false;
102
134
                if (settings->GetCopyQuoting() == 1)
103
135
                {
104
 
            needQuote = IsColText(cols[col]);
 
136
                        needQuote = IsColText(cols[col]);
105
137
                }
106
138
                else if (settings->GetCopyQuoting() == 2)
107
139
                        /* Quote everything */
108
140
                        needQuote = true;
109
141
 
110
142
                if (needQuote)
111
 
            str.Append(settings->GetCopyQuoteChar());
112
 
        str.Append(text);
113
 
        if (needQuote)
114
 
            str.Append(settings->GetCopyQuoteChar());
115
 
    }    
116
 
    return str;
 
143
                        str.Append(settings->GetCopyQuoteChar());
 
144
                str.Append(text);
 
145
                if (needQuote)
 
146
                        str.Append(settings->GetCopyQuoteChar());
 
147
        }
 
148
        return str;
117
149
}
118
150
 
119
151
int ctlSQLGrid::Copy()
120
152
{
121
 
    wxString str;
122
 
    int copied = 0;
123
 
    size_t i;
124
 
 
125
 
    if (GetSelectedRows().GetCount()) 
126
 
        {
127
 
        wxArrayInt rows = GetSelectedRows();
128
 
 
129
 
        for (i=0 ; i < rows.GetCount() ; i++)
130
 
        {
131
 
            str.Append(GetExportLine(rows.Item(i)));
132
 
    
133
 
            if (rows.GetCount() > 1)
134
 
                str.Append(END_OF_LINE);
135
 
        }
136
 
 
137
 
        copied = rows.GetCount();
138
 
    }
139
 
    else if (GetSelectedCols().GetCount()) 
140
 
        {
141
 
        wxArrayInt cols = GetSelectedCols();
142
 
        size_t numRows = GetNumberRows();
143
 
 
144
 
        for (i=0 ; i < numRows ; i++)
145
 
        {
146
 
            str.Append(GetExportLine(i, cols));
147
 
    
148
 
            if (numRows > 1)
149
 
                str.Append(END_OF_LINE);
150
 
        }
151
 
 
152
 
        copied = numRows;
153
 
    }
154
 
    else if (GetSelectionBlockTopLeft().GetCount() > 0 &&
155
 
        GetSelectionBlockBottomRight().GetCount() > 0) 
156
 
        {
157
 
        unsigned int x1, x2, y1, y2;
158
 
 
159
 
        x1 = GetSelectionBlockTopLeft()[0].GetCol();
160
 
        x2 = GetSelectionBlockBottomRight()[0].GetCol();
161
 
        y1 = GetSelectionBlockTopLeft()[0].GetRow();
162
 
        y2 = GetSelectionBlockBottomRight()[0].GetRow();
163
 
 
164
 
        for (i = y1; i <= y2; i++) 
165
 
                {
166
 
            str.Append(GetExportLine(i, x1, x2));
167
 
 
168
 
            if (y2 > y1)
169
 
                str.Append(END_OF_LINE);
170
 
        }
171
 
 
172
 
        copied = y2 - y1 + 1;
173
 
    }
174
 
    else 
175
 
        {
176
 
        int row, col;
177
 
 
178
 
        row = GetGridCursorRow();
179
 
        col = GetGridCursorCol();
180
 
 
181
 
        str.Append(GetExportLine(row, col, col));
182
 
        copied = 1;
183
 
    }
184
 
 
185
 
    if (copied && wxTheClipboard->Open())
186
 
    {
187
 
        wxTheClipboard->SetData(new wxTextDataObject(str));
188
 
        wxTheClipboard->Close();
189
 
    }
190
 
    else {
191
 
        copied = 0;
192
 
    }
193
 
 
194
 
    return copied;
 
153
        wxString str;
 
154
        int copied = 0;
 
155
        size_t i;
 
156
 
 
157
        if (GetSelectedRows().GetCount())
 
158
        {
 
159
                wxArrayInt rows = GetSelectedRows();
 
160
 
 
161
                for (i = 0 ; i < rows.GetCount() ; i++)
 
162
                {
 
163
                        str.Append(GetExportLine(rows.Item(i)));
 
164
 
 
165
                        if (rows.GetCount() > 1)
 
166
                                str.Append(END_OF_LINE);
 
167
                }
 
168
 
 
169
                copied = rows.GetCount();
 
170
        }
 
171
        else if (GetSelectedCols().GetCount())
 
172
        {
 
173
                wxArrayInt cols = GetSelectedCols();
 
174
                size_t numRows = GetNumberRows();
 
175
 
 
176
                for (i = 0 ; i < numRows ; i++)
 
177
                {
 
178
                        str.Append(GetExportLine(i, cols));
 
179
 
 
180
                        if (numRows > 1)
 
181
                                str.Append(END_OF_LINE);
 
182
                }
 
183
 
 
184
                copied = numRows;
 
185
        }
 
186
        else if (GetSelectionBlockTopLeft().GetCount() > 0 &&
 
187
                 GetSelectionBlockBottomRight().GetCount() > 0)
 
188
        {
 
189
                unsigned int x1, x2, y1, y2;
 
190
 
 
191
                x1 = GetSelectionBlockTopLeft()[0].GetCol();
 
192
                x2 = GetSelectionBlockBottomRight()[0].GetCol();
 
193
                y1 = GetSelectionBlockTopLeft()[0].GetRow();
 
194
                y2 = GetSelectionBlockBottomRight()[0].GetRow();
 
195
 
 
196
                for (i = y1; i <= y2; i++)
 
197
                {
 
198
                        str.Append(GetExportLine(i, x1, x2));
 
199
 
 
200
                        if (y2 > y1)
 
201
                                str.Append(END_OF_LINE);
 
202
                }
 
203
 
 
204
                copied = y2 - y1 + 1;
 
205
        }
 
206
        else
 
207
        {
 
208
                int row, col;
 
209
 
 
210
                row = GetGridCursorRow();
 
211
                col = GetGridCursorCol();
 
212
 
 
213
                str.Append(GetExportLine(row, col, col));
 
214
                copied = 1;
 
215
        }
 
216
 
 
217
        if (copied && wxTheClipboard->Open())
 
218
        {
 
219
                wxTheClipboard->SetData(new wxTextDataObject(str));
 
220
                wxTheClipboard->Close();
 
221
        }
 
222
        else
 
223
        {
 
224
                copied = 0;
 
225
        }
 
226
 
 
227
        return copied;
195
228
}
196
229
 
197
 
#define EXTRAEXTENT_HEIGHT 6
198
 
#define EXTRAEXTENT_WIDTH  6
199
 
 
200
 
void ctlSQLGrid::OnLabelDoubleClick(wxGridEvent& event)
 
230
void ctlSQLGrid::OnLabelDoubleClick(wxGridEvent &event)
201
231
{
202
 
    int maxHeight, maxWidth;
203
 
    GetClientSize(&maxWidth, &maxHeight);
204
 
    int row = event.GetRow();
205
 
    int col = event.GetCol();
206
 
 
207
 
    int extent, extentWant=0;
208
 
 
209
 
    if (row >= 0)
210
 
    {
211
 
        for (col=0 ; col < GetNumberCols() ; col++)
212
 
        {
213
 
            extent = GetBestSize(row, col).GetHeight();
214
 
            if (extent > extentWant)
215
 
                extentWant = extent;
216
 
        }
217
 
 
218
 
        extentWant += EXTRAEXTENT_HEIGHT;
219
 
        extentWant = wxMax(extentWant, GetRowMinimalAcceptableHeight());
220
 
        extentWant = wxMin(extentWant, maxHeight*3/4);
221
 
        int currentHeight = GetRowHeight(row);
222
 
            
223
 
        if (currentHeight >= maxHeight*3/4 || currentHeight == extentWant)
224
 
            extentWant = GetRowMinimalAcceptableHeight();
225
 
        else if (currentHeight < maxHeight/4)
226
 
            extentWant = wxMin(maxHeight/4, extentWant);
227
 
        else if (currentHeight < maxHeight/2)
228
 
            extentWant = wxMin(maxHeight/2, extentWant);
229
 
        else if (currentHeight < maxHeight*3/4)
230
 
            extentWant = wxMin(maxHeight*3/4, extentWant);
231
 
 
232
 
        if (extentWant != currentHeight)
233
 
        {
234
 
            BeginBatch();
235
 
            if(IsCellEditControlShown())
236
 
            {
237
 
                HideCellEditControl();
238
 
                SaveEditControlValue();
239
 
            }
240
 
 
241
 
            SetRowHeight(row, extentWant);
242
 
            EndBatch();
243
 
        }
244
 
    }
245
 
    else if (col >= 0)
246
 
    {
247
 
        for (row=0 ; row < GetNumberRows() ; row++)
248
 
        {
249
 
            if (CheckRowPresent(row))
250
 
            {
251
 
                extent = GetBestSize(row, col).GetWidth();
252
 
                if (extent > extentWant)
253
 
                    extentWant=extent;
254
 
            }
255
 
        }
256
 
 
257
 
        extentWant += EXTRAEXTENT_WIDTH;
258
 
        extentWant = wxMax(extentWant, GetColMinimalAcceptableWidth());
259
 
        extentWant = wxMin(extentWant, maxWidth*3/4);
260
 
        int currentWidth=GetColumnWidth(col);
261
 
            
262
 
        if (currentWidth >= maxWidth*3/4 || currentWidth == extentWant)
263
 
            extentWant = GetColMinimalAcceptableWidth();
264
 
        else if (currentWidth < maxWidth/4)
265
 
            extentWant = wxMin(maxWidth/4, extentWant);
266
 
        else if (currentWidth < maxWidth/2)
267
 
            extentWant = wxMin(maxWidth/2, extentWant);
268
 
        else if (currentWidth < maxWidth*3/4)
269
 
            extentWant = wxMin(maxWidth*3/4, extentWant);
270
 
 
271
 
        if (extentWant != currentWidth)
272
 
        {
273
 
            BeginBatch();
274
 
            if(IsCellEditControlShown())
275
 
            {
276
 
                HideCellEditControl();
277
 
                SaveEditControlValue();
278
 
            }
279
 
            SetColumnWidth(col, extentWant);
280
 
            EndBatch();
281
 
        }
282
 
    }
 
232
        int maxHeight, maxWidth;
 
233
        GetClientSize(&maxWidth, &maxHeight);
 
234
        int row = event.GetRow();
 
235
        int col = event.GetCol();
 
236
 
 
237
        int extent, extentWant = 0;
 
238
 
 
239
        if (row >= 0)
 
240
        {
 
241
                for (col = 0 ; col < GetNumberCols() ; col++)
 
242
                {
 
243
                        extent = GetBestSize(row, col).GetHeight();
 
244
                        if (extent > extentWant)
 
245
                                extentWant = extent;
 
246
                }
 
247
 
 
248
                extentWant += EXTRAEXTENT_HEIGHT;
 
249
                extentWant = wxMax(extentWant, GetRowMinimalAcceptableHeight());
 
250
                extentWant = wxMin(extentWant, maxHeight * 3 / 4);
 
251
                int currentHeight = GetRowHeight(row);
 
252
 
 
253
                if (currentHeight >= maxHeight * 3 / 4 || currentHeight == extentWant)
 
254
                        extentWant = GetRowMinimalAcceptableHeight();
 
255
                else if (currentHeight < maxHeight / 4)
 
256
                        extentWant = wxMin(maxHeight / 4, extentWant);
 
257
                else if (currentHeight < maxHeight / 2)
 
258
                        extentWant = wxMin(maxHeight / 2, extentWant);
 
259
                else if (currentHeight < maxHeight * 3 / 4)
 
260
                        extentWant = wxMin(maxHeight * 3 / 4, extentWant);
 
261
 
 
262
                if (extentWant != currentHeight)
 
263
                {
 
264
                        BeginBatch();
 
265
                        if(IsCellEditControlShown())
 
266
                        {
 
267
                                HideCellEditControl();
 
268
                                SaveEditControlValue();
 
269
                        }
 
270
 
 
271
                        SetRowHeight(row, extentWant);
 
272
                        EndBatch();
 
273
                }
 
274
        }
 
275
        else if (col >= 0)
 
276
        {
 
277
                for (row = 0 ; row < GetNumberRows() ; row++)
 
278
                {
 
279
                        if (CheckRowPresent(row))
 
280
                        {
 
281
                                extent = GetBestSize(row, col).GetWidth();
 
282
                                if (extent > extentWant)
 
283
                                        extentWant = extent;
 
284
                        }
 
285
                }
 
286
 
 
287
                extentWant += EXTRAEXTENT_WIDTH;
 
288
                extentWant = wxMax(extentWant, GetColMinimalAcceptableWidth());
 
289
                extentWant = wxMin(extentWant, maxWidth * 3 / 4);
 
290
                int currentWidth = GetColumnWidth(col);
 
291
 
 
292
                if (currentWidth >= maxWidth * 3 / 4 || currentWidth == extentWant)
 
293
                        extentWant = GetColMinimalAcceptableWidth();
 
294
                else if (currentWidth < maxWidth / 4)
 
295
                        extentWant = wxMin(maxWidth / 4, extentWant);
 
296
                else if (currentWidth < maxWidth / 2)
 
297
                        extentWant = wxMin(maxWidth / 2, extentWant);
 
298
                else if (currentWidth < maxWidth * 3 / 4)
 
299
                        extentWant = wxMin(maxWidth * 3 / 4, extentWant);
 
300
 
 
301
                if (extentWant != currentWidth)
 
302
                {
 
303
                        BeginBatch();
 
304
                        if(IsCellEditControlShown())
 
305
                        {
 
306
                                HideCellEditControl();
 
307
                                SaveEditControlValue();
 
308
                        }
 
309
                        SetColumnWidth(col, extentWant);
 
310
                        EndBatch();
 
311
                }
 
312
        }
283
313
}
284
314
 
285
315
wxSize ctlSQLGrid::GetBestSize(int row, int col)
286
316
{
287
 
    wxSize size;
288
 
 
289
 
    wxGridCellAttr* attr = GetCellAttr(row, col);
290
 
    wxGridCellRenderer* renderer = attr->GetRenderer(this, row, col);
291
 
    if ( renderer )
292
 
    {
293
 
        wxClientDC dc(GetGridWindow());
294
 
        size = renderer->GetBestSize(*this, *attr, dc, row, col);
295
 
        renderer->DecRef();
296
 
    }
297
 
 
298
 
    attr->DecRef();
299
 
 
300
 
    return size;
 
317
        wxSize size;
 
318
 
 
319
        wxGridCellAttr *attr = GetCellAttr(row, col);
 
320
        wxGridCellRenderer *renderer = attr->GetRenderer(this, row, col);
 
321
        if ( renderer )
 
322
        {
 
323
                wxClientDC dc(GetGridWindow());
 
324
                size = renderer->GetBestSize(*this, *attr, dc, row, col);
 
325
                renderer->DecRef();
 
326
        }
 
327
 
 
328
        attr->DecRef();
 
329
 
 
330
        return size;
301
331
}