// be in your main program (e.g. hello.cpp). Now IMPLEMENT_APP should add this
// code if required.
-#if defined(__AIX__) || defined(__HPUX__) || defined( __VMS__ ) || defined(__WXPM__)
+#if !wxUSE_GUI || defined(__AIX__) || defined(__HPUX__) || defined( __VMS__ ) || defined(__WXPM__)
#define IMPLEMENT_WXWIN_MAIN \
extern int wxEntry( int argc, char *argv[] ); \
int main(int argc, char *argv[]) { return wxEntry(argc, argv); }
#define wxCAL_SUNDAY_FIRST 0x0000
#define wxCAL_MONDAY_FIRST 0x0001
#define wxCAL_SHOW_HOLIDAYS 0x0002
+#define wxCAL_NO_YEAR_CHANGE 0x0004
+#define wxCAL_NO_MONTH_CHANGE 0x000c // no month change => no year change
/*
* extended dialog specifiers. these values are stored in a different
#include "wx/spinctrl.h" // for wxSpinEvent
class WXDLLEXPORT wxComboBox;
+class WXDLLEXPORT wxStaticText;
#define wxCalendarNameStr _T("CalendarCtrl")
void SetDate(const wxDateTime& date);
const wxDateTime& GetDate() const { return m_date; }
+ // calendar mode
+ // -------------
+
+ // some calendar styles can't be changed after the control creation by
+ // just using SetWindowStyle() and Refresh() and the functions below
+ // should be used instead for them
+
+ // corresponds to wxCAL_NO_YEAR_CHANGE bit
+ void EnableYearChange(bool enable = TRUE);
+
+ // corresponds to wxCAL_NO_MONTH_CHANGE bit
+ void EnableMonthChange(bool enable = TRUE);
+
+ // corresponds to wxCAL_SHOW_HOLIDAYS bit
+ void EnableHolidayDisplay(bool display = TRUE);
+
// customization
// -------------
const wxColour& GetHolidayColourFg() const { return m_colHolidayFg; }
const wxColour& GetHolidayColourBg() const { return m_colHolidayBg; }
- // this function should be called instead of directly changing the
- // wxCAL_SHOW_HOLIDAYS bit in the control style after the control creation
- // (this won't work)
- void EnableHolidayDisplay(bool display = TRUE);
-
// an item without custom attributes is drawn with the default colours and
// font and without border, setting custom attributes allows to modify this
//
GenerateEvent(type2);
}
+ // do we allow changing the month/year?
+ bool AllowMonthChange() const
+ {
+ return (GetWindowStyle() & wxCAL_NO_MONTH_CHANGE)
+ != wxCAL_NO_MONTH_CHANGE;
+ }
+ bool AllowYearChange() const
+ {
+ return !(GetWindowStyle() & wxCAL_NO_YEAR_CHANGE);
+ }
+
+ // show the correct controls
+ void ShowCurrentControls();
+
+ // get the currently shown control for month/year
+ wxControl *GetMonthControl() const;
+ wxControl *GetYearControl() const;
+
// the subcontrols
+ wxStaticText *m_staticMonth;
wxComboBox *m_comboMonth;
+
+ wxStaticText *m_staticYear;
wxSpinCtrl *m_spinYear;
// the current selection
void OnCalendarWeekDayClick(wxCalendarEvent& event);
void OnCalendarChange(wxCalendarEvent& event);
+ wxCalendarCtrl *GetCal() const { return m_calendar; }
+
void StartWithMonday(bool on);
- void ShowHolidays(bool on);
void HighlightSpecial(bool on);
private:
void OnCalHolidays(wxCommandEvent& event);
void OnCalSpecial(wxCommandEvent& event);
+ void OnCalAllowMonth(wxCommandEvent& event);
+ void OnCalAllowYear(wxCommandEvent& event);
+
+ void OnAllowYearUpdate(wxUpdateUIEvent& event);
+
private:
MyPanel *m_panel;
Calendar_Cal_Monday = 200,
Calendar_Cal_Holidays,
Calendar_Cal_Special,
+ Calendar_Cal_Month,
+ Calendar_Cal_Year,
Calendar_CalCtrl = 1000,
};
EVT_MENU(Calendar_Cal_Monday, MyFrame::OnCalMonday)
EVT_MENU(Calendar_Cal_Holidays, MyFrame::OnCalHolidays)
EVT_MENU(Calendar_Cal_Special, MyFrame::OnCalSpecial)
+
+ EVT_MENU(Calendar_Cal_Month, MyFrame::OnCalAllowMonth)
+ EVT_MENU(Calendar_Cal_Year, MyFrame::OnCalAllowYear)
+
+ EVT_UPDATE_UI(Calendar_Cal_Year, MyFrame::OnAllowYearUpdate)
END_EVENT_TABLE()
BEGIN_EVENT_TABLE(MyPanel, wxPanel)
bool MyApp::OnInit()
{
// Create the main application window
- MyFrame *frame = new MyFrame("Minimal wxWindows App",
+ MyFrame *frame = new MyFrame("Calendar wxWindows sample",
wxPoint(50, 50), wxSize(450, 340));
// Show it and tell the application that it's our main window
wxMenu *menuCal = new wxMenu;
menuCal->Append(Calendar_Cal_Monday,
- "&Monday first weekday\tCtrl-M",
+ "Monday &first weekday\tCtrl-F",
"Toggle between Mon and Sun as the first week day",
TRUE);
menuCal->Append(Calendar_Cal_Holidays, "Show &holidays\tCtrl-H",
menuCal->Append(Calendar_Cal_Special, "Highlight &special dates\tCtrl-S",
"Test custom highlighting",
TRUE);
+ menuCal->AppendSeparator();
+ menuCal->Append(Calendar_Cal_Month, "&Month can be changed\tCtrl-M",
+ "Allow changing the month in the calendar",
+ TRUE);
+ menuCal->Append(Calendar_Cal_Year, "&Year can be changed\tCtrl-Y",
+ "Allow changing the year in the calendar",
+ TRUE);
// now append the freshly created menu to the menu bar...
wxMenuBar *menuBar = new wxMenuBar;
menuBar->Check(Calendar_Cal_Monday, TRUE);
menuBar->Check(Calendar_Cal_Holidays, TRUE);
+ menuBar->Check(Calendar_Cal_Month, TRUE);
+ menuBar->Check(Calendar_Cal_Year, TRUE);
// ... and attach this menu bar to the frame
SetMenuBar(menuBar);
void MyFrame::OnCalMonday(wxCommandEvent& event)
{
- m_panel->StartWithMonday(GetMenuBar()->IsChecked(event.GetInt()) != 0);
+ m_panel->StartWithMonday(GetMenuBar()->IsChecked(event.GetInt()));
}
void MyFrame::OnCalHolidays(wxCommandEvent& event)
{
- m_panel->ShowHolidays(GetMenuBar()->IsChecked(event.GetInt()) != 0);
+ bool enable = GetMenuBar()->IsChecked(event.GetInt());
+ m_panel->GetCal()->EnableHolidayDisplay(enable);
}
void MyFrame::OnCalSpecial(wxCommandEvent& event)
{
- m_panel->HighlightSpecial(GetMenuBar()->IsChecked(event.GetInt()) != 0);
+ m_panel->HighlightSpecial(GetMenuBar()->IsChecked(event.GetInt()));
+}
+
+void MyFrame::OnCalAllowMonth(wxCommandEvent& event)
+{
+ bool allow = GetMenuBar()->IsChecked(event.GetInt());
+
+ m_panel->GetCal()->EnableMonthChange(allow);
+}
+
+void MyFrame::OnCalAllowYear(wxCommandEvent& event)
+{
+ bool allow = GetMenuBar()->IsChecked(event.GetInt());
+
+ m_panel->GetCal()->EnableYearChange(allow);
+}
+
+void MyFrame::OnAllowYearUpdate(wxUpdateUIEvent& event)
+{
+ event.Enable( GetMenuBar()->IsChecked(Calendar_Cal_Month));
}
// ----------------------------------------------------------------------------
wxDefaultDateTime,
wxDefaultPosition,
wxDefaultSize,
- wxCAL_MONDAY_FIRST | wxCAL_SHOW_HOLIDAYS);
+ wxCAL_MONDAY_FIRST |
+ wxCAL_SHOW_HOLIDAYS |
+ wxRAISED_BORDER);
wxLayoutConstraints *c = new wxLayoutConstraints;
c->left.SameAs(this, wxLeft, 10);
m_calendar->Refresh();
}
-void MyPanel::ShowHolidays(bool on)
-{
- m_calendar->EnableHolidayDisplay(on);
-}
-
void MyPanel::HighlightSpecial(bool on)
{
if ( on )
//#define TEST_LONGLONG
//#define TEST_MIME
//#define TEST_STRINGS
-//#define TEST_THREADS
-#define TEST_TIME
+#define TEST_THREADS
+//#define TEST_TIME
// ============================================================================
// implementation
#ifndef WX_PRECOMP
#include "wx/app.h"
#include "wx/debug.h"
+ #include "wx/filefn.h"
#endif
#include "wx/module.h"
virtual int OnRun() { wxFAIL_MSG(wxT("unreachable")); return 0; }
};
+// ----------------------------------------------------------------------------
+// private functions
+// ----------------------------------------------------------------------------
+
+static bool DoInit();
+static void DoCleanUp();
+
// ----------------------------------------------------------------------------
// private vars
// ----------------------------------------------------------------------------
wxASSERT_MSG( !wxTheApp,
wxT("either call wxInitialize or create app, not both!") );
- wxClassInfo::InitializeClasses();
-
- wxModule::RegisterModules();
- if ( !wxModule::InitializeModules() )
+ if ( !DoInit() )
{
return FALSE;
}
{
if ( !--gs_nInitCount )
{
- wxModule::CleanUpModules();
+ DoCleanUp();
+ }
+}
+
+int wxEntry(int argc, char **argv)
+{
+ // library initialization
+ if ( !DoInit() )
+ {
+ return -1;
+ }
+
+ // create the app
+ if ( !wxTheApp )
+ {
+ wxCHECK_MSG( wxApp::GetInitializerFunction(), -1,
+ wxT("No application object: use IMPLEMENT_APP macro.") );
+
+ wxAppInitializerFunction fnCreate = wxApp::GetInitializerFunction();
+
+ wxTheApp = (wxApp *)fnCreate();
+ }
+
+ wxCHECK_MSG( wxTheApp, -1, wxT("wxWindows error: no application object") );
+
+ // app preinitialization
+ wxTheApp->argc = argc;
+
+#if wxUSE_UNICODE
+ wxTheApp->argv = new wxChar*[argc+1];
+ int mb_argc = 0;
+ while (mb_argc < argc)
+ {
+ wxTheApp->argv[mb_argc] = wxStrdup(wxConvLibc.cMB2WX(argv[mb_argc]));
+ mb_argc++;
+ }
+ wxTheApp->argv[mb_argc] = (wxChar *)NULL;
+#else
+ wxTheApp->argv = argv;
+#endif
+
+ wxString name = wxFileNameFromPath(argv[0]);
+ wxStripExtension(name);
+ wxTheApp->SetAppName(name);
+
+ int retValue = 0;
+
+ // app initialization
+ if ( !wxTheApp->OnInit() )
+ retValue = -1;
+
+ // app execution
+ if ( retValue == 0 )
+ {
+ retValue = wxTheApp->OnRun();
- wxClassInfo::CleanUpClasses();
+ // app clean up
+ wxTheApp->OnExit();
+ }
+
+ // library clean up
+ DoCleanUp();
+
+ return retValue;
+}
+
+// ----------------------------------------------------------------------------
+// private functions
+// ----------------------------------------------------------------------------
- // delete the application object
- delete wxTheApp;
- wxTheApp = (wxApp *)NULL;
+static bool DoInit()
+{
+ wxClassInfo::InitializeClasses();
+
+ wxModule::RegisterModules();
+ if ( !wxModule::InitializeModules() )
+ {
+ return FALSE;
}
+
+ return TRUE;
+}
+
+static void DoCleanUp()
+{
+#if wxUSE_LOG
+ // flush the logged messages if any
+ wxLog *log = wxLog::GetActiveTarget();
+ if (log != NULL && log->HasPendingMessages())
+ log->Flush();
+
+ // continuing to use user defined log target is unsafe from now on because
+ // some resources may be already unavailable, so replace it by something
+ // more safe
+ wxLog *oldlog = wxLog::SetActiveTarget(new wxLogStderr);
+ if ( oldlog )
+ delete oldlog;
+#endif // wxUSE_LOG
+
+ wxModule::CleanUpModules();
+
+ wxClassInfo::CleanUpClasses();
+
+ // delete the application object
+ delete wxTheApp;
+ wxTheApp = (wxApp *)NULL;
}
const char *StringAtOfs(wxMsgTableEntry *pTable, size_t32 index) const
{ return (const char *)(m_pData + Swap(pTable[index].ofsString)); }
-
+
// convert encoding to platform native one, if neccessary
void ConvertEncoding();
const char *hdr = StringAtOfs(m_pOrigTable, 0);
if (hdr == NULL) return; // not supported by this catalog, does not have non-fuzzy header
if (hdr[0] != 0) return; // ditto
-
+
/* we support catalogs with header (msgid "") that is _not_ marked as "#, fuzzy" (otherwise
the string would not be included into compiled catalog) */
wxString header(StringAtOfs(m_pTransTable, 0));
wxString charset;
int pos = header.Find("Content-Type: text/plain; charset=");
- if (pos == -1) return; // incorrectly filled Content-Type header
- pos += 34 ; /*strlen("Content-Type: text/plain; charset=")*/
- while (header[pos] != '\n') charset << header[pos++];
-
- if ((enc = wxTheFontMapper -> CharsetToEncoding(charset, FALSE)) == wxFONTENCODING_SYSTEM) return;
+ if (pos == wxNOT_FOUND)
+ return; // incorrectly filled Content-Type header
+ size_t n = pos + 34; /*strlen("Content-Type: text/plain; charset=")*/
+ while (header[n] != '\n')
+ charset << header[n++];
+
+ enc = wxTheFontMapper->CharsetToEncoding(charset, FALSE);
+ if ( enc == wxFONTENCODING_SYSTEM )
+ return; // unknown encoding
wxFontEncodingArray a = wxEncodingConverter::GetPlatformEquivalents(enc);
- if (a[0] == enc) return; // no conversion needed, locale uses native encoding
-
- if (a.GetCount() == 0) return; // we don't know common equiv. under this platform
-
+ if (a[0] == enc)
+ return; // no conversion needed, locale uses native encoding
+
+ if (a.GetCount() == 0)
+ return; // we don't know common equiv. under this platform
+
wxEncodingConverter converter;
-
+
converter.Init(enc, a[0]);
- for (unsigned i = 0; i < m_numStrings; i++)
+ for (size_t i = 0; i < m_numStrings; i++)
converter.Convert((char*)StringAtOfs(m_pTransTable, i));
#endif
}
return home;
}
+
+#if 0
+
+wxString wxGetCurrentDir()
+{
+ wxString dir;
+ size_t len = 1024;
+ bool ok;
+ do
+ {
+ ok = getcwd(dir.GetWriteBuf(len + 1), len) != NULL;
+ dir.UngetWriteBuf();
+
+ if ( !ok )
+ {
+ if ( errno != ERANGE )
+ {
+ wxLogSysError(_T("Failed to get current directory"));
+
+ return wxEmptyString;
+ }
+ else
+ {
+ // buffer was too small, retry with a larger one
+ len *= 2;
+ }
+ }
+ //else: ok
+ } while ( !ok );
+
+ return dir;
+}
+
+#endif // 0
#include "wx/settings.h"
#include "wx/brush.h"
#include "wx/combobox.h"
+ #include "wx/stattext.h"
#endif //WX_PRECOMP
#include "wx/calctrl.h"
long style,
const wxString& WXUNUSED(name))
{
- SetWindowStyle(style | (wxRAISED_BORDER | wxWANTS_CHARS));
+ // needed to get the arrow keys normally used for the dialog navigation
+ SetWindowStyle(style | wxWANTS_CHARS);
m_date = date.IsValid() ? date : wxDateTime::Today();
- m_comboMonth = new wxMonthComboBox(this);
m_spinYear = new wxYearSpinCtrl(this);
+ m_staticYear = new wxStaticText(GetParent(), -1, m_date.Format(_T("%Y")),
+ wxDefaultPosition, wxDefaultSize,
+ wxALIGN_CENTRE);
+
+ m_comboMonth = new wxMonthComboBox(this);
+ m_staticMonth = new wxStaticText(GetParent(), -1, m_date.Format(_T("%B")),
+ wxDefaultPosition, wxDefaultSize,
+ wxALIGN_CENTRE);
+
+ ShowCurrentControls();
wxSize sizeReal;
if ( size.x == -1 || size.y == -1 )
return FALSE;
}
- m_comboMonth->Show(show);
- m_spinYear->Show(show);
+ GetMonthControl()->Show(show);
+ GetYearControl()->Show(show);
return TRUE;
}
return FALSE;
}
- m_comboMonth->Enable(enable);
- m_spinYear->Enable(enable);
+ GetMonthControl()->Enable(enable);
+ GetYearControl()->Enable(enable);
return TRUE;
}
+// ----------------------------------------------------------------------------
+// enable/disable month/year controls
+// ----------------------------------------------------------------------------
+
+void wxCalendarCtrl::ShowCurrentControls()
+{
+ if ( AllowMonthChange() )
+ {
+ m_comboMonth->Show();
+ m_staticMonth->Hide();
+
+ if ( AllowYearChange() )
+ {
+ m_spinYear->Show();
+ m_staticYear->Hide();
+
+ // skip the rest
+ return;
+ }
+ }
+ else
+ {
+ m_comboMonth->Hide();
+ m_staticMonth->Show();
+ }
+
+ // year change not allowed here
+ m_spinYear->Hide();
+ m_staticYear->Show();
+}
+
+wxControl *wxCalendarCtrl::GetMonthControl() const
+{
+ return AllowMonthChange() ? m_comboMonth : m_staticMonth;
+}
+
+wxControl *wxCalendarCtrl::GetYearControl() const
+{
+ return AllowYearChange() ? m_spinYear : m_staticYear;
+}
+
+void wxCalendarCtrl::EnableYearChange(bool enable)
+{
+ if ( enable != AllowYearChange() )
+ {
+ long style = GetWindowStyle();
+ if ( enable )
+ style &= ~wxCAL_NO_YEAR_CHANGE;
+ else
+ style |= wxCAL_NO_YEAR_CHANGE;
+ SetWindowStyle(style);
+
+ ShowCurrentControls();
+ }
+}
+
+void wxCalendarCtrl::EnableMonthChange(bool enable)
+{
+ if ( enable != AllowMonthChange() )
+ {
+ long style = GetWindowStyle();
+ if ( enable )
+ style &= ~wxCAL_NO_MONTH_CHANGE;
+ else
+ style |= wxCAL_NO_MONTH_CHANGE;
+ SetWindowStyle(style);
+
+ ShowCurrentControls();
+ }
+}
+
// ----------------------------------------------------------------------------
// changing date
// ----------------------------------------------------------------------------
void wxCalendarCtrl::SetDate(const wxDateTime& date)
{
- if ( m_date.GetMonth() == date.GetMonth() &&
- m_date.GetYear() == date.GetYear() )
+ bool sameMonth = m_date.GetMonth() == date.GetMonth(),
+ sameYear = m_date.GetYear() == date.GetYear();
+
+ if ( sameMonth && sameYear )
{
// just change the day
ChangeDay(date);
}
else
{
+ if ( !AllowMonthChange() || (!AllowYearChange() && !sameYear) )
+ {
+ // forbidden
+ return;
+ }
+
// change everything
m_date = date;
// update the controls
m_comboMonth->SetSelection(m_date.GetMonth());
- m_spinYear->SetValue(m_date.Format(_T("%Y")));
+
+ if ( AllowYearChange() )
+ {
+ m_spinYear->SetValue(m_date.Format(_T("%Y")));
+ }
// update the calendar
Refresh();
// size or position changes: the combobox and spinctrl are along the top of
// the available area and the calendar takes up therest of the space
+// the static controls are supposed to be always smaller than combo/spin so we
+// always use the latter for size calculations and position the static to take
+// the same space
+
// the constants used for the layout
#define VERT_MARGIN 5 // distance between combo and calendar
#define HORZ_MARGIN 15 // spin
height += VERT_MARGIN + wxMax(sizeCombo.y, sizeSpin.y);
+ if ( GetWindowStyle() & (wxRAISED_BORDER | wxSUNKEN_BORDER) )
+ {
+ // the border would clip the last line otherwise
+ height += 4;
+ }
+
return wxSize(width, height);
}
void wxCalendarCtrl::DoMoveWindow(int x, int y, int width, int height)
{
wxSize sizeCombo = m_comboMonth->GetSize();
+ wxSize sizeStatic = m_staticMonth->GetSize();
+
+ int dy = (sizeCombo.y - sizeStatic.y) / 2;
m_comboMonth->Move(x, y);
+ m_staticMonth->SetSize(x, y + dy, sizeCombo.x, sizeStatic.y);
int xDiff = sizeCombo.x + HORZ_MARGIN;
m_spinYear->SetSize(x + xDiff, y, width - xDiff, sizeCombo.y);
+ m_staticYear->SetSize(x + xDiff, y + dy, width - xDiff, sizeStatic.y);
wxSize sizeSpin = m_spinYear->GetSize();
int yDiff = wxMax(sizeSpin.y, sizeCombo.y) + VERT_MARGIN;
// our real top corner is not in this position
if ( y )
{
- *y -= m_comboMonth->GetSize().y + VERT_MARGIN;
+ *y -= GetMonthControl()->GetSize().y + VERT_MARGIN;
}
}
// our real height is bigger
if ( height )
{
- *height += m_comboMonth->GetSize().y + VERT_MARGIN;
+ *height += GetMonthControl()->GetSize().y + VERT_MARGIN;
}
}