]> git.saurik.com Git - wxWidgets.git/blame - src/common/gdicmn.cpp
added SetRecordDefaults() and IsRecordingDefaults() methods
[wxWidgets.git] / src / common / gdicmn.cpp
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: gdicmn.cpp
3// Purpose: Common GDI classes
4// Author: Julian Smart
5// Modified by:
6// Created: 01/02/97
7// RCS-ID: $Id$
8// Copyright: (c) Julian Smart and Markus Holzem
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
13#pragma implementation "gdicmn.h"
14#endif
15
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
18
19#ifdef __BORLANDC__
20#pragma hdrstop
21#endif
22
23#include "wx/gdicmn.h"
24#include "wx/brush.h"
25#include "wx/pen.h"
26#include "wx/bitmap.h"
27#include "wx/icon.h"
28#include "wx/cursor.h"
29#include "wx/font.h"
30#include "wx/palette.h"
31
32#include <string.h>
33
2049ba38 34#ifdef __WXMSW__
c801d85f
KB
35#include <windows.h>
36#endif
37
38#if !USE_SHARED_LIBRARY
c801d85f
KB
39IMPLEMENT_CLASS(wxColourDatabase, wxList)
40IMPLEMENT_DYNAMIC_CLASS(wxFontList, wxList)
41IMPLEMENT_DYNAMIC_CLASS(wxPenList, wxList)
42IMPLEMENT_DYNAMIC_CLASS(wxBrushList, wxList)
43IMPLEMENT_DYNAMIC_CLASS(wxBitmapList, wxList)
6a6c0a8b
JS
44/*
45IMPLEMENT_DYNAMIC_CLASS(wxRect, wxObject)
c801d85f
KB
46IMPLEMENT_DYNAMIC_CLASS(wxPoint, wxObject)
47IMPLEMENT_DYNAMIC_CLASS(wxRealPoint, wxObject)
6a6c0a8b 48*/
c801d85f
KB
49#endif
50
6a6c0a8b 51wxRect::wxRect()
c801d85f
KB
52{
53 x = 0; y = 0; width = 0; height = 0;
54}
55
debe6624 56wxRect::wxRect(long xx, long yy, long w, long h)
c801d85f
KB
57{
58 x = xx; y = yy; width = w; height = h;
59}
60
61wxRect::wxRect(const wxPoint& topLeft, const wxPoint& bottomRight)
62{
63 x = topLeft.x;
64 y = topLeft.y;
65 width = bottomRight.x - topLeft.x;
66 height = bottomRight.y - topLeft.y;
67
68 if (width < 0)
69 {
70 width = -width;
71 x -= width;
72 }
73
74 if (height < 0)
75 {
76 height = -height;
77 x -= height;
78 }
79}
80
81wxRect::wxRect(const wxPoint& point, const wxSize& size)
82{
83 x = point.x; y = point.y;
84 width = size.x; height = size.y;
85}
86
87wxRect::wxRect(const wxRect& rect)
88{
89 x = rect.x;
90 y = rect.y;
91 width = rect.width;
92 height = rect.height;
93}
94
95wxRect& wxRect::operator = (const wxRect& rect)
96{
97 x = rect.x; y = rect.y; width = rect.width; height = rect.height;
98 return *this;
99}
100
101bool wxRect::operator == (const wxRect& rect)
102{
103 return ((x == rect.x) &&
104 (y == rect.y) &&
105 (width == rect.width) &&
106 (height == rect.height));
107}
108
109bool wxRect::operator != (const wxRect& rect)
110{
111 return ((x != rect.x) ||
112 (y != rect.y) ||
113 (width != rect.width) ||
114 (height != rect.height));
115}
116
117wxColourDatabase::wxColourDatabase (int type):
118wxList (type)
119{
120}
121
6a6c0a8b 122wxColourDatabase::~wxColourDatabase ()
c801d85f
KB
123{
124 // Cleanup Colour allocated in Initialize()
125 wxNode *node = First ();
126 while (node)
127 {
128 wxColour *col = (wxColour *) node->Data ();
129 wxNode *next = node->Next ();
130 delete col;
131 node = next;
132 }
133}
134
135// Colour database stuff
6a6c0a8b 136void wxColourDatabase::Initialize ()
c801d85f
KB
137{
138 // Don't initialize for X: colours are found
139 // in FindColour below.
140 // Added: Not all
141
142 struct cdef {
143 char *name;
144 int r,g,b;
145 };
146 cdef cc;
147 static cdef table[]={
148
66bd6b93 149// #ifdef __WXMSW__
c801d85f
KB
150 {"AQUAMARINE",112, 219, 147},
151 {"BLACK",0, 0, 0},
152 {"BLUE", 0, 0, 255},
153 {"BLUE VIOLET", 159, 95, 159},
154 {"BROWN", 165, 42, 42},
155 {"CADET BLUE", 95, 159, 159},
156 {"CORAL", 255, 127, 0},
157 {"CORNFLOWER BLUE", 66, 66, 111},
158 {"CYAN", 0, 255, 255},
159 {"DARK GREY", 47, 47, 47}, // ?
160
161 {"DARK GREEN", 47, 79, 47},
162 {"DARK OLIVE GREEN", 79, 79, 47},
163 {"DARK ORCHID", 153, 50, 204},
164 {"DARK SLATE BLUE", 107, 35, 142},
165 {"DARK SLATE GREY", 47, 79, 79},
166 {"DARK TURQUOISE", 112, 147, 219},
167 {"DIM GREY", 84, 84, 84},
168 {"FIREBRICK", 142, 35, 35},
169 {"FOREST GREEN", 35, 142, 35},
170 {"GOLD", 204, 127, 50},
171 {"GOLDENROD", 219, 219, 112},
172 {"GREY", 128, 128, 128},
173 {"GREEN", 0, 255, 0},
174 {"GREEN YELLOW", 147, 219, 112},
175 {"INDIAN RED", 79, 47, 47},
176 {"KHAKI", 159, 159, 95},
177 {"LIGHT BLUE", 191, 216, 216},
178 {"LIGHT GREY", 192, 192, 192},
179 {"LIGHT STEEL BLUE", 143, 143, 188},
180 {"LIME GREEN", 50, 204, 50},
181 {"LIGHT MAGENTA", 255, 0, 255},
182 {"MAGENTA", 255, 0, 255},
183 {"MAROON", 142, 35, 107},
184 {"MEDIUM AQUAMARINE", 50, 204, 153},
185 {"MEDIUM GREY", 100, 100, 100},
186 {"MEDIUM BLUE", 50, 50, 204},
187 {"MEDIUM FOREST GREEN", 107, 142, 35},
188 {"MEDIUM GOLDENROD", 234, 234, 173},
189 {"MEDIUM ORCHID", 147, 112, 219},
190 {"MEDIUM SEA GREEN", 66, 111, 66},
191 {"MEDIUM SLATE BLUE", 127, 0, 255},
192 {"MEDIUM SPRING GREEN", 127, 255, 0},
193 {"MEDIUM TURQUOISE", 112, 219, 219},
194 {"MEDIUM VIOLET RED", 219, 112, 147},
195 {"MIDNIGHT BLUE", 47, 47, 79},
196 {"NAVY", 35, 35, 142},
197 {"ORANGE", 204, 50, 50},
198 {"ORANGE RED", 255, 0, 127},
199 {"ORCHID", 219, 112, 219},
200 {"PALE GREEN", 143, 188, 143},
201 {"PINK", 188, 143, 234},
202 {"PLUM", 234, 173, 234},
203 {"PURPLE", 176, 0, 255},
204 {"RED", 255, 0, 0},
205 {"SALMON", 111, 66, 66},
206 {"SEA GREEN", 35, 142, 107},
207 {"SIENNA", 142, 107, 35},
208 {"SKY BLUE", 50, 153, 204},
209 {"SLATE BLUE", 0, 127, 255},
210 {"SPRING GREEN", 0, 255, 127},
211 {"STEEL BLUE", 35, 107, 142},
212 {"TAN", 219, 147, 112},
213 {"THISTLE", 216, 191, 216},
214 {"TURQUOISE", 173, 234, 234},
215 {"VIOLET", 79, 47, 79},
216 {"VIOLET RED", 204, 50, 153},
217 {"WHEAT", 216, 216, 191},
218 {"WHITE", 255, 255, 255},
219 {"YELLOW", 255, 255, 0},
220 {"YELLOW GREEN", 153, 204, 50},
66bd6b93 221// #endif
c801d85f 222
2049ba38 223#if defined(__WXGTK__) || defined(__X__)
c801d85f
KB
224 {"MEDIUM GOLDENROD", 234, 234, 173},
225 {"MEDIUM FOREST GREEN", 107, 142, 35},
226 {"LIGHT MAGENTA", 255, 0, 255},
227 {"MEDIUM GREY", 100, 100, 100},
228#endif
229
230 {0,0,0,0}
231 };
232 int i;
233 for (i=0;cc=table[i],cc.name!=0;i++)
234 {
235 Append(cc.name,new wxColour(cc.r,cc.g,cc.b));
236 }
237
238}
239
240/*
241 * Changed by Ian Brown, July 1994.
242 *
243 * When running under X, the Colour Database starts off empty. The X server
244 * is queried for the colour first time after which it is entered into the
245 * database. This allows our client to use the server colour database which
246 * is hopefully gamma corrected for the display being used.
247 */
248
249wxColour *wxColourDatabase::FindColour(const wxString& colour)
250{
251 wxNode *node = Find((char *) (const char *)colour);
252 if (node)
253 return (wxColour *)node->Data();
254
2049ba38 255#ifdef __WXMSW__
c801d85f
KB
256 else return NULL;
257#endif
258
2049ba38 259#ifdef __WXGTK__
c801d85f
KB
260 else {
261 wxColour *col = new wxColour( colour );
262
263 if (!(col->Ok())) {
264 delete col;
265 return NULL;
266 }
267 Append( colour, col );
268 return col;
269 }
270#endif
271
272#ifdef __X__
273 else {
274 XColor xcolour;
275
2049ba38 276#ifdef __WXMOTIF__
c801d85f
KB
277 Display *display = XtDisplay(wxTheApp->topLevel) ;
278#endif
279#ifdef __XVIEW__
280 Xv_Screen screen = xv_get(xview_server, SERVER_NTH_SCREEN, 0);
281 Xv_opaque root_window = xv_get(screen, XV_ROOT);
282 Display *display = (Display *)xv_get(root_window, XV_DISPLAY);
283#endif
284
285 /* MATTHEW: [4] Use wxGetMainColormap */
286 if (!XParseColor(display, wxGetMainColormap(display), colour,&xcolour))
287 return NULL;
288
289 unsigned char r = (unsigned char)(xcolour.red >> 8);
290 unsigned char g = (unsigned char)(xcolour.green >> 8);
291 unsigned char b = (unsigned char)(xcolour.blue >> 8);
292
293 wxColour *col = new wxColour(r, g, b);
294 Append(colour, col);
295
296 return col;
297 }
298#endif
299}
300
301wxString wxColourDatabase::FindName (const wxColour& colour) const
302{
303 unsigned char red = colour.Red ();
304 unsigned char green = colour.Green ();
305 unsigned char blue = colour.Blue ();
66bd6b93 306
c801d85f
KB
307 for (wxNode * node = First (); node; node = node->Next ())
308 {
309 wxColour *col = (wxColour *) node->Data ();
66bd6b93 310
c801d85f
KB
311 if (col->Red () == red && col->Green () == green && col->Blue () == blue)
312 {
313 char *found = node->key.string;
314 if (found)
315 return wxString(found);
316 }
317 }
318 return wxString(""); // Not Found
319
320}
321
322void
6a6c0a8b 323wxInitializeStockObjects ()
c801d85f
KB
324{
325 wxTheBrushList = new wxBrushList;
326 wxThePenList = new wxPenList;
327 wxTheFontList = new wxFontList;
328 wxTheBitmapList = new wxBitmapList;
329
2049ba38 330#ifdef __WXMOTIF__
c801d85f
KB
331#endif
332#ifdef __X__
333 wxFontPool = new XFontPool;
334#endif
335
336 wxNORMAL_FONT = new wxFont (12, wxMODERN, wxNORMAL, wxNORMAL);
337 wxSMALL_FONT = new wxFont (10, wxSWISS, wxNORMAL, wxNORMAL);
338 wxITALIC_FONT = new wxFont (12, wxROMAN, wxITALIC, wxNORMAL);
339 wxSWISS_FONT = new wxFont (12, wxSWISS, wxNORMAL, wxNORMAL);
340
341 wxRED_PEN = new wxPen ("RED", 1, wxSOLID);
342 wxCYAN_PEN = new wxPen ("CYAN", 1, wxSOLID);
343 wxGREEN_PEN = new wxPen ("GREEN", 1, wxSOLID);
344 wxBLACK_PEN = new wxPen ("BLACK", 1, wxSOLID);
345 wxWHITE_PEN = new wxPen ("WHITE", 1, wxSOLID);
346 wxTRANSPARENT_PEN = new wxPen ("BLACK", 1, wxTRANSPARENT);
347 wxBLACK_DASHED_PEN = new wxPen ("BLACK", 1, wxSHORT_DASH);
348 wxGREY_PEN = new wxPen ("GREY", 1, wxSOLID);
349 wxMEDIUM_GREY_PEN = new wxPen ("MEDIUM GREY", 1, wxSOLID);
350 wxLIGHT_GREY_PEN = new wxPen ("LIGHT GREY", 1, wxSOLID);
351
352 wxBLUE_BRUSH = new wxBrush ("BLUE", wxSOLID);
353 wxGREEN_BRUSH = new wxBrush ("GREEN", wxSOLID);
354 wxWHITE_BRUSH = new wxBrush ("WHITE", wxSOLID);
355 wxBLACK_BRUSH = new wxBrush ("BLACK", wxSOLID);
356 wxTRANSPARENT_BRUSH = new wxBrush ("BLACK", wxTRANSPARENT);
357 wxCYAN_BRUSH = new wxBrush ("CYAN", wxSOLID);
358 wxRED_BRUSH = new wxBrush ("RED", wxSOLID);
359 wxGREY_BRUSH = new wxBrush ("GREY", wxSOLID);
360 wxMEDIUM_GREY_BRUSH = new wxBrush ("MEDIUM GREY", wxSOLID);
361 wxLIGHT_GREY_BRUSH = new wxBrush ("LIGHT GREY", wxSOLID);
362
363 wxBLACK = new wxColour ("BLACK");
364 wxWHITE = new wxColour ("WHITE");
365 wxRED = new wxColour ("RED");
366 wxBLUE = new wxColour ("BLUE");
367 wxGREEN = new wxColour ("GREEN");
368 wxCYAN = new wxColour ("CYAN");
369 wxLIGHT_GREY = new wxColour ("LIGHT GREY");
370
371 wxSTANDARD_CURSOR = new wxCursor (wxCURSOR_ARROW);
372 wxHOURGLASS_CURSOR = new wxCursor (wxCURSOR_WAIT);
373 wxCROSS_CURSOR = new wxCursor (wxCURSOR_CROSS);
374}
375
376void
6a6c0a8b 377wxDeleteStockObjects ()
c801d85f
KB
378{
379 DELETEP(wxNORMAL_FONT);
380 DELETEP(wxSMALL_FONT);
381 DELETEP(wxITALIC_FONT);
382 DELETEP(wxSWISS_FONT);
383
384 DELETEP(wxRED_PEN);
385 DELETEP(wxCYAN_PEN);
386 DELETEP(wxGREEN_PEN);
387 DELETEP(wxBLACK_PEN);
388 DELETEP(wxWHITE_PEN);
389 DELETEP(wxTRANSPARENT_PEN);
390 DELETEP(wxBLACK_DASHED_PEN);
391 DELETEP(wxGREY_PEN);
392 DELETEP(wxMEDIUM_GREY_PEN);
393 DELETEP(wxLIGHT_GREY_PEN);
394
395 DELETEP(wxBLUE_BRUSH);
396 DELETEP(wxGREEN_BRUSH);
397 DELETEP(wxWHITE_BRUSH);
398 DELETEP(wxBLACK_BRUSH);
399 DELETEP(wxTRANSPARENT_BRUSH);
400 DELETEP(wxCYAN_BRUSH);
401 DELETEP(wxRED_BRUSH);
402 DELETEP(wxGREY_BRUSH);
403 DELETEP(wxMEDIUM_GREY_BRUSH);
404 DELETEP(wxLIGHT_GREY_BRUSH);
405
406 DELETEP(wxBLACK);
407 DELETEP(wxWHITE);
408 DELETEP(wxRED);
409 DELETEP(wxBLUE);
410 DELETEP(wxGREEN);
411 DELETEP(wxCYAN);
412 DELETEP(wxLIGHT_GREY);
413
414 DELETEP(wxSTANDARD_CURSOR);
415 DELETEP(wxHOURGLASS_CURSOR);
416 DELETEP(wxCROSS_CURSOR);
417}
418
6a6c0a8b 419wxBitmapList::wxBitmapList ()
c801d85f
KB
420{
421}
422
6a6c0a8b 423wxBitmapList::~wxBitmapList ()
c801d85f
KB
424{
425 wxNode *node = First ();
426 while (node)
427 {
428 wxBitmap *bitmap = (wxBitmap *) node->Data ();
429 wxNode *next = node->Next ();
430 delete bitmap;
431// bitmap->FreeResource(TRUE);
432 node = next;
433 }
434}
435
436// Pen and Brush lists
6a6c0a8b 437wxPenList::~wxPenList ()
c801d85f
KB
438{
439 wxNode *node = First ();
440 while (node)
441 {
442 wxPen *pen = (wxPen *) node->Data ();
443 wxNode *next = node->Next ();
444 delete pen;
445// pen->FreeResource(TRUE);
446 node = next;
447 }
448}
449
450void wxPenList::AddPen (wxPen * pen)
451{
452 Append (pen);
453}
454
455void wxPenList::RemovePen (wxPen * pen)
456{
457 DeleteObject (pen);
458}
459
debe6624 460wxPen *wxPenList::FindOrCreatePen (const wxColour& colour, int width, int style)
c801d85f
KB
461{
462 for (wxNode * node = First (); node; node = node->Next ())
463 {
464 wxPen *each_pen = (wxPen *) node->Data ();
465 if (each_pen && each_pen->GetVisible() &&
466 each_pen->GetWidth () == width &&
467 each_pen->GetStyle () == style &&
468 each_pen->GetColour ().Red () == colour.Red () &&
469 each_pen->GetColour ().Green () == colour.Green () &&
470 each_pen->GetColour ().Blue () == colour.Blue ())
471 return each_pen;
472 }
473 wxPen *pen = new wxPen (colour, width, style);
474
475 // Yes, we can return a pointer to this in a later FindOrCreatePen call,
476 // because we created it within FindOrCreatePen. Safeguards against
477 // returning a pointer to an automatic variable and hanging on to it
478 // (dangling pointer).
479 pen->SetVisible(TRUE);
480 return pen;
481}
482
debe6624 483wxPen *wxPenList::FindOrCreatePen (const wxString& colour, int width, int style)
c801d85f
KB
484{
485 wxColour *the_colour = wxTheColourDatabase->FindColour (colour);
486 if (the_colour)
487 return FindOrCreatePen (*the_colour, width, style);
488 else
489 return NULL;
490}
491
6a6c0a8b 492wxBrushList::~wxBrushList ()
c801d85f
KB
493{
494 wxNode *node = First ();
495 while (node)
496 {
497 wxBrush *brush = (wxBrush *) node->Data ();
498 wxNode *next = node->Next ();
499 delete brush;
500 node = next;
501 }
502}
503
504void wxBrushList::AddBrush (wxBrush * brush)
505{
506 Append (brush);
507}
508
debe6624 509wxBrush *wxBrushList::FindOrCreateBrush (const wxColour& colour, int style)
c801d85f
KB
510{
511 for (wxNode * node = First (); node; node = node->Next ())
512 {
513 wxBrush *each_brush = (wxBrush *) node->Data ();
514 if (each_brush && each_brush->GetVisible() &&
515 each_brush->GetStyle () == style &&
516 each_brush->GetColour ().Red () == colour.Red () &&
517 each_brush->GetColour ().Green () == colour.Green () &&
518 each_brush->GetColour ().Blue () == colour.Blue ())
519 return each_brush;
520 }
521 // Yes, we can return a pointer to this in a later FindOrCreateBrush call,
522 // because we created it within FindOrCreateBrush. Safeguards against
523 // returning a pointer to an automatic variable and hanging on to it
524 // (dangling pointer).
525 wxBrush *brush = new wxBrush (colour, style);
526 brush->SetVisible(TRUE);
527 return brush;
528}
529
debe6624 530wxBrush *wxBrushList::FindOrCreateBrush (const wxString& colour, int style)
c801d85f
KB
531{
532 wxColour *the_colour = wxTheColourDatabase->FindColour (colour);
533 if (the_colour)
534 return FindOrCreateBrush (*the_colour, style);
535 else
536 return NULL;
537}
538
539void wxBrushList::RemoveBrush (wxBrush * brush)
540{
541 DeleteObject (brush);
542}
543
6a6c0a8b 544wxFontList::~wxFontList ()
c801d85f 545{
2049ba38 546#ifdef __WXMSW__
c801d85f
KB
547 wxNode *node = First ();
548 while (node)
549 {
550/*
551 wxFont *font = (wxFont *) node->Data ();
552 wxNode *next = node->Next ();
553 delete font;
554 node = next;
555*/
556 // New for 2.0: don't delete the font (it may be a member
557 // of a wxDC, for example)
558 wxFont *font = (wxFont *) node->Data ();
559 wxNode *next = node->Next ();
560
561 // Force the font to be deleted
562 font->FreeResource(TRUE);
563 node = next;
564 }
565#endif
566}
567
568void wxFontList::AddFont (wxFont * font)
569{
570 Append (font);
571}
572
573void wxFontList::RemoveFont (wxFont * font)
574{
575 DeleteObject (font);
576}
577
578wxFont *wxFontList::
debe6624 579 FindOrCreateFont (int PointSize, int FamilyOrFontId, int Style, int Weight, bool underline, const wxString& Face)
c801d85f
KB
580{
581 for (wxNode * node = First (); node; node = node->Next ())
582 {
583 wxFont *each_font = (wxFont *) node->Data ();
584 if (each_font && each_font->GetVisible() && each_font->Ok() &&
585 each_font->GetPointSize () == PointSize &&
586 each_font->GetStyle () == Style &&
587 each_font->GetWeight () == Weight &&
588 each_font->GetUnderlined () == underline &&
2049ba38 589#if defined(__X__) || (defined(__WXMSW__) && USE_PORTABLE_FONTS_IN_MSW)
c801d85f
KB
590 each_font->GetFontId () == FamilyOrFontId) /* New font system */
591#else
592 each_font->GetFamily () == FamilyOrFontId &&
593 (!each_font->GetFaceName() || each_font->GetFaceName() == Face))
594#endif
595 return each_font;
596 }
597 wxFont *font = new wxFont (PointSize, FamilyOrFontId, Style, Weight, underline, Face);
598 font->SetVisible(TRUE);
599 return font;
600}
601
602void wxBitmapList::AddBitmap(wxBitmap *bitmap)
603{ Append(bitmap); }
604void wxBitmapList::RemoveBitmap(wxBitmap *bitmap)
605{ DeleteObject(bitmap); }
606
6a6c0a8b
JS
607wxSize wxGetDisplaySize()
608{
609 int x, y;
610 wxDisplaySize(& x, & y);
611 return wxSize(x, y);
612}
613