1
//////////////////////////////////////////////////////////////////////////
3
// pgAdmin III - PostgreSQL Tools
4
// RCS-ID: $Id: dlgClasses.cpp,v 1.17 2004/10/22 14:25:16 andreas Exp $
5
// Copyright (C) 2002 - 2004, The pgAdmin Development Team
6
// This software is released under the Artistic Licence
8
// dlgClasses.cpp - Some dialogue base classes
10
//////////////////////////////////////////////////////////////////////////
14
#include <wx/settings.h>
15
#include <wx/xrc/xmlres.h>
20
#include "wx/process.h"
28
BEGIN_EVENT_TABLE(pgDialog, wxDialog)
29
EVT_BUTTON (wxID_CANCEL, pgDialog::OnCancel)
30
EVT_CLOSE( pgDialog::OnClose)
35
void pgDialog::PostCreation()
37
wxWindow *statusBarContainer=FindWindow(wxT("unkStatusBar_container"));
39
if (statusBarContainer)
41
statusBar = new wxStatusBar(this, -1, wxST_SIZEGRIP);
42
wxXmlResource::Get()->AttachUnknownControl(wxT("unkStatusBar"), statusBar);
44
if (GetWindowStyle() & wxTHICK_FRAME) // is designed with sizers; don't change
50
wxSize size = btnCancel->GetSize();
51
wxPoint pos = btnCancel->GetPosition();
52
int height = pos.y + size.GetHeight() + ConvertDialogToPixels(wxSize(0,3)).y;
54
height += statusBar->GetSize().GetHeight();
56
int right = pos.x + ConvertDialogToPixels(wxSize(50,0)).x - size.GetWidth();
57
btnCancel->Move(right, pos.y);
61
size = btnOK->GetSize();
62
right -= size.GetWidth() + ConvertDialogToPixels(wxSize(3,0)).x;
63
btnOK->Move(right, pos.y);
67
size = btnApply->GetSize();
68
right -= size.GetWidth() - ConvertDialogToPixels(wxSize(3,0)).x;
69
btnApply->Move(right, pos.y);
74
GetClientSize(&w, &h);
76
SetSize(size.GetWidth(), size.GetHeight() + height - h);
80
void pgDialog::RestorePosition(int defaultX, int defaultY, int defaultW, int defaultH, int minW, int minH)
82
wxPoint pos(settings->Read(dlgName, wxPoint(defaultX, defaultY)));
87
size = settings->Read(dlgName, wxSize(defaultW, defaultH));
89
bool posDefault = (pos.x == -1 && pos.y == -1);
91
CheckOnScreen(pos, size, minW, minH);
92
SetSize(pos.x, pos.y, size.x, size.y);
97
void pgDialog::SavePosition()
99
settings->Write(dlgName, GetSize(), GetPosition());
102
void pgDialog::LoadResource(wxWindow *parent, const wxChar *name)
106
wxXmlResource::Get()->LoadDialog(this, parent, dlgName);
112
void pgDialog::OnClose(wxCloseEvent& event)
121
void pgDialog::OnCancel(wxCommandEvent& ev)
130
///////////////////////////////////////////////////////////////////////////////////////
133
BEGIN_EVENT_TABLE(pgFrame, wxFrame)
134
EVT_MENU(MNU_EXIT, pgFrame::OnExit)
135
EVT_MENU(MNU_RECENT+1, pgFrame::OnRecent)
136
EVT_MENU(MNU_RECENT+2, pgFrame::OnRecent)
137
EVT_MENU(MNU_RECENT+3, pgFrame::OnRecent)
138
EVT_MENU(MNU_RECENT+4, pgFrame::OnRecent)
139
EVT_MENU(MNU_RECENT+5, pgFrame::OnRecent)
140
EVT_MENU(MNU_RECENT+6, pgFrame::OnRecent)
141
EVT_MENU(MNU_RECENT+7, pgFrame::OnRecent)
142
EVT_MENU(MNU_RECENT+8, pgFrame::OnRecent)
143
EVT_MENU(MNU_RECENT+9, pgFrame::OnRecent)
144
EVT_MENU(MNU_BUGREPORT, pgFrame::OnBugreport)
145
EVT_MENU(MNU_HELP, pgFrame::OnHelp)
146
EVT_MENU(MNU_ABOUT, pgFrame::OnAbout)
148
EVT_KEY_DOWN( pgFrame::OnKeyDown)
155
if (!dlgName.IsEmpty())
160
void pgFrame::OnKeyDown(wxKeyEvent& event)
162
event.m_metaDown=false;
167
void pgFrame::OnExit(wxCommandEvent& event)
173
void pgFrame::OnHelp(wxCommandEvent& WXUNUSED(event))
175
wxString page=GetHelpPage();
176
DisplayHelp(this, page);
180
void pgFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
182
frmAbout *winAbout = new frmAbout(this);
183
winAbout->Show(TRUE);
187
void pgFrame::OnBugreport(wxCommandEvent& event)
189
DisplayHelp(this, wxT("bugreport"));
193
void pgFrame::OnRecent(wxCommandEvent& event)
195
int fileNo=event.GetId() - MNU_RECENT;
196
lastPath = settings->Read(recentKey + wxString::Format(wxT("/%d"), fileNo), wxT(""));
198
if (!lastPath.IsNull())
201
dirsep = lastPath.Find(wxFILE_SEP_PATH, true);
202
lastDir = lastPath.Mid(0, dirsep);
203
lastFilename = lastPath.Mid(dirsep+1);
210
void pgFrame::UpdateRecentFiles()
215
if (recentKey.IsEmpty())
216
recentKey = dlgName + wxT("/RecentFiles");
218
wxString lastFiles[10]; // 0 will be unused for convenience
220
int recentIndex=maxFiles;
222
for (i=1 ; i <= maxFiles ; i++)
224
lastFiles[i] = settings->Read(recentKey + wxString::Format(wxT("/%d"), i), wxT(""));
225
if (!lastPath.IsNull() && lastPath.IsSameAs(lastFiles[i], wxARE_FILENAMES_CASE_SENSITIVE))
228
while (i <= maxFiles)
229
lastFiles[i++] = wxT("");
231
if (recentIndex > 1 && !lastPath.IsNull())
233
for (i=recentIndex ; i > 1 ; i--)
234
lastFiles[i] = lastFiles[i-1];
235
lastFiles[1] = lastPath;
238
i=recentFileMenu->GetMenuItemCount();
241
wxMenuItem *item = recentFileMenu->Remove(MNU_RECENT+i);
247
for (i=1 ; i <= maxFiles ; i++)
249
settings->Write(recentKey + wxString::Format(wxT("/%d"), i), lastFiles[i]);
251
if (!lastFiles[i].IsNull())
252
recentFileMenu->Append(MNU_RECENT+i, wxT("&") + wxString::Format(wxT("%d"), i) + wxT(" ") + lastFiles[i]);
257
void pgFrame::RestorePosition(int defaultX, int defaultY, int defaultW, int defaultH, int minW, int minH)
259
wxPoint pos(settings->Read(dlgName, wxPoint(defaultX, defaultY)));
264
size = settings->Read(dlgName, wxSize(defaultW, defaultH));
266
bool posDefault = (pos.x == -1 && pos.y == -1);
268
CheckOnScreen(pos, size, minW, minH);
269
SetSize(pos.x, pos.y, size.x, size.y);
275
void pgFrame::SavePosition()
277
settings->Write(dlgName, GetSize(), GetPosition());
282
//////////////////////////////////////////////////////
284
BEGIN_EVENT_TABLE(DialogWithHelp, pgDialog)
285
EVT_MENU(MNU_HELP, DialogWithHelp::OnHelp)
286
EVT_BUTTON(wxID_HELP, DialogWithHelp::OnHelp)
290
DialogWithHelp::DialogWithHelp(frmMain *frame) : pgDialog()
294
wxAcceleratorEntry entries[2];
295
entries[0].Set(wxACCEL_NORMAL, WXK_F1, MNU_HELP);
296
// this is for GTK because Meta (usually Numlock) is interpreted like Alt
297
// there are too many controls to reset m_Meta in all of them
298
entries[1].Set(wxACCEL_ALT, WXK_F1, MNU_HELP);
299
wxAcceleratorTable accel(2, entries);
301
SetAcceleratorTable(accel);
305
void DialogWithHelp::OnHelp(wxCommandEvent& ev)
307
wxString page=GetHelpPage();
310
DisplaySqlHelp(this, page);
315
////////////////////////////////////////////////////////////////77
318
BEGIN_EVENT_TABLE(ExecutionDialog, DialogWithHelp)
319
EVT_BUTTON (wxID_OK, ExecutionDialog::OnOK)
320
EVT_BUTTON (wxID_CANCEL, ExecutionDialog::OnCancel)
321
EVT_CLOSE( ExecutionDialog::OnClose)
325
ExecutionDialog::ExecutionDialog(frmMain *frame, pgObject *_object) : DialogWithHelp(frame)
333
void ExecutionDialog::OnClose(wxCloseEvent& event)
343
void ExecutionDialog::OnCancel(wxCommandEvent& ev)
347
btnCancel->Disable();
351
wxButton *btn=btnApply;
365
void ExecutionDialog::Abort()
369
if (thread->IsRunning())
377
void ExecutionDialog::OnOK(wxCommandEvent& ev)
381
wxString sql=GetSql();
387
thread=new pgQueryThread(object->GetConnection(), sql);
388
if (thread->Create() != wxTHREAD_NO_ERROR)
394
wxLongLong startTime=wxGetLocalTimeMillis();
396
wxNotebook *nb=CTRL_NOTEBOOK("nbNotebook");
398
nb->SetSelection(nb->GetPageCount()-1);
400
while (thread && thread->IsRunning())
403
// here could be the animation
405
txtMessages->AppendText(thread->GetMessagesAndClear());
411
bool isOk = (thread->ReturnCode() == PGRES_COMMAND_OK || thread->ReturnCode() == PGRES_TUPLES_OK);
414
txtMessages->AppendText(thread->GetMessagesAndClear());
416
if (thread->DataSet() != NULL)
417
wxLogDebug(wxString::Format(_("%d rows."), thread->DataSet()->NumRows()));
422
txtMessages->AppendText(_("Total query runtime: ")
423
+ (wxGetLocalTimeMillis()-startTime).ToString() + wxT(" ms."));
425
btnOK->SetLabel(_("Done"));
426
btnCancel->Disable();
431
txtMessages->AppendText(object->GetConnection()->GetLastError());
437
txtMessages->AppendText(_("\nCancelled.\n"));
440
wxButton *btn=btnApply;
453
#define TIMER_ID 4442
455
BEGIN_EVENT_TABLE(ExternProcessDialog, DialogWithHelp)
456
EVT_BUTTON(wxID_OK, ExternProcessDialog::OnOK)
457
EVT_BUTTON(wxID_CANCEL, ExternProcessDialog::OnCancel)
458
EVT_CLOSE( ExternProcessDialog::OnClose)
459
EVT_END_PROCESS(-1, ExternProcessDialog::OnEndProcess)
460
EVT_TIMER(TIMER_ID, ExternProcessDialog::OnPollProcess)
465
ExternProcessDialog::ExternProcessDialog(frmMain *frame) : DialogWithHelp(frame)
471
timer=new wxTimer(this, TIMER_ID);
476
ExternProcessDialog::~ExternProcessDialog()
483
void ExternProcessDialog::OnOK(wxCommandEvent& ev)
490
pgDialog::OnCancel(ev);
495
bool ExternProcessDialog::Execute(int step, bool finalStep)
501
txtMessages->AppendText(GetDisplayCmd(step) + END_OF_LINE);
506
process = new wxProcess(this);
508
for (i=0 ; i < environment.GetCount() ; i++)
510
wxString str=environment.Item(i);
511
wxSetEnv(str.BeforeFirst('='), str.AfterFirst('='));
514
pid = wxExecute(GetCmd(step), wxEXEC_ASYNC, process);
517
wxNotebook *nb=CTRL_NOTEBOOK("nbNotebook");
519
nb->SetSelection(nb->GetPageCount()-1);
535
void ExternProcessDialog::OnCancel(wxCommandEvent &ev)
539
btnCancel->Disable();
543
pgDialog::OnCancel(ev);
547
void ExternProcessDialog::OnClose(wxCloseEvent &ev)
549
btnCancel->Disable();
551
pgDialog::OnClose(ev);
555
void ExternProcessDialog::OnPollProcess(wxTimerEvent& event)
561
void ExternProcessDialog::checkStreams()
563
if (txtMessages && process)
565
if (process->IsErrorAvailable())
566
readStream(process->GetErrorStream());
567
if (process->IsInputAvailable())
568
readStream(process->GetInputStream());
573
void ExternProcessDialog::readStream(wxInputStream *input)
579
while (size && !input->Eof())
581
input->Read(buffer, sizeof(buffer)-1);
582
size=input->LastRead();
586
txtMessages->AppendText(wxString::FromAscii(buffer));
593
void ExternProcessDialog::OnEndProcess(wxProcessEvent &ev)
598
btnOK->SetLabel(_("Done"));
606
txtMessages->AppendText(END_OF_LINE
607
+ wxString::Format(_("Process returned exit code %d."), ev.GetExitCode())
613
wxKill(ev.GetPid(), wxSIGTERM);
623
void ExternProcessDialog::Abort()
629
wxProcess *tmpProcess=process;
631
wxKill(pid, wxSIGTERM);