aboutsummaryrefslogtreecommitdiff
path: root/src/solaris/classes/sun/awt/motif/MComponentPeer.java
blob: 87f556c93d14ab17e210ad659393782e2d5d2c38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
/*
 * Copyright 1995-2008 Sun Microsystems, Inc.  All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Sun designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Sun in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 */

package sun.awt.motif;

import java.awt.*;
import java.awt.peer.*;
import java.awt.event.PaintEvent;
import java.awt.event.MouseEvent;
import java.awt.event.InputEvent;

import sun.awt.*;
import sun.awt.image.ToolkitImage;
import sun.awt.image.SunVolatileImage;
import java.awt.image.ImageProducer;
import java.awt.image.ImageObserver;
import java.awt.image.ColorModel;
import java.awt.image.VolatileImage;

import java.awt.dnd.DropTarget;
import java.awt.dnd.peer.DropTargetPeer;

import sun.java2d.SunGraphics2D;
import sun.java2d.SurfaceData;

import java.lang.reflect.Method;

import java.util.logging.*;

import sun.java2d.pipe.Region;


public /* REMIND: should not be public */
abstract class MComponentPeer implements ComponentPeer, DropTargetPeer, X11ComponentPeer {

    private static final Logger log = Logger.getLogger("sun.awt.motif.MComponentPeer");
    private static final Logger focusLog = Logger.getLogger("sun.awt.motif.focus.MComponentPeer");

    Component   target;
    long        pData;
    long        jniGlobalRef;
    protected X11GraphicsConfig graphicsConfig;
    SurfaceData surfaceData;
    int         oldWidth = -1;
    int         oldHeight = -1;

    private RepaintArea paintArea;

    boolean isLayouting = false;
    boolean paintPending = false;

    protected boolean disposed = false;
    private static int JAWT_LOCK_ERROR=0x00000001;
    private static int JAWT_LOCK_CLIP_CHANGED=0x00000002;
    private static int JAWT_LOCK_BOUNDS_CHANGED=0x00000004;
    private static int JAWT_LOCK_SURFACE_CHANGED=0x00000008;
    private int drawState = JAWT_LOCK_CLIP_CHANGED |
    JAWT_LOCK_BOUNDS_CHANGED |
    JAWT_LOCK_SURFACE_CHANGED;

    /* These are the enumerated types in awt_p.h*/
    static final int MOTIF_NA = 0 ;
    static final int MOTIF_V1 = 1 ;
    static final int MOTIF_V2 = 2 ;

    private Font font;
    private long backBuffer = 0;
    private VolatileImage xBackBuffer = null;

    static {
        initIDs();
    }

    /* initialize the fieldIDs of fields that may be accessed from C */
    private native static void initIDs();


    /* This will return the last state of a window. ie the specific
     * "gotcha" is that if you iconify a window its obscurity remains
     * unchanged. Current use of this is just in user-initiated scrolling.
     * If that use expands to more cases you may need to "and" this with
     * the value of the iconic state of a Frame.
     * Note that de-iconifying an X11 window DOES generate a new event
     * correctly notifying you of the new visibility of the window
     */
    public boolean isObscured() {

        Container container  = (target instanceof Container) ?
            (Container)target : target.getParent();

        if (container == null) {
            return true;
        }

        Container parent;
        while ((parent = container.getParent()) != null) {
            container = parent;
        }

        if (container instanceof Window) {
            MWindowPeer wpeer = (MWindowPeer)(container.getPeer());
            if (wpeer != null) {
                return (wpeer.winAttr.visibilityState !=
                        MWindowAttributes.AWT_UNOBSCURED);
            }
        }
        return true;
    }

    public boolean canDetermineObscurity() {
        return true;
    }

    abstract void create(MComponentPeer parent);
    void create(MComponentPeer parent, Object arg) {
        create(parent);
    }

    void EFcreate(MComponentPeer parent, int x){}

    native void pInitialize();
    native void pShow();
    native void pHide();
    native void pEnable();
    native void pDisable();
    native void pReshape(int x, int y, int width, int height);
    native void pDispose();
    native void pMakeCursorVisible();
    native Point pGetLocationOnScreen();
    native Point pGetLocationOnScreen2(Window win, MWindowPeer wpeer);
    native void pSetForeground(Color c);
    native void pSetBackground(Color c);
    private native void pSetFont(Font f);

