~ubuntu-branches/ubuntu/hardy/codeblocks/hardy-backports

« back to all changes in this revision

Viewing changes to src/sdk/wxscintilla/src/scintilla/include/Platform.h

  • Committer: Bazaar Package Importer
  • Author(s): Michael Casadevall
  • Date: 2008-07-17 04:39:23 UTC
  • Revision ID: james.westby@ubuntu.com-20080717043923-gmsy5cwkdjswghkm
Tags: upstream-8.02
ImportĀ upstreamĀ versionĀ 8.02

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Scintilla source code edit control
 
2
/** @file Platform.h
 
3
 ** Interface to platform facilities. Also includes some basic utilities.
 
4
 ** Implemented in PlatGTK.cxx for GTK+/Linux, PlatWin.cxx for Windows, and PlatWX.cxx for wxWindows.
 
5
 **/
 
6
// Copyright 1998-2003 by Neil Hodgson <neilh@scintilla.org>
 
7
// The License.txt file describes the conditions under which this software may be distributed.
 
8
 
 
9
#ifndef PLATFORM_H
 
10
#define PLATFORM_H
 
11
 
 
12
// PLAT_GTK = GTK+ on Linux or Win32
 
13
// PLAT_GTK_WIN32 is defined additionally when running PLAT_GTK under Win32
 
14
// PLAT_WIN = Win32 API on Win32 OS
 
15
// PLAT_WX is wxWindows on any supported platform
 
16
 
 
17
#define PLAT_GTK 0
 
18
#define PLAT_GTK_WIN32 0
 
19
#define PLAT_WIN 0
 
20
#define PLAT_WX  0
 
21
#define PLAT_FOX 0
 
22
 
 
23
#if defined(FOX)
 
24
    #undef PLAT_FOX
 
25
    #define PLAT_FOX 1
 
26
 
 
27
#elif defined(__WX__)
 
28
    #undef PLAT_WX
 
29
    #define PLAT_WX  1
 
30
    #undef PLAT_WIN
 
31
    #define PLAT_WIN 1
 
32
 
 
33
#elif defined(GTK)
 
34
    #undef PLAT_GTK
 
35
    #define PLAT_GTK 1
 
36
    #undef PLAT_WX
 
37
    #define PLAT_WX  1
 
38
 
 
39
    #ifdef _MSC_VER
 
40
        #undef PLAT_GTK_WIN32
 
41
        #define PLAT_GTK_WIN32 1
 
42
    #endif
 
43
 
 
44
#else
 
45
    #undef PLAT_WIN
 
46
    #define PLAT_WIN 1
 
47
 
 
48
#endif
 
49
 
 
50
#include <wx/stopwatch.h>
 
51
 
 
52
// Underlying the implementation of the platform classes are platform specific types.
 
53
// Sometimes these need to be passed around by client code so they are defined here
 
54
 
 
55
typedef void *FontID;
 
56
typedef void *SurfaceID;
 
57
typedef void *WindowID;
 
58
typedef void *MenuID;
 
59
typedef void *TickerID;
 
60
typedef void *Function;
 
61
typedef void *IdlerID;
 
62
 
 
63
/**
 
64
 * A geometric point class.
 
65
 * Point is exactly the same as the Win32 POINT and GTK+ GdkPoint so can be used interchangeably.
 
66
 */
 
67
class Point {
 
68
public:
 
69
        int x;
 
70
        int y;
 
71
 
 
72
        explicit Point(int x_=0, int y_=0) : x(x_), y(y_) {
 
73
        }
 
74
 
 
75
        // Other automatically defined methods (assignment, copy constructor, destructor) are fine
 
76
 
 
77
        static Point FromLong(long lpoint);
 
78
};
 
79
 
 
80
/**
 
81
 * A geometric rectangle class.
 
82
 * PRectangle is exactly the same as the Win32 RECT so can be used interchangeably.
 
83
 * PRectangles contain their top and left sides, but not their right and bottom sides.
 
84
 */
 
