📄 oglmisc.cpp
字号:
/////////////////////////////////////////////////////////////////////////////
// Name: misc.cpp
// Purpose: Miscellaneous OGL support functions
// Author: Julian Smart
// Modified by:
// Created: 12/07/98
// RCS-ID: $Id: oglmisc.cpp,v 1.21 2005/10/06 18:17:23 ABX Exp $
// Copyright: (c) Julian Smart
// Licence: wxWindows licence
/////////////////////////////////////////////////////////////////////////////
// For compilers that support precompilation, includes "wx.h".
#include "wx/wxprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif
#if wxUSE_PROLOGIO
#include "wx/deprecated/wxexpr.h"
#endif
#include "wx/types.h"
#ifdef new
#undef new
#endif
#include <ctype.h>
#include <math.h>
#include <stdlib.h>
#include "wx/ogl/ogl.h"
wxFont* g_oglNormalFont;
wxPen* g_oglBlackPen;
wxPen* g_oglWhiteBackgroundPen;
wxPen* g_oglTransparentPen;
wxBrush* g_oglWhiteBackgroundBrush;
wxPen* g_oglBlackForegroundPen;
wxCursor* g_oglBullseyeCursor = NULL;
wxChar* oglBuffer = NULL;
wxList oglObjectCopyMapping(wxKEY_INTEGER);
void wxOGLInitialize()
{
g_oglBullseyeCursor = new wxCursor(wxCURSOR_BULLSEYE);
g_oglNormalFont = new wxFont(10, wxSWISS, wxNORMAL, wxNORMAL);
g_oglBlackPen = new wxPen(wxT("BLACK"), 1, wxSOLID);
g_oglWhiteBackgroundPen = new wxPen(wxT("WHITE"), 1, wxSOLID);
g_oglTransparentPen = new wxPen(wxT("WHITE"), 1, wxTRANSPARENT);
g_oglWhiteBackgroundBrush = new wxBrush(wxT("WHITE"), wxSOLID);
g_oglBlackForegroundPen = new wxPen(wxT("BLACK"), 1, wxSOLID);
OGLInitializeConstraintTypes();
// Initialize big buffer used when writing images
oglBuffer = new wxChar[3000];
}
void wxOGLCleanUp()
{
if (oglBuffer)
{
delete[] oglBuffer;
oglBuffer = NULL;
}
oglBuffer = NULL;
if (g_oglBullseyeCursor)
{
delete g_oglBullseyeCursor;
g_oglBullseyeCursor = NULL;
}
if (g_oglNormalFont)
{
delete g_oglNormalFont;
g_oglNormalFont = NULL;
}
if (g_oglBlackPen)
{
delete g_oglBlackPen;
g_oglBlackPen = NULL;
}
if (g_oglWhiteBackgroundPen)
{
delete g_oglWhiteBackgroundPen;
g_oglWhiteBackgroundPen = NULL;
}
if (g_oglTransparentPen)
{
delete g_oglTransparentPen;
g_oglTransparentPen = NULL;
}
if (g_oglWhiteBackgroundBrush)
{
delete g_oglWhiteBackgroundBrush;
g_oglWhiteBackgroundBrush = NULL;
}
if (g_oglBlackForegroundPen)
{
delete g_oglBlackForegroundPen;
g_oglBlackForegroundPen = NULL;
}
OGLCleanUpConstraintTypes();
}
wxFont *oglMatchFont(int point_size)
{
wxFont *font = wxTheFontList->FindOrCreateFont(point_size, wxSWISS, wxNORMAL, wxNORMAL);
#if 0
switch (point_size)
{
case 4:
font = swiss_font_4;
break;
case 6:
font = swiss_font_6;
break;
case 8:
font = swiss_font_8;
break;
case 12:
font = swiss_font_12;
break;
case 14:
font = swiss_font_14;
break;
case 18:
font = swiss_font_18;
break;
case 24:
font = swiss_font_24;
break;
default:
case 10:
font = swiss_font_10;
break;
}
#endif
return font;
}
int FontSizeDialog(wxFrame *parent, int old_size)
{
if (old_size <= 0)
old_size = 10;
wxString buf;
buf << old_size;
wxString ans = wxGetTextFromUser(wxT("Enter point size"), wxT("Font size"), buf, parent);
if (ans.Length() == 0)
return 0;
long new_size = 0;
ans.ToLong(&new_size);
if ((new_size <= 0) || (new_size > 40))
{
wxMessageBox(wxT("Invalid point size!"), wxT("Error"), wxOK);
return 0;
}
return new_size;
/*
char *strings[8];
strings[0] = "4";
strings[1] = "6";
strings[2] = "8";
strings[3] = "10";
strings[4] = "12";
strings[5] = "14";
strings[6] = "18";
strings[7] = "24";
char *ans = wxGetSingleChoice("Choose", "Choose a font size", 8, strings, parent);
if (ans)
{
int size;
sscanf(ans, "%d", &size);
return oglMatchFont(size);
}
else return NULL;
*/
}
// Centre a list of strings in the given box. xOffset and yOffset are the
// the positions that these lines should be relative to, and this might be
// the same as m_xpos, m_ypos, but might be zero if formatting from left-justifying.
void oglCentreText(wxDC& dc, wxList *text_list,
double m_xpos, double m_ypos, double width, double height,
int formatMode)
{
int n = text_list->GetCount();
if (!text_list || (n == 0))
return;
// First, get maximum dimensions of box enclosing text
long char_height = 0;
long max_width = 0;
long current_width = 0;
// Store text extents for speed
double *widths = new double[n];
wxObjectList::compatibility_iterator current = text_list->GetFirst();
int i = 0;
while (current)
{
wxShapeTextLine *line = (wxShapeTextLine *)current->GetData();
dc.GetTextExtent(line->GetText(), ¤t_width, &char_height);
widths[i] = current_width;
if (current_width > max_width)
max_width = current_width;
current = current->GetNext();
i ++;
}
double max_height = n*char_height;
double xoffset, yoffset, xOffset, yOffset;
if (formatMode & FORMAT_CENTRE_VERT)
{
if (max_height < height)
yoffset = (double)(m_ypos - (height/2.0) + (height - max_height)/2.0);
else
yoffset = (double)(m_ypos - (height/2.0));
yOffset = m_ypos;
}
else
{
yoffset = 0.0;
yOffset = 0.0;
}
if (formatMode & FORMAT_CENTRE_HORIZ)
{
xoffset = (double)(m_xpos - width/2.0);
xOffset = m_xpos;
}
else
{
xoffset = 0.0;
xOffset = 0.0;
}
current = text_list->GetFirst();
i = 0;
while (current)
{
wxShapeTextLine *line = (wxShapeTextLine *)current->GetData();
double x;
if ((formatMode & FORMAT_CENTRE_HORIZ) && (widths[i] < width))
x = (double)((width - widths[i])/2.0 + xoffset);
else
x = xoffset;
double y = (double)(i*char_height + yoffset);
line->SetX( x - xOffset ); line->SetY( y - yOffset );
current = current->GetNext();
i ++;
}
delete[] widths;
}
// Centre a list of strings in the given box
void oglCentreTextNoClipping(wxDC& dc, wxList *text_list,
double m_xpos, double m_ypos, double width, double height)
{
int n = text_list->GetCount();
if (!text_list || (n == 0))
return;
// First, get maximum dimensions of box enclosing text
long char_height = 0;
long max_width = 0;
long current_width = 0;
// Store text extents for speed
double *widths = new double[n];
wxObjectList::compatibility_iterator current = text_list->GetFirst();
int i = 0;
while (current)
{
wxShapeTextLine *line = (wxShapeTextLine *)current->GetData();
dc.GetTextExtent(line->GetText(), ¤t_width, &char_height);
widths[i] = current_width;
if (current_width > max_width)
max_width = current_width;
current = current->GetNext();
i ++;
}
double max_height = n*char_height;
double yoffset = (double)(m_ypos - (height/2.0) + (height - max_height)/2.0);
double xoffset = (double)(m_xpos - width/2.0);
current = text_list->GetFirst();
i = 0;
while (current)
{
wxShapeTextLine *line = (wxShapeTextLine *)current->GetData();
double x = (double)((width - widths[i])/2.0 + xoffset);
double y = (double)(i*char_height + yoffset);
line->SetX( x - m_xpos ); line->SetY( y - m_ypos );
current = current->GetNext();
i ++;
}
delete widths;
}
void oglGetCentredTextExtent(wxDC& dc, wxList *text_list,
double WXUNUSED(m_xpos), double WXUNUSED(m_ypos), double WXUNUSED(width), double WXUNUSED(height),
double *actual_width, double *actual_height)
{
int n = text_list->GetCount();
if (!text_list || (n == 0))
{
*actual_width = 0;
*actual_height = 0;
return;
}
// First, get maximum dimensions of box enclosing text
long char_height = 0;
long max_width = 0;
long current_width = 0;
wxObjectList::compatibility_iterator current = text_list->GetFirst();
while (current)
{
wxShapeTextLine *line = (wxShapeTextLine *)current->GetData();
dc.GetTextExtent(line->GetText(), ¤t_width, &char_height);
if (current_width > max_width)
max_width = current_width;
current = current->GetNext();
}
*actual_height = n*char_height;
*actual_width = max_width;
}
// Format a string to a list of strings that fit in the given box.
// Interpret %n and 10 or 13 as a new line.
wxStringList *oglFormatText(wxDC& dc, const wxString& text, double width, double WXUNUSED(height), int formatMode)
{
// First, parse the string into a list of words
wxStringList word_list;
// Make new lines into NULL strings at this point
int i = 0; int j = 0; int len = text.Length();
wxChar word[400]; word[0] = 0;
bool end_word = false; bool new_line = false;
while (i < len)
{
switch (text[i])
{
case wxT('%'):
{
i ++;
if (i == len)
{ word[j] = wxT('%'); j ++; }
else
{
if (text[i] == wxT('n'))
{ new_line = true; end_word = true; i++; }
else
{ word[j] = wxT('%'); j ++; word[j] = text[i]; j ++; i ++; }
}
break;
}
case 10:
{
new_line = true; end_word = true; i++;
break;
}
case 13:
{
new_line = true; end_word = true; i++;
break;
}
case wxT(' '):
{
end_word = true;
i ++;
break;
}
default:
{
word[j] = text[i];
j ++; i ++;
break;
}
}
if (i == len) end_word = true;
if (end_word)
{
word[j] = 0;
j = 0;
word_list.Add(word);
end_word = false;
}
if (new_line)
{
word_list.Append(NULL);
new_line = false;
}
}
// Now, make a list of strings which can fit in the box
wxStringList *string_list = new wxStringList;
wxString buffer;
wxStringList::compatibility_iterator node = word_list.GetFirst();
long x, y;
while (node)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -