/* * MathPlanner 3.0 - Mathematical design tool. * Copyright(C) 2002 Jarmo Nikkanen * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the * Free Software Foundation. * * You should have received a copy of the GNU General Public License with this program. * */ #include "Header.h" #include "Error.h" #include #include // DISPLAY BOX void m_display_box::SetID(int i) { id=i; } void m_display_box::SetParent(m_base_object *o) { parent=o; } void m_display_box::SetReadOnly(bool r) { ro=r; } bool m_display_box::isReadOnly() { return(ro); } void m_display_box::FeedBack(key_code key) { if (parent) parent->KeyFeedback(key); else Bug("No Parent on display box"); } void m_display_box::Focus(bool f) { focus=f; } void m_display_box::MoveTo(QPoint p) { bounds.SetPosition(p); } void m_display_box::CalculateBounds() { old_bounds=bounds; QFontMetrics met(parent->GetFont(id)); if (string->length()==0) { bounds.width=0; bounds.ascent=0; bounds.descent=0; if (focus==true && parent->HasFocus()) { bounds.width=met.width("X"); bounds.ascent=met.ascent(); bounds.descent=met.descent(); } return; } bounds.width=met.width(*string,string->length()); bounds.ascent=met.ascent(); bounds.descent=met.descent(); } void m_display_box::Draw(QPainter *paint,bool parent_focus,bool clear) { int wi; QPen pen(parent->GetColor(id),0,Qt::SolidLine); QPen pen3(QColor(200,0,0),0,Qt::SolidLine); QFont font=parent->GetFont(id); QFontMetrics met(font); paint->setFont(font); paint->setPen(pen); paint->drawText(bounds.Position(),*string); if (focus==true && parent_focus==true) { paint->setPen(parent->focus_pen); wi=met.width(*string,cursor); paint->drawRect(bounds.Rect()); paint->setPen(pen3); paint->drawLine(QPoint(wi+bounds.x,bounds.y-bounds.ascent+2), QPoint(wi+bounds.x,bounds.y+bounds.descent-2)); } } m_display_box::m_display_box(layout_rect b,QString *s,int byt) { string=s; bounds=b; c_mode=0; max_letters=byt; cursor=0; focus=false; ro=false; parent=NULL; id=1; } void m_display_box::KeyReceived(key_code key) { if (string && !ro) switch(key.code) { case Qt::Key_Left: if (cursor>0) { cursor--; //if (c_mode==1 && cursor==0) FeedBack(key); } else FeedBack(key); break; case Qt::Key_Right : if (cursor<(int)string->length()) cursor++; else FeedBack(key); break; case Qt::Key_Backspace : if (cursor>0) { cursor--; Bug("Back space start"); string->remove(cursor,1); Bug("Back space end"); } break; case Qt::Key_Delete : if ((int)string->length()<=cursor) FeedBack(key); else if (string->remove(cursor,1)==false) FeedBack(key); break; default: if (key.spec==false) { if ((int)string->length()>=max_letters) FeedBack(key); else string->insert(cursor,key.unicode),cursor++; } break; } if (cursor>max_letters) { cursor=max_letters; FeedBack(key); } }