    //Added for bug 4175560
    //Returns the native representation for the Color argument,
    //using the given GraphicsConfiguration.
    native int getNativeColor(Color clr, GraphicsConfiguration gc);

    // Returns the parent of the component, without invoking client
    // code. This must go through native code, because it invokes
    // private methods in the java.awt package, which we cannot
    // do from this package.
    static native Container getParent_NoClientCode(Component component);

    // Returns the parent of the component, without invoking client
    // code. This must go through native code, because it invokes
    // private methods in the java.awt package, which we cannot
    // do from this package.
    static native Component[] getComponents_NoClientCode(Container container);

    void initialize() {
        if (!target.isVisible()) {
            hide();
        }
        Color c;
        Font  f;
        Cursor cursor;

        pInitialize();

        if ((c = target.getForeground()) != null) {
            setForeground(c);
        }
        if ((c = target.getBackground()) != null) {
            setBackground(c);
        }
        if ((f = target.getFont()) != null) {
            setFont(f);
        }
        pSetCursor(target.getCursor());
        if (!target.isEnabled()) {
            disable();
        }
        Rectangle r = target.getBounds();
        reshape(r.x, r.y, r.width, r.height);
        if (target.isVisible()) {
            show();
        }

        surfaceData = graphicsConfig.createSurfaceData(this);
    }

    public void init(Component target, Object arg) {
        this.target = target;
        this.paintArea = new RepaintArea();

        Container parent = MToolkit.getNativeContainer(target);
        MComponentPeer parentPeer = (MComponentPeer) MToolkit.targetToPeer(parent);
        create(parentPeer, arg);

        initialize();
    }

    MComponentPeer(Component target, Object arg) {
        init(target, arg);
    }

    MComponentPeer() {}

    public void init(Component target) {
        this.target = target;
        this.paintArea = new RepaintArea();

        Container parent = MToolkit.getNativeContainer(target);
        MComponentPeer parentPeer = (MComponentPeer) MToolkit.targetToPeer(parent);
        create(parentPeer);

        if (parent != null && parent instanceof ScrollPane) {
            MScrollPanePeer speer = (MScrollPanePeer) parentPeer;
            speer.setScrollChild(this);
        }
        initialize();
    }

    MComponentPeer(Component target) {
        init(target);
    }

    protected void finalize() throws Throwable {
        dispose();
        super.finalize();
    }

    public void setForeground(Color c) {
        pSetForeground(c);
    }

    public void setBackground(Color c) {
        pSetBackground(c);
    }

    public void updateCursorImmediately() {
        MGlobalCursorManager.getCursorManager().updateCursorImmediately();
    }

    public void setFont(Font f) {
        ComponentPeer peer;
        if (f == null) {
            f = defaultFont;
        }
        pSetFont(f);
        if ( target instanceof Container ) {
            Container container = (Container) target;
            int count = container.getComponentCount();
            Component[] children = container.getComponents();
            for (int i=0; i<count; i++) {
                if ( children[i] != null ) {
/*
** note: recursion in the widget in pSetFont() has by now broken any
**       children with different Fonts - so fix now:
*/
                    peer = children[i].getPeer();
                    if (peer != null) {
                        Font rightFont = children[i].getFont();
                        if (!f.equals(rightFont)) {
                            peer.setFont(rightFont);
                        } else
                            if (children[i] instanceof Container) {
                                peer.setFont(f);
                            }
                    }
                }
            }
        }

        /*
         * Keep a reference to the java.awt.Font object in order to
         * preserve the XFontStructs which underlying widgets are using.
         * Save this AFTER changing the widgets in order to keep the
         * previous reference (if any) alive.
         */
        font = f;
    }


    public native void setTargetBackground(Color c);
    public native void pSetCursor(Cursor c);
    public native void pSetScrollbarBackground(Color c);
    public native void pSetInnerForeground(Color c);

    public boolean isFocusable() {
        return false;
    }

    public SurfaceData getSurfaceData() {
        return surfaceData;
    }

    public ColorModel getColorModel() {
        return graphicsConfig.getColorModel();
    }

    public ColorModel getColorModel(int transparency) {
        return graphicsConfig.getColorModel(transparency);
    }

    public int updatePriority() {
        return Thread.NORM_PRIORITY;
    }

    public void repaint(long tm, int x, int y, int width, int height) {
    }

