⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 controls.cxx

📁 windows mobile phone source code
💻 CXX
📖 第 1 页 / 共 3 页
字号:
/*
 * controls.cxx
 *
 * Control interactor classes implmentation.
 *
 * Portable Windows Library
 *
 * Copyright (c) 1993-1998 Equivalence Pty. Ltd.
 *
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.0 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 * the License for the specific language governing rights and limitations
 * under the License.
 *
 * The Original Code is Portable Windows Library.
 *
 * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
 *
 * Portions are Copyright (C) 1993 Free Software Foundation, Inc.
 * All Rights Reserved.
 *
 * Contributor(s): ______________________________________.
 *
 * $Log: controls.cxx,v $
 * Revision 1.66  2001/05/25 06:08:38  robertj
 * Fix of resizing PMultiLineEditBox yet again. Seems that it depends on the
 *   flavour of windows, so did a version that auto compensates.
 *
 * Revision 1.65  2001/05/17 02:58:34  robertj
 * Change PEditBox so has 3d look, thanks Slava.
 *
 * Revision 1.64  2000/08/23 02:39:52  robertj
 * Fixed resize of multi-line edit box windows, again.
 *
 * Revision 1.63  2000/07/11 19:01:36  robertj
 * Fix of resizing PMultiLineEditBox allowing for scroll bars etc.
 *
 * Revision 1.62  2000/07/11 18:24:03  robertj
 * Fixed incorrect return value on PComboBox::GetCurrent() when no selection.
 *
 * Revision 1.61  2000/06/27 00:14:53  robertj
 * Fixed bug that causes crash when gettng cursor/selection position.
 *
 * Revision 1.60  2000/04/13 23:18:42  robertj
 * Fixed problems with sizing controls with scroll bars.
 *
 * Revision 1.59  2000/04/13 01:37:14  robertj
 * Added missing implementation of PEditBox::GetSelection() on Win32.
 * Added PEditBox::ReplaceSelection() function.
 *
 * Revision 1.58  1999/11/23 05:24:32  robertj
 * Moved function from platform independent to platform dependent files.
 *
 * Revision 1.57  1999/11/16 06:51:42  robertj
 * Created PCustomListBox to allow motif native code PStringListBox implementation
 *
 * Revision 1.56  1998/09/24 03:42:36  robertj
 * Added open software license.
 *
 * Revision 1.55  1998/09/21 13:31:01  robertj
 * Changes to support new PListView class. Different call back method.
 *
 * Revision 1.54  1998/09/14 14:57:46  robertj
 * Fixed incorrect return of owner draw mode for PStringListBox class.
 *
 * Revision 1.53  1998/09/14 13:11:14  robertj
 * Fixed memory leak on delete of PStringList, not deleting all its contents.
 * Added tab stops to string list box.
 *
 * Revision 1.52  1998/09/07 14:02:32  robertj
 * Fixed double delete of PStrings in PStringListBix descendents.
 *
 * Revision 1.51  1998/09/03 15:48:34  robertj
 * Added missing vertical scroll bar on drop down boxes.
 *
 * Revision 1.50  1998/08/06 00:55:59  robertj
 * Fixed using non-standard controls as PControl descendants.
 *
 * Revision 1.49  1998/05/07 05:22:11  robertj
 * Fixed bug in colour setting of radio buttons from resource.
 *
 * Revision 1.48  1998/02/06 04:00:17  robertj
 * Fixed colour of controls, again!
 *
 * Revision 1.47  1998/01/26 00:20:04  robertj
 * Fixed colour of static controls in windows and dialogs.
 *
 * Revision 1.46  1995/11/20 11:07:20  robertj
 * Fixed bug in scroll bar colour.
 * Fixed bug causing double notification in scroll bar click.
 *
 * Revision 1.45  1995/11/09 12:29:56  robertj
 * Moved window style bits to member variable.
 *
 * Revision 1.44  1995/08/24 12:43:52  robertj
 * Function OnEndInput() moved to platform dependent code.
 *
 * Revision 1.43  1995/07/02 06:23:32  robertj
 * Fixed mouse button events not appearing in static text controls.
 * Fixed bug with creating right and centred static text controls being left aligned.
 *
 * Revision 1.42  1995/06/17 11:10:02  robertj
 * Added function to determine if control is a tab stop.
 *
 * Revision 1.41  1995/06/04 12:52:15  robertj
 * Fixed bug in assert of list box resource control.
 *
 * Revision 1.40  1995/04/25 11:44:05  robertj
 * Fixed Borland compiler warnings.
 *
 * Revision 1.39  1995/04/02 09:28:05  robertj
 * Added "balloon" help.
 *
 * Revision 1.38  1995/03/12 05:00:24  robertj
 * Re-organisation of DOS/WIN16 and WIN32 platforms to maximise common code.
 * Used built-in equate for WIN32 API (_WIN32).
 *
 * Revision 1.37  1995/02/19  04:16:31  robertj
 * Added callback when PListBox::SetSelection changes selection.
 * Added dynamically linked command processing.
 *
 * Revision 1.36  1995/02/05  00:57:21  robertj
 * Had to change variable name "small" for MSVC 2.0
 *
 * Revision 1.35  1995/01/28  07:28:49  robertj
 * Assured all Windows strings have \n converted to \r\n and vice versa.
 *
 * Revision 1.34  1995/01/27  11:29:21  robertj
 * Fixed numerous bugs.
 * Fixed bug in nested messages.
 * Underscored library variables.
 *
 */

