2008-04-24 12:49:12 +00:00
|
|
|
/***************************************************************************
|
|
|
|
|
|
|
|
CDialog.cpp
|
|
|
|
|
2011-12-31 02:39:20 +00:00
|
|
|
(c) 2000-2012 Benoît Minisini <gambas@users.sourceforge.net>
|
2008-04-24 12:49:12 +00:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
2009-08-17 10:41:51 +00:00
|
|
|
the Free Software Foundation; either version 2, or (at your option)
|
2008-04-24 12:49:12 +00:00
|
|
|
any later version.
|
|
|
|
|
|
|
|
This program 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 General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
2011-06-03 00:51:09 +00:00
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
2011-12-31 02:39:20 +00:00
|
|
|
MA 02110-1301, USA.
|
2008-04-24 12:49:12 +00:00
|
|
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#define __CDIALOG_CPP
|
|
|
|
|
|
|
|
#include <qcolor.h>
|
|
|
|
#include <qapplication.h>
|
|
|
|
#include <qcolordialog.h>
|
|
|
|
#include <qfontdialog.h>
|
2008-04-29 13:40:55 +00:00
|
|
|
#include <QFileDialog>
|
2008-04-24 12:49:12 +00:00
|
|
|
|
|
|
|
#include "gambas.h"
|
|
|
|
#include "CColor.h"
|
|
|
|
#include "CFont.h"
|
|
|
|
|
|
|
|
#include "CDialog.h"
|
|
|
|
|
|
|
|
static QString dialog_title;
|
|
|
|
static GB_ARRAY dialog_filter = NULL;
|
|
|
|
static QString dialog_path;
|
|
|
|
static GB_ARRAY dialog_paths = NULL;
|
2009-08-17 07:42:06 +00:00
|
|
|
static CFONT *dialog_font = NULL;
|
2012-01-31 10:25:32 +00:00
|
|
|
static bool dialog_show_hidden = false;
|
2008-04-24 12:49:12 +00:00
|
|
|
|
|
|
|
static unsigned int dialog_color = 0;
|
|
|
|
|
|
|
|
|
|
|
|
static QString get_filter(void)
|
|
|
|
{
|
|
|
|
QString s;
|
|
|
|
int i;
|
|
|
|
QString filter;
|
|
|
|
|
|
|
|
if (dialog_filter)
|
|
|
|
{
|
|
|
|
for (i = 0; i < (GB.Array.Count(dialog_filter) / 2); i++)
|
|
|
|
{
|
|
|
|
filter = TO_QSTRING(*((char **)GB.Array.Get(dialog_filter, i * 2)));
|
|
|
|
if (filter == "*")
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (s.length())
|
|
|
|
s += ";;";
|
|
|
|
s += TO_QSTRING(*((char **)GB.Array.Get(dialog_filter, i * 2 + 1)));
|
2009-11-30 20:56:56 +00:00
|
|
|
s += " (" + filter.replace(";", " ") + ")";
|
2008-04-24 12:49:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
s += ";;";
|
|
|
|
s += TO_QSTRING(GB.Translate("All files"));
|
|
|
|
s += " (*)";
|
|
|
|
}
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
BEGIN_METHOD_VOID(CDIALOG_exit)
|
|
|
|
|
2009-08-17 07:42:06 +00:00
|
|
|
GB.StoreObject(NULL, POINTER(&dialog_filter));
|
|
|
|
GB.StoreObject(NULL, POINTER(&dialog_paths));
|
|
|
|
GB.StoreObject(NULL, POINTER(&dialog_font));
|
2008-04-24 12:49:12 +00:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
|
2012-01-31 10:25:32 +00:00
|
|
|
BEGIN_PROPERTY(Dialog_Title)
|
2008-04-24 12:49:12 +00:00
|
|
|
|
|
|
|
if (READ_PROPERTY)
|
|
|
|
GB.ReturnNewZeroString(TO_UTF8(dialog_title));
|
|
|
|
else
|
|
|
|
dialog_title = QSTRING_PROP();
|
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
2012-01-31 10:25:32 +00:00
|
|
|
BEGIN_PROPERTY(Dialog_Filter)
|
2008-04-24 12:49:12 +00:00
|
|
|
|
|
|
|
if (READ_PROPERTY)
|
|
|
|
GB.ReturnObject(dialog_filter);
|
|
|
|
else
|
|
|
|
GB.StoreObject(PROP(GB_OBJECT), POINTER(&dialog_filter));
|
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
2012-01-31 10:25:32 +00:00
|
|
|
BEGIN_PROPERTY(Dialog_ShowHidden)
|
|
|
|
|
|
|
|
if (READ_PROPERTY)
|
|
|
|
GB.ReturnBoolean(dialog_show_hidden);
|
|
|
|
else
|
|
|
|
dialog_show_hidden = VPROP(GB_BOOLEAN);
|
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN_PROPERTY(Dialog_Path)
|
2008-04-24 12:49:12 +00:00
|
|
|
|
|
|
|
if (READ_PROPERTY)
|
|
|
|
GB.ReturnNewZeroString(TO_UTF8(dialog_path));
|
|
|
|
else
|
|
|
|
dialog_path = QSTRING_PROP();
|
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
2012-01-31 10:25:32 +00:00
|
|
|
BEGIN_PROPERTY(Dialog_Paths)
|
2008-04-24 12:49:12 +00:00
|
|
|
|
|
|
|
GB.ReturnObject(dialog_paths);
|
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
2012-01-31 10:25:32 +00:00
|
|
|
BEGIN_PROPERTY(Dialog_Font)
|
2008-04-24 12:49:12 +00:00
|
|
|
|
|
|
|
if (READ_PROPERTY)
|
2009-08-17 07:42:06 +00:00
|
|
|
GB.ReturnObject(dialog_font);
|
2008-04-24 12:49:12 +00:00
|
|
|
else
|
|
|
|
{
|
2009-08-17 07:42:06 +00:00
|
|
|
CFONT *font = (CFONT *)VPROP(GB_OBJECT);
|
|
|
|
|
|
|
|
GB.StoreObject(NULL, POINTER(&dialog_font));
|
|
|
|
if (font)
|
2010-03-28 21:58:45 +00:00
|
|
|
{
|
2009-08-17 07:42:06 +00:00
|
|
|
dialog_font = CFONT_create(*font->font);
|
2010-03-28 21:58:45 +00:00
|
|
|
GB.Ref(dialog_font);
|
|
|
|
}
|
2008-04-24 12:49:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
2012-01-31 10:25:32 +00:00
|
|
|
BEGIN_PROPERTY(Dialog_Color)
|
2008-04-24 12:49:12 +00:00
|
|
|
|
|
|
|
if (READ_PROPERTY)
|
|
|
|
GB.ReturnInteger(dialog_color);
|
|
|
|
else
|
|
|
|
dialog_color = VPROP(GB_INTEGER);
|
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
2012-01-31 10:25:32 +00:00
|
|
|
static QString my_getOpenFileName()
|
|
|
|
{
|
|
|
|
QFileDialog dialog(qApp->activeWindow(), dialog_title, dialog_path, get_filter());
|
|
|
|
|
|
|
|
dialog.setMode(QFileDialog::ExistingFile);
|
|
|
|
dialog.setOption(QFileDialog::DontUseNativeDialog);
|
|
|
|
dialog.setFilter(dialog_show_hidden ? (dialog.filter() | QDir::Hidden | QDir::System) : (dialog.filter() & ~(QDir::Hidden | QDir::System)));
|
|
|
|
|
|
|
|
if (dialog.exec() == QDialog::Accepted)
|
|
|
|
return dialog.selectedFiles().value(0);
|
|
|
|
else
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
|
|
|
static QStringList my_getOpenFileNames()
|
|
|
|
{
|
|
|
|
QFileDialog dialog(qApp->activeWindow(), dialog_title, dialog_path, get_filter());
|
|
|
|
|
|
|
|
dialog.setMode(QFileDialog::ExistingFiles);
|
|
|
|
dialog.setOption(QFileDialog::DontUseNativeDialog);
|
|
|
|
dialog.setFilter(dialog_show_hidden ? (dialog.filter() | QDir::Hidden | QDir::System) : (dialog.filter() & ~(QDir::Hidden | QDir::System)));
|
|
|
|
|
|
|
|
if (dialog.exec() == QDialog::Accepted)
|
|
|
|
return dialog.selectedFiles();
|
|
|
|
else
|
|
|
|
return QStringList();
|
|
|
|
}
|
|
|
|
|
|
|
|
static QString my_getSaveFileName()
|
|
|
|
{
|
|
|
|
QFileDialog dialog(qApp->activeWindow(), dialog_title, dialog_path, get_filter());
|
|
|
|
|
|
|
|
dialog.setAcceptMode(QFileDialog::AcceptSave);
|
|
|
|
dialog.setMode(QFileDialog::AnyFile);
|
|
|
|
dialog.setOption(QFileDialog::DontUseNativeDialog);
|
|
|
|
dialog.setFilter(dialog_show_hidden ? (dialog.filter() | QDir::Hidden | QDir::System) : (dialog.filter() & ~(QDir::Hidden | QDir::System)));
|
|
|
|
|
|
|
|
if (dialog.exec() == QDialog::Accepted)
|
|
|
|
return dialog.selectedFiles().value(0);
|
|
|
|
else
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
|
|
|
static QString my_getExistingDirectory()
|
|
|
|
{
|
|
|
|
QFileDialog dialog(qApp->activeWindow(), dialog_title, dialog_path);
|
|
|
|
|
|
|
|
dialog.setMode(QFileDialog::Directory);
|
|
|
|
dialog.setOption(QFileDialog::DontUseNativeDialog);
|
|
|
|
|
|
|
|
if (dialog.exec() == QDialog::Accepted)
|
|
|
|
return dialog.selectedFiles().value(0);
|
|
|
|
else
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN_METHOD(Dialog_OpenFile, GB_BOOLEAN multi)
|
2008-04-24 12:49:12 +00:00
|
|
|
|
|
|
|
if (!VARGOPT(multi, false))
|
|
|
|
{
|
|
|
|
QString file;
|
|
|
|
|
2012-01-31 10:25:32 +00:00
|
|
|
file = my_getOpenFileName();
|
2008-04-24 12:49:12 +00:00
|
|
|
|
|
|
|
if (file.isNull())
|
|
|
|
GB.ReturnBoolean(true);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dialog_path = file;
|
|
|
|
GB.ReturnBoolean(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
QStringList files;
|
|
|
|
GB_ARRAY list;
|
|
|
|
GB_OBJECT ob;
|
2008-04-29 13:40:55 +00:00
|
|
|
int i;
|
2008-04-24 12:49:12 +00:00
|
|
|
|
2012-01-31 10:25:32 +00:00
|
|
|
files = my_getOpenFileNames();
|
2008-04-24 12:49:12 +00:00
|
|
|
|
|
|
|
if (files.isEmpty())
|
|
|
|
{
|
|
|
|
GB.StoreObject(NULL, POINTER(&dialog_paths));
|
|
|
|
GB.ReturnBoolean(true);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
GB.Array.New(&list, GB_T_STRING, files.count());
|
|
|
|
ob.value = list;
|
|
|
|
GB.StoreObject(&ob, POINTER(&dialog_paths));
|
|
|
|
|
|
|
|
for (i = 0; i < files.count(); i++)
|
2010-06-04 23:48:53 +00:00
|
|
|
*(char **)GB.Array.Get(list, i) = GB.NewZeroString(TO_UTF8(files[i]));
|
2008-04-24 12:49:12 +00:00
|
|
|
|
|
|
|
GB.ReturnBoolean(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dialog_title = QString::null;
|
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
|
2012-01-31 10:25:32 +00:00
|
|
|
BEGIN_METHOD_VOID(Dialog_SaveFile)
|
2008-04-24 12:49:12 +00:00
|
|
|
|
|
|
|
QString file;
|
|
|
|
|
2012-01-31 10:25:32 +00:00
|
|
|
file = my_getSaveFileName();
|
2008-04-24 12:49:12 +00:00
|
|
|
|
|
|
|
if (file.isNull())
|
|
|
|
GB.ReturnBoolean(true);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dialog_path = file;
|
|
|
|
GB.ReturnBoolean(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
dialog_title = QString::null;
|
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
|
2012-01-31 10:25:32 +00:00
|
|
|
BEGIN_METHOD_VOID(Dialog_SelectDirectory)
|
2008-04-24 12:49:12 +00:00
|
|
|
|
|
|
|
QString file;
|
|
|
|
|
2012-01-31 10:25:32 +00:00
|
|
|
file = my_getExistingDirectory();
|
2008-04-24 12:49:12 +00:00
|
|
|
|
|
|
|
if (file.isNull())
|
|
|
|
GB.ReturnBoolean(true);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dialog_path = file;
|
|
|
|
GB.ReturnBoolean(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
dialog_title = QString::null;
|
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
|
2012-01-31 10:25:32 +00:00
|
|
|
BEGIN_METHOD_VOID(Dialog_SelectColor)
|
2008-04-24 12:49:12 +00:00
|
|
|
|
|
|
|
QColor color;
|
|
|
|
|
2010-09-08 11:56:05 +00:00
|
|
|
color = QColorDialog::getColor(dialog_color, qApp->activeWindow(), dialog_title); //, qApp->activeWindow());
|
2008-04-24 12:49:12 +00:00
|
|
|
|
|
|
|
if (!color.isValid())
|
|
|
|
GB.ReturnBoolean(true);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dialog_color = color.rgb() & 0xFFFFFF;
|
|
|
|
GB.ReturnBoolean(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
|
2012-01-31 10:25:32 +00:00
|
|
|
BEGIN_METHOD_VOID(Dialog_SelectFont)
|
2008-04-24 12:49:12 +00:00
|
|
|
|
|
|
|
QFont qfont;
|
|
|
|
bool ok;
|
|
|
|
#ifdef USE_DPI
|
|
|
|
int dpiX, dpiY;
|
|
|
|
#endif
|
|
|
|
|
2009-08-17 07:42:06 +00:00
|
|
|
if (dialog_font)
|
|
|
|
qfont = *dialog_font->font;
|
|
|
|
else
|
|
|
|
qfont = QApplication::font();
|
2008-04-24 12:49:12 +00:00
|
|
|
//qDebug("AVANT: %g --> %g", qfont.pointSizeFloat(), SIZE_REAL_TO_VIRTUAL(qfont.pointSizeFloat()));
|
2008-04-29 13:40:55 +00:00
|
|
|
qfont.setPointSizeF(SIZE_REAL_TO_VIRTUAL(qfont.pointSizeF()));
|
2008-04-24 12:49:12 +00:00
|
|
|
|
|
|
|
#ifdef USE_DPI
|
|
|
|
dpiX = QPaintDevice::x11AppDpiX();
|
|
|
|
dpiY = QPaintDevice::x11AppDpiY();
|
|
|
|
QPaintDevice::x11SetAppDpiX(CFONT_dpi);
|
|
|
|
QPaintDevice::x11SetAppDpiY(CFONT_dpi);
|
|
|
|
#endif
|
|
|
|
|
2010-09-08 11:56:05 +00:00
|
|
|
qfont = QFontDialog::getFont(&ok, qfont, qApp->activeWindow(), dialog_title);
|
2008-04-24 12:49:12 +00:00
|
|
|
|
|
|
|
#ifdef USE_DPI
|
|
|
|
QPaintDevice::x11SetAppDpiX(dpiX);
|
|
|
|
QPaintDevice::x11SetAppDpiY(dpiY);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
//qDebug("APRES: %g --> %g", qfont.pointSizeFloat(), SIZE_VIRTUAL_TO_REAL(qfont.pointSizeFloat()));
|
2008-04-29 13:40:55 +00:00
|
|
|
qfont.setPointSizeF(SIZE_VIRTUAL_TO_REAL(qfont.pointSizeF()));
|
2008-04-24 12:49:12 +00:00
|
|
|
|
|
|
|
if (!ok)
|
|
|
|
GB.ReturnBoolean(true);
|
|
|
|
else
|
|
|
|
{
|
2009-08-17 07:42:06 +00:00
|
|
|
GB.StoreObject(NULL, POINTER(&dialog_font));
|
|
|
|
dialog_font = CFONT_create(qfont);
|
2010-03-28 21:58:45 +00:00
|
|
|
GB.Ref(dialog_font);
|
2008-04-24 12:49:12 +00:00
|
|
|
GB.ReturnBoolean(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
GB_DESC CDialogDesc[] =
|
|
|
|
{
|
|
|
|
GB_DECLARE("Dialog", 0), GB_VIRTUAL_CLASS(),
|
|
|
|
|
2009-08-17 07:42:06 +00:00
|
|
|
//GB_STATIC_METHOD("_init", NULL, CDIALOG_init, NULL),
|
2008-04-24 12:49:12 +00:00
|
|
|
GB_STATIC_METHOD("_exit", NULL, CDIALOG_exit, NULL),
|
|
|
|
|
2012-01-31 10:25:32 +00:00
|
|
|
GB_STATIC_METHOD("OpenFile", "b", Dialog_OpenFile, "[(Multi)b]"),
|
|
|
|
GB_STATIC_METHOD("SaveFile", "b", Dialog_SaveFile, NULL),
|
|
|
|
GB_STATIC_METHOD("SelectDirectory", "b", Dialog_SelectDirectory, NULL),
|
|
|
|
GB_STATIC_METHOD("SelectColor", "b", Dialog_SelectColor, NULL),
|
|
|
|
GB_STATIC_METHOD("SelectFont", "b", Dialog_SelectFont, NULL),
|
|
|
|
|
|
|
|
GB_STATIC_PROPERTY("Title", "s", Dialog_Title),
|
|
|
|
GB_STATIC_PROPERTY("Path", "s", Dialog_Path),
|
|
|
|
GB_STATIC_PROPERTY_READ("Paths", "String[]", Dialog_Paths),
|
|
|
|
GB_STATIC_PROPERTY("Filter", "String[]", Dialog_Filter),
|
|
|
|
GB_STATIC_PROPERTY("Color", "i", Dialog_Color),
|
|
|
|
GB_STATIC_PROPERTY("Font", "Font", Dialog_Font),
|
|
|
|
GB_STATIC_PROPERTY("ShowHidden", "b", Dialog_ShowHidden),
|
2008-04-24 12:49:12 +00:00
|
|
|
|
|
|
|
GB_END_DECLARE
|
|
|
|
};
|
|
|
|
|
|
|
|
|