Non-Static Status Bar Attempt

From WxWiki
Jump to navigation Jump to search

What follows is my best attempt to eliminate flickering on MSW for the status bar. I consider it a failure. While the code runs fine, it still flickers, despite my best attempts. Can you hack out a version that works? It is hacked up from the statbar sample program. The basic idea is fairly simple. A status bar is created with two fields, and then a wxTextCtrl is created, with the status bar window as the parent. As the frame is resized, the position of the control is adjusted to the position of the field, and the bits are copied from the old position to the new position.

/////////////////////////////////////////////////////////////////////////////
// Name:        statbar.cpp
// Purpose:     wxStatusBar sample
// Author:      Vadim Zeitlin
// Modified by:
// Created:     04.02.00
// RCS-ID:      $Id: statbar.cpp,v 1.26 2006/04/09 11:00:45 VZ Exp $
// Copyright:   (c) Vadim Zeitlin
// Licence:     wxWindows licence
/////////////////////////////////////////////////////////////////////////////

// *** Hacked up badly by JC, to try to get a status bar that doesn't flicker 
//     under Win32. Just copy the statbar sample to another folder under samples, 
//     drop this in, and it should compile and run fine. (4/27/07)

// ============================================================================
// declarations
// ============================================================================

// ----------------------------------------------------------------------------
// headers
// ----------------------------------------------------------------------------

// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"

#ifdef __BORLANDC__
    #pragma hdrstop
#endif

#if !wxUSE_STATUSBAR
    #error "You need to set wxUSE_STATUSBAR to 1 to compile this sample"
#endif // wxUSE_STATUSBAR

// for all others, include the necessary headers
#ifndef WX_PRECOMP
    #include "wx/app.h"
    #include "wx/log.h"
    #include "wx/frame.h"
    #include "wx/statusbr.h"
    #include "wx/timer.h"
    #include "wx/checkbox.h"
    #include "wx/statbmp.h"
    #include "wx/menu.h"
    #include "wx/msgdlg.h"
    #include "wx/textdlg.h"
    #include "wx/sizer.h"
    #include "wx/stattext.h"
    #include "wx/bmpbuttn.h"
    #include "wx/dcmemory.h"
    #include "wx/settings.h"
#endif

#include "wx/datetime.h"
#include "wx/numdlg.h"

// ----------------------------------------------------------------------------
// resources
// ----------------------------------------------------------------------------

// ----------------------------------------------------------------------------
// private classes
// ----------------------------------------------------------------------------

// Define a new application type, each program should derive a class from wxApp
class MyApp : public wxApp
{
public:
    // override base class virtuals
    // ----------------------------

    // this one is called on application startup and is a good place for the app
    // initialization (doing it here and not in the ctor allows to have an error
    // return: if OnInit() returns false, the application terminates)
    virtual bool OnInit();
};

// A custom status bar which contains controls, icons &c
class MyStatusBar : public wxStatusBar
{
public:
    MyStatusBar(wxWindow *parent, long style);
    virtual ~MyStatusBar();


    // event handlers
    void OnSize(wxSizeEvent& event);

private:
    // toggle the state of the status bar controls

    wxTextCtrl *m_textcontrol;

    DECLARE_EVENT_TABLE()
};

// Define a new frame type: this is going to be our main frame
class MyFrame : public wxFrame
{
public:
    // ctor(s)
    MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size, long style);
    virtual ~MyFrame();

    // event handlers (these functions should _not_ be virtual)
    void OnQuit(wxCommandEvent& event);

private:
    void OnUpdateSetStatusFields(wxUpdateUIEvent& event);
    void DoCreateStatusBar();

    MyStatusBar *m_statbarCustom;

    // any class wishing to process wxWidgets events must use this macro
    DECLARE_EVENT_TABLE()
};

// ----------------------------------------------------------------------------
// constants
// ----------------------------------------------------------------------------

// IDs for the controls and the menu commands
enum
{
    // menu items
    StatusBar_Quit = 1,
};


// ----------------------------------------------------------------------------
// event tables and other macros for wxWidgets
// ----------------------------------------------------------------------------