#include <pwlib.h>
#include <math.h>

static char ControlResourceMismatch[] = "Control class mismatch from resource";


//////////////////////////////////////////////////////////////////////////////
// PControl

PControl::PControl(PInteractor * parent, const PNotifier & notify, void *valuePtr)
  : PInteractor(PAssertNULL(parent)),
    callback(notify)
{
  PAssert(_hWnd == NULL, PLogicError);

  notifyForStateUpdate = FALSE;
  valuePointer = valuePtr;

  PRESOURCE_ID id = (PRESOURCE_ID)(parent->GetNumChildren() + 100);
  if (parent->IsDescendant(PInteractorLayout::Class())) {
    while (((PInteractorLayout*)parent)->GetControl(id))
      id++;
  }
  controlID = id;
}


PControl::PControl(PInteractorLayout * parent,
                 PRESOURCE_ID ctlID, const PNotifier & notify, void * valuePtr)
  : PInteractor(parent, GetDlgItem(PAssertNULL(parent)->GetHWND(), ctlID)),
    callback(notify)
{
  PAssert(_hWnd != NULL, "GetDlgItem() failed.");

  notifyForStateUpdate = FALSE;
  valuePointer = valuePtr;

  controlID = ctlID;

  _styleBits = GetWindowLong(_hWnd, GWL_STYLE);
  _exStyleBits = GetWindowLong(_hWnd, GWL_EXSTYLE);
  SubClassControlWndProc();
}


BOOL PControl::IsTabStop() const
{
  return (_styleBits&WS_TABSTOP) != 0;
}


void PControl::CreateHWND()
{
  HINSTANCE hInst = owner->GetInstance();

  WNDCLASS wclass;
  GetCreateWinInfo(wclass);
  if (!GetClassInfo(hInst, wclass.lpszClassName, &wclass))
    PAssertOS(RegisterClass(&wclass));

  _hWnd = CreateWindowEx(_exStyleBits,
                         wclass.lpszClassName,
                         "",
                         _styleBits,
                         -100, -100,
                         10, 10,
                         parent->GetHWND(),
                         (HMENU)(controlID),
                         hInst,
                         NULL);
  owner->AddWindowHandle(PAssertNULL(_hWnd), this);

  SubClassControlWndProc();
  SetWndFont();
}


void PControl::SubClassControlWndProc()
{
  wndProc = (WNDPROC)GetWindowLong(_hWnd, GWL_WNDPROC);
  WNDPROC pwlibWndProc = owner->GetWndProcPtr(PApplication::WndProcType);
  if (wndProc != pwlibWndProc)
    SetWindowLong(_hWnd, GWL_WNDPROC, (DWORD)pwlibWndProc);
  else
    wndProc = NULL;

#ifdef _WIN32
  SetClassLong(_hWnd, GCL_STYLE, GetClassLong(_hWnd, GCL_STYLE)|CS_DBLCLKS);
#else
  SetClassWord(_hWnd, GCW_STYLE, GetClassWord(_hWnd, GCW_STYLE)|CS_DBLCLKS);
#endif
}


void PControl::SetControlID(PRESOURCE_ID ID)
{
  controlID = ID;
  PSET_WINDOW_VALUE(GetHWND(), ID, controlID);
}


void PControl::DefWndProc()
{
  if (wndProc == NULL)
    PInteractor::DefWndProc();
  else {
    _msg->lResult =
         CallWindowProc(wndProc, _hWnd, _msg->event, _msg->wParam, _msg->lParam);
    _msg->processed = TRUE;
  }
}


int PControl::TranslateOption(NMHDR &) const
{
  return -1;
}


//////////////////////////////////////////////////////////////////////////////
// PNamedControl

PNamedControl::PNamedControl(PInteractor * parent,
           const PString & newName, const PNotifier & notify, void * valuePtr)
  : PControl(parent, notify, valuePtr), initName(newName)
{
}


void PNamedControl::SetName(const PString & name)
{
  SendMessage(GetHWND(), WM_SETREDRAW, FALSE, 0L);
  SetWndText(name);
  SendMessage(_hWnd, WM_SETREDRAW, TRUE, 0L);
  InvalidateRect(_hWnd, NULL, TRUE);
  UpdateWindow(_hWnd);
}


void PNamedControl::CreateHWND()
{
  PControl::CreateHWND();
  SetName(initName);
}