    public void paint(Graphics g) {
        Dimension d = target.getSize();
        if (g instanceof Graphics2D ||
            g instanceof sun.awt.Graphics2Delegate) {
            // background color is setup correctly, so just use clearRect
            g.clearRect(0, 0, d.width, d.height);
        } else {
            // emulate clearRect
            g.setColor(target.getBackground());
            g.fillRect(0, 0, d.width, d.height);
            g.setColor(target.getForeground());
        }

        target.paint(g);
    }
    public void print(Graphics g) {
        Dimension d = target.getSize();
        if (g instanceof Graphics2D ||
            g instanceof sun.awt.Graphics2Delegate) {
            // background color is setup correctly, so just use clearRect
            g.clearRect(0, 0, d.width, d.height);
        } else {
            // emulate clearRect
            g.setColor(target.getBackground());
            g.fillRect(0, 0, d.width, d.height);
            g.setColor(target.getForeground());
        }

        target.print(g);
    }

    public void coalescePaintEvent(PaintEvent e) {
        Rectangle r = e.getUpdateRect();
        paintArea.add(r, e.getID());

        if (log.isLoggable(Level.FINEST)) {
            switch(e.getID()) {
              case PaintEvent.UPDATE:
                  log.log(Level.FINEST, "coalescePaintEvent: UPDATE: add: x = " +
                          r.x + ", y = " + r.y + ", width = " + r.width + ", height = " + r.height);
                  return;
              case PaintEvent.PAINT:
                  log.log(Level.FINEST, "coalescePaintEvent: PAINT: add: x = " +
                          r.x + ", y = " + r.y + ", width = " + r.width + ", height = " + r.height);
                  return;
            }
        }
    }

    native void nativeHandleEvent(AWTEvent e);

    /**
     * Returns whether or not this component should be given focus on mouse click.
     * Default implementation return whether or not this peer is "focusable"
     * Descendants might want to override it to extend/restrict conditions at which this
     * component should be focused by click (see MCanvasPeer and MPanelPeer)
     */
    protected boolean shouldFocusOnClick() {
        return isFocusable();
    }

    /**
     * Checks whether or not this component would be focused by native system if it would be allowed to do so.
     * Currently it checks that it displayable, visible, enabled and focusable.
     */
    static boolean canBeFocusedByClick(Component component) {
        if (component == null) {
            return false;
        } else {
            return component.isDisplayable() && component.isVisible() && component.isEnabled() && component.isFocusable();
        }
    }

    static Method requestFocusWithCause;

    static void callRequestFocusInWindow(Component target, CausedFocusEvent.Cause cause) {
        if (requestFocusWithCause == null) {
            requestFocusWithCause = SunToolkit.getMethod(Component.class, "requestFocusInWindow", new Class[] {CausedFocusEvent.Cause.class});
        }
        if (requestFocusWithCause != null) {
            try {
                requestFocusWithCause.invoke(target, new Object[] {cause});
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void handleEvent(AWTEvent e) {
        int id = e.getID();

        switch(id) {
          case PaintEvent.PAINT:
              // Got native painting
              paintPending = false;
              // Fallthrough to next statement
          case PaintEvent.UPDATE:
              // Skip all painting while layouting and all UPDATEs
              // while waiting for native paint
              if (!isLayouting && !paintPending) {
                  paintArea.paint(target,false);
              }
              return;
          case MouseEvent.MOUSE_PRESSED:
              if (target == e.getSource() && !((InputEvent)e).isConsumed() && shouldFocusOnClick()
                  && !target.isFocusOwner() && canBeFocusedByClick(target))
              {
                  callRequestFocusInWindow(target, CausedFocusEvent.Cause.MOUSE_EVENT);
              }
              break;
          default:
              break;
        }

        // Call the native code
        nativeHandleEvent(e);
    }

    /* New API for 1.1 */
    public Dimension getMinimumSize() {
        return target.getSize();
    }

    /* New API for 1.1 */
    public Dimension getPreferredSize() {
        return getMinimumSize();
    }

    // Do nothing for heavyweight implementation
    public void layout() {}

    public Rectangle getBounds() {
        return ((Component)target).getBounds();
    }

    public Object getTarget() {
        return target;
    }

    public java.awt.Toolkit getToolkit() {
        // XXX: bogus
        return Toolkit.getDefaultToolkit();
    }

    // fallback default font object
    final static Font defaultFont = new Font(Font.DIALOG, Font.PLAIN, 12);

    public synchronized Graphics getGraphics() {
        if (!disposed) {
            Component target = (Component) this.target;

            /* Fix for bug 4746122. Color and Font shouldn't be null */
            Color bgColor = target.getBackground();
            if (bgColor == null) {
                bgColor = SystemColor.window;
            }
            Color fgColor = target.getForeground();
            if (fgColor == null) {
                fgColor = SystemColor.windowText;
            }
            Font font = target.getFont();
            if (font == null) {
                font = defaultFont;
            }
            return new SunGraphics2D(surfaceData, fgColor, bgColor, font);
        }

        return null;
    }

    public Image createImage(ImageProducer producer) {
        return new ToolkitImage(producer);
    }

    public Image createImage(int width, int height) {
        return graphicsConfig.createAcceleratedImage(target, width, height);
    }

    public VolatileImage createVolatileImage(int width, int height) {
        return new SunVolatileImage(target, width, height);
    }

    public boolean prepareImage(Image img, int w, int h, ImageObserver o) {
        return getToolkit().prepareImage(img, w, h, o);
    }

    public int checkImage(Image img, int w, int h, ImageObserver o) {
        return getToolkit().checkImage(img, w, h, o);
    }

    public FontMetrics getFontMetrics(Font font) {
        return X11FontMetrics.getFontMetrics(font);
    }

    /*
     * Subclasses should override disposeImpl() instead of dispose(). Client
     * code should always invoke dispose(), never disposeImpl().
     */
    protected void disposeImpl() {
        SurfaceData oldData = surfaceData;
        surfaceData = null;
        oldData.invalidate();
        MToolkit.targetDisposedPeer(target, this);
        pDispose();
    }
    public final void dispose() {
        boolean call_disposeImpl = false;

        if (!disposed) {
            synchronized (this) {
                SunToolkit.awtLock();
                try {
                    if (!disposed) {
                        disposed = call_disposeImpl = true;
                    }
                } finally {
                    SunToolkit.awtUnlock();
                }
            }
        }

        if (call_disposeImpl) {
            disposeImpl();
        }
    }

    native static boolean processSynchronousLightweightTransfer(Component heavyweight, Component descendant,
                                                                boolean temporary, boolean focusedWindowChangeAllowed,
                                                                long time);
    public boolean requestFocus
    (Component lightweightChild, boolean temporary,
         boolean focusedWindowChangeAllowed, long time, CausedFocusEvent.Cause cause) {
        if (processSynchronousLightweightTransfer((Component)target, lightweightChild, temporary,
                                                  focusedWindowChangeAllowed, time)) {
            return true;
        } else {
            if (focusLog.isLoggable(Level.FINER)) {
                focusLog.log(Level.FINER, "Current native focused window " + getNativeFocusedWindow());
            }
            /**
             * The problems with requests in non-focused window arise because shouldNativelyFocusHeavyweight
             * checks that native window is focused while appropriate WINDOW_GAINED_FOCUS has not yet
             * been processed - it is in EventQueue. Thus, SNFH allows native request and stores request record
             * in requests list - and it breaks our requests sequence as first record on WGF should be the last focus
             * owner which had focus before WLF. So, we should not add request record for such requests
             * but store this component in mostRecent - and return true as before for compatibility.
             */
            Container parent = (target instanceof Container) ? ((Container)target) : (target.getParent());
            // Search for parent window
            while (parent != null && !(parent instanceof Window)) {
                parent = getParent_NoClientCode(parent);
            }
            if (parent != null) {
                Window parentWindow = (Window)parent;
                // and check that it is focused
                if (focusLog.isLoggable(Level.FINER)) {
                    focusLog.log(Level.FINER, "Parent window " + parentWindow);
                }
                if (!parentWindow.isFocused() && getNativeFocusedWindow() == parentWindow) {
                    // if it is not - skip requesting focus on Solaris
                    // but return true for compatibility.
                    return true;
                } else if (getNativeFocusedWindow() != parentWindow) {
                    WindowPeer wpeer = (WindowPeer)parentWindow.getPeer();
                    boolean res = wpeer.requestWindowFocus();
                    if (focusLog.isLoggable(Level.FINER)) {
                        focusLog.log(Level.FINER, "Requested window focus: " + res);
                    }
                    // If parent window can be made focused and has been made focused(synchronously)
                    // then we can proceed with children, otherwise we retreat.
                    if (!(res && parentWindow.isFocused())) {
                        focusLog.finer("Waiting for asynchronous processing of window focus request");
                        KeyboardFocusManagerPeerImpl.removeLastFocusRequest(target);
                        return false;
                    }
                }
            }
            return _requestFocus(lightweightChild, temporary, focusedWindowChangeAllowed, time, cause);
        }
    }

    native boolean _requestFocus
        (Component lightweightChild, boolean temporary,
         boolean focusedWindowChangeAllowed, long time, CausedFocusEvent.Cause cause);

    static native Window getNativeFocusedWindow();

    /*
     * Post an event to the event queue.
     */
    // NOTE: This method may be called by privileged threads.
    //       DO NOT INVOKE CLIENT CODE ON THIS THREAD!
    void postEvent(AWTEvent event) {
        MToolkit.postEvent(MToolkit.targetToAppContext(target), event);
    }

    /* Callbacks for window-system events to the frame
     *
     * NOTE: This method may be called by privileged threads.
     *       DO NOT INVOKE CLIENT CODE ON THIS THREAD!
     */
    void handleExpose(int x, int y, int w, int h) {
        if ( !ComponentAccessor.getIgnoreRepaint(target) ) {
            postEvent(new PaintEvent(target, PaintEvent.PAINT,
                                     new Rectangle(x, y, w, h)));
        }
    }

    /* Callbacks for window-system events to the frame
     *
     * NOTE: This method may be called by privileged threads.
     *       DO NOT INVOKE CLIENT CODE ON THIS THREAD!
     */
    void handleRepaint(int x, int y, int w, int h) {
        if ( !ComponentAccessor.getIgnoreRepaint(target) ) {
            postEvent(new PaintEvent(target, PaintEvent.UPDATE,
                                     new Rectangle(x, y, w, h)));
        }
    }

    /* Return the component's z-order position relative to
     * other peer'd siblings (don't count lightweight siblings
     * or siblings who don't yet have valid peers).
     *
     * NOTE: This method may be called by privileged threads.
     *       DO NOT INVOKE CLIENT CODE ON THIS THREAD!
     */
    public int getZOrderPosition_NoClientCode() {
        // SECURITY: use _NoClientCode() methods, because we may
        //           be running on a privileged thread
        Container p = getParent_NoClientCode(target);
        if (p != null) {
            // SECURITY: use _NoClientCode() methods, because we may
            //           be running on a privileged thread
            Component children[] = getComponents_NoClientCode(p);
            int i;
            int index = 0;
            for (i = 0; i < children.length; i++) {
                if (children[i] == target) {
                    return index;
                } else {
                    Object cpeer = MToolkit.targetToPeer(children[i]);
                    if (cpeer != null &&
                        !(cpeer instanceof java.awt.peer.LightweightPeer)) {
                        index++;
                    }
                }
            }
        }
        return -1;
    }

    /*
     * drawXXX() methods are used to print the native components by
     * rendering the Motif look ourselves.
     * ToDo(aim): needs to query native motif for more accurate color
     * information.
     */
    void draw3DOval(Graphics g, Color bg,
                    int x, int y, int w, int h, boolean raised) {
        Color c = g.getColor();
        Color shadow = bg.darker();
        Color highlight = bg.brighter();

        g.setColor(raised ? highlight : shadow);
        g.drawArc(x, y, w, h, 45, 180);
        g.setColor(raised ? shadow : highlight);
        g.drawArc(x, y, w, h, 225, 180);
        g.setColor(c);
    }
    void draw3DRect(Graphics g, Color bg,
                    int x, int y, int width, int height,
                    boolean raised) {
        Color c = g.getColor();
        Color shadow = bg.darker();
        Color highlight = bg.brighter();

        g.setColor(raised ? highlight : shadow);
        g.drawLine(x, y, x, y + height);
        g.drawLine(x + 1, y, x + width - 1, y);
        g.setColor(raised ? shadow : highlight);
        g.drawLine(x + 1, y + height, x + width, y + height);
        g.drawLine(x + width, y, x + width, y + height - 1);
        g.setColor(c);
    }
    void drawScrollbar(Graphics g, Color bg, int thickness, int length,
                       int min, int max, int val, int vis, boolean horizontal) {
        Color c = g.getColor();
        double f = (double)(length - 2*(thickness-1)) / Math.max(1, ((max - min) + vis));
        int v1 = thickness + (int)(f * (val - min));
        int v2 = (int)(f * vis);
        int w2 = thickness-4;
        int tpts_x[] = new int[3];
        int tpts_y[] = new int[3];

        if (length < 3*w2 ) {
            v1 = v2 = 0;
            if (length < 2*w2 + 2) {
                w2 = (length-2)/2;
            }
        } else  if (v2 < 7) {
            // enforce a minimum handle size
            v1 = Math.max(0, v1 - ((7 - v2)>>1));
            v2 = 7;
        }

        int ctr   = thickness/2;
        int sbmin = ctr - w2/2;
        int sbmax = ctr + w2/2;

        // paint the background slightly darker
        {
            Color d = new Color((int) (bg.getRed()   * 0.85),
                                (int) (bg.getGreen() * 0.85),
                                (int) (bg.getBlue()  * 0.85));

            g.setColor(d);
            if (horizontal) {
                g.fillRect(0, 0, length, thickness);
            } else {
                g.fillRect(0, 0, thickness, length);
            }
        }

        // paint the thumb and arrows in the normal background color
        g.setColor(bg);
        if (v1 > 0) {
            if (horizontal) {
                g.fillRect(v1, 3, v2, thickness-3);
            } else {
                g.fillRect(3, v1, thickness-3, v2);
            }
        }

        tpts_x[0] = ctr;        tpts_y[0] = 2;
        tpts_x[1] = sbmin;      tpts_y[1] = w2;
        tpts_x[2] = sbmax;      tpts_y[2] = w2;
        if (horizontal) {
            g.fillPolygon(tpts_y, tpts_x, 3);
        } else {
            g.fillPolygon(tpts_x, tpts_y, 3);
        }

        tpts_y[0] = length-2;
        tpts_y[1] = length-w2;
        tpts_y[2] = length-w2;
        if (horizontal) {
            g.fillPolygon(tpts_y, tpts_x, 3);
        } else {
            g.fillPolygon(tpts_x, tpts_y, 3);
        }

        Color highlight = bg.brighter();

        // // // // draw the "highlighted" edges
        g.setColor(highlight);

        // outline & arrows
        if (horizontal) {
            g.drawLine(1, thickness, length - 1, thickness);
            g.drawLine(length - 1, 1, length - 1, thickness);

            // arrows
            g.drawLine(1, ctr, w2, sbmin);
            g.drawLine(length - w2, sbmin, length - w2, sbmax);
            g.drawLine(length - w2, sbmin, length - 2, ctr);

        } else {
            g.drawLine(thickness, 1, thickness, length - 1);
            g.drawLine(1, length - 1, thickness, length - 1);

            // arrows
            g.drawLine(ctr, 1, sbmin, w2);
            g.drawLine(sbmin, length - w2, sbmax, length - w2);
            g.drawLine(sbmin, length - w2, ctr, length - 2);
        }

        // thumb
        if (v1 > 0) {
            if (horizontal) {
                g.drawLine(v1, 2, v1 + v2, 2);
                g.drawLine(v1, 2, v1, thickness-3);
            } else {
                g.drawLine(2, v1, 2, v1 + v2);
                g.drawLine(2, v1, thickness-3, v1);
            }
        }

        Color shadow = bg.darker();

        // // // // draw the "shadowed" edges
        g.setColor(shadow);

        // outline && arrows
        if (horizontal) {
            g.drawLine(0, 0, 0, thickness);
            g.drawLine(0, 0, length - 1, 0);

            // arrows
            g.drawLine(w2, sbmin, w2, sbmax);
            g.drawLine(w2, sbmax, 1, ctr);
            g.drawLine(length-2, ctr, length-w2, sbmax);

        } else {
            g.drawLine(0, 0, thickness, 0);
            g.drawLine(0, 0, 0, length - 1);

            // arrows
            g.drawLine(sbmin, w2, sbmax, w2);
            g.drawLine(sbmax, w2, ctr, 1);
            g.drawLine(ctr, length-2, sbmax, length-w2);
        }

        // thumb
        if (v1 > 0) {
            if (horizontal) {
                g.drawLine(v1 + v2, 2, v1 + v2, thickness-2);
                g.drawLine(v1, thickness-2, v1 + v2, thickness-2);
            } else {
                g.drawLine(2, v1 + v2, thickness-2, v1 + v2);
                g.drawLine(thickness-2, v1, thickness-2, v1 + v2);
            }
        }
        g.setColor(c);
    }

    public String toString() {
        return getClass().getName() + "[" + target + "]";
    }

    /* New 1.1 API */
    public void setVisible(boolean b) {
        if (b) {
            Dimension s = target.getSize();
            oldWidth = s.width;
            oldHeight = s.height;
            pShow();
        } else {
            pHide();
        }
    }

    /* New 1.1 API */
    public void setEnabled(boolean b) {
        if (b) {
            pEnable();
        } else {
            pDisable();
        }
    }

    /* New 1.1 API */
    public Point getLocationOnScreen() {
        synchronized (target.getTreeLock()) {
            Component comp = target;
            while (comp != null && !(comp instanceof Window)) {
                comp = getParent_NoClientCode(comp);
            }

            // applets, embedded, etc - translate directly
            if (comp == null || comp instanceof sun.awt.EmbeddedFrame) {
                return pGetLocationOnScreen();
            }

            MWindowPeer wpeer = (MWindowPeer)(MToolkit.targetToPeer(comp));
            if (wpeer == null) {
                return pGetLocationOnScreen();
            }
            return pGetLocationOnScreen2((Window)comp, wpeer);
        }
    }

    public int serialNum = 0;

    /* Returns the native paint should be posted after setting new size
     */
    public boolean checkNativePaintOnSetBounds(int width, int height) {
        return (width != oldWidth) || (height != oldHeight);
    }

    void setBounds(int x, int y, int width, int height) {
        setBounds(x, y, width, height, SET_BOUNDS);
    }

    /* New 1.1 API */
    public void setBounds(int x, int y, int width, int height, int op) {
        if (disposed) return;

        Container parent = getParent_NoClientCode(target);

        // Should set paintPending before reshape to prevent
        // thread race between PaintEvent and setBounds
        // This part of the 4267393 fix proved to be unstable under solaris,
        // dissabled due to regressions 4418155, 4486762, 4490079
        paintPending = false; //checkNativePaintOnSetBounds(width, height);

        // Note: it would be ideal to NOT execute this if it's
        // merely a Move which is occurring.
        if (parent != null && parent instanceof ScrollPane) {
            MScrollPanePeer speer = (MScrollPanePeer)parent.getPeer();
            if (!speer.ignore) {
                pReshape(x, y, width, height);
                speer.childResized(width, height);
            }
        } else {
            pReshape(x, y, width, height);
        }

        if ((width != oldWidth) || (height != oldHeight)) {
            SurfaceData oldData = surfaceData;
            if (oldData != null) {
                surfaceData = graphicsConfig.createSurfaceData(this);
                oldData.invalidate();
            }
            oldWidth = width;
            oldHeight = height;
        }
        validateSurface(width, height);
        serialNum++;
    }

    void validateSurface(int width, int height) {
        SunToolkit.awtLock();
        try {
            if (!disposed && (width != oldWidth || height != oldHeight)) {
                SurfaceData oldData = surfaceData;
                if (oldData != null) {
                    surfaceData = graphicsConfig.createSurfaceData(this);
                    oldData.invalidate();
                }
                oldWidth = width;
                oldHeight = height;
            }
        } finally {
            SunToolkit.awtUnlock();
        }
    }

    public void beginValidate() {
    }

    native void restoreFocus();

    public void endValidate() {
        restoreFocus();
    }

    public void beginLayout() {
        // Skip all painting till endLayout
        isLayouting = true;
    }

    public void endLayout() {
        if (!paintPending && !paintArea.isEmpty() &&
            !((Component)target).getIgnoreRepaint()) {
            // if not waiting for native painting repaint damaged area
            postEvent(new PaintEvent((Component)target, PaintEvent.PAINT,
                                     new Rectangle()));
        }
        isLayouting = false;
    }

    /**
     * DEPRECATED:  Replaced by setVisible(boolean).
     */
    public void show() {
        setVisible(true);
    }

    /**
     * DEPRECATED:  Replaced by setVisible(boolean).
     */
    public void hide() {
        setVisible(false);
    }

    /**
     * DEPRECATED:  Replaced by setEnabled(boolean).
     */
    public void enable() {
        setEnabled(true);
    }

    /**
     * DEPRECATED:  Replaced by setEnabled(boolean).
     */
    public void disable() {
        setEnabled(false);
    }

    /**
     * DEPRECATED:  Replaced by setBounds(int, int, int, int).
     */
    public void reshape(int x, int y, int width, int height) {
        setBounds(x, y, width, height);
    }

    /**
     * DEPRECATED:  Replaced by getMinimumSize().
     */
    public Dimension minimumSize() {
        return getMinimumSize();
    }

    /**
     * DEPRECATED:  Replaced by getPreferredSize().
     */
    public Dimension preferredSize() {
        return getPreferredSize();
    }

    /**
     *
     */

    public void addDropTarget(DropTarget dt) {
        if (MToolkit.useMotifDnD()) {
            addNativeDropTarget(dt);
        } else {
            Component comp = target;
            while(!(comp == null || comp instanceof java.awt.Window)) {
                comp = getParent_NoClientCode(comp);
            }

            if (comp instanceof Window) {
                MWindowPeer wpeer = (MWindowPeer)(comp.getPeer());
                if (wpeer != null) {
                    wpeer.addDropTarget();
                }
            }
        }
    }

    /**
     *
     */

    public void removeDropTarget(DropTarget dt) {
        if (MToolkit.useMotifDnD()) {
            removeNativeDropTarget(dt);
        } else {
            Component comp = target;
            while(!(comp == null || comp instanceof java.awt.Window)) {
                comp = getParent_NoClientCode(comp);
            }

            if (comp instanceof Window) {
                MWindowPeer wpeer = (MWindowPeer)(comp.getPeer());
                if (wpeer != null) {
                    wpeer.removeDropTarget();
                }
            }
        }
    }

    public void notifyTextComponentChange(boolean add){
        Container parent = getParent_NoClientCode(target);
        while(!(parent == null ||
                parent instanceof java.awt.Frame ||
                parent instanceof java.awt.Dialog)) {
            parent = getParent_NoClientCode(parent);
        }

        if (parent instanceof java.awt.Frame ||
            parent instanceof java.awt.Dialog) {
            if (add)
                ((MInputMethodControl)parent.getPeer()).addTextComponent((MComponentPeer)this);
            else
                ((MInputMethodControl)parent.getPeer()).removeTextComponent((MComponentPeer)this);
        }
    }

    native void addNativeDropTarget(DropTarget dt);

    native void removeNativeDropTarget(DropTarget dt);

    public GraphicsConfiguration getGraphicsConfiguration() {
        GraphicsConfiguration ret = graphicsConfig;
        if (ret == null) {
            ret = target.getGraphicsConfiguration();
        }
        return ret;
    }

    // Returns true if we are inside begin/endLayout and
    // are waiting for native painting
    public boolean isPaintPending() {
        return paintPending && isLayouting;
    }

    public boolean handlesWheelScrolling() {
        return false;
    }

    /**
     * The following multibuffering-related methods delegate to our
     * associated GraphicsConfig (X11 or GLX) to handle the appropriate
     * native windowing system specific actions.
     */

    private native long getWindow(long pData);

    public long getContentWindow() {
        return getWindow(pData);
    }

    public void createBuffers(int numBuffers, BufferCapabilities caps)
      throws AWTException
    {
        backBuffer = graphicsConfig.createBackBuffer(this, numBuffers, caps);
        xBackBuffer = graphicsConfig.createBackBufferImage(target,
                                                           backBuffer);
    }

    public void flip(int x1, int y1, int x2, int y2,
                     BufferCapabilities.FlipContents flipAction)
    {
        if (backBuffer == 0) {
            throw new IllegalStateException("Buffers have not been created");
        }
        graphicsConfig.flip(this, target, xBackBuffer,
                            x1, y1, x2, y2, flipAction);
    }

    public Image getBackBuffer() {
        if (backBuffer == 0) {
            throw new IllegalStateException("Buffers have not been created");
        }
        return xBackBuffer;
    }

    public void destroyBuffers() {
        graphicsConfig.destroyBackBuffer(backBuffer);
        backBuffer = 0;
        xBackBuffer = null;
    }

    /**
     * @see java.awt.peer.ComponentPeer#isReparentSupported
     */
    public boolean isReparentSupported() {
        return false;
    }

    /**
     * @see java.awt.peer.ComponentPeer#reparent
     */
    public void reparent(ContainerPeer newNativeParent) {
        throw new UnsupportedOperationException();
    }

    /**
     * Applies the shape to the native component window.
     * @since 1.7
     */
    public void applyShape(Region shape) {
    }

}