// the event tables connect the wxWidgets events with the functions (event
// handlers) which process them. It can be also done at run-time, but for the
// simple menu events like this the static method is much simpler.
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
    EVT_MENU(StatusBar_Quit,  MyFrame::OnQuit)
END_EVENT_TABLE()

BEGIN_EVENT_TABLE(MyStatusBar, wxStatusBar)
    EVT_SIZE(MyStatusBar::OnSize)
END_EVENT_TABLE()

// Create a new application object: this macro will allow wxWidgets to create
// the application object during program execution (it's better than using a
// static object for many reasons) and also declares the accessor function
// wxGetApp() which will return the reference of the right type (i.e. MyApp and
// not wxApp)
IMPLEMENT_APP(MyApp)

// ============================================================================
// implementation
// ============================================================================

// ----------------------------------------------------------------------------
// the application class
// ----------------------------------------------------------------------------

// `Main program' equivalent: the program execution "starts" here
bool MyApp::OnInit()
{
    // create the main application window
    MyFrame *frame = new MyFrame(_T("wxStatusBar sample"),wxPoint(50, 50), 
      wxSize(450, 340), wxNO_FULL_REPAINT_ON_RESIZE|wxCLIP_CHILDREN|
      wxDEFAULT_FRAME_STYLE);

    // and show it (the frames, unlike simple controls, are not shown when
    // created initially)
    frame->Show(true);

    // success: wxApp::OnRun() will be called which will enter the main message
    // loop and the application will run. If we returned 'false' here, the
    // application would exit immediately.
    return true;
}

// ----------------------------------------------------------------------------
// main frame
// ----------------------------------------------------------------------------

// frame constructor
MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size, long style)
       : wxFrame((wxWindow *)NULL, wxID_ANY, title, pos, size, style)
{
    m_statbarCustom = NULL;


    // create a menu bar
    wxMenu *menuFile = new wxMenu;
    menuFile->Append(StatusBar_Quit, _T("E&xit\tAlt-X"), _T("Quit this program"));

    // now append the freshly created menu to the menu bar...
    wxMenuBar *menuBar = new wxMenuBar();
    menuBar->Append(menuFile, _T("&File"));

    // ... and attach this menu bar to the frame
    SetMenuBar(menuBar);

    //// create default status bar to start with

	wxStatusBar *statbarOld = GetStatusBar();
    if ( statbarOld )
    {
        statbarOld->Hide();
    }

    if ( !m_statbarCustom )
    {
        m_statbarCustom = new MyStatusBar(this, wxNO_FULL_REPAINT_ON_RESIZE|wxOWNER_DRAWN, 
          wxST_SIZEGRIP);
    }
    SetStatusBar(m_statbarCustom);

     GetStatusBar()->Show();
}

MyFrame::~MyFrame()
{
    SetStatusBar(NULL);

    delete m_statbarCustom;
}

void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
{
    // true is to force the frame to close
    Close(true);
}


// ----------------------------------------------------------------------------
// MyStatusBar
// ----------------------------------------------------------------------------


MyStatusBar::MyStatusBar(wxWindow *parent, long style)
           : wxStatusBar(parent, wxID_ANY, style, _(""))
{
    static const int widths[2] = { -1, 150};

    SetFieldsCount(2);
    SetStatusWidths(2, widths);
	
	 m_textcontrol=new wxTextCtrl(this,-1,_(""),wxPoint(32,200),wxSize(30,21),
           wxNO_BORDER|wxCLIP_SIBLINGS);
   m_textcontrol->SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_MENU));
   m_textcontrol->SetLabel(_("Start:"));

	wxRect rect;

	GetFieldRect(1, rect);
    m_textcontrol->SetSize(rect.x + 2, rect.y + 2, rect.width - 4, rect.height - 4);

}

MyStatusBar::~MyStatusBar()
{
}

void MyStatusBar::OnSize(wxSizeEvent& event)
{
    if ( !m_textcontrol )
        return;

    wxRect rect;
    GetFieldRect(1, rect);

    m_textcontrol->SetSize(rect.x + 2, rect.y + 2, rect.width - 4, rect.height - 4);

   // event.Skip();
}