void PNamedControl::GetCreateWinInfo(WNDCLASS & wndClass)
{
  PControl::GetCreateWinInfo(wndClass);

  wndClass.lpszClassName = "Button";
  _styleBits |= WS_TABSTOP;
}


//////////////////////////////////////////////////////////////////////////////
// PStaticText

PStaticText::PStaticText(PInteractorLayout * parent,
                PRESOURCE_ID ctlID, const PNotifier & notify, void * valuePtr)
  : PNamedControl(parent, ctlID, notify, valuePtr)
{
  switch (_styleBits&15) {
    case SS_LEFT :
    case SS_SIMPLE :
    case SS_LEFTNOWORDWRAP :
      alignment = PCanvas::LeftAlign;
      break;

    case SS_CENTER :
      alignment = PCanvas::Centred;
      break;

    case SS_RIGHT :
      alignment = PCanvas::RightAlign;
      break;

    default:
      PAssertAlways(ControlResourceMismatch);
  }

  foregroundColour = parent->GetForegroundColour();
  backgroundColour = parent->GetBackgroundColour();
}


void PStaticText::SetAlignment(int newAlign)
{
  if (alignment != newAlign) {
    alignment = newAlign;
    _styleBits &= ~15;
    switch (alignment&PCanvas::HorizontalAlignmentMask) {
      case PCanvas::LeftAlign :
        _styleBits |= SS_LEFT;
        break;

      case PCanvas::Centred :
        _styleBits |= SS_CENTER;
        break;

      case PCanvas::RightAlign :
        _styleBits |= SS_RIGHT;
    }
    SetWindowLong(_hWnd, GWL_STYLE, _styleBits);
    Invalidate();
  }
}


void PStaticText::GetCreateWinInfo(WNDCLASS & wndClass)
{
  PNamedControl::GetCreateWinInfo(wndClass);

  wndClass.lpszClassName = "Static";

  switch (alignment&PCanvas::HorizontalAlignmentMask) {
    case PCanvas::RightAlign :
      _styleBits |= SS_RIGHT;
      break;

    case PCanvas::Centred :
      _styleBits |= SS_CENTER;
      break;

    default :
      _styleBits |= SS_LEFT;
  }
}


void PStaticText::OnRedraw(PCanvas & canvas)
{
  if (canvas.NullHDC() &&
              (alignment&PCanvas::VerticalAlignmentMask) == PCanvas::TopAlign)
    PControl::OnRedraw(canvas);
  else
    canvas.DrawString(canvas.GetDrawingBounds(), GetName(), alignment);
}


void PStaticText::WndProc()
{
  if (_msg->event == WM_NCHITTEST)
    _msg->lResult = HTCLIENT;
  else
    PNamedControl::WndProc();
}


//////////////////////////////////////////////////////////////////////////////
// PEditBox

void PEditBox::SetText(const PString & name)
{
  SetWndText(name);
  parent->OnControlNotify(*this, EditChange);
}


BOOL PEditBox::GetSelection(PINDEX * start, PINDEX * finish) const
{
  PINDEX dummyStart;
  if (start == NULL)
    start = &dummyStart;

  PINDEX dummyFinish;
  if (finish == NULL)
    finish = &dummyFinish;

  SendMessage(GetHWND(), EM_GETSEL, (WPARAM)start, (LPARAM)finish);
  return *finish > *start;
}


int PEditBox::TranslateOption(NMHDR & msg) const
{
  return msg.code == EN_CHANGE ? NotifyChange : -1;
}


void PEditBox::GetCreateWinInfo(WNDCLASS & wndClass)
{
  PControl::GetCreateWinInfo(wndClass);

  wndClass.lpszClassName = "Edit";
  _styleBits |= WS_BORDER|WS_TABSTOP|ES_AUTOHSCROLL;
  _exStyleBits |= WS_EX_CLIENTEDGE;
}


//////////////////////////////////////////////////////////////////////////////
// PPasswordEditBox

PPasswordEditBox::PPasswordEditBox(PInteractorLayout * parent,
              PRESOURCE_ID ctlID, const PNotifier & notify, PString * valuePtr)
  : PEditBox(parent, ctlID, notify, valuePtr)
{
  PAssert((_styleBits&ES_PASSWORD) != 0, ControlResourceMismatch);
}


void PPasswordEditBox::GetCreateWinInfo(WNDCLASS & wndClass)
{
  PEditBox::GetCreateWinInfo(wndClass);
  _styleBits |= ES_PASSWORD;
}


//////////////////////////////////////////////////////////////////////////////
// PMultiLineEditBox

PMultiLineEditBox::PMultiLineEditBox(PInteractorLayout * parent,
              PRESOURCE_ID ctlID, const PNotifier & notify, PString * valuePtr)
  : PEditBox(parent, ctlID, notify, valuePtr)
{
  PAssert((_styleBits&ES_MULTILINE) != 0, ControlResourceMismatch);
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -