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
|
// This file is part of fityk program. Copyright 2001-2013 Marcin Wojdyr
// Licence: GNU General Public License ver. 2+
#include <ctype.h>
#include <wx/wx.h>
#include <wx/config.h>
#include <wx/colordlg.h>
#include <wx/statline.h>
#include <wx/msgdlg.h>
#include <wx/stdpaths.h>
#include "cmn.h"
using namespace std;
namespace {
/// Round real to integer. Defined here to avoid dependency on ../common.h.
int iround(double d) { return static_cast<int>(floor(d+0.5)); }
}
bool cfg_read_bool(wxConfigBase *cf, const wxString& key, bool def_val)
{
bool b;
cf->Read(key, &b, def_val);
return b;
}
double cfg_read_double(wxConfigBase *cf, const wxString& key, double def_val)
{
double d;
cf->Read(key, &d, def_val);
return d;
}
// the storing of color values was changed in 0.9.7
// before:
// config->Write(key + wxT("/Red"), (int) value.Red());
// config->Write(key + wxT("/Green"), (int) value.Green());
// config->Write(key + wxT("/Blue"), (int) value.Blue());
// after:
// config->Write(key + wxT("_col"), value.GetAsString(wxC2S_CSS_SYNTAX));
wxColour cfg_read_color(const wxConfigBase *config, const wxString& key,
const wxColour& default_value)
{
wxString val = config->Read(key + wxT("_col"), wxEmptyString);
if (val.empty() && config->Exists(key + wxT("/Red")))
return wxColour(
config->Read (key + wxT("/Red"), (int) default_value.Red()),
config->Read (key + wxT("/Green"), (int) default_value.Green()),
config->Read (key + wxT("/Blue"), (int) default_value.Blue()));
return val.empty() ? default_value : wxColour(val);
}
void cfg_write_color(wxConfigBase *config, const wxString& key,
const wxColour& value)
{
config->Write(key + wxT("_col"), value.GetAsString(wxC2S_CSS_SYNTAX));
if (config->Exists(key + wxT("/Red")))
config->DeleteGroup(key);
}
wxFont cfg_read_font(wxConfigBase const *config, wxString const& key,
wxFont const &default_value)
{
wxString str = config->Read(key, wxEmptyString);
if (str.empty())
return default_value;
wxFont font;
bool ok = font.SetNativeFontInfo(str);
return ok ? font : default_value;
}
void cfg_write_font (wxConfigBase *config, const wxString& key,
const wxFont& value)
{
config->Write(key, value.IsOk() ? value.GetNativeFontInfoDesc()
: wxString());
}
bool change_color_dlg(wxColour& col)
{
wxColourData col_data;
col_data.SetCustomColour(0, col);
col_data.SetColour(col);
wxColourDialog dialog(0, &col_data);
bool ok = (dialog.ShowModal() == wxID_OK);
if (ok)
col = dialog.GetColourData().GetColour();
return ok;
}
void add_apply_close_buttons(wxWindow *parent, wxSizer *top_sizer)
{
top_sizer->Add(new wxStaticLine(parent, -1), 0, wxEXPAND|wxLEFT|wxRIGHT, 5);
wxBoxSizer *s = new wxBoxSizer(wxHORIZONTAL);
s->Add(new wxButton(parent, wxID_APPLY, wxT("&Apply")), 0, wxALL, 5);
s->Add(new wxButton(parent, wxID_CLOSE, wxT("&Close")), 0, wxALL, 5);
top_sizer->Add(s, 0, wxALL|wxALIGN_CENTER, 0);
}
//===============================================================
// ProportionalSplitter
//===============================================================
ProportionalSplitter::ProportionalSplitter(wxWindow* parent, wxWindowID id,
float proportion, const wxSize& size,
long style)
: wxSplitterWindow(parent, id, wxDefaultPosition, size, style),
m_proportion(proportion), m_firstpaint(true)
{
//wxASSERT(m_proportion >= 0. && m_proportion <= 1.);
SetMinimumPaneSize(20);
ResetSash();
Connect(GetId(), wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGED,
wxSplitterEventHandler(ProportionalSplitter::OnSashChanged));
Connect(GetId(), wxEVT_SIZE,
wxSizeEventHandler(ProportionalSplitter::OnReSize));
//hack to set sizes on first paint event
Connect(GetId(), wxEVT_PAINT,
wxPaintEventHandler(ProportionalSplitter::OnPaint));
}
bool ProportionalSplitter::SplitHorizProp(wxWindow* win1, wxWindow* win2,
float proportion)
{
if (proportion >= 0.f && proportion <= 1.f)
m_proportion = proportion;
int height = GetClientSize().GetHeight();
int h = iround(height * m_proportion);
//sometimes there is a strange problem without it (why?)
if (h < GetMinimumPaneSize() || h > height-GetMinimumPaneSize())
h = 0;
return wxSplitterWindow::SplitHorizontally(win1, win2, h);
}
bool ProportionalSplitter::SplitVertProp(wxWindow* win1, wxWindow* win2,
float proportion)
{
if (proportion >= 0.f && proportion <= 1.f)
m_proportion = proportion;
int width = GetClientSize().GetWidth();
int w = iround(width * m_proportion);
if (w < GetMinimumPaneSize() || w > width-GetMinimumPaneSize())
w = 0;
return wxSplitterWindow::SplitVertically(win1, win2, w);
}
int ProportionalSplitter::GetExpectedSashPosition()
{
return iround(GetWindowSize() * m_proportion);
}
void ProportionalSplitter::SetSashPosition(int position)
{
if (position < 0 || position > GetWindowSize())
return;
m_proportion = float(position) / GetWindowSize();
wxSplitterWindow::SetSashPosition(position);
}
void ProportionalSplitter::ResetSash()
{
wxSplitterWindow::SetSashPosition(GetExpectedSashPosition());
}
void ProportionalSplitter::OnReSize(wxSizeEvent& event)
{
// We may need to adjust the sash based on m_proportion.
ResetSash();
event.Skip();
}
void ProportionalSplitter::OnSashChanged(wxSplitterEvent &event)
{
// This event is triggered on start-up in some cases,
// eg. on wxGTK, in a setup with h-splitter containing two v-splitters,
// if the inner splitters are split first the lower v-splitter gets it.
if (!m_firstpaint) {
event.Skip();
return;
}
// We'll change m_proportion now based on where user dragged the sash.
const wxSize& s = GetSize();
int t = GetSplitMode() == wxSPLIT_HORIZONTAL ? s.GetHeight() : s.GetWidth();
float prop = float(event.GetSashPosition()) / t;
if (prop > 0.f && prop < 1.f)
m_proportion = prop;
event.Skip();
}
void ProportionalSplitter::OnPaint(wxPaintEvent &event)
{
if (m_firstpaint) {
if (GetSashPosition() != GetExpectedSashPosition())
ResetSash();
m_firstpaint = false;
}
event.Skip();
}
// KFTextCtrl
void KFTextCtrl::OnKillFocus(wxFocusEvent& event)
{
wxCommandEvent ev(wxEVT_COMMAND_TEXT_ENTER, GetId());
ev.SetEventObject(this);
ProcessEvent(ev);
event.Skip();
}
BEGIN_EVENT_TABLE(KFTextCtrl, wxTextCtrl)
EVT_KILL_FOCUS(KFTextCtrl::OnKillFocus)
END_EVENT_TABLE()
/// All configuration is stored in files in directory:
/// Unix: ~/.fityk
/// Windows: C:\Documents and Settings\username\Application Data\fityk
/// Mac: ~/Library/Application Support/fityk
wxString get_conf_file(string const& filename)
{
return wxStandardPaths::Get().GetUserDataDir()
+ wxFILE_SEP_PATH + s2wx(filename);
}
bool should_focus_input(wxKeyEvent& event)
{
if (event.AltDown() || event.ControlDown() || event.CmdDown())
return false;
int c = event.GetKeyCode();
return c < 128 && (c == ' ' || isalnum(c) || ispunct(c));
}
void updateControlWithItems(wxControlWithItems *cwi, vector<string> const& v)
{
if (v.size() != (size_t) cwi->GetCount()) {
cwi->Clear();
for (size_t i = 0; i < v.size(); ++i)
cwi->Append(s2wx(v[i]));
} else
for (size_t i = 0; i < v.size(); ++i)
if (cwi->GetString(i) != s2wx(v[i]))
cwi->SetString(i, s2wx(v[i]));
}
SmallStaticText::SmallStaticText(wxWindow* parent, const wxString& label)
: wxStaticText(parent, wxID_ANY, label)
{
wxFont font = GetFont();
#ifdef __WXMSW__
font.SetPointSize(font.GetPointSize() - 1);
#else
font.SetPointSize(font.GetPointSize() - 2);
#endif
SetFont(font);
}
DialogCloser* DialogCloser::instance_ = NULL;
DialogCloser* DialogCloser::instance()
{
if (instance_ == NULL)
instance_ = new DialogCloser();
return instance_;
}
void DialogCloser::OnClose(wxCommandEvent& event)
{
wxWindow* w = (wxWindow*) event.GetEventObject();
while (!w->IsTopLevel())
w = w->GetParent();
wxDialog *dialog = wxDynamicCast(w, wxDialog);
assert(dialog->IsModal());
dialog->EndModal(wxID_CANCEL);
}
TextComboDlg::TextComboDlg(wxWindow *parent, const wxString& message,
const wxString& caption)
: wxDialog(parent, -1, caption)
{
wxBoxSizer *topsizer = new wxBoxSizer(wxVERTICAL);
topsizer->Add(CreateTextSizer(message), wxSizerFlags().DoubleBorder());
combo = new wxComboBox(this, -1);
topsizer->Add(combo, wxSizerFlags().Expand().TripleBorder(wxLEFT|wxRIGHT));
wxSizer *buttonSizer = CreateSeparatedButtonSizer(wxOK|wxCANCEL);
if (buttonSizer)
topsizer->Add(buttonSizer, wxSizerFlags().DoubleBorder().Expand());
SetSizerAndFit(topsizer);
combo->SetFocus();
}
|