85
class PRectangle {
 
86
public:
 
87
        int left;
 
88
        int top;
 
89
        int right;
 
90
        int bottom;
 
91
 
 
92
        PRectangle(int left_=0, int top_=0, int right_=0, int bottom_ = 0) :
 
93
                left(left_), top(top_), right(right_), bottom(bottom_) {
 
94
        }
 
95
 
 
96
        // Other automatically defined methods (assignment, copy constructor, destructor) are fine
 
97
 
 
98
        bool operator==(PRectangle &rc) {
 
99
                return (rc.left == left) && (rc.right == right) &&
 
100
                        (rc.top == top) && (rc.bottom == bottom);
 
101
        }
 
102
        bool Contains(Point pt) {
 
103
                return (pt.x >= left) && (pt.x <= right) &&
 
104
                        (pt.y >= top) && (pt.y <= bottom);
 
105
        }
 
106
        bool Contains(PRectangle rc) {
 
107
                return (rc.left >= left) && (rc.right <= right) &&
 
108
                        (rc.top >= top) && (rc.bottom <= bottom);
 
109
        }
 
110
        bool Intersects(PRectangle other) {
 
111
                return (right > other.left) && (left < other.right) &&
 
112
                        (bottom > other.top) && (top < other.bottom);
 
113
        }
 
114
        void Move(int xDelta, int yDelta) {
 
115
                left += xDelta;
 
116
                top += yDelta;
 
117
                right += xDelta;
 
118
                bottom += yDelta;
 
119
        }
 
120
        int Width() { return right - left; }
 
121
        int Height() { return bottom - top; }
 
122
};
 
123
 
 
124
/**
 
125
 * In some circumstances, including Win32 in paletted mode and GTK+, each colour
 
126
 * must be allocated before use. The desired colours are held in the ColourDesired class,
 
127
 * and after allocation the allocation entry is stored in the ColourAllocated class. In other
 
128
 * circumstances, such as Win32 in true colour mode, the allocation process just copies
 
129
 * the RGB values from the desired to the allocated class.
 
130
 * As each desired colour requires allocation before it can be used, the ColourPair class
 
131
 * holds both a ColourDesired and a ColourAllocated
 
132
 * The Palette class is responsible for managing the palette of colours which contains a
 
133
 * list of ColourPair objects and performs the allocation.
 
134
 */
 
135
 
 
136
/**
 
137
 * Holds a desired RGB colour.
 
138
 */
 
139
class ColourDesired {
 
140
        long co;
 
141
public:
 
142
        ColourDesired(long lcol=0) {
 
143
                co = lcol;
 
144
        }
 
145
 
 
146
        ColourDesired(unsigned int red, unsigned int green, unsigned int blue) {
 
147
                Set(red, green, blue);
 
148
        }
 
149
 
 
150
        bool operator==(const ColourDesired &other) const {
 
151
                return co == other.co;
 
152
        }
 
153
 
 
154
        void Set(long lcol) {
 
155
                co = lcol;
 
156
        }
 
157
 
 
158
        void Set(unsigned int red, unsigned int green, unsigned int blue) {
 
159
                co = red | (green << 8) | (blue << 16);
 
160
        }
 
161
 
 
162
        static inline unsigned int ValueOfHex(const char ch) {
 
163
                if (ch >= '0' && ch <= '9')
 
164
                        return ch - '0';
 
165
                else if (ch >= 'A' && ch <= 'F')
 
166
                        return ch - 'A' + 10;
 
167
                else if (ch >= 'a' && ch <= 'f')
 
168
                        return ch - 'a' + 10;
 
169
                else
 
170
                        return 0;
 
171
        }
 
172
 
 
173
        void Set(const char *val) {
 
174
                if (*val == '#') {
 
175
                        val++;
 
176
                }
 
177
                unsigned int r = ValueOfHex(val[0]) * 16 + ValueOfHex(val[1]);
 
178
                unsigned int g = ValueOfHex(val[2]) * 16 + ValueOfHex(val[3]);
 
179
                unsigned int b = ValueOfHex(val[4]) * 16 + ValueOfHex(val[5]);
 
180
                Set(r, g, b);
 
181
        }
 
182
 
 
183
        long AsLong() const {
 
184
                return co;
 
185
        }
 
186
 
 
187
        unsigned int GetRed() {
 
188
                return co & 0xff;
 
189
        }
 
190
 
 
191
        unsigned int GetGreen() {
 
192
                return (co >> 8) & 0xff;
 
193
        }
 
194
 
 
195
        unsigned int GetBlue() {
 
196
                return (co >> 16) & 0xff;
 
197
        }
 
198
};
 
199
 
 
200
/**
 
201
 * Holds an allocated RGB colour which may be an approximation to the desired colour.
 
202
 */
 
203
class ColourAllocated {
 
204
        long coAllocated;
 
205
 
 
206
public:
 
207
 
 
208
        ColourAllocated(long lcol=0) {
 
209
                coAllocated = lcol;
 
210
        }
 
211
 
 
212
        void Set(long lcol) {
 
213
                coAllocated = lcol;
 
214
        }
 
215
 
 
216
        long AsLong() const {
 
217
                return coAllocated;
 
218
        }
 
219
};
 
