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