x11dialog.cpp

来自「这是VCF框架的代码」· C++ 代码 · 共 541 行 · 第 1/2 页

CPP
541
字号
//X11Dialog.cpp/*Copyright 2000-2004 The VCF Project.Please see License.txt in the top level directorywhere you installed the VCF.*/#include "vcf/ApplicationKit/ApplicationKit.h"#include "vcf/ApplicationKit/ApplicationKitPrivate.h"#include "vcf/ApplicationKit/Label.h"#include "vcf/ApplicationKit/PushButton.h"#include "vcf/ApplicationKit/X11Dialog.h"#include "vcf/ApplicationKit/X11UIToolkit.h"#include <X11/Xatom.h>#include <X11/Xmd.h>using namespace VCF;class X11MessageBox : public Dialog {public:	void init( const String& message, const String& caption,				const long& messageButtons, const Dialog::MessageStyle& messageStyle ) {		X11UIToolkit* toolkit = reinterpret_cast<X11UIToolkit*>(UIToolkit::getDefaultUIToolkit());		setCaption( caption );		Label* label = new Label();		label->setCaption( message );		const VCFChar* P = message.c_str();		const VCFChar* start = P;		const VCFChar* line = P;		double maxWidth = 150.0;		double height = 0.0;		double x,y;		x = y = 0.0;		while ( (P-start) < message.size() ) {			if ( (*P == '\n') || ((*P == '\r') && (*(P+1) == '\n')) ) {				String text;				text.append( line, P-line );				height += getContext()->getTextHeight( text );				maxWidth = maxVal<double>( maxWidth, getContext()->getTextWidth( text ) );				line = P;				line ++;				if ( *P == '\r' ) {					line ++;					P++;				}			}			P++;		}		if ( line < P ) {			String text;			text.append( line, P-line );			height += getContext()->getTextHeight( text );			maxWidth = maxVal<double>( maxWidth, getContext()->getTextWidth( text ) );		}		label->setBounds( 10, 10, maxWidth, height + label->getFont()->getHeight()/2.0 );		add( label );		maxWidth = label->getWidth() + toolkit->getUIMetricsManager()->getPreferredSpacingFor( UIMetricsManager::stContainerBorderDelta ) * 2;		/*		mbOK = 1,		mbHelp = 2,		mbYesNo = 4,		mbYesNoCancel = 8,		mbOKCancel = 16,		mbRetryCancel = 32,		mbAbortRetryIgnore = 64		*/		PushButton* button = new PushButton();		int buttonCount = 1;		if ( (Dialog::mbYesNo & messageButtons) || (Dialog::mbOKCancel & messageButtons) || (Dialog::mbRetryCancel & messageButtons) ) {			buttonCount = 2;		}		else if ( (Dialog::mbAbortRetryIgnore & messageButtons) || (Dialog::mbYesNoCancel & messageButtons) ) {			buttonCount = 3;		}		if ( (Dialog::mbHelp & messageButtons) ) {			buttonCount ++;		}		double spacer = toolkit->getUIMetricsManager()->getPreferredSpacingFor( UIMetricsManager::stControlHorizontalSpacing );		y = label->getTop() + label->getHeight() + toolkit->getUIMetricsManager()->getPreferredSpacingFor( UIMetricsManager::stContainerBorderDelta );		double buttonWidth = button->getPreferredWidth();		x = maxWidth - ( button->getPreferredWidth() + toolkit->getUIMetricsManager()->getPreferredSpacingFor( UIMetricsManager::stContainerBorderDelta ) );		height = y;		for ( int i=0;i<buttonCount;i++ ) {			if ( i > 0 ) {				button = new PushButton();			}			switch( i ) {				case 0 : {					if ( (Dialog::mbOK & messageButtons) || (Dialog::mbOKCancel & messageButtons) ) {						button->setCommandType( BC_OK );						button->setCaption( "OK" );					}					else if ( (Dialog::mbYesNo & messageButtons) || (Dialog::mbYesNoCancel & messageButtons) ) {						button->setCommandType( BC_YES );						button->setCaption( "Yes" );					}					else if ( Dialog::mbAbortRetryIgnore & messageButtons ){						button->setCommandType( BC_ABORT );						button->setCaption( "Abort" );					}					else if ( Dialog::mbRetryCancel & messageButtons ) {						button->setCommandType( BC_RETRY );						button->setCaption( "Retry" );					}					button->setDefault( true );					button->setFocus( true );				}				break;				case 1 : {					if ( (Dialog::mbYesNo & messageButtons) || (Dialog::mbYesNoCancel & messageButtons) ) {						button->setCommandType( BC_NO );						button->setCaption( "No" );					}					else if ( (Dialog::mbOKCancel & messageButtons) || (Dialog::mbRetryCancel & messageButtons) ) {						button->setCommandType( BC_CANCEL );						button->setCaption( "Cancel" );					}					else if  ( Dialog::mbAbortRetryIgnore & messageButtons )  {						button->setCommandType( BC_RETRY );						button->setCaption( "Retry" );					}					else if ( Dialog::mbHelp & messageButtons )  {						button->setCommandType( BC_HELP );						button->setCaption( "Help" );					}				}				break;				case 2 : {					if ( Dialog::mbAbortRetryIgnore & messageButtons ) {						button->setCommandType( BC_IGNORE );						button->setCaption( "Ignore" );					}					else if ( Dialog::mbYesNoCancel & messageButtons ) {						button->setCommandType( BC_CANCEL );						button->setCaption( "Cancel" );					}					else if ( Dialog::mbHelp & messageButtons )  {						button->setCommandType( BC_HELP );						button->setCaption( "Help" );					}				}				break;				case 3 : {					if ( Dialog::mbHelp & messageButtons )  {						button->setCommandType( BC_HELP );						button->setCaption( "Help" );					}				}				break;			}			button->setBounds( x, y, buttonWidth, button->getPreferredHeight() );			add( button );			x += buttonWidth + spacer;		}		maxWidth = maxVal<double>( x , maxWidth );		height += button->getHeight() + toolkit->getUIMetricsManager()->getPreferredSpacingFor( UIMetricsManager::stContainerBorderDelta );		setHeight( height );		setWidth( maxWidth );	}};X11Dialog::X11Dialog( Control* owner, Dialog* component ) :	owner_(owner),	runningModal_(false){	control_ = component;	X11GraphicsToolkit* grafToolkit = reinterpret_cast<X11GraphicsToolkit*>(GraphicsToolkit::getDefaultGraphicsToolkit());	X11UIToolkit* toolkit = reinterpret_cast<X11UIToolkit*>( UIToolkit::getDefaultUIToolkit() );	Display* display = grafToolkit->getX11Display();	wmProtocols_ = XInternAtom( display, "WM_PROTOCOLS", False );	XSetWindowAttributes attrs;	memset( &attrs, 0, sizeof(XSetWindowAttributes) );	attrs.background_pixmap = None;	attrs.background_pixel = 0;	attrs.border_pixmap = CopyFromParent;	attrs.border_pixel = 0;	attrs.bit_gravity = ForgetGravity;	attrs.win_gravity = NorthWestGravity;	attrs.backing_store = NotUseful;	attrs.colormap = CopyFromParent;	attrs.backing_planes = 0xffffffff;	attrs.cursor = None;	attrs.override_redirect = true;	int createWndAttrMask = 0;	wndHandle_ = XCreateWindow( display,										DefaultRootWindow( display ),										0, 0, 1, 1, 0,										CopyFromParent,										InputOutput,										CopyFromParent,										createWndAttrMask,										&attrs );	if ( NULL != wndHandle_ ) {		int eventMask = ExposureMask | KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask;		eventMask |= FocusChangeMask | PropertyChangeMask | StructureNotifyMask | EnterWindowMask | LeaveWindowMask;		eventMask |= PointerMotionMask | Button1MotionMask | Button2MotionMask | Button3MotionMask;		eventMask |= VisibilityChangeMask;// | SubstructureNotifyMask;		Atom deleteWndMsgAtom = toolkit->getDeleteWindowMsg();		XSetWMProtocols( display, wndHandle_, &deleteWndMsgAtom, 1);		xLib::Window mainWndHandle = NULL;		Application* app = Application::getRunningInstance();		if ( NULL != app ) {			Window* mainWind = app->getMainWindow();			mainWndHandle = (xLib::Window) mainWind->getPeer()->getHandleID();		}		if ( NULL != mainWndHandle ) {			XSetTransientForHint( display, wndHandle_, mainWndHandle );		}

⌨️ 快捷键说明

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