220
 
 
221
/**
 
222
 * Colour pairs hold a desired colour and an allocated colour.
 
223
 */
 
224
struct ColourPair {
 
225
        ColourDesired desired;
 
226
        ColourAllocated allocated;
 
227
 
 
228
        ColourPair(ColourDesired desired_=ColourDesired(0,0,0)) {
 
229
                desired = desired_;
 
230
                allocated.Set(desired.AsLong());
 
231
        }
 
232
        void Copy() {
 
233
                allocated.Set(desired.AsLong());
 
234
        }
 
235
};
 
236
 
 
237
class Window;   // Forward declaration for Palette
 
238
 
 
239
/**
 
240
 * Colour palette management.
 
241
 */
 
242
class Palette {
 
243
        int used;
 
244
        enum {numEntries = 100};
 
245
        ColourPair entries[numEntries];
 
246
#if PLAT_GTK
 
247
        void *allocatedPalette; // GdkColor *
 
248
        int allocatedLen;
 
249
#endif
 
250
public:
 
251
#if PLAT_WIN
 
252
        void *hpal;
 
253
#endif
 
254
        bool allowRealization;
 
255
 
 
256
        Palette();
 
257
        ~Palette();
 
258
 
 
259
        void Release();
 
260
 
 
261
        /**
 
262
         * This method either adds a colour to the list of wanted colours (want==true)
 
263
         * or retrieves the allocated colour back to the ColourPair.
 
264
         * This is one method to make it easier to keep the code for wanting and retrieving in sync.
 
265
         */
 
266
        void WantFind(ColourPair &cp, bool want);
 
267
 
 
268
        void Allocate(Window &w);
 
269
};
 
270
 
 
271
/**
 
272
 * Font management.
 
273
 */
 
274
class Font {
 
275
protected:
 
276
        FontID id;
 
277
#if PLAT_WX
 
278
        int ascent;
 
279
#endif
 
280
        // Private so Font objects can not be copied
 
281
        Font(const Font &) {}
 
282
        Font &operator=(const Font &) { id=0; return *this; }
 
283
public:
 
284
        Font();
 
285
        virtual ~Font();
 
286
 
 
287
        virtual void Create(const char *faceName, int characterSet, int size,
 
288
                bool bold, bool italic, bool extraFontFlag=false);
 
289
        virtual void Release();
 
290
 
 
291
        FontID GetID() { return id; }
 
292
        // Alias another font - caller guarantees not to Release
 
293
        void SetID(FontID id_) { id = id_; }
 
294
        friend class Surface;
 
295
        friend class SurfaceImpl;
 
296
};
 
297
 
 
298
/**
 
299
 * A surface abstracts a place to draw.
 
300
 */
 
301
class Surface {
 
302
private:
 
303
        // Private so Surface objects can not be copied
 
304
        Surface(const Surface &) {}
 
305
        Surface &operator=(const Surface &) { return *this; }
 
306
public:
 
307
        Surface() {};
 
308
        virtual ~Surface() {};
 
309
        static Surface *Allocate();
 
310
 
 
311
        virtual void Init(WindowID wid)=0;
 
312
        virtual void Init(SurfaceID sid, WindowID wid)=0;
 
313
        virtual void InitPixMap(int width, int height, Surface *surface_, WindowID wid)=0;
 
314
 
 
315
        virtual void Release()=0;
 
316
        virtual bool Initialised()=0;
 
317
        virtual void PenColour(ColourAllocated fore)=0;
 
318
        virtual int LogPixelsY()=0;
 
319
        virtual int DeviceHeightFont(int points)=0;
 
320
        virtual void MoveTo(int x_, int y_)=0;
 
321
        virtual void LineTo(int x_, int y_)=0;
 
322
        virtual void Polygon(Point *pts, int npts, ColourAllocated fore, ColourAllocated back)=0;
 
323
        virtual void RectangleDraw(PRectangle rc, ColourAllocated fore, ColourAllocated back)=0;
 
324
        virtual void FillRectangle(PRectangle rc, ColourAllocated back)=0;
 
325
        virtual void FillRectangle(PRectangle rc, Surface &surfacePattern)=0;
 
326
        virtual void RoundedRectangle(PRectangle rc, ColourAllocated fore, ColourAllocated back)=0;
 
327
        virtual void Ellipse(PRectangle rc, ColourAllocated fore, ColourAllocated back)=0;
 
328
        virtual void Copy(PRectangle rc, Point from, Surface &surfaceSource)=0;
 
329
 
 
330
        virtual void DrawTextNoClip(PRectangle rc, Font &font_, int ybase, const char *s, int len, ColourAllocated fore, ColourAllocated back)=0;
 
331
        virtual void DrawTextClipped(PRectangle rc, Font &font_, int ybase, const char *s, int len, ColourAllocated fore, ColourAllocated back)=0;
 
332
        virtual void DrawTextTransparent(PRectangle rc, Font &font_, int ybase, const char *s, int len, ColourAllocated fore)=0;
 
333
        virtual void MeasureWidths(Font &font_, const char *s, int len, int *positions)=0;
 
334
        virtual int WidthText(Font &font_, const char *s, int len)=0;
 
335
        virtual int WidthChar(Font &font_, char ch)=0;
 
336
        virtual int Ascent(Font &font_)=0;
 
337
        virtual int Descent(Font &font_)=0;
 
338
        virtual int InternalLeading(Font &font_)=0;
 
339
        virtual int ExternalLeading(Font &font_)=0;
 
340
        virtual int Height(Font &font_)=0;
 
341
        virtual int AverageCharWidth(Font &font_)=0;
 
342
 
 
343
        virtual int SetPalette(Palette *pal, bool inBackGround)=0;
 
344
        virtual void SetClip(PRectangle rc)=0;
 
345
        virtual void FlushCachedState()=0;
 
346
 
 
347
        virtual void SetUnicodeMode(bool unicodeMode_)=0;
 
348
        virtual void SetDBCSMode(int codePage)=0;
 
349
};
 
