File: PushButton.cxx

package info (click to toggle)
oonsoo 1.2-8
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,604 kB
  • ctags: 422
  • sloc: cpp: 24,373; makefile: 82
file content (145 lines) | stat: -rw-r--r-- 4,145 bytes parent folder | download | duplicates (4)
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
///////////////////////////////////////////////////////////////////////////////
// $Id: PushButton.cxx,v 1.2 1996/01/06 05:09:40 bwmott Exp $
///////////////////////////////////////////////////////////////////////////////
//
// PushButton.hxx - Push button widget
//
//
// Bradford W. Mott
// Copyright (C) 1994
// December 12,1994
//
///////////////////////////////////////////////////////////////////////////////
// $Log: PushButton.cxx,v $
// Revision 1.2  1996/01/06 05:09:40  bwmott
// Changed all NULLs to 0
//
// Revision 1.1  1995/01/08 06:51:20  bmott
// Initial revision
//
///////////////////////////////////////////////////////////////////////////////

#include "PushButton.hxx"
#include "UIApplication.hxx"
#include "ContainerWidget.hxx"
#include "misc.hxx"

///////////////////////////////////////////////////////////////////////////////
// Constructor
///////////////////////////////////////////////////////////////////////////////
PushButton::PushButton(ContainerWidget *const parent, 
    const char *const widgetName,
    int x, int y, int width, int height, 
    const char* label, Command* pushCommand)
    : BasicWidget(parent, widgetName, x, y, width, height),
      myLabel(label),
      myCommand(pushCommand)
{
  // Create window for widget
  myWindow = XCreateSimpleWindow(application->display(), myParent->window(),
      x, y, width, height,
      0, 0, 0);

  // PushButtons have to respond to several event types
  XSelectInput(application->display(), myWindow, 
      ButtonPressMask | ExposureMask | EnterWindowMask | 
      LeaveWindowMask | ButtonReleaseMask );
}


///////////////////////////////////////////////////////////////////////////////
// Destructor
///////////////////////////////////////////////////////////////////////////////
PushButton::~PushButton()
{
  // Delete my command
  delete myCommand;
}

///////////////////////////////////////////////////////////////////////////////
// Update the graphical view of the push button
///////////////////////////////////////////////////////////////////////////////
void PushButton::updateView()
{
  // Setup GC and Font
  XFontStruct* fontInfo = application->font();

  XGCValues gcValues;
  gcValues.foreground = foreground();
  gcValues.background = background();
  gcValues.font = fontInfo->fid;
  GC gc = XCreateGC(application->display(), myWindow,
      GCForeground | GCBackground | GCFont, &gcValues); 

  Draw3DBorder(this, 3, Raised);
   
  // Center label in widget
  int x = (width() - XTextWidth(fontInfo, myLabel, strlen(myLabel)) ) / 2;
  int y = height()/2 - ((fontInfo->ascent+fontInfo->descent)/2) + fontInfo->ascent;

  XDrawString(application->display(), myWindow, gc,
    x, y, myLabel, strlen(myLabel));

  // Free the GC
  XFreeGC(application->display(), gc); 
}

///////////////////////////////////////////////////////////////////////////////
// Called whenever an event arrives for me (I override the default)
///////////////////////////////////////////////////////////////////////////////
void PushButton::handleEvent(XEvent* event)
{
  // Handle the event
  if (event->type == Expose)
  {
    updateView();
  }
  else if ((event->type == ButtonPress) && (event->xbutton.button == Button1))
  {
    // Flag to tell if the button was really pressed
    int pressed = 1;

    // Push the button in
    Draw3DBorder(this, 3, Sunken);

    // Track the mouse until the button is released
    int done = 0;
    while(!done)
    {
      XEvent event;
    
      XNextEvent(application->display(), &event);
      switch(event.type)
      {
        case EnterNotify:
          // Push the button in
          Draw3DBorder(this, 3, Sunken);
          pressed = 1;
          break;

        case LeaveNotify:
          // Pop the button back out
          Draw3DBorder(this, 3, Raised);
          pressed = 0;
          break;

        case ButtonRelease:
          // Pop the button back out
          Draw3DBorder(this, 3, Raised);

          done = 1;
          break;

        default:
          break;
      }
    }

    // Execute my command if the button was pressed
    if(pressed)
    {
      myCommand->execute(0);
    }
  } 
}