350
 
 
351
/**
 
352
 * A simple callback action passing one piece of untyped user data.
 
353
 */
 
354
typedef void (*CallBackAction)(void*);
 
355
 
 
356
/**
 
357
 * Class to hide the details of window manipulation.
 
358
 * Does not own the window which will normally have a longer life than this object.
 
359
 */
 
360
class Window {
 
361
protected:
 
362
        WindowID id;
 
363
public:
 
364
        Window() : id(0), cursorLast(cursorInvalid) {}
 
365
        Window(const Window &source) : id(source.id), cursorLast(cursorInvalid) {}
 
366
        virtual ~Window();
 
367
        Window &operator=(WindowID id_) {
 
368
                id = id_;
 
369
                return *this;
 
370
        }
 
371
        WindowID GetID() const { return id; }
 
372
        bool Created() const { return id != 0; }
 
373
        void Destroy();
 
374
        bool HasFocus();
 
375
        PRectangle GetPosition();
 
376
        void SetPosition(PRectangle rc);
 
377
        void SetPositionRelative(PRectangle rc, Window relativeTo);
 
378
        PRectangle GetClientPosition();
 
379
        void Show(bool show=true);
 
380
        void InvalidateAll();
 
381
        void InvalidateRectangle(PRectangle rc);
 
382
        virtual void SetFont(Font &font);
 
383
        enum Cursor { cursorInvalid, cursorText, cursorArrow, cursorUp, cursorWait, cursorHoriz, cursorVert, cursorReverseArrow, cursorHand };
 
384
        void SetCursor(Cursor curs);
 
385
        void SetTitle(const char *s);
 
386
private:
 
387
        Cursor cursorLast;
 
388
};
 
389
 
 
390
/**
 
391
 * Listbox management.
 
392
 */
 
393
 
 
394
class ListBox : public Window {
 
395
public:
 
396
        ListBox();
 
397
        virtual ~ListBox();
 
398
        static ListBox *Allocate();
 
399
 
 
400
        virtual void SetFont(Font &font)=0;
 
401
        virtual void Create(Window &parent, int ctrlID, Point location, int lineHeight_, bool unicodeMode_)=0;
 
402
        virtual void SetAverageCharWidth(int width)=0;
 
403
        virtual void SetVisibleRows(int rows)=0;
 
404
        virtual int GetVisibleRows() const=0;
 
405
        virtual PRectangle GetDesiredRect()=0;
 
406
        virtual int CaretFromEdge()=0;
 
407
        virtual void Clear()=0;
 
408
        virtual void Append(char *s, int type = -1)=0;
 
409
        virtual int Length()=0;
 
410
        virtual void Select(int n)=0;
 
411
        virtual int GetSelection()=0;
 
412
        virtual int Find(const char *prefix)=0;
 
413
        virtual void GetValue(int n, char *value, int len)=0;
 
414
        virtual void RegisterImage(int type, const char *xpm_data)=0;
 
415
        virtual void ClearRegisteredImages()=0;
 
416
        virtual void SetDoubleClickAction(CallBackAction, void *)=0;
 
417
        virtual void SetList(const char* list, char separator, char typesep)=0;
 
418
};
 
419
 
 
420
/**
 
421
 * Menu management.
 
422
 */
 
423
class Menu {
 
424
        MenuID id;
 
425
public:
 
426
        Menu();
 
427
        MenuID GetID() { return id; }
 
428
        void CreatePopUp();
 
429
        void Destroy();
 
430
        void Show(Point pt, Window &w);
 
431
};
 
432
 
 
433
class ElapsedTime {
 
434
        long bigBit;
 
435
        long littleBit;
 
436
public:
 
437
        ElapsedTime();
 
438
        double Duration(bool reset=false);
 
439
private:
 
440
    wxStopWatch m_StopWatch;
 
441
};
 
442
 
 
443
/**
 
444
 * Dynamic Library (DLL/SO/...) loading
 
445
 */
 
446
class DynamicLibrary {
 
447
public:
 
448
        virtual ~DynamicLibrary() {};
 
449
 
 
450
        /// @return Pointer to function "name", or NULL on failure.
 
451
        virtual Function FindFunction(const char *name) = 0;
 
452
 
 
453
        /// @return true if the library was loaded successfully.
 
454
        virtual bool IsValid() = 0;
 
455
 
 
456
        /// @return An instance of a DynamicLibrary subclass with "modulePath" loaded.
 
457
        static DynamicLibrary *Load(const char *modulePath);
 
458
};
 
459
 
 
460
/**
 
461
 * Platform class used to retrieve system wide parameters such as double click speed
 
462
 * and chrome colour. Not a creatable object, more of a module with several functions.
 
463
 */
 
464
class Platform {
 
465
        // Private so Platform objects can not be copied
 
466
        Platform(const Platform &) {}
 
467
        Platform &operator=(const Platform &) { return *this; }
 
468
public:
 
469
        // Should be private because no new Platforms are ever created
 
470
        // but gcc warns about this
 
471
        Platform() {}
 
472
        ~Platform() {}
 
473
        static ColourDesired Chrome();
 
474
        static ColourDesired ChromeHighlight();
 
475
        static const char *DefaultFont();
 
476
        static int DefaultFontSize();
 
477
        static unsigned int DoubleClickTime();
 
478
        static bool MouseButtonBounce();
 
479
        static void DebugDisplay(const char *s);
 
480
        static bool IsKeyDown(int key);
 
481
        static long SendScintilla(
 
482
                WindowID w, unsigned int msg, unsigned long wParam=0, long lParam=0);
 
483
        static long SendScintillaPointer(
 
484
                WindowID w, unsigned int msg, unsigned long wParam=0, void *lParam=0);
 
485
        static bool IsDBCSLeadByte(int codePage, char ch);
 
486
        static int DBCSCharLength(int codePage, const char *s);
 
487
        static int DBCSCharMaxLength();
 
488
 
 
489
        // These are utility functions not really tied to a platform
 
490
        static int Minimum(int a, int b);
 
491
        static int Maximum(int a, int b);
 
492
        // Next three assume 16 bit shorts and 32 bit longs
 
493
        static long LongFromTwoShorts(short a,short b) {
 
494
                return (a) | ((b) << 16);
 
495
        }
 
496
        static short HighShortFromLong(long x) {
 
497
                return static_cast<short>(x >> 16);
 
498
        }
 
499
        static short LowShortFromLong(long x) {
 
500
                return static_cast<short>(x & 0xffff);
 
501
        }
 
502
        static void DebugPrintf(const char *format, ...);
 
503
        static bool ShowAssertionPopUps(bool assertionPopUps_);
 
504
        static void Assert(const char *c, const char *file, int line);
 
505
        static int Clamp(int val, int minVal, int maxVal);
 
506
};
 
507
 
 
508
#ifdef  NDEBUG
 
509
#define PLATFORM_ASSERT(c) ((void)0)
 
510
#else
 
511
#define PLATFORM_ASSERT(c) ((c) ? (void)(0) : Platform::Assert(#c, __FILE__, __LINE__))
 
512
#endif
 
513
 
 
514
// Shut up annoying Visual C++ warnings:
 
515
#ifdef _MSC_VER
 
516
#pragma warning(disable: 4244 4309 4514 4710)
 
517
#endif
 
518
 
 
519
#endif